libcoap  4.2.1
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 "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 
33 #define _coap_address_equals_impl(A, B) \
34  ((A)->port == (B)->port \
35  && (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
36 
37 #define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
38 
39 #define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)
40 
41 #elif defined(WITH_CONTIKI)
42 
43 #include "uip.h"
44 
45 typedef struct coap_address_t {
46  uip_ipaddr_t addr;
47  uint16_t port;
49 
50 #define _coap_address_equals_impl(A,B) \
51  ((A)->port == (B)->port \
52  && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
53 
55 #define _coap_address_isany_impl(A) 0
56 
57 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
58 
59 #else /* WITH_LWIP || WITH_CONTIKI */
60 
62 typedef struct coap_address_t {
63  socklen_t size;
64  union {
65  struct sockaddr sa;
66  struct sockaddr_in sin;
67  struct sockaddr_in6 sin6;
68  } addr;
70 
76 int coap_address_equals(const coap_address_t *a, const coap_address_t *b);
77 
80  /* need to compare only relevant parts of sockaddr_in6 */
81  switch (a->addr.sa.sa_family) {
82  case AF_INET:
83  return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
84  case AF_INET6:
85  return memcmp(&in6addr_any,
86  &a->addr.sin6.sin6_addr,
87  sizeof(in6addr_any)) == 0;
88  default:
89  ;
90  }
91 
92  return 0;
93 }
94 #endif /* WITH_LWIP || WITH_CONTIKI */
95 
104 
105 /* Convenience function to copy IPv6 addresses without garbage. */
106 
109 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
110  memcpy( dst, src, sizeof( coap_address_t ) );
111 #else
112  memset( dst, 0, sizeof( coap_address_t ) );
113  dst->size = src->size;
114  if ( src->addr.sa.sa_family == AF_INET6 ) {
115  dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
116  dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
117  dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
118  dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
119  } else if ( src->addr.sa.sa_family == AF_INET ) {
120  dst->addr.sin = src->addr.sin;
121  } else {
122  memcpy( &dst->addr, &src->addr, src->size );
123  }
124 #endif
125 }
126 
127 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
128 
135  assert(a); assert(b);
136  return _coap_address_equals_impl(a, b);
137 }
138 #endif
139 
147  assert(a);
148  return _coap_address_isany_impl(a);
149 }
150 
151 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
152 
157 int coap_is_mcast(const coap_address_t *a);
158 #else /* !WITH_LWIP && !WITH_CONTIKI */
159 
164 coap_is_mcast(const coap_address_t *a) {
165  return a && _coap_is_mcast_impl(a);
166 }
167 #endif /* !WITH_LWIP && !WITH_CONTIKI */
168 
169 #endif /* COAP_ADDRESS_H_ */
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:31
struct sockaddr_in6 sin6
Definition: address.h:67
struct sockaddr_in sin
Definition: address.h:66
multi-purpose address abstraction
Definition: address.h:62
struct coap_address_t coap_address_t
multi-purpose address abstraction
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:71
COAP_STATIC_INLINE int _coap_address_isany_impl(const coap_address_t *a)
Definition: address.h:79
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
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:146
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: address.h:108
union coap_address_t::@0 addr
socklen_t size
size of addr
Definition: address.h:63
struct sockaddr sa
Definition: address.h:65
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:54