+/*
+ * Copyright (c) 1987, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley. The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
#ifndef lint
-static char sccsid[] = "@(#)ping.c 4.6 (Berkeley) %G%";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1987, 1988 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)ping.c 4.12 (Berkeley) %G%";
+#endif /* not lint */
/*
* P I N G . C
char *inet_ntoa();
int npackets;
+int burst = 1;
int ntransmitted = 0; /* sequence # for outbound packets = #sent */
int ident;
hostname = hnamebuf;
} else {
hp = gethostbyname(av[0]);
- if (hp) {
- to->sin_family = hp->h_addrtype;
- bcopy(hp->h_addr, (caddr_t)&to->sin_addr, hp->h_length);
- hostname = hp->h_name;
- toaddr = inet_ntoa(to->sin_addr.s_addr);
- } else {
- printf("%s: unknown host %s\n", argv[0], av[0]);
+ if (!hp) {
+ fprintf(stderr, "ping: %s: ", av[0]);
+ herror((char *)NULL);
exit(1);
}
+ to->sin_family = hp->h_addrtype;
+ bcopy(hp->h_addr, (caddr_t)&to->sin_addr, hp->h_length);
+ hostname = hp->h_name;
+ toaddr = inet_ntoa(to->sin_addr.s_addr);
}
if( argc >= 2 )
for (;;) {
int len = sizeof (packet);
- int fromlen = sizeof (from);
+ int fromlen;
int cc;
+ fromlen = sizeof (from);
if ( (cc=recvfrom(s, packet, len, 0, &from, &fromlen)) < 0) {
if( errno == EINTR )
continue;
{
static u_char outpack[MAXPACKET];
register struct icmp *icp = (struct icmp *) outpack;
- int i, cc;
+ int i, cc, n;
register struct timeval *tp = (struct timeval *) &outpack[8];
register u_char *datap = &outpack[8+sizeof(struct timeval)];
icp->icmp_cksum = in_cksum( icp, cc );
/* cc = sendto(s, msg, len, flags, to, tolen) */
- i = sendto( s, outpack, cc, 0, &whereto, sizeof(struct sockaddr) );
-
- if( i < 0 || i != cc ) {
- if( i<0 ) perror("sendto");
- printf("ping: wrote %s %d chars, ret=%d\n",
- hostname, cc, i );
- fflush(stdout);
+ for (n = 0; n < burst; n++) {
+ i = sendto(s, outpack, cc, 0, &whereto, sizeof(whereto));
+
+ if( i < 0 || i != cc ) {
+ if( i<0 ) perror("sendto");
+ printf("ping: wrote %s %d chars, ret=%d\n",
+ hostname, cc, i );
+ fflush(stdout);
+ }
}
}
register u_short *w = addr;
register u_short answer;
register int sum = 0;
+ u_short odd_byte = 0;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum),
}
/* mop up an odd byte, if necessary */
- if( nleft == 1 )
- sum += *(u_char *)w;
+ if( nleft == 1 ) {
+ *(u_char *)(&odd_byte) = *(u_char *)w;
+ sum += odd_byte;
+ }
/*
* add back carry outs from top 16 bits to low 16 bits
printf("\n----%s PING Statistics----\n", hostname );
printf("%d packets transmitted, ", ntransmitted );
printf("%d packets received, ", nreceived );
- if (ntransmitted)
- printf("%d%% packet loss",
- (int) (((ntransmitted-nreceived)*100) / ntransmitted ) );
+ if (ntransmitted) {
+ if (nreceived <= ntransmitted)
+ printf("%d%% packet loss",
+ (int) (((ntransmitted-nreceived)*100) / ntransmitted));
+ else
+ printf("%.2f responses per request",
+ (float) nreceived / (float) ntransmitted);
+ }
printf("\n");
if (nreceived && timing)
printf("round-trip (ms) min/avg/max = %d/%d/%d\n",