用C语言获取子网掩码

用C语言获取子网掩码,c,subnet,C,Subnet,我想得到IP地址和子网掩码。现在IP部分已经完成了,但是我找不到任何套接字函数可以返回包含子网掩码的结构。 是否存在以结构形式返回的套接字函数 谢谢 在Unix中使用 struct ifaddrs有一个名为ifa\u netmask(接口的netmask)的成员 在windows中使用iPhoneper #include <winsock2.h> #include <iphlpapi.h> #include <stdio.h> #include <std

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

谢谢

在Unix中使用

struct ifaddrs
有一个名为
ifa\u netmask
(接口的netmask)的成员


在windows中使用iPhoneper

#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;
}
#包括
#包括
#包括
#包括
#pragma注释(lib,“iphlapi.lib”)
#定义MALLOC(x)HeapAlloc(GetProcessHeap(),0,(x))
#定义自由(x)堆自由(GetProcessHeap(),0,(x))
/*注意:也可以使用malloc()和free()*/
int_u_CDECLmain()
{
PIP_适配器_信息pAdapterInfo;
ULONG-ulOutBufLen=sizeof(IP适配器信息);
padapternfo=(IP_适配器信息*)MALLOC(sizeof(IP_适配器信息));
GetAdapterInfo(pAdapterInfo和ulOutBufLen);
printf(“\tIP Mask:\t%s\n”,padapternfo->IpAddressList.IpMask.String);
}
如果(pAdapterInfo)
免费(pAdapterInfo);
返回0;
}

借用了Linux手册页中的代码,并引用了以下代码:

#定义_GNU__SOURCE/*以获取niu MAXSERV和niu MAXHOST的定义*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#include/*strcasecmp()*/
使用ifaddrs(常量字符*ifa\U名称,
字符*地址,字符*网络掩码)
{
结构IFADRS*ifap,*ifa;
*sa中的结构sockaddr_;
char*s;
int=0;
if(getifaddrs(&ifap)==-1){
perror(“getifaddrs”);
退出(退出失败);
}
对于(ifa=ifap;ifa&&!找到;ifa=ifa->ifa\U下一步){
如果(ifa->ifa\U地址==NULL)
继续;
if(STRCACMP(ifa\U名称,ifa->ifa\U名称))
继续;
/*IPv4*/
if(ifa->ifa地址->sa家庭!=AF家庭)
继续;
sa=(结构sockaddr_in*)ifa->ifa_addr;
s=inet\u ntoa(sa->sin\u地址);
strcpy(地址s);
sa=(结构sockaddr_in*)ifa->ifa_网络掩码;
s=inet\u ntoa(sa->sin\u地址);
strcpy(网络掩码,s);
发现=1;
}
freeifaddrs(ifap);
如果(找到)
返回退出成功;
返回退出失败;
}
内部主(空)
{
char*addr=malloc(NI_MAXHOST);
char*netmask=malloc(NI_MAXHOST);
如果(!使用ifaddrs(“enp6s0”,地址,网络掩码”)获取地址和网络掩码)
printf(“[%s]%s%s\n”,函数,地址,网络掩码);
其他的
printf(“接口错误。\n”);
免费(addr);
免费(网络掩码);
返回0;
}
Interface: lo   Address: 255.0.0.0
Interface: eth0 Address: 255.255.255.0
#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;
}
#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;
}