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
coap_address_get_port(const coap_address_t * addr)33 coap_address_get_port(const coap_address_t *addr) {
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
coap_address_set_port(coap_address_t * addr,uint16_t port)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
coap_address_equals(const coap_address_t * a,const coap_address_t * b)60 coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
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
83 #ifdef COAP_SUPPORT_SOCKET_BROADCAST
coap_is_bcast(const coap_address_t * a)84 int coap_is_bcast(const coap_address_t *a) {
85 if (a == NULL) {
86 return 0;
87 }
88
89 switch (a->addr.sa.sa_family) {
90 case AF_INET:
91 if (a->addr.sin.sin_addr.s_addr == 0xFFFFFFFF) {
92 return 1;
93 } else {
94 return 0;
95 }
96 case AF_INET6:
97 /* not support ipv6 */
98 return 0;
99 default:
100 return 0;
101 }
102 }
103 #endif
104
coap_is_mcast(const coap_address_t * a)105 int coap_is_mcast(const coap_address_t *a) {
106 if (!a)
107 return 0;
108
109 switch (a->addr.sa.sa_family) {
110 case AF_INET:
111 return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr));
112 case AF_INET6:
113 return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr) ||
114 (IN6_IS_ADDR_V4MAPPED(&a->addr.sin6.sin6_addr) &&
115 IN_MULTICAST(ntohl(a->addr.sin6.sin6_addr.s6_addr[12])));
116 default: /* fall through and signal error */
117 ;
118 }
119 return 0;
120 }
121
122 #endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
123
coap_address_init(coap_address_t * addr)124 void coap_address_init(coap_address_t *addr) {
125 assert(addr);
126 memset(addr, 0, sizeof(coap_address_t));
127 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
128 /* lwip and Contiki have constant address sizes and don't need the .size part */
129 addr->size = sizeof(addr->addr);
130 #endif
131 }
132
coap_address_ntop(const coap_address_t * addr,char * dst,int len)133 void coap_address_ntop(const coap_address_t *addr, char *dst, int len) {
134 if ((addr == NULL) || (dst == NULL) || (len < INET6_ADDRSTRLEN)) {
135 return;
136 }
137 #if defined(WITH_LWIP)
138 (void)ipaddr_ntoa_r(&(addr->addr), dst, len);
139 #elif defined(WITH_CONTIKI)
140 /* not supported yet */
141 #else
142 if (addr->addr.sa.sa_family == AF_INET) {
143 const void *addrptr = &addr->addr.sin.sin_addr;
144 (void)inet_ntop(addr->addr.sa.sa_family, addrptr, dst, len);
145 } else {
146 const void *addrptr = &addr->addr.sin6.sin6_addr;
147 (void)inet_ntop(addr->addr.sa.sa_family, addrptr, dst, len);
148 }
149 #endif
150 }
151