[erlang-questions] : : Finding IP addresses in a PC

David Hopwood david.nospam.hopwood@REDACTED
Wed Oct 4 17:54:11 CEST 2006


Raimo Niskanen wrote:
> I was thinking more in the line of using inet_ntoa() and inet_addr(),
> or rather use ntohl() and htonl() around the current fmt_addr(),
> or even rewrite fmt_addr to take a char[] as input, in
> network byte order.
> 
> Rewriting fmt_addr() as you suggested works on little-endian
> machines such as Intel, and there are not many big-endian
> Windows platforms out there, but I think it is
> a confusing and wrong(tm) way to do it.

If you use GetAdaptersInfo, the string formatting is done for you in
the IpAddress.String field:

  IP_ADAPTER_INFO dummy;
  DWORD len = 0;
  if (GetAdaptersInfo(&dummy, &len) != ERROR_BUFFER_OVERFLOW || len == 0) {
      return FAILED;
  }

  IP_ADAPTER_INFO pAdapters = (IP_ADAPTER_INFO *) malloc(len);
  if (pAdapters == NULL) {
      return FAILED;
  }

  DWORD err = GetAdaptersInfo(pAdapters, &len);
  if (err != ERROR_SUCCESS) {
      return FAILED;
  }

  do {
      IP_ADDR_STRING *pip = &pAdapters->IpAddressList;
      while (pip != NULL) {
          add_address(pip->IpAddress.String);
          pip = pip->Next;
      }
      pAdapters = pAdapters->Next;
  } while (pAdapters != NULL);

I wrote:
> Note that this API has some limitations (no IPv6 support, only one address
> returned per adapter)

Correction: it can return multiple addresses for each adapter, as shown above.
GetAdaptersAddresses is only needed for IPv6.

-- 
David Hopwood <david.nospam.hopwood@REDACTED>





More information about the erlang-questions mailing list