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-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 #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 #endif /* WITH_POSIX */
93 
101 static inline void
103  assert(addr);
104  memset(addr, 0, sizeof(coap_address_t));
105 #ifdef WITH_POSIX
106  /* lwip and Contiki have constant address sizes and doesn't need the .size part */
107  addr->size = sizeof(addr->addr);
108 #endif
109 }
110 
111 #ifndef WITH_POSIX
112 
117 static inline int
119  assert(a); assert(b);
120  return _coap_address_equals_impl(a, b);
121 }
122 #endif
123 
129 static inline int
131  assert(a);
132  return _coap_address_isany_impl(a);
133 }
134 
135 #ifdef WITH_POSIX
136 
140 int coap_is_mcast(const coap_address_t *a);
141 #else /* WITH_POSIX */
142 
146 static inline int
147 coap_is_mcast(const coap_address_t *a) {
148  return a && _coap_is_mcast_impl(a);
149 }
150 #endif /* WITH_POSIX */
151 
152 #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:18
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
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:130
static void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.h:102
union coap_address_t::@0 addr
socklen_t size
size of addr
Definition: address.h:60
struct sockaddr sa
Definition: address.h:62
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:41