libcoap  4.3.0rc1
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 "coap2/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 uint16_t
32  assert(addr != NULL);
33  switch (addr->addr.sa.sa_family) {
34  case AF_INET: return ntohs(addr->addr.sin.sin_port);
35  case AF_INET6: return ntohs(addr->addr.sin6.sin6_port);
36  default: /* undefined */
37  ;
38  }
39  return 0;
40 }
41 
42 void
43 coap_address_set_port(coap_address_t *addr, uint16_t port) {
44  assert(addr != NULL);
45  switch (addr->addr.sa.sa_family) {
46  case AF_INET:
47  addr->addr.sin.sin_port = htons(port);
48  break;
49  case AF_INET6:
50  addr->addr.sin6.sin6_port = htons(port);
51  break;
52  default: /* undefined */
53  ;
54  }
55 }
56 
57 int
59  assert(a); assert(b);
60 
61  if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
62  return 0;
63 
64  /* need to compare only relevant parts of sockaddr_in6 */
65  switch (a->addr.sa.sa_family) {
66  case AF_INET:
67  return
68  a->addr.sin.sin_port == b->addr.sin.sin_port &&
69  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
70  sizeof(struct in_addr)) == 0;
71  case AF_INET6:
72  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
73  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
74  sizeof(struct in6_addr)) == 0;
75  default: /* fall through and signal error */
76  ;
77  }
78  return 0;
79 }
80 
82  if (!a)
83  return 0;
84 
85  switch (a->addr.sa.sa_family) {
86  case AF_INET:
87  return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr));
88  case AF_INET6:
89  return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr) ||
90  (IN6_IS_ADDR_V4MAPPED(&a->addr.sin6.sin6_addr) &&
91  IN_MULTICAST(ntohl(a->addr.sin6.sin6_addr.s6_addr[12])));
92  default: /* fall through and signal error */
93  ;
94  }
95  return 0;
96 }
97 
98 #endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */
99 
101  assert(addr);
102  memset(addr, 0, sizeof(coap_address_t));
103 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
104  /* lwip and Contiki have constant address sizes and don't need the .size part */
105  addr->size = sizeof(addr->addr);
106 #endif
107 }
108 
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:43
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:100
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
Definition: address.c:81
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: address.c:31
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:58
Pulls together all the internal only header files.
multi-purpose address abstraction
Definition: address.h:94
socklen_t size
size of addr
Definition: address.h:95
struct sockaddr_in sin
Definition: address.h:98
struct sockaddr_in6 sin6
Definition: address.h:99
struct sockaddr sa
Definition: address.h:97
union coap_address_t::@0 addr