libcoap  4.1.2
address.h
Go to the documentation of this file.
1 /*
2  * address.h -- representation of network addresses
3  *
4  * Copyright (C) 2010-2011,2015 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 #ifdef WITH_LWIP
25 #include <lwip/ip_addr.h>
26 
27 typedef struct coap_address_t {
28  uint16_t port;
29  ip_addr_t addr;
31 
32 #define _coap_address_equals_impl(A, B) (!!ip_addr_cmp(&(A)->addr,&(B)->addr))
33 
34 #define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
35 
36 #define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)
37 #endif /* WITH_LWIP */
38 
39 #ifdef WITH_CONTIKI
40 #include "uip.h"
41 
42 typedef struct coap_address_t {
43  uip_ipaddr_t addr;
44  unsigned short port;
46 
47 #define _coap_address_equals_impl(A,B) \
48  ((A)->port == (B)->port \
49  && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
50 
52 #define _coap_address_isany_impl(A) 0
53 
54 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
55 #endif /* WITH_CONTIKI */
56 
57 #ifdef WITH_POSIX
58 
59 typedef struct coap_address_t {
60  socklen_t size;
61  union {
62  struct sockaddr sa;
63  struct sockaddr_storage st;
64  struct sockaddr_in sin;
65  struct sockaddr_in6 sin6;
66  } addr;
68 
74 int coap_address_equals(const coap_address_t *a, const coap_address_t *b);
75 
76 static inline int
78  /* need to compare only relevant parts of sockaddr_in6 */
79  switch (a->addr.sa.sa_family) {
80  case AF_INET:
81  return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
82  case AF_INET6:
83  return memcmp(&in6addr_any,
84  &a->addr.sin6.sin6_addr,
85  sizeof(in6addr_any)) == 0;
86  default:
87  ;
88  }
89 
90  return 0;
91 }
92 
93 static inline int
95  if (!a)
96  return 0;
97 
98  switch (a->addr.sa.sa_family) {
99  case AF_INET:
100  return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
101  case AF_INET6:
102  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
103  default: /* fall through and signal error */
104  ;
105  }
106  return 0;
107 }
108 #endif /* WITH_POSIX */
109 
117 static inline void
119  assert(addr);
120  memset(addr, 0, sizeof(coap_address_t));
121 #ifdef WITH_POSIX
122  /* lwip and Contiki have constant address sizes and doesn't need the .size part */
123  addr->size = sizeof(addr->addr);
124 #endif
125 }
126 
127 #ifndef WITH_POSIX
128 
133 static inline int
135  assert(a); assert(b);
136  return _coap_address_equals_impl(a, b);
137 }
138 #endif
139 
145 static inline int
147  assert(a);
148  return _coap_address_isany_impl(a);
149 }
150 
155 static inline int
157  return a && _coap_is_mcast_impl(a);
158 }
159 
160 #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:17
static int _coap_is_mcast_impl(const coap_address_t *a)
Definition: address.h:94
struct sockaddr_in6 sin6
Definition: address.h:65
struct sockaddr_in sin
Definition: address.h:64
multi-purpose address abstraction
Definition: address.h:59
struct coap_address_t coap_address_t
multi-purpose address abstraction
static int _coap_address_isany_impl(const coap_address_t *a)
Definition: address.h:77
static int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.h:156
struct sockaddr_storage st
Definition: address.h:63
#define assert(...)
Definition: mem.c:17
static int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
Definition: address.h:146
static void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.h:118
union coap_address_t::@0 addr
socklen_t size
size of addr
Definition: address.h:60
struct sockaddr sa
Definition: address.h:62