1 /* address.h -- representation of network addresses
3 * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
11 * @brief representation of network addresses
14 #ifndef _COAP_ADDRESS_H_
15 #define _COAP_ADDRESS_H_
23 #warning "assertions are disabled"
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
35 #ifdef HAVE_NETINET_IN_H
36 #include <sys/socket.h>
39 #ifdef HAVE_WINSOCK2_H
43 #ifdef HAVE_WS2TCPIP_H
48 #define DEV_ADDR_SIZE_MAX (16)
52 #include <lwip/ip_addr.h>
54 typedef struct coap_address_t
60 /* FIXME oversimplification: just assuming it's an ipv4 address instead of
61 * looking up the appropraite lwip function */
63 #define _coap_address_equals_impl(A, B) ((A)->addr.addr == (B)->addr.addr && A->port == B->port)
65 /* FIXME sure there is something in lwip */
67 #define _coap_is_mcast_impl(Address) 0
69 #endif /* WITH_LWIP */
73 typedef struct coap_address_t
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)))
85 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
86 #endif /* WITH_CONTIKI */
87 #if defined(WITH_POSIX) || defined(_WIN32)
89 /** multi-purpose address abstraction */
90 typedef struct coap_address_t
92 socklen_t size; /**< size of addr */
96 struct sockaddr_storage st;
97 struct sockaddr_in sin;
98 struct sockaddr_in6 sin6;
103 _coap_address_equals_impl(const coap_address_t *a,
104 const coap_address_t *b)
106 if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
109 /* need to compare only relevant parts of sockaddr_in6 */
110 switch (a->addr.sa.sa_family)
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;
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 */
128 _coap_is_mcast_impl(const coap_address_t *a)
134 switch (a->addr.sa.sa_family)
137 return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
138 /* TODO - Enable this once IPv6 comes in TizenRT */
141 return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
143 default: /* fall through and signal error */
148 #endif /* WITH_POSIX */
151 typedef struct coap_address_t
153 uint32_t size; /**< length of the address stored in addr field. */
154 uint8_t addr[DEV_ADDR_SIZE_MAX]; /**< device address. */
158 _coap_address_equals_impl(const coap_address_t *a,
159 const coap_address_t *b)
163 if ((a == NULL) || (b == NULL))
166 if (a->size != b->size)
169 for (i = 0; i < a->size; i++)
171 if (a->addr[i] != b->addr[i])
178 _coap_is_mcast_impl(const coap_address_t *a)
187 #endif /* WITH_ARDUINO */
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.
194 * @param addr The coap_address_t object to initialize.
196 INLINE_API void coap_address_init(coap_address_t *addr)
199 memset(addr, 0, sizeof(coap_address_t));
201 /* lwip has constandt address sizes and doesn't need the .size part */
202 addr->size = sizeof(addr->addr);
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;
211 INLINE_API int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
215 return _coap_address_equals_impl(a, b);
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.
222 INLINE_API int coap_is_mcast(const coap_address_t *a)
224 return a && _coap_is_mcast_impl(a);
227 #endif /* _COAP_ADDRESS_H_ */