|
|
Definition at line 208 of file icmp_main.c.
{
icmp_t* icp=(icmp_t*)IcmpHandle;
unsigned char* reqbuf;
int reqsize;
struct icmp_echo_reply* ier;
struct ip* ip_header;
struct icmp* icmp_header;
char* endbuf;
int ip_header_len;
int maxlen;
fd_set fdr;
struct timeval timeout;
DWORD send_time,recv_time;
struct sockaddr_in addr;
unsigned int addrlen;
unsigned short id,seq,cksum;
int res;
if (IcmpHandle==INVALID_HANDLE_VALUE) {
SetLastError(ERROR_INVALID_HANDLE);
return 0;
}
if (ReplySize<sizeof(ICMP_ECHO_REPLY)+ICMP_MINLEN) {
SetLastError(IP_BUF_TOO_SMALL);
return 0;
}
id=GetCurrentProcessId() & 0xFFFF;
seq=InterlockedIncrement(&icmp_sequence) & 0xFFFF;
reqsize=ICMP_MINLEN+RequestSize;
reqbuf=HeapAlloc(GetProcessHeap(), 0, reqsize);
if (reqbuf==NULL) {
SetLastError(ERROR_OUTOFMEMORY);
return 0;
}
icmp_header=(struct icmp*)reqbuf;
icmp_header->icmp_type=ICMP_ECHO;
icmp_header->icmp_code=0;
icmp_header->icmp_cksum=0;
icmp_header->icmp_id=id;
icmp_header->icmp_seq=seq;
memcpy(reqbuf+ICMP_MINLEN, RequestData, RequestSize);
icmp_header->icmp_cksum=cksum=in_cksum((u_short*)reqbuf,reqsize);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=DestinationAddress;
addr.sin_port=0;
if (RequestOptions!=NULL) {
int val;
if (icp->default_opts.OptionsSize==IP_OPTS_UNKNOWN) {
int len;
len=sizeof(val);
getsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,&len);
icp->default_opts.Ttl=val;
len=sizeof(val);
getsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,&len);
icp->default_opts.Tos=val;
}
val=RequestOptions->Ttl;
setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
val=RequestOptions->Tos;
setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
icp->default_opts.OptionsSize=IP_OPTS_CUSTOM;
} else if (icp->default_opts.OptionsSize==IP_OPTS_CUSTOM) {
int val;
val=icp->default_opts.Ttl;
setsockopt(icp->sid,IPPROTO_IP,IP_TTL,(char *)&val,sizeof(val));
val=icp->default_opts.Tos;
setsockopt(icp->sid,IPPROTO_IP,IP_TOS,(char *)&val,sizeof(val));
icp->default_opts.OptionsSize=IP_OPTS_DEFAULT;
}
FD_ZERO(&fdr);
FD_SET(icp->sid,&fdr);
timeout.tv_sec=Timeout/1000;
timeout.tv_usec=(Timeout % 1000)*1000;
addrlen=sizeof(addr);
ier=ReplyBuffer;
ip_header=(struct ip *) ((char *) ReplyBuffer+sizeof(ICMP_ECHO_REPLY));
endbuf=(char *) ReplyBuffer+ReplySize;
maxlen=ReplySize-sizeof(ICMP_ECHO_REPLY);
TRACE("Sending %d bytes (RequestSize=%d) to %s\n", reqsize, RequestSize, inet_ntoa(addr.sin_addr));
#if 0
if (TRACE_ON(icmp)){
unsigned char* buf=(unsigned char*)reqbuf;
int i;
printf("Output buffer:\n");
for (i=0;i<reqsize;i++)
printf("%2x,", buf[i]);
printf("\n");
}
#endif
send_time = GetTickCount();
res=sendto(icp->sid, (const char*)reqbuf, reqsize, 0, (struct sockaddr*)&addr, sizeof(addr));
HeapFree(GetProcessHeap (), 0, reqbuf);
if (res<0) {
if (WSAGetLastError()==WSAEMSGSIZE)
SetLastError(IP_PACKET_TOO_BIG);
else {
switch (WSAGetLastError()) {
case WSAENETUNREACH:
SetLastError(IP_DEST_NET_UNREACHABLE);
break;
case WSAEHOSTUNREACH:
SetLastError(IP_DEST_HOST_UNREACHABLE);
break;
default:
TRACE("unknown error: errno=%d\n",WSAGetLastError());
SetLastError(IP_GENERAL_FAILURE);
}
}
return 0;
}
ip_header_len=0;
while ((res=select(icp->sid+1,&fdr,NULL,NULL,&timeout))>0) {
recv_time = GetTickCount();
res=recvfrom(icp->sid, (char*)ip_header, maxlen, 0, (struct sockaddr*)&addr,(int*)&addrlen);
TRACE("received %d bytes from %s\n",res, inet_ntoa(addr.sin_addr));
ier->Status=IP_REQ_TIMED_OUT;
if ((ip_header->ip_p==IPPROTO_ICMP) && (res>=sizeof(struct ip)+ICMP_MINLEN)) {
ip_header_len=ip_header->ip_hl << 2;
icmp_header=(struct icmp*)(((char*)ip_header)+ip_header_len);
TRACE("received an ICMP packet of type,code=%d,%d\n",icmp_header->icmp_type,icmp_header->icmp_code);
if (icmp_header->icmp_type==ICMP_ECHOREPLY) {
if ((icmp_header->icmp_id==id) && (icmp_header->icmp_seq==seq))
ier->Status=IP_SUCCESS;
} else {
switch (icmp_header->icmp_type) {
case ICMP_UNREACH:
switch (icmp_header->icmp_code) {
case ICMP_UNREACH_HOST:
#ifdef ICMP_UNREACH_HOST_UNKNOWN
case ICMP_UNREACH_HOST_UNKNOWN:
#endif
#ifdef ICMP_UNREACH_ISOLATED
case ICMP_UNREACH_ISOLATED:
#endif
#ifdef ICMP_UNREACH_HOST_PROHIB
case ICMP_UNREACH_HOST_PROHIB:
#endif
#ifdef ICMP_UNREACH_TOSHOST
case ICMP_UNREACH_TOSHOST:
#endif
ier->Status=IP_DEST_HOST_UNREACHABLE;
break;
case ICMP_UNREACH_PORT:
ier->Status=IP_DEST_PORT_UNREACHABLE;
break;
case ICMP_UNREACH_PROTOCOL:
ier->Status=IP_DEST_PROT_UNREACHABLE;
break;
case ICMP_UNREACH_SRCFAIL:
ier->Status=IP_BAD_ROUTE;
break;
default:
ier->Status=IP_DEST_NET_UNREACHABLE;
}
break;
case ICMP_TIMXCEED:
if (icmp_header->icmp_code==ICMP_TIMXCEED_REASS)
ier->Status=IP_TTL_EXPIRED_REASSEM;
else
ier->Status=IP_TTL_EXPIRED_TRANSIT;
break;
case ICMP_PARAMPROB:
ier->Status=IP_PARAM_PROBLEM;
break;
case ICMP_SOURCEQUENCH:
ier->Status=IP_SOURCE_QUENCH;
break;
}
if (ier->Status!=IP_REQ_TIMED_OUT) {
struct ip* rep_ip_header;
struct icmp* rep_icmp_header;
rep_ip_header=(struct ip*)(((char*)icmp_header)+ICMP_MINLEN);
rep_icmp_header=(struct icmp*)(((char*)rep_ip_header)+(rep_ip_header->ip_hl << 2));
if (ip_header_len+ICMP_MINLEN+(rep_ip_header->ip_hl << 2)+ICMP_MINLEN>ip_header->ip_len) {
ier->Status=IP_REQ_TIMED_OUT;
} else if ((rep_icmp_header->icmp_type!=ICMP_ECHO) ||
(rep_icmp_header->icmp_code!=0) ||
(rep_icmp_header->icmp_id!=id) ||
(rep_icmp_header->icmp_seq!=seq)) {
TRACE("skipping type,code=%d,%d id,seq=%d,%d cksum=%d\n",
rep_icmp_header->icmp_type,rep_icmp_header->icmp_code,
rep_icmp_header->icmp_id,rep_icmp_header->icmp_seq,
rep_icmp_header->icmp_cksum);
TRACE("expected type,code=8,0 id,seq=%d,%d cksum=%d\n",
id,seq,
cksum);
ier->Status=IP_REQ_TIMED_OUT;
}
}
}
}
if (ier->Status==IP_REQ_TIMED_OUT) {
int t = Timeout - (recv_time - send_time);
if (t < 0) t = 0;
timeout.tv_sec = t / 1000;
timeout.tv_usec = (t % 1000) * 1000;
continue;
} else {
memcpy(&ier->Address,&ip_header->ip_src,sizeof(IPAddr));
ier->RoundTripTime= recv_time - send_time;
ier->DataSize=res-ip_header_len-ICMP_MINLEN;
ier->Reserved=0;
ier->Data=endbuf-ier->DataSize;
memmove(ier->Data,((char*)ip_header)+ip_header_len+ICMP_MINLEN,ier->DataSize);
ier->Options.Ttl=ip_header->ip_ttl;
ier->Options.Tos=ip_header->ip_tos;
ier->Options.Flags=ip_header->ip_off >> 13;
ier->Options.OptionsSize=ip_header_len-sizeof(struct ip);
if (ier->Options.OptionsSize!=0) {
ier->Options.OptionsData=(unsigned char *) ier->Data-ier->Options.OptionsSize;
memmove(ier->Options.OptionsData,((char*)ip_header)+ip_header_len,ier->Options.OptionsSize);
endbuf=(char*)ier->Options.OptionsData;
} else {
ier->Options.OptionsData=NULL;
endbuf=ier->Data;
}
ier++;
ip_header=(struct ip*)(((char*)ip_header)+sizeof(ICMP_ECHO_REPLY));
maxlen=endbuf-(char*)ip_header;
timeout.tv_sec=0;
timeout.tv_usec=0;
}
FD_ZERO(&fdr);
FD_SET(icp->sid,&fdr);
}
res=ier-(ICMP_ECHO_REPLY*)ReplyBuffer;
if (res==0)
SetLastError(IP_REQ_TIMED_OUT);
TRACE("received %d replies\n",res);
return res;
}
|