libcoap  4.1.2
address.c
Go to the documentation of this file.
1 /* address.c -- representation of network addresses
2  *
3  * Copyright (C) 2015-2016 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 
9 #ifdef WITH_POSIX
10 #include <assert.h>
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
13 #include <sys/socket.h>
14 
15 #include "address.h"
16 
17 int
19  assert(a); assert(b);
20 
21  if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
22  return 0;
23 
24  /* need to compare only relevant parts of sockaddr_in6 */
25  switch (a->addr.sa.sa_family) {
26  case AF_INET:
27  return
28  a->addr.sin.sin_port == b->addr.sin.sin_port &&
29  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
30  sizeof(struct in_addr)) == 0;
31  case AF_INET6:
32  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
33  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
34  sizeof(struct in6_addr)) == 0;
35  default: /* fall through and signal error */
36  ;
37  }
38  return 0;
39 }
40 
42  if (!a)
43  return 0;
44 
45  switch (a->addr.sa.sa_family) {
46  case AF_INET:
47  return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr));
48  case AF_INET6:
49  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
50  default: /* fall through and signal error */
51  ;
52  }
53  return 0;
54 }
55 #else /* WITH_POSIX */
56 
57 /* make compilers happy that do not like empty modules */
58 static inline void dummy()
59 {
60 }
61 
62 #endif /* not WITH_POSIX */
63 
struct sockaddr_in6 sin6
Definition: address.h:65
struct sockaddr_in sin
Definition: address.h:64
multi-purpose address abstraction
Definition: address.h:59
#define assert(...)
Definition: mem.c:17
Representation of network addresses.
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
union coap_address_t::@0 addr
socklen_t size
size of addr
Definition: address.h:60
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:41
struct sockaddr sa
Definition: address.h:62