Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enumerate all IP addresses attached to a machine, in POSIX C?
    primarykey
    data
    text
    <p><strong>Background:</strong></p> <p>I'm writing a daemon that makes outgoing TCP/IP connections. It will be running on machines with multiple (non-loopback) IP addresses. I'd like the users to be able to specify, in the daemon's config file, which IP address(es) to use for outgoing connections, or <code>*</code> to use all.</p> <p>The addresses will be used in a rotation, each connection going out from the IP address used least recently. This behavior is important, as is <code>*</code> being a replacement for "all" so daemons running on multiple machines can point to the same config file on a fileshare, and have each use its own set of IP addresses.</p> <p><strong>Problem:</strong></p> <p>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?</p> <p>I'm in C, and if possible I'd like to use POSIX only, but the daemon will probably only ever run on Linux boxes, so I'd accept a Linux-centric answer.</p> <p>Each IP address will be available on exactly one (possibly virtual) network device and vice versa, so a way to enumerate network devices and get associated IP addresses would also suffice, though I wouldn't really be happy about it. (Side questions: Is it even possible to associate multiple IP addresses with a single device? How 'bout the same IP under multiple devices? Not important.)</p> <p><strong>Insufficient Solutions:</strong></p> <ul> <li><code>gethostname()</code>/<code>gethostbyname()</code> (as <a href="https://stackoverflow.com/questions/122208/get-the-ip-address-of-local-computer">this question</a>). Using that method, I only ever get 127.0.0.1 back (or .1.1 in Debian). I suspect this is because the hostname of the machine is in the <code>hosts</code> file, and that's as far as <code>gethostbyname()</code> checks. (I believe that's why in Debian I always get 127.0.1.1: Debian defaults to adding localhost as 127.0.0.1 and the machine's hostname as 127.0.1.1 to the <code>hosts</code> file, right?) I'd like a solution that ignores <code>hosts</code> and gives me everything actually there.</li> <li>I've had no more luck with <code>getaddrinfo()</code> than <code>gethostname()</code>/<code>gethostbyname()</code>. It seems to be bound by the same problem. I tested this passing the machine's hostname and a <code>NULL</code> service (port) into it; the docs say passing a <code>NULL</code> hostname AND a <code>NULL</code> service is illegal, and this is backed up by testing. Not sure how else to ask it for everything on the machine, but I'm open to suggestions in this vein.</li> <li>EDIT: <a href="https://stackoverflow.com/questions/259389/finding-an-ip-from-an-interface-name/259424#259424">this answer</a> shows how to get the IP address from a device name, but doesn't show how to enumerate the device names. Any ideas?</li> </ul> <p>FINAL EDIT: I've accepted <a href="https://stackoverflow.com/questions/1160963/how-to-enumerate-all-ip-addresses-attached-to-a-machine-in-posix-c/1161018#1161018">caskey's answer</a> to give him the credit for pointing me in the direction of how this needs to be done. I've posted my <a href="https://stackoverflow.com/questions/1160963/how-to-enumerate-all-ip-addresses-attached-to-a-machine-in-posix-c/1162010#1162010">own answer</a> listing the source code of how exactly to do it in case anyone else needs it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload