Witness: enum witness_interface_state
[metze/wireshark/wip.git] / epan / address.h
1 /* address.h
2  * Definitions for structures storing addresses, and for the type of
3  * variables holding port-type values
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #ifndef __ADDRESS_H__
27 #define __ADDRESS_H__
28
29 #include <string.h>     /* for memcmp */
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34
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 */
38
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     AT_J1939,              /* J1939 */
63     AT_DEVICENET           /* DeviceNet */
64 } address_type;
65
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 */
71 } address;
72
73 /** Initialize an address with the given values.
74  *
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.
80  */
81 static inline void
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;
85     addr->hf   = -1;
86     addr->len  = addr_len;
87 }
88 #define SET_ADDRESS(addr, addr_type, addr_len, addr_data) \
89     set_address((addr), (addr_type), (addr_len), (addr_data))
90
91 /** Initialize an address from TVB data.
92  *
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().
95  *
96  * This calls tvb_get_ptr() (including throwing any exceptions) before
97  * modifying the address.
98  *
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.
105  */
106 #define TVB_SET_ADDRESS(addr, addr_type, tvb, offset, addr_len) \
107     do {                            \
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); \
110     } while (0)
111
112 /** Initialize an address with the given values including an associated field.
113  *
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.
120  */
121 static inline void
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;
125     addr->hf   = addr_hf;
126     addr->len  = addr_len;
127 }
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))
130
131 /** Initialize an address from TVB data including an associated field.
132  *
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().
135  *
136  * This calls tvb_get_ptr() (including throwing any exceptions) before
137  * modifying the address.
138  *
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.
146  */
147 #define TVB_SET_ADDRESS_HF(addr, addr_type, tvb, offset, addr_len, addr_hf) \
148     do {                            \
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)); \
151     } while (0)
152
153 /** Compare two addresses.
154  *
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.
160  */
161 static inline int
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);
168 }
169 #define CMP_ADDRESS(addr1, addr2) cmp_address((addr1), (addr2))
170
171 /** Check two addresses for equality.
172  *
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.
177  *
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.
181  */
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
188                       )
189                  )
190             ) return TRUE;
191     return FALSE;
192 }
193 #define ADDRESSES_EQUAL(addr1, addr2) addresses_equal((addr1), (addr2))
194
195 /** Copy an address, allocating a new buffer for the address data.
196  *
197  * @param to [in,out] The destination address.
198  * @param from [in] The source address.
199  */
200 static inline void
201 copy_address(address *to, const address *from) {
202     guint8 *to_data;
203
204     to->type = from->type;
205     to->len = from->len;
206     to->hf = from->hf;
207     to_data = (guint8 *)g_malloc(from->len);
208     memcpy(to_data, from->data, from->len);
209     to->data = to_data;
210 }
211 #define COPY_ADDRESS(to, from) copy_address((to), (from))
212
213 /** Perform a shallow copy of the address (both addresses point to the same
214  * memory location).
215  *
216  * @param to [in,out] The destination address.
217  * @param from [in] The source address.
218  */
219 static inline void
220 copy_address_shallow(address *to, const address *from) {
221     memcpy(to, from, sizeof(address));
222     /*
223     to->type = from->type;
224     to->len = from->len;
225     to->hf = from->hf;
226     to->data = from->data;
227     */
228 }
229 #define COPY_ADDRESS_SHALLOW(to, from) copy_address_shallow((to), (from))
230
231 /** Copy an address, allocating a new buffer for the address data
232  *  using seasonal memory.
233  *
234  * @param to [in,out] The destination address.
235  * @param from [in] The source address.
236  */
237 #define SE_COPY_ADDRESS(to, from)     \
238     do {                              \
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; \
244     } while (0)
245
246
247 /** Hash an address into a hash value (which must already have been set).
248  *
249  * @param hash_val The existing hash value.
250  * @param addr The address to add.
251  * @return The new hash value.
252  */
253 static inline guint
254 add_address_to_hash(guint hash_val, const address *addr) {
255     const guint8 *hash_data = (const guint8 *)(addr)->data;
256     int idx;
257
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 );
262     }
263     return hash_val;
264 }
265 #define ADD_ADDRESS_TO_HASH(hash_val, addr) do { hash_val = add_address_to_hash(hash_val, (addr)); } while (0)
266
267 /** Hash an address into a hash value (which must already have been set).
268  *  64-bit version of add_address_to_hash().
269  *
270  * @param hash_val The existing hash value.
271  * @param addr The address to add.
272  * @return The new hash value.
273  */
274 static inline guint64
275 add_address_to_hash64(guint64 hash_val, const address *addr) {
276     const guint8 *hash_data = (const guint8 *)(addr)->data;
277     int idx;
278
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 );
283     }
284     return hash_val;
285 }
286
287 /* Types of port numbers Wireshark knows about. */
288 typedef enum {
289     PT_NONE,            /* no port number */
290     PT_SCTP,            /* SCTP */
291     PT_TCP,             /* TCP */
292     PT_UDP,             /* UDP */
293     PT_DCCP,            /* DCCP */
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 */
302     PT_I2C,
303     PT_IBQP,            /* Infiniband QP number */
304     PT_BLUETOOTH
305 } port_type;
306
307 /* Types of circuit IDs Wireshark knows about. */
308 typedef enum {
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 */
319 } circuit_type;
320
321 #ifdef __cplusplus
322 }
323 #endif /* __cplusplus */
324
325 #endif /* __ADDRESS_H__ */
326
327 /*
328  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
329  *
330  * Local variables:
331  * c-basic-offset: 4
332  * tab-width: 8
333  * indent-tabs-mode: nil
334  * End:
335  *
336  * vi: set shiftwidth=4 tabstop=8 expandtab:
337  * :indentSize=4:tabSize=8:noTabs=true:
338  */