| 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
23extern "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 */
37typedef 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
70static inline void
71clear_address(address *addr)
72{
73 addr->type = AT_NONE;
74 addr->len = 0;
75 addr->data = NULL;
76 addr->priv = NULL;
77}
78
87static inline void
88set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
89 if (addr_len == 0) {
90 /* Zero length must mean no data */
91 ws_assert(addr_data == NULL);
92 } else {
93 /* Must not be AT_NONE - AT_NONE must have no data */
94 ws_assert(addr_type != AT_NONE);
95 /* Make sure we *do* have data */
96 ws_assert(addr_data != NULL);
97 }
98 addr->type = addr_type;
99 addr->len = addr_len;
100 addr->data = addr_data;
101 addr->priv = NULL;
102}
103
104static inline void
105set_address_ipv4(address *addr, const ipv4_addr_and_mask *ipv4) {
106 addr->type = AT_IPv4;
107 addr->len = 4;
108 uint32_t val = g_htonl(ipv4->addr);
109 addr->priv = g_memdup2(&val, sizeof(val));
110 addr->data = addr->priv;
111}
112
113static inline void
114set_address_ipv6(address *addr, const ipv6_addr_and_prefix *ipv6) {
115 set_address(addr, AT_IPv6, sizeof(ws_in6_addr), &ipv6->addr);
116}
117
133static inline void
134set_address_tvb(address *addr, int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
135 const void *p;
136
137 if (addr_len != 0) {
138 /* Must not be AT_NONE - AT_NONE must have no data */
139 ws_assert(addr_type != AT_NONE);
140 p = tvb_get_ptr(tvb, offset, addr_len);
141 } else
142 p = NULL;
143 set_address(addr, addr_type, addr_len, p);
144}
145
156static inline void
157alloc_address_wmem(wmem_allocator_t *scope, address *addr,
158 int addr_type, int addr_len, const void *addr_data) {
159 ws_assert(addr);
160 clear_address(addr);
161 addr->type = addr_type;
162 if (addr_len == 0) {
163 /* Zero length must mean no data */
164 ws_assert(addr_data == NULL);
165 /* Nothing to copy */
166 return;
167 }
168 /* Must not be AT_NONE - AT_NONE must have no data */
169 ws_assert(addr_type != AT_NONE);
170 /* Make sure we *do* have data to copy */
171 ws_assert(addr_data != NULL);
172 addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
173 addr->len = addr_len;
174}
175
188static inline void
189alloc_address_tvb(wmem_allocator_t *scope, address *addr,
190 int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
191 const void *p;
192
193 p = tvb_get_ptr(tvb, offset, addr_len);
194 alloc_address_wmem(scope, addr, addr_type, addr_len, p);
195}
196
205static inline int
206cmp_address(const address *addr1, const address *addr2) {
207 if (addr1->type > addr2->type) return 1;
208 if (addr1->type < addr2->type) return -1;
209 if (addr1->len > addr2->len) return 1;
210 if (addr1->len < addr2->len) return -1;
211 if (addr1->len == 0) {
212 /*
213 * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
214 * if both addresses are zero-length, don't compare them
215 * (there's nothing to compare, so they're equal).
216 */
217 return 0;
218 }
219 return memcmp(addr1->data, addr2->data, addr1->len);
220}
221
233static inline bool
234addresses_equal(const address *addr1, const address *addr2) {
235 /*
236 * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
237 * if both addresses are zero-length, don't compare them
238 * (there's nothing to compare, so they're equal).
239 */
240 if (addr1->type == addr2->type &&
241 addr1->len == addr2->len &&
242 (addr1->len == 0 ||
243 memcmp(addr1->data, addr2->data, addr1->len) == 0))
244 return true;
245 return false;
246}
247
259static inline bool
260addresses_data_equal(const address *addr1, const address *addr2) {
261 if ( addr1->len == addr2->len
262 && memcmp(addr1->data, addr2->data, addr1->len) == 0
263 ) return true;
264 return false;
265}
266
276static inline void
277copy_address_shallow(address *to, const address *from) {
278 set_address(to, from->type, from->len, from->data);
279}
280
288static inline void
289copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
290 alloc_address_wmem(scope, to, from->type, from->len, from->data);
291}
292
298static inline void
299copy_address(address *to, const address *from) {
300 copy_address_wmem(NULL, to, from);
301}
302
308static inline void
309free_address_wmem(wmem_allocator_t *scope, address *addr) {
310 /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
311 if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
312 /* Make sure API use is correct */
313 /* if priv is not null then data == priv */
314 ws_assert(addr->data == addr->priv);
315 wmem_free(scope, addr->priv);
316 }
317 clear_address(addr);
318}
319
324static inline void
325free_address(address *addr) {
326 free_address_wmem(NULL, addr);
327}
328
335static inline unsigned
336add_address_to_hash(unsigned hash_val, const address *addr) {
337 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
338 int idx;
339
340 for (idx = 0; idx < (addr)->len; idx++) {
341 hash_val += hash_data[idx];
342 hash_val += ( hash_val << 10 );
343 hash_val ^= ( hash_val >> 6 );
344 }
345 return hash_val;
346}
347
355static inline uint64_t
356add_address_to_hash64(uint64_t hash_val, const address *addr) {
357 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
358 int idx;
359
360 for (idx = 0; idx < (addr)->len; idx++) {
361 hash_val += hash_data[idx];
362 hash_val += ( hash_val << 10 );
363 hash_val ^= ( hash_val >> 6 );
364 }
365 return hash_val;
366}
367
368WS_DLL_PUBLIC unsigned address_to_bytes(const address *addr, uint8_t *buf, unsigned buf_len);
369
370/* Types of port numbers Wireshark knows about. */
371typedef enum {
372 PT_NONE, /* no port number */
373 PT_SCTP, /* SCTP */
374 PT_TCP, /* TCP */
375 PT_UDP, /* UDP */
376 PT_DCCP, /* DCCP */
377 PT_IPX, /* IPX sockets */
378 PT_DDP, /* DDP AppleTalk connection */
379 PT_IDP, /* XNS IDP sockets */
380 PT_USB, /* USB endpoint 0xffff means the host */
381 PT_I2C,
382 PT_IBQP, /* Infiniband QP number */
383 PT_BLUETOOTH,
384 PT_IWARP_MPA, /* iWarp MPA */
385 PT_MCTP
386} port_type;
387
388#ifdef __cplusplus
389}
390#endif /* __cplusplus */
391
392#endif /* __ADDRESS_H__ */
393
394/*
395 * Editor modelines - https://www.wireshark.org/tools/modelines.html
396 *
397 * Local variables:
398 * c-basic-offset: 4
399 * tab-width: 8
400 * indent-tabs-mode: nil
401 * End:
402 *
403 * vi: set shiftwidth=4 tabstop=8 expandtab:
404 * :indentSize=4:tabSize=8:noTabs=true:
405 */
tvb_get_ptr
const uint8_t * tvb_get_ptr(tvbuff_t *tvb, const int offset, const int length)
Returns a raw pointer to tvbuff data. Use with extreme caution.
Definition tvbuff.c:996
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:35
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