libcoap 4.3.5-develop-5347a62
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-2026 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
15
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
58coap_malloc_endpoint(void) {
60}
61
62void
63coap_mfree_endpoint(coap_endpoint_t *ep) {
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 coap_addr_hash_t t_addr_hash;
343#endif /* COAP_SERVER_SUPPORT */
344#if defined(COAP_EPOLL_SUPPORT) || defined(WITH_LWIP) || defined(RIOT_VERSION)
345 (void)sockets;
346 (void)max_sockets;
347#endif /* COAP_EPOLL_SUPPORT || WITH_LWIP || RIOT_VERSION*/
348
350 *num_sockets = 0;
351
352#if COAP_SERVER_SUPPORT
353 /* Check to see if we need to send off any Observe requests */
354 coap_check_notify_lkd(ctx);
355
356#if COAP_ASYNC_SUPPORT
357 /* Check to see if we need to send off any Async requests */
358 if (coap_check_async(ctx, now, &s_timeout)) {
359 if (s_timeout < timeout)
360 timeout = s_timeout;
361 }
362#endif /* COAP_ASYNC_SUPPORT */
363#endif /* COAP_SERVER_SUPPORT */
364
365 /* Check to see if we need to send off any retransmit request */
366 nextpdu = coap_peek_next(ctx);
367 while (nextpdu && now >= ctx->sendqueue_basetime &&
368 nextpdu->t <= now - ctx->sendqueue_basetime) {
369 coap_session_t *session = nextpdu->session;
370
371 coap_session_hold_lkd(session);
374 nextpdu = coap_peek_next(ctx);
375 }
376 if (nextpdu && now >= ctx->sendqueue_basetime &&
377 (nextpdu->t - (now - ctx->sendqueue_basetime) < timeout))
378 timeout = nextpdu->t - (now - ctx->sendqueue_basetime);
379
380 /* Check for DTLS timeouts */
381 if (ctx->dtls_context) {
384 if (tls_timeout > 0) {
385 if (tls_timeout < now + COAP_TICKS_PER_SECOND / 10)
386 tls_timeout = now + COAP_TICKS_PER_SECOND / 10;
387 coap_log_debug("** DTLS global timeout set to %dms\n",
388 (int)((tls_timeout - now) * 1000 / COAP_TICKS_PER_SECOND));
389 if (tls_timeout - now < timeout)
390 timeout = tls_timeout - now;
391 }
392#if COAP_SERVER_SUPPORT
393 } else {
394 check_dtls_timeouts = 1;
395#endif /* COAP_SERVER_SUPPORT */
396 }
397 }
398#if COAP_PROXY_SUPPORT
399 if (coap_proxy_check_timeouts(ctx, now, &s_timeout)) {
400 if (s_timeout < timeout)
401 timeout = s_timeout;
402 }
403#endif /* COAP_PROXY_SUPPORT */
404#if COAP_SERVER_SUPPORT
405 coap_endpoint_t *ep;
406 coap_tick_t session_timeout;
407
408 if (ctx->session_timeout > 0)
409 session_timeout = ctx->session_timeout * COAP_TICKS_PER_SECOND;
410 else
412
413 LL_FOREACH(ctx->endpoint, ep) {
414#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
416 if (*num_sockets < max_sockets)
417 sockets[(*num_sockets)++] = &ep->sock;
418 }
419#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
420 SESSIONS_ITER_EP_SAFE(ep->sessions, s, stmp, t_addr_hash) {
421 /* Check whether any idle server sessions should be released */
422 if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 &&
423 s->delayqueue == NULL &&
425 !(s->client_initiated && ctx->reconnect_time) &&
426#endif /* COAP_CLIENT_SUPPORT */
427 (s->last_rx_tx + session_timeout <= now ||
431 continue;
432 } else if (s->is_rate_limiting) {
433 continue;
434 } else {
435 if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 &&
436 s->delayqueue == NULL && s->last_rx_tx + session_timeout <= now) {
437 /* Has to be positive based on if() above */
438 s_timeout = (s->last_rx_tx + session_timeout) - now;
439 if (s_timeout < timeout)
440 timeout = s_timeout;
441 }
442 /* Make sure the session object is not deleted in any callbacks */
444 /* Check any DTLS timeouts and expire if appropriate */
445 if (check_dtls_timeouts && s->state == COAP_SESSION_STATE_HANDSHAKE &&
446 s->proto == COAP_PROTO_DTLS && s->tls) {
447 coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
448 while (tls_timeout > 0 && tls_timeout <= now) {
449 coap_log_debug("** %s: DTLS retransmit timeout\n",
452 goto release_1;
453
454 if (s->tls)
455 tls_timeout = coap_dtls_get_timeout(s, now);
456 else {
457 tls_timeout = 0;
458 timeout = 1;
459 }
460 }
461 if (tls_timeout > 0 && tls_timeout - now < timeout)
462 timeout = tls_timeout - now;
463 }
464 /* Check if any server large receives are missing blocks */
465 if (s->lg_srcv) {
466 if (coap_block_check_lg_srcv_timeouts(s, now, &s_timeout)) {
467 if (s_timeout < timeout)
468 timeout = s_timeout;
469 }
470 }
471 /* Check if any server large sending have timed out */
472 if (s->lg_xmit) {
473 if (coap_block_check_lg_xmit_timeouts(s, now, &s_timeout)) {
474 if (s_timeout < timeout)
475 timeout = s_timeout;
476 }
477 }
478#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
480 if (*num_sockets < max_sockets && !(s->sock.flags & COAP_SOCKET_SLAVE))
481 sockets[(*num_sockets)++] = &s->sock;
482 }
483#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
484#if COAP_Q_BLOCK_SUPPORT
485 /*
486 * Check if any server large transmits have hit MAX_PAYLOAD and need
487 * restarting
488 */
489 if (s->lg_xmit && !s->is_rate_limiting) {
490 if (coap_block_check_q_block2_xmit(s, now, &s_timeout)) {
491 if (s_timeout < timeout)
492 timeout = s_timeout;
493 }
494 }
495#endif /* COAP_Q_BLOCK_SUPPORT */
496release_1:
498 }
499 if (s->type == COAP_SESSION_TYPE_SERVER &&
501 (s->ref_subscriptions || s->ref_proxy_subs
503 || (s->client_initiated && ctx->reconnect_time)
504#endif /* COAP_CLIENT_SUPPORT */
505 ) &&
506 !s->con_active && ctx->ping_timeout > 0) {
507 /* Only do this if this session is observing or reconnecting */
508 if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) {
509 /* Time to send a ping */
510 coap_mid_t mid;
511
513 /* Some issue - not safe to continue processing */
514 s->ping_failed++;
515 s->last_rx_tx = now;
516#if COAP_CLIENT_SUPPORT
517 if (s->client_initiated && ctx->reconnect_time) {
519 }
520#endif /* COAP_CLIENT_SUPPORT */
522 continue;
523 }
524 s->last_ping_mid = mid;
525 if ((s->last_ping > 0 && s->last_pong < s->last_ping) || s->ping_failed > COAP_MAX_PING_FAILURES) {
526#if COAP_CLIENT_SUPPORT
527 if (s->client_initiated && ctx->reconnect_time) {
529 } else {
530#endif /* COAP_CLIENT_SUPPORT */
532#if COAP_CLIENT_SUPPORT
533 }
534#endif /* COAP_CLIENT_SUPPORT */
535 /* check the next session */
536 continue;
537 }
538 s->last_rx_tx = now;
539 s->last_ping = now;
540 } else {
541 /* Always positive due to if() above */
542 s_timeout = (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND) - now;
543 if (s_timeout < timeout)
544 timeout = s_timeout;
545 }
546 }
547#if COAP_CLIENT_SUPPORT
548 timeout = coap_check_need_reconnect(ctx, s, now, timeout);
549#endif /* COAP_CLIENT_SUPPORT */
550 }
551 }
552#endif /* COAP_SERVER_SUPPORT */
553#if COAP_CLIENT_SUPPORT
554 SESSIONS_ITER_CL_SAFE(ctx->sessions, s, stmp) {
555 if (s->is_rate_limiting) {
556 continue;
557 }
558 if (s->type == COAP_SESSION_TYPE_CLIENT &&
560 ctx->ping_timeout > 0) {
561 if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) {
562 /* Time to send a ping */
563 coap_mid_t mid;
564
566 /* Some issue - not safe to continue processing */
567 s->ping_failed++;
568 s->last_rx_tx = now;
571 continue;
572 }
573 s->last_ping_mid = mid;
574 if (s->last_ping > 0 && s->last_pong < s->last_ping) {
576 }
578#if COAP_CLIENT_SUPPORT
579 if (s->client_initiated) {
580 if (ctx->reconnect_time) {
582 } else {
584 }
585 } else {
586#endif /* COAP_CLIENT_SUPPORT */
587#if COAP_SERVER_SUPPORT
589#endif /* COAP_SERVER_SUPPORT */
590#if COAP_CLIENT_SUPPORT
591 }
592#endif /* COAP_CLIENT_SUPPORT */
593 continue;
594 } else {
595 s->last_rx_tx = now;
596 s->last_ping = now;
597 }
598 } else {
599 /* Always positive due to if() above */
600 s_timeout = (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND) - now;
601 if (s_timeout < timeout)
602 timeout = s_timeout;
603 }
604 }
605 timeout = coap_check_need_reconnect(ctx, s, now, timeout);
606
607#if !COAP_DISABLE_TCP
609 s->state == COAP_SESSION_STATE_CSM && ctx->csm_timeout_ms > 0) {
610 if (s->csm_tx == 0) {
611 s->csm_tx = now;
612 s_timeout = (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000;
613 } else if (s->csm_tx + (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000 <= now) {
614 /* timed out - cannot handle 0, so has to be just +ve */
615 s_timeout = 1;
616 } else {
617 s_timeout = (s->csm_tx + (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000) - now;
618 }
619 if (s_timeout < timeout)
620 timeout = s_timeout;
621 }
622#endif /* !COAP_DISABLE_TCP */
623
624 /* Make sure the session object is not deleted in any callbacks */
626 /* Check any DTLS timeouts and expire if appropriate */
628 s->proto == COAP_PROTO_DTLS && s->tls) {
629 coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
630 while (tls_timeout > 0 && tls_timeout <= now) {
631 coap_log_debug("** %s: DTLS retransmit timeout\n", coap_session_str(s));
633 goto release_2;
634
635 if (s->tls)
636 tls_timeout = coap_dtls_get_timeout(s, now);
637 else {
638 tls_timeout = 0;
639 s_timeout = 1;
640 }
641 }
642 if (tls_timeout > 0 && tls_timeout - now < timeout)
643 s_timeout = tls_timeout - now;
644 if (s_timeout < timeout)
645 timeout = s_timeout;
646 }
647
648 /* Check if any client large receives are missing blocks */
649 if (s->lg_crcv) {
650 if (coap_block_check_lg_crcv_timeouts(s, now, &s_timeout)) {
651 if (s_timeout < timeout)
652 timeout = s_timeout;
653 }
654 }
655 /* Check if any client large sending have timed out */
656 if (s->lg_xmit) {
657 if (coap_block_check_lg_xmit_timeouts(s, now, &s_timeout)) {
658 if (s_timeout < timeout)
659 timeout = s_timeout;
660 }
661 }
662 if (s->doing_first && s->doing_first_timeout) {
663 if ((s->doing_first_timeout + 5 * COAP_TICKS_PER_SECOND) > now) {
664 s_timeout = s->doing_first_timeout + 5 * COAP_TICKS_PER_SECOND - now;
665 if (s_timeout < timeout)
666 timeout = s_timeout;
667 } else {
668 /* Session set up has failed */
669 coap_queue_t *sent;
670 coap_queue_t *q;
671
672 if (s->state == COAP_SESSION_STATE_CSM) {
673 coap_log_debug("** %s: timeout waiting for CSM response\n",
675 s->csm_not_seen = 1;
676 } else {
677 coap_log_debug("** %s: timeout waiting for first response\n",
679 }
680#if COAP_Q_BLOCK_SUPPORT
681 /* Check if testing for Q-Block */
682 if (s->block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
683 coap_log_debug("Q-Block support assumed not available\n");
684 set_block_mode_drop_q(s->block_mode);
685 }
686#endif /* COAP_Q_BLOCK_SUPPORT */
687 /* Check if testing for Extended Token */
691 coap_log_debug("Extended Token support assumed not available\n");
692 }
693 /*
694 * Remove the test packet to stop re-transmission.
695 * The test packet may be in the delayqueue ((D)TLS has not been set up),
696 * or in the sendqueue if sent and is pending re-transmission.
697 */
699 if (q) {
700 if (q->pdu->type==COAP_MESSAGE_CON) {
701 coap_handle_nack(s, q->pdu,
702 s->proto == COAP_PROTO_DTLS ?
704 q->id);
705 }
706 coap_log_debug("** %s: mid=0x%04x: removed\n",
707 coap_session_str(s), q->id);
709 }
711 if (sent) {
712 if (sent->pdu && sent->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(s->proto)) {
713 if (s->con_active)
714 s->con_active--;
715 }
717 }
720 /* Flush out any entries on session->delayqueue */
722 }
723 }
724 }
725
726#if COAP_Q_BLOCK_SUPPORT
727 /*
728 * Check if any client large transmits have hit MAX_PAYLOAD and need
729 * restarting
730 */
731 if (s->lg_xmit) {
732 if (coap_block_check_q_block1_xmit(s, now, &s_timeout)) {
733 if (s_timeout < timeout)
734 timeout = s_timeout;
735 }
736 }
737#endif /* COAP_Q_BLOCK_SUPPORT */
738
739#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
740 /*
741 * Not listed if the unhold below is to free off s, which a pending
742 * disconnect can do by dropping the sendqueue's references to s
743 */
744 if (s->ref > 1 && !s->disconnect_pending &&
748 if (*num_sockets < max_sockets && !(s->sock.flags & COAP_SOCKET_SLAVE))
749 sockets[(*num_sockets)++] = &s->sock;
750 }
751#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
752 /* Any disconnect deferred by the work above is done here */
754 continue;
755release_2:
757 }
758#endif /* COAP_CLIENT_SUPPORT */
759
760 return (unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
761}
762
763/*
764 * return 0 Insufficient space to hold fds, or fds not supported
765 * 1 All fds found
766 */
767COAP_API unsigned 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 unsigned int ret;
777
778 coap_lock_lock(return 0);
779 ret = coap_io_get_fds_lkd(ctx, read_fds, have_read_fds, max_read_fds, write_fds,
780 have_write_fds, max_write_fds, rem_timeout_ms);
782 return ret;
783}
784
785#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
786static int
787coap_add_fd(coap_fd_t fd, coap_fd_t this_fds[], unsigned int *have_this_fds,
788 unsigned int max_this_fds) {
789 if (*have_this_fds < max_this_fds) {
790 this_fds[(*have_this_fds)++] = fd;
791 return 1;
792 }
793 coap_log_warn("coap_io_get_fds: Insufficient space for new fd (%u >= %u)\n", *have_this_fds,
794 max_this_fds);
795 return 0;
796}
797
798/*
799 * return 0 Insufficient space to hold fds, or fds not supported
800 * 1 All fds found
801 */
802unsigned int
804 coap_fd_t read_fds[],
805 unsigned int *have_read_fds,
806 unsigned int max_read_fds,
807 coap_fd_t write_fds[],
808 unsigned int *have_write_fds,
809 unsigned int max_write_fds,
810 unsigned int *rem_timeout_ms) {
811 *have_read_fds = 0;
812 *have_write_fds = 0;
813
814#ifdef COAP_EPOLL_SUPPORT
815 (void)write_fds;
816 (void)max_write_fds;;
817
818 if (!coap_add_fd(ctx->epfd, read_fds, have_read_fds, max_read_fds))
819 return 0;
820 /* epoll is making use of timerfd, so no need to return any timeout */
821 *rem_timeout_ms = 0;
822 return 1;
823#else /* ! COAP_EPOLL_SUPPORT */
824 coap_session_t *s, *rtmp;
825 coap_tick_t now;
826 unsigned int timeout_ms;
827#if COAP_SERVER_SUPPORT
828 coap_endpoint_t *ep;
829 coap_addr_hash_t t_addr_hash;
830
831 LL_FOREACH(ctx->endpoint, ep) {
832 if (ep->sock.flags & (COAP_SOCKET_WANT_READ | COAP_SOCKET_WANT_ACCEPT)) {
833 if (!coap_add_fd(ep->sock.fd, read_fds, have_read_fds, max_read_fds))
834 return 0;
835 }
836 if (ep->sock.flags & (COAP_SOCKET_WANT_WRITE | COAP_SOCKET_WANT_CONNECT)) {
837 if (!coap_add_fd(ep->sock.fd, write_fds, have_write_fds, max_write_fds))
838 return 0;
839 }
840 SESSIONS_ITER_EP_SAFE(ep->sessions, s, rtmp, t_addr_hash) {
842 if (!coap_add_fd(s->sock.fd, read_fds, have_read_fds, max_read_fds))
843 return 0;
844 }
846 if (!coap_add_fd(s->sock.fd, write_fds, have_write_fds, max_write_fds))
847 return 0;
848 }
849 }
850 }
851#endif /* COAP_SERVER_SUPPORT */
852
853#if COAP_CLIENT_SUPPORT
854 SESSIONS_ITER_CL_SAFE(ctx->sessions, s, rtmp) {
856 if (!coap_add_fd(s->sock.fd, read_fds, have_read_fds, max_read_fds))
857 return 0;
858 }
860 if (!coap_add_fd(s->sock.fd, write_fds, have_write_fds, max_write_fds))
861 return 0;
862 }
863 }
864#endif /* COAP_CLIENT_SUPPORT */
865
866 coap_ticks(&now);
867 timeout_ms = (unsigned int)(ctx->next_timeout ? ctx->next_timeout > now ?
868 ctx->next_timeout - now : 0 : 0) *
870 *rem_timeout_ms = timeout_ms;
871 return 1;
872#endif /* ! COAP_EPOLL_SUPPORT */
873}
874
875#else /* WITH_LWIP || WITH_CONTIKI */
876
877/*
878 * return 0 Insufficient space to hold fds, or fds not supported
879 * 1 All fds found
880 */
881unsigned int
883 coap_fd_t read_fds[],
884 unsigned int *have_read_fds,
885 unsigned int max_read_fds,
886 coap_fd_t write_fds[],
887 unsigned int *have_write_fds,
888 unsigned int max_write_fds,
889 unsigned int *rem_timeout_ms) {
890 (void)ctx;
891 (void)read_fds;
892 (void)max_read_fds;
893 (void)write_fds;
894 (void)max_write_fds;
895
896 *have_read_fds = 0;
897 *have_write_fds = 0;
898 *rem_timeout_ms = 0;
899
900 coap_log_warn("coap_io_get_fds: Not supported\n");
901 return 0;
902}
903#endif /* WITH_LWIP || WITH_CONTIKI */
904
905COAP_API int
907 int ret;
908
909 coap_lock_lock(return 0);
910 ret = coap_io_pending_lkd(context);
912 return ret;
913}
914
915/*
916 * return 1 I/O pending
917 * 0 No I/O pending
918 */
919int
921 coap_session_t *s, *rtmp;
922#if COAP_SERVER_SUPPORT
923 coap_endpoint_t *ep;
924#endif /* COAP_SERVER_SUPPORT */
925
926 if (!context)
927 return 0;
929 if (coap_io_process_lkd(context, COAP_IO_NO_WAIT) < 0)
930 return 0;
931
932 if (context->sendqueue)
933 return 1;
934#if COAP_SERVER_SUPPORT
935 LL_FOREACH(context->endpoint, ep) {
936 SESSIONS_ITER(ep->sessions, s, rtmp) {
937 if (s->delayqueue)
938 return 1;
939 if (s->lg_xmit)
940 return 1;
941 if (s->lg_srcv)
942 return 1;
943 }
944 }
945#endif /* COAP_SERVER_SUPPORT */
946#if COAP_CLIENT_SUPPORT
947 SESSIONS_ITER(context->sessions, s, rtmp) {
948 if (s->delayqueue)
949 return 1;
950 if (s->lg_xmit)
951 return 1;
952 if (s->lg_crcv)
953 return 1;
954 }
955#endif /* COAP_CLIENT_SUPPORT */
956 return 0;
957}
958
959const char *
961 return strerror(error);
962}
963#ifdef _WIN32
964const char *
966 coap_win_error_to_errno();
967 return coap_socket_format_errno(errno);
968}
969#else /* _WIN32 */
970const char *
972 return coap_socket_format_errno(errno);
973}
974#endif /* _WIN32 */
975
978#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
979 return sock->fd;
980#else
981 (void)(sock);
982 return COAP_INVALID_SOCKET;
983#endif
984}
985
988 return sock->flags;
989}
990
991COAP_API void
993 sock->flags = flags;
994}
struct coap_endpoint_t coap_endpoint_t
#define COAP_CLIENT_SUPPORT
const char * coap_socket_format_errno(int error)
Definition coap_io.c:960
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:787
const char * coap_socket_strerror(void)
Definition coap_io.c:971
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:55
@ COAP_NACK_NOT_DELIVERABLE
Definition coap_io.h:66
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:68
int coap_fd_t
Definition coap_io.h:49
#define COAP_INVALID_SOCKET
Definition coap_io.h:52
#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
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_ENDPOINT
Definition coap_mem.h:39
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:507
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED)
Definition coap_notls.c:502
int coap_dtls_handle_timeout(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:516
#define NULL
Definition coap_option.h:30
#define SESSIONS_ITER_EP_SAFE(e, el, rtmp, hhtmp)
#define SESSIONS_ITER_CL_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:920
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:803
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:1649
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:906
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:768
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:867
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:992
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:977
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:987
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:149
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
#define COAP_MAX_DELAY_TICKS
Definition coap_time.h:231
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:5563
int coap_delete_node_lkd(coap_queue_t *node)
Destroys specified node.
Definition coap_net.c:206
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition coap_net.c:249
coap_queue_t * coap_remove_mid_from_delayq(coap_session_t *session, coap_mid_t mid)
This function removes the node with given mid from the delayqueue.
Definition coap_net.c:3283
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:257
coap_mid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition coap_net.c:2522
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t mid, coap_bin_const_t *token, coap_queue_t **node)
This function removes the element with given mid from the list given list.
Definition coap_net.c:3337
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:497
@ 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:152
#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:195
#define COAP_TOKEN_DEFAULT_MAX
Definition coap_pdu.h:58
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:198
@ COAP_PROTO_DTLS
Definition coap_pdu.h:248
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
#define COAP_MAX_PING_FAILURES
void coap_session_hold_lkd(coap_session_t *session)
Mark the session as in use for a unit of work (reading and dispatching received data,...
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
void coap_session_unhold_lkd(coap_session_t *session)
End a unit of work started by coap_session_hold_lkd().
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_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
The session is to be disconnected (a send on it has failed, the peer or the (D)TLS layer has closed i...
@ 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
Only used for servers for hashing incoming packets.
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 ping_timeout
Minimum inactivity time before sending a ping message.
coap_queue_t * sendqueue
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.
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_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
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.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_socket_t sock
socket object for the session, if any
uint32_t max_token_size
Largest token size supported RFC8974.
uint32_t ping_failed
Ping failure count.
coap_session_state_t state
current state of relationship with peer
uint8_t is_rate_limiting
Currently NON rate limiting.
coap_mid_t remote_test_mid
mid used for checking remote support
coap_proto_t proto
protocol used
uint8_t disconnect_pending
Set if a disconnect is deferred until the session is no longer in use.
unsigned ref
reference count from queues
coap_bin_const_t * last_token
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_session_type_t type
client or server side socket
coap_context_t * context
session's context
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