libcoap  4.2.1
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_internal.h"
10 
11 #if !defined(WITH_CONTIKI) && !defined(WITH_LWIP)
12 #ifdef HAVE_ARPA_INET_H
13 #include <arpa/inet.h>
14 #endif
15 #ifdef HAVE_NETINET_IN_H
16 #include <netinet/in.h>
17 #endif
18 #ifdef HAVE_SYS_SOCKET_H
19 #include <sys/socket.h>
20 #endif
21 #ifdef HAVE_WS2TCPIP_H
22 #include <ws2tcpip.h>
23 #endif
24 
25 #ifdef RIOT_VERSION
26 /* FIXME */
27 #define IN_MULTICAST(Address) (0)
28 #endif /* RIOT_VERSION */
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 
69 #endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
70 
72  assert(addr);
73  memset(addr, 0, sizeof(coap_address_t));
74 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
75  /* lwip and Contiki have constant address sizes and don't need the .size part */
76  addr->size = sizeof(addr->addr);
77 #endif
78 }
79 
struct sockaddr_in6 sin6
Definition: address.h:67
struct sockaddr_in sin
Definition: address.h:66
multi-purpose address abstraction
Definition: address.h:62
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:71
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
Pulls together all the internal only header files.