1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
| #include <WinSock2.h> #include <ws2spi.h> #include <ws2tcpip.h> #include <strsafe.h> #include <algorithm> #include "rtc_base/networkprotocolheader.h" #include "rtc_base/checks.h" #include "rtc_base/flags.h" #include "rtc_base/timeutils.h"
DECLARE_bool(h); DECLARE_int(m); DECLARE_int(w); DECLARE_int(s); DECLARE_int(n);
DEFINE_bool(h, false, "帮助"); DEFINE_int(m, 30, "最大跃点数"); DEFINE_int(w, 3000, "等待每次回复的超时时间(毫秒)"); DEFINE_int(s, 3000, "发送ICMP包超时时间(毫秒)"); DEFINE_int(n, 3, "每个跃点发送的请求数");
const int kPingDataSize = 36; __u32 kLocalIP = 0;
__u32 GetLocalIPv4Address() { return inet_addr("192.168.42.26"); char hostname[MAX_PATH] = { 0 }; gethostname(hostname, MAX_PATH); struct hostent FAR* lpHostEnt = gethostbyname(hostname); if (lpHostEnt == NULL) { return htonl(0x7f000001); }
LPSTR lpAddr = lpHostEnt->h_addr_list[0];
struct in_addr addr; memcpy(&addr, lpAddr, 4);
return addr.s_addr; }
std::string IPv4ToString(__u32 ip) { in_addr addr; addr.s_addr = ip; char *p= inet_ntoa(addr); if (p) return p; return ""; }
std::string GetPrintString(const char* fmt, ...) { char buf[100]; va_list arglist; va_start(arglist, fmt);
StringCchVPrintfA(buf, 100, fmt, arglist); va_end(arglist);
return buf; }
bool print_ip(__u32* ips, int count, __u32 dest_ip) { bool has_ip = false; bool trace_end = false;
for (int i = 0; i < count; i++) { if (ips[i] != 0) { printf(" %s", IPv4ToString(ips[i]).c_str()); has_ip = true; trace_end = (ips[i] == dest_ip); } } if (!has_ip) printf(" timeout");
printf("\n");
if (trace_end) printf("Trace Complete\n"); return trace_end; }
void FillRequestIPPacket(__u8* ip_packet, __u16 ip_packet_size, __u16 seq, __u8 ttl, __u32 dest_addr) { RTC_DCHECK(ip_packet);
iphdr* p_iphdr = reinterpret_cast<iphdr*>(ip_packet); memset(p_iphdr, 0, sizeof(iphdr)); p_iphdr->version = 4; p_iphdr->ihl = sizeof(iphdr)/4; p_iphdr->tos = 0; p_iphdr->frag_off = 0; p_iphdr->id = (__u16)rtc::Time32(); p_iphdr->ttl = ttl; p_iphdr->protocol = IPPROTO_ICMP; p_iphdr->tot_len = ip_packet_size; p_iphdr->daddr = dest_addr; p_iphdr->saddr = kLocalIP; p_iphdr->check = rtc::GetCheckSum(reinterpret_cast<__u16*>(p_iphdr), p_iphdr->ihl*4);
ping_hdr* p_ping_hdr = reinterpret_cast<ping_hdr*>(ip_packet + p_iphdr->ihl*4); p_ping_hdr->common_hdr.type = 8; p_ping_hdr->common_hdr.code = 0; p_ping_hdr->id = (__u16)GetCurrentProcessId(); p_ping_hdr->seq = seq;
if (kPingDataSize > 0) memset((void*)((__u8*)p_ping_hdr+sizeof(ping_hdr)), 'E', kPingDataSize);
p_ping_hdr->common_hdr.check = 0; p_ping_hdr->common_hdr.check = rtc::GetCheckSum(reinterpret_cast<__u16*>(p_ping_hdr), ip_packet_size - p_iphdr->ihl*4); }
bool DecodeIPPacket(const __u8* ip_packet, __u16 ip_packet_size, __u32 send_time, __u32* src_addr) { const iphdr* ip_hdr = reinterpret_cast<const iphdr*>(ip_packet); __u32 use_time = rtc::Time32() - send_time;
__u16 ip_hdr_len = ip_hdr->ihl * 4;
const icmp_common_hdr *icmp_hdr = reinterpret_cast<const icmp_common_hdr*>(ip_packet + ip_hdr_len); if (icmp_hdr->type == 0 && icmp_hdr->code == 0) { const ping_hdr *p_ping_hdr = reinterpret_cast<const ping_hdr*>(icmp_hdr); if (p_ping_hdr->id != (__u16)GetCurrentProcessId()) { printf("other process ping response packet, pid=%d\n", GetCurrentProcessId()); return false; }
printf("%-10s", GetPrintString("<%d ms", use_time == 0 ? 1 : use_time).c_str()); *src_addr = ip_hdr->saddr; return true; } else if (icmp_hdr->type == 11 && icmp_hdr->code == 0) { printf("%-10s", GetPrintString("<%d ms", use_time == 0 ? 1 : use_time).c_str());
*src_addr = ip_hdr->saddr; return true; } else { printf("unexpected response, type=%d, code=%d\n", icmp_hdr->type, icmp_hdr->code); return false; } }
#define SAFE_RELEASE \ if (req_ip_packet) { \ delete[] req_ip_packet; \ req_ip_packet = NULL;\ }\ if (rsp_ip_packet) { \ delete[] rsp_ip_packet; \ rsp_ip_packet = NULL;\ }\ if (s != INVALID_SOCKET) {\ closesocket(s);\ s = INVALID_SOCKET;\ }\ WSACleanup();
int main(int argc, char**argv) { rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true); if (FLAG_h) { rtc::FlagList::Print(NULL, false); return 1; }
char *hostname = argv[argc - 1]; if (!hostname || strlen(hostname) == 0) { printf("Invalid host name\n"); return 1; }
printf("Trace %s\n", hostname);
WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); WSAStartup(wVersionRequested, &wsaData);
sockaddr_in from; int from_len = sizeof(sockaddr_in);
kLocalIP = GetLocalIPv4Address();
sockaddr_in dest; memset(&dest, 0, sizeof(sockaddr_in)); dest.sin_family = AF_INET; dest.sin_addr.s_addr = inet_addr(hostname);
if (dest.sin_addr.s_addr == INADDR_NONE) { unsigned long begin_time = rtc::Time32(); struct addrinfo* result = nullptr; struct addrinfo hints = { 0 }; hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_ADDRCONFIG; int ret = getaddrinfo(hostname, nullptr, &hints, &result); if (ret != 0) { printf("Resolve host name failed, error code = %d\n", ret); return 1; } unsigned long end_time = rtc::Time32(); struct addrinfo* cursor = result; printf("------------------------------\n"); printf("Resolve [time < %d ms]: \n", end_time - begin_time); bool flag = false; for (; cursor; cursor = cursor->ai_next) { sockaddr_in *paddr_in = reinterpret_cast<sockaddr_in *>(cursor->ai_addr); printf("%s\n", inet_ntoa(paddr_in->sin_addr));
if (!flag) { dest.sin_addr = paddr_in->sin_addr; flag = true; } } freeaddrinfo(result); printf("-------------------------------\n"); }
printf("Tracing %s [%d max hops]: \n", inet_ntoa(dest.sin_addr), FLAG_m);
SOCKET s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if (s == INVALID_SOCKET) { printf("create socket failed, error code = %d\n", WSAGetLastError()); WSACleanup(); return 1; }
int err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&FLAG_s), sizeof(FLAG_s)); RTC_DCHECK(err != SOCKET_ERROR); if (err == SOCKET_ERROR) { printf("setsockopt for SO_SNDTIMEO failed, error code = %d\n", WSAGetLastError()); closesocket(s); WSACleanup(); return 1; }
err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&FLAG_w), sizeof(FLAG_w)); RTC_DCHECK(err != SOCKET_ERROR); if (err == SOCKET_ERROR) { printf("setsockopt for SO_RCVTIMEO failed, error code = %d\n", WSAGetLastError()); closesocket(s); WSACleanup(); return 1; }
int opt = 1; err = setsockopt(s, IPPROTO_IP, IP_HDRINCL, reinterpret_cast<const char*>(&opt), sizeof(opt)); RTC_DCHECK(err != SOCKET_ERROR); if (err == SOCKET_ERROR) { printf("setsockopt for IP_HDRINCL failed, error code = %d\n", WSAGetLastError()); closesocket(s); WSACleanup(); return 1; }
__u16 req_ip_packet_size = sizeof(iphdr) + sizeof(ping_hdr) + kPingDataSize; __u8 *req_ip_packet = new __u8[req_ip_packet_size]; memset(req_ip_packet, 0, req_ip_packet_size);
__u16 rsp_ip_packet_size = sizeof(iphdr) + sizeof(icmp_common_hdr) + 4 + sizeof(iphdr) + 8;
rsp_ip_packet_size = std::max(rsp_ip_packet_size, req_ip_packet_size);
__u8 *rsp_ip_packet = new __u8[rsp_ip_packet_size]; memset(rsp_ip_packet, 0, rsp_ip_packet_size); RTC_DCHECK(rsp_ip_packet);
int ttl = 1; int seq = 0; __u32 *ips = new __u32[FLAG_n];
for(int hop = 1; hop <= FLAG_m; hop++) { printf(" %-4d", hop);
for (int i = 0; i < FLAG_n; i++) { ips[i] = 0; seq++; FillRequestIPPacket(req_ip_packet, req_ip_packet_size, seq, ttl, dest.sin_addr.s_addr);
__u32 send_time = rtc::Time32(); int sent = sendto(s, reinterpret_cast<const char*>(req_ip_packet), req_ip_packet_size, 0, reinterpret_cast<const sockaddr*>(&dest), sizeof(sockaddr));
if (sent == SOCKET_ERROR) { printf("%-10s", "*");
if (i == FLAG_n - 1) if (print_ip(ips, FLAG_n, dest.sin_addr.s_addr)) { SAFE_RELEASE; return 0; } continue; }
int bread = recvfrom(s, reinterpret_cast<char*>(rsp_ip_packet), rsp_ip_packet_size, 0, reinterpret_cast<sockaddr*>(&from), &from_len);
if (bread == SOCKET_ERROR) { int gle = WSAGetLastError(); printf("%-10s", "*");
if (i == FLAG_n - 1) if (print_ip(ips, FLAG_n, dest.sin_addr.s_addr)) { SAFE_RELEASE; return 0; } continue; }
__u32 dest_ip = 0; DecodeIPPacket(reinterpret_cast<const __u8*>(rsp_ip_packet), rsp_ip_packet_size, send_time, &dest_ip); ips[i] = dest_ip;
if (i == FLAG_n - 1) if (print_ip(ips, FLAG_n, dest.sin_addr.s_addr)) { SAFE_RELEASE; return 0; } }
ttl++; }
SAFE_RELEASE; return 0; }
|