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

前端 未结 6 857
遥遥无期
遥遥无期 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条回答
  •  囚心锁ツ
    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

提交回复
热议问题