Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 2 | /* |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 3 | * $Id: ping.c,v 1.16 2000/06/07 20:38:15 proski Exp $ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 4 | * Mini ping implementation for busybox |
| 5 | * |
| 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * This version of ping is adapted from the ping in netkit-base 0.10, |
| 23 | * which is: |
| 24 | * |
| 25 | * Copyright (c) 1989 The Regents of the University of California. |
| 26 | * All rights reserved. |
| 27 | * |
| 28 | * This code is derived from software contributed to Berkeley by |
| 29 | * Mike Muuss. |
| 30 | * |
| 31 | * Original copyright notice is retained at the end of this file. |
| 32 | */ |
| 33 | |
| 34 | #include "internal.h" |
| 35 | #include <sys/param.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/file.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <sys/times.h> |
| 40 | #include <sys/signal.h> |
| 41 | |
| 42 | #include <netinet/in.h> |
| 43 | #include <netinet/ip.h> |
| 44 | #include <netinet/ip_icmp.h> |
| 45 | #include <arpa/inet.h> |
| 46 | #include <netdb.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <errno.h> |
| 50 | |
| 51 | #define DEFDATALEN 56 |
| 52 | #define MAXIPLEN 60 |
| 53 | #define MAXICMPLEN 76 |
| 54 | #define MAXPACKET 65468 |
| 55 | #define MAX_DUP_CHK (8 * 128) |
| 56 | #define MAXWAIT 10 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | #define PINGINTERVAL 1 /* second */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 58 | |
| 59 | #define O_QUIET (1 << 0) |
| 60 | |
| 61 | #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ |
| 62 | #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ |
| 63 | #define SET(bit) (A(bit) |= B(bit)) |
| 64 | #define CLR(bit) (A(bit) &= (~B(bit))) |
| 65 | #define TST(bit) (A(bit) & B(bit)) |
| 66 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 67 | static void ping(const char *host); |
| 68 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 69 | /* common routines */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 70 | static int in_cksum(unsigned short *buf, int sz) |
| 71 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | int nleft = sz; |
| 73 | int sum = 0; |
| 74 | unsigned short *w = buf; |
| 75 | unsigned short ans = 0; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 76 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | while (nleft > 1) { |
| 78 | sum += *w++; |
| 79 | nleft -= 2; |
| 80 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 81 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | if (nleft == 1) { |
| 83 | *(unsigned char *) (&ans) = *(unsigned char *) w; |
| 84 | sum += ans; |
| 85 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 86 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | sum = (sum >> 16) + (sum & 0xFFFF); |
| 88 | sum += (sum >> 16); |
| 89 | ans = ~sum; |
| 90 | return (ans); |
| 91 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 92 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 93 | /* simple version */ |
| 94 | #ifdef BB_SIMPLE_PING |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 95 | static const char *ping_usage = "ping host\n" |
| 96 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 97 | "\nSend ICMP ECHO_REQUEST packets to network hosts\n" |
| 98 | #endif |
| 99 | ; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 100 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | static char *hostname = NULL; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 102 | |
| 103 | static void noresp(int ign) |
| 104 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | printf("No response from %s\n", hostname); |
| 106 | exit(0); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 109 | static void ping(const char *host) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 110 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | struct hostent *h; |
| 112 | struct sockaddr_in pingaddr; |
| 113 | struct icmp *pkt; |
| 114 | int pingsock, c; |
| 115 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 116 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 118 | perror("ping: creating a raw socket"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 119 | exit(1); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 120 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | |
| 122 | /* drop root privs if running setuid */ |
| 123 | setuid(getuid()); |
| 124 | |
| 125 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
| 126 | |
| 127 | pingaddr.sin_family = AF_INET; |
| 128 | if (!(h = gethostbyname(host))) { |
| 129 | fprintf(stderr, "ping: unknown host %s\n", host); |
| 130 | exit(1); |
| 131 | } |
| 132 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
| 133 | hostname = h->h_name; |
| 134 | |
| 135 | pkt = (struct icmp *) packet; |
| 136 | memset(pkt, 0, sizeof(packet)); |
| 137 | pkt->icmp_type = ICMP_ECHO; |
| 138 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
| 139 | |
| 140 | c = sendto(pingsock, packet, sizeof(packet), 0, |
| 141 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 142 | |
| 143 | if (c < 0 || c != sizeof(packet)) { |
| 144 | if (c < 0) |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 145 | perror("ping: sendto"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | fprintf(stderr, "ping: write incomplete\n"); |
| 147 | exit(1); |
| 148 | } |
| 149 | |
| 150 | signal(SIGALRM, noresp); |
| 151 | alarm(5); /* give the host 5000ms to respond */ |
| 152 | /* listen for replies */ |
| 153 | while (1) { |
| 154 | struct sockaddr_in from; |
| 155 | size_t fromlen = sizeof(from); |
| 156 | |
| 157 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 158 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 159 | if (errno == EINTR) |
| 160 | continue; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 161 | perror("ping: recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 162 | continue; |
| 163 | } |
| 164 | if (c >= 76) { /* ip + icmp */ |
| 165 | struct iphdr *iphdr = (struct iphdr *) packet; |
| 166 | |
| 167 | pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */ |
| 168 | if (pkt->icmp_type == ICMP_ECHOREPLY) |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | printf("%s is alive!\n", hostname); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 173 | return; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | extern int ping_main(int argc, char **argv) |
| 177 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 178 | argc--; |
| 179 | argv++; |
| 180 | if (argc < 1) |
| 181 | usage(ping_usage); |
| 182 | ping(*argv); |
| 183 | exit(TRUE); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 186 | #else /* ! BB_SIMPLE_PING */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 187 | /* full(er) version */ |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 188 | static const char *ping_usage = "ping [OPTION]... host\n" |
| 189 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 190 | "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | "Options:\n" |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 192 | "\t-c COUNT\tSend only COUNT pings.\n" |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 193 | "\t-s SIZE\t\tSend SIZE data bytes in packets (default=56).\n" |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 194 | "\t-q\t\tQuiet mode, only displays output at start\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 195 | "\t\t\tand when finished.\n" |
| 196 | #endif |
| 197 | ; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 198 | |
| 199 | static char *hostname = NULL; |
| 200 | static struct sockaddr_in pingaddr; |
| 201 | static int pingsock = -1; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 202 | static int datalen = DEFDATALEN; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 203 | |
| 204 | static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0; |
| 205 | static int myid = 0, options = 0; |
| 206 | static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0; |
| 207 | static char rcvd_tbl[MAX_DUP_CHK / 8]; |
| 208 | |
| 209 | static void sendping(int); |
| 210 | static void pingstats(int); |
| 211 | static void unpack(char *, int, struct sockaddr_in *); |
| 212 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 213 | /**************************************************************************/ |
| 214 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | static void pingstats(int ign) |
| 216 | { |
| 217 | signal(SIGINT, SIG_IGN); |
| 218 | |
| 219 | printf("\n--- %s ping statistics ---\n", hostname); |
| 220 | printf("%ld packets transmitted, ", ntransmitted); |
| 221 | printf("%ld packets received, ", nreceived); |
| 222 | if (nrepeats) |
| 223 | printf("%ld duplicates, ", nrepeats); |
| 224 | if (ntransmitted) |
| 225 | printf("%ld%% packet loss\n", |
| 226 | (ntransmitted - nreceived) * 100 / ntransmitted); |
| 227 | if (nreceived) |
| 228 | printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", |
| 229 | tmin / 10, tmin % 10, |
| 230 | (tsum / (nreceived + nrepeats)) / 10, |
| 231 | (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10); |
| 232 | exit(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void sendping(int ign) |
| 236 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | struct icmp *pkt; |
| 238 | int i; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 239 | char packet[datalen + 8]; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 240 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | pkt = (struct icmp *) packet; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 242 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 243 | pkt->icmp_type = ICMP_ECHO; |
| 244 | pkt->icmp_code = 0; |
| 245 | pkt->icmp_cksum = 0; |
| 246 | pkt->icmp_seq = ntransmitted++; |
| 247 | pkt->icmp_id = myid; |
| 248 | CLR(pkt->icmp_seq % MAX_DUP_CHK); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 249 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 250 | gettimeofday((struct timeval *) &packet[8], NULL); |
| 251 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 252 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 253 | i = sendto(pingsock, packet, sizeof(packet), 0, |
| 254 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 255 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 256 | if (i < 0) |
| 257 | fatalError("ping: sendto: %s\n", strerror(errno)); |
| 258 | else if (i != sizeof(packet)) |
| 259 | fatalError("ping wrote %d chars; %d expected\n", i, |
| 260 | (int)sizeof(packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 261 | |
| 262 | signal(SIGALRM, sendping); |
| 263 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 264 | alarm(PINGINTERVAL); |
| 265 | } else { /* done, wait for the last ping to come back */ |
| 266 | /* todo, don't necessarily need to wait so long... */ |
| 267 | signal(SIGALRM, pingstats); |
| 268 | alarm(MAXWAIT); |
| 269 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 270 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 271 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 272 | static char *icmp_type_name (int id) |
| 273 | { |
| 274 | switch (id) { |
| 275 | case ICMP_ECHOREPLY: return "Echo Reply"; |
| 276 | case ICMP_DEST_UNREACH: return "Destination Unreachable"; |
| 277 | case ICMP_SOURCE_QUENCH: return "Source Quench"; |
| 278 | case ICMP_REDIRECT: return "Redirect (change route)"; |
| 279 | case ICMP_ECHO: return "Echo Request"; |
| 280 | case ICMP_TIME_EXCEEDED: return "Time Exceeded"; |
| 281 | case ICMP_PARAMETERPROB: return "Parameter Problem"; |
| 282 | case ICMP_TIMESTAMP: return "Timestamp Request"; |
| 283 | case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; |
| 284 | case ICMP_INFO_REQUEST: return "Information Request"; |
| 285 | case ICMP_INFO_REPLY: return "Information Reply"; |
| 286 | case ICMP_ADDRESS: return "Address Mask Request"; |
| 287 | case ICMP_ADDRESSREPLY: return "Address Mask Reply"; |
| 288 | default: return "unknown ICMP type"; |
| 289 | } |
| 290 | } |
| 291 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 292 | static void unpack(char *buf, int sz, struct sockaddr_in *from) |
| 293 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 294 | struct icmp *icmppkt; |
| 295 | struct iphdr *iphdr; |
| 296 | struct timeval tv, *tp; |
| 297 | int hlen, dupflag; |
| 298 | unsigned long triptime; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 299 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 300 | gettimeofday(&tv, NULL); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 301 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 302 | /* check IP header */ |
| 303 | iphdr = (struct iphdr *) buf; |
| 304 | hlen = iphdr->ihl << 2; |
| 305 | /* discard if too short */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 306 | if (sz < (datalen + ICMP_MINLEN)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 307 | return; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 308 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 309 | sz -= hlen; |
| 310 | icmppkt = (struct icmp *) (buf + hlen); |
| 311 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 312 | if (icmppkt->icmp_id != myid) |
| 313 | return; /* not our ping */ |
| 314 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 315 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 316 | ++nreceived; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 317 | tp = (struct timeval *) icmppkt->icmp_data; |
| 318 | |
| 319 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 320 | --tv.tv_sec; |
| 321 | tv.tv_usec += 1000000; |
| 322 | } |
| 323 | tv.tv_sec -= tp->tv_sec; |
| 324 | |
| 325 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 326 | tsum += triptime; |
| 327 | if (triptime < tmin) |
| 328 | tmin = triptime; |
| 329 | if (triptime > tmax) |
| 330 | tmax = triptime; |
| 331 | |
| 332 | if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) { |
| 333 | ++nrepeats; |
| 334 | --nreceived; |
| 335 | dupflag = 1; |
| 336 | } else { |
| 337 | SET(icmppkt->icmp_seq % MAX_DUP_CHK); |
| 338 | dupflag = 0; |
| 339 | } |
| 340 | |
| 341 | if (options & O_QUIET) |
| 342 | return; |
| 343 | |
| 344 | printf("%d bytes from %s: icmp_seq=%u", sz, |
| 345 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
| 346 | icmppkt->icmp_seq); |
| 347 | printf(" ttl=%d", iphdr->ttl); |
| 348 | printf(" time=%lu.%lu ms", triptime / 10, triptime % 10); |
| 349 | if (dupflag) |
| 350 | printf(" (DUP!)"); |
| 351 | printf("\n"); |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 352 | } else |
| 353 | if (icmppkt->icmp_type != ICMP_ECHO) |
| 354 | fprintf(stderr, |
| 355 | "Warning: Got ICMP %d (%s)\n", |
| 356 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 359 | static void ping(const char *host) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 360 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 361 | struct protoent *proto; |
| 362 | struct hostent *h; |
| 363 | char buf[MAXHOSTNAMELEN]; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 364 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 365 | int sockopt; |
| 366 | |
| 367 | proto = getprotobyname("icmp"); |
| 368 | /* if getprotobyname failed, just silently force |
| 369 | * proto->p_proto to have the correct value for "icmp" */ |
| 370 | if ((pingsock = socket(AF_INET, SOCK_RAW, |
| 371 | (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */ |
| 372 | if (errno == EPERM) { |
| 373 | fprintf(stderr, "ping: permission denied. (are you root?)\n"); |
| 374 | } else { |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 375 | perror("ping: creating a raw socket"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 376 | } |
| 377 | exit(1); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 378 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 379 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 380 | /* drop root privs if running setuid */ |
| 381 | setuid(getuid()); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 382 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 383 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 384 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 385 | pingaddr.sin_family = AF_INET; |
| 386 | if (!(h = gethostbyname(host))) { |
| 387 | fprintf(stderr, "ping: unknown host %s\n", host); |
| 388 | exit(1); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 389 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 390 | |
| 391 | if (h->h_addrtype != AF_INET) { |
| 392 | fprintf(stderr, |
| 393 | "ping: unknown address type; only AF_INET is currently supported.\n"); |
| 394 | exit(1); |
| 395 | } |
| 396 | |
| 397 | pingaddr.sin_family = AF_INET; /* h->h_addrtype */ |
| 398 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
| 399 | strncpy(buf, h->h_name, sizeof(buf) - 1); |
| 400 | hostname = buf; |
| 401 | |
| 402 | /* enable broadcast pings */ |
| 403 | sockopt = 1; |
| 404 | setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt, |
| 405 | sizeof(sockopt)); |
| 406 | |
| 407 | /* set recv buf for broadcast pings */ |
| 408 | sockopt = 48 * 1024; |
| 409 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt, |
| 410 | sizeof(sockopt)); |
| 411 | |
| 412 | printf("PING %s (%s): %d data bytes\n", |
| 413 | hostname, |
| 414 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 415 | datalen); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 416 | |
| 417 | signal(SIGINT, pingstats); |
| 418 | |
| 419 | /* start the ping's going ... */ |
| 420 | sendping(0); |
| 421 | |
| 422 | /* listen for replies */ |
| 423 | while (1) { |
| 424 | struct sockaddr_in from; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 425 | socklen_t fromlen = (socklen_t) sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 426 | int c; |
| 427 | |
| 428 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 429 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 430 | if (errno == EINTR) |
| 431 | continue; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 432 | perror("ping: recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 433 | continue; |
| 434 | } |
| 435 | unpack(packet, c, &from); |
| 436 | if (pingcount > 0 && nreceived >= pingcount) |
| 437 | break; |
| 438 | } |
| 439 | pingstats(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | extern int ping_main(int argc, char **argv) |
| 443 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 444 | char *thisarg; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 445 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 446 | argc--; |
| 447 | argv++; |
| 448 | options = 0; |
| 449 | /* Parse any options */ |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 450 | while (argc >= 1 && **argv == '-') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 451 | thisarg = *argv; |
| 452 | thisarg++; |
| 453 | switch (*thisarg) { |
| 454 | case 'q': |
| 455 | options |= O_QUIET; |
| 456 | break; |
| 457 | case 'c': |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 458 | if (--argc <= 0) |
| 459 | usage(ping_usage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 460 | argv++; |
| 461 | pingcount = atoi(*argv); |
| 462 | break; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 463 | case 's': |
| 464 | if (--argc <= 0) |
| 465 | usage(ping_usage); |
| 466 | argv++; |
| 467 | datalen = atoi(*argv); |
| 468 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 469 | default: |
| 470 | usage(ping_usage); |
| 471 | } |
| 472 | argc--; |
| 473 | argv++; |
| 474 | } |
| 475 | if (argc < 1) |
| 476 | usage(ping_usage); |
| 477 | |
| 478 | myid = getpid() & 0xFFFF; |
| 479 | ping(*argv); |
| 480 | exit(TRUE); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 481 | } |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 482 | #endif /* ! BB_SIMPLE_PING */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * Copyright (c) 1989 The Regents of the University of California. |
| 486 | * All rights reserved. |
| 487 | * |
| 488 | * This code is derived from software contributed to Berkeley by |
| 489 | * Mike Muuss. |
| 490 | * |
| 491 | * Redistribution and use in source and binary forms, with or without |
| 492 | * modification, are permitted provided that the following conditions |
| 493 | * are met: |
| 494 | * 1. Redistributions of source code must retain the above copyright |
| 495 | * notice, this list of conditions and the following disclaimer. |
| 496 | * 2. Redistributions in binary form must reproduce the above copyright |
| 497 | * notice, this list of conditions and the following disclaimer in the |
| 498 | * documentation and/or other materials provided with the distribution. |
| 499 | * 3. All advertising materials mentioning features or use of this software |
| 500 | * must display the following acknowledgement: |
| 501 | * This product includes software developed by the University of |
| 502 | * California, Berkeley and its contributors. |
| 503 | * 4. Neither the name of the University nor the names of its contributors |
| 504 | * may be used to endorse or promote products derived from this software |
| 505 | * without specific prior written permission. |
| 506 | * |
| 507 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 508 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 509 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 510 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 511 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 512 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 513 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 514 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 515 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 516 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 517 | * SUCH DAMAGE. |
| 518 | */ |