libcoap 4.3.5-develop-18bb1ff
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#endif /* COAP_CLIENT_SUPPORT */
308
309/*
310 * return 0 No i/o pending
311 * +ve millisecs to next i/o activity
312 */
313unsigned int
315 coap_socket_t *sockets[],
316 unsigned int max_sockets,
317 unsigned int *num_sockets,
318 coap_tick_t now) {
319 coap_queue_t *nextpdu;
320 coap_session_t *s, *stmp;
322 coap_tick_t s_timeout;
323#if COAP_SERVER_SUPPORT
324 int check_dtls_timeouts = 0;
325#endif /* COAP_SERVER_SUPPORT */
326#if defined(COAP_EPOLL_SUPPORT) || defined(WITH_LWIP) || defined(RIOT_VERSION)
327 (void)sockets;
328 (void)max_sockets;
329#endif /* COAP_EPOLL_SUPPORT || WITH_LWIP || RIOT_VERSION*/
330
332 *num_sockets = 0;
333
334#if COAP_SERVER_SUPPORT
335 /* Check to see if we need to send off any Observe requests */
337
338#if COAP_ASYNC_SUPPORT
339 /* Check to see if we need to send off any Async requests */
340 if (coap_check_async(ctx, now, &s_timeout)) {
341 if (s_timeout < timeout)
342 timeout = s_timeout;
343 }
344#endif /* COAP_ASYNC_SUPPORT */
345#endif /* COAP_SERVER_SUPPORT */
346
347 /* Check to see if we need to send off any retransmit request */
348 nextpdu = coap_peek_next(ctx);
349 while (nextpdu && now >= ctx->sendqueue_basetime &&
350 nextpdu->t <= now - ctx->sendqueue_basetime) {
352 nextpdu = coap_peek_next(ctx);
353 }
354 if (nextpdu && now >= ctx->sendqueue_basetime &&
355 (nextpdu->t - (now - ctx->sendqueue_basetime) < timeout))
356 timeout = nextpdu->t - (now - ctx->sendqueue_basetime);
357
358 /* Check for DTLS timeouts */
359 if (ctx->dtls_context) {
362 if (tls_timeout > 0) {
363 if (tls_timeout < now + COAP_TICKS_PER_SECOND / 10)
364 tls_timeout = now + COAP_TICKS_PER_SECOND / 10;
365 coap_log_debug("** DTLS global timeout set to %dms\n",
366 (int)((tls_timeout - now) * 1000 / COAP_TICKS_PER_SECOND));
367 if (tls_timeout - now < timeout)
368 timeout = tls_timeout - now;
369 }
370#if COAP_SERVER_SUPPORT
371 } else {
372 check_dtls_timeouts = 1;
373#endif /* COAP_SERVER_SUPPORT */
374 }
375 }
376#if COAP_PROXY_SUPPORT
377 if (coap_proxy_check_timeouts(ctx, now, &s_timeout)) {
378 if (s_timeout < timeout)
379 timeout = s_timeout;
380 }
381#endif /* COAP_PROXY_SUPPORT */
382#if COAP_SERVER_SUPPORT
383 coap_endpoint_t *ep;
384 coap_tick_t session_timeout;
385
386 if (ctx->session_timeout > 0)
387 session_timeout = ctx->session_timeout * COAP_TICKS_PER_SECOND;
388 else
390
391 LL_FOREACH(ctx->endpoint, ep) {
392#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
394 if (*num_sockets < max_sockets)
395 sockets[(*num_sockets)++] = &ep->sock;
396 }
397#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
398 SESSIONS_ITER_SAFE(ep->sessions, s, stmp) {
399 /* Check whether any idle server sessions should be released */
400 if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 &&
401 s->delayqueue == NULL &&
403 !(s->client_initiated && ctx->reconnect_time) &&
404#endif /* COAP_CLIENT_SUPPORT */
405 (s->last_rx_tx + session_timeout <= now ||
409 continue;
410 } else {
411 if (s->type == COAP_SESSION_TYPE_SERVER && s->ref == 0 &&
412 s->delayqueue == NULL && s->last_rx_tx + session_timeout <= now) {
413 /* Has to be positive based on if() above */
414 s_timeout = (s->last_rx_tx + session_timeout) - now;
415 if (s_timeout < timeout)
416 timeout = s_timeout;
417 }
418 /* Make sure the session object is not deleted in any callbacks */
420 /* Check any DTLS timeouts and expire if appropriate */
421 if (check_dtls_timeouts && s->state == COAP_SESSION_STATE_HANDSHAKE &&
422 s->proto == COAP_PROTO_DTLS && s->tls) {
423 coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
424 while (tls_timeout > 0 && tls_timeout <= now) {
425 coap_log_debug("** %s: DTLS retransmit timeout\n",
428 goto release_1;
429
430 if (s->tls)
431 tls_timeout = coap_dtls_get_timeout(s, now);
432 else {
433 tls_timeout = 0;
434 timeout = 1;
435 }
436 }
437 if (tls_timeout > 0 && tls_timeout - now < timeout)
438 timeout = tls_timeout - now;
439 }
440 /* Check if any server large receives are missing blocks */
441 if (s->lg_srcv) {
442 if (coap_block_check_lg_srcv_timeouts(s, now, &s_timeout)) {
443 if (s_timeout < timeout)
444 timeout = s_timeout;
445 }
446 }
447 /* Check if any server large sending have timed out */
448 if (s->lg_xmit) {
449 if (coap_block_check_lg_xmit_timeouts(s, now, &s_timeout)) {
450 if (s_timeout < timeout)
451 timeout = s_timeout;
452 }
453 }
454#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
456 if (*num_sockets < max_sockets && !(s->sock.flags & COAP_SOCKET_SLAVE))
457 sockets[(*num_sockets)++] = &s->sock;
458 }
459#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
460#if COAP_Q_BLOCK_SUPPORT
461 /*
462 * Check if any server large transmits have hit MAX_PAYLOAD and need
463 * restarting
464 */
465 if (s->lg_xmit) {
466 if (coap_block_check_q_block2_xmit(s, now, &s_timeout)) {
467 if (s_timeout < timeout)
468 timeout = s_timeout;
469 }
470 }
471#endif /* COAP_Q_BLOCK_SUPPORT */
472release_1:
474 }
475 if (s->type == COAP_SESSION_TYPE_SERVER &&
479 || (s->client_initiated && ctx->reconnect_time)
480#endif /* COAP_CLIENT_SUPPORT */
481 ) &&
482 !s->con_active && ctx->ping_timeout > 0) {
483 /* Only do this if this session is observing or reconnecting */
484 if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) {
485 /* Time to send a ping */
486 coap_mid_t mid;
487
489 /* Some issue - not safe to continue processing */
490 s->last_rx_tx = now;
491#if COAP_CLIENT_SUPPORT
492 if (s->client_initiated && ctx->reconnect_time) {
494 }
495#endif /* COAP_CLIENT_SUPPORT */
496 continue;
497 }
498 s->last_ping_mid = mid;
499 if (s->last_ping > 0 && s->last_pong < s->last_ping) {
500#if COAP_CLIENT_SUPPORT
501 if (s->client_initiated && ctx->reconnect_time) {
503 } else {
504#endif /* COAP_CLIENT_SUPPORT */
506#if COAP_CLIENT_SUPPORT
507 }
508#endif /* COAP_CLIENT_SUPPORT */
509 /* check the next session */
510 continue;
511 }
512 s->last_rx_tx = now;
513 s->last_ping = now;
514 } else {
515 /* Always positive due to if() above */
516 s_timeout = (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND) - now;
517 if (s_timeout < timeout)
518 timeout = s_timeout;
519 }
520 }
521#if COAP_CLIENT_SUPPORT
522 timeout = coap_check_need_reconnect(ctx, s, now, timeout);
523#endif /* COAP_CLIENT_SUPPORT */
524 }
525 }
526#endif /* COAP_SERVER_SUPPORT */
527#if COAP_CLIENT_SUPPORT
528 SESSIONS_ITER_SAFE(ctx->sessions, s, stmp) {
529 if (s->type == COAP_SESSION_TYPE_CLIENT &&
531 ctx->ping_timeout > 0) {
532 if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) {
533 /* Time to send a ping */
534 coap_mid_t mid;
535
537 /* Some issue - not safe to continue processing */
538 s->last_rx_tx = now;
540 continue;
541 }
542 s->last_ping_mid = mid;
543 if (s->last_ping > 0 && s->last_pong < s->last_ping) {
545 }
546 s->last_rx_tx = now;
547 s->last_ping = now;
548 } else {
549 /* Always positive due to if() above */
550 s_timeout = (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND) - now;
551 if (s_timeout < timeout)
552 timeout = s_timeout;
553 }
554 }
555 timeout = coap_check_need_reconnect(ctx, s, now, timeout);
556
557#if !COAP_DISABLE_TCP
559 s->state == COAP_SESSION_STATE_CSM && ctx->csm_timeout_ms > 0) {
560 if (s->csm_tx == 0) {
561 s->csm_tx = now;
562 s_timeout = (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000;
563 } else if (s->csm_tx + (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000 <= now) {
564 /* timed out - cannot handle 0, so has to be just +ve */
565 s_timeout = 1;
566 } else {
567 s_timeout = (s->csm_tx + (ctx->csm_timeout_ms * COAP_TICKS_PER_SECOND) / 1000) - now;
568 }
569 if (s_timeout < timeout)
570 timeout = s_timeout;
571 }
572#endif /* !COAP_DISABLE_TCP */
573
574 /* Make sure the session object is not deleted in any callbacks */
576 /* Check any DTLS timeouts and expire if appropriate */
578 s->proto == COAP_PROTO_DTLS && s->tls) {
579 coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now);
580 while (tls_timeout > 0 && tls_timeout <= now) {
581 coap_log_debug("** %s: DTLS retransmit timeout\n", coap_session_str(s));
583 goto release_2;
584
585 if (s->tls)
586 tls_timeout = coap_dtls_get_timeout(s, now);
587 else {
588 tls_timeout = 0;
589 timeout = 1;
590 }
591 }
592 if (tls_timeout > 0 && tls_timeout - now < timeout)
593 timeout = tls_timeout - now;
594 }
595
596 /* Check if any client large receives are missing blocks */
597 if (s->lg_crcv) {
598 if (coap_block_check_lg_crcv_timeouts(s, now, &s_timeout)) {
599 if (s_timeout < timeout)
600 timeout = s_timeout;
601 }
602 }
603 /* Check if any client large sending have timed out */
604 if (s->lg_xmit) {
605 if (coap_block_check_lg_xmit_timeouts(s, now, &s_timeout)) {
606 if (s_timeout < timeout)
607 timeout = s_timeout;
608 }
609 }
610#if COAP_Q_BLOCK_SUPPORT
611 /*
612 * Check if any client large transmits have hit MAX_PAYLOAD and need
613 * restarting
614 */
615 if (s->lg_xmit) {
616 if (coap_block_check_q_block1_xmit(s, now, &s_timeout)) {
617 if (s_timeout < timeout)
618 timeout = s_timeout;
619 }
620 }
621#endif /* COAP_Q_BLOCK_SUPPORT */
622
623#if !defined(COAP_EPOLL_SUPPORT) && !defined(WITH_LWIP) && !defined(RIOT_VERSION)
624 assert(s->ref > 1);
628 if (*num_sockets < max_sockets && !(s->sock.flags & COAP_SOCKET_SLAVE))
629 sockets[(*num_sockets)++] = &s->sock;
630 }
631#endif /* ! COAP_EPOLL_SUPPORT && ! WITH_LWIP && ! RIOT_VERSION */
632release_2:
634 }
635#endif /* COAP_CLIENT_SUPPORT */
636
637 return (unsigned int)((timeout * 1000 + COAP_TICKS_PER_SECOND - 1) / COAP_TICKS_PER_SECOND);
638}
639
640/*
641 * return 0 Insufficient space to hold fds, or fds not supported
642 * 1 All fds found
643 */
644COAP_API unsigned int
646 coap_fd_t read_fds[],
647 unsigned int *have_read_fds,
648 unsigned int max_read_fds,
649 coap_fd_t write_fds[],
650 unsigned int *have_write_fds,
651 unsigned int max_write_fds,
652 unsigned int *rem_timeout_ms) {
653 unsigned int ret;
654
655 coap_lock_lock(return 0);
656 ret = coap_io_get_fds_lkd(ctx, read_fds, have_read_fds, max_read_fds, write_fds,
657 have_write_fds, max_write_fds, rem_timeout_ms);
659 return ret;
660}
661
662#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
663static int
664coap_add_fd(coap_fd_t fd, coap_fd_t this_fds[], unsigned int *have_this_fds,
665 unsigned int max_this_fds) {
666 if (*have_this_fds < max_this_fds) {
667 this_fds[(*have_this_fds)++] = fd;
668 return 1;
669 }
670 coap_log_warn("coap_io_get_fds: Insufficient space for new fd (%u >= %u)\n", *have_this_fds,
671 max_this_fds);
672 return 0;
673}
674
675/*
676 * return 0 Insufficient space to hold fds, or fds not supported
677 * 1 All fds found
678 */
679unsigned int
681 coap_fd_t read_fds[],
682 unsigned int *have_read_fds,
683 unsigned int max_read_fds,
684 coap_fd_t write_fds[],
685 unsigned int *have_write_fds,
686 unsigned int max_write_fds,
687 unsigned int *rem_timeout_ms) {
688 *have_read_fds = 0;
689 *have_write_fds = 0;
690
691#ifdef COAP_EPOLL_SUPPORT
692 (void)write_fds;
693 (void)max_write_fds;;
694
695 if (!coap_add_fd(ctx->epfd, read_fds, have_read_fds, max_read_fds))
696 return 0;
697 /* epoll is making use of timerfd, so no need to return any timeout */
698 *rem_timeout_ms = 0;
699 return 1;
700#else /* ! COAP_EPOLL_SUPPORT */
701 coap_session_t *s, *rtmp;
702 coap_tick_t now;
703 unsigned int timeout_ms;
704#if COAP_SERVER_SUPPORT
705 coap_endpoint_t *ep;
706
707 LL_FOREACH(ctx->endpoint, ep) {
709 if (!coap_add_fd(ep->sock.fd, read_fds, have_read_fds, max_read_fds))
710 return 0;
711 }
713 if (!coap_add_fd(ep->sock.fd, write_fds, have_write_fds, max_write_fds))
714 return 0;
715 }
716 SESSIONS_ITER_SAFE(ep->sessions, s, rtmp) {
718 if (!coap_add_fd(s->sock.fd, read_fds, have_read_fds, max_read_fds))
719 return 0;
720 }
722 if (!coap_add_fd(s->sock.fd, write_fds, have_write_fds, max_write_fds))
723 return 0;
724 }
725 }
726 }
727#endif /* COAP_SERVER_SUPPORT */
728
729#if COAP_CLIENT_SUPPORT
730 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
732 if (!coap_add_fd(s->sock.fd, read_fds, have_read_fds, max_read_fds))
733 return 0;
734 }
736 if (!coap_add_fd(s->sock.fd, write_fds, have_write_fds, max_write_fds))
737 return 0;
738 }
739 }
740#endif /* COAP_CLIENT_SUPPORT */
741
742 coap_ticks(&now);
743 timeout_ms = (unsigned int)(ctx->next_timeout ? ctx->next_timeout > now ?
744 ctx->next_timeout - now : 0 : 0) *
746 *rem_timeout_ms = timeout_ms;
747 return 1;
748#endif /* ! COAP_EPOLL_SUPPORT */
749}
750
751#else /* WITH_LWIP || WITH_CONTIKI */
752
753/*
754 * return 0 Insufficient space to hold fds, or fds not supported
755 * 1 All fds found
756 */
757unsigned int
759 coap_fd_t read_fds[],
760 unsigned int *have_read_fds,
761 unsigned int max_read_fds,
762 coap_fd_t write_fds[],
763 unsigned int *have_write_fds,
764 unsigned int max_write_fds,
765 unsigned int *rem_timeout_ms) {
766 (void)ctx;
767 (void)read_fds;
768 (void)max_read_fds;
769 (void)write_fds;
770 (void)max_write_fds;
771
772 *have_read_fds = 0;
773 *have_write_fds = 0;
774 *rem_timeout_ms = 0;
775
776 coap_log_warn("coap_io_get_fds: Not supported\n");
777 return 0;
778}
779#endif /* WITH_LWIP || WITH_CONTIKI */
780
781COAP_API int
783 int ret;
784
785 coap_lock_lock(return 0);
786 ret = coap_io_pending_lkd(context);
788 return ret;
789}
790
791/*
792 * return 1 I/O pending
793 * 0 No I/O pending
794 */
795int
797 coap_session_t *s, *rtmp;
798#if COAP_SERVER_SUPPORT
799 coap_endpoint_t *ep;
800#endif /* COAP_SERVER_SUPPORT */
801
802 if (!context)
803 return 0;
805 if (coap_io_process_lkd(context, COAP_IO_NO_WAIT) < 0)
806 return 0;
807
808 if (context->sendqueue)
809 return 1;
810#if COAP_SERVER_SUPPORT
811 LL_FOREACH(context->endpoint, ep) {
812 SESSIONS_ITER(ep->sessions, s, rtmp) {
813 if (s->delayqueue)
814 return 1;
815 if (s->lg_xmit)
816 return 1;
817 if (s->lg_srcv)
818 return 1;
819 }
820 }
821#endif /* COAP_SERVER_SUPPORT */
822#if COAP_CLIENT_SUPPORT
823 SESSIONS_ITER(context->sessions, s, rtmp) {
824 if (s->delayqueue)
825 return 1;
826 if (s->lg_xmit)
827 return 1;
828 if (s->lg_crcv)
829 return 1;
830 }
831#endif /* COAP_CLIENT_SUPPORT */
832 return 0;
833}
834
835const char *
837 return strerror(error);
838}
839#ifdef _WIN32
840const char *
842 coap_win_error_to_errno();
843 return coap_socket_format_errno(errno);
844}
845#else /* _WIN32 */
846const char *
848 return coap_socket_format_errno(errno);
849}
850#endif /* _WIN32 */
851
854#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
855 return sock->fd;
856#else
857 (void)(sock);
858 return COAP_INVALID_SOCKET;
859#endif
860}
861
864 return sock->flags;
865}
866
867COAP_API void
869 sock->flags = flags;
870}
#define COAP_CLIENT_SUPPORT
const char * coap_socket_format_errno(int error)
Definition coap_io.c:836
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:664
const char * coap_socket_strerror(void)
Definition coap_io.c:847
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
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
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:796
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:680
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:314
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_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:782
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:645
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:868
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:853
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:863
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:230
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:4963
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:2259
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_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:142
#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_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:271
@ COAP_PROTO_DTLS
Definition coap_pdu.h:321
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_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.
#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
Queue entry.
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
unsigned ref_subscriptions
reference count of current subscriptions
coap_socket_t sock
socket object for the session, if any
coap_session_state_t state
current state of relationship with peer
uint8_t client_initiated
Session initially started as a client.
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_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