Wireshark  2.9.0-477-g68ec514b
The Wireshark network protocol analyzer
address.h
1 /* address.h
2  * Definitions for structures storing addresses, and for the type of
3  * variables holding port-type values
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #ifndef __ADDRESS_H__
13 #define __ADDRESS_H__
14 
15 #include <string.h> /* for memcmp */
16 
17 #include "tvbuff.h"
18 #include "wmem/wmem.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif /* __cplusplus */
23 
24 /* Types of "global" addresses Wireshark knows about. */
25 /* Address types can be added here if there are many dissectors that use them or just
26  * within a specific dissector.
27  * If an address type is added here, it must be "registered" within address_types.c
28  * For dissector address types, just use the address_type_dissector_register function
29  * from address_types.h
30  */
31 typedef enum {
32  AT_NONE, /* no link-layer address */
33  AT_ETHER, /* MAC (Ethernet, 802.x, FDDI) address */
34  AT_IPv4, /* IPv4 */
35  AT_IPv6, /* IPv6 */
36  AT_IPX, /* IPX */
37  AT_FC, /* Fibre Channel */
38  AT_FCWWN, /* Fibre Channel WWN */
39  AT_STRINGZ, /* null-terminated string */
40  AT_EUI64, /* IEEE EUI-64 */
41  AT_IB, /* Infiniband GID/LID */
42  AT_AX25, /* AX.25 */
43 
44  AT_END_OF_LIST /* Must be last in list */
45 } address_type;
46 
47 typedef struct _address {
48  int type; /* type of address */
49  int len; /* length of address, in bytes */
50  const void *data; /* pointer to address data */
51 
52  /* private */
53  void *priv;
54 } address;
55 
56 #define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
57 #define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
58 
59 static inline void
60 clear_address(address *addr)
61 {
62  addr->type = AT_NONE;
63  addr->len = 0;
64  addr->data = NULL;
65  addr->priv = NULL;
66 }
67 
76 static inline void
77 set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
78  if (addr_len == 0) {
79  /* Zero length must mean no data */
80  g_assert(addr_data == NULL);
81  } else {
82  /* Must not be AT_NONE - AT_NONE must have no data */
83  g_assert(addr_type != AT_NONE);
84  /* Make sure we *do* have data */
85  g_assert(addr_data != NULL);
86  }
87  addr->type = addr_type;
88  addr->len = addr_len;
89  addr->data = addr_data;
90  addr->priv = NULL;
91 }
92 
108 static inline void
109 set_address_tvb(address *addr, int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
110  const void *p;
111 
112  if (addr_len != 0) {
113  /* Must not be AT_NONE - AT_NONE must have no data */
114  g_assert(addr_type != AT_NONE);
115  p = tvb_get_ptr(tvb, offset, addr_len);
116  } else
117  p = NULL;
118  set_address(addr, addr_type, addr_len, p);
119 }
120 
131 static inline void
132 alloc_address_wmem(wmem_allocator_t *scope, address *addr,
133  int addr_type, int addr_len, const void *addr_data) {
134  g_assert(addr);
135  clear_address(addr);
136  addr->type = addr_type;
137  if (addr_len == 0) {
138  /* Zero length must mean no data */
139  g_assert(addr_data == NULL);
140  /* Nothing to copy */
141  return;
142  }
143  /* Must not be AT_NONE - AT_NONE must have no data */
144  g_assert(addr_type != AT_NONE);
145  /* Make sure we *do* have data to copy */
146  g_assert(addr_data != NULL);
147  addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
148  addr->len = addr_len;
149 }
150 
163 static inline void
164 alloc_address_tvb(wmem_allocator_t *scope, address *addr,
165  int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
166  const void *p;
167 
168  p = tvb_get_ptr(tvb, offset, addr_len);
169  alloc_address_wmem(scope, addr, addr_type, addr_len, p);
170 }
171 
180 static inline int
181 cmp_address(const address *addr1, const address *addr2) {
182  if (addr1->type > addr2->type) return 1;
183  if (addr1->type < addr2->type) return -1;
184  if (addr1->len > addr2->len) return 1;
185  if (addr1->len < addr2->len) return -1;
186  if (addr1->len == 0) {
187  /*
188  * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
189  * if both addresses are zero-length, don't compare them
190  * (there's nothing to compare, so they're equal).
191  */
192  return 0;
193  }
194  return memcmp(addr1->data, addr2->data, addr1->len);
195 }
196 
208 static inline gboolean
209 addresses_equal(const address *addr1, const address *addr2) {
210  /*
211  * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
212  * if both addresses are zero-length, don't compare them
213  * (there's nothing to compare, so they're equal).
214  */
215  if (addr1->type == addr2->type &&
216  addr1->len == addr2->len &&
217  (addr1->len == 0 ||
218  memcmp(addr1->data, addr2->data, addr1->len) == 0))
219  return TRUE;
220  return FALSE;
221 }
222 
234 static inline gboolean
235 addresses_data_equal(const address *addr1, const address *addr2) {
236  if ( addr1->len == addr2->len
237  && memcmp(addr1->data, addr2->data, addr1->len) == 0
238  ) return TRUE;
239  return FALSE;
240 }
241 
251 static inline void
252 copy_address_shallow(address *to, const address *from) {
253  set_address(to, from->type, from->len, from->data);
254 }
255 
263 static inline void
264 copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
265  alloc_address_wmem(scope, to, from->type, from->len, from->data);
266 }
267 
273 static inline void
274 copy_address(address *to, const address *from) {
275  copy_address_wmem(NULL, to, from);
276 }
277 
283 static inline void
284 free_address_wmem(wmem_allocator_t *scope, address *addr) {
285  /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
286  if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
287  /* Make sure API use is correct */
288  /* if priv is not null then data == priv */
289  g_assert(addr->data == addr->priv);
290  wmem_free(scope, addr->priv);
291  }
292  clear_address(addr);
293 }
294 
299 static inline void
300 free_address(address *addr) {
301  free_address_wmem(NULL, addr);
302 }
303 
310 static inline guint
311 add_address_to_hash(guint hash_val, const address *addr) {
312  const guint8 *hash_data = (const guint8 *)(addr)->data;
313  int idx;
314 
315  for (idx = 0; idx < (addr)->len; idx++) {
316  hash_val += hash_data[idx];
317  hash_val += ( hash_val << 10 );
318  hash_val ^= ( hash_val >> 6 );
319  }
320  return hash_val;
321 }
322 
330 static inline guint64
331 add_address_to_hash64(guint64 hash_val, const address *addr) {
332  const guint8 *hash_data = (const guint8 *)(addr)->data;
333  int idx;
334 
335  for (idx = 0; idx < (addr)->len; idx++) {
336  hash_val += hash_data[idx];
337  hash_val += ( hash_val << 10 );
338  hash_val ^= ( hash_val >> 6 );
339  }
340  return hash_val;
341 }
342 
343 WS_DLL_PUBLIC guint address_to_bytes(const address *addr, guint8 *buf, guint buf_len);
344 
345 /* Types of port numbers Wireshark knows about. */
346 typedef enum {
347  PT_NONE, /* no port number */
348  PT_SCTP, /* SCTP */
349  PT_TCP, /* TCP */
350  PT_UDP, /* UDP */
351  PT_DCCP, /* DCCP */
352  PT_IPX, /* IPX sockets */
353  PT_DDP, /* DDP AppleTalk connection */
354  PT_IDP, /* XNS IDP sockets */
355  PT_USB, /* USB endpoint 0xffff means the host */
356  PT_I2C,
357  PT_IBQP, /* Infiniband QP number */
358  PT_BLUETOOTH
359 } port_type;
360 
361 #ifdef __cplusplus
362 }
363 #endif /* __cplusplus */
364 
365 #endif /* __ADDRESS_H__ */
366 
367 /*
368  * Editor modelines - http://www.wireshark.org/tools/modelines.html
369  *
370  * Local variables:
371  * c-basic-offset: 4
372  * tab-width: 8
373  * indent-tabs-mode: nil
374  * End:
375  *
376  * vi: set shiftwidth=4 tabstop=8 expandtab:
377  * :indentSize=4:tabSize=8:noTabs=true:
378  */
Definition: tvbuff-int.h:35
void * wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
Definition: wmem_miscutl.c:19
Definition: wmem_allocator.h:26
void wmem_free(wmem_allocator_t *allocator, void *ptr)
Definition: wmem_core.c:64
const guint8 * tvb_get_ptr(tvbuff_t *tvb, const gint offset, const gint length)
Definition: tvbuff.c:897
Definition: address.h:47