| Wireshark 4.7.0 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
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#include <wsutil/inet_cidr.h>
21
22#ifdef __cplusplus
23 extern "C" {
24#endif /* __cplusplus */
25
26/* Types of "global" addresses Wireshark knows about. */
27/* Address types can be added here if there are many dissectors that use them or just
28 * within a specific dissector.
29 * If an address type is added here, it must be "registered" within address_types.c
30 * For dissector address types, just use the address_type_dissector_register function
31 * from address_types.h
32 *
33 * AT_NUMERIC - a numeric address type can consist of a uint8_t, uint16_t, uint32_t or uint64_t
34 * value. If no correct length is provided, to avoid data bleed, a uint8_t is
35 * assumed. Only representation (aka conversion of value to string) is implemented for this type.
36 */
37 typedef enum {
38 AT_NONE, /* no link-layer address */
39 AT_ETHER, /* MAC (Ethernet, 802.x, FDDI) address */
40 AT_IPv4, /* IPv4 */
41 AT_IPv6, /* IPv6 */
42 AT_IPX, /* IPX */
43 AT_FC, /* Fibre Channel */
44 AT_FCWWN, /* Fibre Channel WWN */
45 AT_STRINGZ, /* null-terminated string */
46 AT_EUI64, /* IEEE EUI-64 */
47 AT_IB, /* Infiniband GID/LID */
48 AT_AX25, /* AX.25 */
49 AT_VINES, /* Banyan Vines address */
50 AT_NUMERIC, /* Numeric address type. */
51 AT_MCTP, /* MCTP */
52 AT_ILNP_NID, /* ILNP NID */
53 AT_ILNP_L64, /* ILNP L64 */
54 AT_ILNP_ILV, /* ILNP ILV */
55 AT_END_OF_LIST /* Must be last in list */
56} address_type;
57
58 typedef struct _address {
59 int type; /* type of address */
60 int len; /* length of address, in bytes */
61 const void *data; /* pointer to address data */
62
63 /* private */
64 void *priv;
65} address;
66
67#define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
68#define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
69
74 static inline void
75clear_address(address *addr)
76{
77 addr->type = AT_NONE;
78 addr->len = 0;
79 addr->data = NULL;
80 addr->priv = NULL;
81}
82
91 static inline void
92set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
93 if (addr_len == 0) {
94 /* Zero length must mean no data */
95 ws_assert(addr_data == NULL);
96 } else {
97 /* Must not be AT_NONE - AT_NONE must have no data */
98 ws_assert(addr_type != AT_NONE);
99 /* Make sure we *do* have data */
100 ws_assert(addr_data != NULL);
101 }
102 addr->type = addr_type;
103 addr->len = addr_len;
104 addr->data = addr_data;
105 addr->priv = NULL;
106}
107
116 static inline void
117set_address_ipv4(address *addr, const ipv4_addr_and_mask *ipv4) {
118 addr->type = AT_IPv4;
119 addr->len = 4;
120 uint32_t val = g_htonl(ipv4->addr);
121 addr->priv = g_memdup2(&val, sizeof(val));
122 addr->data = addr->priv;
123}
124
133 static inline void
134set_address_ipv6(address *addr, const ipv6_addr_and_prefix *ipv6) {
135 set_address(addr, AT_IPv6, sizeof(ws_in6_addr), &ipv6->addr);
136}
137
154 static inline void
155set_address_tvb(address *addr, int addr_type, unsigned addr_len, tvbuff_t *tvb, unsigned offset) {
156 const void *p;
157
158 if (addr_len != 0) {
159 /* Must not be AT_NONE - AT_NONE must have no data */
160 ws_assert(addr_type != AT_NONE);
161 p = tvb_get_ptr(tvb, offset, addr_len);
162 } else
163 p = NULL;
164 set_address(addr, addr_type, addr_len, p);
165}
166
178 static inline void
179alloc_address_wmem(wmem_allocator_t *scope, address *addr,
180 int addr_type, int addr_len, const void *addr_data) {
181 ws_assert(addr);
182 clear_address(addr);
183 addr->type = addr_type;
184 if (addr_len == 0) {
185 /* Zero length must mean no data */
186 ws_assert(addr_data == NULL);
187 /* Nothing to copy */
188 return;
189 }
190 /* Must not be AT_NONE - AT_NONE must have no data */
191 ws_assert(addr_type != AT_NONE);
192 /* Make sure we *do* have data to copy */
193 ws_assert(addr_data != NULL);
194 addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
195 addr->len = addr_len;
196}
197
211 static inline void
212alloc_address_tvb(wmem_allocator_t *scope, address *addr,
213 int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
214 const void *p;
215
216 p = tvb_get_ptr(tvb, offset, addr_len);
217 alloc_address_wmem(scope, addr, addr_type, addr_len, p);
218}
219
229 static inline int
230cmp_address(const address *addr1, const address *addr2) {
231 if (addr1->type > addr2->type) return 1;
232 if (addr1->type < addr2->type) return -1;
233 if (addr1->len > addr2->len) return 1;
234 if (addr1->len < addr2->len) return -1;
235 if (addr1->len == 0) {
236 /*
237 * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
238 * if both addresses are zero-length, don't compare them
239 * (there's nothing to compare, so they're equal).
240 */
241 return 0;
242 }
243 return memcmp(addr1->data, addr2->data, addr1->len);
244}
245
258 static inline bool
259addresses_equal(const address *addr1, const address *addr2) {
260 /*
261 * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
262 * if both addresses are zero-length, don't compare them
263 * (there's nothing to compare, so they're equal).
264 */
265 if (addr1->type == addr2->type &&
266 addr1->len == addr2->len &&
267 (addr1->len == 0 ||
268 memcmp(addr1->data, addr2->data, addr1->len) == 0))
269 return true;
270 return false;
271}
272
285 static inline bool
286addresses_data_equal(const address *addr1, const address *addr2) {
287 if ( addr1->len == addr2->len
288 && memcmp(addr1->data, addr2->data, addr1->len) == 0
289 ) return true;
290 return false;
291}
292
303 static inline void
304copy_address_shallow(address *to, const address *from) {
305 set_address(to, from->type, from->len, from->data);
306}
307
316 static inline void
317copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
318 alloc_address_wmem(scope, to, from->type, from->len, from->data);
319}
320
327 static inline void
328copy_address(address *to, const address *from) {
329 copy_address_wmem(NULL, to, from);
330}
331
338 static inline void
339free_address_wmem(wmem_allocator_t *scope, address *addr) {
340 /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
341 if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
342 /* Make sure API use is correct */
343 /* if priv is not null then data == priv */
344 ws_assert(addr->data == addr->priv);
345 wmem_free(scope, addr->priv);
346 }
347 clear_address(addr);
348}
349
355 static inline void
356free_address(address *addr) {
357 free_address_wmem(NULL, addr);
358}
359
367 static inline unsigned
368add_address_to_hash(unsigned hash_val, const address *addr) {
369 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
370 int idx;
371
372 for (idx = 0; idx < (addr)->len; idx++) {
373 hash_val += hash_data[idx];
374 hash_val += ( hash_val << 10 );
375 hash_val ^= ( hash_val >> 6 );
376 }
377 return hash_val;
378}
379
388 static inline uint64_t
389add_address_to_hash64(uint64_t hash_val, const address *addr) {
390 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
391 int idx;
392
393 for (idx = 0; idx < (addr)->len; idx++) {
394 hash_val += hash_data[idx];
395 hash_val += ( hash_val << 10 );
396 hash_val ^= ( hash_val >> 6 );
397 }
398 return hash_val;
399}
400
412WS_DLL_PUBLIC unsigned address_to_bytes(const address *addr, uint8_t *buf, unsigned buf_len);
413
414/* Types of port numbers Wireshark knows about. */
415 typedef enum {
416 PT_NONE, /* no port number */
417 PT_SCTP, /* SCTP */
418 PT_TCP, /* TCP */
419 PT_UDP, /* UDP */
420 PT_DCCP, /* DCCP */
421 PT_IPX, /* IPX sockets */
422 PT_DDP, /* DDP AppleTalk connection */
423 PT_IDP, /* XNS IDP sockets */
424 PT_USB, /* USB endpoint 0xffff means the host */
425 PT_I2C,
426 PT_IBQP, /* Infiniband QP number */
427 PT_BLUETOOTH,
428 PT_IWARP_MPA, /* iWarp MPA */
429 PT_MCTP
430} port_type;
431
432#ifdef __cplusplus
433}
434#endif /* __cplusplus */
435
436#endif /* __ADDRESS_H__ */
437
438/*
439 * Editor modelines - https://www.wireshark.org/tools/modelines.html
440 *
441 * Local variables:
442 * c-basic-offset: 4
443 * tab-width: 8
444 * indent-tabs-mode: nil
445 * End:
446 *
447 * vi: set shiftwidth=4 tabstop=8 expandtab:
448 * :indentSize=4:tabSize=8:noTabs=true:
449 */
address_to_bytes
WS_DLL_PUBLIC unsigned address_to_bytes(const address *addr, uint8_t *buf, unsigned buf_len)
Converts an address to a byte array.
Definition address_types.c:989
tvb_get_ptr
const uint8_t * tvb_get_ptr(tvbuff_t *tvb, const unsigned offset, const unsigned length)
Returns a raw pointer to tvbuff data. Use with extreme caution.
Definition tvbuff.c:1129
wmem_memdup
void * wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
Copies a block of memory.
Definition wmem_miscutl.c:19
wmem_free
void wmem_free(wmem_allocator_t *allocator, void *ptr)
Returns the allocated memory to the allocator.
Definition wmem_core.c:62
inet_cidr.h
_address
Definition address.h:58
_wmem_allocator_t
Internal memory allocator interface used by the wmem subsystem.
Definition wmem_allocator.h:34
e_in6_addr
Represents a 128-bit IPv6 address.
Definition inet_addr.h:27
ipv4_addr_and_mask
Definition inet_cidr.h:22
ipv6_addr_and_prefix
Definition inet_cidr.h:27
tvbuff
Definition tvbuff-int.h:36
tvbuff.h
wmem_scopes.h
ws_assert.h
ws_assert
#define ws_assert(expr)
Unconditionally assert an expression when assertions are enabled.
Definition ws_assert.h:102