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 */
63 AT_DEVICENET /* DeviceNet */
66 typedef struct _address {
67 address_type type; /* type of address */
68 int hf; /* the specific field that this addr is */
69 int len; /* length of address, in bytes */
70 const void *data; /* pointer to address data */
73 /** Initialize an address with the given values.
75 * @param addr [in,out] The address to initialize.
76 * @param addr_type [in] Address type.
77 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
78 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
79 * @param addr_data [in] Pointer to the address data.
82 set_address(address *addr, address_type addr_type, int addr_len, const void * addr_data) {
83 addr->data = addr_data;
84 addr->type = addr_type;
88 #define SET_ADDRESS(addr, addr_type, addr_len, addr_data) \
89 set_address((addr), (addr_type), (addr_len), (addr_data))
91 /** Initialize an address from TVB data.
93 * Same as SET_ADDRESS but it takes a TVB and an offset. This is preferred
94 * over passing the return value of tvb_get_ptr() to set_address().
96 * This calls tvb_get_ptr() (including throwing any exceptions) before
97 * modifying the address.
99 * @param addr [in,out] The address to initialize.
100 * @param addr_type [in] Address type.
101 * @param tvb [in] Pointer to the TVB.
102 * @param offset [in] Offset within the TVB.
103 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
104 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
106 #define TVB_SET_ADDRESS(addr, addr_type, tvb, offset, addr_len) \
108 const void *TVB_SET_ADDRESS_data = (const void *)tvb_get_ptr(tvb, offset, addr_len); \
109 set_address((addr), (addr_type), (addr_len), TVB_SET_ADDRESS_data); \
112 /** Initialize an address with the given values including an associated field.
114 * @param addr [in,out] The address to initialize.
115 * @param addr_type [in] Address type.
116 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
117 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
118 * @param addr_data [in] Pointer to the address data.
119 * @param addr_hf [in] The header field index to associate with the address.
122 set_address_hf(address *addr, address_type addr_type, int addr_len, const void * addr_data, int addr_hf) {
123 addr->data = addr_data;
124 addr->type = addr_type;
126 addr->len = addr_len;
128 #define SET_ADDRESS_HF(addr, addr_type, addr_len, addr_data, addr_hf) \
129 set_address_hf((addr), (addr_type), (addr_len), (addr_hf))
131 /** Initialize an address from TVB data including an associated field.
133 * Same as SET_ADDRESS_HF but it takes a TVB and an offset. This is preferred
134 * over passing the return value of tvb_get_ptr() to set_address().
136 * This calls tvb_get_ptr() (including throwing any exceptions) before
137 * modifying the address.
139 * @param addr [in,out] The address to initialize.
140 * @param addr_type [in] Address type.
141 * @param tvb [in] Pointer to the TVB.
142 * @param offset [in] Offset within the TVB.
143 * @param addr_len [in] The length in bytes of the address data. For example, 4 for
144 * AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
145 * @param addr_hf [in] The header field index to associate with the address.
147 #define TVB_SET_ADDRESS_HF(addr, addr_type, tvb, offset, addr_len, addr_hf) \
149 const void *TVB_SET_ADDRESS_data = (const void *) tvb_get_ptr(tvb, offset, addr_len); \
150 set_address_hf((addr), (addr_type), (addr_len), TVB_SET_ADDRESS_data, (addr_hf)); \
153 /** Compare two addresses.
155 * @param addr1 [in] The first address to compare.
156 * @param addr2 [in] The second address to compare.
157 * @return 0 if the addresses are equal,
158 * A positive number if addr1 > addr2 in some nondefined metric,
159 * A negative number if addr1 < addr2 in some nondefined metric.
162 cmp_address(const address *addr1, const address *addr2) {
163 if (addr1->type > addr2->type) return 1;
164 if (addr1->type < addr2->type) return -1;
165 if (addr1->len > addr2->len) return 1;
166 if (addr1->len < addr2->len) return -1;
167 return memcmp(addr1->data, addr2->data, addr1->len);
169 #define CMP_ADDRESS(addr1, addr2) cmp_address((addr1), (addr2))
171 /** Check two addresses for equality.
173 * Given two addresses, return "true" if they're equal, "false" otherwise.
174 * Addresses are equal only if they have the same type; if the type is
175 * AT_NONE, they are then equal, otherwise they must have the same
176 * amount of data and the data must be the same.
178 * @param addr1 [in] The first address to compare.
179 * @param addr2 [in] The second address to compare.
180 * @return TRUE if the adresses are equal, FALSE otherwise.
182 static inline gboolean
183 addresses_equal(const address *addr1, const address *addr2) {
184 if (addr1->type == addr2->type
185 && ( addr1->type == AT_NONE
186 || ( addr1->len == addr2->len
187 && memcmp(addr1->data, addr2->data, addr1->len) == 0
193 #define ADDRESSES_EQUAL(addr1, addr2) addresses_equal((addr1), (addr2))
195 /** Copy an address, allocating a new buffer for the address data.
197 * @param to [in,out] The destination address.
198 * @param from [in] The source address.
201 copy_address(address *to, const address *from) {
204 to->type = from->type;
207 to_data = (guint8 *)g_malloc(from->len);
208 memcpy(to_data, from->data, from->len);
211 #define COPY_ADDRESS(to, from) copy_address((to), (from))
213 /** Perform a shallow copy of the address (both addresses point to the same
216 * @param to [in,out] The destination address.
217 * @param from [in] The source address.
220 copy_address_shallow(address *to, const address *from) {
221 memcpy(to, from, sizeof(address));
223 to->type = from->type;
226 to->data = from->data;
229 #define COPY_ADDRESS_SHALLOW(to, from) copy_address_shallow((to), (from))
231 /** Copy an address, allocating a new buffer for the address data
232 * using seasonal memory.
234 * @param to [in,out] The destination address.
235 * @param from [in] The source address.
237 #define SE_COPY_ADDRESS(to, from) \
239 void *SE_COPY_ADDRESS_data; \
240 copy_address_shallow((to), (from)); \
241 SE_COPY_ADDRESS_data = se_alloc((from)->len); \
242 memcpy(SE_COPY_ADDRESS_data, (from)->data, (from)->len); \
243 (to)->data = SE_COPY_ADDRESS_data; \
247 /** Hash an address into a hash value (which must already have been set).
249 * @param hash_val The existing hash value.
250 * @param addr The address to add.
251 * @return The new hash value.
254 add_address_to_hash(guint hash_val, const address *addr) {
255 const guint8 *hash_data = (const guint8 *)(addr)->data;
258 for (idx = 0; idx < (addr)->len; idx++) {
259 hash_val += hash_data[idx];
260 hash_val += ( hash_val << 10 );
261 hash_val ^= ( hash_val >> 6 );
265 #define ADD_ADDRESS_TO_HASH(hash_val, addr) do { hash_val = add_address_to_hash(hash_val, (addr)); } while (0)
267 /** Hash an address into a hash value (which must already have been set).
268 * 64-bit version of add_address_to_hash().
270 * @param hash_val The existing hash value.
271 * @param addr The address to add.
272 * @return The new hash value.
274 static inline guint64
275 add_address_to_hash64(guint64 hash_val, const address *addr) {
276 const guint8 *hash_data = (const guint8 *)(addr)->data;
279 for (idx = 0; idx < (addr)->len; idx++) {
280 hash_val += hash_data[idx];
281 hash_val += ( hash_val << 10 );
282 hash_val ^= ( hash_val >> 6 );
287 /* Types of port numbers Wireshark knows about. */
289 PT_NONE, /* no port number */
294 PT_IPX, /* IPX sockets */
295 PT_NCP, /* NCP connection */
296 PT_EXCHG, /* Fibre Channel exchange */
297 PT_DDP, /* DDP AppleTalk connection */
298 PT_SBCCS, /* FICON */
299 PT_IDP, /* XNS IDP sockets */
300 PT_TIPC, /* TIPC PORT */
301 PT_USB, /* USB endpoint 0xffff means the host */
303 PT_IBQP, /* Infiniband QP number */
307 /* Types of circuit IDs Wireshark knows about. */
309 CT_NONE, /* no circuit type */
310 CT_DLCI, /* Frame Relay DLCI */
311 CT_ISDN, /* ISDN channel number */
312 CT_X25, /* X.25 logical channel number */
313 CT_ISUP, /* ISDN User Part CIC */
314 CT_IAX2, /* IAX2 call id */
315 CT_H223, /* H.223 logical channel number */
316 CT_BICC, /* BICC Circuit identifier */
317 CT_DVBCI /* DVB-CI session number|transport connection id */
318 /* Could also have ATM VPI/VCI pairs */
323 #endif /* __cplusplus */
325 #endif /* __ADDRESS_H__ */
328 * Editor modelines - http://www.wireshark.org/tools/modelines.html
333 * indent-tabs-mode: nil
336 * vi: set shiftwidth=4 tabstop=8 expandtab:
337 * :indentSize=4:tabSize=8:noTabs=true: