address.h
Go to the documentation of this file.
/*
* address.h -- representation of network addresses
*
* Copyright (C) 2010-2011,2015-2016 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef _COAP_ADDRESS_H_
#define _COAP_ADDRESS_H_
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "libcoap.h"
#ifdef WITH_LWIP
#include <lwip/ip_addr.h>
typedef struct coap_address_t {
uint16_t port;
ip_addr_t addr;
} coap_address_t;
#define _coap_address_equals_impl(A, B) (!!ip_addr_cmp(&(A)->addr,&(B)->addr))
#define _coap_address_isany_impl(A) ip_addr_isany(&(A)->addr)
#define _coap_is_mcast_impl(Address) ip_addr_ismulticast(&(Address)->addr)
#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)))
#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
typedef struct coap_address_t {
socklen_t size;
union {
struct sockaddr sa;
struct sockaddr_storage st;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} addr;
} coap_address_t;
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;
}
#endif /* WITH_POSIX */
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
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
static inline int
coap_address_isany(const coap_address_t *a) {
assert(a);
return _coap_address_isany_impl(a);
}
#ifdef WITH_POSIX
int coap_is_mcast(const coap_address_t *a);
#else /* WITH_POSIX */
static inline int
coap_is_mcast(const coap_address_t *a) {
return a && _coap_is_mcast_impl(a);
}
#endif /* WITH_POSIX */
#endif /* _COAP_ADDRESS_H_ */
coap_address_equals
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
coap_address_t::sin6
struct sockaddr_in6 sin6
Definition: address.h:65
coap_address_t::sin
struct sockaddr_in sin
Definition: address.h:64
coap_address_t
multi-purpose address abstraction
Definition: address.h:59
coap_address_t
struct coap_address_t coap_address_t
multi-purpose address abstraction
_coap_address_isany_impl
static int _coap_address_isany_impl(const coap_address_t *a)
Definition: address.h:77
coap_address_t::st
struct sockaddr_storage st
Definition: address.h:63
assert
#define assert(...)
Definition: mem.c:17
coap_address_isany
static int coap_address_isany(const coap_address_t *a)
Checks if given address object a denotes the wildcard address.
Definition: address.h:130
libcoap.h
coap_address_init
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
coap_address_t::addr
union coap_address_t::@0 addr
coap_address_t::size
socklen_t size
size of addr
Definition: address.h:60
coap_address_t::sa
struct sockaddr sa
Definition: address.h:62
coap_is_mcast
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:41