未分类题
下面是一个简单的使用RAWSOCKET实现的ping程序,填入(n)处。
/*simple ping program*/
struct sockaddr_in saddr;
int rawsock;
unsigned short in_cksum(unsigned short*addr, int len)
{ int sum=0;
unsigned short res=0;
while(1en>1){
sum+=*addr++; len-=2;
}
if(len=1){
*((unsigned char *)(&res))=*((unsigned char *)addr); sum+=res;
}
sum=(sum>>16)+(sam & 0xffff);
sum+=(sum>>16); res=~sum;
return res;
}
void ping(int signo)
{
int len;
int i;
static unsigned short seq=0;
char buff[8192];
struct timeval tv;
struet icmp*icmph=(struct icmp * )buff;
long*data=(long*)icmph→icmp_data;
bzero(buff, 8192);
gettimeofday(&tv, NULL);
icmph→icmp_type=ICMP_ECHO;
icmph→icmp_code=0;
icmph→icmp_cksum=0;
icmph→icmp_id=0;
icmph→icmp_seq=0;
icmph→icmp_id=getpid()&0xffff;
icmph→icmp_seq=seq++;
data[0]=tv.tv_sec;
data[1]=tv.tv_usec;
for(i=8; i< ; i++)
icmph→icmp_data[i]=(unsigned char)i;
icmph→icmp_cksum=in_cksum((unsigned short *)buff, ? 72);
len; sendto(rawsock, buff, 72, 0, &saddr, sizeof(saddr));
A.tv_sec;
B.tv_usec;
C.2f
D.tv_see-data[0])*1000.0+(tv.tv_usec-data[0])/1000.0);
E.bbb.ccc.ddd/n',
F.sin_family=(3);
G.sin_addr)
【参考答案】
(1)argc !=2 //判断参数个数是否为2;(2)SOCK_RAW //指定套接字类型为SOCK_RAW;(3)A......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)