replace : iotivity -> iotivity-sec
[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 /* TODO - Enable this once IPv6 comes in TizenRT */
139 #ifndef __TIZENRT__
140         case AF_INET6:
141         return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
142 #endif
143         default: /* fall through and signal error */
144         ;
145     }
146     return 0;
147 }
148 #endif /* WITH_POSIX */
149
150 #ifdef WITH_ARDUINO
151 typedef struct coap_address_t
152 {
153     uint32_t     size;                    /**< length of the address stored in addr field. */
154     uint8_t      addr[DEV_ADDR_SIZE_MAX]; /**< device address. */
155 } coap_address_t;
156
157 INLINE_API int
158 _coap_address_equals_impl(const coap_address_t *a,
159                           const coap_address_t *b)
160 {
161     uint32_t i;
162
163     if ((a == NULL) || (b == NULL))
164         return 0;
165
166     if (a->size != b->size)
167         return 0;
168
169     for (i = 0; i < a->size; i++)
170     {
171         if (a->addr[i] != b->addr[i])
172             return 0;
173     }
174     return 1;
175 }
176
177 INLINE_API int
178 _coap_is_mcast_impl(const coap_address_t *a)
179 {
180     if (!a)
181         return 0;
182
183     /* TODO */
184     return 0;
185 }
186
187 #endif /* WITH_ARDUINO */
188
189 /**
190  * Resets the given coap_address_t object @p addr to its default
191  * values.  In particular, the member size must be initialized to the
192  * available size for storing addresses.
193  *
194  * @param addr The coap_address_t object to initialize.
195  */
196 INLINE_API void coap_address_init(coap_address_t *addr)
197 {
198     assert(addr);
199     memset(addr, 0, sizeof(coap_address_t));
200 #ifndef WITH_LWIP
201     /* lwip has constandt address sizes and doesn't need the .size part */
202     addr->size = sizeof(addr->addr);
203 #endif
204 }
205
206 /**
207  * Compares given address objects @p a and @p b. This function returns
208  * @c 1 if addresses are equal, @c 0 otherwise. The parameters @p a
209  * and @p b must not be @c NULL;
210  */
211 INLINE_API int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
212 {
213     assert(a);
214     assert(b);
215     return _coap_address_equals_impl(a, b);
216 }
217
218 /**
219  * Checks if given address @p a denotes a multicast address.  This
220  * function returns @c 1 if @p a is multicast, @c 0 otherwise.
221  */
222 INLINE_API int coap_is_mcast(const coap_address_t *a)
223 {
224     return a && _coap_is_mcast_impl(a);
225 }
226
227 #endif /* _COAP_ADDRESS_H_ */