-1

Found this "Remote IP of TCP Socket" at: https://e2e.ti.com/support/wireless_connectivity/simplelink_wifi_cc3000/f/851/t/380635

I am trying to log the remote ip address of a client. Using Ethernet, I have accomplished remote ip address logging. Trying to do the same with the Adafruit CC3000 library.

Answer provided; does not respond every time with the correct "client.IP". When it is wrong; client.IP is always "client.IP: 0.0.0.0.0" which occurs more frequently than the correct "client.IP" of my computer. Why is does this happen? Is there a solution? Answer is on the right track; need additional help, so client.IP is correct every time.

Edit: File modified is Adafruit_CC3000_Server.cpp Only change was to the clientIndex value. Will this cause an impact on client connecting? File is contained in "Arduino Stack Exchange.zip" linked in this post and is located in the Adafruit_CC3000 folder of the zip. Changes are commented.

Modified files with client ip returned. https://drive.google.com/open?id=0Byo8QWKyqAT9TUt4d0FteENQS2s CC3000.zip

William

3
  • As a guess one of three things may be happening: you are trying to get an address without a valid connection, the address was never saved, or it was saved but overwritten before you try to obtain it. You may need to figure out how to instrument or debug the code to determine which. Commented Oct 18, 2016 at 18:05
  • I found that if I set clientIndex = 2; every time I get correct client ip address. Are there any adverse effects to setting clientIndex equal to 2? What happens to other clientIndex values? Commented Oct 18, 2016 at 20:23
  • Where exactly did you make this change? Commented Oct 18, 2016 at 21:33

1 Answer 1

1

This function is not built-in so you'll have to modify the library slightly. This is one way to do it, I think:

  • Navigate to the class Adafruit_CC3000_ClientRef in the file Adafruit_CC3000_Server.h. In the class definition, just before the private methods and attributes are defined, you will add a new public attribute as follows:

    uint8_t ip_addr[4];  // assuming a typical IPv4 address
    

    It will hold the IP address of each ClientRef instance as the connection is accepted.

  • Go to Adafruit_CC3000_Server::acceptNewConnections() near the end of Adafruit_CC3000_Server.cpp. A few lines will be added to get the IP address from the CC3000 and store it temporarily in the appropriate struct:

    static sockaddr tSocketClientAddr;  // file scope
    static socklen_t addr_len;
    
    // Accept new connections and update the connected clients.
    bool Adafruit_CC3000_Server::acceptNewConnections() {
      bool newClientCreated = false;
      // For any unconnected client, see if new connections are pending and accept
      // them as a new client.
      for (int i = 0; i < MAX_SERVER_CLIENTS; ++i) {
        if (!_clients[i].connected()) {
          // Note: Because the non-blocking option was set for the listening
          // socket this call will not block and instead return SOC_IN_PROGRESS (-2) 
          // if there are no pending client connections. Also, the address of the 
          // connected client is not needed, so those parameters are set to NULL.
          cc3k_int_poll();
          addr_len = sizeof(tSocketClientAddr);
          int soc = accept(_listenSocket, &tSocketClientAddr, &addr_len);
          if (soc > -1) {
            _clients[i] = Adafruit_CC3000_Client(soc);
            newClientCreated = true;
          }
          // else either there were no sockets to accept or an error occured.
        }
      }
      return newClientCreated;
    }
    
  • You'll also edit the getClientRef() method in Adafruit_CC3000_Server.cpp:

    Adafruit_CC3000_ClientRef Adafruit_CC3000_Server::getClientRef(int8_t clientIndex) {
      if (clientIndex != -1) {
        Adafruit_CC3000_ClientRef newClient =  Adafruit_CC3000_ClientRef(&_clients[clientIndex]);
        // at least 2 (family) + 2 (port) + 4 (ip_addr) bytes expected
        // also check if address is standard ipv4
        if ((addr_len >= 8) && (tSocketClientAddr.sa_family == AF_INET)){
           // copy address into array; in network byte order  
           memcpy(newClient.ip_addr, &(tSocketClientAddr.sa_data[2]), 4);
        }
        return newClient;
      }
    
      // Couldn't find a client ready to read, so return a client that is not 
      // connected to signal no clients are available for reading (convention
      // used by the Ethernet library).
      return Adafruit_CC3000_ClientRef(NULL);
    }
    

    Save all the files.

  • Now the address is stored in an array for each client and can be accessed from your sketch. For example, you can access the address in your sketch like this:

    In the loop() of the the HttpServer example:

     Adafruit_CC3000_ClientRef client = httpServer.available();
     if (client) {
       Serial.println(F("Client connected."));
       for (int i = 0; i < 3; i++){
         Serial.print(client.ip_addr[i]);
         Serial.print('.');
       }
       Serial.println(client.ip_addr[3]); 
       // other code
     }
    

Completely untested. Good luck.

5
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented May 22, 2016 at 20:44
  • @NickGammon by moving the followup you have basically broken this question and answer - it doesn't work, but the history to understand it isn't here either, which breaks the whole everything should be right here, not in external references spirit of stack exchange. Commented Oct 18, 2016 at 18:03
  • @ChrisStratton - what are you talking about? What followup? You mean the comments? Commented Oct 18, 2016 at 20:11
  • Yes the comments, which are the way people are supposed to respond to answers that almost but not completely work. How else would this question be resolved - start over from scratch with no benefit from the effort already expended? Just give up and close it? Commented Oct 18, 2016 at 21:33
  • The "effort already expended" is safely archived in chat, @ChrisStratton. See the link Nick posted above. If you care to refer to it when editing this answer or posting your own, you'll find it readily accessible. Commented Oct 18, 2016 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.