libcoap  4.3.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  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * This file is part of the CoAP library libcoap. Please see
8  * README for terms of use.
9  */
10 
11 #include "coap3/coap_internal.h"
12 
13 #if !defined(WITH_CONTIKI) && !defined(WITH_LWIP)
14 #ifdef HAVE_ARPA_INET_H
15 #include <arpa/inet.h>
16 #endif
17 #ifdef HAVE_NETINET_IN_H
18 #include <netinet/in.h>
19 #endif
20 #ifdef HAVE_SYS_SOCKET_H
21 #include <sys/socket.h>
22 #endif
23 #ifdef HAVE_WS2TCPIP_H
24 #include <ws2tcpip.h>
25 #endif
26 
27 #ifdef RIOT_VERSION
28 /* FIXME */
29 #define IN_MULTICAST(Address) (0)
30 #endif /* RIOT_VERSION */
31 
32 uint16_t
34  assert(addr != NULL);
35  switch (addr->addr.sa.sa_family) {
36  case AF_INET: return ntohs(addr->addr.sin.sin_port);
37  case AF_INET6: return ntohs(addr->addr.sin6.sin6_port);
38  default: /* undefined */
39  ;
40  }
41  return 0;
42 }
43 
44 void
45 coap_address_set_port(coap_address_t *addr, uint16_t port) {
46  assert(addr != NULL);
47  switch (addr->addr.sa.sa_family) {
48  case AF_INET:
49  addr->addr.sin.sin_port = htons(port);
50  break;
51  case AF_INET6:
52  addr->addr.sin6.sin6_port = htons(port);
53  break;
54  default: /* undefined */
55  ;
56  }
57 }
58 
59 int
61  assert(a); assert(b);
62 
63  if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
64  return 0;
65 
66  /* need to compare only relevant parts of sockaddr_in6 */
67  switch (a->addr.sa.sa_family) {
68  case AF_INET:
69  return
70  a->addr.sin.sin_port == b->addr.sin.sin_port &&
71  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
72  sizeof(struct in_addr)) == 0;
73  case AF_INET6:
74  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
75  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
76  sizeof(struct in6_addr)) == 0;
77  default: /* fall through and signal error */
78  ;
79  }
80  return 0;
81 }
82 
84  if (!a)
85  return 0;
86 
87  switch (a->addr.sa.sa_family) {
88  case AF_INET:
89  return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr));
90  case AF_INET6:
91  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr) ||
92  (IN6_IS_ADDR_V4MAPPED(&a->addr.sin6.sin6_addr) &&
93  IN_MULTICAST(ntohl(a->addr.sin6.sin6_addr.s6_addr[12])));
94  default: /* fall through and signal error */
95  ;
96  }
97  return 0;
98 }
99 
100 #endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
101 
103  assert(addr);
104  memset(addr, 0, sizeof(coap_address_t));
105 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
106  /* lwip and Contiki have constant address sizes and don't need the .size part */
107  addr->size = sizeof(addr->addr);
108 #endif
109 }
110 
void coap_address_set_port(coap_address_t *addr, uint16_t port)
Set the port field of addr to port (in host byte order).
Definition: address.c:45
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:102
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:83
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: address.c:33
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:60
Pulls together all the internal only header files.
multi-purpose address abstraction
Definition: address.h:96
socklen_t size
size of addr
Definition: address.h:97
struct sockaddr_in sin
Definition: address.h:100
struct sockaddr_in6 sin6
Definition: address.h:101
struct sockaddr sa
Definition: address.h:99
union coap_address_t::@0 addr