libcoap  4.0.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
address.h
Go to the documentation of this file.
1 /* address.h -- representation of network addresses
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
14 #ifndef _COAP_ADDRESS_H_
15 #define _COAP_ADDRESS_H_
16 
17 #include "config.h"
18 
19 #ifdef HAVE_ASSERT_H
20 #include <assert.h>
21 #else
22 #ifndef assert
23 #warning "assertions are disabled"
24 # define assert(x)
25 #endif
26 #endif
27 
28 #include <string.h>
29 
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 
34 #ifdef HAVE_NETINET_IN_H
35 #include <sys/socket.h>
36 #endif
37 
38 #ifdef WITH_CONTIKI
39 #include "uip.h"
40 
41 typedef struct __coap_address_t {
42  unsigned char size;
43  uip_ipaddr_t addr;
44  unsigned short port;
46 
47 #define coap_address_t __coap_address_t
48 
49 #define _coap_address_equals_impl(A,B) \
50  ((A)->size == (B)->size \
51  && (A)->port == (B)->port \
52  && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
53 
54 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
55 #endif /* WITH_CONTIKI */
56 
58 #ifndef coap_address_t
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 
69 #define coap_address_t __coap_address_t
70 
71 static inline int
73  const coap_address_t *b) {
74  if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
75  return 0;
76 
77  /* need to compare only relevant parts of sockaddr_in6 */
78  switch (a->addr.sa.sa_family) {
79  case AF_INET:
80  return
81  a->addr.sin.sin_port == b->addr.sin.sin_port &&
82  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
83  sizeof(struct in_addr)) == 0;
84  case AF_INET6:
85  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
86  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
87  sizeof(struct in6_addr)) == 0;
88  default: /* fall through and signal error */
89  ;
90  }
91  return 0;
92 }
93 
94 static inline int
96  if (!a)
97  return 0;
98 
99  switch (a->addr.sa.sa_family) {
100  case AF_INET:
101  return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
102 case AF_INET6:
103  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
104  default: /* fall through and signal error */
105  ;
106  }
107  return 0;
108 }
109 #endif /* coap_address_t */
110 
118 static inline void
120  assert(addr);
121  memset(addr, 0, sizeof(coap_address_t));
122  addr->size = sizeof(addr->addr);
123 }
124 
130 static inline int
132  assert(a); assert(b);
133  return _coap_address_equals_impl(a, b);
134 }
135 
140 static inline int
142  return a && _coap_is_mcast_impl(a);
143 }
144 
145 #endif /* _COAP_ADDRESS_H_ */