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