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) {
120 p = tvb_get_ptr(tvb, offset, addr_len);
123 set_address(addr, addr_type, addr_len, p);
126 /** Initialize an address with the given values, allocating a new buffer
127 * for the address data using wmem-scoped memory.
129 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
130 * @param addr [in,out] The address to initialize.
131 * @param addr_type [in] Address type.
132 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
133 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
134 * @param addr_data [in] Pointer to the address data.
137 alloc_address_wmem(wmem_allocator_t *scope, address *addr,
138 int addr_type, int addr_len, const void *addr_data) {
141 addr->type = addr_type;
142 if (addr_type == AT_NONE || addr_len <= 0 || addr_data == NULL) {
143 g_assert(addr_len <= 0);
144 g_assert(addr_data == NULL);
147 addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
148 addr->len = addr_len;
151 /** Allocate an address from TVB data.
153 * Same as alloc_address_wmem but it takes a TVB and an offset.
155 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
156 * @param addr [in,out] The address to initialize.
157 * @param addr_type [in] Address type.
158 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
159 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
160 * @param tvb [in] Pointer to the TVB.
161 * @param offset [in] Offset within the TVB.
164 alloc_address_tvb(wmem_allocator_t *scope, address *addr,
165 int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
168 p = tvb_get_ptr(tvb, offset, addr_len);
169 alloc_address_wmem(scope, addr, addr_type, addr_len, p);
172 /** Compare two addresses.
174 * @param addr1 [in] The first address to compare.
175 * @param addr2 [in] The second address to compare.
176 * @return 0 if the addresses are equal,
177 * A positive number if addr1 > addr2 in some nondefined metric,
178 * A negative number if addr1 < addr2 in some nondefined metric.
181 cmp_address(const address *addr1, const address *addr2) {
182 if (addr1->type > addr2->type) return 1;
183 if (addr1->type < addr2->type) return -1;
184 if (addr1->len > addr2->len) return 1;
185 if (addr1->len < addr2->len) return -1;
186 return memcmp(addr1->data, addr2->data, addr1->len);
189 /** Check two addresses for equality.
191 * Given two addresses, return "true" if they're equal, "false" otherwise.
192 * Addresses are equal only if they have the same type; if the type is
193 * AT_NONE, they are then equal, otherwise they must have the same
194 * amount of data and the data must be the same.
196 * @param addr1 [in] The first address to compare.
197 * @param addr2 [in] The second address to compare.
198 * @return TRUE if the adresses are equal, FALSE otherwise.
200 static inline gboolean
201 addresses_equal(const address *addr1, const address *addr2) {
202 if (addr1->type == addr2->type
203 && ( addr1->type == AT_NONE
204 || ( addr1->len == addr2->len
205 && memcmp(addr1->data, addr2->data, addr1->len) == 0
212 /** Check the data of two addresses for equality.
214 * Given two addresses, return "true" if they have the same length and,
215 * their data is equal, "false" otherwise.
216 * The address types are ignored. This can be used to compare custom
217 * address types defined with address_type_dissector_register.
219 * @param addr1 [in] The first address to compare.
220 * @param addr2 [in] The second address to compare.
221 * @return TRUE if the adresses are equal, FALSE otherwise.
223 static inline gboolean
224 addresses_data_equal(const address *addr1, const address *addr2) {
225 if ( addr1->len == addr2->len
226 && memcmp(addr1->data, addr2->data, addr1->len) == 0
231 /** Perform a shallow copy of the address (both addresses point to the same
234 * @param to [in,out] The destination address.
235 * @param from [in] The source address.
237 * \warning Make sure 'from' memory stays valid for the lifetime of this object.
238 * Also it's strongly recommended to use this function instead of copy-assign.
241 copy_address_shallow(address *to, const address *from) {
242 set_address(to, from->type, from->len, from->data);
245 /** Copy an address, allocating a new buffer for the address data
246 * using wmem-scoped memory.
248 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
249 * @param to [in,out] The destination address.
250 * @param from [in] The source address.
253 copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
254 alloc_address_wmem(scope, to, from->type, from->len, from->data);
257 /** Copy an address, allocating a new buffer for the address data.
259 * @param to [in,out] The destination address.
260 * @param from [in] The source address.
263 copy_address(address *to, const address *from) {
264 copy_address_wmem(NULL, to, from);
267 /** Free an address allocated with wmem-scoped memory.
269 * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
270 * @param addr [in,out] The address whose data to free.
273 free_address_wmem(wmem_allocator_t *scope, address *addr) {
274 /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
275 if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
276 /* Make sure API use is correct */
277 /* if priv is not null then data == priv */
278 g_assert(addr->data == addr->priv);
279 wmem_free(scope, addr->priv);
286 * @param addr [in,out] The address whose data to free.
289 free_address(address *addr) {
290 free_address_wmem(NULL, addr);
293 /** Hash an address into a hash value (which must already have been set).
295 * @param hash_val The existing hash value.
296 * @param addr The address to add.
297 * @return The new hash value.
300 add_address_to_hash(guint hash_val, const address *addr) {
301 const guint8 *hash_data = (const guint8 *)(addr)->data;
304 for (idx = 0; idx < (addr)->len; idx++) {
305 hash_val += hash_data[idx];
306 hash_val += ( hash_val << 10 );
307 hash_val ^= ( hash_val >> 6 );
312 /** Hash an address into a hash value (which must already have been set).
313 * 64-bit version of add_address_to_hash().
315 * @param hash_val The existing hash value.
316 * @param addr The address to add.
317 * @return The new hash value.
319 static inline guint64
320 add_address_to_hash64(guint64 hash_val, const address *addr) {
321 const guint8 *hash_data = (const guint8 *)(addr)->data;
324 for (idx = 0; idx < (addr)->len; idx++) {
325 hash_val += hash_data[idx];
326 hash_val += ( hash_val << 10 );
327 hash_val ^= ( hash_val >> 6 );
332 /* Types of port numbers Wireshark knows about. */
334 PT_NONE, /* no port number */
339 PT_IPX, /* IPX sockets */
340 PT_NCP, /* NCP connection */
341 PT_EXCHG, /* Fibre Channel exchange */
342 PT_DDP, /* DDP AppleTalk connection */
343 PT_SBCCS, /* FICON */
344 PT_IDP, /* XNS IDP sockets */
345 PT_TIPC, /* TIPC PORT */
346 PT_USB, /* USB endpoint 0xffff means the host */
348 PT_IBQP, /* Infiniband QP number */
353 /* Types of circuit IDs Wireshark knows about. */
355 CT_NONE, /* no circuit type */
356 CT_DLCI, /* Frame Relay DLCI */
357 CT_ISDN, /* ISDN channel number */
358 CT_X25, /* X.25 logical channel number */
359 CT_ISUP, /* ISDN User Part CIC */
360 CT_IAX2, /* IAX2 call id */
361 CT_H223, /* H.223 logical channel number */
362 CT_BICC, /* BICC Circuit identifier */
363 CT_DVBCI, /* DVB-CI session number|transport connection id */
364 CT_ISO14443 /* ISO14443 connection between terminal and card
365 the circuit ID is always 0, there's only one
367 /* Could also have ATM VPI/VCI pairs */
372 #endif /* __cplusplus */
374 #endif /* __ADDRESS_H__ */
377 * Editor modelines - http://www.wireshark.org/tools/modelines.html
382 * indent-tabs-mode: nil
385 * vi: set shiftwidth=4 tabstop=8 expandtab:
386 * :indentSize=4:tabSize=8:noTabs=true: