⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip_test.cpp

📁 此为计算机网络课程设计C/C++源代码 包括一些协议的底层实现
💻 CPP
字号:
/*IP地址的合法性及子网判断*/

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>

/*类声明*/
class IpTest{
private:
	char ip[15];

	char subnetPlusMask[18];
	char subnet[18];
	char TempIp[15];
	char TempSub[18];
	int mask;
	bool maskIsValid;

public:
	IpTest(char *,char *);
	~IpTest(){
	};
	bool NoIllegalChar(char *);		/*非法字符的判断*/
	bool ipIsValid(char *);			/*判断IP地址是否合法*/
	bool subnetIsValid(char *);		/*判断子网号是否合法*/
	void belong();					/*判断IP是否为子网成员*/
	void print();
};

IpTest::IpTest(char *subnetPlusMask,char *ip)
{
	maskIsValid=true;
	char temp[2];

	/*把'/'前的字符复制到subnet字符数组中*/
	int smLen=strlen(subnetPlusMask);

	for (int i=0;i<smLen && subnetPlusMask[i] != '/';i++)
	{
		this->subnet[i]=subnetPlusMask[i];
		this->TempSub[i]=subnetPlusMask[i];
	}
	subnet[i]='\0';
	TempSub[i]='\0';

	if (i<=smLen-2)		/*初始化mask和maskIsValid*/
	{
		if (i==smLen-2)
		{
			temp[0]=subnetPlusMask[i+1];
			if (!isdigit(temp[0]))
			{
				maskIsValid=false;
			}
		}
		else if (i==smLen-3)
		{
			temp[0]=subnetPlusMask[i+1];
			temp[1]=subnetPlusMask[i+2];
			if (!(isdigit(temp[0])) && isdigit(temp[1]) )
			{
				maskIsValid=false;
			}
		}
		mask=atoi(temp);
		if (mask<0 || mask>32)
		{
			maskIsValid=false;
		}
	}
	else			/*用十进制数表示的掩码中的1的个数只能是一位数或者两位数*/
	{
		maskIsValid=false;
	}

	strcpy(this->subnetPlusMask,subnetPlusMask);	/*给subnetPlusMask赋值*/
	strcpy(this->ip,ip);
	strcpy(this->TempIp,ip);
}

/*调用判别函数,输出结果*/
void IpTest::print()
{
	bool subIsV=subnetIsValid(TempSub);
	bool ipIsV=ipIsValid(TempIp);

	if (!subIsV)		/*subnet非法*/
	{
		cout<<"subnet is invalid !"<<endl;
	}
	else
		cout<<"valid subnet: "<<subnet<<endl;

	if (!maskIsValid)	/*mask非法*/
	{
		cout<<"mask is invalid !"<<endl;
	}
	else
		cout<<"valid mask:	"<<mask<<endl;

	if (!ipIsV)			/*ip非法*/
	{
		cout<<"ip is invalid !"<<endl;
	}
	else
		cout<<"valid ip:	"<<ip<<endl;

	/* 判断IP是否belong subnet */
	if (subIsV && ipIsV && maskIsValid)
	{
		belong();
	}
}

/*子函数,判断输入是否含有非数字字符*/
bool IpTest::NoIllegalChar(char *ch)
{
	unsigned int i,k=0;
	for (i=0;i<strlen(ch);i++)
	{
		if (isdigit(*(ch+i))==0)		/*判断每一位是否为数字字符*/
		{
			return false;
		}
	}
	return true;						/*若不含有非数字字符则返回true*/
}

/*判断IP地址是否合法*/
bool IpTest::ipIsValid(char *ip)
{
	char ch[]=".";						/*分隔法*/
	char *token,*dot[4];
	int iplen=0;

	token=strtok(ip,ch);				/*以"."标志将IP字符串按节分开*/
	while (token!=NULL)
	{
		dot[iplen]=token;				/*循环进行,直到结束*/
		iplen++;
		token=strtok(NULL,ch);			/*将分开的每段赋值给dot*/
	}

	if (iplen!=4)
	{
		return false;					/*段数不对*/
	}

	for (int i=0;i<4;i++)				/*段数不对*/
	{
		if (!NoIllegalChar(dot[i]) || atoi(dot[i])>255)
		{
			/*有非法字符或是某段值非法*/
			return false;
		}
	}

	return true;
}

/*判断子网号是否合法*/
bool IpTest::subnetIsValid(char *subnet)
{
	if (!ipIsValid(subnet))			/*调用判别IP地址合法性的函数*/
	{
		return false;
	}
	return true;
}

/*判断IP是否为子网成员,判断子网号与掩码是否匹配,以及子网号主机号全0全1问题*/
void IpTest::belong()
{
	int subLen=strlen(subnet);
	int ipLen=strlen(ip);

	unsigned int iIPA,iSubA,iMask;
	unsigned char subA[4],ipA[4];
	char temp[3];
	int i,j,t=0;

	for (i=0,j=0;i<subLen;i++)		/*不用再检错*/
	{
		if (subnet[i]!='.')
		{
			temp[j++]=subnet[i];	/*temp数组中放'.'间的串*/
		}
		else
		{
			subA[3-t]=atoi(temp);	/*subA数组中放'.'间的数据*/
			j=0;
			t++;
			temp[0]=temp[1]=temp[2]='\0';
		}

	}

	subA[0]=atoi(temp);
	temp[0]=temp[1]=temp[2]='\0';
	iSubA=*(unsigned int *)subA;	/*iSubA中放sunnet中'.'除外的串对应的数*/

	for (i=0,j=0,t=0;i<ipLen;i++)	/*不用再检错*/
	{
		if (ip[i]!='.')
		{
			temp[j++]=ip[i];
		}
		else
		{
			ipA[3-t]=atoi(temp);
			j=0;
			t++;
			temp[0]=temp[1]=temp[2]='\0';
		}
	}

	ipA[0]=atoi(temp);
	iIPA=*(unsigned int *)ipA;		/*iIPA中放IP中'.'除外的串对应的数*/

	iMask=0xffffffff;
	iMask<<=(32-mask);			/*获得掩码*/

	if ((iSubA | iMask) != iMask)
	{
		cout<<"子网号与掩码不匹配,error!"<<endl;
		return;
	}
	if ((iSubA ^ iMask)==0)
	{
		cout<<"子网号全为1,error!"<<endl;
		return;
	}
	if ((iSubA & iMask)==0)
	{
		cout<<"子网号全为0,error!"<<endl;
		return;
	}
	if ((iSubA)==(iIPA & iMask))
	{
		if ((iIPA | iMask) == iMask)
		{
			cout<<"主机号全0,error!"<<endl;
			return;
		}
		if ((iIPA | iMask) == 0xffffffff)
		{
			cout<<"主机号全1,error!"<<endl;
			return;			
		}

		/*IP属于subnet*/
		cout<<" "<<ip<<" belongs to "<<subnetPlusMask<<endl;
		return;
	}
	else
		cout<<" "<<ip<<" doesn't belong to "<<subnetPlusMask<<endl;
}

void main(int argc,char *argv[])
{
	if (argc!=3)						/*判断参数格式是否正确*/
	{
		cout<<"error"<<endl<<" format shoud be:ip_test subnet/mask ip "<<endl;
		return;
	}
	else
	{
		if (strlen(argv[1])>18)			/*先判断最简单的错误,长度是否超出*/
		{
			cout<<"subnet/mask is too long";
			return;
		}
		if (strlen(argv[2])>15)
		{
			cout<<"ip is too long";
			return;
		}

		IpTest test(argv[1],argv[2]);
		test.print();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -