请问这个ping为什么输入杂乱字符串也会返回一个IP地址 并且能ping通
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib,"WS2_32")
typedef struct ip_hdr
{
unsigned char ipverlen;
unsigned char iptos;
unsigned short iplength;
unsigned short ipid;
unsigned short ipflags;
unsigned char ipttl;
unsigned char ipprotocol;
unsigned short ipchecksum;
unsigned long ipsource;
unsigned long ipdestination;
}IP_HDR,*PIP_HDR;
typedef struct icmp_hdr
{
unsigned char type;
unsigned char code;
unsigned short checksum;
unsigned short id;
unsigned short sequence;
unsigned long timestamp;
}ICMP_HDR,*PICMP_HDR;
class CInitSock
{
public:
CInitSock()
{
WORD version=MAKEWORD(2,2);
WSADATA wsadata;
if(::WSAStartup(version,&wsadata)!=0)
exit(0);
}
~CInitSock()
{
::WSACleanup();
}
};
CInitSock InitSock;
unsigned short checksum(unsigned short* buff,int size);
bool IPcnt(char *buff,char *&IPbuff);
int main(int argv,char **argc)
{
if(argv>2)
{
printf("参数使用错误!\n");
return 0;
}
char PC_IP[]="127.0.0.1";
char *szDestIP=NULL;
char input_buff[256];
int cnt;
if(argv==2)
{
if(strcmp(argc[1],"localhost")==0)
{
szDestIP=PC_IP;
cnt=1;
}else
cnt=IPcnt(argc[1],szDestIP);
}
else
{
printf("请输入地址:\n");
scanf_s("%s",input_buff,256);
if(strcmp(input_buff,"localhost")==0)
{
szDestIP=PC_IP;
cnt=1;
}else
cnt=IPcnt(input_buff,szDestIP);
}
if(cnt==false)
{
printf("输入地址有误!\n");
exit(0);
}
SOCKET sRaw=::socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
if(sRaw==SOCKET_ERROR)
{
printf("套接字创建失败!\n");
return -1;
}
int nTime=1000;
::setsockopt(sRaw,SOL_SOCKET,SO_RCVTIMEO,(char*)&nTime,sizeof(nTime));
SOCKADDR_IN dest;
dest.sin_family=AF_INET;
dest.sin_port=htons(0);
if(szDestIP==NULL)
dest.sin_addr.S_un.S_addr=inet_addr(PC_IP);
else
dest.sin_addr.S_un.S_addr=inet_addr(szDestIP);
char buff[sizeof(ICMP_HDR)];
ICMP_HDR* pIcmp = (ICMP_HDR*)buff;
pIcmp->type = 8;
pIcmp->code = 0;
pIcmp->id = (unsigned short)::GetCurrentProcessId();
unsigned short nSeq = 0;
char recvBuf[1024];
SOCKADDR_IN from;
int nLen=sizeof(from);
int nCout=0;
printf("正在 Ping %s 具有 32 字节的数据:\n",szDestIP);
while(TRUE)
{
int nRet;
if(nCout++==4)
break;
pIcmp->checksum=0;
pIcmp->timestamp=::GetTickCount();
pIcmp->sequence=nSeq++;
pIcmp->checksum=checksum((unsigned short*)buff,sizeof(ICMP_HDR));
nRet = ::sendto(sRaw,buff,sizeof(ICMP_HDR),0,(SOCKADDR*)&dest,sizeof(dest));
if(nRet==SOCKET_ERROR)
{
printf("sendto() 错误: %d \n",::WSAGetLastError());
return -1;
}
nRet=::recvfrom(sRaw,recvBuf,1024,0,(sockaddr*)&from,&nLen);
if(nRet==SOCKET_ERROR)
{
if(::WSAGetLastError()==WSAETIMEDOUT)
{
printf(" 请求超时 ! \n");
continue;
}
printf("recvfrom() 错误: %d\n",::WSAGetLastError());
return -1;
}
int nTick=::GetTickCount();
ICMP_HDR* pRecvIcmp=(ICMP_HDR*)(recvBuf+20);
IP_HDR* pRecvIp=(IP_HDR*)recvBuf;
if(pRecvIcmp->type!=0||pRecvIcmp->id!=::GetCurrentProcessId())
{
printf("错误的数据包!\n");
return -1;
}
printf("来自 %s 的回复: 字节=%d 时间=%d TTL=%d\n",inet_ntoa(from.sin_addr),nRet,nTick-pRecvIcmp->timestamp,pRecvIp->ipttl);
::Sleep(1000);
}
return 0;
}
unsigned short checksum(unsigned short *buff,int size)
{
unsigned long cksum=0;
while(size>1)
{
cksum+=*buff++;
size-=sizeof(unsigned short);
}
if(size)
{
cksum+=*(unsigned char*)buff;
}
cksum=(cksum>>16)+(cksum&0xffff);
cksum+=(cksum>>16);
return (unsigned short)(~cksum);
}
bool IPcnt(char *buff,char *&IPbuff)
{
in_addr addr;
bool cnt=0;
char *IP;
unsigned long ip=inet_addr(buff);
if(ip==INADDR_NONE)
{
hostent *pHost=::gethostbyname(buff);
if(pHost==NULL)
return cnt;
printf("该网址IP有:\n");
for(int i=0;;i++)
{
char *p=pHost->h_addr_list[i];
if(p==NULL)
break;
memcpy(&addr.S_un.S_addr,p,pHost->h_length);
IP=::inet_ntoa(addr);
printf("%s\n",IP);
cnt=1;
}
if(cnt==1)
IPbuff=IP;
}else
{
addr.S_un.S_addr=ip;
IP=::inet_ntoa(addr);
IPbuff=IP;
cnt=1;
}
return cnt;
}