Some Noob Student
Some Noob Student

Reputation: 14584

How to differentiate ethernet from the others?

It was suggested in an answer here, Get the IP address of the machine, one could use getifaddrs() to obtain the IP address of the machine the program was running on, which worked great :D:D

However, running the same program on two different systems, one displayed

SERVER_ADDRESS lo 127.0.0.1
SERVER_ADDRESS eth0 129.xxx.xxx.xxx
SERVER_ADDRESS virbr0 192.zzz.zzz.1

while the other displayed

SERVER_ADDRESS lo0 127.0.0.1
SERVER_ADDRESS en0 192.yyy.yyy.yyy

I was going to use strcmp to differentiate ethernet, but now I realized it doesn't work across systems since different strings may be printed out.

Is there a function (or better way) to check whether or not an ifa_name is ethernet or not?

Upvotes: 0

Views: 3994

Answers (3)

Nitinkumar Ambekar
Nitinkumar Ambekar

Reputation: 1009

/* Output:
Name: 'eth0' Addr: 'xxx.xxx.xxx.xxx'
Name: 'eth0:0' Addr: 'xxx.xxx.xxx.xxx'
*/

    struct ifaddrs *ifaddr;
    char ip[255];

    if (getifaddrs(&ifaddr) == -1)
       {
          //sprintf(ip[0], "%s", strerror(errno));
       }
       else
       {
          struct ifaddrs *ifa;
          int i = 0, family;
          for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
          {
             if (ifa->ifa_addr == NULL)
             {
                continue;
             }
             family = ifa->ifa_addr->sa_family;
             if (family == AF_INET || family == AF_INET6)
             {
                if(!(ifa->ifa_flags & IFF_LOOPBACK) && (family == AF_INET) && getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0)
                {
                   printf("Name: '%s' Addr: '%s'\n", ifa->ifa_name, ip);
                }
             }
          }
       }
       freeifaddrs(ifaddr);

Upvotes: 1

alk
alk

Reputation: 70981

To (again) state this explicitly: All addresses of the 127.0.0.0/255.0.0.0 net (range) as there are the addresses from 127.0.0.0 to 127.255.255.255, are defined to be treated as local loopback addresses. Data addressed to such will not leave the local machine.

Anyhow as you are already using getifaddrs() live is easy .. - just test the member ifa_flags of the structure(s) struct ifaddrs returned for IFF_LOOPBACK like so:

#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if.h> /* for IFF_LOOPBACK */

...

struct ifaddrs * pIfAddrs = NULL;

if (!getifaddrs(&pIfAddrs)) {
  /* Test if the first interface is looping back to the local host. */
  int iIsLoopBack = (0 != (pIfAddrs->ifa_flags & IFF_LOOPBACK)); 
  ...
}

...

/* clean up */
if (pIfAddrs) {
  freeifaddrs(pIfAddrs);
  pIfAddrs = NULL;
}

...

Upvotes: 7

Alex Gray
Alex Gray

Reputation: 16473

You're likely to run into more than just that issue.. say for example there are multiple NICs, vLANs, WANS that look like LANS, and vice-versa, etc.

What is known? 127.X.X.X Throw out that result and you've got your non-loopbacks.

If you want to know if the address is private or not.. you'll then have to go down this road.

Upvotes: 2

Related Questions