/*
- * Copyright (c) 1987 Regents of the University of California.
+ * Copyright (c) 1987, 1988 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
#ifndef lint
char copyright[] =
-"@(#) Copyright (c) 1987 Regents of the University of California.\n\
+"@(#) 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.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)ping.c 4.12 (Berkeley) %G%";
#endif /* not lint */
/*
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);
+ }
}
}
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",