Get network interface name from IPv4 address
Asked Answered
K

2

5

Given an IPv4 address, how can I obtain its associated network interface name, like "Ethernet adapter Local Area Connection", in Windows using C++? Alternately, how can I obtain a list of both network interface names and IPv4 addresses for the local computer? I'm able to get only the IPv4 addresses using getaddrinfo and inet_ntoa.

Kerwon answered 25/6, 2013 at 3:56 Comment(4)
It depend on platform. For example, you should use GetAdaptersInfo on windows.Swat
@Swat The documentation for GetAdaptersInfo says to use GetAdaptersAddresses on XP and later. Is this preferred?Ruysdael
Yes, better to use GetAdaptersAddresses if you want make it works on XP or later.Swat
This seems work gist.github.com/yoggy/1241986Swat
S
10
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>

#pragma comment(lib, "iphlpapi.lib")

int
main(int argc, char** argv) {
  PIP_ADAPTER_INFO pAdapterInfo;
  pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO));
  ULONG buflen = sizeof(IP_ADAPTER_INFO);

  if(GetAdaptersInfo(pAdapterInfo, &buflen) == ERROR_BUFFER_OVERFLOW) {
    free(pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *) malloc(buflen);
  }

  if(GetAdaptersInfo(pAdapterInfo, &buflen) == NO_ERROR) {
    PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
    while (pAdapter) {
      printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
      printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
      printf("\tAdapter Addr: \t%ld\n", pAdapter->Address);
      printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
      printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
      printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
      if(pAdapter->DhcpEnabled) {
        printf("\tDHCP Enabled: Yes\n");
        printf("\t\tDHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String);
        printf("\tLease Obtained: %ld\n", pAdapter->LeaseObtained);
      } else {
        printf("\tDHCP Enabled: No\n");
      }
      if(pAdapter->HaveWins) {
        printf("\tHave Wins: Yes\n");
        printf("\t\tPrimary Wins Server: \t%s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
        printf("\t\tSecondary Wins Server: \t%s\n", pAdapter->SecondaryWinsServer.IpAddress.String);
      } else {
        printf("\tHave Wins: No\n");
      }
      pAdapter = pAdapter->Next;
    }
  } else {
    printf("Call to GetAdaptersInfo failed.\n");
  }
}

As @sonyisda1 mentioned, this is taken from MSDN.

Swat answered 25/6, 2013 at 4:7 Comment(1)
Probably should be mentioned this is taken from MSDN and if using WinXP or later GetAdaptersAddresses should be used insteadJeffcott
K
6

I'll share my minimal version as well:

#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>

#pragma comment(lib, "iphlpapi.lib")

int main() {
    ULONG buflen = sizeof(IP_ADAPTER_INFO);
    IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(buflen);

    if (GetAdaptersInfo(pAdapterInfo, &buflen) == ERROR_BUFFER_OVERFLOW) {
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO *)malloc(buflen);
    }

    if (GetAdaptersInfo(pAdapterInfo, &buflen) == NO_ERROR) {
        for (IP_ADAPTER_INFO *pAdapter = pAdapterInfo; pAdapter; pAdapter = pAdapter->Next) {
            printf("%s (%s)\n", pAdapter->IpAddressList.IpAddress.String, pAdapter->Description);
        }
    }

    if (pAdapterInfo) free(pAdapterInfo);
    return 0;
}
Kerwon answered 25/6, 2013 at 14:47 Comment(1)
This is helpfulDalessio

© 2022 - 2024 — McMap. All rights reserved.