From: Guy Harris Date: Wed, 9 Aug 2023 07:39:56 +0000 (-0700) Subject: CONTRIBUTION: mention the nd_ types. X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/570bb7d1f8f87be048e8299a4c38b82b092a9b7b CONTRIBUTION: mention the nd_ types. While we're at it, define a complete set of nd_intN_t types, for all values of N between 1 and 8, just as we have a complete set of nd_uintN_t types. --- diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5b9df8c9..879471be 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -323,6 +323,39 @@ and ask! the string is a sequence of XX:XX:... values for the bytes of the address. +* When defining a structure corresponding to a packet or part of a + packet, so that a pointer to packet data can be cast to a pointer to + that structure and that structure pointer used to refer to fields in + the packet, use the `nd_*` types for the structure members. + + Those types all are aligned only on a 1-byte boundary, so a + compiler will not assume that the structure is aligned on a boundary + stricter than one byte; there is no guarantee that fields in packets + are aligned on any particular boundary. + + This means that all padding in the structure must be explicitly + declared as fields in the structure. + + The `nd_*` types for integral values are: + + * `nd_uintN_t`, for unsigned integral values, where *N* is the number + of bytes in the value. + * `nd_intN_t`, for signed integral values, where *N* is the number + of bytes in the value. + + The `nd_*` types for IP addresses are: + + * `nd_ipv4`, for IPv4 addresses; + * `nd_ipv6`, for IPv6 addresses. + + The `nd_*` types for link-layer addresses are: + + * `nd_mac48`, for MAC-48 (Ethernet, 802.11, etc.) addresses; + * `nd_eui64`, for EUI-64 values. + + The `nd_*` type for a byte in a sequence of bytes is `nd_byte`; an + *N*-byte sequence should be declared as `nd_byte[N]`. + * Do invalid packet checks in code: Think that your code can receive in input not only a valid packet but any arbitrary random sequence of octets (packet * built malformed originally by the sender or by a fuzz tester, diff --git a/netdissect.h b/netdissect.h index 7255701e..6bbac0b1 100644 --- a/netdissect.h +++ b/netdissect.h @@ -59,9 +59,16 @@ typedef signed char nd_int8_t[1]; /* * "unsigned char" so that sign extension isn't done on the - * individual bytes while they're being assembled. + * individual bytes while they're being assembled. Use + * GET_S_BE_n() and GET_S_LE_n() macros to extract the value + * as a signed integer. */ +typedef unsigned char nd_int16_t[2]; +typedef unsigned char nd_int24_t[3]; typedef unsigned char nd_int32_t[4]; +typedef unsigned char nd_int40_t[5]; +typedef unsigned char nd_int48_t[6]; +typedef unsigned char nd_int56_t[7]; typedef unsigned char nd_int64_t[8]; #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */