How to enumerate all IP addresses attached to a machine, in POSIX C?

前端 未结 6 860
遥遥无期
遥遥无期 2021-01-03 03:25

Background:

I\'m writing a daemon that makes outgoing TCP/IP connections. It will be running on machines with multiple (non-loopback) IP addresses.

相关标签:
6条回答
  • How do I get a list of all the IP addresses a machine can make outgoing (i.e. to any other computer) connections on? Given a list of all IP addresses, how would I filter out loopback addresses?

    Look at the source code of lsof and netstat. You'll see it involves traversing kernel memory structures, not just making system calls.

    0 讨论(0)
  • 2021-01-03 03:58

    Here's my proof of concept code using caskey's accepted answer, for posterity's sake:

    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <net/if.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    static const char * flags(int sd, const char * name)
    {
        static char buf[1024];
    
        static struct ifreq ifreq;
        strcpy(ifreq.ifr_name, name);
    
        int r = ioctl(sd, SIOCGIFFLAGS, (char *)&ifreq);
        assert(r == 0);
    
        int l = 0;
    #define FLAG(b) if(ifreq.ifr_flags & b) l += snprintf(buf + l, sizeof(buf) - l, #b " ")
        FLAG(IFF_UP);
        FLAG(IFF_BROADCAST);
        FLAG(IFF_DEBUG);
        FLAG(IFF_LOOPBACK);
        FLAG(IFF_POINTOPOINT);
        FLAG(IFF_RUNNING);
        FLAG(IFF_NOARP);
        FLAG(IFF_PROMISC);
        FLAG(IFF_NOTRAILERS);
        FLAG(IFF_ALLMULTI);
        FLAG(IFF_MASTER);
        FLAG(IFF_SLAVE);
        FLAG(IFF_MULTICAST);
        FLAG(IFF_PORTSEL);
        FLAG(IFF_AUTOMEDIA);
        FLAG(IFF_DYNAMIC);
    #undef FLAG
    
        return buf;
    }
    
    int main(void)
    {
        static struct ifreq ifreqs[32];
        struct ifconf ifconf;
        memset(&ifconf, 0, sizeof(ifconf));
        ifconf.ifc_req = ifreqs;
        ifconf.ifc_len = sizeof(ifreqs);
    
        int sd = socket(PF_INET, SOCK_STREAM, 0);
        assert(sd >= 0);
    
        int r = ioctl(sd, SIOCGIFCONF, (char *)&ifconf);
        assert(r == 0);
    
        for(int i = 0; i < ifconf.ifc_len/sizeof(struct ifreq); ++i)
        {
            printf("%s: %s\n", ifreqs[i].ifr_name, inet_ntoa(((struct sockaddr_in *)&ifreqs[i].ifr_addr)->sin_addr));
            printf(" flags: %s\n", flags(sd, ifreqs[i].ifr_name));
        }
    
        close(sd);
    
        return 0;
    }
    

    Works like a charm!

    0 讨论(0)
  • 2021-01-03 03:58

    You can get the interface info required a couple of ways including calling ioctl() with the SIOCGIFCONF option and looping through the returned structures to get the interface address info.

    Given a list of all IP addresses, how would I filter out loopback addresses?

    See ifreq struct in caskey's answer. You can determine the loopback (properly) with:

    if (ifru_flags & IFF_LOOPBACK) 
    

    Constants are in if.h

    0 讨论(0)
  • 2021-01-03 03:59

    Some answers to side questions:

    • Adding multiple IPs to a device can be done with aliasing. Linux creates devices named like eth0:0 when you do this.

      ifconfig eth0:0 10.0.0.1

    • Having the same IP under multiple devices can be done with channel bonding/link aggregation.

    0 讨论(0)
  • 2021-01-03 04:01

    This can only be done in an operating system dependent fashion. You could try parsing the output of 'iptables', but the right answer for linux is to use ioctl.

    SIOCGIFCONF  takes  a  struct  ifconf *.  The ifc_buf field points to a
          buffer of length ifc_len bytes, into which the kernel writes a list of
          type struct ifreq [].
    

    The struct ifreq is documented in linux/if.h:

    struct ifreq 
    {
    #define IFHWADDRLEN     6
            union
            {
                    char    ifrn_name[IFNAMSIZ];            /* if name, e.g. "en0" */
            } ifr_ifrn;
    
            union {
                    struct  sockaddr ifru_addr;
                    struct  sockaddr ifru_dstaddr;
                    struct  sockaddr ifru_broadaddr;
                    struct  sockaddr ifru_netmask;
                    struct  sockaddr ifru_hwaddr;
                    short   ifru_flags;
                    int     ifru_ivalue;
                    int     ifru_mtu;
                    struct  ifmap ifru_map;
                    char    ifru_slave[IFNAMSIZ];   /* Just fits the size */
                    char    ifru_newname[IFNAMSIZ];
                    void *  ifru_data;
                    struct  if_settings ifru_settings;
            } ifr_ifru;
    };
    

    As you can see, it contains the address information you desire.

    0 讨论(0)
  • 2021-01-03 04:07

    are you sure you are using gethostname()/gethostbyname() correctly? check out here, the only problem I see with doing this is that it's possible that a domain name has multiple ip addresses mapped to it. If that's the case then there's no way of knowing what the ip address belonging to the local machine is

    0 讨论(0)
提交回复
热议问题