getip.c

Utilities to find our own IP addresses

Summary
getip.cUtilities to find our own IP addresses
Functions
netx_getmyipReturn IP address(es)
netx_getmyipstrGet a list of IP addresses in string format.
netx_freeipstrFree the strings allocated by netx_getmyipstr

Functions

netx_getmyip

Return IP address(es)

Getting our IP address is non-trivial.  Most personal computers have multiple interfaces (e.g. wired and WiFi) and frequently have multiple active addresses on one interface (e.g.  DHCP falling back to Avahi).  Also modern stacks often run both IPv4 and IPv6 side by side.

The traditional method of calling gethostname then gethostbyname (superseded on Linux by getaddrinfo()) relies on functioning name resolution which is not the case on many of the types of network where ACN is used.

For purposes of advertisement (SLP) we can advertise the same service over multiple interfaces so returning multiple addresses is desirable, but rules of epi29 (epi13) expect prioritization (e.g. so link local addresses are only advertised when no routable address is available).

For the multicast generation method of epi10 “the (unicast) IP address of the interface to be used” is required, but this is simply a way to reduce the probability of conflicting addresses and the protocol will not fail if the wrong address is used so a best guess is sufficient.

The strategy we use is to provide a set of filters and potentially return a multiple of addresses.

arguments

interfacesnull-terminated array of hardware interface names to select.  Wildcards are allowed. e.g.  “eth0*” matches “eth0”, “eth0:1” etc.  If NULL then all interfaces are considered.  Negative matches, preceeded by ‘!’ are permitted, e.g.  “!lo”.  When comparing a name against the array, searching stops with success at the fisrt positive match and stops with failure at the first negative match.  This allows partial selections using wildcards by preceeding the more general wildcard by a more specific one. e.g.  “!eth0:foo”, “eth0*” which selects any eth0 interface except eth0:foo.
flagmask, flagmatchinterface flags must match according to the expression (interfaceflags & flagmask) == (flagmatch & flagmask).  See getipflag_e for available flags.
addrlistpointer to area to put matching addresses
sizesize in bytes of the addrlist area

netx_getmyipstr

int netx_getmyipstr(const char *interfaces[],
uint32_t flagmask,
uint32_t flagmatch,
char **ipstrs,
int maxaddrs)

Get a list of IP addresses in string format.

netx_freeipstr

void netx_freeipstr(char **strs)

Free the strings allocated by netx_getmyipstr

int netx_getmyipstr(const char *interfaces[],
uint32_t flagmask,
uint32_t flagmatch,
char **ipstrs,
int maxaddrs)
Get a list of IP addresses in string format.
void netx_freeipstr(char **strs)
Free the strings allocated by netx_getmyipstr
Close