ping.c (17474) | ping.c (17724) |
---|---|
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Muuss. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 91 unchanged lines hidden (view full) --- 100#define F_INTERVAL 0x002 101#define F_NUMERIC 0x004 102#define F_PINGFILLED 0x008 103#define F_QUIET 0x010 104#define F_RROUTE 0x020 105#define F_SO_DEBUG 0x040 106#define F_SO_DONTROUTE 0x080 107#define F_VERBOSE 0x100 | 1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Muuss. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 91 unchanged lines hidden (view full) --- 100#define F_INTERVAL 0x002 101#define F_NUMERIC 0x004 102#define F_PINGFILLED 0x008 103#define F_QUIET 0x010 104#define F_RROUTE 0x020 105#define F_SO_DEBUG 0x040 106#define F_SO_DONTROUTE 0x080 107#define F_VERBOSE 0x100 |
108#define F_QUIET2 0x200 |
|
108 109/* 110 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 111 * number of received sequence numbers we can keep track of. Change 128 112 * to 8192 for complete accuracy... 113 */ 114#define MAX_DUP_CHK (8 * 128) 115int mx_dup_ck = MAX_DUP_CHK; --- 61 unchanged lines hidden (view full) --- 177 preload = 0; 178 if (tcgetattr (0, &ts) != -1) { 179 reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 180 ts.c_lflag |= NOKERNINFO; 181 tcsetattr (0, TCSANOW, &ts); 182 } 183 184 datap = &outpack[8 + sizeof(struct timeval)]; | 109 110/* 111 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 112 * number of received sequence numbers we can keep track of. Change 128 113 * to 8192 for complete accuracy... 114 */ 115#define MAX_DUP_CHK (8 * 128) 116int mx_dup_ck = MAX_DUP_CHK; --- 61 unchanged lines hidden (view full) --- 178 preload = 0; 179 if (tcgetattr (0, &ts) != -1) { 180 reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 181 ts.c_lflag |= NOKERNINFO; 182 tcsetattr (0, TCSANOW, &ts); 183 } 184 185 datap = &outpack[8 + sizeof(struct timeval)]; |
185 while ((ch = getopt(argc, argv, "Rc:dfh:i:l:np:qrs:v")) != EOF) | 186 while ((ch = getopt(argc, argv, "QRc:dfh:i:l:np:qrs:v")) != EOF) |
186 switch(ch) { 187 case 'c': 188 npackets = atoi(optarg); 189 if (npackets <= 0) { 190 (void)fprintf(stderr, 191 "ping: bad number of packets to transmit.\n"); 192 exit(1); 193 } --- 29 unchanged lines hidden (view full) --- 223 break; 224 case 'n': 225 options |= F_NUMERIC; 226 break; 227 case 'p': /* fill buffer with user pattern */ 228 options |= F_PINGFILLED; 229 fill((char *)datap, optarg); 230 break; | 187 switch(ch) { 188 case 'c': 189 npackets = atoi(optarg); 190 if (npackets <= 0) { 191 (void)fprintf(stderr, 192 "ping: bad number of packets to transmit.\n"); 193 exit(1); 194 } --- 29 unchanged lines hidden (view full) --- 224 break; 225 case 'n': 226 options |= F_NUMERIC; 227 break; 228 case 'p': /* fill buffer with user pattern */ 229 options |= F_PINGFILLED; 230 fill((char *)datap, optarg); 231 break; |
232 case 'Q': 233 options |= F_QUIET2; 234 break; |
|
231 case 'q': 232 options |= F_QUIET; 233 break; 234 case 'R': 235 options |= F_RROUTE; 236 break; 237 case 'r': 238 options |= F_SO_DONTROUTE; --- 185 unchanged lines hidden (view full) --- 424 } 425} 426 427/* 428 * pinger -- 429 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 430 * will be added on by the kernel. The ID field is our UNIX process ID, 431 * and the sequence number is an ascending integer. The first 8 bytes | 235 case 'q': 236 options |= F_QUIET; 237 break; 238 case 'R': 239 options |= F_RROUTE; 240 break; 241 case 'r': 242 options |= F_SO_DONTROUTE; --- 185 unchanged lines hidden (view full) --- 428 } 429} 430 431/* 432 * pinger -- 433 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 434 * will be added on by the kernel. The ID field is our UNIX process ID, 435 * and the sequence number is an ascending integer. The first 8 bytes |
432 * of the data portion are used to hold a UNIX "timeval" struct in VAX | 436 * of the data portion are used to hold a UNIX "timeval" struct in host |
433 * byte-order, to compute the round-trip time. 434 */ 435pinger() 436{ 437 register struct icmp *icp; 438 register int cc; 439 int i; 440 --- 123 unchanged lines hidden (view full) --- 564 (void)printf("\n\t"); 565 (void)printf("%x ", *cp); 566 } 567 break; 568 } 569 } 570 } 571 } else { | 437 * byte-order, to compute the round-trip time. 438 */ 439pinger() 440{ 441 register struct icmp *icp; 442 register int cc; 443 int i; 444 --- 123 unchanged lines hidden (view full) --- 568 (void)printf("\n\t"); 569 (void)printf("%x ", *cp); 570 } 571 break; 572 } 573 } 574 } 575 } else { |
572 /* We've got something other than an ECHOREPLY */ 573 if (!(options & F_VERBOSE)) 574 return; 575 (void)printf("%d bytes from %s: ", cc, 576 pr_addr(from->sin_addr.s_addr)); 577 pr_icmph(icp); | 576 /* 577 * We've got something other than an ECHOREPLY. 578 * See if it's a reply to something that we sent. 579 * We can compare IP destination, protocol, 580 * and ICMP type and ID. 581 */ 582#ifndef icmp_data 583 struct ip *oip = &icp->icmp_ip; 584#else 585 struct ip *oip = (struct ip *)icp->icmp_data; 586#endif 587 struct icmp *oicmp = (struct icmp *)(oip + 1); 588 589 if ((options & F_VERBOSE) || 590 (!(options & F_QUIET2) && 591 (oip->ip_dst.s_addr == 592 ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && 593 (oip->ip_p == IPPROTO_ICMP) && 594 (oicmp->icmp_type == ICMP_ECHO) && 595 (oicmp->icmp_id == ident))) { 596 (void)printf("%d bytes from %s: ", cc, 597 pr_addr(from->sin_addr.s_addr)); 598 pr_icmph(icp); 599 } else 600 return; |
578 } 579 580 /* Display any IP options */ 581 cp = (u_char *)buf + sizeof(struct ip); 582 583 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 584 switch (*cp) { 585 case IPOPT_EOL: --- 227 unchanged lines hidden (view full) --- 813 break; 814 case ICMP_UNREACH_PROTOCOL: 815 (void)printf("Destination Protocol Unreachable\n"); 816 break; 817 case ICMP_UNREACH_PORT: 818 (void)printf("Destination Port Unreachable\n"); 819 break; 820 case ICMP_UNREACH_NEEDFRAG: | 601 } 602 603 /* Display any IP options */ 604 cp = (u_char *)buf + sizeof(struct ip); 605 606 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 607 switch (*cp) { 608 case IPOPT_EOL: --- 227 unchanged lines hidden (view full) --- 836 break; 837 case ICMP_UNREACH_PROTOCOL: 838 (void)printf("Destination Protocol Unreachable\n"); 839 break; 840 case ICMP_UNREACH_PORT: 841 (void)printf("Destination Port Unreachable\n"); 842 break; 843 case ICMP_UNREACH_NEEDFRAG: |
821 (void)printf("frag needed and DF set\n"); | 844 (void)printf("frag needed and DF set (MTU %d)\n", 845 icp->icmp_nextmtu); |
822 break; 823 case ICMP_UNREACH_SRCFAIL: 824 (void)printf("Source Route Failed\n"); 825 break; | 846 break; 847 case ICMP_UNREACH_SRCFAIL: 848 (void)printf("Source Route Failed\n"); 849 break; |
850 case ICMP_UNREACH_FILTER_PROHIB: 851 (void)printf("Communication prohibited by filter\n"); 852 break; |
|
826 default: 827 (void)printf("Dest Unreachable, Bad Code: %d\n", 828 icp->icmp_code); 829 break; 830 } 831 /* Print returned IP header information */ 832#ifndef icmp_data 833 pr_retip(&icp->icmp_ip); --- 77 unchanged lines hidden (view full) --- 911 case ICMP_IREQ: 912 (void)printf("Information Request\n"); 913 /* XXX ID + Seq */ 914 break; 915 case ICMP_IREQREPLY: 916 (void)printf("Information Reply\n"); 917 /* XXX ID + Seq */ 918 break; | 853 default: 854 (void)printf("Dest Unreachable, Bad Code: %d\n", 855 icp->icmp_code); 856 break; 857 } 858 /* Print returned IP header information */ 859#ifndef icmp_data 860 pr_retip(&icp->icmp_ip); --- 77 unchanged lines hidden (view full) --- 938 case ICMP_IREQ: 939 (void)printf("Information Request\n"); 940 /* XXX ID + Seq */ 941 break; 942 case ICMP_IREQREPLY: 943 (void)printf("Information Reply\n"); 944 /* XXX ID + Seq */ 945 break; |
919#ifdef ICMP_MASKREQ | |
920 case ICMP_MASKREQ: 921 (void)printf("Address Mask Request\n"); 922 break; | 946 case ICMP_MASKREQ: 947 (void)printf("Address Mask Request\n"); 948 break; |
923#endif 924#ifdef ICMP_MASKREPLY | |
925 case ICMP_MASKREPLY: 926 (void)printf("Address Mask Reply\n"); 927 break; | 949 case ICMP_MASKREPLY: 950 (void)printf("Address Mask Reply\n"); 951 break; |
928#endif | 952 case ICMP_ROUTERADVERT: 953 (void)printf("Router Advertisement\n"); 954 break; 955 case ICMP_ROUTERSOLICIT: 956 (void)printf("Router Solicitation\n"); 957 break; |
929 default: 930 (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 931 } 932} 933 934/* 935 * pr_iph -- 936 * Print an IP header with options. 937 */ 938pr_iph(ip) 939 struct ip *ip; 940{ 941 int hlen; 942 u_char *cp; 943 944 hlen = ip->ip_hl << 2; 945 cp = (u_char *)ip + 20; /* point to options */ 946 | 958 default: 959 (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 960 } 961} 962 963/* 964 * pr_iph -- 965 * Print an IP header with options. 966 */ 967pr_iph(ip) 968 struct ip *ip; 969{ 970 int hlen; 971 u_char *cp; 972 973 hlen = ip->ip_hl << 2; 974 cp = (u_char *)ip + 20; /* point to options */ 975 |
947 (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Data\n"); | 976 (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); |
948 (void)printf(" %1x %1x %02x %04x %04x", | 977 (void)printf(" %1x %1x %02x %04x %04x", |
949 ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id); 950 (void)printf(" %1x %04x", ((ip->ip_off) & 0xe000) >> 13, 951 (ip->ip_off) & 0x1fff); 952 (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, ip->ip_sum); | 978 ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 979 ntohs(ip->ip_id)); 980 (void)printf(" %1x %04x", (ntohl(ip->ip_off) & 0xe000) >> 13, 981 ntohl(ip->ip_off) & 0x1fff); 982 (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 983 ntohs(ip->ip_sum)); |
953 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 954 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); | 984 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 985 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); |
955 /* dump and option bytes */ | 986 /* dump any option bytes */ |
956 while (hlen-- > 20) { 957 (void)printf("%02x", *cp++); 958 } 959 (void)putchar('\n'); 960} 961 962/* 963 * pr_addr -- --- 81 unchanged lines hidden --- | 987 while (hlen-- > 20) { 988 (void)printf("%02x", *cp++); 989 } 990 (void)putchar('\n'); 991} 992 993/* 994 * pr_addr -- --- 81 unchanged lines hidden --- |