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