libcoap 4.3.5-develop-93694e6
Loading...
Searching...
No Matches
coap_strm_riot.c
Go to the documentation of this file.
1/* coap_strm_riot.c -- Default Stream (TCP) network I/O functions for libcoap on RIOT
2 *
3 * Copyright (C) 2019-2026 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
15
17
18#if defined(RIOT_VERSION)
19
20#include "net/gnrc.h"
21#include "net/gnrc/ipv6.h"
22#include "net/gnrc/netreg.h"
23#include "net/sock/tcp.h"
24#include "net/sock/async.h"
25#include "net/sock/async/event.h"
26
27#include "coap3/coap_riot.h"
28
29#define COAP_SELECT_THREAD_FLAG (1U << 4)
30
31int
33 return !COAP_DISABLE_TCP;
34}
35
36#if ! COAP_DISABLE_TCP
37
38/*
39 * strm
40 * return +ve Number of bytes written.
41 * 0 No data written.
42 * -1 Error (error in errno).
43 */
44ssize_t
45coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
46 ssize_t ret;
47
49 ret = sock_tcp_write(&sock->tcp, data, data_len);
50 if (ret < 0) {
51 errno = -ret;
52 ret = -1;
53 if (errno==EAGAIN ||
54#if EAGAIN != EWOULDBLOCK
55 errno == EWOULDBLOCK ||
56#endif
57 errno == EINTR) {
59 return 0;
60 }
61 if (errno == EPIPE || errno == ECONNRESET) {
62 coap_log_info("coap_socket_write: send: %s\n",
64 } else {
65 coap_log_warn("coap_socket_write: send: %s\n",
67 }
68 return -1;
69 }
70 if (ret < (ssize_t)data_len) {
72 }
73 return ret;
74}
75
76/*
77 * strm
78 * return >=0 Number of bytes read.
79 * -1 Error (error in errno).
80 */
81ssize_t
82coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
83 ssize_t ret;
84
85 ret = sock_tcp_read(&sock->tcp, data, data_len, SOCK_NO_TIMEOUT);
86 if (ret == 0) {
87 /* graceful shutdown */
89 errno = ECONNRESET;
90 return -1;
91 } else if (ret < 0) {
92 errno = -ret;
93 ret = -1;
95 if (errno==EAGAIN ||
96#if EAGAIN != EWOULDBLOCK
97 errno == EWOULDBLOCK ||
98#endif
99 errno == EINTR) {
100 return 0;
101 }
102 if (errno != ECONNRESET) {
103 coap_log_warn("coap_socket_read: recv: %s\n",
105 }
106 return -1;
107 }
108 if (ret < (ssize_t)data_len)
110 return ret;
111}
112
113#ifdef MODULE_LWIP_TCP
114static void
115tcp_recv_session_cb(sock_tcp_t *sock, sock_async_flags_t flags, void *arg) {
116 coap_session_t *session = (coap_session_t *)arg;
117
118 (void)sock;
119 if (!(flags & (SOCK_ASYNC_MSG_RECV | SOCK_ASYNC_MSG_SENT)))
120 return;
121
122 if (flags & SOCK_ASYNC_MSG_RECV)
123 session->sock.flags |= COAP_SOCKET_CAN_READ;
124 if (session->context->selecting_thread) {
125 thread_flags_set(session->context->selecting_thread,
126 COAP_SELECT_THREAD_FLAG);
127 }
128}
129#endif /* MODULE_LWIP_TCP */
130
131event_queue_t queue;
132
133#if COAP_CLIENT_SUPPORT
134
135int
137 const coap_address_t *local_if,
138 const coap_address_t *server,
139 int default_port,
140 coap_address_t *local_addr,
141 coap_address_t *remote_addr) {
142 sock_tcp_ep_t local;
143 sock_tcp_ep_t remote;
144 coap_address_t connect_addr;
145 int ret;
146
147 coap_address_copy(&connect_addr, server);
148
150
151 if (local_if && local_if->riot.family) {
152 if (local_if->riot.family != connect_addr.riot.family) {
153 coap_log_warn("coap_socket_connect_tcp1: local address family != "
154 "remote address family\n");
155 goto error;
156 }
157 }
158
159 local.netif = SOCK_ADDR_ANY_NETIF;
160 remote.netif = SOCK_ADDR_ANY_NETIF;
161 switch (connect_addr.riot.family) {
162#if COAP_IPV4_SUPPORT
163 case AF_INET:
164 local.family = AF_INET;
165 local.port = 0;
166 if (local_if) {
167 memcpy(local.addr.ipv4, &local_if->riot.addr.ipv4, sizeof(local.addr.ipv4));
168 local.port = local_if->riot.port;
169 } else {
170 memset(local.addr.ipv4, 0, sizeof(local.addr.ipv4));
171 }
172 remote.family = AF_INET;
173 memcpy(remote.addr.ipv4, &server->riot.addr.ipv4, sizeof(remote.addr.ipv4));
174 if (connect_addr.riot.port == 0)
175 connect_addr.riot.port = default_port;
176 remote.port = connect_addr.riot.port;
177 break;
178#endif /* COAP_IPV4_SUPPORT */
179#if COAP_IPV6_SUPPORT
180 case AF_INET6:
181 local.family = AF_INET6;
182 local.port = 0;
183 if (local_if) {
184 memcpy(local.addr.ipv6, &local_if->riot.addr.ipv6, sizeof(local.addr.ipv6));
185 local.port = local_if->riot.port;
186 } else {
187 memset(local.addr.ipv6, 0, sizeof(local.addr.ipv6));
188 }
189 remote.family = AF_INET6;
190 memcpy(remote.addr.ipv6, &server->riot.addr.ipv6, sizeof(remote.addr.ipv6));
191 if (connect_addr.riot.port == 0)
192 connect_addr.riot.port = default_port;
193 remote.port = connect_addr.riot.port;
194 break;
195#endif /* COAP_IPV6_SUPPORT */
196 default:
197 coap_log_alert("coap_socket_connect_tcp1: unsupported sa_family %d\n",
198 connect_addr.riot.family);
199 goto error;
200 }
201
202 ret = sock_tcp_connect(&sock->tcp, &remote, 0, 0);
203 if (ret < 0) {
204 errno = -ret;
205 ret = -1;
206 coap_log_warn("coap_socket_connect_tcp1: sock_tcp_create: %s (%d)\n",
207 coap_socket_strerror(), connect_addr.riot.family);
208 goto error;
209 }
210 ret = sock_tcp_get_local(&sock->tcp, &local);
211 if (ret != 0) {
212 errno = -ret;
213 ret = -1;
214 coap_log_warn("coap_socket_connect_tcp1: sock_tcp_get_local: %s\n",
216 }
217 memcpy(&local_addr->riot, &local, sizeof(local_addr->riot));
218
219 ret = sock_tcp_get_remote(&sock->tcp, &remote);
220 if (ret != 0) {
221 errno = -ret;
222 ret = -1;
223 coap_log_warn("coap_socket_connect_tcp: sock_tcp_get_remote: %s\n",
225 }
226 memcpy(&remote_addr->riot, &remote, sizeof(remote_addr->riot));
227
228#ifdef MODULE_LWIP_TCP
229 sock_tcp_event_init(&sock->tcp, &queue, tcp_recv_session_cb, sock->session);
230 sock_tcp_set_cb(&sock->tcp, tcp_recv_session_cb, sock->session);
231#endif /* MODULE_LWIP_TCP */
232
234 return 1;
235
236error:
238 return 0;
239}
240
241int
243 coap_address_t *local_addr,
244 coap_address_t *remote_addr) {
245 (void)sock;
246 (void)local_addr;
247 (void)remote_addr;
248
249 return -1;
250}
251#endif /* COAP_CLIENT_SUPPORT */
252
253#if COAP_SERVER_SUPPORT
254
255#define SOCK_QUEUE_LEN (1U)
256
257static sock_tcp_t sock_queue[SOCK_QUEUE_LEN];
258static sock_tcp_queue_t queue;
259
260int
262 const coap_address_t *listen_addr,
263 coap_address_t *bound_addr) {
264 ssize_t ret;
265
266 (void)sock;
267
268 ret = sock_tcp_listen(&queue, &listen_addr->riot, sock_queue, SOCK_QUEUE_LEN, 0);
269 if (ret < 0) {
270 errno = -ret;
271 return 0;
272 }
273
274 coap_address_copy(bound_addr, listen_addr);
275
276 return 1;
277}
278
279int
281 coap_socket_t *new_client,
282 coap_address_t *local_addr,
283 coap_address_t *remote_addr,
284 void *extra) {
285 sock_tcp_t *sock = NULL;
286 ssize_t ret;
287 sock_tcp_ep_t scratch;
288
289 (void)extra;
291 ret = sock_tcp_accept(&queue, &sock, SOCK_NO_TIMEOUT);
292 if (ret < 0) {
293 errno = -ret;
294 return 0;
295 }
296 if (sock == NULL || ret < 0) {
297 coap_log_warn("coap_socket_accept_tcp: accept: %s\n",
299 return 0;
300 }
301 new_client->tcp = *sock;
302
303 ret = sock_tcp_get_remote(&new_client->tcp, &scratch);
304 if (ret < 0) {
305 errno = -ret;
306 return 0;
307 }
308 memcpy(&remote_addr->riot, &scratch, sizeof(remote_addr->riot));
309 ret = sock_tcp_get_local(&new_client->tcp, &scratch);
310 if (ret < 0) {
311 errno = -ret;
312 return 0;
313 }
314 memcpy(&local_addr->riot, &scratch, sizeof(local_addr->riot));
315
316#ifdef MODULE_LWIP_TCP
317 sock_tcp_set_cb(&new_client->tcp, tcp_recv_session_cb, new_client->session);
318#endif /* MODULE_LWIP_TCP */
319 return 1;
320}
321#endif /* COAP_SERVER_SUPPORT */
322
323void
325 if (sock->flags != COAP_SOCKET_EMPTY) {
326 sock_tcp_disconnect(&sock->tcp);
327 }
328 sock->flags = COAP_SOCKET_EMPTY;
329}
330
331#endif /* ! COAP_DISABLE_TCP */
332
333#else /* ! RIOT_VERSION */
334
335#ifdef __clang__
336/* Make compilers happy that do not like empty modules. As this function is
337 * never used, we ignore -Wunused-function at the end of compiling this file
338 */
339#pragma GCC diagnostic ignored "-Wunused-function"
340#endif
341static inline void
342dummy(void) {
343}
344
345#endif /* ! RIOT_VERSION */
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
const char * coap_socket_strerror(void)
Definition coap_io.c:958
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
#define COAP_SOCKET_CONNECTED
the socket is connected
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
Library specific build wrapper for coap_internal.h.
#define NULL
Definition coap_option.h:30
RIOT-specific definitions for libcoap.
static void dummy(void)
#define coap_log_alert(...)
Definition coap_debug.h:90
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Create a new TCP socket and then listen for new incoming TCP sessions.
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
Function interface for data stream receiving off a socket.
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Function interface for data stream sending off a socket.
int coap_socket_connect_tcp1(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Create a new TCP socket and initiate the connection.
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, coap_address_t *local_addr, coap_address_t *remote_addr, void *extra)
Accept a new incoming TCP session.
void coap_socket_strm_close(coap_socket_t *sock)
Function interface to close off a stream socket.
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
Complete the TCP Connection.
int coap_tcp_is_supported(void)
Check whether TCP is available.
Multi-purpose address abstraction.
union coap_address_t::@236157306151077000227147123371042320347205022262 addr
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_socket_t sock
socket object for the session, if any
coap_context_t * context
session's context
coap_session_t * session
Used to determine session owner.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values