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_
27 #warning "assertions are disabled"
36 #ifdef HAVE_NETINET_IN_H
37 #include <netinet/in.h>
39 #ifdef HAVE_NETINET_IN_H
40 #include <sys/socket.h>
44 #include <lwip/ip_addr.h>
46 typedef struct coap_address_t {
51 /* FIXME oversimplification: just assuming it's an ipv4 address instead of
52 * looking up the appropraite lwip function */
54 #define _coap_address_equals_impl(A, B) ((A)->addr.addr == (B)->addr.addr && A->port == B->port)
56 /* FIXME sure there is something in lwip */
58 #define _coap_is_mcast_impl(Address) 0
60 #endif /* WITH_LWIP */
64 typedef struct coap_address_t {
70 #define _coap_address_equals_impl(A,B) \
71 ((A)->size == (B)->size \
72 && (A)->port == (B)->port \
73 && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
75 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
76 #endif /* WITH_CONTIKI */
81 /** multi-purpose address abstraction */
82 typedef struct coap_address_t {
83 socklen_t size; /**< size of addr */
86 struct sockaddr_storage st;
87 struct sockaddr_in sin;
88 struct sockaddr_in6 sin6;
94 _coap_address_equals_impl(const coap_address_t *a,
95 const coap_address_t *b) {
96 if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
99 /* need to compare only relevant parts of sockaddr_in6 */
100 switch (a->addr.sa.sa_family) {
103 a->addr.sin.sin_port == b->addr.sin.sin_port &&
104 memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
105 sizeof(struct in_addr)) == 0;
107 return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
108 memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
109 sizeof(struct in6_addr)) == 0;
110 default: /* fall through and signal error */
117 _coap_is_mcast_impl(const coap_address_t *a) {
121 switch (a->addr.sa.sa_family) {
123 return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
125 return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
126 default: /* fall through and signal error */
132 #endif /* WITH_POSIX */
135 typedef OCDevAddr coap_address_t;
138 _coap_address_equals_impl(const coap_address_t *a,
139 const coap_address_t *b) {
142 if ((a == NULL) || (b == NULL))
145 if (a->size != b->size)
148 for (i = 0; i < a->size; i++)
150 if (a->addr[i] != b->addr[i])
157 _coap_is_mcast_impl(const coap_address_t *a) {
165 #endif /* WITH_ARDUINO */
168 * Resets the given coap_address_t object @p addr to its default
169 * values. In particular, the member size must be initialized to the
170 * available size for storing addresses.
172 * @param addr The coap_address_t object to initialize.
175 coap_address_init(coap_address_t *addr) {
177 memset(addr, 0, sizeof(coap_address_t));
179 /* lwip has constandt address sizes and doesn't need the .size part */
180 addr->size = sizeof(addr->addr);
185 * Compares given address objects @p a and @p b. This function returns
186 * @c 1 if addresses are equal, @c 0 otherwise. The parameters @p a
187 * and @p b must not be @c NULL;
190 coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
191 assert(a); assert(b);
192 return _coap_address_equals_impl(a, b);
196 * Checks if given address @p a denotes a multicast address. This
197 * function returns @c 1 if @p a is multicast, @c 0 otherwise.
200 coap_is_mcast(const coap_address_t *a) {
201 return a && _coap_is_mcast_impl(a);
208 #endif /* _COAP_ADDRESS_H_ */