libcoap  4.3.0
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  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * This file is part of the CoAP library libcoap. Please see README for terms
9  * of use.
10  */
11 
17 #ifndef COAP_ADDRESS_H_
18 #define COAP_ADDRESS_H_
19 
20 #include <assert.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include "libcoap.h"
25 
26 #if defined(WITH_LWIP)
27 
28 #include <lwip/ip_addr.h>
29 
30 typedef struct coap_address_t {
31  uint16_t port;
32  ip_addr_t addr;
34 
38 COAP_STATIC_INLINE uint16_t
40  return ntohs(addr->port);
41 }
42 
47 coap_address_set_port(coap_address_t *addr, uint16_t port) {
48  addr->port = htons(port);
49 }
50 
51 #define _coap_address_equals_impl(A, B) \
52  ((A)->port == (B)->port \
53  && (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
54 
55 #define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
56 
57 #define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)
58 
59 #elif defined(WITH_CONTIKI)
60 
61 #include "uip.h"
62 
63 typedef struct coap_address_t {
64  uip_ipaddr_t addr;
65  uint16_t port;
67 
71 COAP_STATIC_INLINE uint16_t
73  return uip_ntohs(addr->port);
74 }
75 
80 coap_address_set_port(coap_address_t *addr, uint16_t port) {
81  addr->port = uip_htons(port);
82 }
83 
84 #define _coap_address_equals_impl(A,B) \
85  ((A)->port == (B)->port \
86  && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
87 
89 #define _coap_address_isany_impl(A) 0
90 
91 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
92 
93 #else /* WITH_LWIP || WITH_CONTIKI */
94 
96 typedef struct coap_address_t {
97  socklen_t size;
98  union {
99  struct sockaddr sa;
100  struct sockaddr_in sin;
101  struct sockaddr_in6 sin6;
102  } addr;
104 
108 uint16_t coap_address_get_port(const coap_address_t *addr);
109 
113 void coap_address_set_port(coap_address_t *addr, uint16_t port);
114 
120 int coap_address_equals(const coap_address_t *a, const coap_address_t *b);
121 
124  /* need to compare only relevant parts of sockaddr_in6 */
125  switch (a->addr.sa.sa_family) {
126  case AF_INET:
127  return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
128  case AF_INET6:
129  return memcmp(&in6addr_any,
130  &a->addr.sin6.sin6_addr,
131  sizeof(in6addr_any)) == 0;
132  default:
133  ;
134  }
135 
136  return 0;
137 }
138 #endif /* WITH_LWIP || WITH_CONTIKI */
139 
148 
149 /* Convenience function to copy IPv6 addresses without garbage. */
150 
153 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
154  memcpy( dst, src, sizeof( coap_address_t ) );
155 #else
156  memset( dst, 0, sizeof( coap_address_t ) );
157  dst->size = src->size;
158  if ( src->addr.sa.sa_family == AF_INET6 ) {
159  dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
160  dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
161  dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
162  dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
163  } else if ( src->addr.sa.sa_family == AF_INET ) {
164  dst->addr.sin = src->addr.sin;
165  } else {
166  memcpy( &dst->addr, &src->addr, src->size );
167  }
168 #endif
169 }
170 
171 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
179  assert(a); assert(b);
180  return _coap_address_equals_impl(a, b);
181 }
182 #endif
183 
191  assert(a);
192  return _coap_address_isany_impl(a);
193 }
194 
195 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
196 
201 int coap_is_mcast(const coap_address_t *a);
202 #else /* !WITH_LWIP && !WITH_CONTIKI */
208 coap_is_mcast(const coap_address_t *a) {
209  return a && _coap_is_mcast_impl(a);
210 }
211 #endif /* !WITH_LWIP && !WITH_CONTIKI */
212 
213 #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:190
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:45
COAP_STATIC_INLINE int _coap_address_isany_impl(const coap_address_t *a)
Definition: address.h:123
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:102
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:83
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: address.c:33
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:152
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:60
#define COAP_STATIC_INLINE
Definition: libcoap.h:40
multi-purpose address abstraction
Definition: address.h:96
socklen_t size
size of addr
Definition: address.h:97
struct sockaddr_in sin
Definition: address.h:100
struct sockaddr_in6 sin6
Definition: address.h:101
struct sockaddr sa
Definition: address.h:99
union coap_address_t::@0 addr