Clamp down on address data structure usage and AT_NONE semantics
[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  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 #ifndef __ADDRESS_H__
25 #define __ADDRESS_H__
26
27 #include <string.h>     /* for memcmp */
28
29 #include "tvbuff.h"
30 #include "wmem/wmem.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35
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
42  */
43 typedef enum {
44     AT_NONE,               /* no link-layer address */
45     AT_ETHER,              /* MAC (Ethernet, 802.x, FDDI) address */
46     AT_IPv4,               /* IPv4 */
47     AT_IPv6,               /* IPv6 */
48     AT_IPX,                /* IPX */
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) */
58     AT_AX25,               /* AX.25 */
59
60     AT_END_OF_LIST         /* Must be last in list */
61 } address_type;
62
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 */
67
68     /* private */
69     void         *priv;
70 } address;
71
72 #define ADDRESS_INIT(type, len, data) {type, len, data, NULL}
73 #define ADDRESS_INIT_NONE ADDRESS_INIT(AT_NONE, 0, NULL)
74
75 static inline void
76 clear_address(address *addr)
77 {
78     addr->type = AT_NONE;
79     addr->len  = 0;
80     addr->data = NULL;
81     addr->priv = NULL;
82 }
83
84 /** Initialize an address with the given values.
85  *
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.
91  */
92 static inline void
93 set_address(address *addr, int addr_type, int addr_len, const void *addr_data) {
94     addr->type = addr_type;
95     addr->len  = addr_len;
96     addr->data = addr_data;
97     addr->priv = NULL;
98 }
99
100 /** Initialize an address from TVB data.
101  *
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().
104  *
105  * This calls tvb_get_ptr() (including throwing any exceptions) before
106  * modifying the address.
107  *
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.
114  */
115 static inline void
116 set_address_tvb(address *addr, int addr_type, int addr_len, tvbuff_t *tvb, int offset) {
117     const void *p;
118
119     p = tvb_get_ptr(tvb, offset, addr_len);
120     set_address(addr, addr_type, addr_len, p);
121 }
122
123 /** Initialize an address with the given values, allocating a new buffer
124  * for the address data using wmem-scoped memory.
125  *
126  * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
127  * @param addr [in,out] The address to initialize.
128  * @param addr_type [in] Address type.
129  * @param addr_len [in] The length in bytes of the address data. For example, 4 for
130  *                     AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
131  * @param addr_data [in] Pointer to the address data.
132  */
133 static inline void
134 alloc_address_wmem(wmem_allocator_t *scope, address *addr,
135                         int addr_type, int addr_len, const void *addr_data) {
136     g_assert(addr);
137     clear_address(addr);
138     addr->type = addr_type;
139     if (addr_type == AT_NONE || addr_len <= 0 || addr_data == NULL) {
140         g_assert(addr_len <= 0);
141         g_assert(addr_data == NULL);
142         return;
143     }
144     addr->data = addr->priv = wmem_memdup(scope, addr_data, addr_len);
145     addr->len = addr_len;
146 }
147
148 /** Allocate an address from TVB data.
149  *
150  * Same as alloc_address_wmem but it takes a TVB and an offset.
151  *
152  * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
153  * @param addr [in,out] The address to initialize.
154  * @param addr_type [in] Address type.
155  * @param addr_len [in] The length in bytes of the address data. For example, 4 for
156  *                     AT_IPv4 or sizeof(struct e_in6_addr) for AT_IPv6.
157  * @param tvb [in] Pointer to the TVB.
158  * @param offset [in] Offset within the TVB.
159  */
160 static inline void
161 alloc_address_tvb(wmem_allocator_t *scope, address *addr,
162                     int addr_type, int addr_len,  tvbuff_t *tvb, int offset) {
163     const void *p;
164
165     p = tvb_get_ptr(tvb, offset, addr_len);
166     alloc_address_wmem(scope, addr, addr_type, addr_len, p);
167 }
168
169 /** Compare two addresses.
170  *
171  * @param addr1 [in] The first address to compare.
172  * @param addr2 [in] The second address to compare.
173  * @return 0 if the addresses are equal,
174  *  A positive number if addr1 > addr2 in some nondefined metric,
175  *  A negative number if addr1 < addr2 in some nondefined metric.
176  */
177 static inline int
178 cmp_address(const address *addr1, const address *addr2) {
179     if (addr1->type > addr2->type) return 1;
180     if (addr1->type < addr2->type) return -1;
181     if (addr1->len  > addr2->len) return 1;
182     if (addr1->len  < addr2->len) return -1;
183     return memcmp(addr1->data, addr2->data, addr1->len);
184 }
185
186 /** Check two addresses for equality.
187  *
188  * Given two addresses, return "true" if they're equal, "false" otherwise.
189  * Addresses are equal only if they have the same type; if the type is
190  * AT_NONE, they are then equal, otherwise they must have the same
191  * amount of data and the data must be the same.
192  *
193  * @param addr1 [in] The first address to compare.
194  * @param addr2 [in] The second address to compare.
195  * @return TRUE if the adresses are equal, FALSE otherwise.
196  */
197 static inline gboolean
198 addresses_equal(const address *addr1, const address *addr2) {
199     if (addr1->type == addr2->type
200             && ( addr1->type == AT_NONE
201                  || ( addr1->len == addr2->len
202                       && memcmp(addr1->data, addr2->data, addr1->len) == 0
203                       )
204                  )
205             ) return TRUE;
206     return FALSE;
207 }
208
209 /** Check the data of two addresses for equality.
210  *
211  * Given two addresses, return "true" if they have the same length and,
212  * their data is equal, "false" otherwise.
213  * The address types are ignored. This can be used to compare custom
214  * address types defined with address_type_dissector_register.
215  *
216  * @param addr1 [in] The first address to compare.
217  * @param addr2 [in] The second address to compare.
218  * @return TRUE if the adresses are equal, FALSE otherwise.
219  */
220 static inline gboolean
221 addresses_data_equal(const address *addr1, const address *addr2) {
222     if ( addr1->len == addr2->len
223             && memcmp(addr1->data, addr2->data, addr1->len) == 0
224             ) return TRUE;
225     return FALSE;
226 }
227
228 /** Perform a shallow copy of the address (both addresses point to the same
229  * memory location).
230  *
231  * @param to [in,out] The destination address.
232  * @param from [in] The source address.
233  *
234  * \warning Make sure 'from' memory stays valid for the lifetime of this object.
235  * Also it's strongly recommended to use this function instead of copy-assign.
236  */
237 static inline void
238 copy_address_shallow(address *to, const address *from) {
239     set_address(to, from->type, from->len, from->data);
240 }
241
242 /** Copy an address, allocating a new buffer for the address data
243  *  using wmem-scoped memory.
244  *
245  * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
246  * @param to [in,out] The destination address.
247  * @param from [in] The source address.
248  */
249 static inline void
250 copy_address_wmem(wmem_allocator_t *scope, address *to, const address *from) {
251     alloc_address_wmem(scope, to, from->type, from->len, from->data);
252 }
253
254 /** Copy an address, allocating a new buffer for the address data.
255  *
256  * @param to [in,out] The destination address.
257  * @param from [in] The source address.
258  */
259 static inline void
260 copy_address(address *to, const address *from) {
261     copy_address_wmem(NULL, to, from);
262 }
263
264 /** Free an address allocated with wmem-scoped memory.
265  *
266  * @param scope [in] The lifetime of the allocated memory, e.g., wmem_packet_scope()
267  * @param addr [in,out] The address whose data to free.
268  */
269 static inline void
270 free_address_wmem(wmem_allocator_t *scope, address *addr) {
271     /* Because many dissectors set 'type = AT_NONE' to mean clear we check for that */
272     if (addr->type != AT_NONE && addr->len > 0 && addr->priv != NULL) {
273         /* Make sure API use is correct */
274         /* if priv is not null then data == priv */
275         g_assert(addr->data == addr->priv);
276         wmem_free(scope, addr->priv);
277     }
278     clear_address(addr);
279 }
280
281 /** Free an address.
282  *
283  * @param addr [in,out] The address whose data to free.
284  */
285 static inline void
286 free_address(address *addr) {
287     free_address_wmem(NULL, addr);
288 }
289
290 /** Hash an address into a hash value (which must already have been set).
291  *
292  * @param hash_val The existing hash value.
293  * @param addr The address to add.
294  * @return The new hash value.
295  */
296 static inline guint
297 add_address_to_hash(guint hash_val, const address *addr) {
298     const guint8 *hash_data = (const guint8 *)(addr)->data;
299     int idx;
300
301     for (idx = 0; idx < (addr)->len; idx++) {
302         hash_val += hash_data[idx];
303         hash_val += ( hash_val << 10 );
304         hash_val ^= ( hash_val >> 6 );
305     }
306     return hash_val;
307 }
308
309 /** Hash an address into a hash value (which must already have been set).
310  *  64-bit version of add_address_to_hash().
311  *
312  * @param hash_val The existing hash value.
313  * @param addr The address to add.
314  * @return The new hash value.
315  */
316 static inline guint64
317 add_address_to_hash64(guint64 hash_val, const address *addr) {
318     const guint8 *hash_data = (const guint8 *)(addr)->data;
319     int idx;
320
321     for (idx = 0; idx < (addr)->len; idx++) {
322         hash_val += hash_data[idx];
323         hash_val += ( hash_val << 10 );
324         hash_val ^= ( hash_val >> 6 );
325     }
326     return hash_val;
327 }
328
329 /* Types of port numbers Wireshark knows about. */
330 typedef enum {
331     PT_NONE,            /* no port number */
332     PT_SCTP,            /* SCTP */
333     PT_TCP,             /* TCP */
334     PT_UDP,             /* UDP */
335     PT_DCCP,            /* DCCP */
336     PT_IPX,             /* IPX sockets */
337     PT_NCP,             /* NCP connection */
338     PT_EXCHG,           /* Fibre Channel exchange */
339     PT_DDP,             /* DDP AppleTalk connection */
340     PT_SBCCS,           /* FICON */
341     PT_IDP,             /* XNS IDP sockets */
342     PT_TIPC,            /* TIPC PORT */
343     PT_USB,             /* USB endpoint 0xffff means the host */
344     PT_I2C,
345     PT_IBQP,            /* Infiniband QP number */
346     PT_BLUETOOTH,
347     PT_TDMOP
348 } port_type;
349
350 /* Types of circuit IDs Wireshark knows about. */
351 typedef enum {
352     CT_NONE,            /* no circuit type */
353     CT_DLCI,            /* Frame Relay DLCI */
354     CT_ISDN,            /* ISDN channel number */
355     CT_X25,             /* X.25 logical channel number */
356     CT_ISUP,            /* ISDN User Part CIC */
357     CT_IAX2,            /* IAX2 call id */
358     CT_H223,            /* H.223 logical channel number */
359     CT_BICC,            /* BICC Circuit identifier */
360     CT_DVBCI,           /* DVB-CI session number|transport connection id */
361     CT_ISO14443         /* ISO14443 connection between terminal and card
362                            the circuit ID is always 0, there's only one
363                            such connection */
364     /* Could also have ATM VPI/VCI pairs */
365 } circuit_type;
366
367 #ifdef __cplusplus
368 }
369 #endif /* __cplusplus */
370
371 #endif /* __ADDRESS_H__ */
372
373 /*
374  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
375  *
376  * Local variables:
377  * c-basic-offset: 4
378  * tab-width: 8
379  * indent-tabs-mode: nil
380  * End:
381  *
382  * vi: set shiftwidth=4 tabstop=8 expandtab:
383  * :indentSize=4:tabSize=8:noTabs=true:
384  */