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.
* 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.
*/
* 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 */
* 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 */
#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>
#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.
}
}
trunc:
- UNALIGNED_MEMCPY(&retval, &ip->ip_dst.s_addr, sizeof(uint32_t));
+ UNALIGNED_MEMCPY(&retval, &ip->ip_dst, sizeof(uint32_t));
return retval;
}
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);
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));
{
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 */
} 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;
}