libcoap  4.3.0rc1
address.h
Go to the documentation of this file.
1 /*
2  * address.h -- representation of network addresses
3  *
4  * Copyright (C) 2010-2011,2015-2016 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
15 #ifndef COAP_ADDRESS_H_
16 #define COAP_ADDRESS_H_
17 
18 #include <assert.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include "coap2/libcoap.h"
23 
24 #if defined(WITH_LWIP)
25 
26 #include <lwip/ip_addr.h>
27 
28 typedef struct coap_address_t {
29  uint16_t port;
30  ip_addr_t addr;
32 
36 COAP_STATIC_INLINE uint16_t
38  return ntohs(addr->port);
39 }
40 
45 coap_address_set_port(coap_address_t *addr, uint16_t port) {
46  addr->port = htons(port);
47 }
48 
49 #define _coap_address_equals_impl(A, B) \
50  ((A)->port == (B)->port \
51  && (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
52 
53 #define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
54 
55 #define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)
56 
57 #elif defined(WITH_CONTIKI)
58 
59 #include "uip.h"
60 
61 typedef struct coap_address_t {
62  uip_ipaddr_t addr;
63  uint16_t port;
65 
69 COAP_STATIC_INLINE uint16_t
71  return uip_ntohs(addr->port);
72 }
73 
78 coap_address_set_port(coap_address_t *addr, uint16_t port) {
79  addr->port = uip_htons(port);
80 }
81 
82 #define _coap_address_equals_impl(A,B) \
83  ((A)->port == (B)->port \
84  && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
85 
87 #define _coap_address_isany_impl(A) 0
88 
89 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
90 
91 #else /* WITH_LWIP || WITH_CONTIKI */
92 
94 typedef struct coap_address_t {
95  socklen_t size;
96  union {
97  struct sockaddr sa;
98  struct sockaddr_in sin;
99  struct sockaddr_in6 sin6;
100  } addr;
102 
106 uint16_t coap_address_get_port(const coap_address_t *addr);
107 
111 void coap_address_set_port(coap_address_t *addr, uint16_t port);
112 
118 int coap_address_equals(const coap_address_t *a, const coap_address_t *b);
119 
122  /* need to compare only relevant parts of sockaddr_in6 */
123  switch (a->addr.sa.sa_family) {
124  case AF_INET:
125  return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
126  case AF_INET6:
127  return memcmp(&in6addr_any,
128  &a->addr.sin6.sin6_addr,
129  sizeof(in6addr_any)) == 0;
130  default:
131  ;
132  }
133 
134  return 0;
135 }
136 #endif /* WITH_LWIP || WITH_CONTIKI */
137 
146 
147 /* Convenience function to copy IPv6 addresses without garbage. */
148 
151 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
152  memcpy( dst, src, sizeof( coap_address_t ) );
153 #else
154  memset( dst, 0, sizeof( coap_address_t ) );
155  dst->size = src->size;
156  if ( src->addr.sa.sa_family == AF_INET6 ) {
157  dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
158  dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
159  dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
160  dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
161  } else if ( src->addr.sa.sa_family == AF_INET ) {
162  dst->addr.sin = src->addr.sin;
163  } else {
164  memcpy( &dst->addr, &src->addr, src->size );
165  }
166 #endif
167 }
168 
169 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
177  assert(a); assert(b);
178  return _coap_address_equals_impl(a, b);
179 }
180 #endif
181 
189  assert(a);
190  return _coap_address_isany_impl(a);
191 }
192 
193 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
194 
199 int coap_is_mcast(const coap_address_t *a);
200 #else /* !WITH_LWIP && !WITH_CONTIKI */
206 coap_is_mcast(const coap_address_t *a) {
207  return a && _coap_is_mcast_impl(a);
208 }
209 #endif /* !WITH_LWIP && !WITH_CONTIKI */
210 
211 #endif /* COAP_ADDRESS_H_ */
COAP_STATIC_INLINE int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
Definition: address.h:188
void coap_address_set_port(coap_address_t *addr, uint16_t port)
Set the port field of addr to port (in host byte order).
Definition: address.c:43
COAP_STATIC_INLINE int _coap_address_isany_impl(const coap_address_t *a)
Definition: address.h:121
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:100
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:81
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: address.c:31
struct coap_address_t coap_address_t
multi-purpose address abstraction
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: address.h:150
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:58
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
multi-purpose address abstraction
Definition: address.h:94
socklen_t size
size of addr
Definition: address.h:95
struct sockaddr_in sin
Definition: address.h:98
struct sockaddr_in6 sin6
Definition: address.h:99
struct sockaddr sa
Definition: address.h:97
union coap_address_t::@0 addr