在C语言中获取子网掩码

5

我想获取IP地址和子网掩码。现在已完成了IP部分,但我找不到任何可以返回包含子网掩码结构的套接字函数。

是否存在可以返回子网掩码结构的套接字函数?

谢谢!

3个回答

10

在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

3
在Windows中使用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;
}

2

我借鉴了Linux Man页面上的代码,并参考了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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接