3 Replies Latest reply on Jan 4, 2015 3:55 AM by AlphaCHarly

    EthernetClient.remoteIP() function

    AlphaCHarly

      Folks,

      I am working on a weather web server and I would like to record all the remote IPs requesting services to my web server.

      Using the UNO or MEGA boards I would a modification on the Arduino Forum, for the EthernetClient.cpp model where implement the client.getRemoteIP(rip) function that return the IP of the client requesting services to the web server.

      There is a Ethernetudp.remoteIP() function, but I needed on the client class.

      I wonder if some of you has implemented something similar.

      Thanks in advance.

      AC/.  

        • 1. Re: EthernetClient.remoteIP() function
          Intel_Alvarado

          Hi AlphaCHarly,

           

          As I understand from your post, you want to alter your Arduino EthernetClient.cpp file and add it the functionality of Ethernetudp.remoteIP() function http://arduino.cc/en/Reference/EthernetUDPRemoteIP is this correct?  Are you using an Edison board or only an Arduino UNO or MEGA? Have you already altered any .cpp files? If so what changes have you made?

           

          Regards

          Sergio

          • 2. Re: EthernetClient.remoteIP() function
            AlphaCHarly

            The modification to the EthernetClient.cpp (for the UNO and Mega with the Ethernet shield ) was found on the Arduino Forum and it is quite used by the community as it is mentioned there.

            I would like to have similar functionality on my Galileo/Edison implementations.

            • 3. Re: EthernetClient.remoteIP() function
              AlphaCHarly

              Folks,

              Finally I was able to answer to myself, let me share my findings.

              The EthernetClient instance contains a pointer to the socket: "_sock" that is public, so it can be accessed,

              so once that you have that socket you can use the whole set of inet libraries to the the remote IP, the remote hostname, etc,...

               

              See some excerpts of my code.

              AC/.

               

              -----------------------------

               

              #include <Dhcp.h>

              #include <Dns.h>

              #include <Ethernet.h>

              #include <EthernetClient.h>

              #include <EthernetServer.h>

              #include <EthernetUdp.h>

               

              #include <stdio.h>

              #include <errno.h>

              #include <netdb.h>

              #include <sys/types.h>

              #include <sys/socket.h>

              #include <netinet/in.h>

              #include <arpa/inet.h>

               

              #include <Arduino.h>

               

               

              char _ipstr[INET6_ADDRSTRLEN];    // addr in format xxx.yyy.zzz.aaa

               

               

              byte __remoteIP[4] = {0,0,0,0};   // IP address in bin format 

               

               

              void

              PrintIP (byte * ip)                // just print an IP for debugging purposes

              {

                Serial.print("\nPrintIP: ");

                for (int i=0; i<4; i++)

                  {

                    Serial.print(ip[i], DEC);

                    if (i<3) Serial.print(".");

                  }

                Serial.println();

              }

               

               

              char *

              RemoteIP( EthernetClient client )  // return the remote IP addr of a connected client           

              {

                  memset(_ipstr, 0, sizeof(_ipstr));

                  if (Debug) Serial.print("RemoteIP: ");

                  if (client)

                  {

                      socklen_t len;

                      struct sockaddr_storage addr;

                      int so=client._sock;

                      memset(_ipstr, 0, sizeof(_ipstr));

                      if (so)

                      { 

                          if (Debug) Serial.print(" Socket: ");

                          if (Debug) Serial.print(so,HEX);

                          if (Debug) Serial.print(" ");

                          getpeername(so, (struct sockaddr*)&addr, &len);

                          if (addr.ss_family == AF_INET)

                          {    

                              struct sockaddr_in *s = (struct sockaddr_in *)&addr;    

                              inet_ntop(AF_INET, &s->sin_addr, _ipstr, sizeof _ipstr);

                          }

                      }

                      if (Debug) Serial.print(_ipstr);

                  }

                  return (_ipstr);

              }

               

              byte *

              getRemoteIP( EthernetClient client )  // return the remote IP addr of a connected client on bin format.

              {

                memset(__remoteIP, 0, sizeof(__remoteIP));

                if (Debug) Serial.print("getRemoteIP: ");

                if (client)

                {

                    char *ipa=RemoteIP(client);

                    // Serial.print("getRemoteIP:"); Serial.println(ipa);

                    for (int i=0; i<4; i++)

                    {

                      __remoteIP[i] = (char) atoi(ipa);

                      while (i < 3 && *ipa++ != '.');

                    }

                }

                if (Debug) PrintIP(__remoteIP);

                return (__remoteIP);

              }

               

              void

              printremoteIP (int s)

              {

              // assume s is a connected socket

                socklen_t len;

                struct sockaddr_storage addr;

               

                int port;

                len = sizeof addr;

                getpeername(s, (struct sockaddr*)&addr, &len); // deal with both IPv4 and IPv6:

                if (addr.ss_family == AF_INET)

                {    

                      struct sockaddr_in *s = (struct sockaddr_in *)&addr;

                      port = ntohs(s->sin_port);    

                      inet_ntop(AF_INET, &s->sin_addr, _ipstr, sizeof _ipstr);

                }

               

                Serial.print("\nPeer IP address: ");

                Serial.println(_ipstr);

                Serial.print("Peer port      : ");

                Serial.println(port);

              }

               

              char *

              printhostname(char * ipa)

              {

                struct hostent *he;

                struct in_addr ipv4addr;

                if (Debug) Serial.print("Print Remote hostname: ");

                if (Debug) Serial.println(ipa);

                inet_pton(AF_INET, ipa, &ipv4addr);

                he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);

                Serial.print  ("Remote Host name: ");

                Serial.println(he->h_name);

                return(he->h_name);

              }