Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/ping 1. Allocate the max packet size before accounting ...



details:   https://anonhg.NetBSD.org/src/rev/334e5da86b8f
branches:  trunk
changeset: 783574:334e5da86b8f
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Dec 30 02:41:11 2012 +0000

description:
1. Allocate the max packet size before accounting for phdrlen, harmless.
2. In the clear-route-cache sendto, don't send 0 bytes (if -s was specified
   with < 8, phdrlen would be 0).
3. Always send ICMP_MINLEN packets; this is what everyone else does. Makes
   ping -s n where n < 8 work.
4. The condition for checking the data bytes was completely wrong. only check
   the data bytes if we got all of them.
5. The condition for printing a newline was wrong; before it would not print
   a newline before printing the data bytes, and it would append to the previous
   error message.

diffstat:

 sbin/ping/ping.c |  15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)

diffs (65 lines):

diff -r a5d2770acb63 -r 334e5da86b8f sbin/ping/ping.c
--- a/sbin/ping/ping.c  Sat Dec 29 23:44:22 2012 +0000
+++ b/sbin/ping/ping.c  Sun Dec 30 02:41:11 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ping.c,v 1.103 2012/09/18 04:07:44 msaitoh Exp $       */
+/*     $NetBSD: ping.c,v 1.104 2012/12/30 02:41:11 christos Exp $      */
 
 /*
  * Copyright (c) 1989, 1993
@@ -58,7 +58,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: ping.c,v 1.103 2012/09/18 04:07:44 msaitoh Exp $");
+__RCSID("$NetBSD: ping.c,v 1.104 2012/12/30 02:41:11 christos Exp $");
 #endif
 
 #include <stdio.h>
@@ -467,8 +467,8 @@
                phdrlen = PHDR_LEN;
        } else
                phdrlen = 0;
+
        datalen -= phdrlen;
-
        packlen = datalen + 60 + 76;    /* MAXIP + MAXICMP */
        if ((packet = malloc(packlen)) == NULL)
                err(1, "Out of memory");
@@ -857,7 +857,7 @@
                               (char *)&sw,sizeof(sw)) < 0)
                        err(1, "Can't turn off special IP header");
                if (prog_sendto(sloop, (char *) &opack_icmp,
-                          phdrlen, MSG_DONTROUTE,
+                          ICMP_MINLEN, MSG_DONTROUTE,
                           (struct sockaddr *)&loc_addr,
                           sizeof(struct sockaddr_in)) < 0) {
                        /*
@@ -887,7 +887,7 @@
        } else if (pingflags & F_TIMING64)
                (void) memcpy(&opack_icmp.icmp_data[0], &now, sizeof(now));
 
-       cc = datalen + phdrlen;
+       cc = MAX(datalen, ICMP_MINLEN) + phdrlen;
        opack_icmp.icmp_cksum = 0;
        opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp, cc);
 
@@ -1096,7 +1096,8 @@
                        PR_PACK_SUB();
 
                /* check the data */
-               if (datalen > phdrlen
+               if ((size_t)(tot_len - hlen) >
+                   offsetof(struct icmp, icmp_data) + datalen
                    && !(pingflags & F_PING_RANDOM)
                    && memcmp(icp->icmp_data + phdrlen,
                            opack_icmp.icmp_data + phdrlen,
@@ -1112,7 +1113,7 @@
                                     (u_char)opack_icmp.icmp_data[i],
                                     (u_char)icp->icmp_data[i]);
                        for (i = phdrlen; i < datalen; i++) {
-                               if ((i % 16) == phdrlen)
+                               if ((i % 16) == 0)
                                        (void)printf("\n\t");
                                (void)printf("%2x ",(u_char)icp->icmp_data[i]);
                        }



Home | Main Index | Thread Index | Old Index