免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2885 | 回复: 4
打印 上一主题 下一主题

写了个ping程序,能ping自己,ping不通网关,怎么会事?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-05 11:41 |只看该作者 |倒序浏览
好象只能收到第一个包的reply
然后就没有响应了

论坛徽章:
0
2 [报告]
发表于 2005-03-05 12:03 |只看该作者

写了个ping程序,能ping自己,ping不通网关,怎么会事??


  1. #include<unistd.h>;
  2. #include<errno.h>;
  3. #include<sys/types.h>;
  4. #include<sys/socket.h>;
  5. #include<sys/time.h>;
  6. #include<signal.h>;
  7. #include<arpa/inet.h>;
  8. #include<netinet/in.h>;
  9. #include<netinet/in_systm.h>;
  10. #include<netinet/ip.h>;
  11. #include<netinet/ip_icmp.h>;
  12. #include<netdb.h>;

  13. int sock;
  14. char sendbuf[1500],recvbuf[1500];
  15. int datalen=56;
  16. int nsend=0;
  17. pid_t pid;
  18. char *host,namebuf[50];
  19. struct sockaddr_in to;
  20. struct sockaddr_in from;

  21. unsigned short in_cksum(unsigned short *,int);
  22. void err_quit(char *);
  23. void pinger(void);
  24. void ping_loop(void);
  25. void proc_pack(ssize_t ,struct timeval *);
  26. void sig_alrm(int);


  27. main(int argc,char **argv)
  28. {
  29.         struct sigaction act;
  30.         struct hostent *ht;
  31.         pid=getpid();
  32.         act.sa_handler=sig_alrm;
  33.         act.sa_flags=0;
  34.         sigemptyset(&act.sa_mask);
  35.         sigaction(SIGALRM,&act,NULL);
  36. //        signal(SIGALRM,sig_alrm);
  37.         to.sin_family=AF_INET;
  38.         to.sin_addr.s_addr=inet_addr(argv[1]);
  39.         if(to.sin_addr.s_addr!=(unsigned int)-1)
  40.                 host=argv[1];
  41.         else
  42.         {
  43.                 if(!(ht=gethostbyname(argv[1])))
  44.                         err_quit("gethostbyname");
  45.                 to.sin_family=ht->;h_addrtype;
  46.                 memmove(&to.sin_addr,ht->;h_addr,ht->;h_length);
  47.                 strncpy(namebuf,ht->;h_name,sizeof(namebuf)-1);
  48.                 host=namebuf;
  49.         }
  50.         printf("PING %s (%s): %d bytes data\n",host,
  51.                 inet_ntoa(*(struct in_addr *)&to.sin_addr.s_addr),
  52.                 datalen);
  53.         ping_loop();
  54. }
  55. void ping_loop(void)
  56. {       
  57.         int size;
  58.         socklen_t len;
  59.         ssize_t n;
  60.         struct timeval tv;
  61.         if((sock=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))<0)       
  62.                 err_quit("socket");
  63.         sig_alrm(SIGALRM);
  64.         for(;;)
  65.         {       
  66.                 len=sizeof(from);
  67.         //        alarm(1);
  68.                 n=recvfrom(sock,recvbuf,1500,0,(struct sockaddr *)&from,&len);
  69.                 if(n<0)
  70.                 {
  71.                         if(errno==EINTR)
  72.                                 continue;
  73.                         else
  74.                                 err_quit("recvfrom");
  75.                 }
  76.                 gettimeofday(&tv,NULL);
  77.                 proc_pack(n,&tv);
  78.         }
  79. }
  80. void sig_alrm(int signo)
  81. {
  82.         //signal(SIGALRM,sig_alrm);
  83.         pinger();
  84.         alarm(1);
  85.         return;
  86. }
  87. void pinger(void)
  88. {
  89.         struct icmp *icp;
  90.         int cc;
  91.         int i;
  92.         icp=(struct icmp *)sendbuf;
  93.         icp->;icmp_type=ICMP_ECHO;
  94.         icp->;icmp_code=0;
  95.         icp->;icmp_seq=nsend++;
  96.         icp->;icmp_id=pid;
  97.         gettimeofday((struct timeval *)icp->;icmp_data,NULL);
  98.         cc=datalen+8;
  99.         icp->;icmp_cksum=in_cksum((unsigned short *)icp,cc);
  100.         i=sendto(sock,sendbuf,cc,0,
  101.                 (struct sockaddr *)&to,sizeof(struct sockaddr));
  102.         if(i<0||i!=cc)
  103.                 err_quit("sendto");
  104. }       
  105. unsigned short in_cksum(unsigned short *addr,int len)
  106. {
  107.         int nleft=len;
  108.         int sum=0;
  109.         unsigned short *w=addr;
  110.         unsigned short answer=0;
  111.         while(nleft>;1)
  112.         {
  113.                 sum+=*w++;
  114.                 nleft-=2;
  115.         }
  116.         if(nleft==1)
  117.         {
  118.                 *(unsigned char *)(&answer)=*(unsigned char *)w;
  119.                 sum+=answer;
  120.         }
  121.         sum=(sum>;>;16)+(sum&0xffff);
  122.         sum+=(sum>;>;16);
  123.         answer=~sum;
  124.         return(answer);
  125. }
  126. void proc_pack(ssize_t len,struct timeval *tvrecv)
  127. {
  128.         int hlen1,icmplen;
  129.         double rtt;
  130.         struct ip *ip;
  131.         struct icmp *icmp;       
  132.         struct timeval *tvsend;
  133.         char buf[50];
  134.         ip=(struct ip *)recvbuf;
  135.         hlen1=ip->;ip_hl<<2;
  136.         icmp=(struct icmp *)(recvbuf+hlen1);
  137.         if((icmplen=len-hlen1)<8)
  138.                 err_quit("icmplen1");
  139.         if(icmp->;icmp_type==ICMP_ECHOREPLY)
  140.         {
  141.                 if(icmp->;icmp_id!=pid)
  142.                         return;
  143.                 if(icmplen<16)
  144.                         err_quit("icmplen2");
  145.                 printf("%d bytes from %s:seq=%u,ttl=%d\n",
  146.                    icmplen,
  147.                    inet_ntoa(from.sin_addr),
  148.                    icmp->;icmp_seq,
  149.                    ip->;ip_ttl);
  150.         }
  151. }
  152. void err_quit(char *msg)
  153. {
  154.         perror(msg);       
  155.         exit(1);
  156. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2005-03-05 12:06 |只看该作者

写了个ping程序,能ping自己,ping不通网关,怎么会事??

调试时发现recvfrom总是中断,然后continue,怎么不调用sig_alrm呀

论坛徽章:
0
4 [报告]
发表于 2005-03-05 12:23 |只看该作者

写了个ping程序,能ping自己,ping不通网关,怎么会事??

参考了unix网络编程卷1和4.4bsd 的ping代码,怎么就是不行??

论坛徽章:
0
5 [报告]
发表于 2005-03-05 23:24 |只看该作者

写了个ping程序,能ping自己,ping不通网关,怎么会事??

改好了,每次调用pinger()前把sendbuf清0

  1.     bzero(sendbuf,1500);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP