As suggested by Anders: back out 37112.
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 } address_type;
61
62 typedef struct _address {
63   address_type  type;           /* type of address */
64   int           len;            /* length of address, in bytes */
65   const void    *data;          /* pointer to address data */
66 } address;
67
68 #define SET_ADDRESS(addr, addr_type, addr_len, addr_data) { \
69         (addr)->type = (addr_type); \
70         (addr)->len = (addr_len); \
71         (addr)->data = (addr_data); \
72         }
73
74 /*
75  * Given two addresses, return
76  *  0 if the addresses are equal,
77  *  a positive number if addr1>addr2 in some nondefined metric,
78  *  a negative number if addr1<addr2 in some nondefined metric
79  */
80 #define CMP_ADDRESS(addr1, addr2) \
81         (       ((addr1)->type > (addr2)->type)?1:      \
82                 ((addr1)->type < (addr2)->type)?-1:     \
83                 ((addr1)->len  > (addr2)->len) ?1:      \
84                 ((addr1)->len  < (addr2)->len) ?-1:     \
85                 memcmp((addr1)->data, (addr2)->data, (addr1)->len)\
86         )
87
88 /*
89  * Given two addresses, return "true" if they're equal, "false" otherwise.
90  * Addresses are equal only if they have the same type; if the type is
91  * AT_NONE, they are then equal, otherwise they must have the same
92  * amount of data and the data must be the same.
93  */
94 #define ADDRESSES_EQUAL(addr1, addr2)                                   \
95         (                                                               \
96          (addr1)->type == (addr2)->type &&                              \
97          (                                                              \
98           (addr1)->type == AT_NONE ||                                   \
99           (                                                             \
100            (addr1)->len == (addr2)->len &&                              \
101            memcmp((addr1)->data, (addr2)->data, (addr1)->len) == 0      \
102           )                                                             \
103          )                                                              \
104         )
105
106 /*
107  * Copy an address, allocating a new buffer for the address data.
108  */
109 #define COPY_ADDRESS(to, from) { \
110         guint8 *COPY_ADDRESS_data; \
111         (to)->type = (from)->type; \
112         (to)->len = (from)->len; \
113         COPY_ADDRESS_data = g_malloc((from)->len); \
114         memcpy(COPY_ADDRESS_data, (from)->data, (from)->len); \
115         (to)->data = COPY_ADDRESS_data; \
116         }
117
118 #define SE_COPY_ADDRESS(to, from) { \
119         guint8 *SE_COPY_ADDRESS_data; \
120         (to)->type = (from)->type; \
121         (to)->len = (from)->len; \
122         SE_COPY_ADDRESS_data = se_alloc((from)->len); \
123         memcpy(SE_COPY_ADDRESS_data, (from)->data, (from)->len); \
124         (to)->data = SE_COPY_ADDRESS_data; \
125         }
126
127 /*
128  * Hash an address into a hash value (which must already have been set).
129  */
130 #define ADD_ADDRESS_TO_HASH(hash_val, addr) { \
131         const guint8 *ADD_ADDRESS_TO_HASH_data; \
132         int ADD_ADDRESS_TO_HASH_index; \
133         ADD_ADDRESS_TO_HASH_data = (addr)->data; \
134         for (ADD_ADDRESS_TO_HASH_index = 0; \
135              ADD_ADDRESS_TO_HASH_index < (addr)->len; \
136              ADD_ADDRESS_TO_HASH_index++) \
137              hash_val += ADD_ADDRESS_TO_HASH_data[ADD_ADDRESS_TO_HASH_index]; \
138         }
139
140 /* Types of port numbers Wireshark knows about. */
141 typedef enum {
142   PT_NONE,              /* no port number */
143   PT_SCTP,              /* SCTP */
144   PT_TCP,               /* TCP */
145   PT_UDP,               /* UDP */
146   PT_DCCP,              /* DCCP */
147   PT_IPX,               /* IPX sockets */
148   PT_NCP,               /* NCP connection */
149   PT_EXCHG,             /* Fibre Channel exchange */
150   PT_DDP,               /* DDP AppleTalk connection */
151   PT_SBCCS,             /* FICON */
152   PT_IDP,               /* XNS IDP sockets */
153   PT_TIPC,              /* TIPC PORT */
154   PT_USB,               /* USB endpoint 0xffff means the host */
155   PT_I2C,
156   PT_IBQP               /* Infiniband QP number */
157 } port_type;
158
159 /* Types of circuit IDs Wireshark knows about. */
160 typedef enum {
161   CT_NONE,              /* no circuit type */
162   CT_DLCI,              /* Frame Relay DLCI */
163   CT_ISDN,              /* ISDN channel number */
164   CT_X25,               /* X.25 logical channel number */
165   CT_ISUP,              /* ISDN User Part CIC */
166   CT_IAX2,              /* IAX2 call id */
167   CT_H223,              /* H.223 logical channel number */
168   CT_BICC               /* BICC Circuit identifier */
169   /* Could also have ATM VPI/VCI pairs */
170 } circuit_type;
171
172 #ifdef __cplusplus
173 }
174 #endif /* __cplusplus */
175
176 #endif /* __ADDRESS_H__ */