]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Introduce data types to use for integral values in packet structures.
authorGuy Harris <[email protected]>
Tue, 6 Oct 2015 16:41:01 +0000 (09:41 -0700)
committerGuy Harris <[email protected]>
Tue, 6 Oct 2015 16:41:01 +0000 (09:41 -0700)
They are defined as arrays of bytes, so

1) no padding is inserted before them to put them on natural
   boundaries, so they can be used if the values *aren't* so
   aligned;

2) you have to use EXTRACT_ macros with them - which you should
   be doing *anyway*, to avoid explicitly or implicitly making
   assumptions about byte order or alignment safety on the
   platform for which your code is being built (it'd better work
   when built for little-endian x86 or for big-endian *and*
   strict-alignment-requiring SPARC).

Use them in the LISP (no, not the programming language!) dissector;
UNALIGNED means "this structure is not guaranteed to be aligned as a
whole, so don't generate code that assumes it is", not "this structure's
individual members shouldn't have padding to put them on natural
boundaries", so it's not sufficient to do that.  (Using these types
*might* suffice to ensure that code that assumes alignment not be
generated, but never underestimate SPARC compilers' eagerness to use
single load and store instructions to fetch big-endian 16-bit, 32-bit,
and 64-bit values from packets that really aren't guaranteed to be
aligned.)

extract.h
print-lisp.c

index cb62ebddb61e9a7f0c85a5af782f1afd20b1dd1d..e8219936dc44af725d18d29ac5572375b52b225d 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.
  */
index 6b5b8151ce1c87ecf1b2966eb7b13d74c9bf8d49..d8a031954310699c58883aa80dde499b17d0385e 100644 (file)
@@ -186,38 +186,38 @@ static const struct tok lisp_loc_flag[] = {
 };
 
 typedef struct map_register_hdr {
-       uint8_t type_and_flag;
-       uint8_t reserved;
-       uint8_t reserved_and_flag2;
-       uint8_t record_count;
-       uint64_t nonce;
-       uint16_t key_id;
-       uint16_t auth_data_len;
-} UNALIGNED lisp_map_register_hdr;
+       nd_uint8_t type_and_flag;
+       nd_uint8_t reserved;
+       nd_uint8_t reserved_and_flag2;
+       nd_uint8_t record_count;
+       nd_uint64_t nonce;
+       nd_uint16_t key_id;
+       nd_uint16_t auth_data_len;
+} lisp_map_register_hdr;
 
 #define MAP_REGISTER_HDR_LEN sizeof(lisp_map_register_hdr)
 
 typedef struct map_register_eid {
-       uint32_t ttl;
-       uint8_t locator_count;
-       uint8_t eid_prefix_mask_length;
-       uint8_t act_auth_inc_res;
-       uint8_t reserved;
-       uint8_t reserved_version_hi;
-       uint8_t version_low;
-       uint16_t eid_prefix_afi;
-} UNALIGNED lisp_map_register_eid;
+       nd_uint32_t ttl;
+       nd_uint8_t locator_count;
+       nd_uint8_t eid_prefix_mask_length;
+       nd_uint8_t act_auth_inc_res;
+       nd_uint8_t reserved;
+       nd_uint8_t reserved_version_hi;
+       nd_uint8_t version_low;
+       nd_uint16_t eid_prefix_afi;
+} lisp_map_register_eid;
 
 #define MAP_REGISTER_EID_LEN sizeof(lisp_map_register_eid)
 
 typedef struct map_register_loc {
-       uint8_t priority;
-       uint8_t weight;
-       uint8_t m_priority;
-       uint8_t m_weight;
-       uint16_t unused_and_flag;
-       uint16_t locator_afi;
-} UNALIGNED lisp_map_register_loc;
+       nd_uint8_t priority;
+       nd_uint8_t weight;
+       nd_uint8_t m_priority;
+       nd_uint8_t m_weight;
+       nd_uint16_t unused_and_flag;
+       nd_uint16_t locator_afi;
+} lisp_map_register_loc;
 
 #define MAP_REGISTER_LOC_LEN sizeof(lisp_map_register_loc)