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>
40 #define DEV_ADDR_SIZE_MAX (16)
44 #include <lwip/ip_addr.h>
46 typedef struct coap_address_t
52 /* FIXME oversimplification: just assuming it's an ipv4 address instead of
53 * looking up the appropraite lwip function */
55 #define _coap_address_equals_impl(A, B) ((A)->addr.addr == (B)->addr.addr && A->port == B->port)
57 /* FIXME sure there is something in lwip */
59 #define _coap_is_mcast_impl(Address) 0
61 #endif /* WITH_LWIP */
65 typedef struct coap_address_t
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)))
77 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
78 #endif /* WITH_CONTIKI */
81 /** multi-purpose address abstraction */
82 typedef struct coap_address_t
84 socklen_t size; /**< size of addr */
88 struct sockaddr_storage st;
89 struct sockaddr_in sin;
90 struct sockaddr_in6 sin6;
95 _coap_address_equals_impl(const coap_address_t *a,
96 const coap_address_t *b)
98 if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
101 /* need to compare only relevant parts of sockaddr_in6 */
102 switch (a->addr.sa.sa_family)
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;
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 */
120 _coap_is_mcast_impl(const coap_address_t *a)
126 switch (a->addr.sa.sa_family)
129 return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
131 return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
132 default: /* fall through and signal error */
137 #endif /* WITH_POSIX */
140 typedef struct coap_address_t
142 uint32_t size; /**< length of the address stored in addr field. */
143 uint8_t addr[DEV_ADDR_SIZE_MAX]; /**< device address. */
147 _coap_address_equals_impl(const coap_address_t *a,
148 const coap_address_t *b)
152 if ((a == NULL) || (b == NULL))
155 if (a->size != b->size)
158 for (i = 0; i < a->size; i++)
160 if (a->addr[i] != b->addr[i])
167 _coap_is_mcast_impl(const coap_address_t *a)
176 #endif /* WITH_ARDUINO */
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.
183 * @param addr The coap_address_t object to initialize.
185 static inline void coap_address_init(coap_address_t *addr)
188 memset(addr, 0, sizeof(coap_address_t));
190 /* lwip has constandt address sizes and doesn't need the .size part */
191 addr->size = sizeof(addr->addr);
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;
200 static inline int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
204 return _coap_address_equals_impl(a, b);
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.
211 static inline int coap_is_mcast(const coap_address_t *a)
213 return a && _coap_is_mcast_impl(a);
216 #endif /* _COAP_ADDRESS_H_ */