libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs 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 #include <stdint.h>
30 
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
33 #endif
34 
35 #ifdef HAVE_NETINET_IN_H
36 #include <sys/socket.h>
37 #endif
38 
39 #ifdef WITH_LWIP
40 #include <lwip/ip_addr.h>
41 
42 typedef struct coap_address_t {
43  uint16_t port;
44  ip_addr_t addr;
45 } coap_address_t;
46 
47 /* FIXME oversimplification: just assuming it's an ipv4 address instead of
48  * looking up the appropraite lwip function */
49 
50 #define _coap_address_equals_impl(A, B) ((A)->addr.addr == (B)->addr.addr && A->port == B->port)
51 
52 /* FIXME sure there is something in lwip */
53 
54 #define _coap_is_mcast_impl(Address) 0
55 
56 #endif /* WITH_LWIP */
57 #ifdef WITH_CONTIKI
58 #include "uip.h"
59 
60 typedef struct coap_address_t {
61  unsigned char size;
62  uip_ipaddr_t addr;
63  unsigned short port;
64 } coap_address_t;
65 
66 #define _coap_address_equals_impl(A,B) \
67  ((A)->size == (B)->size \
68  && (A)->port == (B)->port \
69  && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
70 
71 #define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
72 #endif /* WITH_CONTIKI */
73 #ifdef WITH_POSIX
74 
76 typedef struct coap_address_t {
77  socklen_t size;
78  union {
79  struct sockaddr sa;
80  struct sockaddr_storage st;
81  struct sockaddr_in sin;
82  struct sockaddr_in6 sin6;
83  } addr;
84 } coap_address_t;
85 
86 static inline int
87 _coap_address_equals_impl(const coap_address_t *a,
88  const coap_address_t *b) {
89  if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
90  return 0;
91 
92  /* need to compare only relevant parts of sockaddr_in6 */
93  switch (a->addr.sa.sa_family) {
94  case AF_INET:
95  return
96  a->addr.sin.sin_port == b->addr.sin.sin_port &&
97  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
98  sizeof(struct in_addr)) == 0;
99  case AF_INET6:
100  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
101  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
102  sizeof(struct in6_addr)) == 0;
103  default: /* fall through and signal error */
104  ;
105  }
106  return 0;
107 }
108 
109 static inline int
110 _coap_is_mcast_impl(const coap_address_t *a) {
111  if (!a)
112  return 0;
113 
114  switch (a->addr.sa.sa_family) {
115  case AF_INET:
116  return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
117 case AF_INET6:
118  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
119  default: /* fall through and signal error */
120  ;
121  }
122  return 0;
123 }
124 #endif /* WITH_POSIX */
125 
133 static inline void
134 coap_address_init(coap_address_t *addr) {
135  assert(addr);
136  memset(addr, 0, sizeof(coap_address_t));
137 #ifndef WITH_LWIP
138  /* lwip has constandt address sizes and doesn't need the .size part */
139  addr->size = sizeof(addr->addr);
140 #endif
141 }
142 
148 static inline int
149 coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
150  assert(a); assert(b);
151  return _coap_address_equals_impl(a, b);
152 }
153 
158 static inline int
159 coap_is_mcast(const coap_address_t *a) {
160  return a && _coap_is_mcast_impl(a);
161 }
162 
163 #endif /* _COAP_ADDRESS_H_ */
static int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.h:159
static void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.h:134
static int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.h:149