Files
libcoap/include/coap/address.h
T
Carsten Schoenert 489eb73f0c i/c/*.h: remove obsolete #ifdef HAVE_* constructs
The various variable checks within the public header files are not
useful as such checks doesn't have to be done in these files.

The public header files have only to provide the function declarations
and some macro definitions, any check for the availability of needed
other system header files has to be done by the user on his own.

TODO: The check for syslog.h in debug.h has to be moved out of the
public part into the source of the library later.
2016-01-10 17:49:07 +01:00

165 lines
4.2 KiB
C

/* address.h -- representation of network addresses
*
* Copyright (C) 2010,2011,2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
/**
* @file address.h
* @brief Representation of network addresses
*/
#ifndef _COAP_ADDRESS_H_
#define _COAP_ADDRESS_H_
#include <assert.h>
#include <stdint.h>
#include <string.h>
#ifdef WITH_LWIP
#include <lwip/ip_addr.h>
typedef struct coap_address_t {
uint16_t port;
ip_addr_t addr;
} coap_address_t;
/* FIXME oversimplification: just assuming it's an ipv4 address instead of
* looking up the appropraite lwip function */
#define _coap_address_equals_impl(A, B) \
((A)->addr.addr == (B)->addr.addr \
&& A->port == B->port)
/** @todo implementation of _coap_address_isany_impl() for Contiki */
#define _coap_address_isany_impl(A) 0
/* FIXME sure there is something in lwip */
#define _coap_is_mcast_impl(Address) 0
#endif /* WITH_LWIP */
#ifdef WITH_CONTIKI
#include "uip.h"
typedef struct coap_address_t {
uip_ipaddr_t addr;
unsigned short port;
} coap_address_t;
#define _coap_address_equals_impl(A,B) \
((A)->port == (B)->port \
&& uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
/** @todo implementation of _coap_address_isany_impl() for Contiki */
#define _coap_address_isany_impl(A) 0
#define _coap_is_mcast_impl(Address) uip_is_addr_mcast(&((Address)->addr))
#endif /* WITH_CONTIKI */
#ifdef WITH_POSIX
/** multi-purpose address abstraction */
typedef struct coap_address_t {
socklen_t size; /**< size of addr */
union {
struct sockaddr sa;
struct sockaddr_storage st;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} addr;
} coap_address_t;
/**
* Compares given address objects @p a and @p b. This function returns @c 1 if
* addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be
* @c NULL;
*/
int coap_address_equals(const coap_address_t *a, const coap_address_t *b);
static inline int
_coap_address_isany_impl(const coap_address_t *a) {
/* need to compare only relevant parts of sockaddr_in6 */
switch (a->addr.sa.sa_family) {
case AF_INET:
return a->addr.sin.sin_addr.s_addr == INADDR_ANY;
case AF_INET6:
return memcmp(&in6addr_any,
&a->addr.sin6.sin6_addr,
sizeof(in6addr_any)) == 0;
default:
;
}
return 0;
}
static inline int
_coap_is_mcast_impl(const coap_address_t *a) {
if (!a)
return 0;
switch (a->addr.sa.sa_family) {
case AF_INET:
return IN_MULTICAST(a->addr.sin.sin_addr.s_addr);
case AF_INET6:
return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
default: /* fall through and signal error */
;
}
return 0;
}
#endif /* WITH_POSIX */
/**
* Resets the given coap_address_t object @p addr to its default values. In
* particular, the member size must be initialized to the available size for
* storing addresses.
*
* @param addr The coap_address_t object to initialize.
*/
static inline void
coap_address_init(coap_address_t *addr) {
assert(addr);
memset(addr, 0, sizeof(coap_address_t));
#ifdef WITH_POSIX
/* lwip and Contiki have constant address sizes and doesn't need the .size part */
addr->size = sizeof(addr->addr);
#endif
}
#ifndef WITH_POSIX
/**
* Compares given address objects @p a and @p b. This function returns @c 1 if
* addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be
* @c NULL;
*/
static inline int
coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
assert(a); assert(b);
return _coap_address_equals_impl(a, b);
}
#endif
/**
* Checks if given address object @p a denotes the wildcard address. This
* function returns @c 1 if this is the case, @c 0 otherwise. The parameters @p
* a must not be @c NULL;
*/
static inline int
coap_address_isany(const coap_address_t *a) {
assert(a);
return _coap_address_isany_impl(a);
}
/**
* Checks if given address @p a denotes a multicast address. This function
* returns @c 1 if @p a is multicast, @c 0 otherwise.
*/
static inline int
coap_is_mcast(const coap_address_t *a) {
return a && _coap_is_mcast_impl(a);
}
#endif /* _COAP_ADDRESS_H_ */