ping.c (38560) | ping.c (42337) |
---|---|
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 --- 31 unchanged lines hidden (view full) --- 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 46#endif 47static const char rcsid[] = | 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 --- 31 unchanged lines hidden (view full) --- 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 46#endif 47static const char rcsid[] = |
48 "$Id: ping.c,v 1.40 1998/08/26 01:58:39 dillon Exp $"; | 48 "$Id: ping.c,v 1.41 1998/08/26 18:51:37 des Exp $"; |
49#endif /* not lint */ 50 51/* 52 * P I N G . C 53 * 54 * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 55 * measure round-trip-delays and packet loss across network paths. 56 * --- 79 unchanged lines hidden (view full) --- 136 137struct sockaddr whereto; /* who to ping */ 138int datalen = DEFDATALEN; 139int s; /* socket file descriptor */ 140u_char outpack[MAXPACKET]; 141char BSPACE = '\b'; /* characters written for flood */ 142char DOT = '.'; 143char *hostname; | 49#endif /* not lint */ 50 51/* 52 * P I N G . C 53 * 54 * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 55 * measure round-trip-delays and packet loss across network paths. 56 * --- 79 unchanged lines hidden (view full) --- 136 137struct sockaddr whereto; /* who to ping */ 138int datalen = DEFDATALEN; 139int s; /* socket file descriptor */ 140u_char outpack[MAXPACKET]; 141char BSPACE = '\b'; /* characters written for flood */ 142char DOT = '.'; 143char *hostname; |
144char *shostname; |
|
144int ident; /* process id to identify our packets */ 145int uid; /* cached uid for micro-optimization */ 146 147/* counters */ 148long npackets; /* max packets to transmit */ 149long nreceived; /* # of packets we got back */ 150long nrepeats; /* number of duplicates */ 151long ntransmitted; /* sequence # for outbound packets = #sent */ --- 27 unchanged lines hidden (view full) --- 179 180int 181main(argc, argv) 182 int argc; 183 char *const *argv; 184{ 185 struct timeval last, intvl; 186 struct hostent *hp; | 145int ident; /* process id to identify our packets */ 146int uid; /* cached uid for micro-optimization */ 147 148/* counters */ 149long npackets; /* max packets to transmit */ 150long nreceived; /* # of packets we got back */ 151long nrepeats; /* number of duplicates */ 152long ntransmitted; /* sequence # for outbound packets = #sent */ --- 27 unchanged lines hidden (view full) --- 180 181int 182main(argc, argv) 183 int argc; 184 char *const *argv; 185{ 186 struct timeval last, intvl; 187 struct hostent *hp; |
187 struct sockaddr_in *to; | 188 struct sockaddr_in *to, sin; |
188 struct termios ts; 189 register int i; 190 int ch, hold, packlen, preload, sockerrno, almost_done = 0; 191 struct in_addr ifaddr; 192 unsigned char ttl, loop; 193 u_char *datap, *packet; | 189 struct termios ts; 190 register int i; 191 int ch, hold, packlen, preload, sockerrno, almost_done = 0; 192 struct in_addr ifaddr; 193 unsigned char ttl, loop; 194 u_char *datap, *packet; |
194 char *target, hnamebuf[MAXHOSTNAMELEN]; | 195 char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN]; 196 char snamebuf[MAXHOSTNAMELEN]; |
195 char *ep; 196 u_long ultmp; 197#ifdef IP_OPTIONS 198 char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 199#endif 200 struct sigaction si_sa; 201 struct iovec iov; 202 struct msghdr msg; --- 9 unchanged lines hidden (view full) --- 212 sockerrno = errno; 213 214 setuid(getuid()); 215 uid = getuid(); 216 217 preload = 0; 218 219 datap = &outpack[8 + PHDR_LEN]; | 197 char *ep; 198 u_long ultmp; 199#ifdef IP_OPTIONS 200 char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 201#endif 202 struct sigaction si_sa; 203 struct iovec iov; 204 struct msghdr msg; --- 9 unchanged lines hidden (view full) --- 214 sockerrno = errno; 215 216 setuid(getuid()); 217 uid = getuid(); 218 219 preload = 0; 220 221 datap = &outpack[8 + PHDR_LEN]; |
220 while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) { | 222 while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:v")) != -1) { |
221 switch(ch) { 222 case 'a': 223 options |= F_AUDIBLE; 224 break; 225 case 'c': 226 ultmp = strtoul(optarg, &ep, 0); 227 if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 228 errx(EX_USAGE, --- 82 unchanged lines hidden (view full) --- 311 if (ultmp > MAXPACKET) 312 errx(EX_USAGE, "packet size too large: %lu", 313 ultmp); 314 if (*ep || ep == optarg || !ultmp) 315 errx(EX_USAGE, "invalid packet size: `%s'", 316 optarg); 317 datalen = ultmp; 318 break; | 223 switch(ch) { 224 case 'a': 225 options |= F_AUDIBLE; 226 break; 227 case 'c': 228 ultmp = strtoul(optarg, &ep, 0); 229 if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 230 errx(EX_USAGE, --- 82 unchanged lines hidden (view full) --- 313 if (ultmp > MAXPACKET) 314 errx(EX_USAGE, "packet size too large: %lu", 315 ultmp); 316 if (*ep || ep == optarg || !ultmp) 317 errx(EX_USAGE, "invalid packet size: `%s'", 318 optarg); 319 datalen = ultmp; 320 break; |
321 case 'S': 322 source = optarg; 323 break; |
|
319 case 'T': /* multicast TTL */ 320 ultmp = strtoul(optarg, &ep, 0); 321 if (*ep || ep == optarg || ultmp > 255) 322 errx(EX_USAGE, "invalid multicast TTL: `%s'", 323 optarg); 324 ttl = ultmp; 325 options |= F_MTTL; 326 break; --- 4 unchanged lines hidden (view full) --- 331 usage(); 332 } 333 } 334 335 if (argc - optind != 1) 336 usage(); 337 target = argv[optind]; 338 | 324 case 'T': /* multicast TTL */ 325 ultmp = strtoul(optarg, &ep, 0); 326 if (*ep || ep == optarg || ultmp > 255) 327 errx(EX_USAGE, "invalid multicast TTL: `%s'", 328 optarg); 329 ttl = ultmp; 330 options |= F_MTTL; 331 break; --- 4 unchanged lines hidden (view full) --- 336 usage(); 337 } 338 } 339 340 if (argc - optind != 1) 341 usage(); 342 target = argv[optind]; 343 |
344 if (source) { 345 bzero((char *)&sin, sizeof(sin)); 346 sin.sin_family = AF_INET; 347 if (inet_aton(source, &sin.sin_addr) != 0) { 348 shostname = source; 349 } else { 350 hp = gethostbyname2(source, AF_INET); 351 if (!hp) 352 errx(EX_NOHOST, "cannot resolve %s: %s", 353 source, hstrerror(h_errno)); 354 355 sin.sin_len = sizeof sin; 356 if (hp->h_length > sizeof(sin.sin_addr)) 357 errx(1,"gethostbyname2: illegal address"); 358 memcpy(&sin.sin_addr, hp->h_addr_list[0], 359 sizeof (sin.sin_addr)); 360 (void)strncpy(snamebuf, hp->h_name, 361 sizeof(snamebuf) - 1); 362 snamebuf[sizeof(snamebuf) - 1] = '\0'; 363 shostname = snamebuf; 364 } 365 if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1) 366 err(1, "bind"); 367 } 368 |
|
339 bzero((char *)&whereto, sizeof(struct sockaddr)); 340 to = (struct sockaddr_in *)&whereto; 341 to->sin_family = AF_INET; 342 if (inet_aton(target, &to->sin_addr) != 0) { 343 hostname = target; 344 } else { 345 hp = gethostbyname2(target, AF_INET); 346 if (!hp) --- 92 unchanged lines hidden (view full) --- 439 * ethernet, or just want to fill the arp cache to get some stuff for 440 * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 441 * or multicast pings if they wish. 442 */ 443 hold = 48 * 1024; 444 (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 445 sizeof(hold)); 446 | 369 bzero((char *)&whereto, sizeof(struct sockaddr)); 370 to = (struct sockaddr_in *)&whereto; 371 to->sin_family = AF_INET; 372 if (inet_aton(target, &to->sin_addr) != 0) { 373 hostname = target; 374 } else { 375 hp = gethostbyname2(target, AF_INET); 376 if (!hp) --- 92 unchanged lines hidden (view full) --- 469 * ethernet, or just want to fill the arp cache to get some stuff for 470 * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 471 * or multicast pings if they wish. 472 */ 473 hold = 48 * 1024; 474 (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 475 sizeof(hold)); 476 |
447 if (to->sin_family == AF_INET) 448 (void)printf("PING %s (%s): %d data bytes\n", hostname, 449 inet_ntoa(to->sin_addr), 450 datalen); 451 else | 477 if (to->sin_family == AF_INET) { 478 (void)printf("PING %s (%s)", hostname, 479 inet_ntoa(to->sin_addr)); 480 if (source) 481 (void)printf(" from %s", shostname); 482 (void)printf(": %d data bytes\n", datalen); 483 } else |
452 (void)printf("PING %s: %d data bytes\n", hostname, datalen); 453 454 /* 455 * Use sigaction() instead of signal() to get unambiguous semantics, 456 * in particular with SA_RESTART not set. 457 */ 458 459 sigemptyset(&si_sa.sa_mask); --- 819 unchanged lines hidden (view full) --- 1279 (void)printf("%02x", bp[jj] & 0xFF); 1280 (void)printf("\n"); 1281 } 1282} 1283 1284static void 1285usage() 1286{ | 484 (void)printf("PING %s: %d data bytes\n", hostname, datalen); 485 486 /* 487 * Use sigaction() instead of signal() to get unambiguous semantics, 488 * in particular with SA_RESTART not set. 489 */ 490 491 sigemptyset(&si_sa.sa_mask); --- 819 unchanged lines hidden (view full) --- 1311 (void)printf("%02x", bp[jj] & 0xFF); 1312 (void)printf("\n"); 1313 } 1314} 1315 1316static void 1317usage() 1318{ |
1287 fprintf(stderr, "%s\n%s\n", | 1319 fprintf(stderr, "%s\n%s\n%s\n", |
1288"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]", | 1320"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]", |
1289" [-s packetsize] [host | [-L] [-I iface] [-T ttl] mcast-group]"); | 1321" [-s packetsize] [-S src_addr]", 1322" [host | [-L] [-I iface] [-T ttl] mcast-group]"); |
1290 exit(EX_USAGE); 1291} | 1323 exit(EX_USAGE); 1324} |