libcoap 4.3.5-develop-08ae1a7
Loading...
Searching...
No Matches
coap_io.c
Go to the documentation of this file.
1/* coap_io.c -- Default network I/O functions for libcoap
2 *
3 * Copyright (C) 2012,2014,2016-2025 Olaf Bergmann <bergmann@tzi.org> and others
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
17
18#ifdef HAVE_STDIO_H
19# include <stdio.h>
20#endif
21#ifdef HAVE_UNISTD_H
22# include <unistd.h>
23#endif
24
25#ifndef __ZEPHYR__
26#ifdef HAVE_SYS_SELECT_H
27# include <sys/select.h>
28#endif
29#ifdef HAVE_SYS_SOCKET_H
30# include <sys/socket.h>
31#endif
32#ifdef HAVE_SYS_IOCTL_H
33#include <sys/ioctl.h>
34#endif
35#ifdef HAVE_NETINET_IN_H
36# include <netinet/in.h>
37#endif
38#ifdef HAVE_SYS_UIO_H
39# include <sys/uio.h>
40#endif
41#ifdef _WIN32
42#include <stdio.h>
43#endif /* _WIN32 */
44#ifdef COAP_EPOLL_SUPPORT
45#include <sys/epoll.h>
46#include <sys/timerfd.h>
47#ifdef HAVE_LIMITS_H
48#include <limits.h>
49#endif
50#endif /* COAP_EPOLL_SUPPORT */
51#else /* __ZEPHYR__ */
52#include <sys/ioctl.h>
53#include <sys/select.h>
54#endif /* __ZEPHYR__ */
55
56#if COAP_SERVER_SUPPORT
60}
61
62void
65}
66#endif /* COAP_SERVER_SUPPORT */
67
68#ifndef WITH_CONTIKI
69void
71#if COAP_EPOLL_SUPPORT
72 if (context->eptimerfd != -1) {
73 coap_tick_t now;
74
75 coap_ticks(&now);
76 if (context->next_timeout == 0 || context->next_timeout > now + delay) {
77 struct itimerspec new_value;
78 int ret;
79
80 context->next_timeout = now + delay;
81 memset(&new_value, 0, sizeof(new_value));
82 if (delay == 0) {
83 new_value.it_value.tv_nsec = 1; /* small but not zero */
84 } else {
85 new_value.it_value.tv_sec = delay / COAP_TICKS_PER_SECOND;
86 new_value.it_value.tv_nsec = (delay % COAP_TICKS_PER_SECOND) *
87 1000000;
88 }
89 ret = timerfd_settime(context->eptimerfd, 0, &new_value, NULL);
90 if (ret == -1) {
91 coap_log_err("%s: timerfd_settime failed: %s (%d)\n",
92 "coap_update_io_timer",
93 coap_socket_strerror(), errno);
94 }
95#ifdef COAP_DEBUG_WAKEUP_TIMES
96 else {
97 coap_log_debug("****** Next wakeup time %3ld.%09ld\n",
98 new_value.it_value.tv_sec, new_value.it_value.tv_nsec);
99 }
100#endif /* COAP_DEBUG_WAKEUP_TIMES */
101 }
102 }
103#else /* ! COAP_EPOLL_SUPPORT */
104 coap_tick_t now;
105
106 coap_ticks(&now);
107 if (context->next_timeout == 0 || context->next_timeout > now + delay) {
108 context->next_timeout = now + delay;
109 }
110#endif /* ! COAP_EPOLL_SUPPORT */
111}
112#endif /* ! WITH_CONTIKI */
113
114#ifdef _WIN32
115void
116coap_win_error_to_errno(void) {
117 int w_error = WSAGetLastError();
118 switch (w_error) {
119 case WSA_NOT_ENOUGH_MEMORY:
120 errno = ENOMEM;
121 break;
122 case WSA_INVALID_PARAMETER:
123 errno = EINVAL;
124 break;
125 case WSAEINTR:
126 errno = EINTR;
127 break;
128 case WSAEBADF:
129 errno = EBADF;
130 break;
131 case WSAEACCES:
132 errno = EACCES;
133 break;
134 case WSAEFAULT:
135 errno = EFAULT;
136 break;
137 case WSAEINVAL:
138 errno = EINVAL;
139 break;
140 case WSAEMFILE:
141 errno = EMFILE;
142 break;
143 case WSAEWOULDBLOCK:
144 errno = EWOULDBLOCK;
145 break;
146 case WSAENETDOWN:
147 errno = ENETDOWN;
148 break;
149 case WSAENETUNREACH:
150 errno = ENETUNREACH;
151 break;
152 case WSAENETRESET:
153 errno = ENETRESET;
154 break;
155 case WSAECONNABORTED:
156 errno = ECONNABORTED;
157 break;
158 case WSAECONNRESET:
159 errno = ECONNRESET;
160 break;
161 case WSAENOBUFS:
162 errno = ENOBUFS;
163 break;
164 case WSAETIMEDOUT:
165 errno = ETIMEDOUT;
166 break;
167 case WSAECONNREFUSED:
168 errno = ECONNREFUSED;
169 break;
170 case WSAEADDRNOTAVAIL:
171 errno = EADDRNOTAVAIL;
172 break;
173 default:
174 coap_log_err("WSAGetLastError: %d mapping to errno failed - please fix\n",
175 w_error);
176 errno = EPERM;
177 break;
178 }
179}
180#endif /* _WIN32 */
181
182#if !defined(WITH_LWIP) && !defined(__ZEPHYR__)
183#if (!defined(WITH_CONTIKI)) != ( defined(HAVE_NETINET_IN_H) || defined(HAVE_WS2TCPIP_H) )
184/* define struct in6_pktinfo and struct in_pktinfo if not available
185 FIXME: check with configure
186*/
187#if !defined(__MINGW32__) && !defined(RIOT_VERSION)
188struct in6_pktinfo {
189 struct in6_addr ipi6_addr; /* src/dst IPv6 address */
190 unsigned int ipi6_ifindex; /* send/recv interface index */
191};
192
193struct in_pktinfo {
194 int ipi_ifindex;
195 struct in_addr ipi_spec_dst;
196 struct in_addr ipi_addr;
197};
198#endif /* ! __MINGW32__ && ! RIOT_VERSION */
199#endif
200#endif /* ! WITH_LWIP && ! __ZEPHYR__ */
201
202void
203coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length) {
204 *address = packet->payload;
205 *length = packet->length;
206}
207
208COAP_API unsigned int
210 unsigned int ret;
211
212 coap_lock_lock(return 0);
213 ret = coap_io_prepare_epoll_lkd(ctx, now);
215 return ret;
216}
217
218unsigned int
220#ifndef COAP_EPOLL_SUPPORT
221 (void)ctx;
222 (void)now;
223 coap_log_emerg("coap_io_prepare_epoll() requires libcoap compiled for using epoll\n");
224 return 0;
225#else /* COAP_EPOLL_SUPPORT */
226 coap_socket_t *sockets[1];
227 unsigned int max_sockets = sizeof(sockets)/sizeof(sockets[0]);
228 unsigned int num_sockets;
229 unsigned int timeout;
230
232 /* Use the common logic */
233 timeout = coap_io_prepare_io_lkd(ctx, sockets, max_sockets, &num_sockets, now);
234 /* Save when the next expected I/O is to take place */
235 ctx->next_timeout = timeout ? now + timeout : 0;
236 if (ctx->eptimerfd != -1) {
237 struct itimerspec new_value;
238 int ret;
239
240 memset(&new_value, 0, sizeof(new_value));
241 coap_ticks(&now);
242 if (ctx->next_timeout != 0 && ctx->next_timeout > now) {
243 coap_tick_t rem_timeout = ctx->next_timeout - now;
244 /* Need to trigger an event on ctx->eptimerfd in the future */
245 new_value.it_value.tv_sec = rem_timeout / COAP_TICKS_PER_SECOND;
246 new_value.it_value.tv_nsec = (rem_timeout % COAP_TICKS_PER_SECOND) *
247 1000000;
248 }
249#ifdef COAP_DEBUG_WAKEUP_TIMES
250 coap_log_debug("****** Next wakeup time %3ld.%09ld\n",
251 new_value.it_value.tv_sec, new_value.it_value.tv_nsec);
252#endif /* COAP_DEBUG_WAKEUP_TIMES */
253 /* reset, or specify a future time for eptimerfd to trigger */
254 ret = timerfd_settime(ctx->eptimerfd, 0, &new_value, NULL);
255 if (ret == -1) {
256 coap_log_err("%s: timerfd_settime failed: %s (%d)\n",
257 "coap_io_prepare_epoll",
258 coap_socket_strerror(), errno);
259 }
260 }
261 return timeout;
262#endif /* COAP_EPOLL_SUPPORT */
263}
264
265/*
266 * return 0 No i/o pending
267 * +ve millisecs to next i/o activity
268 */
269COAP_API unsigned int
271 coap_socket_t *sockets[],
272 unsigned int max_sockets,
273 unsigned int *num_sockets,
274 coap_tick_t now) {
275 unsigned int ret;
276
277 coap_lock_lock(return 0);
278 ret = coap_io_prepare_io_lkd(ctx, sockets, max_sockets, num_sockets, now);
280 return ret;
281}
282
283#if COAP_CLIENT_SUPPORT
284static coap_tick_t
285coap_check_need_reconnect(coap_context_t *ctx, coap_session_t *s, coap_tick_t now,
286 coap_tick_t timeout) {
287 coap_tick_t s_timeout;
288
289 if (s->client_initiated && s->session_failed && ctx->reconnect_time) {
290 if (s->last_rx_tx + ctx->reconnect_time * COAP_TICKS_PER_SECOND <= now) {
291 if (!coap_session_reconnect(s)) {
292 /* server is not back up yet - delay retry a while */
293 s->last_rx_tx = now;
294 s_timeout = ctx->reconnect_time * COAP_TICKS_PER_SECOND;
295 if (timeout == 0 || s_timeout < timeout)
296 timeout = s_timeout;
297 }
298 } else {
299 /* Always positive due to if() above */
300 s_timeout = (s->last_rx_tx + ctx->reconnect_time * COAP_TICKS_PER_SECOND) - now;
301 if (s_timeout < timeout)
302 timeout = s_timeout;
303 }
304 }
305 return timeout;
306}
307
308void
310 if (session->doing_first) {
311 session->doing_first = 0;
312 while (!session->doing_first && session->doing_first_pdu) {
313 coap_pdu_t *k_pdu = session->doing_first_pdu;
314
315 LL_DELETE(session->doing_first_pdu, session->doing_first_pdu);
316 coap_log_debug("** %s: mid=0x%04x: released\n",
317 coap_session_str(session), k_pdu->mid);
318 if (coap_send_lkd(session, k_pdu) == COAP_INVALID_MID) {
320 }
321 }
322 }
323}
324#endif /* COAP_CLIENT_SUPPORT */
325
326/*
327 * return 0 No i/o pending
328 * +ve millisecs to next i/o activity
329 */
330unsigned int
332 coap_socket_t *sockets[],
333 unsigned int max_sockets,
334 unsigned int *num_sockets,
335 coap_tick_t now) {
336 coap_queue_t *nextpdu;
337 coap_session_t *s, *stmp;
339 coap_tick_t s_timeout;
340#if COAP_SERVER_SUPPORT
341 int check_dtls_timeouts = 0;
342#endif /* COAP_SERVER_SUPPORT */
343#if defined(COAP_EPOLL_SUPPORT) || defined(WITH_LWIP) || defined(RIOT_VERSION)
344 (void)sockets;
345 (void)max_sockets;
346#endif /* COAP_EPOLL_SUPPORT || WITH_LWIP || RIOT_VERSION*/
347
349 *num_sockets = 0;
350
351#if COAP_SERVER_SUPPORT
352 /* Check to see if we need to send off any Observe requests */
354
355#if COAP_ASYNC_SUPPORT
356 /* Check to see if we need to send off any Async requests */
357 if (coap_check_async(ctx, now, &s_timeout)) {
358 if (s_timeout < timeout)
359 timeout = s_timeout;
360 }
361#endif /* COAP_ASYNC_SUPPORT */
362#endif /* COAP_SERVER_SUPPORT */
363
364 /* Check to see if we need to send off any retransmit request */
365 nextpdu = coap_peek_next(ctx);
366 while (nextpdu && now >= ctx->sendqueue_basetime &&
367 nextpdu->t <= now - ctx->sendqueue_basetime) {
369 nextpdu = coap_peek_next(ctx);
370 }
371 if (nextpdu && now >= ctx->sendqueue_basetime &&
372 (nextpdu->t - (now - ctx->sendqueue_basetime) < timeout))
373 timeout = nextpdu->t - (now - ctx->sendqueue_basetime);
374
375 /* Check for DTLS timeouts */
376 if (ctx->dtls_context) {
379 if (tls_timeout > 0) {
380 if (tls_timeout < now + COAP_TICKS_PER_SECOND / 10)
381 tls_timeout = now + COAP_TICKS_PER_SECOND / 10;
382 coap_log_debug("** DTLS global timeout set to %dms\n",
383 (int)((tls_timeout - now) * 1000 / COAP_TICKS_PER_SECOND));
384 if (tls_timeout - now < timeout)
385 timeout = tls_timeout - now;
386 }
387#if COAP_SERVER_SUPPORT
388 } else {
389 check_dtls_timeouts = 1;
390#endif /* COAP_SERVER_SUPPORT */
391 }
392 }
393#if COAP_PROXY_SUPPORT
394 if (coap_proxy_check_timeouts(ctx, now, &s_timeout)) {
395 if (s_timeout < timeout)
396 timeout = s_timeout;
397 }
398#endif /* COAP_PROXY_SUPPORT */
399#if COAP_SERVER_SUPPORT
400 coap_endpoint_t *ep;
401 coap_tick_t session_timeout;
402
403 if (ctx->session_timeout > 0)
404 session_timeout = ctx->session_timeout * COAP_TICKS_PER_SECOND;
405 else
407
408 LL_FOREACH(ctx->endpoint, ep) {
409#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
411 if (*num_sockets < max_sockets)
412 sockets[(*num_sockets)++] = &ep->sock;
413 }
414#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
415 SESSIONS_ITER_SAFE(ep->sessions, s, stmp) {
416 /* Check whether any idle server sessions should be released */
417 if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 &&
418 s->delayqueue == NULL &&
420 !(s->client_initiated && ctx->reconnect_time) &&
421#endif /* COAP_CLIENT_SUPPORT */
422 (s->last_rx_tx + session_timeout <= now ||
426 continue;
427 } else {
428 if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 &&
429 s->delayqueue == NULL && s->last_rx_tx + session_timeout <= now) {
430 /* Has to be positive based on if() above */
431 s_timeout = (s->last_rx_tx + session_timeout) - now;
432 if (s_timeout < timeout)
433 timeout = s_timeout;
434 }
435 /* Make sure the session object is not deleted in any callbacks */
437 /* Check any DTLS timeouts and expire if appropriate */
438 if (check_dtls_timeouts && s->state == COAP_SESSION_STATE_HANDSHAKE &&
439 s->proto == COAP_PROTO_DTLS && s->tls) {
440 coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
441 while (tls_timeout > 0 && tls_timeout <= now) {
442 coap_log_debug("** %s: DTLS retransmit timeout\n",
445 goto release_1;
446
447 if (s->tls)
448 tls_timeout = coap_dtls_get_timeout(s, now);
449 else {
450 tls_timeout = 0;
451 timeout = 1;
452 }
453 }
454 if (tls_timeout > 0 && tls_timeout - now < timeout)
455 timeout = tls_timeout - now;
456 }
457 /* Check if any server large receives are missing blocks */
458 if (s->lg_srcv) {
459 if (coap_block_check_lg_srcv_timeouts(s, now, &s_timeout)) {
460 if (s_timeout < timeout)
461 timeout = s_timeout;
462 }
463 }
464 /* Check if any server large sending have timed out */
465 if (s->lg_xmit) {
466 if (coap_block_check_lg_xmit_timeouts(s, now, &s_timeout)) {
467 if (s_timeout < timeout)
468 timeout = s_timeout;
469 }
470 }
471#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
473 if (*num_sockets < max_sockets && !(s->sock.flags & COAP_SOCKET_SLAVE))
474 sockets[(*num_sockets)++] = &s->sock;
475 }
476#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
477#if COAP_Q_BLOCK_SUPPORT
478 /*
479 * Check if any server large transmits have hit MAX_PAYLOAD and need
480 * restarting
481 */
482 if (s->lg_xmit) {
483 if (coap_block_check_q_block2_xmit(s, now, &s_timeout)) {
484 if (s_timeout < timeout)
485 timeout = s_timeout;
486 }
487 }
488#endif /* COAP_Q_BLOCK_SUPPORT */
489release_1:
491 }
492 if (s->type == COAP_SESSION_TYPE_SERVER &&
496 || (s->client_initiated && ctx->reconnect_time)
497#endif /* COAP_CLIENT_SUPPORT */
498 ) &&
499 !s->con_active && ctx->ping_timeout > 0) {
500 /* Only do this if this session is observing or reconnecting */
501 if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) {
502 /* Time to send a ping */
503 coap_mid_t mid;
504
506 /* Some issue - not safe to continue processing */
507 s->last_rx_tx = now;
508#if COAP_CLIENT_SUPPORT
509 if (s->client_initiated && ctx->reconnect_time) {
511 }
512#endif /* COAP_CLIENT_SUPPORT */
513 continue;
514 }
515 s->last_ping_mid = mid;
516 if (s->last_ping > 0 && s->last_pong < s->last_ping) {
517#if COAP_CLIENT_SUPPORT
518 if (s->client_initiated && ctx->reconnect_time) {
520 } else {
521#endif /* COAP_CLIENT_SUPPORT */
523#if COAP_CLIENT_SUPPORT
524 }
525#endif /* COAP_CLIENT_SUPPORT */
526 /* check the next session */
527 continue;
528 }
529 s->last_rx_tx = now;
530 s->last_ping = now;
531 } else {
532 /* Always positive due to if() above */
533 s_timeout = (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND) - now;
534 if (s_timeout < timeout)
535 timeout = s_timeout;
536 }
537 }
538#if COAP_CLIENT_SUPPORT
539 timeout = coap_check_need_reconnect(ctx, s, now, timeout);
540#endif /* COAP_CLIENT_SUPPORT */
541 }
542 }
543#endif /* COAP_SERVER_SUPPORT */
544#if COAP_CLIENT_SUPPORT
545 SESSIONS_ITER_SAFE(ctx->sessions, s, stmp) {
546 if (s->type == COAP_SESSION_TYPE_CLIENT &&
548 ctx->ping_timeout > 0) {
549 if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) {
550 /* Time to send a ping */
551 coap_mid_t mid;
552
554 /* Some issue - not safe to continue processing */
555 s->last_rx_tx = now;
557 continue;
558 }
559 s->last_ping_mid = mid;
560 if (s->last_ping > 0 && s->last_pong < s->last_ping) {
562 }
563 s->last_rx_tx = now;
564 s->last_ping = now;
565 } else {
566 /* Always positive due to if() above */
567 s_timeout = (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND) - now;
568 if (s_timeout < timeout)
569 timeout = s_timeout;
570 }
571 }
572 timeout = coap_check_need_reconnect(ctx, s, now, timeout);
573
574#if !COAP_DISABLE_TCP
576 s->state == COAP_SESSION_STATE_CSM && ctx->csm_timeout_ms > 0) {
577 if (s->csm_tx == 0) {
578 s->csm_tx = now;
579 s_timeout = (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000;
580 } else if (s->csm_tx + (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000 <= now) {
581 /* timed out - cannot handle 0, so has to be just +ve */
582 s_timeout = 1;
583 } else {
584 s_timeout = (s->csm_tx + (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000) - now;
585 }
586 if (s_timeout < timeout)
587 timeout = s_timeout;
588 }
589#endif /* !COAP_DISABLE_TCP */
590
591 /* Make sure the session object is not deleted in any callbacks */
593 /* Check any DTLS timeouts and expire if appropriate */
595 s->proto == COAP_PROTO_DTLS && s->tls) {
596 coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
597 while (tls_timeout > 0 && tls_timeout <= now) {
598 coap_log_debug("** %s: DTLS retransmit timeout\n", coap_session_str(s));
600 goto release_2;
601
602 if (s->tls)
603 tls_timeout = coap_dtls_get_timeout(s, now);
604 else {
605 tls_timeout = 0;
606 timeout = 1;
607 }
608 }
609 if (tls_timeout > 0 && tls_timeout - now < timeout)
610 timeout = tls_timeout - now;
611 }
612
613 /* Check if any client large receives are missing blocks */
614 if (s->lg_crcv) {
615 if (coap_block_check_lg_crcv_timeouts(s, now, &s_timeout)) {
616 if (s_timeout < timeout)
617 timeout = s_timeout;
618 }
619 }
620 /* Check if any client large sending have timed out */
621 if (s->lg_xmit) {
622 if (coap_block_check_lg_xmit_timeouts(s, now, &s_timeout)) {
623 if (s_timeout < timeout)
624 timeout = s_timeout;
625 }
626 }
627 if (s->doing_first && s->doing_first_timeout) {
628 if ((s->doing_first_timeout + 5 * COAP_TICKS_PER_SECOND) > now) {
629 s_timeout = s->doing_first_timeout + 5 * COAP_TICKS_PER_SECOND - now;
630 if (s_timeout < timeout)
631 timeout = s_timeout;
632 } else {
633 /* Session set up has failed */
634 coap_queue_t *sent;
635 coap_queue_t *p = NULL;
636 coap_queue_t *q;
637 coap_queue_t *tmp;
638
639 if (s->state == COAP_SESSION_STATE_CSM) {
640 coap_log_debug("** %s: timeout waiting for CSM response\n",
642 s->csm_not_seen = 1;
643 } else {
644 coap_log_debug("** %s: timeout waiting for first response\n",
646 }
647#if COAP_Q_BLOCK_SUPPORT
648 /* Check if testing for Q-Block */
649 if (s->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
650 coap_log_debug("Q-Block support assumed not available\n");
651 set_block_mode_drop_q(s->block_mode);
652 }
653#endif /* COAP_Q_BLOCK_SUPPORT */
654 /* Check if testing for Extended Token */
658 coap_log_debug("Extended Token support assumed not available\n");
659 }
660 /*
661 * Remove the test packet to stop re-transmission.
662 * The test packet may be in the delayqueue ((D)TLS has not been set up),
663 * or in the sendqueue if sent and is pending re-transmission.
664 */
665 LL_FOREACH_SAFE(s->delayqueue, q, tmp) {
666 if (q->pdu->mid == s->remote_test_mid) {
667 if (q->pdu->type==COAP_MESSAGE_CON) {
668 coap_handle_nack(s, q->pdu,
669 s->proto == COAP_PROTO_DTLS ?
671 q->id);
672 }
673 coap_log_debug("** %s: mid=0x%04x: removed\n",
674 coap_session_str(s), q->pdu->mid);
675 if (p) {
676 p->next = q->next;
677 } else {
678 s->delayqueue = q->next;
679 }
681 break;
682 }
683 p = q;
684 }
686 if (sent && sent->pdu && sent->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(s->proto)) {
687 if (s->con_active)
688 s->con_active--;
689 }
692 /* Flush out any entries on session->delayqueue */
694 }
695 }
696 }
697
698#if COAP_Q_BLOCK_SUPPORT
699 /*
700 * Check if any client large transmits have hit MAX_PAYLOAD and need
701 * restarting
702 */
703 if (s->lg_xmit) {
704 if (coap_block_check_q_block1_xmit(s, now, &s_timeout)) {
705 if (s_timeout < timeout)
706 timeout = s_timeout;
707 }
708 }
709#endif /* COAP_Q_BLOCK_SUPPORT */
710
711#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
712 assert(s->ref > 1);
716 if (*num_sockets < max_sockets && !(s->sock.flags & COAP_SOCKET_SLAVE))
717 sockets[(*num_sockets)++] = &s->sock;
718 }
719#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
720release_2:
722 }
723#endif /* COAP_CLIENT_SUPPORT */
724
725 return (unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
726}
727
728/*
729 * return 0 Insufficient space to hold fds, or fds not supported
730 * 1 All fds found
731 */
732COAP_API unsigned int
734 coap_fd_t read_fds[],
735 unsigned int *have_read_fds,
736 unsigned int max_read_fds,
737 coap_fd_t write_fds[],
738 unsigned int *have_write_fds,
739 unsigned int max_write_fds,
740 unsigned int *rem_timeout_ms) {
741 unsigned int ret;
742
743 coap_lock_lock(return 0);
744 ret = coap_io_get_fds_lkd(ctx, read_fds, have_read_fds, max_read_fds, write_fds,
745 have_write_fds, max_write_fds, rem_timeout_ms);
747 return ret;
748}
749
750#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
751static int
752coap_add_fd(coap_fd_t fd, coap_fd_t this_fds[], unsigned int *have_this_fds,
753 unsigned int max_this_fds) {
754 if (*have_this_fds < max_this_fds) {
755 this_fds[(*have_this_fds)++] = fd;
756 return 1;
757 }
758 coap_log_warn("coap_io_get_fds: Insufficient space for new fd (%u >= %u)\n", *have_this_fds,
759 max_this_fds);
760 return 0;
761}
762
763/*
764 * return 0 Insufficient space to hold fds, or fds not supported
765 * 1 All fds found
766 */
767unsigned int
769 coap_fd_t read_fds[],
770 unsigned int *have_read_fds,
771 unsigned int max_read_fds,
772 coap_fd_t write_fds[],
773 unsigned int *have_write_fds,
774 unsigned int max_write_fds,
775 unsigned int *rem_timeout_ms) {
776 *have_read_fds = 0;
777 *have_write_fds = 0;
778
779#ifdef COAP_EPOLL_SUPPORT
780 (void)write_fds;
781 (void)max_write_fds;;
782
783 if (!coap_add_fd(ctx->epfd, read_fds, have_read_fds, max_read_fds))
784 return 0;
785 /* epoll is making use of timerfd, so no need to return any timeout */
786 *rem_timeout_ms = 0;
787 return 1;
788#else /* ! COAP_EPOLL_SUPPORT */
789 coap_session_t *s, *rtmp;
790 coap_tick_t now;
791 unsigned int timeout_ms;
792#if COAP_SERVER_SUPPORT
793 coap_endpoint_t *ep;
794
795 LL_FOREACH(ctx->endpoint, ep) {
797 if (!coap_add_fd(ep->sock.fd, read_fds, have_read_fds, max_read_fds))
798 return 0;
799 }
801 if (!coap_add_fd(ep->sock.fd, write_fds, have_write_fds, max_write_fds))
802 return 0;
803 }
804 SESSIONS_ITER_SAFE(ep->sessions, s, rtmp) {
806 if (!coap_add_fd(s->sock.fd, read_fds, have_read_fds, max_read_fds))
807 return 0;
808 }
810 if (!coap_add_fd(s->sock.fd, write_fds, have_write_fds, max_write_fds))
811 return 0;
812 }
813 }
814 }
815#endif /* COAP_SERVER_SUPPORT */
816
817#if COAP_CLIENT_SUPPORT
818 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
820 if (!coap_add_fd(s->sock.fd, read_fds, have_read_fds, max_read_fds))
821 return 0;
822 }
824 if (!coap_add_fd(s->sock.fd, write_fds, have_write_fds, max_write_fds))
825 return 0;
826 }
827 }
828#endif /* COAP_CLIENT_SUPPORT */
829
830 coap_ticks(&now);
831 timeout_ms = (unsigned int)(ctx->next_timeout ? ctx->next_timeout > now ?
832 ctx->next_timeout - now : 0 : 0) *
834 *rem_timeout_ms = timeout_ms;
835 return 1;
836#endif /* ! COAP_EPOLL_SUPPORT */
837}
838
839#else /* WITH_LWIP || WITH_CONTIKI */
840
841/*
842 * return 0 Insufficient space to hold fds, or fds not supported
843 * 1 All fds found
844 */
845unsigned int
847 coap_fd_t read_fds[],
848 unsigned int *have_read_fds,
849 unsigned int max_read_fds,
850 coap_fd_t write_fds[],
851 unsigned int *have_write_fds,
852 unsigned int max_write_fds,
853 unsigned int *rem_timeout_ms) {
854 (void)ctx;
855 (void)read_fds;
856 (void)max_read_fds;
857 (void)write_fds;
858 (void)max_write_fds;
859
860 *have_read_fds = 0;
861 *have_write_fds = 0;
862 *rem_timeout_ms = 0;
863
864 coap_log_warn("coap_io_get_fds: Not supported\n");
865 return 0;
866}
867#endif /* WITH_LWIP || WITH_CONTIKI */
868
869COAP_API int
871 int ret;
872
873 coap_lock_lock(return 0);
874 ret = coap_io_pending_lkd(context);
876 return ret;
877}
878
879/*
880 * return 1 I/O pending
881 * 0 No I/O pending
882 */
883int
885 coap_session_t *s, *rtmp;
886#if COAP_SERVER_SUPPORT
887 coap_endpoint_t *ep;
888#endif /* COAP_SERVER_SUPPORT */
889
890 if (!context)
891 return 0;
893 if (coap_io_process_lkd(context, COAP_IO_NO_WAIT) < 0)
894 return 0;
895
896 if (context->sendqueue)
897 return 1;
898#if COAP_SERVER_SUPPORT
899 LL_FOREACH(context->endpoint, ep) {
900 SESSIONS_ITER(ep->sessions, s, rtmp) {
901 if (s->delayqueue)
902 return 1;
903 if (s->lg_xmit)
904 return 1;
905 if (s->lg_srcv)
906 return 1;
907 }
908 }
909#endif /* COAP_SERVER_SUPPORT */
910#if COAP_CLIENT_SUPPORT
911 SESSIONS_ITER(context->sessions, s, rtmp) {
912 if (s->delayqueue)
913 return 1;
914 if (s->lg_xmit)
915 return 1;
916 if (s->lg_crcv)
917 return 1;
918 }
919#endif /* COAP_CLIENT_SUPPORT */
920 return 0;
921}
922
923const char *
925 return strerror(error);
926}
927#ifdef _WIN32
928const char *
930 coap_win_error_to_errno();
931 return coap_socket_format_errno(errno);
932}
933#else /* _WIN32 */
934const char *
936 return coap_socket_format_errno(errno);
937}
938#endif /* _WIN32 */
939
942#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
943 return sock->fd;
944#else
945 (void)(sock);
946 return COAP_INVALID_SOCKET;
947#endif
948}
949
952 return sock->flags;
953}
954
955COAP_API void
957 sock->flags = flags;
958}
#define COAP_CLIENT_SUPPORT
const char * coap_socket_format_errno(int error)
Definition coap_io.c:924
static int coap_add_fd(coap_fd_t fd, coap_fd_t this_fds[], unsigned int *have_this_fds, unsigned int max_this_fds)
Definition coap_io.c:752
const char * coap_socket_strerror(void)
Definition coap_io.c:935
void coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length)
Given a packet, set msg and msg_len to an address and length of the packet's data in memory.
Definition coap_io.c:203
void coap_update_io_timer(coap_context_t *context, coap_tick_t delay)
Update when to continue with I/O processing, unless packets come in in the meantime.
Definition coap_io.c:70
uint16_t coap_socket_flags_t
Definition coap_io.h:57
@ COAP_NACK_NOT_DELIVERABLE
Definition coap_io.h:68
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:70
int coap_fd_t
Definition coap_io.h:51
#define COAP_INVALID_SOCKET
Definition coap_io.h:54
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
#define COAP_SOCKET_SLAVE
socket is a slave socket - do not close
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
coap_endpoint_t * coap_malloc_endpoint(void)
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
void coap_mfree_endpoint(coap_endpoint_t *ep)
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_ENDPOINT
Definition coap_mem.h:40
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_tick_t coap_dtls_get_timeout(coap_session_t *session COAP_UNUSED, coap_tick_t now COAP_UNUSED)
Definition coap_notls.c:229
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition coap_notls.c:224
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:238
#define SESSIONS_ITER_SAFE(e, el, rtmp)
#define SESSIONS_ITER(e, el, rtmp)
#define COAP_DEFAULT_SESSION_TIMEOUT
void coap_reset_doing_first(coap_session_t *session)
Reset doing the first packet state when testing for optional functionality.
int coap_io_pending_lkd(coap_context_t *context)
Check to see if there is any i/o pending for the context.
Definition coap_io.c:884
unsigned int coap_io_get_fds_lkd(coap_context_t *ctx, coap_fd_t read_fds[], unsigned int *have_read_fds, unsigned int max_read_fds, coap_fd_t write_fds[], unsigned int *have_write_fds, unsigned int max_write_fds, unsigned int *rem_timeout_ms)
Definition coap_io.c:768
int coap_io_process_lkd(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
unsigned int coap_io_prepare_io_lkd(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
Iterates through all the coap_socket_t structures embedded in endpoints or sessions associated with t...
Definition coap_io.c:331
unsigned int coap_io_prepare_epoll_lkd(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
Definition coap_io.c:219
coap_mid_t coap_send_lkd(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1484
COAP_API int coap_io_pending(coap_context_t *context)
Check to see if there is any i/o pending for the context.
Definition coap_io.c:870
COAP_API unsigned int coap_io_get_fds(coap_context_t *ctx, coap_fd_t read_fds[], unsigned int *have_read_fds, unsigned int max_read_fds, coap_fd_t write_fds[], unsigned int *have_write_fds, unsigned int max_write_fds, unsigned int *rem_timeout_ms)
Definition coap_io.c:733
COAP_API unsigned int coap_io_prepare_io(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
Iterates through all the coap_socket_t structures embedded in endpoints or sessions associated with t...
Definition coap_io.c:270
#define COAP_IO_NO_WAIT
Definition coap_net.h:826
COAP_API void coap_socket_set_flags(coap_socket_t *sock, coap_socket_flags_t flags)
Set the libcoap internal flags for a socket.
Definition coap_io.c:956
COAP_API unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
Definition coap_io.c:209
COAP_API coap_fd_t coap_socket_get_fd(coap_socket_t *sock)
Get the libcoap internal file descriptor for a socket.
Definition coap_io.c:941
COAP_API coap_socket_flags_t coap_socket_get_flags(coap_socket_t *sock)
Get the libcoap internal flags for a socket.
Definition coap_io.c:951
int coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
int coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:151
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:166
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:233
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition coap_net.c:5000
int coap_delete_node_lkd(coap_queue_t *node)
Destroys specified node.
Definition coap_net.c:214
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition coap_net.c:2995
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition coap_net.c:257
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
Definition coap_net.c:265
coap_mid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition coap_net.c:2282
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
Definition coap_notls.c:219
@ COAP_EVENT_FIRST_PDU_FAIL
Triggered when the initial app PDU cannot be transmitted.
Definition coap_event.h:114
@ COAP_EVENT_SERVER_SESSION_DEL
Called in the CoAP IO loop if a server session is deleted (e.g., due to inactivity or because the max...
Definition coap_event.h:98
@ COAP_EVENT_KEEPALIVE_FAILURE
Triggered when no response to a keep alive (ping) packet.
Definition coap_event.h:144
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
#define coap_log_emerg(...)
Definition coap_debug.h:87
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:268
#define COAP_TOKEN_DEFAULT_MAX
Definition coap_pdu.h:60
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:271
@ COAP_PROTO_DTLS
Definition coap_pdu.h:321
@ COAP_MESSAGE_CON
Definition coap_pdu.h:73
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
int coap_session_reconnect(coap_session_t *session)
Close the current session (if not already closed) and reconnect to server (client session only).
void coap_session_server_keepalive_failed(coap_session_t *session)
Clear down a session following a keepalive failure.
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
void coap_session_failed(coap_session_t *session)
Session has failed due to a socket error.
coap_mid_t coap_session_send_ping_lkd(coap_session_t *session)
Send a ping message for the session.
void coap_session_free(coap_session_t *session)
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
coap_session_t * coap_session_reference_lkd(coap_session_t *session)
Increment reference counter on a session.
@ COAP_EXT_T_CHECKING
Token size check request sent.
@ COAP_EXT_T_CHECKED
Token size valid.
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
@ COAP_SESSION_TYPE_SERVER
server-side
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_CSM
@ COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_NONE
void coap_check_notify_lkd(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
The CoAP stack's global state is stored in a coap_context_t object.
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
unsigned int reconnect_time
Time to wait before reconnecting a failed client session.
coap_session_t * sessions
client sessions
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
coap_queue_t * sendqueue
coap_endpoint_t * endpoint
the endpoints used for listening
uint32_t csm_timeout_ms
Timeout for waiting for a CSM from the remote side.
coap_tick_t next_timeout
When the next timeout is to occur.
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
Abstraction of virtual endpoint that can be attached to coap_context_t.
coap_session_t * sessions
hash table or list of active sessions
coap_socket_t sock
socket object for the interface, if any
size_t length
length of payload
unsigned char * payload
payload
structure for CoAP PDUs
coap_mid_t mid
message id, if any, in regular host byte order
coap_pdu_type_t type
message type
Queue entry.
coap_pdu_t * pdu
the CoAP PDU to send
struct coap_queue_t * next
coap_mid_t id
CoAP message id.
coap_tick_t t
when to send PDU for the next time
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
volatile uint8_t max_token_checked
Check for max token size coap_ext_token_check_t.
uint8_t csm_not_seen
Set if timeout waiting for CSM.
unsigned ref_subscriptions
reference count of current subscriptions
coap_tick_t doing_first_timeout
If doing_first, when to timeout.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t doing_first
Set if doing client's first request.
coap_socket_t sock
socket object for the session, if any
uint32_t max_token_size
Largest token size supported RFC8974.
coap_session_state_t state
current state of relationship with peer
uint8_t client_initiated
Session initially started as a client.
coap_mid_t remote_test_mid
mid used for checking remote support
unsigned ref_proxy_subs
reference count of current proxy subscriptions
coap_proto_t proto
protocol used
unsigned ref
reference count from queues
void * tls
security parameters
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_session_type_t type
client or server side socket
coap_pdu_t * doing_first_pdu
If doing fist, PDU to retry sending.
coap_context_t * context
session's context
uint8_t session_failed
Set if session failed and can try re-connect.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
struct in6_addr ipi6_addr
unsigned int ipi6_ifindex
struct in_addr ipi_spec_dst
struct in_addr ipi_addr