aboutsummaryrefslogtreecommitdiff
path: root/networking/ping.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/ping.c')
-rw-r--r--networking/ping.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/networking/ping.c b/networking/ping.c
index cfe682646..82d5b7a85 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -186,6 +186,7 @@ create_icmp_socket(void)
186struct globals { 186struct globals {
187 char *hostname; 187 char *hostname;
188 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; 188 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
189 uint16_t myid;
189} FIX_ALIASING; 190} FIX_ALIASING;
190#define G (*(struct globals*)bb_common_bufsiz1) 191#define G (*(struct globals*)bb_common_bufsiz1)
191#define INIT_G() do { setup_common_bufsiz(); } while (0) 192#define INIT_G() do { setup_common_bufsiz(); } while (0)
@@ -204,6 +205,7 @@ static void ping4(len_and_sockaddr *lsa)
204 pkt = (struct icmp *) G.packet; 205 pkt = (struct icmp *) G.packet;
205 /*memset(pkt, 0, sizeof(G.packet)); already is */ 206 /*memset(pkt, 0, sizeof(G.packet)); already is */
206 pkt->icmp_type = ICMP_ECHO; 207 pkt->icmp_type = ICMP_ECHO;
208 pkt->icmp_id = G.myid;
207 pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet)); 209 pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet));
208 210
209 xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len); 211 xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len);
@@ -228,6 +230,8 @@ static void ping4(len_and_sockaddr *lsa)
228 struct iphdr *iphdr = (struct iphdr *) G.packet; 230 struct iphdr *iphdr = (struct iphdr *) G.packet;
229 231
230 pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */ 232 pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */
233 if (pkt->icmp_id != G.myid)
234 continue; /* not our ping */
231 if (pkt->icmp_type == ICMP_ECHOREPLY) 235 if (pkt->icmp_type == ICMP_ECHOREPLY)
232 break; 236 break;
233 } 237 }
@@ -246,6 +250,7 @@ static void ping6(len_and_sockaddr *lsa)
246 pkt = (struct icmp6_hdr *) G.packet; 250 pkt = (struct icmp6_hdr *) G.packet;
247 /*memset(pkt, 0, sizeof(G.packet)); already is */ 251 /*memset(pkt, 0, sizeof(G.packet)); already is */
248 pkt->icmp6_type = ICMP6_ECHO_REQUEST; 252 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
253 pkt->icmp6_id = G.myid;
249 254
250 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); 255 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
251 setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt); 256 setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt);
@@ -269,6 +274,8 @@ static void ping6(len_and_sockaddr *lsa)
269 continue; 274 continue;
270 } 275 }
271 if (c >= ICMP_MINLEN) { /* icmp6_hdr */ 276 if (c >= ICMP_MINLEN) { /* icmp6_hdr */
277 if (pkt->icmp6_id != G.myid)
278 continue; /* not our ping */
272 if (pkt->icmp6_type == ICMP6_ECHO_REPLY) 279 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
273 break; 280 break;
274 } 281 }
@@ -317,6 +324,7 @@ static int common_ping_main(sa_family_t af, char **argv)
317 alarm(5); /* give the host 5000ms to respond */ 324 alarm(5); /* give the host 5000ms to respond */
318 325
319 create_icmp_socket(lsa); 326 create_icmp_socket(lsa);
327 G.myid = (uint16_t) getpid();
320#if ENABLE_PING6 328#if ENABLE_PING6
321 if (lsa->u.sa.sa_family == AF_INET6) 329 if (lsa->u.sa.sa_family == AF_INET6)
322 ping6(lsa); 330 ping6(lsa);
@@ -333,7 +341,7 @@ static int common_ping_main(sa_family_t af, char **argv)
333 341
334/* Full(er) version */ 342/* Full(er) version */
335 343
336#define OPT_STRING ("qvc:s:t:w:W:I:np:4" IF_PING6("6")) 344#define OPT_STRING ("qvc:+s:t:+w:+W:+I:np:4" IF_PING6("6"))
337enum { 345enum {
338 OPT_QUIET = 1 << 0, 346 OPT_QUIET = 1 << 0,
339 OPT_VERBOSE = 1 << 1, 347 OPT_VERBOSE = 1 << 1,
@@ -857,7 +865,7 @@ static int common_ping_main(int opt, char **argv)
857 INIT_G(); 865 INIT_G();
858 866
859 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */ 867 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
860 opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+"; 868 opt_complementary = "=1:q--v:v--q";
861 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_p); 869 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_p);
862 if (opt & OPT_s) 870 if (opt & OPT_s)
863 datalen = xatou16(str_s); // -s 871 datalen = xatou16(str_s); // -s