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