address.c
Go to the documentation of this file.
/* address.c -- representation of network addresses
*
* Copyright (C) 2015-2016 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#ifdef WITH_POSIX
#include <assert.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "address.h"
int
coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
assert(a); assert(b);
if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
return 0;
/* need to compare only relevant parts of sockaddr_in6 */
switch (a->addr.sa.sa_family) {
case AF_INET:
return
a->addr.sin.sin_port == b->addr.sin.sin_port &&
memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
sizeof(struct in_addr)) == 0;
case AF_INET6:
return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
sizeof(struct in6_addr)) == 0;
default: /* fall through and signal error */
;
}
return 0;
}
int coap_is_mcast(const coap_address_t *a) {
if (!a)
return 0;
switch (a->addr.sa.sa_family) {
case AF_INET:
return IN_MULTICAST(ntohl(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;
}
#else /* WITH_POSIX */
/* make compilers happy that do not like empty modules */
static inline void dummy()
{
}
#endif /* not WITH_POSIX */
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
assert
#define assert(...)
Definition: mem.c:17
address.h
Representation of network addresses.
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::addr
union coap_address_t::@0 addr
coap_address_t::size
socklen_t size
size of addr
Definition: address.h:60
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
coap_address_t::sa
struct sockaddr sa
Definition: address.h:62