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 {
47 /* FIXME oversimplification: just assuming it's an ipv4 address instead of
48 * looking up the appropraite lwip function */
50 #define _coap_address_equals_impl(A, B) ((A)->addr.addr == (B)->addr.addr && A->port == B->port)
52 /* FIXME sure there is something in lwip */
54 #define _coap_is_mcast_impl(Address) 0
56 #endif /* WITH_LWIP */
60 typedef struct coap_address_t {
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)))
71 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
72 #endif /* WITH_CONTIKI */
76 /** multi-purpose address abstraction */
77 typedef struct coap_address_t {
78 socklen_t size; /**< size of addr */
81 struct sockaddr_storage st;
82 struct sockaddr_in sin;
83 struct sockaddr_in6 sin6;
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)
94 /* need to compare only relevant parts of sockaddr_in6 */
95 switch (a->addr.sa.sa_family) {
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;
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 */
112 _coap_is_mcast_impl(const coap_address_t *a) {
116 switch (a->addr.sa.sa_family) {
118 return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
120 return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
121 default: /* fall through and signal error */
126 #endif /* WITH_POSIX */
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.
133 * @param addr The coap_address_t object to initialize.
136 coap_address_init(coap_address_t *addr) {
138 memset(addr, 0, sizeof(coap_address_t));
140 /* lwip has constandt address sizes and doesn't need the .size part */
141 addr->size = sizeof(addr->addr);
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;
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);
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.
161 coap_is_mcast(const coap_address_t *a) {
162 return a && _coap_is_mcast_impl(a);
165 #endif /* _COAP_ADDRESS_H_ */