2 * Definitions for structures storing addresses, and for the type of
3 * variables holding port-type values
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <string.h> /* for memcmp */
30 #include "wmem/wmem.h"
34 #endif /* __cplusplus */
36 /* Types of "global" addresses Wireshark knows about. */
37 /* Address types can be added here if there are many dissectors that use them or just
38 * within a specific dissector.
39 * If an address type is added here, it must be "registered" within address_types.c
40 * For dissector address types, just use the address_type_dissector_register function
41 * from address_types.h
44 AT_NONE, /* no link-layer address */
45 AT_ETHER, /* MAC (Ethernet, 802.x, FDDI) address */
49 AT_VINES, /* Banyan Vines */
50 AT_FC, /* Fibre Channel */
51 AT_FCWWN, /* Fibre Channel WWN */
52 AT_SS7PC, /* SS7 Point Code */
53 AT_STRINGZ, /* null-terminated string */
54 AT_EUI64, /* IEEE EUI-64 */
55 AT_IB, /* Infiniband GID/LID */
56 AT_USB, /* USB Device address
57 * (0xffffffff represents the host) */
60 AT_END_OF_LIST /* Must be last in list */
63 typedef struct _address {
64 int type; /* type of address */
65 int len; /* length of address, in bytes */
66 const void *data; /* pointer to address data */
72 #define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
73 #define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
76 clear_address(address *addr)
84 /** Initialize an address with the given values.
86 * @param addr [in,out] The address to initialize.
87 * @param addr_type [in] Address type.
88 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
89 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
90 * @param addr_data [in] Pointer to the address data.
93 set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
94 addr->type = addr_type;
96 addr->data = addr_data;
100 /** Initialize an address from TVB data.
102 * Same as set_address but it takes a TVB and an offset. This is preferred
103 * over passing the return value of tvb_get_ptr() to set_address().
105 * This calls tvb_get_ptr() (including throwing any exceptions) before
106 * modifying the address.
108 * @param addr [in,out] The address to initialize.
109 * @param addr_type [in] Address type.
110 * @param tvb [in] Pointer to the TVB.
111 * @param offset [in] Offset within the TVB.
112 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
113 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
116 set_address_tvb(address *addr, int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
119 p = tvb_get_ptr(tvb, offset, addr_len);
120 set_address(addr, addr_type, addr_len, p);
123 /** Initialize an address with the given values, allocating a new buffer
124 * for the address data using wmem-scoped memory.
126 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
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(struct e_in6_addr) for AT_IPv6.
131 * @param addr_data [in] Pointer to the address data.
134 alloc_address_wmem(wmem_allocator_t *scope, address *addr,
135 int addr_type, int addr_len, const void *addr_data) {
138 addr->type = addr_type;
139 if (addr_type == AT_NONE || addr_len <= 0 || addr_data == NULL) {
140 g_assert(addr_len <= 0);
141 g_assert(addr_data == NULL);
144 addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
145 addr->len = addr_len;
148 /** Allocate an address from TVB data.
150 * Same as alloc_address_wmem but it takes a TVB and an offset.
152 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
153 * @param addr [in,out] The address to initialize.
154 * @param addr_type [in] Address type.
155 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
156 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
157 * @param tvb [in] Pointer to the TVB.
158 * @param offset [in] Offset within the TVB.
161 alloc_address_tvb(wmem_allocator_t *scope, address *addr,
162 int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
165 p = tvb_get_ptr(tvb, offset, addr_len);
166 alloc_address_wmem(scope, addr, addr_type, addr_len, p);
169 /** Compare two addresses.
171 * @param addr1 [in] The first address to compare.
172 * @param addr2 [in] The second address to compare.
173 * @return 0 if the addresses are equal,
174 * A positive number if addr1 > addr2 in some nondefined metric,
175 * A negative number if addr1 < addr2 in some nondefined metric.
178 cmp_address(const address *addr1, const address *addr2) {
179 if (addr1->type > addr2->type) return 1;
180 if (addr1->type < addr2->type) return -1;
181 if (addr1->len > addr2->len) return 1;
182 if (addr1->len < addr2->len) return -1;
183 return memcmp(addr1->data, addr2->data, addr1->len);
186 /** Check two addresses for equality.
188 * Given two addresses, return "true" if they're equal, "false" otherwise.
189 * Addresses are equal only if they have the same type; if the type is
190 * AT_NONE, they are then equal, otherwise they must have the same
191 * amount of data and the data must be the same.
193 * @param addr1 [in] The first address to compare.
194 * @param addr2 [in] The second address to compare.
195 * @return TRUE if the adresses are equal, FALSE otherwise.
197 static inline gboolean
198 addresses_equal(const address *addr1, const address *addr2) {
199 if (addr1->type == addr2->type
200 && ( addr1->type == AT_NONE
201 || ( addr1->len == addr2->len
202 && memcmp(addr1->data, addr2->data, addr1->len) == 0
209 /** Check the data of two addresses for equality.
211 * Given two addresses, return "true" if they have the same length and,
212 * their data is equal, "false" otherwise.
213 * The address types are ignored. This can be used to compare custom
214 * address types defined with address_type_dissector_register.
216 * @param addr1 [in] The first address to compare.
217 * @param addr2 [in] The second address to compare.
218 * @return TRUE if the adresses are equal, FALSE otherwise.
220 static inline gboolean
221 addresses_data_equal(const address *addr1, const address *addr2) {
222 if ( addr1->len == addr2->len
223 && memcmp(addr1->data, addr2->data, addr1->len) == 0
228 /** Perform a shallow copy of the address (both addresses point to the same
231 * @param to [in,out] The destination address.
232 * @param from [in] The source address.
234 * \warning Make sure 'from' memory stays valid for the lifetime of this object.
235 * Also it's strongly recommended to use this function instead of copy-assign.
238 copy_address_shallow(address *to, const address *from) {
239 set_address(to, from->type, from->len, from->data);
242 /** Copy an address, allocating a new buffer for the address data
243 * using wmem-scoped memory.
245 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
246 * @param to [in,out] The destination address.
247 * @param from [in] The source address.
250 copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
251 alloc_address_wmem(scope, to, from->type, from->len, from->data);
254 /** Copy an address, allocating a new buffer for the address data.
256 * @param to [in,out] The destination address.
257 * @param from [in] The source address.
260 copy_address(address *to, const address *from) {
261 copy_address_wmem(NULL, to, from);
264 /** Free an address allocated with wmem-scoped memory.
266 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
267 * @param addr [in,out] The address whose data to free.
270 free_address_wmem(wmem_allocator_t *scope, address *addr) {
271 /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
272 if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
273 /* Make sure API use is correct */
274 /* if priv is not null then data == priv */
275 g_assert(addr->data == addr->priv);
276 wmem_free(scope, addr->priv);
283 * @param addr [in,out] The address whose data to free.
286 free_address(address *addr) {
287 free_address_wmem(NULL, addr);
290 /** Hash an address into a hash value (which must already have been set).
292 * @param hash_val The existing hash value.
293 * @param addr The address to add.
294 * @return The new hash value.
297 add_address_to_hash(guint hash_val, const address *addr) {
298 const guint8 *hash_data = (const guint8 *)(addr)->data;
301 for (idx = 0; idx < (addr)->len; idx++) {
302 hash_val += hash_data[idx];
303 hash_val += ( hash_val << 10 );
304 hash_val ^= ( hash_val >> 6 );
309 /** Hash an address into a hash value (which must already have been set).
310 * 64-bit version of add_address_to_hash().
312 * @param hash_val The existing hash value.
313 * @param addr The address to add.
314 * @return The new hash value.
316 static inline guint64
317 add_address_to_hash64(guint64 hash_val, const address *addr) {
318 const guint8 *hash_data = (const guint8 *)(addr)->data;
321 for (idx = 0; idx < (addr)->len; idx++) {
322 hash_val += hash_data[idx];
323 hash_val += ( hash_val << 10 );
324 hash_val ^= ( hash_val >> 6 );
329 /* Types of port numbers Wireshark knows about. */
331 PT_NONE, /* no port number */
336 PT_IPX, /* IPX sockets */
337 PT_NCP, /* NCP connection */
338 PT_EXCHG, /* Fibre Channel exchange */
339 PT_DDP, /* DDP AppleTalk connection */
340 PT_SBCCS, /* FICON */
341 PT_IDP, /* XNS IDP sockets */
342 PT_TIPC, /* TIPC PORT */
343 PT_USB, /* USB endpoint 0xffff means the host */
345 PT_IBQP, /* Infiniband QP number */
350 /* Types of circuit IDs Wireshark knows about. */
352 CT_NONE, /* no circuit type */
353 CT_DLCI, /* Frame Relay DLCI */
354 CT_ISDN, /* ISDN channel number */
355 CT_X25, /* X.25 logical channel number */
356 CT_ISUP, /* ISDN User Part CIC */
357 CT_IAX2, /* IAX2 call id */
358 CT_H223, /* H.223 logical channel number */
359 CT_BICC, /* BICC Circuit identifier */
360 CT_DVBCI, /* DVB-CI session number|transport connection id */
361 CT_ISO14443 /* ISO14443 connection between terminal and card
362 the circuit ID is always 0, there's only one
364 /* Could also have ATM VPI/VCI pairs */
369 #endif /* __cplusplus */
371 #endif /* __ADDRESS_H__ */
374 * Editor modelines - http://www.wireshark.org/tools/modelines.html
379 * indent-tabs-mode: nil
382 * vi: set shiftwidth=4 tabstop=8 expandtab:
383 * :indentSize=4:tabSize=8:noTabs=true: