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