2013-08-07 45 views
2

我想獲取IP地址和子網掩碼。現在IP部分已經完成,但是我找不到任何套接字函數,它會返回一個帶有子網掩碼的結構。 是否存在一個套接字函數,它返回一個結構?在C中獲取SubnetMask

謝謝!

回答

3

在使用IPHelper窗口。

#include <winsock2.h> 
#include <iphlpapi.h> 
#include <stdio.h> 
#include <stdlib.h> 
#pragma comment(lib, "IPHLPAPI.lib") 

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) 
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) 

/* Note: could also use malloc() and free() */ 

int __cdecl main() 
{ 

    PIP_ADAPTER_INFO pAdapterInfo; 
    ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); 
    pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO)); 
    GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); 
    printf("\tIP Mask: \t%s\n", pAdapterInfo->IpAddressList.IpMask.String); 
    } 
    if (pAdapterInfo) 
     FREE(pAdapterInfo); 

    return 0; 
} 
5

在Unix的使用getifaddrs

struct ifaddrs富人名爲ifa_netmask(接口的網絡掩碼)構件

#include <arpa/inet.h> 
#include <sys/socket.h> 
#include <ifaddrs.h> 
#include <stdio.h> 

int main() 
{ 
    struct ifaddrs *ifap, *ifa; 
    struct sockaddr_in *sa; 
    char *addr; 

    getifaddrs (&ifap); 
    for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 
     if (ifa->ifa_addr->sa_family==AF_INET) { 
      sa = (struct sockaddr_in *) ifa->ifa_netmask; 
      addr = inet_ntoa(sa->sin_addr); 
      printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr); 
     } 
    } 

    freeifaddrs(ifap); 
    return 0; 
} 

輸出

Interface: lo Address: 255.0.0.0 
Interface: eth0 Address: 255.255.255.0 
2
從Linux手冊頁

借來的代碼,並從Keine Lust簡稱代碼:

#define _GNU_SOURCE  /* To get defns of NI_MAXSERV and NI_MAXHOST */ 

#include <arpa/inet.h> 
#include <sys/socket.h> 
#include <netdb.h> 
#include <ifaddrs.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <linux/if_link.h> 
#include <string.h>  /* strcasecmp() */ 

int get_addr_and_netmask_using_ifaddrs(const char* ifa_name, 
             char *addr, char *netmask) 
{ 
    struct ifaddrs *ifap, *ifa; 
    struct sockaddr_in *sa; 
    char *s; 
    int found = 0; 

    if (getifaddrs(&ifap) == -1) { 
     perror("getifaddrs"); 
     exit(EXIT_FAILURE); 
    } 

    for (ifa = ifap; ifa && !found; ifa = ifa->ifa_next) { 
     if (ifa->ifa_addr == NULL) 
      continue; 

     if (strcasecmp(ifa_name, ifa->ifa_name)) 
      continue; 

     /* IPv4 */ 
     if (ifa->ifa_addr->sa_family != AF_INET) 
      continue; 

     sa = (struct sockaddr_in *) ifa->ifa_addr; 
     s = inet_ntoa(sa->sin_addr); 
     strcpy(addr, s); 

     sa = (struct sockaddr_in *) ifa->ifa_netmask; 
     s = inet_ntoa(sa->sin_addr); 
     strcpy(netmask, s); 

     found = 1; 
    } 

    freeifaddrs(ifap); 

    if (found) 
     return EXIT_SUCCESS; 
    return EXIT_FAILURE; 
} 

int main(void) 
{ 
    char *addr = malloc(NI_MAXHOST); 
    char *netmask = malloc(NI_MAXHOST); 

    if (!get_addr_and_netmask_using_ifaddrs ("enp6s0", addr, netmask)) 
     printf("[%s]%s %s\n", __func__, addr, netmask); 
    else 
     printf("interface error.\n"); 

    free(addr); 
    free(netmask); 

    return 0; 
}