Wireshark 4.5.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
53 AT_END_OF_LIST /* Must be last in list */
54} address_type;
55
56typedef struct _address {
57 int type; /* type of address */
58 int len; /* length of address, in bytes */
59 const void *data; /* pointer to address data */
60
61 /* private */
62 void *priv;
63} address;
64
65#define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
66#define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
67
68static inline void
69clear_address(address *addr)
70{
71 addr->type = AT_NONE;
72 addr->len = 0;
73 addr->data = NULL;
74 addr->priv = NULL;
75}
76
85static inline void
86set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
87 if (addr_len == 0) {
88 /* Zero length must mean no data */
89 ws_assert(addr_data == NULL);
90 } else {
91 /* Must not be AT_NONE - AT_NONE must have no data */
92 ws_assert(addr_type != AT_NONE);
93 /* Make sure we *do* have data */
94 ws_assert(addr_data != NULL);
95 }
96 addr->type = addr_type;
97 addr->len = addr_len;
98 addr->data = addr_data;
99 addr->priv = NULL;
100}
101
102static inline void
103set_address_ipv4(address *addr, const ipv4_addr_and_mask *ipv4) {
104 addr->type = AT_IPv4;
105 addr->len = 4;
106 uint32_t val = g_htonl(ipv4->addr);
107 addr->priv = g_memdup2(&val, sizeof(val));
108 addr->data = addr->priv;
109}
110
111static inline void
112set_address_ipv6(address *addr, const ipv6_addr_and_prefix *ipv6) {
113 set_address(addr, AT_IPv6, sizeof(ws_in6_addr), &ipv6->addr);
114}
115
131static inline void
132set_address_tvb(address *addr, int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
133 const void *p;
134
135 if (addr_len != 0) {
136 /* Must not be AT_NONE - AT_NONE must have no data */
137 ws_assert(addr_type != AT_NONE);
138 p = tvb_get_ptr(tvb, offset, addr_len);
139 } else
140 p = NULL;
141 set_address(addr, addr_type, addr_len, p);
142}
143
154static inline void
155alloc_address_wmem(wmem_allocator_t *scope, address *addr,
156 int addr_type, int addr_len, const void *addr_data) {
157 ws_assert(addr);
158 clear_address(addr);
159 addr->type = addr_type;
160 if (addr_len == 0) {
161 /* Zero length must mean no data */
162 ws_assert(addr_data == NULL);
163 /* Nothing to copy */
164 return;
165 }
166 /* Must not be AT_NONE - AT_NONE must have no data */
167 ws_assert(addr_type != AT_NONE);
168 /* Make sure we *do* have data to copy */
169 ws_assert(addr_data != NULL);
170 addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
171 addr->len = addr_len;
172}
173
186static inline void
187alloc_address_tvb(wmem_allocator_t *scope, address *addr,
188 int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
189 const void *p;
190
191 p = tvb_get_ptr(tvb, offset, addr_len);
192 alloc_address_wmem(scope, addr, addr_type, addr_len, p);
193}
194
203static inline int
204cmp_address(const address *addr1, const address *addr2) {
205 if (addr1->type > addr2->type) return 1;
206 if (addr1->type < addr2->type) return -1;
207 if (addr1->len > addr2->len) return 1;
208 if (addr1->len < addr2->len) return -1;
209 if (addr1->len == 0) {
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 return 0;
216 }
217 return memcmp(addr1->data, addr2->data, addr1->len);
218}
219
231static inline bool
232addresses_equal(const address *addr1, const address *addr2) {
233 /*
234 * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
235 * if both addresses are zero-length, don't compare them
236 * (there's nothing to compare, so they're equal).
237 */
238 if (addr1->type == addr2->type &&
239 addr1->len == addr2->len &&
240 (addr1->len == 0 ||
241 memcmp(addr1->data, addr2->data, addr1->len) == 0))
242 return true;
243 return false;
244}
245
257static inline bool
258addresses_data_equal(const address *addr1, const address *addr2) {
259 if ( addr1->len == addr2->len
260 && memcmp(addr1->data, addr2->data, addr1->len) == 0
261 ) return true;
262 return false;
263}
264
274static inline void
275copy_address_shallow(address *to, const address *from) {
276 set_address(to, from->type, from->len, from->data);
277}
278
286static inline void
287copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
288 alloc_address_wmem(scope, to, from->type, from->len, from->data);
289}
290
296static inline void
297copy_address(address *to, const address *from) {
298 copy_address_wmem(NULL, to, from);
299}
300
306static inline void
307free_address_wmem(wmem_allocator_t *scope, address *addr) {
308 /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
309 if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
310 /* Make sure API use is correct */
311 /* if priv is not null then data == priv */
312 ws_assert(addr->data == addr->priv);
313 wmem_free(scope, addr->priv);
314 }
315 clear_address(addr);
316}
317
322static inline void
323free_address(address *addr) {
324 free_address_wmem(NULL, addr);
325}
326
333static inline unsigned
334add_address_to_hash(unsigned hash_val, const address *addr) {
335 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
336 int idx;
337
338 for (idx = 0; idx < (addr)->len; idx++) {
339 hash_val += hash_data[idx];
340 hash_val += ( hash_val << 10 );
341 hash_val ^= ( hash_val >> 6 );
342 }
343 return hash_val;
344}
345
353static inline uint64_t
354add_address_to_hash64(uint64_t hash_val, const address *addr) {
355 const uint8_t *hash_data = (const uint8_t *)(addr)->data;
356 int idx;
357
358 for (idx = 0; idx < (addr)->len; idx++) {
359 hash_val += hash_data[idx];
360 hash_val += ( hash_val << 10 );
361 hash_val ^= ( hash_val >> 6 );
362 }
363 return hash_val;
364}
365
366WS_DLL_PUBLIC unsigned address_to_bytes(const address *addr, uint8_t *buf, unsigned buf_len);
367
368/* Types of port numbers Wireshark knows about. */
369typedef enum {
370 PT_NONE, /* no port number */
371 PT_SCTP, /* SCTP */
372 PT_TCP, /* TCP */
373 PT_UDP, /* UDP */
374 PT_DCCP, /* DCCP */
375 PT_IPX, /* IPX sockets */
376 PT_DDP, /* DDP AppleTalk connection */
377 PT_IDP, /* XNS IDP sockets */
378 PT_USB, /* USB endpoint 0xffff means the host */
379 PT_I2C,
380 PT_IBQP, /* Infiniband QP number */
381 PT_BLUETOOTH,
382 PT_IWARP_MPA, /* iWarp MPA */
383 PT_MCTP
384} port_type;
385
386#ifdef __cplusplus
387}
388#endif /* __cplusplus */
389
390#endif /* __ADDRESS_H__ */
391
392/*
393 * Editor modelines - https://www.wireshark.org/tools/modelines.html
394 *
395 * Local variables:
396 * c-basic-offset: 4
397 * tab-width: 8
398 * indent-tabs-mode: nil
399 * End:
400 *
401 * vi: set shiftwidth=4 tabstop=8 expandtab:
402 * :indentSize=4:tabSize=8:noTabs=true:
403 */
const uint8_t * tvb_get_ptr(tvbuff_t *tvb, const int offset, const int length)
Definition tvbuff.c:1001
void * wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
Definition wmem_miscutl.c:19
void wmem_free(wmem_allocator_t *allocator, void *ptr)
Definition wmem_core.c:62
Definition address.h:56
Definition wmem_allocator.h:27
Definition inet_addr.h:21
Definition inet_cidr.h:22
Definition inet_cidr.h:27
Definition tvbuff-int.h:35