Wireshark 4.7.0
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
20extern "C" {
21#endif /* __cplusplus */
22
23/* Types of "global" addresses Wireshark knows about. */
57
58
62typedef struct _address {
63 int type;
64 int len;
65 const void *data;
67 /* private */
68 void *priv;
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
82static 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
99static 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
124static 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
141static 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
162static 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
186static 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
219static 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
237static 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
266static 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
293static 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
311static inline void
312copy_address_shallow(address *to, const address *from) {
313 set_address(to, from->type, from->len, from->data);
314}
315
324static 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
335static inline void
336copy_address(address *to, const address *from) {
337 copy_address_wmem(NULL, to, from);
338}
339
346static 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
363static inline void
364free_address(address *addr) {
365 free_address_wmem(NULL, addr);
366}
367
375static 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
396static 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
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
Identifies the type of a network layer or link layer address.
Definition address.h:37
@ AT_ETHER
Definition address.h:39
@ AT_IPX
Definition address.h:42
@ AT_IB
Definition address.h:47
@ AT_IPv6
Definition address.h:41
@ AT_MCTP
Definition address.h:51
@ AT_NUMERIC
Definition address.h:50
@ AT_FCWWN
Definition address.h:44
@ AT_FC
Definition address.h:43
@ AT_ILNP_NID
Definition address.h:52
@ AT_ILNP_L64
Definition address.h:53
@ AT_IPv4
Definition address.h:40
@ AT_STRINGZ
Definition address.h:45
@ AT_NONE
Definition address.h:38
@ AT_VINES
Definition address.h:49
@ AT_END_OF_LIST
Definition address.h:55
@ AT_ILNP_ILV
Definition address.h:54
@ AT_AX25
Definition address.h:48
@ AT_EUI64
Definition address.h:46
struct _address address
Holds a network or link-layer address of any supported type.
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
Transport-layer port number types recognized by Wireshark.
Definition address.h:425
@ PT_MCTP
Definition address.h:439
@ PT_DDP
Definition address.h:432
@ PT_USB
Definition address.h:434
@ PT_BLUETOOTH
Definition address.h:437
@ PT_IBQP
Definition address.h:436
@ PT_I2C
Definition address.h:435
@ PT_TCP
Definition address.h:428
@ PT_NONE
Definition address.h:426
@ PT_IPX
Definition address.h:431
@ PT_IDP
Definition address.h:433
@ PT_UDP
Definition address.h:429
@ PT_DCCP
Definition address.h:430
@ PT_IWARP_MPA
Definition address.h:438
@ PT_SCTP
Definition address.h:427
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
void * wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
Copies a block of memory.
Definition wmem_miscutl.c:19
void wmem_free(wmem_allocator_t *allocator, void *ptr)
Returns the allocated memory to the allocator.
Definition wmem_core.c:62
Holds a network or link-layer address of any supported type.
Definition address.h:62
int len
Definition address.h:64
void * priv
Definition address.h:68
int type
Definition address.h:63
const void * data
Definition address.h:65
Internal memory allocator interface used by the wmem subsystem.
Definition wmem_allocator.h:34
Represents a 128-bit IPv6 address.
Definition inet_addr.h:27
Definition inet_cidr.h:22
Definition inet_cidr.h:27
Core tvbuff (testy virtual buffer) structure representing a region of packet data,...
Definition tvbuff-int.h:95
#define ws_assert(expr)
Unconditionally assert an expression when assertions are enabled.
Definition ws_assert.h:102