| Wireshark 4.7.3 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
address.h
Go to the documentation of this file.
1
11#pragma once
12#include <string.h> /* for memcmp */
13
14#include "tvbuff.h"
15#include <epan/wmem_scopes.h>
16#include <wsutil/ws_assert.h>
17#include <wsutil/inet_cidr.h>
18
19#ifdef __cplusplus
20 extern "C" {
21#endif /* __cplusplus */
22
23/* Types of "global" addresses Wireshark knows about. */
37 typedef enum {
38 AT_NONE,
39 AT_ETHER,
40 AT_IPv4,
41 AT_IPv6,
42 AT_IPX,
43 AT_FC,
44 AT_FCWWN,
45 AT_STRINGZ,
46 AT_EUI64,
47 AT_IB,
48 AT_AX25,
49 AT_VINES,
50 AT_NUMERIC,
51 AT_MCTP,
52 AT_ILNP_NID,
53 AT_ILNP_L64,
54 AT_ILNP_ILV,
55 AT_END_OF_LIST
56} address_type;
57
58
62 typedef struct _address {
63 int type;
64 int len;
65 const void *data;
66
67 /* private */
68 void *priv;
69} address;
70
71
73#define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
74
76#define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
77
82 static inline void
83clear_address(address *addr)
84{
85 addr->type = AT_NONE;
86 addr->len = 0;
87 addr->data = NULL;
88 addr->priv = NULL;
89}
90
99 static inline void
100set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
101 if (addr_len == 0) {
102 /* Zero length must mean no data */
103 ws_assert(addr_data == NULL);
104 } else {
105 /* Must not be AT_NONE - AT_NONE must have no data */
106 ws_assert(addr_type != AT_NONE);
107 /* Make sure we *do* have data */
108 ws_assert(addr_data != NULL);
109 }
110 addr->type = addr_type;
111 addr->len = addr_len;
112 addr->data = addr_data;
113 addr->priv = NULL;
114}
115
124 static inline void
125set_address_ipv4(address *addr, const ipv4_addr_and_mask *ipv4) {
126 addr->type = AT_IPv4;
127 addr->len = 4;
128 uint32_t val = g_htonl(ipv4->addr);
129 addr->priv = g_memdup2(&val, sizeof(val));
130 addr->data = addr->priv;
131}
132
141 static inline void
142set_address_ipv6(address *addr, const ipv6_addr_and_prefix *ipv6) {
143 set_address(addr, AT_IPv6, sizeof(ws_in6_addr), &ipv6->addr);
144}
145
162 static inline void
163set_address_tvb(address *addr, int addr_type, unsigned addr_len, tvbuff_t *tvb, unsigned offset) {
164 const void *p;
165
166 if (addr_len != 0) {
167 /* Must not be AT_NONE - AT_NONE must have no data */
168 ws_assert(addr_type != AT_NONE);
169 p = tvb_get_ptr(tvb, offset, addr_len);
170 } else
171 p = NULL;
172 set_address(addr, addr_type, addr_len, p);
173}
174
186 static inline void
187alloc_address_wmem(wmem_allocator_t *scope, address *addr,
188 int addr_type, int addr_len, const void *addr_data) {
189 ws_assert(addr);
190 clear_address(addr);
191 addr->type = addr_type;
192 if (addr_len == 0) {
193 /* Zero length must mean no data */
194 ws_assert(addr_data == NULL);
195 /* Nothing to copy */
196 return;
197 }
198 /* Must not be AT_NONE - AT_NONE must have no data */
199 ws_assert(addr_type != AT_NONE);
200 /* Make sure we *do* have data to copy */
201 ws_assert(addr_data != NULL);
202 addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
203 addr->len = addr_len;
204}
205
219 static inline void
220alloc_address_tvb(wmem_allocator_t *scope, address *addr,
221 int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
222 const void *p;
223
224 p = tvb_get_ptr(tvb, offset, addr_len);
225 alloc_address_wmem(scope, addr, addr_type, addr_len, p);
226}
227
237 static inline int
238cmp_address(const address *addr1, const address *addr2) {
239 if (addr1->type > addr2->type) return 1;
240 if (addr1->type < addr2->type) return -1;
241 if (addr1->len > addr2->len) return 1;
242 if (addr1->len < addr2->len) return -1;
243 if (addr1->len == 0) {
244 /*
245 * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
246 * if both addresses are zero-length, don't compare them
247 * (there's nothing to compare, so they're equal).
248 */
249 return 0;
250 }
251 return memcmp(addr1->data, addr2->data, addr1->len);
252}
253
266 static inline bool
267addresses_equal(const address *addr1, const address *addr2) {
268 /*
269 * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
270 * if both addresses are zero-length, don't compare them
271 * (there's nothing to compare, so they're equal).
272 */
273 if (addr1->type == addr2->type &&
274 addr1->len == addr2->len &&
275 (addr1->len == 0 ||
276 memcmp(addr1->data, addr2->data, addr1->len) == 0))
277 return true;
278 return false;
279}
280
293 static inline bool
294addresses_data_equal(const address *addr1, const address *addr2) {
295 if ( addr1->len == addr2->len
296 && memcmp(addr1->data, addr2->data, addr1->len) == 0
297 ) return true;
298 return false;
299}
300
311 static inline void
312copy_address_shallow(address *to, const address *from) {
313 set_address(to, from->type, from->len, from->data);
314}
315
324 static inline void
325copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
326 alloc_address_wmem(scope, to, from->type, from->len, from->data);
327}
328
335 static inline void
336copy_address(address *to, const address *from) {
337 copy_address_wmem(NULL, to, from);
338}
339
346 static inline void
347free_address_wmem(wmem_allocator_t *scope, address *addr) {
348 /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
349 if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
350 /* Make sure API use is correct */
351 /* if priv is not null then data == priv */
352 ws_assert(addr->data == addr->priv);
353 wmem_free(scope, addr->priv);
354 }
355 clear_address(addr);
356}
357
363 static inline void
364free_address(address *addr) {
365 free_address_wmem(NULL, addr);
366}
367
375 static inline unsigned
376add_address_to_hash(unsigned hash_val, const address *addr) {
377 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
378 int idx;
379
380 for (idx = 0; idx < (addr)->len; idx++) {
381 hash_val += hash_data[idx];
382 hash_val += ( hash_val << 10 );
383 hash_val ^= ( hash_val >> 6 );
384 }
385 return hash_val;
386}
387
396 static inline uint64_t
397add_address_to_hash64(uint64_t hash_val, const address *addr) {
398 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
399 int idx;
400
401 for (idx = 0; idx < (addr)->len; idx++) {
402 hash_val += hash_data[idx];
403 hash_val += ( hash_val << 10 );
404 hash_val ^= ( hash_val >> 6 );
405 }
406 return hash_val;
407}
408
420WS_DLL_PUBLIC unsigned address_to_bytes(const address *addr, uint8_t *buf, unsigned buf_len);
421
425 typedef enum {
426 PT_NONE,
427 PT_SCTP,
428 PT_TCP,
429 PT_UDP,
430 PT_DCCP,
431 PT_IPX,
432 PT_DDP,
433 PT_IDP,
434 PT_USB,
435 PT_I2C,
436 PT_IBQP,
437 PT_BLUETOOTH,
438 PT_IWARP_MPA,
439 PT_MCTP
440} port_type;
441
442#ifdef __cplusplus
443}
444#endif /* __cplusplus */
445
446/*
447 * Editor modelines - https://www.wireshark.org/tools/modelines.html
448 *
449 * Local variables:
450 * c-basic-offset: 4
451 * tab-width: 8
452 * indent-tabs-mode: nil
453 * End:
454 *
455 * vi: set shiftwidth=4 tabstop=8 expandtab:
456 * :indentSize=4:tabSize=8:noTabs=true:
457 */
address_type
address_type
Identifies the type of a network layer or link layer address.
Definition address.h:37
AT_ETHER
@ AT_ETHER
Definition address.h:39
AT_IPX
@ AT_IPX
Definition address.h:42
AT_IB
@ AT_IB
Definition address.h:47
AT_IPv6
@ AT_IPv6
Definition address.h:41
AT_MCTP
@ AT_MCTP
Definition address.h:51
AT_NUMERIC
@ AT_NUMERIC
Definition address.h:50
AT_FCWWN
@ AT_FCWWN
Definition address.h:44
AT_FC
@ AT_FC
Definition address.h:43
AT_ILNP_NID
@ AT_ILNP_NID
Definition address.h:52
AT_ILNP_L64
@ AT_ILNP_L64
Definition address.h:53
AT_IPv4
@ AT_IPv4
Definition address.h:40
AT_STRINGZ
@ AT_STRINGZ
Definition address.h:45
AT_NONE
@ AT_NONE
Definition address.h:38
AT_VINES
@ AT_VINES
Definition address.h:49
AT_END_OF_LIST
@ AT_END_OF_LIST
Definition address.h:55
AT_ILNP_ILV
@ AT_ILNP_ILV
Definition address.h:54
AT_AX25
@ AT_AX25
Definition address.h:48
AT_EUI64
@ AT_EUI64
Definition address.h:46
address
struct _address address
Holds a network or link-layer address of any supported type.
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
port_type
port_type
Transport-layer port number types recognized by Wireshark.
Definition address.h:425
PT_MCTP
@ PT_MCTP
Definition address.h:439
PT_DDP
@ PT_DDP
Definition address.h:432
PT_USB
@ PT_USB
Definition address.h:434
PT_BLUETOOTH
@ PT_BLUETOOTH
Definition address.h:437
PT_IBQP
@ PT_IBQP
Definition address.h:436
PT_I2C
@ PT_I2C
Definition address.h:435
PT_TCP
@ PT_TCP
Definition address.h:428
PT_NONE
@ PT_NONE
Definition address.h:426
PT_IPX
@ PT_IPX
Definition address.h:431
PT_IDP
@ PT_IDP
Definition address.h:433
PT_UDP
@ PT_UDP
Definition address.h:429
PT_DCCP
@ PT_DCCP
Definition address.h:430
PT_IWARP_MPA
@ PT_IWARP_MPA
Definition address.h:438
PT_SCTP
@ PT_SCTP
Definition address.h:427
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:1122
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
wmem_allocator_t
struct _wmem_allocator_t wmem_allocator_t
Definition wmem_core.h:44
ws_in6_addr
struct e_in6_addr ws_in6_addr
Represents a 128-bit IPv6 address.
inet_cidr.h
_address
Holds a network or link-layer address of any supported type.
Definition address.h:62
_address::len
int len
Definition address.h:64
_address::priv
void * priv
Definition address.h:68
_address::type
int type
Definition address.h:63
_address::data
const void * data
Definition address.h:65
ipv4_addr_and_mask
Holds an IPv4 address paired with its subnet mask, both in host byte order.
Definition inet_cidr.h:25
ipv6_addr_and_prefix
Holds an IPv6 address paired with its prefix length.
Definition inet_cidr.h:33
ipv6_addr_and_prefix::addr
ws_in6_addr addr
Definition inet_cidr.h:34
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