网友您好, 请在下方输入框内输入要搜索的题目:

题目内容 (请给出正确答案)

下面是一个简单的使用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));

alarm(1);

}

void sigint(int signo)

{ printf("CATCH SIGINT !!! \n");

close(rawsock);

exit(0);

}

void dumppkt(char*buf, int len)

{ struct ip*iph=(struct ip*)buf;

int i=iph→ip_h1*4;

struct icmp*icmph=(struct icmp*)&buf[i];

long*data=(long*)iemph→icmp_data;

struct timeval tv;

gettimeofday(&tv, NULL);

if(icmph→icmp_type! =ICMP_ECHOREPLY)

return;

if(icmph→icmp_id! =(getpid()&0xffff))

return;

printf("From %s:ttl=% d seq=% d time=%.2f ms\n",

inet_ntoa(iph→ip_src),iph→ip_ttl?,

icmph→icmp_seq,

(tv.tv_see-data[0])*1000.0+(tv.tv_usec-data[0])/1000.0);

}

int main(int argc, char*argv[])

{ int len;

stuct timeval now;

char recvbuff[8192];

if(1){

printf("%s aaa.bbb.ccc.ddd\n", argv[0]);

exit(1);

}

rawsock=soeket(AF_INET, (2), IPPROTO_ICMP);

if(rawsock<0) {

perror("soeket");

exit(1);

}

bzero ( &saddr, sizeof(saddr));

saddr.sin_family=(3);

if( inet_aton( argv[1], &saddr.sin_addr) <0) {

printf("invalid IP address: %s\n", argv[1]);

exit(1);

}

signal(SICALRM, ping);

signal(SICINT, sigint);

alarm(1);

while (1){

len=read (4), recvbuff, 8192);

if( len<0 && errno=EINTR)

continue;

else it( len<0)

perror("read");

else if( len>0)

dumppkt(recvbuff, len);

}

close (5);

exit(0);

}


参考答案

更多 “ 下面是一个简单的使用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));alarm(1);}void sigint(int signo){ printf("CATCH SIGINT !!! \n");close(rawsock);exit(0);}void dumppkt(char*buf, int len){ struct ip*iph=(struct ip*)buf;int i=iph→ip_h1*4;struct icmp*icmph=(struct icmp*)buf[i];long*data=(long*)iemph→icmp_data;struct timeval tv;gettimeofday(tv, NULL);if(icmph→icmp_type! =ICMP_ECHOREPLY)return;if(icmph→icmp_id! =(getpid()0xffff))return;printf("From %s:ttl=% d seq=% d time=%.2f ms\n",inet_ntoa(iph→ip_src),iph→ip_ttl?,icmph→icmp_seq,(tv.tv_see-data[0])*1000.0+(tv.tv_usec-data[0])/1000.0);}int main(int argc, char*argv[]){ int len;stuct timeval now;char recvbuff[8192];if(1){printf("%s aaa.bbb.ccc.ddd\n", argv[0]);exit(1);}rawsock=soeket(AF_INET, (2), IPPROTO_ICMP);if(rawsock<0) {perror("soeket");exit(1);}bzero ( saddr, sizeof(saddr));saddr.sin_family=(3);if( inet_aton( argv[1], saddr.sin_addr) <0) {printf("invalid IP address: %s\n", argv[1]);exit(1);}signal(SICALRM, ping);signal(SICINT, sigint);alarm(1);while (1){len=read (4), recvbuff, 8192);if( len<0 errno=EINTR)continue;else it( len<0)perror("read");else if( len>0)dumppkt(recvbuff, len);}close (5);exit(0);} ” 相关考题
考题 在Window系统下,若使ping命令不断的向目标主机(IP为210.45.165.20)发送数据,可以使用命令()。 A、ping210.45.165.20B、ping-t210.45.165.20C、ping-n210.45.165.20D、ping-l210.45.165.20

考题 文中( 2 )处正确的答案是( )。A.ping 127.0.0.0B.ping 127.0.0.1C.ping 127.0.1.1D.ping 127.1.1.1

考题 关于Ping的使用,下面正确的有()a.pingtecb.ping-s 202. 38. 75.14c.pingboozhang@sdbd.pingwww.ustc

考题 向IP地址为192.168.1.30的主机持续ping的命令是()。 A、ping192.168.1.30–tB、ping192.168.1.30–l1000C、ping–n192.168.1.301000D、ping–l1000–t192.168.1.30

考题 使用Ping 命令对地址10.10.10.59 发送 20 次请求,以下命令正确的是( )A.ping -t 20 10.10.10.59B.ping -n 20 10.10.10.59C.ping -1 20 10.10.10.59D.ping -c 20 10.10.10.59

考题 在VRP平台上,如果想要让ping程序以指定的IP作为回显请求报文的源地址,可以使用下面哪一个参数(). A.-aB.-sC.-dD.-n

考题 下面哪一个操作可以确定TCP/IP协议的安装或运行存在问题(). A.ping10.1.1.1B.ping127.0.0.1C.ping169.254.1.1D.ping192.168.1.1

考题 ping是一个非常常用的网络测试程序,下列命令格式错误的是()。 A.ping 127.0.0.1–nB.ping www.163.com–n17C.ping 127.0.0.1–tD.ping www.163.com–l64

考题 使用Ping 命令对地址10.10.10.59 发送 20 次请求,以下命令正确的是( )A.ping -t 20 10.10.10.59 B.ping -n 20 10.10.10.59 C.ping -l 20 10.10.10.59 D.ping -c20 10.10.10.59

考题 PING程序是应用层直接使用网络层协议的一个特例。