c26784c05f148e688ef55aba699d9cedb368abfb
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / include / coap / address.h
1 /* address.h -- representation of network addresses
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8
9 /**
10  * @file address.h
11  * @brief representation of network addresses
12  */
13
14 #ifndef _COAP_ADDRESS_H_
15 #define _COAP_ADDRESS_H_
16
17 #include "config.h"
18
19 #ifdef HAVE_ASSERT_H
20 #include <assert.h>
21 #else
22 #ifndef assert
23 #warning "assertions are disabled"
24 #  define assert(x)
25 #endif
26 #endif
27
28 #include <string.h>
29 #include <stdint.h>
30
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
33 #endif
34
35 #ifdef HAVE_NETINET_IN_H
36 #include <sys/socket.h>
37 #endif
38
39 #ifdef HAVE_WINSOCK2_H
40 #include <winsock2.h>
41 #endif
42
43 #ifdef HAVE_WS2TCPIP_H
44 #include <ws2tcpip.h>
45 #endif
46
47 #ifdef WITH_ARDUINO
48 #define DEV_ADDR_SIZE_MAX (16)
49 #endif
50
51 #ifdef WITH_LWIP
52 #include <lwip/ip_addr.h>
53
54 typedef struct coap_address_t
55 {
56     uint16_t port;
57     ip_addr_t addr;
58 }coap_address_t;
59
60 /* FIXME oversimplification: just assuming it's an ipv4 address instead of
61  * looking up the appropraite lwip function */
62
63 #define _coap_address_equals_impl(A, B) ((A)->addr.addr == (B)->addr.addr && A->port == B->port)
64
65 /* FIXME sure there is something in lwip */
66
67 #define _coap_is_mcast_impl(Address) 0
68
69 #endif /* WITH_LWIP */
70 #ifdef WITH_CONTIKI
71 #include "uip.h"
72
73 typedef struct coap_address_t
74 {
75     unsigned char size;
76     uip_ipaddr_t addr;
77     unsigned short port;
78 }coap_address_t;
79
80 #define _coap_address_equals_impl(A,B)                          \
81   ((A)->size == (B)->size                                       \
82    && (A)->port == (B)->port                                    \
83    && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
84
85 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
86 #endif /* WITH_CONTIKI */
87 #if defined(WITH_POSIX) || defined(_WIN32)
88
89 /** multi-purpose address abstraction */
90 typedef struct coap_address_t
91 {
92     socklen_t size; /**< size of addr */
93     union
94     {
95         struct sockaddr sa;
96         struct sockaddr_storage st;
97         struct sockaddr_in sin;
98         struct sockaddr_in6 sin6;
99     }addr;
100 }coap_address_t;
101
102 INLINE_API int
103 _coap_address_equals_impl(const coap_address_t *a,
104         const coap_address_t *b)
105 {
106     if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
107     return 0;
108
109     /* need to compare only relevant parts of sockaddr_in6 */
110     switch (a->addr.sa.sa_family)
111     {
112         case AF_INET:
113         return
114         a->addr.sin.sin_port == b->addr.sin.sin_port &&
115         memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
116                 sizeof(struct in_addr)) == 0;
117         case AF_INET6:
118         return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
119         memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
120                 sizeof(struct in6_addr)) == 0;
121         default: /* fall through and signal error */
122         ;
123     }
124     return 0;
125 }
126
127 INLINE_API int
128 _coap_is_mcast_impl(const coap_address_t *a)
129 {
130     if (!a)
131     {
132         return 0;
133     }
134     switch (a->addr.sa.sa_family)
135     {
136         case AF_INET:
137         return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
138         case AF_INET6:
139         return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
140         default: /* fall through and signal error */
141         ;
142     }
143     return 0;
144 }
145 #endif /* WITH_POSIX */
146
147 #ifdef WITH_ARDUINO
148 typedef struct coap_address_t
149 {
150     uint32_t     size;                    /**< length of the address stored in addr field. */
151     uint8_t      addr[DEV_ADDR_SIZE_MAX]; /**< device address. */
152 } coap_address_t;
153
154 INLINE_API int
155 _coap_address_equals_impl(const coap_address_t *a,
156                           const coap_address_t *b)
157 {
158     uint32_t i;
159
160     if ((a == NULL) || (b == NULL))
161         return 0;
162
163     if (a->size != b->size)
164         return 0;
165
166     for (i = 0; i < a->size; i++)
167     {
168         if (a->addr[i] != b->addr[i])
169             return 0;
170     }
171     return 1;
172 }
173
174 INLINE_API int
175 _coap_is_mcast_impl(const coap_address_t *a)
176 {
177     if (!a)
178         return 0;
179
180     /* TODO */
181     return 0;
182 }
183
184 #endif /* WITH_ARDUINO */
185
186 /**
187  * Resets the given coap_address_t object @p addr to its default
188  * values.  In particular, the member size must be initialized to the
189  * available size for storing addresses.
190  *
191  * @param addr The coap_address_t object to initialize.
192  */
193 INLINE_API void coap_address_init(coap_address_t *addr)
194 {
195     assert(addr);
196     memset(addr, 0, sizeof(coap_address_t));
197 #ifndef WITH_LWIP
198     /* lwip has constandt address sizes and doesn't need the .size part */
199     addr->size = sizeof(addr->addr);
200 #endif
201 }
202
203 /**
204  * Compares given address objects @p a and @p b. This function returns
205  * @c 1 if addresses are equal, @c 0 otherwise. The parameters @p a
206  * and @p b must not be @c NULL;
207  */
208 INLINE_API int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
209 {
210     assert(a);
211     assert(b);
212     return _coap_address_equals_impl(a, b);
213 }
214
215 /**
216  * Checks if given address @p a denotes a multicast address.  This
217  * function returns @c 1 if @p a is multicast, @c 0 otherwise.
218  */
219 INLINE_API int coap_is_mcast(const coap_address_t *a)
220 {
221     return a && _coap_is_mcast_impl(a);
222 }
223
224 #endif /* _COAP_ADDRESS_H_ */