1 /* address.h
2  * Definitions for structures storing addresses, and for the type of
3  * variables holding port-type values
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
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 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24 
25 /* Types of "global" addresses Wireshark knows about. */
26 /* Address types can be added here if there are many dissectors that use them or just
27  * within a specific dissector.
28  * If an address type is added here, it must be "registered" within address_types.c
29  * For dissector address types, just use the address_type_dissector_register function
30  * from address_types.h
31  */
32 typedef enum {
33     AT_NONE,               /* no link-layer address */
34     AT_ETHER,              /* MAC (Ethernet, 802.x, FDDI) address */
35     AT_IPv4,               /* IPv4 */
36     AT_IPv6,               /* IPv6 */
37     AT_IPX,                /* IPX */
38     AT_FC,                 /* Fibre Channel */
39     AT_FCWWN,              /* Fibre Channel WWN */
40     AT_STRINGZ,            /* null-terminated string */
41     AT_EUI64,              /* IEEE EUI-64 */
42     AT_IB,                 /* Infiniband GID/LID */
43     AT_AX25,               /* AX.25 */
44     AT_VINES,              /* Banyan Vines address */
45 
46     AT_END_OF_LIST         /* Must be last in list */
47 } address_type;
48 
49 typedef struct _address {
50     int           type;         /* type of address */
51     int           len;          /* length of address, in bytes */
52     const void   *data;         /* pointer to address data */
53 
54     /* private */
55     void         *priv;
56 } address;
57 
58 #define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
59 #define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
60 
61 static inline void
clear_address(address * addr)62 clear_address(address *addr)
63 {
64     addr->type = AT_NONE;
65     addr->len  = 0;
66     addr->data = NULL;
67     addr->priv = NULL;
68 }
69 
70 /** Initialize an address with the given values.
71  *
72  * @param addr [in,out] The address to initialize.
73  * @param addr_type [in] Address type.
74  * @param addr_len [in] The length in bytes of the address data. For example, 4 for
75  *                     AT_IPv4 or sizeof(ws_in6_addr) for AT_IPv6.
76  * @param addr_data [in] Pointer to the address data.
77  */
78 static inline void
set_address(address * addr,int addr_type,int addr_len,const void * addr_data)79 set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
80     if (addr_len == 0) {
81         /* Zero length must mean no data */
82         ws_assert(addr_data == NULL);
83     } else {
84         /* Must not be AT_NONE - AT_NONE must have no data */
85         ws_assert(addr_type != AT_NONE);
86         /* Make sure we *do* have data */
87         ws_assert(addr_data != NULL);
88     }
89     addr->type = addr_type;
90     addr->len  = addr_len;
91     addr->data = addr_data;
92     addr->priv = NULL;
93 }
94 
95 /** Initialize an address from TVB data.
96  *
97  * Same as set_address but it takes a TVB and an offset. This is preferred
98  * over passing the return value of tvb_get_ptr() to set_address().
99  *
100  * This calls tvb_get_ptr() (including throwing any exceptions) before
101  * modifying the address.
102  *
103  * @param addr [in,out] The address to initialize.
104  * @param addr_type [in] Address type.
105  * @param tvb [in] Pointer to the TVB.
106  * @param offset [in] Offset within the TVB.
107  * @param addr_len [in] The length in bytes of the address data. For example, 4 for
108  *                     AT_IPv4 or sizeof(ws_in6_addr) for AT_IPv6.
109  */
110 static inline void
set_address_tvb(address * addr,int addr_type,int addr_len,tvbuff_t * tvb,int offset)111 set_address_tvb(address *addr, int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
112     const void *p;
113 
114     if (addr_len != 0) {
115         /* Must not be AT_NONE - AT_NONE must have no data */
116         ws_assert(addr_type != AT_NONE);
117         p = tvb_get_ptr(tvb, offset, addr_len);
118     } else
119         p = NULL;
120     set_address(addr, addr_type, addr_len, p);
121 }
122 
123 /** Initialize an address with the given values, allocating a new buffer
124  * for the address data using wmem-scoped memory.
125  *
126  * @param scope [in] The lifetime of the allocated memory, e.g., pinfo->pool
127  * @param addr [in,out] The address to initialize.
128  * @param addr_type [in] Address type.
129  * @param addr_len [in] The length in bytes of the address data. For example, 4 for
130  *                     AT_IPv4 or sizeof(ws_in6_addr) for AT_IPv6.
131  * @param addr_data [in] Pointer to the address data.
132  */
133 static inline void
alloc_address_wmem(wmem_allocator_t * scope,address * addr,int addr_type,int addr_len,const void * addr_data)134 alloc_address_wmem(wmem_allocator_t *scope, address *addr,
135                         int addr_type, int addr_len, const void *addr_data) {
136     ws_assert(addr);
137     clear_address(addr);
138     addr->type = addr_type;
139     if (addr_len == 0) {
140         /* Zero length must mean no data */
141         ws_assert(addr_data == NULL);
142         /* Nothing to copy */
143         return;
144     }
145     /* Must not be AT_NONE - AT_NONE must have no data */
146     ws_assert(addr_type != AT_NONE);
147     /* Make sure we *do* have data to copy */
148     ws_assert(addr_data != NULL);
149     addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
150     addr->len = addr_len;
151 }
152 
153 /** Allocate an address from TVB data.
154  *
155  * Same as alloc_address_wmem but it takes a TVB and an offset.
156  *
157  * @param scope [in] The lifetime of the allocated memory, e.g., pinfo->pool
158  * @param addr [in,out] The address to initialize.
159  * @param addr_type [in] Address type.
160  * @param addr_len [in] The length in bytes of the address data. For example, 4 for
161  *                     AT_IPv4 or sizeof(ws_in6_addr) for AT_IPv6.
162  * @param tvb [in] Pointer to the TVB.
163  * @param offset [in] Offset within the TVB.
164  */
165 static inline void
alloc_address_tvb(wmem_allocator_t * scope,address * addr,int addr_type,int addr_len,tvbuff_t * tvb,int offset)166 alloc_address_tvb(wmem_allocator_t *scope, address *addr,
167                     int addr_type, int addr_len,  tvbuff_t *tvb, int offset) {
168     const void *p;
169 
170     p = tvb_get_ptr(tvb, offset, addr_len);
171     alloc_address_wmem(scope, addr, addr_type, addr_len, p);
172 }
173 
174 /** Compare two addresses.
175  *
176  * @param addr1 [in] The first address to compare.
177  * @param addr2 [in] The second address to compare.
178  * @return 0 if the addresses are equal,
179  *  A positive number if addr1 > addr2 in some nondefined metric,
180  *  A negative number if addr1 < addr2 in some nondefined metric.
181  */
182 static inline int
cmp_address(const address * addr1,const address * addr2)183 cmp_address(const address *addr1, const address *addr2) {
184     if (addr1->type > addr2->type) return 1;
185     if (addr1->type < addr2->type) return -1;
186     if (addr1->len  > addr2->len) return 1;
187     if (addr1->len  < addr2->len) return -1;
188     if (addr1->len == 0) {
189         /*
190          * memcmp(NULL, NULL, 0) is *not* guaranteed to work, so
191          * if both addresses are zero-length, don't compare them
192          * (there's nothing to compare, so they're equal).
193          */
194         return 0;
195     }
196     return memcmp(addr1->data, addr2->data, addr1->len);
197 }
198 
199 /** Check two addresses for equality.
200  *
201  * Given two addresses, return "true" if they're equal, "false" otherwise.
202  * Addresses are equal only if they have the same type and length; if the
203  * length is zero, they are then equal, otherwise the data must be the
204  * same.
205  *
206  * @param addr1 [in] The first address to compare.
207  * @param addr2 [in] The second address to compare.
208  * @return TRUE if the addresses are equal, FALSE otherwise.
209  */
210 static inline gboolean
addresses_equal(const address * addr1,const address * addr2)211 addresses_equal(const address *addr1, const address *addr2) {
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     if (addr1->type == addr2->type &&
218         addr1->len == addr2->len &&
219         (addr1->len == 0 ||
220          memcmp(addr1->data, addr2->data, addr1->len) == 0))
221         return TRUE;
222     return FALSE;
223 }
224 
225 /** Check the data of two addresses for equality.
226  *
227  * Given two addresses, return "true" if they have the same length and,
228  * their data is equal, "false" otherwise.
229  * The address types are ignored. This can be used to compare custom
230  * address types defined with address_type_dissector_register.
231  *
232  * @param addr1 [in] The first address to compare.
233  * @param addr2 [in] The second address to compare.
234  * @return TRUE if the addresses are equal, FALSE otherwise.
235  */
236 static inline gboolean
addresses_data_equal(const address * addr1,const address * addr2)237 addresses_data_equal(const address *addr1, const address *addr2) {
238     if ( addr1->len == addr2->len
239             && memcmp(addr1->data, addr2->data, addr1->len) == 0
240             ) return TRUE;
241     return FALSE;
242 }
243 
244 /** Perform a shallow copy of the address (both addresses point to the same
245  * memory location).
246  *
247  * @param to [in,out] The destination address.
248  * @param from [in] The source address.
249  *
250  * \warning Make sure 'from' memory stays valid for the lifetime of this object.
251  * Also it's strongly recommended to use this function instead of copy-assign.
252  */
253 static inline void
copy_address_shallow(address * to,const address * from)254 copy_address_shallow(address *to, const address *from) {
255     set_address(to, from->type, from->len, from->data);
256 }
257 
258 /** Copy an address, allocating a new buffer for the address data
259  *  using wmem-scoped memory.
260  *
261  * @param scope [in] The lifetime of the allocated memory, e.g., pinfo->pool
262  * @param to [in,out] The destination address.
263  * @param from [in] The source address.
264  */
265 static inline void
copy_address_wmem(wmem_allocator_t * scope,address * to,const address * from)266 copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
267     alloc_address_wmem(scope, to, from->type, from->len, from->data);
268 }
269 
270 /** Copy an address, allocating a new buffer for the address data.
271  *
272  * @param to [in,out] The destination address.
273  * @param from [in] The source address.
274  */
275 static inline void
copy_address(address * to,const address * from)276 copy_address(address *to, const address *from) {
277     copy_address_wmem(NULL, to, from);
278 }
279 
280 /** Free an address allocated with wmem-scoped memory.
281  *
282  * @param scope [in] The lifetime of the allocated memory, e.g., pinfo->pool
283  * @param addr [in,out] The address whose data to free.
284  */
285 static inline void
free_address_wmem(wmem_allocator_t * scope,address * addr)286 free_address_wmem(wmem_allocator_t *scope, address *addr) {
287     /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
288     if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
289         /* Make sure API use is correct */
290         /* if priv is not null then data == priv */
291         ws_assert(addr->data == addr->priv);
292         wmem_free(scope, addr->priv);
293     }
294     clear_address(addr);
295 }
296 
297 /** Free an address.
298  *
299  * @param addr [in,out] The address whose data to free.
300  */
301 static inline void
free_address(address * addr)302 free_address(address *addr) {
303     free_address_wmem(NULL, addr);
304 }
305 
306 /** Hash an address into a hash value (which must already have been set).
307  *
308  * @param hash_val The existing hash value.
309  * @param addr The address to add.
310  * @return The new hash value.
311  */
312 static inline guint
add_address_to_hash(guint hash_val,const address * addr)313 add_address_to_hash(guint hash_val, const address *addr) {
314     const guint8 *hash_data = (const guint8 *)(addr)->data;
315     int idx;
316 
317     for (idx = 0; idx < (addr)->len; idx++) {
318         hash_val += hash_data[idx];
319         hash_val += ( hash_val << 10 );
320         hash_val ^= ( hash_val >> 6 );
321     }
322     return hash_val;
323 }
324 
325 /** Hash an address into a hash value (which must already have been set).
326  *  64-bit version of add_address_to_hash().
327  *
328  * @param hash_val The existing hash value.
329  * @param addr The address to add.
330  * @return The new hash value.
331  */
332 static inline guint64
add_address_to_hash64(guint64 hash_val,const address * addr)333 add_address_to_hash64(guint64 hash_val, const address *addr) {
334     const guint8 *hash_data = (const guint8 *)(addr)->data;
335     int idx;
336 
337     for (idx = 0; idx < (addr)->len; idx++) {
338         hash_val += hash_data[idx];
339         hash_val += ( hash_val << 10 );
340         hash_val ^= ( hash_val >> 6 );
341     }
342     return hash_val;
343 }
344 
345 WS_DLL_PUBLIC guint address_to_bytes(const address *addr, guint8 *buf, guint buf_len);
346 
347 /* Types of port numbers Wireshark knows about. */
348 typedef enum {
349     PT_NONE,            /* no port number */
350     PT_SCTP,            /* SCTP */
351     PT_TCP,             /* TCP */
352     PT_UDP,             /* UDP */
353     PT_DCCP,            /* DCCP */
354     PT_IPX,             /* IPX sockets */
355     PT_DDP,             /* DDP AppleTalk connection */
356     PT_IDP,             /* XNS IDP sockets */
357     PT_USB,             /* USB endpoint 0xffff means the host */
358     PT_I2C,
359     PT_IBQP,            /* Infiniband QP number */
360     PT_BLUETOOTH,
361     PT_IWARP_MPA        /* iWarp MPA */
362 } port_type;
363 
364 #ifdef __cplusplus
365 }
366 #endif /* __cplusplus */
367 
368 #endif /* __ADDRESS_H__ */
369 
370 /*
371  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
372  *
373  * Local variables:
374  * c-basic-offset: 4
375  * tab-width: 8
376  * indent-tabs-mode: nil
377  * End:
378  *
379  * vi: set shiftwidth=4 tabstop=8 expandtab:
380  * :indentSize=4:tabSize=8:noTabs=true:
381  */
382