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