📄 waitpacket.c
字号:
{ status = rtt(&sequence, ntohs(udp.uh_dport), &ms_delay); if (!opt_quiet) { log_ip(status, sequence); printf("seq=%d rtt=%.1f ms\n", sequence, ms_delay); } if (opt_incdport && !opt_force_incdport) dst_port++; return 1; } return 0;}int recv_tcp(void *packet, size_t size){ struct mytcphdr tcp; int sequence = 0, status; float ms_delay; char flags[16]; if (size < TCPHDR_SIZE) { printf("[|tcp]\n"); return 0; } memcpy(&tcp, packet, sizeof(tcp)); /* check if the packet matches */ if ((ntohs(tcp.th_sport) == dst_port) || (opt_force_incdport && (ntohs(tcp.th_sport) >= base_dst_port && ntohs(tcp.th_sport) <= dst_port))) { tcp_exitcode = tcp.th_flags; status = rtt(&sequence, ntohs(tcp.th_dport), &ms_delay); if (opt_seqnum) { static __u32 old_th_seq = 0; __u32 seq_diff, tmp; tmp = ntohl(tcp.th_seq); if (tmp >= old_th_seq) seq_diff = tmp - old_th_seq; else seq_diff = (4294967295U - old_th_seq) + tmp; old_th_seq = tmp; printf("%10lu +%lu\n", (unsigned long) tmp, (unsigned long) seq_diff); goto out; } if (opt_quiet) goto out; flags[0] = '\0'; if (tcp.th_flags & TH_RST) strcat(flags, "R"); if (tcp.th_flags & TH_SYN) strcat(flags, "S"); if (tcp.th_flags & TH_ACK) strcat(flags, "A"); if (tcp.th_flags & TH_FIN) strcat(flags, "F"); if (tcp.th_flags & TH_PUSH) strcat(flags, "P"); if (tcp.th_flags & TH_URG) strcat(flags, "U"); if (tcp.th_flags & TH_X) strcat(flags, "X"); if (tcp.th_flags & TH_Y) strcat(flags, "Y"); if (flags[0] == '\0') strcat(flags, "none"); log_ip(status, sequence); printf("sport=%d flags=%s seq=%d win=%d rtt=%.1f ms\n", ntohs(tcp.th_sport), flags, sequence, ntohs(tcp.th_win), ms_delay); if (opt_verbose) { printf("seq=%lu ack=%lu sum=%x urp=%u\n\n", (unsigned long) ntohl(tcp.th_seq), (unsigned long) ntohl(tcp.th_ack), tcp.th_sum, ntohs(tcp.th_urp)); } /* Get and log the TCP timestamp */ if (opt_tcp_timestamp) print_tcp_timestamp(packet, size);out: if (opt_incdport && !opt_force_incdport) dst_port++; return 1; } return 0;}/* Try to extract information about the original packet from the * ICMP error to obtain the round time trip * * Note that size is the the packet size starting from the * IP packet quoted in the ICMP error, it may be negative * if the ICMP is broken */#ifndef WIN32int icmp_unreach_rtt(void *quoted_ip, int size, int *seqp, float *ms_delay)#elseint icmp_unreach_rtt(void *quoted_ip2, int size, int *seqp, float *ms_delay)#endif{ int src_port; int sequence = 0; int quoted_iphdr_size; struct myudphdr udp; struct myicmphdr icmp; struct myiphdr qip;#ifdef WIN32 char *quoted_ip = quoted_ip2;#endif /* The user specified --no-rtt */ if (opt_tr_no_rtt) return -1; if (size < sizeof(struct myiphdr)) return -1; memcpy(&qip, quoted_ip, sizeof(struct myiphdr)); quoted_iphdr_size = qip.ihl << 2; /* Ok, enough room, try to get the rtt, * but check if the original packet was an UDP/TCP one */ if (qip.protocol == IPPROTO_TCP || qip.protocol == IPPROTO_UDP) { /* We need at least 2 bytes of the quoted UDP/TCP header * for the source port */ if ((size - quoted_iphdr_size) < 2) return -1; /* Use the UDP header for both UDP and TCP, they are * the same in the 4 first bytes (source and dest port) */ memcpy(&udp, quoted_ip+quoted_iphdr_size, sizeof(udp)); src_port = htons(udp.uh_sport); return rtt(&sequence, src_port, ms_delay); } else if (qip.protocol == IPPROTO_ICMP) { int s; /* We need the whole 8 byte ICMP header to get * the sequence field, also the type must be * ICMP_ECHO */ memcpy(&icmp, quoted_ip+quoted_iphdr_size, sizeof(icmp)); if ((size - quoted_iphdr_size) < 8 || icmp.type != ICMP_ECHO) return -1; s = icmp.un.echo.sequence; return rtt(&s, 0, ms_delay); } return -1; /* no way */}void print_tcp_timestamp(void *tcp, int tcpsize){ int optlen; unsigned char *opt; __u32 tstamp, echo; static __u32 last_tstamp = 0; struct mytcphdr tmptcphdr; unsigned int tcphdrlen; if (tcpsize < TCPHDR_SIZE) return; memcpy(&tmptcphdr, tcp, sizeof(struct mytcphdr)); tcphdrlen = tmptcphdr.th_off * 4; /* bad len or no options in the TCP header */ if (tcphdrlen <= 20 || tcphdrlen < tcpsize) return; optlen = tcphdrlen - TCPHDR_SIZE; opt = (unsigned char*)tcp + TCPHDR_SIZE; /* skips the TCP fix header */ while(optlen) { switch(*opt) { case 0: /* end of option */ return; case 1: /* noop */ opt++; optlen--; continue; default: if (optlen < 2) return; if (opt[1] > optlen) return; if (opt[0] != 8) { /* not timestamp */ optlen -= opt[1]; opt += opt[1]; continue; } /* timestamp found */ if (opt[1] != 10) /* bad len */ return; memcpy(&tstamp, opt+2, 4); memcpy(&echo, opt+6, 4); tstamp = ntohl(tstamp); echo = ntohl(echo); goto found; } }found: printf(" TCP timestamp: tcpts=%u\n", tstamp); if (last_tstamp && !opt_waitinusec) { int tsdiff = (tstamp - last_tstamp) / sending_wait; int hz_set[] = { 2, 10, 100, 1000, 0 }; int hzdiff = -1; int hz = 0, sec; int days, hours, minutes; if (tsdiff > 0) { int i = 0; while(hz_set[i]) { if (hzdiff == -1) { hzdiff = ABS(tsdiff-hz_set[i]); hz = hz_set[i]; } else if (hzdiff > ABS(tsdiff-hz_set[i])) { hzdiff = ABS(tsdiff-hz_set[i]); hz = hz_set[i]; } i++; } printf(" HZ seems hz=%d\n", hz); sec = tstamp/hz; /* Get the uptime in seconds */ days = sec / (3600*24); sec %= 3600*24; hours = sec / 3600; sec %= 3600; minutes = sec / 60; sec %= 60; printf(" System uptime seems: %d days, %d hours, " "%d minutes, %d seconds\n", days, hours, minutes, sec); } } printf("\n"); last_tstamp = tstamp;}/* This function is exported to listen.c also */int read_packet(void *packet, int size){#if (!defined OSTYPE_LINUX) || (defined FORCE_LIBPCAP) size = pcap_recv(packet, size); if (size == -1) perror("[wait_packet] pcap_recv()");#else size = recv(sockpacket, packet, size, 0); if (size == -1) { if (errno != EINTR) perror("[wait_packet] recv"); else return 0; }#endif return size;}#ifndef WIN32void hex_dump(void *packet, int size)#elsevoid hex_dump(void *packet2, int size)#endif{#ifndef WIN32 unsigned char *byte = packet;#else unsigned char *byte = (unsigned char *)packet2;#endif int count = 0;#ifdef WIN32 unsigned char *packet = byte;#endif printf("\t\t"); for (; byte < (unsigned char*) (packet+size); byte++) { count++; printf("%02x", *byte); if (count % 2 == 0) printf(" "); if (count % 16 == 0) printf("\n\t\t"); } printf("\n\n");}#ifndef WIN32void human_dump(void *packet, int size)#elsevoid human_dump(void *packet2, int size)#endif{#ifndef WIN32 unsigned char *byte = packet;#else unsigned char *byte = packet2;#endif int count = 0;#ifdef WIN32 unsigned char *packet = byte;#endif printf("\t\t"); for (; byte < (unsigned char*) (packet+size); byte++) { count ++; if (isprint(*byte)) printf("%c", *byte); else printf("."); if (count % 32 == 0) printf("\n\t\t"); } printf("\n\n");}void handle_hcmp(char *packet, int size){ char *p; struct hcmphdr hcmph; unsigned int seq; /* Search for the reverse signature inside the packet */ if ((p = memstr(packet, rsign, size)) == NULL) return; if (opt_debug) fprintf(stderr, "DEBUG: HCMP received\n"); p+=strlen(rsign); if ((size-(packet-p)) < sizeof(struct hcmphdr)) { if (opt_verbose || opt_debug) fprintf(stderr, "bad HCMP len received\n"); return; } memcpy(&hcmph, p, sizeof(hcmph)); switch(hcmph.type) { case HCMP_RESTART: seq = ntohs(hcmph.typedep.seqnum); src_id = seq; /* set the id */ datafiller(NULL, seq); /* data seek */ if (opt_debug) printf("DEBUG: HCMP restart from %d\n", seq); return; case HCMP_SOURCE_QUENCH: case HCMP_SOURCE_STIRUP: printf("HCMP source quench/stirup received\n"); return; default: if (opt_verbose || opt_debug) fprintf(stderr, "bad HCMP type received\n"); return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -