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