Wireshark  4.3.0
The Wireshark network protocol analyzer
address.h
Go to the documentation of this file.
1 
12 #ifndef __ADDRESS_H__
13 #define __ADDRESS_H__
14 
15 #include <string.h> /* for memcmp */
16 
17 #include "tvbuff.h"
18 #include <epan/wmem_scopes.h>
19 #include <wsutil/ws_assert.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24 
25 /* Types of "global" addresses Wireshark knows about. */
26 /* Address types can be added here if there are many dissectors that use them or just
27  * within a specific dissector.
28  * If an address type is added here, it must be "registered" within address_types.c
29  * For dissector address types, just use the address_type_dissector_register function
30  * from address_types.h
31  *
32  * AT_NUMERIC - a numeric address type can consist of a guint8, guint16, guint32 or guint64
33  * value. If no correct length is provided, to avoid data bleed, a guint8 is
34  * assumed. Only representation (aka conversion of value to string) is implemented for this type.
35  */
36 typedef enum {
37  AT_NONE, /* no link-layer address */
38  AT_ETHER, /* MAC (Ethernet, 802.x, FDDI) address */
39  AT_IPv4, /* IPv4 */
40  AT_IPv6, /* IPv6 */
41  AT_IPX, /* IPX */
42  AT_FC, /* Fibre Channel */
43  AT_FCWWN, /* Fibre Channel WWN */
44  AT_STRINGZ, /* null-terminated string */
45  AT_EUI64, /* IEEE EUI-64 */
46  AT_IB, /* Infiniband GID/LID */
47  AT_AX25, /* AX.25 */
48  AT_VINES, /* Banyan Vines address */
49  AT_NUMERIC, /* Numeric address type. */
50  AT_MCTP, /* MCTP */
51 
52  AT_END_OF_LIST /* Must be last in list */
53 } address_type;
54 
55 typedef struct _address {
56  int type; /* type of address */
57  int len; /* length of address, in bytes */
58  const void *data; /* pointer to address data */
59 
60  /* private */
61  void *priv;
62 } address;
63 
64 #define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
65 #define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
66 
67 static inline void
68 clear_address(address *addr)
69 {
70  addr->type = AT_NONE;
71  addr->len = 0;
72  addr->data = NULL;
73  addr->priv = NULL;
74 }
75 
84 static inline void
85 set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
86  if (addr_len == 0) {
87  /* Zero length must mean no data */
88  ws_assert(addr_data == NULL);
89  } else {
90  /* Must not be AT_NONE - AT_NONE must have no data */
91  ws_assert(addr_type != AT_NONE);
92  /* Make sure we *do* have data */
93  ws_assert(addr_data != NULL);
94  }
95  addr->type = addr_type;
96  addr->len = addr_len;
97  addr->data = addr_data;
98  addr->priv = NULL;
99 }
100 
116 static inline void
117 set_address_tvb(address *addr, int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
118  const void *p;
119 
120  if (addr_len != 0) {
121  /* Must not be AT_NONE - AT_NONE must have no data */
122  ws_assert(addr_type != AT_NONE);
123  p = tvb_get_ptr(tvb, offset, addr_len);
124  } else
125  p = NULL;
126  set_address(addr, addr_type, addr_len, p);
127 }
128 
139 static inline void
140 alloc_address_wmem(wmem_allocator_t *scope, address *addr,
141  int addr_type, int addr_len, const void *addr_data) {
142  ws_assert(addr);
143  clear_address(addr);
144  addr->type = addr_type;
145  if (addr_len == 0) {
146  /* Zero length must mean no data */
147  ws_assert(addr_data == NULL);
148  /* Nothing to copy */
149  return;
150  }
151  /* Must not be AT_NONE - AT_NONE must have no data */
152  ws_assert(addr_type != AT_NONE);
153  /* Make sure we *do* have data to copy */
154  ws_assert(addr_data != NULL);
155  addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
156  addr->len = addr_len;
157 }
158 
171 static inline void
172 alloc_address_tvb(wmem_allocator_t *scope, address *addr,
173  int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
174  const void *p;
175 
176  p = tvb_get_ptr(tvb, offset, addr_len);
177  alloc_address_wmem(scope, addr, addr_type, addr_len, p);
178 }
179 
188 static inline int
189 cmp_address(const address *addr1, const address *addr2) {
190  if (addr1->type > addr2->type) return 1;
191  if (addr1->type < addr2->type) return -1;
192  if (addr1->len > addr2->len) return 1;
193  if (addr1->len < addr2->len) return -1;
194  if (addr1->len == 0) {
195  /*
196  * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
197  * if both addresses are zero-length, don't compare them
198  * (there's nothing to compare, so they're equal).
199  */
200  return 0;
201  }
202  return memcmp(addr1->data, addr2->data, addr1->len);
203 }
204 
216 static inline gboolean
217 addresses_equal(const address *addr1, const address *addr2) {
218  /*
219  * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
220  * if both addresses are zero-length, don't compare them
221  * (there's nothing to compare, so they're equal).
222  */
223  if (addr1->type == addr2->type &&
224  addr1->len == addr2->len &&
225  (addr1->len == 0 ||
226  memcmp(addr1->data, addr2->data, addr1->len) == 0))
227  return TRUE;
228  return FALSE;
229 }
230 
242 static inline gboolean
243 addresses_data_equal(const address *addr1, const address *addr2) {
244  if ( addr1->len == addr2->len
245  && memcmp(addr1->data, addr2->data, addr1->len) == 0
246  ) return TRUE;
247  return FALSE;
248 }
249 
259 static inline void
260 copy_address_shallow(address *to, const address *from) {
261  set_address(to, from->type, from->len, from->data);
262 }
263 
271 static inline void
272 copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
273  alloc_address_wmem(scope, to, from->type, from->len, from->data);
274 }
275 
281 static inline void
282 copy_address(address *to, const address *from) {
283  copy_address_wmem(NULL, to, from);
284 }
285 
291 static inline void
292 free_address_wmem(wmem_allocator_t *scope, address *addr) {
293  /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
294  if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
295  /* Make sure API use is correct */
296  /* if priv is not null then data == priv */
297  ws_assert(addr->data == addr->priv);
298  wmem_free(scope, addr->priv);
299  }
300  clear_address(addr);
301 }
302 
307 static inline void
308 free_address(address *addr) {
309  free_address_wmem(NULL, addr);
310 }
311 
318 static inline guint
319 add_address_to_hash(guint hash_val, const address *addr) {
320  const guint8 *hash_data = (const guint8 *)(addr)->data;
321  int idx;
322 
323  for (idx = 0; idx < (addr)->len; idx++) {
324  hash_val += hash_data[idx];
325  hash_val += ( hash_val << 10 );
326  hash_val ^= ( hash_val >> 6 );
327  }
328  return hash_val;
329 }
330 
338 static inline guint64
339 add_address_to_hash64(guint64 hash_val, const address *addr) {
340  const guint8 *hash_data = (const guint8 *)(addr)->data;
341  int idx;
342 
343  for (idx = 0; idx < (addr)->len; idx++) {
344  hash_val += hash_data[idx];
345  hash_val += ( hash_val << 10 );
346  hash_val ^= ( hash_val >> 6 );
347  }
348  return hash_val;
349 }
350 
351 WS_DLL_PUBLIC guint address_to_bytes(const address *addr, guint8 *buf, guint buf_len);
352 
353 /* Types of port numbers Wireshark knows about. */
354 typedef enum {
355  PT_NONE, /* no port number */
356  PT_SCTP, /* SCTP */
357  PT_TCP, /* TCP */
358  PT_UDP, /* UDP */
359  PT_DCCP, /* DCCP */
360  PT_IPX, /* IPX sockets */
361  PT_DDP, /* DDP AppleTalk connection */
362  PT_IDP, /* XNS IDP sockets */
363  PT_USB, /* USB endpoint 0xffff means the host */
364  PT_I2C,
365  PT_IBQP, /* Infiniband QP number */
366  PT_BLUETOOTH,
367  PT_IWARP_MPA, /* iWarp MPA */
368  PT_MCTP
369 } port_type;
370 
371 #ifdef __cplusplus
372 }
373 #endif /* __cplusplus */
374 
375 #endif /* __ADDRESS_H__ */
376 
377 /*
378  * Editor modelines - https://www.wireshark.org/tools/modelines.html
379  *
380  * Local variables:
381  * c-basic-offset: 4
382  * tab-width: 8
383  * indent-tabs-mode: nil
384  * End:
385  *
386  * vi: set shiftwidth=4 tabstop=8 expandtab:
387  * :indentSize=4:tabSize=8:noTabs=true:
388  */
const guint8 * tvb_get_ptr(tvbuff_t *tvb, const gint offset, const gint length)
Definition: tvbuff.c:1006
void * wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
Definition: wmem_miscutl.c:19
void wmem_free(wmem_allocator_t *allocator, void *ptr)
Definition: wmem_core.c:62
Definition: address.h:55
Definition: wmem_allocator.h:27
Definition: tvbuff-int.h:35