libcoap 4.3.1
coap_address.h
Go to the documentation of this file.
1/*
2 * coap_address.h -- representation of network addresses
3 *
4 * Copyright (C) 2010-2011,2015-2016,2022 Olaf Bergmann <bergmann@tzi.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see README for terms
9 * of use.
10 */
11
17#ifndef COAP_ADDRESS_H_
18#define COAP_ADDRESS_H_
19
20#include <assert.h>
21#include <stdint.h>
22#include <string.h>
23#include <sys/types.h>
24#include "libcoap.h"
25
26#if defined(WITH_LWIP)
27
28#include <lwip/ip_addr.h>
29
30typedef struct coap_address_t {
31 uint16_t port;
32 ip_addr_t addr;
34
38COAP_STATIC_INLINE uint16_t
40 return ntohs(addr->port);
41}
42
47coap_address_set_port(coap_address_t *addr, uint16_t port) {
48 addr->port = htons(port);
49}
50
51#define _coap_address_equals_impl(A, B) \
52 ((A)->port == (B)->port \
53 && (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
54
55#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
56
57#define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)
58
59#elif defined(WITH_CONTIKI)
60
61#include "uip.h"
62
63typedef struct coap_address_t {
64 uip_ipaddr_t addr;
65 uint16_t port;
67
71COAP_STATIC_INLINE uint16_t
73 return uip_ntohs(addr->port);
74}
75
80coap_address_set_port(coap_address_t *addr, uint16_t port) {
81 addr->port = uip_htons(port);
82}
83
84#define _coap_address_equals_impl(A,B) \
85 ((A)->port == (B)->port \
86 && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
87
89#define _coap_address_isany_impl(A) 0
90
91#define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
92
93#else /* WITH_LWIP || WITH_CONTIKI */
94
96typedef struct coap_address_t {
97 socklen_t size;
98 union {
99 struct sockaddr sa;
100 struct sockaddr_in sin;
101 struct sockaddr_in6 sin6;
104
108uint16_t coap_address_get_port(const coap_address_t *addr);
109
113void coap_address_set_port(coap_address_t *addr, uint16_t port);
114
121
124 /* need to compare only relevant parts of sockaddr_in6 */
125 switch (a->addr.sa.sa_family) {
126 case AF_INET:
127 return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
128 case AF_INET6:
129 return memcmp(&in6addr_any,
130 &a->addr.sin6.sin6_addr,
131 sizeof(in6addr_any)) == 0;
132 default:
133 ;
134 }
135
136 return 0;
137}
138#endif /* WITH_LWIP || WITH_CONTIKI */
139
148
149/* Convenience function to copy IPv6 addresses without garbage. */
150
153#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
154 memcpy( dst, src, sizeof( coap_address_t ) );
155#else
156 memset( dst, 0, sizeof( coap_address_t ) );
157 dst->size = src->size;
158 if ( src->addr.sa.sa_family == AF_INET6 ) {
159 dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family;
160 dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr;
161 dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port;
162 dst->addr.sin6.sin6_scope_id = src->addr.sin6.sin6_scope_id;
163 } else if ( src->addr.sa.sa_family == AF_INET ) {
164 dst->addr.sin = src->addr.sin;
165 } else {
166 memcpy( &dst->addr, &src->addr, src->size );
167 }
168#endif
169}
170
171#if defined(WITH_LWIP) || defined(WITH_CONTIKI)
179 assert(a); assert(b);
180 return _coap_address_equals_impl(a, b);
181}
182#endif
183
191 assert(a);
192 return _coap_address_isany_impl(a);
193}
194
195#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
196
201int coap_is_mcast(const coap_address_t *a);
202#else /* !WITH_LWIP && !WITH_CONTIKI */
209 return a && _coap_is_mcast_impl(a);
210}
211#endif /* !WITH_LWIP && !WITH_CONTIKI */
212
213#endif /* COAP_ADDRESS_H_ */
COAP_STATIC_INLINE int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
Definition: coap_address.h:190
void coap_address_set_port(coap_address_t *addr, uint16_t port)
Set the port field of addr to port (in host byte order).
Definition: coap_address.c:50
COAP_STATIC_INLINE int _coap_address_isany_impl(const coap_address_t *a)
Definition: coap_address.h:123
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: coap_address.c:107
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: coap_address.c:88
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: coap_address.c:38
struct coap_address_t coap_address_t
multi-purpose address abstraction
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: coap_address.h:152
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: coap_address.c:65
Platform specific header file for CoAP stack.
#define COAP_STATIC_INLINE
Definition: libcoap.h:45
multi-purpose address abstraction
Definition: coap_address.h:96
socklen_t size
size of addr
Definition: coap_address.h:97
struct sockaddr_in sin
Definition: coap_address.h:100
struct sockaddr_in6 sin6
Definition: coap_address.h:101
struct sockaddr sa
Definition: coap_address.h:99
union coap_address_t::@0 addr