]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Use the nd_uintN_t types more.
authorGuy Harris <[email protected]>
Wed, 7 Oct 2015 07:14:20 +0000 (00:14 -0700)
committerGuy Harris <[email protected]>
Wed, 7 Oct 2015 07:14:20 +0000 (00:14 -0700)
Define them in netdissect.h.

Use them in ip.h, and *don't* mark the structures as UNALIGNED; that
should no longer be necessary.

Add a new nd_ipv4 type to use as an IPv4 address; it represents the 4
bytes of IPv4 address as an array of unsigned chars, so that its natural
alignment is only on a byte boundary.

Those changes found some places where we weren't using
UNALIGNED_MEMCPY() to extract IPv4 addresses from packets; use it.

extract.h
ip.h
netdissect.h
print-esp.c
print-ip.c
print-rx.c
print-tcp.c

index e8219936dc44af725d18d29ac5572375b52b225d..cb62ebddb61e9a7f0c85a5af782f1afd20b1dd1d 100644 (file)
--- a/extract.h
+++ b/extract.h
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
-/*
- * Data types corresponding to multi-byte integral values within data
- * structures.  These are defined as arrays of octets, so that they're
- * not aligned on their "natural" boundaries, and so that you *must*
- * use the EXTRACT_ macros to extract them (which you should be doing
- * *anyway*, so as not to assume a particular byte order or alignment
- * in your code).
- */
-typedef unsigned char nd_uint16_t[2];
-typedef unsigned char nd_uint24_t[3];
-typedef unsigned char nd_uint32_t[4];
-typedef unsigned char nd_uint40_t[5];
-typedef unsigned char nd_uint48_t[6];
-typedef unsigned char nd_uint56_t[7];
-typedef unsigned char nd_uint64_t[8];
-
-/*
- * Data types corresponding to single-byte integral values, for
- * completeness.
- */
-typedef unsigned char nd_uint8_t;
-typedef signed char nd_int8_t;
-
 /*
  * Macros to extract possibly-unaligned big-endian integral values.
  */
diff --git a/ip.h b/ip.h
index a0c2c6f6f7d0b18c6349883b1083390837486f73..8179061ef13143a924ed8f72195cc956b6dfeed3 100644 (file)
--- a/ip.h
+++ b/ip.h
  * against negative integers quite easily, and fail in subtle ways.
  */
 struct ip {
-       uint8_t         ip_vhl;         /* header length, version */
+       nd_uint8_t      ip_vhl;         /* header length, version */
 #define IP_V(ip)       (((ip)->ip_vhl & 0xf0) >> 4)
 #define IP_HL(ip)      ((ip)->ip_vhl & 0x0f)
-       uint8_t         ip_tos;         /* type of service */
-       uint16_t        ip_len;         /* total length */
-       uint16_t        ip_id;          /* identification */
-       uint16_t        ip_off;         /* fragment offset field */
+       nd_uint8_t      ip_tos;         /* type of service */
+       nd_uint16_t     ip_len;         /* total length */
+       nd_uint16_t     ip_id;          /* identification */
+       nd_uint16_t     ip_off;         /* fragment offset field */
 #define        IP_DF 0x4000                    /* dont fragment flag */
 #define        IP_MF 0x2000                    /* more fragments flag */
 #define        IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
-       uint8_t         ip_ttl;         /* time to live */
-       uint8_t         ip_p;           /* protocol */
-       uint16_t        ip_sum;         /* checksum */
-       struct  in_addr ip_src,ip_dst;  /* source and dest address */
-} UNALIGNED;
+       nd_uint8_t      ip_ttl;         /* time to live */
+       nd_uint8_t      ip_p;           /* protocol */
+       nd_uint16_t     ip_sum;         /* checksum */
+       nd_ipv4         ip_src,ip_dst;  /* source and dest address */
+};
 
 #define        IP_MAXPACKET    65535           /* maximum packet size */
 
@@ -123,20 +123,20 @@ struct ip {
  * Time stamp option structure.
  */
 struct ip_timestamp {
-       uint8_t         ipt_code;       /* IPOPT_TS */
-       uint8_t         ipt_len;        /* size of structure (variable) */
-       uint8_t         ipt_ptr;        /* index of current entry */
-       uint8_t         ipt_oflwflg;    /* flags, overflow counter */
+       nd_uint8_t      ipt_code;       /* IPOPT_TS */
+       nd_uint8_t      ipt_len;        /* size of structure (variable) */
+       nd_uint8_t      ipt_ptr;        /* index of current entry */
+       nd_uint8_t      ipt_oflwflg;    /* flags, overflow counter */
 #define IPTS_OFLW(ip)  (((ipt)->ipt_oflwflg & 0xf0) >> 4)
 #define IPTS_FLG(ip)   ((ipt)->ipt_oflwflg & 0x0f)
        union ipt_timestamp {
-               uint32_t ipt_time[1];
+               nd_uint32_t ipt_time[1];
                struct  ipt_ta {
-                       struct in_addr ipt_addr;
-                       uint32_t ipt_time;
+                       nd_ipv4 ipt_addr;
+                       nd_uint32_t ipt_time;
                } ipt_ta[1];
        } ipt_timestamp;
-} UNALIGNED;
+};
 
 /* flag bits for ipt_flg */
 #define        IPOPT_TS_TSONLY         0               /* timestamps only */
index 29c4e0a11986c19d368c2f85864a469616d0b054..47624d19f0ee1213f3fb5a091af05ab2d8cdf7c8 100644 (file)
 #define __attribute__(x)
 #endif
 
+/*
+ * Data types corresponding to multi-byte integral values within data
+ * structures.  These are defined as arrays of octets, so that they're
+ * not aligned on their "natural" boundaries, and so that you *must*
+ * use the EXTRACT_ macros to extract them (which you should be doing
+ * *anyway*, so as not to assume a particular byte order or alignment
+ * in your code).
+ */
+typedef unsigned char nd_uint16_t[2];
+typedef unsigned char nd_uint24_t[3];
+typedef unsigned char nd_uint32_t[4];
+typedef unsigned char nd_uint40_t[5];
+typedef unsigned char nd_uint48_t[6];
+typedef unsigned char nd_uint56_t[7];
+typedef unsigned char nd_uint64_t[8];
+
+/*
+ * Use this for IPv4 addresses.  It's defined as an array of octets, so
+ * that it's not aligned on its "natural" boundary, and it's defined as
+ * a structure in the hopes that this makes it harder to naively use
+ * EXTRACT_32BITS() to extract the value - in many cases you just want
+ * to use UNALIGNED_MEMCPY() to copy its value, so that it remains in
+ * network byte order.
+ */
+typedef struct {
+       unsigned char bytes[4];
+} nd_ipv4;
+
+/*
+ * Data types corresponding to single-byte integral values, for
+ * completeness.
+ */
+typedef unsigned char nd_uint8_t;
+typedef signed char nd_int8_t;
+
 /* snprintf et al */
 
 #include <stdarg.h>
index 50cc2d1f7b72a3abfc1e59872134adfdf14022dc..247796da8426ed64b243f280607bcd957b5d121e 100644 (file)
 #endif
 #endif
 
-#include "ip.h"
-#include "ip6.h"
-
 #include "netdissect.h"
 #include "strtoaddr.h"
 #include "extract.h"
 
 #include "ascii_strcasecmp.h"
 
+#include "ip.h"
+#include "ip6.h"
+
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  * All rights reserved.
index 32304aedf80b76e2624ad23d13032f4d76cbf48f..d64aef7532055ea27c9b911b3f5ab8f8eb586ea9 100644 (file)
@@ -122,7 +122,7 @@ ip_finddst(netdissect_options *ndo,
                }
        }
 trunc:
-       UNALIGNED_MEMCPY(&retval, &ip->ip_dst.s_addr, sizeof(uint32_t));
+       UNALIGNED_MEMCPY(&retval, &ip->ip_dst, sizeof(uint32_t));
        return retval;
 }
 
@@ -147,9 +147,9 @@ nextproto4_cksum(netdissect_options *ndo,
        ph.len = htons((uint16_t)len);
        ph.mbz = 0;
        ph.proto = next_proto;
-       UNALIGNED_MEMCPY(&ph.src, &ip->ip_src.s_addr, sizeof(uint32_t));
+       UNALIGNED_MEMCPY(&ph.src, &ip->ip_src, sizeof(uint32_t));
        if (IP_HL(ip) == 5)
-               UNALIGNED_MEMCPY(&ph.dst, &ip->ip_dst.s_addr, sizeof(uint32_t));
+               UNALIGNED_MEMCPY(&ph.dst, &ip->ip_dst, sizeof(uint32_t));
        else
                ph.dst = ip_finddst(ndo, ip);
 
index 322e0c0c56823af38b2fd501dab2b53092650185..516c387ea6c08108a553757ec22178ad51163be7 100644 (file)
@@ -688,8 +688,8 @@ rx_cache_insert(netdissect_options *ndo,
                rx_cache_next = 0;
 
        rxent->callnum = rxh->callNumber;
-       rxent->client = ip->ip_src;
-       rxent->server = ip->ip_dst;
+       UNALIGNED_MEMCPY(&rxent->client, &ip->ip_src, sizeof(uint32_t));
+       UNALIGNED_MEMCPY(&rxent->server, &ip->ip_dst, sizeof(uint32_t));
        rxent->dport = dport;
        rxent->serviceId = rxh->serviceId;
        rxent->opcode = EXTRACT_32BITS(bp + sizeof(struct rx_header));
@@ -708,8 +708,11 @@ rx_cache_find(const struct rx_header *rxh, const struct ip *ip, int sport,
 {
        int i;
        struct rx_cache_entry *rxent;
-       uint32_t clip = ip->ip_dst.s_addr;
-       uint32_t sip = ip->ip_src.s_addr;
+       uint32_t clip;
+       uint32_t sip;
+
+       UNALIGNED_MEMCPY(&clip, &ip->ip_dst, sizeof(uint32_t));
+       UNALIGNED_MEMCPY(&sip, &ip->ip_src, sizeof(uint32_t));
 
        /* Start the search where we last left off */
 
index 76811532390335247e948705df4d31f76da04189..ce9bf11e68d2f20aa7b34fbb8e42f9c2821ef897 100644 (file)
@@ -293,25 +293,22 @@ tcp_print(netdissect_options *ndo,
                 } else {
                         register struct tcp_seq_hash *th;
                         struct tcp_seq_hash *tcp_seq_hash;
-                        const struct in_addr *src, *dst;
                         struct tha tha;
 
                         tcp_seq_hash = tcp_seq_hash4;
-                        src = &ip->ip_src;
-                        dst = &ip->ip_dst;
                         if (sport > dport)
                                 rev = 1;
                         else if (sport == dport) {
-                                if (UNALIGNED_MEMCMP(src, dst, sizeof ip->ip_dst) > 0)
+                                if (UNALIGNED_MEMCMP(&ip->ip_src, &ip->ip_dst, sizeof ip->ip_dst) > 0)
                                         rev = 1;
                         }
                         if (rev) {
-                                UNALIGNED_MEMCPY(&tha.src, dst, sizeof ip->ip_dst);
-                                UNALIGNED_MEMCPY(&tha.dst, src, sizeof ip->ip_src);
+                                UNALIGNED_MEMCPY(&tha.src, &ip->ip_dst, sizeof ip->ip_dst);
+                                UNALIGNED_MEMCPY(&tha.dst, &ip->ip_src, sizeof ip->ip_src);
                                 tha.port = dport << 16 | sport;
                         } else {
-                                UNALIGNED_MEMCPY(&tha.dst, dst, sizeof ip->ip_dst);
-                                UNALIGNED_MEMCPY(&tha.src, src, sizeof ip->ip_src);
+                                UNALIGNED_MEMCPY(&tha.dst, &ip->ip_dst, sizeof ip->ip_dst);
+                                UNALIGNED_MEMCPY(&tha.src, &ip->ip_src, sizeof ip->ip_src);
                                 tha.port = sport << 16 | dport;
                         }