libcoap  4.2.0
address.c
Go to the documentation of this file.
1 /* address.c -- representation of network addresses
2  *
3  * Copyright (C) 2015-2016,2019 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 #ifdef RIOT_VERSION
31 /* FIXME */
32 #define IN_MULTICAST(Address) (0)
33 #endif /* RIOT_VERSION */
34 
35 int
37  assert(a); assert(b);
38 
39  if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
40  return 0;
41 
42  /* need to compare only relevant parts of sockaddr_in6 */
43  switch (a->addr.sa.sa_family) {
44  case AF_INET:
45  return
46  a->addr.sin.sin_port == b->addr.sin.sin_port &&
47  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
48  sizeof(struct in_addr)) == 0;
49  case AF_INET6:
50  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
51  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
52  sizeof(struct in6_addr)) == 0;
53  default: /* fall through and signal error */
54  ;
55  }
56  return 0;
57 }
58 
60  if (!a)
61  return 0;
62 
63  switch (a->addr.sa.sa_family) {
64  case AF_INET:
65  return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr));
66  case AF_INET6:
67  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr);
68  default: /* fall through and signal error */
69  ;
70  }
71  return 0;
72 }
73 #else /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
74 
75 #ifdef __clang__
76 /* Make compilers happy that do not like empty modules. As this function is
77  * never used, we ignore -Wunused-function at the end of compiling this file
78  */
79 #pragma GCC diagnostic ignored "-Wunused-function"
80 #endif
81 static inline void dummy(void) {
82 }
83 
84 #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:36
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:59
struct sockaddr sa
Definition: address.h:65