- // Module: Traceroute.c
- //
- // Description:
- // This sample is fairly similar to the ping.c sample. It
- // creates ICMP packets and sends them to the intended
- // recipient. The time-to-live value for the ICMP packet
- // is initially 1 and is incremented by one everytime
- // an ICMP response is received. This way we can look
- // at the source IP address of these replies to find
- // the route to the intended recipient. Once we receive
- // an ICMP port unreachable message we know that we have
- // reached the inteded recipient and we can stop.
- //
- // Compile:
- // cl -o Traceroute Traceroute.c ws_32.lib /Zp1
- //
- // Command Line Options/Parameters
- // traceroute.exe hostname [max-hops]
- // hostname Hostname or IP dotted decimal address of
- // the endpoint.
- // max-hops Maximum number of hops (routers) to
- // traverse to the endpoint before quitting.
- //
- #pragma pack(4)
- #pragma comment(lib, "ws2_32.lib")
- #define WIN32_LEAN_AND_MEAN
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdio.h>
- #include <stdlib.h>
- //
- // Defines for ICMP message types
- //
- #define ICMP_ECHOREPLY 0
- #define ICMP_DESTUNREACH 3
- #define ICMP_SRCQUENCH 4
- #define ICMP_REDIRECT 5
- #define ICMP_ECHO 8
- #define ICMP_TIMEOUT 11
- #define ICMP_PARMERR 12
- #define MAX_HOPS 30
- #define ICMP_MIN 8 // Minimum 8 byte icmp packet (just header)
- //
- // IP Header
- //
- typedef struct iphdr
- {
- unsigned int h_len:4; // Length of the header
- unsigned int version:4; // Version of IP
- unsigned char tos; // Type of service
- unsigned short total_len; // Total length of the packet
- unsigned short ident; // Unique identifier
- unsigned short frag_and_flags; // Flags
- unsigned char ttl; // Time to live
- unsigned char proto; // Protocol (TCP, UDP etc)
- unsigned short checksum; // IP checksum
- unsigned int sourceIP; // Source IP
- unsigned int destIP; // Destination IP
- } IpHeader;
- //
- // ICMP header
- //
- typedef struct _ihdr </
在windows下实现traceroute功能的源代码
最新推荐文章于 2025-02-26 00:15:00 发布