免费注册 查看新帖 |

Chinaunix

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

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-07-04 13:38 |只看该作者 |倒序浏览
因为我要作这个程序,
所以当了这上面的源码,
但是我发现,
发送icmp数据包后
得不到回应
这是为什么?????

但是ping本机是可以通的!!!!!!!!!!!!!!!!111
谢谢

源码如下我只改动了一点)



/* PING.C  
/*  
/* ping source code distribute by cpu || digger.  
/* for unix family only. compil and link success in sco unix.  
/* i think linux no problem too. u can try it.
/* before read this code, you shoud know about the principle of  
/* tcp/ip, especially icmp protocol, u also should also know some  
/* about BSD socket API, and unix system signal programmin/g.
/*  
/* cc -o ping ping.c -lsocket, then u will get executable file,  
/* but must act as root when cc it, and then set euid attribute  
/* for this ping, then u can execute it as common user.  
/* because only root can have authority to creat raw socket.  
/*  
/* i love socket, if so do u,  
/* call me, cpu == digger  

*/

#include <sys/types.h>;
#include <sys/socket.h>;
#include <netinet/in.h>;
#include <arpa/inet.h>;
#include <fcntl.h>;
#include <unistd.h>;
#include <stdarg.h>;
#include <stdio.h>;
#include <netdb.h>;
#include <sys/ioctl.h>;
#include <net/if.h>;
#include <stdlib.h>;
#include <stdio.h>;
#include <string.h>;
#include <signal.h>;
#include <errno.h>;

#define MAX(x,y) (x>;y)?x:y
#define MIN(x,y) (x<y)?x:y
#define ICMP_ECHO 8 /* icmp echo requir */
#define ICMP_ECHOREPLY 0 /* icmp echo reply */  
#define ICMP_HEADSIZE 8 /* icmp packet header size */  
#define IP_HEADSIZE 20 /* ip packet header size */  

typedef struct tagIpHead /* icmp packet header */  
{  
  u_char ip_verlen; /* ip version and ip header lenth*/  
  u_char ip_tos; /* ip type of service */  
  u_short ip_len; /* ip packet lenghth */  
  u_short ip_id; /* ip packet identification */  
  u_short ip_fragoff; /* ip packet fragment and offset */  
  u_char ip_ttl; /* ip packet time to live */  
  u_char ip_proto; /* ip packet protocol type */  
  u_short ip_chksum; /* ip packet header checksum */  
  u_long ip_src_addr; /* ip source ip adress */  
  u_long ip_dst_addr; /* ip destination ip adress */  
} IPHEAD;  

typedef struct tagIcmpHead /* icmp header */  
{  
  u_char icmp_type; /* icmp service type */  
  /* 8 echo require, 0 echo reply */  
  u_char icmp_code; /* icmp header code */  
  u_short icmp_chksum; /* icmp header chksum */  
  u_short icmp_id; /* icmp packet identification */  
  u_short icmp_seq; /* icmp packet sequent */  
  u_char icmp_data[1]; /* icmp data, use as pointer */  
} ICMPHEAD;  

/* for check sum of icmp header */
u_short ChkSum( u_short * pIcmpData, int iDataLen )  
{  
  u_short iSum;  
  u_short iOddByte;  

  iSum = 0;  

  while ( iDataLen >; 1 )
  { /* xor the next unsigned int data */  
    iSum ^= *pIcmpData++;  
    iDataLen -= 2;  
  }  

  if ( iDataLen == 1 )
  { /* the rest odd byte */  
    iOddByte = 0;  
    *((u_char *)&iOddByte) = *(u_char *)pIcmpData;  
    iSum ^= iOddByte;  
  }  

  iSum ^= 0xffff; /* xor 0xffff == not it */  
  return(iSum);  
}  

/* since 1970.1.1 00:00:00, */
/* in 1/1000000 second */
long time_now() /* return time passed by */  
{  
  struct timeval now;  
  long lPassed;  
  gettimeofday(&now, 0);  
  lPassed = now.tv_sec * 1000000 + now.tv_usec;  
  /* now.tv_sec in second */  
  /* now.tv_usec in 1/1000000 second */  
  return lPassed;  
}  

char* host; /* destination host */  
char* prog; /* program name */  
extern errno; /* system global parameter */  
long lSendTime; /* each time when send, change it */  
u_short seq; /* the icmp packet seqence */  
int iTimeOut; /* time out parameter */  
int sock, sent, recvd, max, min, total;  
/* sent : icmp packet already sent */  
/* recvd: proper icmp packet received */  
/* max, min: max min round trip time */  
/* total: total round trip time */  
/* store to calculate average */  
u_long lHostIp; /* host ip adress */  
struct sockaddr_in it; /* destination host information */  

int ping();  
void stat();  

main(int argc, char** argv)  
{  
  struct hostent* h;  
  char buf[200];  
  char dst_host[32];  
  int i, namelen;  
  IPHEAD* pIpHead;  
  ICMPHEAD* pIcmpHead;  

  if (argc < 2)
  {
    /* ping the destination host */  



    /* every timeout second */  
    /* default timeout is 1 second */  

    printf("usage: %s [-timeout] host|IP\n", argv[0]);  
    exit(0);  
  }  
  prog = argv[0];  
  host = argc == 2 ? argv[1] : argv[2];  
  iTimeOut = argc == 2 ? 1 : atoi(argv[1]);  

  /* creat the raw socket for icmp */  

  if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
  {  
    perror("socket";
    exit(2);  
  }  

  /* set destination host information */  

  bzero(&it, sizeof(it));  
  it.sin_family = AF_INET;  

  /* check host format */  

  if ( ( lHostIp = inet_addr(host) ) != INADDR_NONE )
  {  
  /* is available ip adress */  
  it.sin_addr.s_addr = lHostIp;  
  strcpy( dst_host, host );  
  }
  else if ( h = gethostbyname(host) )
  {  
    /* is available host name */  
    /* from hosts file of local host */  
    /* or from DNS */  
    bcopy(h->;h_addr, &it.sin_addr, h->;h_length);  
    sprintf( dst_host, "%s (%s)", host,  
    inet_ntoa(it.sin_addr) );  
  }
  else
  {  
    /* bad ip adress or host name */  
    /* exit */  
    fprintf( stderr, "bad IP or host\n" );  
    exit(3);  
  }

  namelen = sizeof(it);

  printf("\nPinging %s, send %d bytes\n", dst_host,  
    IP_HEADSIZE + ICMP_HEADSIZE + sizeof(long));

  seq = 0; /* first icmp_seq = 0 */  
  sigset(SIGINT, stat); /* when press del or ctrl+c, call stat */  
  /* to statistic the result , and then exit */  
  sigset(SIGALRM, ping); /* hook ping function to timer */  
  alarm(iTimeOut); /* start timer, call ping every timeout */  
  /* seconds */  
  ping();
  for ( ;; )
  { /* waiting for every echo back */  
    /* icmp packet and check it */  
    register size;  
    register u_char ttl;  
    register delta;  
    register iIpHeadLen;  

    /* block to received echo back datagram */  

    size = recvfrom(sock, buf, sizeof(buf), 0,  
    (struct sockaddr *)&it, &namelen);
//    size = recv(sock, buf, sizeof(buf), 0);

    printf("size:%d\n", size);
    printf("errno:%d\n", errno);
    printf("eintr:%d\n", EINTR);
   
//    if (size == -1 && errno == EINTR)
    if (size == -1)
    {
      /* receive error or system call */  
      /* interrupted */
      printf("error\n";
      continue;  
    }  

    /* calculate the round trip time, */  
    /* time when receive minus time when send */  

    delta = (int)((time_now() - lSendTime)/1000);  

    /* get echo back packet and check its ip header */  

    pIpHead = (IPHEAD *)buf;  

    /* get the ip packet lenth */  
    /* if too small, not the icmp echoreply packet */  
    /* give it up */  

    iIpHeadLen = (int)((pIpHead->;ip_verlen & 0x0f) << 2);  
    if (size < iIpHeadLen + ICMP_HEADSIZE)
    {  
      continue;  
    }  
    ttl = pIpHead->;ip_ttl; /* time to live param */  

    /* get the icmp header information */  
    pIcmpHead = (ICMPHEAD *)(buf + iIpHeadLen);  

    /* not icmp echo reply packet, give it up */  
    if (pIcmpHead->;icmp_type != ICMP_ECHOREPLY)
    {  
      continue;  
    }  

    /* not proper icmp sequent number, give it up */  
    if (pIcmpHead->;icmp_id != seq || pIcmpHead->;icmp_seq != seq)
    {  
      continue;  
    }  

    /* print out result for each icmp */  
    /* echo reply information */  
    sprintf( buf, "icmp_seq=%u bytes=%d ttl=%d", pIcmpHead->;icmp_seq, size, ttl );  
    fprintf(stderr, "reply from %s: %s time=%d ms\n", host, buf, delta);  

    /* calculate some statistic information */  
    /* max, min, average round trip time */  
    /* received icmp echo reply packet numbers */  
    max = MAX(delta, max);  
    min = min ? MIN(delta, min) : delta;  
    total += delta;  
    ++ recvd;  

    /* for next icmp sequence */  

    ++ seq;  
  }  
}  

ping()  
{  
  char buf[200];  
  int iPacketSize;

  
  /* make the icmp header information */  

  ICMPHEAD *pIcmpHead;
  pIcmpHead =(ICMPHEAD *)buf;
  pIcmpHead->;icmp_type = ICMP_ECHO;  
  pIcmpHead->;icmp_code = 0;  
  pIcmpHead->;icmp_id = seq;  
  pIcmpHead->;icmp_seq = seq;  
  pIcmpHead->;icmp_chksum = 0;  

  /* store time information as icmp packet content, 4 bytes */  
  /* u may store other information instead */  

  *((long *)pIcmpHead->;icmp_data) = time_now();  

  iPacketSize = ICMP_HEADSIZE + 4; /* icmp packet length */  

  /* icmp header check sum */  

  pIcmpHead->;icmp_chksum = ChkSum((u_short *)pIcmpHead,  
  iPacketSize );  

  /* remember the time when send for calculate round trip time */  
  lSendTime = time_now();  

  printf("pIcmpHead:%s:::\n", pIcmpHead);
  /* send the icmp packet to des host */
  int s;
//  if ( (s = sendto(sock, pIcmpHead, iPacketSize, 0, (struct sockaddr *)&it, sizeof(it) )) < 0)  //change把buf该成了pIcmpHead
  if ( (s = sendto(sock, pIcmpHead, iPacketSize, 0, (struct sockaddr *)&it, sizeof(it) )) < 0)  //change
  {  
    perror("send failed";  
    exit(6);
  }
  printf("s:%d\n", s);
  printf("here\n";
  
  /* packet number been sent */  
  ++sent;  
  printf("sent:%d\n", sent);

  /* reset the timer hooker to me again */  
  alarm(iTimeOut);

}  

void stat() /* print the statistic information for this time's ping */  
{  
  if (sent)
  {  
    printf("\n----- %s ping statistics summerized -----\n", host );  
    printf("%d packets sent, %d packets received, %.2f%% lost\n",  
      sent, recvd, (float)(sent-recvd)/(float)sent*100 );  
  }  
  if (recvd)
  {  
    printf("round_trip min/avg/max: %d/%d/%d ms\n\n",  min, total/recvd, max );  
  }  
  exit(0);  
}

论坛徽章:
0
2 [报告]
发表于 2003-07-04 14:20 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!

难道没有人能帮我么??

论坛徽章:
0
3 [报告]
发表于 2003-07-04 14:45 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!

发的时候成功没有

另外看看它的说明
会不会是你自己改错了

源码排版一下看的人会更多

论坛徽章:
0
4 [报告]
发表于 2003-07-04 14:49 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!

发的时候一经编译成功了,
我不会源码排版阿

帮帮忙!!

论坛徽章:
0
5 [报告]
发表于 2003-07-04 14:52 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!



  1. /* PING.C  
  2. /*  
  3. /* ping source code distribute by cpu || digger.  
  4. /* for unix family only. compil and link success in sco unix.  
  5. /* i think linux no problem too. u can try it.
  6. /* before read this code, you shoud know about the principle of  
  7. /* tcp/ip, especially icmp protocol, u also should also know some  
  8. /* about BSD socket API, and unix system signal programmin/g.
  9. /*  
  10. /* cc -o ping ping.c -lsocket, then u will get executable file,  
  11. /* but must act as root when cc it, and then set euid attribute  
  12. /* for this ping, then u can execute it as common user.  
  13. /* because only root can have authority to creat raw socket.  
  14. /*  
  15. /* i love socket, if so do u,  
  16. /* call me, cpu == digger  

  17. */

  18. #include <sys/types.h>;
  19. #include <sys/socket.h>;
  20. #include <netinet/in.h>;
  21. #include <arpa/inet.h>;
  22. #include <fcntl.h>;
  23. #include <unistd.h>;
  24. #include <stdarg.h>;
  25. #include <stdio.h>;
  26. #include <netdb.h>;
  27. #include <sys/ioctl.h>;
  28. #include <net/if.h>;
  29. #include <stdlib.h>;
  30. #include <stdio.h>;
  31. #include <string.h>;
  32. #include <signal.h>;
  33. #include <errno.h>;

  34. #define MAX(x,y) (x>;y)?x:y
  35. #define MIN(x,y) (x<y)?x:y
  36. #define ICMP_ECHO 8 /* icmp echo requir */
  37. #define ICMP_ECHOREPLY 0 /* icmp echo reply */  
  38. #define ICMP_HEADSIZE 8 /* icmp packet header size */  
  39. #define IP_HEADSIZE 20 /* ip packet header size */  

  40. typedef struct tagIpHead /* icmp packet header */  
  41. {  
  42.   u_char ip_verlen; /* ip version and ip header lenth*/  
  43.   u_char ip_tos; /* ip type of service */  
  44.   u_short ip_len; /* ip packet lenghth */  
  45.   u_short ip_id; /* ip packet identification */  
  46.   u_short ip_fragoff; /* ip packet fragment and offset */  
  47.   u_char ip_ttl; /* ip packet time to live */  
  48.   u_char ip_proto; /* ip packet protocol type */  
  49.   u_short ip_chksum; /* ip packet header checksum */  
  50.   u_long ip_src_addr; /* ip source ip adress */  
  51.   u_long ip_dst_addr; /* ip destination ip adress */  
  52. } IPHEAD;  

  53. typedef struct tagIcmpHead /* icmp header */  
  54. {  
  55.   u_char icmp_type; /* icmp service type */  
  56.   /* 8 echo require, 0 echo reply */  
  57.   u_char icmp_code; /* icmp header code */  
  58.   u_short icmp_chksum; /* icmp header chksum */  
  59.   u_short icmp_id; /* icmp packet identification */  
  60.   u_short icmp_seq; /* icmp packet sequent */  
  61.   u_char icmp_data[1]; /* icmp data, use as pointer */  
  62. } ICMPHEAD;  

  63. /* for check sum of icmp header */
  64. u_short ChkSum( u_short * pIcmpData, int iDataLen )  
  65. {  
  66.   u_short iSum;  
  67.   u_short iOddByte;  

  68.   iSum = 0;  

  69.   while ( iDataLen >; 1 )
  70.   { /* xor the next unsigned int data */  
  71.     iSum ^= *pIcmpData++;  
  72.     iDataLen -= 2;  
  73.   }  

  74.   if ( iDataLen == 1 )
  75.   { /* the rest odd byte */  
  76.     iOddByte = 0;  
  77.     *((u_char *)&iOddByte) = *(u_char *)pIcmpData;  
  78.     iSum ^= iOddByte;  
  79.   }  

  80.   iSum ^= 0xffff; /* xor 0xffff == not it */  
  81.   return(iSum);  
  82. }  

  83. /* since 1970.1.1 00:00:00, */
  84. /* in 1/1000000 second */
  85. long time_now() /* return time passed by */  
  86. {  
  87.   struct timeval now;  
  88.   long lPassed;  
  89.   gettimeofday(&now, 0);  
  90.   lPassed = now.tv_sec * 1000000 + now.tv_usec;  
  91.   /* now.tv_sec in second */  
  92.   /* now.tv_usec in 1/1000000 second */  
  93.   return lPassed;  
  94. }  

  95. char* host; /* destination host */  
  96. char* prog; /* program name */  
  97. extern errno; /* system global parameter */  
  98. long lSendTime; /* each time when send, change it */  
  99. u_short seq; /* the icmp packet seqence */  
  100. int iTimeOut; /* time out parameter */  
  101. int sock, sent, recvd, max, min, total;  
  102. /* sent : icmp packet already sent */  
  103. /* recvd: proper icmp packet received */  
  104. /* max, min: max min round trip time */  
  105. /* total: total round trip time */  
  106. /* store to calculate average */  
  107. u_long lHostIp; /* host ip adress */  
  108. struct sockaddr_in it; /* destination host information */  

  109. int ping();  
  110. void stat();  

  111. main(int argc, char** argv)  
  112. {  
  113.   struct hostent* h;  
  114.   char buf[200];  
  115.   char dst_host[32];  
  116.   int i, namelen;  
  117.   IPHEAD* pIpHead;  
  118.   ICMPHEAD* pIcmpHead;  

  119.   if (argc < 2)
  120.   {
  121.     /* ping the destination host */  




  122.     /* every timeout second */  
  123.     /* default timeout is 1 second */  

  124.     printf("usage: %s [-timeout] host|IP\n", argv[0]);  
  125.     exit(0);  
  126.   }  
  127.   prog = argv[0];  
  128.   host = argc == 2 ? argv[1] : argv[2];  
  129.   iTimeOut = argc == 2 ? 1 : atoi(argv[1]);  

  130.   /* creat the raw socket for icmp */  

  131.   if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
  132.   {  
  133.     perror("socket");
  134.     exit(2);  
  135.   }  

  136.   /* set destination host information */  

  137.   bzero(&it, sizeof(it));  
  138.   it.sin_family = AF_INET;  

  139.   /* check host format */  

  140.   if ( ( lHostIp = inet_addr(host) ) != INADDR_NONE )
  141.   {  
  142.   /* is available ip adress */  
  143.   it.sin_addr.s_addr = lHostIp;  
  144.   strcpy( dst_host, host );  
  145.   }
  146.   else if ( h = gethostbyname(host) )
  147.   {  
  148.     /* is available host name */  
  149.     /* from hosts file of local host */  
  150.     /* or from DNS */  
  151.     bcopy(h->;h_addr, &it.sin_addr, h->;h_length);  
  152.     sprintf( dst_host, "%s (%s)", host,  
  153.     inet_ntoa(it.sin_addr) );  
  154.   }
  155.   else
  156.   {  
  157.     /* bad ip adress or host name */  
  158.     /* exit */  
  159.     fprintf( stderr, "bad IP or host\n" );  
  160.     exit(3);  
  161.   }

  162.   namelen = sizeof(it);

  163.   printf("\nPinging %s, send %d bytes\n", dst_host,  
  164.     IP_HEADSIZE + ICMP_HEADSIZE + sizeof(long));

  165.   seq = 0; /* first icmp_seq = 0 */  
  166.   sigset(SIGINT, stat); /* when press del or ctrl+c, call stat */  
  167.   /* to statistic the result , and then exit */  
  168.   sigset(SIGALRM, ping); /* hook ping function to timer */  
  169.   alarm(iTimeOut); /* start timer, call ping every timeout */  
  170.   /* seconds */  
  171.   ping();
  172.   for ( ;; )
  173.   { /* waiting for every echo back */  
  174.     /* icmp packet and check it */  
  175.     register size;  
  176.     register u_char ttl;  
  177.     register delta;  
  178.     register iIpHeadLen;  

  179.     /* block to received echo back datagram */  

  180.     size = recvfrom(sock, buf, sizeof(buf), 0,  
  181.     (struct sockaddr *)&it, &namelen);
  182. //    size = recv(sock, buf, sizeof(buf), 0);

  183.     printf("size:%d\n", size);
  184.     printf("errno:%d\n", errno);
  185.     printf("eintr:%d\n", EINTR);
  186.    
  187. //    if (size == -1 && errno == EINTR)
  188.     if (size == -1)
  189.     {
  190.       /* receive error or system call */  
  191.       /* interrupted */
  192.       printf("error\n");
  193.       continue;  
  194.     }  

  195.     printf("recvbuf:%s\n", buf);
  196.     /* calculate the round trip time, */  
  197.     /* time when receive minus time when send */  

  198.     delta = (int)((time_now() - lSendTime)/1000);  

  199.     /* get echo back packet and check its ip header */  

  200.     pIpHead = (IPHEAD *)buf;  

  201.     /* get the ip packet lenth */  
  202.     /* if too small, not the icmp echoreply packet */  
  203.     /* give it up */  

  204.     iIpHeadLen = (int)((pIpHead->;ip_verlen & 0x0f) << 2);  
  205.     if (size < iIpHeadLen + ICMP_HEADSIZE)
  206.     {  
  207.       continue;  
  208.     }  
  209.     ttl = pIpHead->;ip_ttl; /* time to live param */  

  210.     /* get the icmp header information */  
  211.     pIcmpHead = (ICMPHEAD *)(buf + iIpHeadLen);  

  212.     /* not icmp echo reply packet, give it up */  
  213.     if (pIcmpHead->;icmp_type != ICMP_ECHOREPLY)
  214.     {  
  215.       continue;  
  216.     }  

  217.     /* not proper icmp sequent number, give it up */  
  218.     if (pIcmpHead->;icmp_id != seq || pIcmpHead->;icmp_seq != seq)
  219.     {  
  220.       continue;  
  221.     }  

  222.     /* print out result for each icmp */  
  223.     /* echo reply information */  
  224.     sprintf( buf, "icmp_seq=%u bytes=%d ttl=%d", pIcmpHead->;icmp_seq, size, ttl );  
  225.     fprintf(stderr, "reply from %s: %s time=%d ms\n", host, buf, delta);  

  226.     /* calculate some statistic information */  
  227.     /* max, min, average round trip time */  
  228.     /* received icmp echo reply packet numbers */  
  229.     max = MAX(delta, max);  
  230.     min = min ? MIN(delta, min) : delta;  
  231.     total += delta;  
  232.     ++ recvd;  

  233.     /* for next icmp sequence */  

  234.     ++ seq;  
  235.   }  
  236. }  

  237. ping()  
  238. {  
  239.   char buf[200];  
  240.   int iPacketSize;

  241.   
  242.   /* make the icmp header information */  

  243.   ICMPHEAD *pIcmpHead;
  244.   pIcmpHead =(ICMPHEAD *)buf;
  245.   pIcmpHead->;icmp_type = ICMP_ECHO;  
  246.   pIcmpHead->;icmp_code = 0;  
  247.   pIcmpHead->;icmp_id = seq;  
  248.   pIcmpHead->;icmp_seq = seq;  
  249.   pIcmpHead->;icmp_chksum = 0;  

  250.   /* store time information as icmp packet content, 4 bytes */  
  251.   /* u may store other information instead */  

  252.   *((long *)pIcmpHead->;icmp_data) = time_now();  

  253.   iPacketSize = ICMP_HEADSIZE + 4; /* icmp packet length */  

  254.   /* icmp header check sum */  

  255.   pIcmpHead->;icmp_chksum = ChkSum((u_short *)pIcmpHead,  
  256.   iPacketSize );  

  257.   /* remember the time when send for calculate round trip time */  
  258.   lSendTime = time_now();  

  259.   printf("pIcmpHead:%s:::\n", pIcmpHead);
  260.   /* send the icmp packet to des host */
  261.   int s;
  262. //  if ( (s = sendto(sock, pIcmpHead, iPacketSize, 0, (struct sockaddr *)&it, sizeof(it) )) < 0)  //change
  263.   if ( (s = sendto(sock, buf, iPacketSize, 0, (struct sockaddr *)&it, sizeof(it) )) < 0)  //change
  264.   {  
  265.     perror("send failed");  
  266.     exit(6);
  267.   }
  268.   printf("s:%d\n", s);
  269.   printf("here\n");
  270.   
  271.   /* packet number been sent */  
  272.   ++sent;  

  273.   printf("sent:%d\n", sent);

  274.   /* reset the timer hooker to me again */  
  275.   alarm(iTimeOut);

  276. }  

  277. void stat() /* print the statistic information for this time's ping */  
  278. {  
  279.   if (sent)
  280.   {  
  281.     printf("\n----- %s ping statistics summerized -----\n", host );  
  282.     printf("%d packets sent, %d packets received, %.2f%% lost\n",  
  283.       sent, recvd, (float)(sent-recvd)/(float)sent*100 );  
  284.   }  
  285.   if (recvd)
  286.   {  
  287.     printf("round_trip min/avg/max: %d/%d/%d ms\n\n",  min, total/recvd, max );  
  288.   }  
  289.   exit(0);  
  290. }  







复制代码

论坛徽章:
0
6 [报告]
发表于 2003-07-04 15:03 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!


  1.         if ( (s = sendto(sock, buf, iPacketSize, 0, (struct sockaddr *)&it, sizeof(it) )) < 0)  //change
  2.         {  
  3.                 perror("send failed");  
  4.                 exit(6);
  5.         }
复制代码


这里s返回值等于几

论坛徽章:
0
7 [报告]
发表于 2003-07-04 15:05 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!

如果我没记错的话,那个源程序在作数据包首部校验和的算法有错误。
我用我的一个计算校验和的函数替代原来的chkSum就可以了。


  1. u_short myChkSum(u_short *addr, int len)
  2. {
  3.         register int nleft = len;
  4.         register u_short *w = addr;
  5.         register u_short answer;
  6.         register int sum = 0;

  7.         /*
  8.          *  Our algorithm is simple, using a 32 bit accumulator (sum),
  9.          *  we add sequential 16 bit words to it, and at the end, fold
  10.          *  back all the carry bits from the top 16 bits into the lower
  11.          *  16 bits.
  12.          */
  13.         while( nleft >; 1 )  {
  14.                 sum += *w++;
  15.                 nleft -= 2;
  16.         }

  17.         /* mop up an odd byte, if necessary */
  18.        
  19.         if( nleft == 1 ) {
  20.                 u_short        u = 0;

  21.                 *(u_char *)(&u) = *(u_char *)w ;
  22.                 sum += u;
  23.         }

  24.         /*
  25.          * add back carry outs from top 16 bits to low 16 bits
  26.          */
  27.         sum = (sum >;>; 16) + (sum & 0xffff);        /* add hi 16 to low 16 */
  28.         sum += (sum >;>; 16);                        /* add carry */
  29.         answer = ~sum;                                /* truncate to 16 bits */
  30.         return (answer);
  31. }
复制代码

论坛徽章:
0
8 [报告]
发表于 2003-07-04 15:18 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!

liupch  兄能不能介绍它的校验和产生原理

论坛徽章:
0
9 [报告]
发表于 2003-07-04 15:29 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!

[quote]原帖由 "无双"]liupch  兄能不能介绍它的校验和产生原理[/quote 发表:
     

就是标准的RCR冗余校验

论坛徽章:
0
10 [报告]
发表于 2003-07-04 15:33 |只看该作者

版主帮忙!!!关于"永远的unix上"ping的源码的调试问题!

真是厉害!!!!!!!!!!!!!!!!!11


多谢!!!!!!!!!!!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP