FIXUP: WIP: verification_trailer
[wireshark-wip.git] / epan / address.h
blob5fdbc7eaf7aaa6b6003fb93e3ba74e81b2e67369
1 /* address.h
2 * Definitions for structures storing addresses, and for the type of
3 * variables holding port-type values
5 * $Id$
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.
26 #ifndef __ADDRESS_H__
27 #define __ADDRESS_H__
29 #include <string.h> /* for memcmp */
31 #ifdef __cplusplus
32 extern "C" {
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 */
39 typedef enum {
40 AT_NONE, /* no link-layer address */
41 AT_ETHER, /* MAC (Ethernet, 802.x, FDDI) address */
42 AT_IPv4, /* IPv4 */
43 AT_IPv6, /* IPv6 */
44 AT_IPX, /* IPX */
45 AT_SNA, /* SNA */
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) */
59 AT_AX25, /* AX.25 */
60 AT_IEEE_802_15_4_SHORT /* IEEE 802.15.4 16-bit short address */
61 /* (the long addresses are EUI-64's */
62 } address_type;
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 */
69 } address;
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.
79 static inline void
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;
83 addr->hf = -1;
84 addr->len = addr_len;
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) \
105 do { \
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); \
108 } while (0)
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.
119 static inline void
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;
123 addr->hf = addr_hf;
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) \
146 do { \
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)); \
149 } while (0)
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.
159 static inline int
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
188 ) return TRUE;
189 return FALSE;
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.
198 static inline void
199 copy_address(address *to, const address *from) {
200 guint8 *to_data;
202 to->type = from->type;
203 to->len = from->len;
204 to->hf = from->hf;
205 to_data = (guint8 *)g_malloc(from->len);
206 memcpy(to_data, from->data, from->len);
207 to->data = to_data;
209 #define COPY_ADDRESS(to, from) copy_address((to), (from))
211 /** Perform a shallow copy of the address (both addresses point to the same
212 * memory location).
214 * @param to[in,out] The destination address.
215 * @param from[in] The source address.
217 static inline void
218 copy_address_shallow(address *to, const address *from) {
219 memcpy(to, from, sizeof(address));
221 to->type = from->type;
222 to->len = from->len;
223 to->hf = from->hf;
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) \
236 do { \
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; \
242 } while (0)
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.
251 static inline guint
252 add_address_to_hash(guint hash_val, const address *addr) {
253 const guint8 *hash_data = (const guint8 *)(addr)->data;
254 int idx;
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 );
261 return hash_val;
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;
275 int idx;
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 );
282 return hash_val;
285 /* Types of port numbers Wireshark knows about. */
286 typedef enum {
287 PT_NONE, /* no port number */
288 PT_SCTP, /* SCTP */
289 PT_TCP, /* TCP */
290 PT_UDP, /* UDP */
291 PT_DCCP, /* DCCP */
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 */
300 PT_I2C,
301 PT_IBQP, /* Infiniband QP number */
302 PT_BLUETOOTH
303 } port_type;
305 /* Types of circuit IDs Wireshark knows about. */
306 typedef enum {
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 */
317 } circuit_type;
319 #ifdef __cplusplus
321 #endif /* __cplusplus */
323 #endif /* __ADDRESS_H__ */
326 * Editor modelines - http://www.wireshark.org/tools/modelines.html
328 * Local variables:
329 * c-basic-offset: 4
330 * tab-width: 8
331 * indent-tabs-mode: nil
332 * End:
334 * vi: set shiftwidth=4 tabstop=8 expandtab:
335 * :indentSize=4:tabSize=8:noTabs=true: