Getting Client IP Address

Hey guys!

I've been trying to figure out a way to get the IP of a client when using the Ethernet library, I've been looking through w1500.h and w1500.cpp and have seen some stuff that might be used to accomplish this, although I can't seem to find where a lot of the functions are declared (such as writeGAR).

Can anyone point me in the right direction?

Thanks,
Aaron

Well, I just stumbled onto this. I took a wild guess and it looks like it may work:

void W5100Class::getRemoteIPAddress(SOCKET s, uint8_t *_addr) {
  readSnDIPR(s, _addr);
}

Update: For those interested, you can find my modifications here: GitHub - ablakely/Ethernet-ablakely: My modifications to the Arduino Ethernet lib :smiley:

Some test code that has a remote IP function in it.

//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte rip[4];
//byte rip[] = {0,0,0,0};
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 
client.getRemoteIP(rip);
for (int bcount= 0; bcount < 4; bcount++)
     { 
        Serial.print(rip[bcount], DEC); 
        if (bcount<3) Serial.print(".");
     } 

Serial.println();
          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); 

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

If you're talking about my library modifications, this:

client.getRemoteIP(rip);
for (int bcount= 0; bcount < 4; bcount++)
     { 
        Serial.print(rip[bcount], DEC); 
        if (bcount<3) Serial.print(".");
     } 
Serial.println();

should be:

uint8_t socket = client.getSocket();
byte *rip[4];

client.getRemoteIP(socket, rip);
for (int bcount = 0; bcount < 4; bcount++)
{
    Serial.print(rip[bcount], DEC);
    Serial.print('.');
}
Serial.println()

Although, I might be able to make it work like the code you posted.

I added some code so that:

client.getRemoteIP(rip)

and

client.getRemoteIP(socket, rip)

are both valid.

Although, I might be able to make it work like the code you posted.

Although I haven't tested my posted code to any extent, it seems to work without a library modification (with the possible exception of the below mod I have done).

http://code.google.com/p/arduino/issues/detail?id=605

Well yesterday I spent 2 hours looking through the code before trying to make my own and found nothing. I downloaded the current IDE and libraries yesterday morning then had to use the w5100.cpp and w5100.h off the github because the one I got from the arduino site would hang on Ethernet.begin(). But anyways I think I have the recent code and what I could tell there was no functions to do what I wanted so I wrote my own.

I spend a lot of time to find a solution of error

no matching function for call to 'EthernetClient::getRemoteIP(byte* [4])'

The right way is to replace the line of code byte *rip[4];
with

uint8_t rip[4]; 
or  uint8_t rip[] = {0,0,0,0};

Really thanks for the new Ethernet library, but why is the example wrong?

So there is an error on the 164 line in file EthernetClient.cpp too, the line must be

uint8_t *EthernetClient::getRemoteIP(uint8_t *remoteIP)

Post that might be of interest.

http://forum.arduino.cc/index.php/topic,82416.0.html