Fix bugs I introduced. Now
[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 #include "emem.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35
36 /* Types of addresses Wireshark knows about. */
37 /* If a new address type is added here, a string representation procedure should */
38 /* also be included in address_to_str_buf defined in to_str.c, for presentation purposes */
39
40 typedef enum {
41   AT_NONE,               /* no link-layer address */
42   AT_ETHER,              /* MAC (Ethernet, 802.x, FDDI) address */
43   AT_IPv4,               /* IPv4 */
44   AT_IPv6,               /* IPv6 */
45   AT_IPX,                /* IPX */
46   AT_SNA,                /* SNA */
47   AT_ATALK,              /* Appletalk DDP */
48   AT_VINES,              /* Banyan Vines */
49   AT_OSI,                /* OSI NSAP */
50   AT_ARCNET,             /* ARCNET */
51   AT_FC,                 /* Fibre Channel */
52   AT_SS7PC,              /* SS7 Point Code */
53   AT_STRINGZ,            /* null-terminated string */
54   AT_EUI64,              /* IEEE EUI-64 */
55   AT_URI,                /* URI/URL/URN */
56   AT_TIPC,               /* TIPC Address Zone,Subnetwork,Processor */
57   AT_IB,                 /* Infiniband GID/LID */
58   AT_USB,                /* USB Device address
59                           * (0xffffffff represents the host) */
60   AT_AX25,               /* AX.25 */
61   AT_IEEE_802_15_4_SHORT /* IEEE 802.15.4 16-bit short address */
62                          /* (the long addresses are EUI-64's */
63 } address_type;
64
65 typedef struct _address {
66   address_type  type;           /* type of address */
67   int           hf;             /* the specific field that this addr is */
68   int           len;            /* length of address, in bytes */
69   const void    *data;          /* pointer to address data */
70 } address;
71
72 #define SET_ADDRESS(addr, addr_type, addr_len, addr_data) { \
73         (addr)->data = (addr_data); \
74         (addr)->type = (addr_type); \
75         (addr)->hf   = -1;          \
76         (addr)->len  = (addr_len);  \
77         }
78
79 /* Same as SET_ADDRESS but it takes a TVB and an offset instead of
80  * (frequently) a pointer into a TVB.  This allow us to get the tvb_get_ptr()
81  * call out of the dissectors.
82  *
83  * Call tvb_get_ptr() first in case it throws an exception: then we won't
84  * modify the address at all.
85  */
86 #define TVB_SET_ADDRESS(addr, addr_type, tvb, offset, addr_len) { \
87         (addr)->data = tvb_get_ptr(tvb, offset, addr_len); \
88         (addr)->type = (addr_type); \
89         (addr)->hf   = -1;          \
90         (addr)->len  = (addr_len);  \
91         }
92
93 #define SET_ADDRESS_HF(addr, addr_type, addr_len, addr_data, addr_hf) { \
94         (addr)->data = (addr_data); \
95         (addr)->type = (addr_type); \
96         (addr)->hf   = (addr_hf);   \
97         (addr)->len  = (addr_len);  \
98         }
99
100 /* Same as SET_ADDRESS_HF but it takes a TVB and an offset instead of
101  * (frequently) a pointer into a TVB.  This allow us to get the tvb_get_ptr()
102  * call out of the dissectors.
103  *
104  * Call tvb_get_ptr() first in case it throws an exception: then we won't
105  * modify the address at all.
106  */
107 #define TVB_SET_ADDRESS_HF(addr, addr_type, tvb, offset, addr_len, addr_hf) { \
108         (addr)->data = tvb_get_ptr(tvb, offset, addr_len); \
109         (addr)->type = (addr_type); \
110         (addr)->hf   = (addr_hf);   \
111         (addr)->len  = (addr_len);  \
112         }
113
114 /*
115  * Given two addresses, return
116  *  0 if the addresses are equal,
117  *  a positive number if addr1>addr2 in some nondefined metric,
118  *  a negative number if addr1<addr2 in some nondefined metric
119  */
120 #define CMP_ADDRESS(addr1, addr2) \
121         (       ((addr1)->type > (addr2)->type)?1:      \
122                 ((addr1)->type < (addr2)->type)?-1:     \
123                 ((addr1)->len  > (addr2)->len) ?1:      \
124                 ((addr1)->len  < (addr2)->len) ?-1:     \
125                 memcmp((addr1)->data, (addr2)->data, (addr1)->len)\
126         )
127
128 /*
129  * Given two addresses, return "true" if they're equal, "false" otherwise.
130  * Addresses are equal only if they have the same type; if the type is
131  * AT_NONE, they are then equal, otherwise they must have the same
132  * amount of data and the data must be the same.
133  */
134 #define ADDRESSES_EQUAL(addr1, addr2)                                   \
135         (                                                               \
136          (addr1)->type == (addr2)->type &&                              \
137          (                                                              \
138           (addr1)->type == AT_NONE ||                                   \
139           (                                                             \
140            (addr1)->len == (addr2)->len &&                              \
141            memcmp((addr1)->data, (addr2)->data, (addr1)->len) == 0      \
142           )                                                             \
143          )                                                              \
144         )
145
146 /*
147  * Copy an address, allocating a new buffer for the address data.
148  */
149 #define COPY_ADDRESS(to, from) { \
150         guint8 *COPY_ADDRESS_data; \
151         (to)->type = (from)->type; \
152         (to)->len = (from)->len; \
153         (to)->hf = (from)->hf; \
154         COPY_ADDRESS_data = g_malloc((from)->len); \
155         memcpy(COPY_ADDRESS_data, (from)->data, (from)->len); \
156         (to)->data = COPY_ADDRESS_data; \
157         }
158
159 /* Perform a shallow copy of the address (both addresses point to the same
160  * memory location).
161  */
162 #define COPY_ADDRESS_SHALLOW(to, from) \
163         (to)->type = (from)->type; \
164         (to)->len = (from)->len; \
165         (to)->hf = (from)->hf; \
166         (to)->data = (from)->data;
167
168 #define SE_COPY_ADDRESS(to, from) { \
169         guint8 *SE_COPY_ADDRESS_data; \
170         (to)->type = (from)->type; \
171         (to)->len = (from)->len; \
172         (to)->hf = (from)->hf; \
173         SE_COPY_ADDRESS_data = se_alloc((from)->len); \
174         memcpy(SE_COPY_ADDRESS_data, (from)->data, (from)->len); \
175         (to)->data = SE_COPY_ADDRESS_data; \
176         }
177
178 /*
179  * Hash an address into a hash value (which must already have been set).
180  */
181 #define ADD_ADDRESS_TO_HASH(hash_val, addr) { \
182         const guint8 *ADD_ADDRESS_TO_HASH_data; \
183         int ADD_ADDRESS_TO_HASH_index; \
184         ADD_ADDRESS_TO_HASH_data = (addr)->data; \
185         for (ADD_ADDRESS_TO_HASH_index = 0; \
186              ADD_ADDRESS_TO_HASH_index < (addr)->len; \
187              ADD_ADDRESS_TO_HASH_index++) \
188              hash_val += ADD_ADDRESS_TO_HASH_data[ADD_ADDRESS_TO_HASH_index]; \
189         }
190
191 /* Types of port numbers Wireshark knows about. */
192 typedef enum {
193   PT_NONE,              /* no port number */
194   PT_SCTP,              /* SCTP */
195   PT_TCP,               /* TCP */
196   PT_UDP,               /* UDP */
197   PT_DCCP,              /* DCCP */
198   PT_IPX,               /* IPX sockets */
199   PT_NCP,               /* NCP connection */
200   PT_EXCHG,             /* Fibre Channel exchange */
201   PT_DDP,               /* DDP AppleTalk connection */
202   PT_SBCCS,             /* FICON */
203   PT_IDP,               /* XNS IDP sockets */
204   PT_TIPC,              /* TIPC PORT */
205   PT_USB,               /* USB endpoint 0xffff means the host */
206   PT_I2C,
207   PT_IBQP               /* Infiniband QP number */
208 } port_type;
209
210 /* Types of circuit IDs Wireshark knows about. */
211 typedef enum {
212   CT_NONE,              /* no circuit type */
213   CT_DLCI,              /* Frame Relay DLCI */
214   CT_ISDN,              /* ISDN channel number */
215   CT_X25,               /* X.25 logical channel number */
216   CT_ISUP,              /* ISDN User Part CIC */
217   CT_IAX2,              /* IAX2 call id */
218   CT_H223,              /* H.223 logical channel number */
219   CT_BICC,              /* BICC Circuit identifier */
220   CT_DVBCI              /* DVB-CI session number */
221   /* Could also have ATM VPI/VCI pairs */
222 } circuit_type;
223
224 #ifdef __cplusplus
225 }
226 #endif /* __cplusplus */
227
228 #endif /* __ADDRESS_H__ */