编译:
gcc isSameSubnet.c -o isSameSubnet -g -Wall
自测:
./isSameSubnet 192.168.1.1 192.168.2.2 255.255.0.0
result = same
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
//根据IP地址去本地查询对应的掩码
char* getMaskByIP(const char* szIPAddr)
{
static char szNetMask[20] = {};
char* szNetMaskDesc = "netmask";
FILE* netinfo = popen("/sbin/ifconfig", "r");
if(!netinfo){
puts("error while open pipe");
exit(1);
}
char strTmp[200];
char* strMaskBegin = 0;
char* strMaskEnd = 0;
while( fgets(strTmp, sizeof(strTmp) - 1, netinfo) != NULL )
{
if(strstr(strTmp, szIPAddr))
{
strMaskBegin = strstr(strTmp, szNetMaskDesc);
if(!strMaskBegin)
{
got