在windows下实现traceroute功能的源代码

  1. // Module: Traceroute.c
  2. //
  3. // Description:
  4. //    This sample is fairly similar to the ping.c sample. It 
  5. //    creates ICMP packets and sends them to the intended 
  6. //    recipient. The time-to-live value for the ICMP packet 
  7. //    is initially 1 and is incremented by one everytime
  8. //    an ICMP response is received. This way we can look
  9. //    at the source IP address of these replies to find
  10. //    the route to the intended recipient. Once we receive
  11. //    an ICMP port unreachable message we know that we have
  12. //    reached the inteded recipient and we can stop.
  13. //
  14. // Compile:
  15. //    cl -o Traceroute Traceroute.c ws_32.lib /Zp1
  16. //
  17. // Command Line Options/Parameters
  18. //    traceroute.exe hostname [max-hops]
  19. //    hostname         Hostname or IP dotted decimal address of
  20. //                      the endpoint.
  21. //    max-hops         Maximum number of hops (routers) to 
  22. //                      traverse to the endpoint before quitting.
  23. //
  24. #pragma pack(4)
  25. #pragma comment(lib, "ws2_32.lib")
  26. #define WIN32_LEAN_AND_MEAN
  27. #include <winsock2.h>
  28. #include <ws2tcpip.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. //
  32. // Defines for ICMP message types
  33. //
  34. #define ICMP_ECHOREPLY      0
  35. #define ICMP_DESTUNREACH    3
  36. #define ICMP_SRCQUENCH      4
  37. #define ICMP_REDIRECT       5
  38. #define ICMP_ECHO           8
  39. #define ICMP_TIMEOUT       11
  40. #define ICMP_PARMERR       12
  41. #define MAX_HOPS           30
  42. #define ICMP_MIN 8    // Minimum 8 byte icmp packet (just header)
  43. //
  44. // IP Header
  45. //
  46. typedef struct iphdr 
  47. {
  48.     unsigned int   h_len:4;        // Length of the header
  49.     unsigned int   version:4;      // Version of IP
  50.     unsigned char  tos;            // Type of service
  51.     unsigned short total_len;      // Total length of the packet
  52.     unsigned short ident;          // Unique identifier
  53.     unsigned short frag_and_flags; // Flags
  54.     unsigned char  ttl;            // Time to live
  55.     unsigned char  proto;          // Protocol (TCP, UDP etc)
  56.     unsigned short checksum;       // IP checksum
  57.     unsigned int   sourceIP;       // Source IP
  58.     unsigned int   destIP;         // Destination IP
  59. } IpHeader;
  60. //
  61. // ICMP header
  62. //
  63. typedef struct _ihdr </
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值