libcoap  4.2.0rc1
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 #include "coap_config.h"
10 
11 #if !defined(WITH_CONTIKI) && !defined(WITH_LWIP)
12 #ifdef HAVE_ASSERT_H
13 #include <assert.h>
14 #endif
15 #ifdef HAVE_ARPA_INET_H
16 #include <arpa/inet.h>
17 #endif
18 #ifdef HAVE_NETINET_IN_H
19 #include <netinet/in.h>
20 #endif
21 #ifdef HAVE_SYS_SOCKET_H
22 #include <sys/socket.h>
23 #endif
24 #ifdef HAVE_WS2TCPIP_H
25 #include <ws2tcpip.h>
26 #endif
27 
28 #include "address.h"
29 
30 int
32  assert(a); assert(b);
33 
34  if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
35  return 0;
36 
37  /* need to compare only relevant parts of sockaddr_in6 */
38  switch (a->addr.sa.sa_family) {
39  case AF_INET:
40  return
41  a->addr.sin.sin_port == b->addr.sin.sin_port &&
42  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
43  sizeof(struct in_addr)) == 0;
44  case AF_INET6:
45  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
46  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
47  sizeof(struct in6_addr)) == 0;
48  default: /* fall through and signal error */
49  ;
50  }
51  return 0;
52 }
53 
55  if (!a)
56  return 0;
57 
58  switch (a->addr.sa.sa_family) {
59  case AF_INET:
60  return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr));
61  case AF_INET6:
62  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
63  default: /* fall through and signal error */
64  ;
65  }
66  return 0;
67 }
68 #else /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
69 
70 #ifdef __clang__
71 /* Make compilers happy that do not like empty modules. As this function is
72  * never used, we ignore -Wunused-function at the end of compiling this file
73  */
74 #pragma GCC diagnostic ignored "-Wunused-function"
75 #endif
76 static inline void dummy(void) {
77 }
78 
79 #endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
struct sockaddr_in6 sin6
Definition: address.h:67
struct sockaddr_in sin
Definition: address.h:66
multi-purpose address abstraction
Definition: address.h:62
#define assert(...)
Definition: mem.c:18
COAP_STATIC_INLINE void dummy(void)
Definition: coap_time.c:132
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:31
union coap_address_t::@0 addr
socklen_t size
size of addr
Definition: address.h:63
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:54
struct sockaddr sa
Definition: address.h:65