2 * Definitions for structures storing addresses, and for the type of
3 * variables holding port-type values
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <string.h> /* for memcmp */
33 #endif /* __cplusplus */
35 /* Types of addresses Wireshark knows about. */
36 /* If a new address type is added here, a string representation procedure should */
37 /* also be included in address_to_str_buf defined in to_str.c, for presentation purposes */
40 AT_NONE
, /* no link-layer address */
41 AT_ETHER
, /* MAC (Ethernet, 802.x, FDDI) address */
46 AT_ATALK
, /* Appletalk DDP */
47 AT_VINES
, /* Banyan Vines */
48 AT_OSI
, /* OSI NSAP */
49 AT_ARCNET
, /* ARCNET */
50 AT_FC
, /* Fibre Channel */
51 AT_SS7PC
, /* SS7 Point Code */
52 AT_STRINGZ
, /* null-terminated string */
53 AT_EUI64
, /* IEEE EUI-64 */
54 AT_URI
, /* URI/URL/URN */
55 AT_TIPC
, /* TIPC Address Zone,Subnetwork,Processor */
56 AT_IB
, /* Infiniband GID/LID */
57 AT_USB
, /* USB Device address
58 * (0xffffffff represents the host) */
60 AT_IEEE_802_15_4_SHORT
/* IEEE 802.15.4 16-bit short address */
61 /* (the long addresses are EUI-64's */
64 typedef struct _address
{
65 address_type type
; /* type of address */
66 int hf
; /* the specific field that this addr is */
67 int len
; /* length of address, in bytes */
68 const void *data
; /* pointer to address data */
71 /** Initialize an address with the given values.
73 * @param addr[in,out] The address to initialize.
74 * @param addr_type[in] Address type.
75 * @param addr_len[in] The length in bytes of the address data. For example, 4 for
76 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
77 * @param addr_data[in] Pointer to the address data.
80 set_address(address
*addr
, address_type addr_type
, int addr_len
, const void * addr_data
) {
81 addr
->data
= addr_data
;
82 addr
->type
= addr_type
;
86 #define SET_ADDRESS(addr, addr_type, addr_len, addr_data) \
87 set_address((addr), (addr_type), (addr_len), (addr_data))
89 /** Initialize an address from TVB data.
91 * Same as SET_ADDRESS but it takes a TVB and an offset. This is preferred
92 * over passing the return value of tvb_get_ptr() to set_address().
94 * This calls tvb_get_ptr() (including throwing any exceptions) before
95 * modifying the address.
97 * @param addr[in,out] The address to initialize.
98 * @param addr_type[in] Address type.
99 * @param tvb[in] Pointer to the TVB.
100 * @param offset[in] Offset within the TVB.
101 * @param addr_len[in] The length in bytes of the address data. For example, 4 for
102 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
104 #define TVB_SET_ADDRESS(addr, addr_type, tvb, offset, addr_len) \
106 const void *TVB_SET_ADDRESS_data = (void *)tvb_get_ptr(tvb, offset, addr_len); \
107 set_address((addr), (addr_type), (addr_len), TVB_SET_ADDRESS_data); \
110 /** Initialize an address with the given values including an associated field.
112 * @param addr[in,out] The address to initialize.
113 * @param addr_type[in] Address type.
114 * @param addr_len[in] The length in bytes of the address data. For example, 4 for
115 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
116 * @param addr_data[in] Pointer to the address data.
117 * @param addr_hf[in] The header field index to associate with the address.
120 set_address_hf(address
*addr
, address_type addr_type
, int addr_len
, const void * addr_data
, int addr_hf
) {
121 addr
->data
= addr_data
;
122 addr
->type
= addr_type
;
124 addr
->len
= addr_len
;
126 #define SET_ADDRESS_HF(addr, addr_type, addr_len, addr_data, addr_hf) \
127 set_address_hf((addr), (addr_type), (addr_len), (addr_hf))
129 /** Initialize an address from TVB data including an associated field.
131 * Same as SET_ADDRESS_HF but it takes a TVB and an offset. This is preferred
132 * over passing the return value of tvb_get_ptr() to set_address().
134 * This calls tvb_get_ptr() (including throwing any exceptions) before
135 * modifying the address.
137 * @param addr[in,out] The address to initialize.
138 * @param addr_type[in] Address type.
139 * @param tvb[in] Pointer to the TVB.
140 * @param offset[in] Offset within the TVB.
141 * @param addr_len[in] The length in bytes of the address data. For example, 4 for
142 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
143 * @param addr_hf[in] The header field index to associate with the address.
145 #define TVB_SET_ADDRESS_HF(addr, addr_type, tvb, offset, addr_len, addr_hf) \
147 const void *TVB_SET_ADDRESS_data = (void *) tvb_get_ptr(tvb, offset, addr_len); \
148 set_address_hf((addr), (addr_type), (addr_len), TVB_SET_ADDRESS_data, (addr_hf)); \
151 /** Compare two addresses.
153 * @param addr1[in] The first address to compare.
154 * @param addr2[in] The second address to compare.
155 * @return 0 if the addresses are equal,
156 * A positive number if addr1 > addr2 in some nondefined metric,
157 * A negative number if addr1 < addr2 in some nondefined metric.
160 cmp_address(const address
*addr1
, const address
*addr2
) {
161 if (addr1
->type
> addr2
->type
) return 1;
162 if (addr1
->type
< addr2
->type
) return -1;
163 if (addr1
->len
> addr2
->len
) return 1;
164 if (addr1
->len
< addr2
->len
) return -1;
165 return memcmp(addr1
->data
, addr2
->data
, addr1
->len
);
167 #define CMP_ADDRESS(addr1, addr2) cmp_address((addr1), (addr2))
169 /** Check two addresses for equality.
171 * Given two addresses, return "true" if they're equal, "false" otherwise.
172 * Addresses are equal only if they have the same type; if the type is
173 * AT_NONE, they are then equal, otherwise they must have the same
174 * amount of data and the data must be the same.
176 * @param addr1[in] The first address to compare.
177 * @param addr2[in] The second address to compare.
178 * @return TRUE if the adresses are equal, FALSE otherwise.
180 static inline gboolean
181 addresses_equal(const address
*addr1
, const address
*addr2
) {
182 if (addr1
->type
== addr2
->type
183 && ( addr1
->type
== AT_NONE
184 || ( addr1
->len
== addr2
->len
185 && memcmp(addr1
->data
, addr2
->data
, addr1
->len
) == 0
191 #define ADDRESSES_EQUAL(addr1, addr2) addresses_equal((addr1), (addr2))
193 /** Copy an address, allocating a new buffer for the address data.
195 * @param to[in,out] The destination address.
196 * @param from[in] The source address.
199 copy_address(address
*to
, const address
*from
) {
202 to
->type
= from
->type
;
205 to_data
= (guint8
*)g_malloc(from
->len
);
206 memcpy(to_data
, from
->data
, from
->len
);
209 #define COPY_ADDRESS(to, from) copy_address((to), (from))
211 /** Perform a shallow copy of the address (both addresses point to the same
214 * @param to[in,out] The destination address.
215 * @param from[in] The source address.
218 copy_address_shallow(address
*to
, const address
*from
) {
219 memcpy(to
, from
, sizeof(address
));
221 to->type = from->type;
224 to->data = from->data;
227 #define COPY_ADDRESS_SHALLOW(to, from) copy_address_shallow((to), (from))
229 /** Copy an address, allocating a new buffer for the address data
230 * using seasonal memory.
232 * @param to[in,out] The destination address.
233 * @param from[in] The source address.
235 #define SE_COPY_ADDRESS(to, from) \
237 void *SE_COPY_ADDRESS_data; \
238 copy_address_shallow((to), (from)); \
239 SE_COPY_ADDRESS_data = se_alloc((from)->len); \
240 memcpy(SE_COPY_ADDRESS_data, (from)->data, (from)->len); \
241 (to)->data = SE_COPY_ADDRESS_data; \
245 /** Hash an address into a hash value (which must already have been set).
247 * @param hash_val The existing hash value.
248 * @param addr The address to add.
249 * @return The new hash value.
252 add_address_to_hash(guint hash_val
, const address
*addr
) {
253 const guint8
*hash_data
= (const guint8
*)(addr
)->data
;
256 for (idx
= 0; idx
< (addr
)->len
; idx
++) {
257 hash_val
+= hash_data
[idx
];
258 hash_val
+= ( hash_val
<< 10 );
259 hash_val
^= ( hash_val
>> 6 );
263 #define ADD_ADDRESS_TO_HASH(hash_val, addr) do { hash_val = add_address_to_hash(hash_val, (addr)); } while (0)
265 /** Hash an address into a hash value (which must already have been set).
266 * 64-bit version of add_address_to_hash().
268 * @param hash_val The existing hash value.
269 * @param addr The address to add.
270 * @return The new hash value.
272 static inline guint64
273 add_address_to_hash64(guint64 hash_val
, const address
*addr
) {
274 const guint8
*hash_data
= (const guint8
*)(addr
)->data
;
277 for (idx
= 0; idx
< (addr
)->len
; idx
++) {
278 hash_val
+= hash_data
[idx
];
279 hash_val
+= ( hash_val
<< 10 );
280 hash_val
^= ( hash_val
>> 6 );
285 /* Types of port numbers Wireshark knows about. */
287 PT_NONE
, /* no port number */
292 PT_IPX
, /* IPX sockets */
293 PT_NCP
, /* NCP connection */
294 PT_EXCHG
, /* Fibre Channel exchange */
295 PT_DDP
, /* DDP AppleTalk connection */
296 PT_SBCCS
, /* FICON */
297 PT_IDP
, /* XNS IDP sockets */
298 PT_TIPC
, /* TIPC PORT */
299 PT_USB
, /* USB endpoint 0xffff means the host */
301 PT_IBQP
, /* Infiniband QP number */
305 /* Types of circuit IDs Wireshark knows about. */
307 CT_NONE
, /* no circuit type */
308 CT_DLCI
, /* Frame Relay DLCI */
309 CT_ISDN
, /* ISDN channel number */
310 CT_X25
, /* X.25 logical channel number */
311 CT_ISUP
, /* ISDN User Part CIC */
312 CT_IAX2
, /* IAX2 call id */
313 CT_H223
, /* H.223 logical channel number */
314 CT_BICC
, /* BICC Circuit identifier */
315 CT_DVBCI
/* DVB-CI session number|transport connection id */
316 /* Could also have ATM VPI/VCI pairs */
321 #endif /* __cplusplus */
323 #endif /* __ADDRESS_H__ */
326 * Editor modelines - http://www.wireshark.org/tools/modelines.html
331 * indent-tabs-mode: nil
334 * vi: set shiftwidth=4 tabstop=8 expandtab:
335 * :indentSize=4:tabSize=8:noTabs=true: