libcoap  4.2.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  * 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 
105  assert(addr);
106  memset(addr, 0, sizeof(coap_address_t));
107 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
108  /* lwip and Contiki have constant address sizes and doesn't need the .size part */
109  addr->size = sizeof(addr->addr);
110 #endif
111 }
112 
113 /* Convenience function to copy IPv6 addresses without garbage. */
114 
117 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
118  memcpy( dst, src, sizeof( coap_address_t ) );
119 #else
120  memset( dst, 0, sizeof( coap_address_t ) );
121  dst->size = src->size;
122  if ( src->addr.sa.sa_family == AF_INET6 ) {
123  dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
124  dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
125  dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
126  dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
127  } else if ( src->addr.sa.sa_family == AF_INET ) {
128  dst->addr.sin = src->addr.sin;
129  } else {
130  memcpy( &dst->addr, &src->addr, src->size );
131  }
132 #endif
133 }
134 
135 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
136 
143  assert(a); assert(b);
144  return _coap_address_equals_impl(a, b);
145 }
146 #endif
147 
155  assert(a);
156  return _coap_address_isany_impl(a);
157 }
158 
159 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
160 
165 int coap_is_mcast(const coap_address_t *a);
166 #else /* !WITH_LWIP && !WITH_CONTIKI */
167 
172 coap_is_mcast(const coap_address_t *a) {
173  return a && _coap_is_mcast_impl(a);
174 }
175 #endif /* !WITH_LWIP && !WITH_CONTIKI */
176 
177 #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:36
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
COAP_STATIC_INLINE void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.h:104
#define assert(...)
Definition: mem.c:18
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:154
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: address.h:116
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:59