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 #include <lwip/ip_addr.h>
42 typedef struct coap_address_t
48 /* FIXME oversimplification: just assuming it's an ipv4 address instead of
49 * looking up the appropraite lwip function */
51 #define _coap_address_equals_impl(A, B) ((A)->addr.addr == (B)->addr.addr && A->port == B->port)
53 /* FIXME sure there is something in lwip */
55 #define _coap_is_mcast_impl(Address) 0
57 #endif /* WITH_LWIP */
61 typedef struct coap_address_t
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)))
73 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
74 #endif /* WITH_CONTIKI */
77 /** multi-purpose address abstraction */
78 typedef struct coap_address_t
80 socklen_t size; /**< size of addr */
84 struct sockaddr_storage st;
85 struct sockaddr_in sin;
86 struct sockaddr_in6 sin6;
91 _coap_address_equals_impl(const coap_address_t *a,
92 const coap_address_t *b)
94 if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
97 /* need to compare only relevant parts of sockaddr_in6 */
98 switch (a->addr.sa.sa_family)
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;
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 */
116 _coap_is_mcast_impl(const coap_address_t *a)
120 printf("[COAP] address - coap_address_t is false\n");
123 switch (a->addr.sa.sa_family)
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);
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");
136 #endif /* WITH_POSIX */
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.
143 * @param addr The coap_address_t object to initialize.
145 static inline void coap_address_init(coap_address_t *addr)
148 memset(addr, 0, sizeof(coap_address_t));
150 /* lwip has constandt address sizes and doesn't need the .size part */
151 addr->size = sizeof(addr->addr);
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;
160 static inline int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
164 return _coap_address_equals_impl(a, b);
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.
171 static inline int coap_is_mcast(const coap_address_t *a)
173 return a && _coap_is_mcast_impl(a);
176 #endif /* _COAP_ADDRESS_H_ */