Quantcast
Channel: Intel Communities: Message List
Viewing all articles
Browse latest Browse all 17767

Re: EthernetClient.remoteIP() function

$
0
0

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);

}


Viewing all articles
Browse latest Browse all 17767

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>