8

I'm trying to port a MacOSX app to windows and I've come up against a problem around getifaddrs. Basically windows does not support it. I'm trying to figure a way to re-implement it (for AF_INET and AF_INET6) but the "equivalent" functionality on windows appears to be nothing like the MacOSX support.

Has someone done this sort of conversion before? If so is there a nice way I can get windows to report me interface info like MacOSX does?

Goz
  • 61,365
  • 24
  • 124
  • 204

4 Answers4

12

The closest functions on Windows are GetAdaptersInfo and GetAdaptersAddresses. The MSDN documentation is pretty comprehensive, so you should find everything you need.

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
  • yeah I saw those. I'm slightly unsure how to get an IPv6 zone index from that info though ... I may have missed the obvious though. – Goz Jun 18 '10 at 19:03
6

getifaddrs() is not a portable solution, so if you need to support multiple platforms, you should consider using getaddrinfo, which is POSIX- and Windows-friendly. It is a little more complicated at first glance, but it really isn't that bad. This SO question has some good answers and links on the topic. (In particular, the showip.c example on Beej's page is quite helpful, and here is a Windows example; note that it is missing an #include <stdio.h> at the top, and be sure to link against Ws2_32.lib.)

Community
  • 1
  • 1
pattivacek
  • 5,617
  • 5
  • 48
  • 62
  • Why the downvote? I am extremely curious to know if I've written something incorrect or misleading. – pattivacek May 21 '14 at 14:58
  • Question is not about resolving hostname into ip address(es). – Vasily Redkin Apr 01 '15 at 09:03
  • @VasilyRedkin, did you follow the links or read the man page for `getaddinfo`? That function doesn't just translate hostnames to IP addresses. It can be used to report network interface information; as mentioned in my answer, see the showip.c example on [Beej's networking guide](http://beej.us/guide/bgnet/output/html/multipage/syscalls.html#getaddrinfo). – pattivacek Apr 01 '15 at 13:42
  • Yes, i read man page for getaddrinfo(3) on linux and freebsd and i read shoip example on linked article. Also i read glibc's implementation [source code](http://www.netperf.org/svn/netperf2/trunk/src/missing/getaddrinfo.c) and getaddrinfo manual in [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520%28v=vs.85%29.aspx). – Vasily Redkin Apr 02 '15 at 07:50
  • 1
    **Only** MSDN article states, that "If the pNodeName parameter points to a computer name, all permanent addresses for the computer that can be used as a source address are returned. On Windows Vista and later, these addresses would include all unicast IP addresses returned by the GetUnicastIpAddressTable or GetUnicastIpAddressEntry functions in which the SkipAsSource member is set to false in the MIB_UNICASTIPADDRESS_ROW structure". So your answer is partially true for windows only. – Vasily Redkin Apr 02 '15 at 07:50
  • @VasilyRedkin, that link does not look like glibc's implementation of `getaddrinfo`, or if it is, it is out of date. Try [this link](https://fossies.org/dox/glibc-2.21/sysdeps_2posix_2getaddrinfo_8c_source.html). If you actually use `getaddrinfo` on any platform, you will see that it does indeed return IP addresses for every interface the function can find. You do have to provide a hostname or service, but `gethostname` is a simple, portable method of achieving those ends. The OP was asking about Windows, but this works as well on POSIX-friendly systems. – pattivacek Apr 02 '15 at 14:24
  • Agree, wrong link. I was fooled by google :) Shame on me. It looks like you're right. Anyway, why that behaviour is not documented in man pages? – Vasily Redkin Apr 02 '15 at 15:29
  • @VasilyRedkin, you're not wrong that the man page is tough to make sense of. This line is perhaps the key relevant part: "The getaddrinfo(3) function creates a linked list of addrinfo structures, one for each network address subject to any restrictions imposed by the hints parameter." – pattivacek Apr 02 '15 at 18:26
3

I've done it here, including a separate version for Wine as the IP version agnostic routines don't current work as expected,

http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/getifaddrs.c

LGPL 2.1 license

edit: Wine 1.3 fixes the issues encountered with the IPv4 & IPv6 friendly API.

Steve-o
  • 12,678
  • 2
  • 41
  • 60
1

maybe win32 port of libpcap can help you?

(or port of libnet, if one exists)

zed_0xff
  • 32,417
  • 7
  • 53
  • 72