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