1 /*
2  * net.h -- CoAP network interface
3  *
4  * Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * This file is part of the CoAP library libcoap. Please see README for terms
9  * of use.
10  */
11 
12 #ifndef COAP_NET_H_
13 #define COAP_NET_H_
14 
15 #include <stdlib.h>
16 #include <string.h>
17 #ifndef _WIN32
18 #include <sys/time.h>
19 #endif
20 #include <time.h>
21 
22 #ifdef WITH_LWIP
23 #include <lwip/ip_addr.h>
24 #endif
25 
26 #include "coap_io.h"
27 #include "coap_dtls.h"
28 #include "coap_event.h"
29 #include "pdu.h"
30 #include "coap_session.h"
31 
32 /**
33  * @defgroup context Context Handling
34  * API functions for handling PDUs using CoAP Contexts
35  * @{
36  */
37 
38 typedef enum coap_response_t {
39   COAP_RESPONSE_FAIL, /**< Response not liked - send CoAP RST packet */
40   COAP_RESPONSE_OK    /**< Response is fine */
41 } coap_response_t;
42 
43 /**
44  * Response handler that is used as callback in coap_context_t.
45  *
46  * @param session CoAP session.
47  * @param sent The PDU that was transmitted.
48  * @param received The PDU that was received.
49  * @param mid CoAP transaction ID.
50 
51  * @return @c COAP_RESPONSE_OK if successful, else @c COAP_RESPONSE_FAIL which
52  *         triggers sending a RST packet.
53  */
54 typedef coap_response_t (*coap_response_handler_t)(coap_session_t *session,
55                                                    const coap_pdu_t *sent,
56                                                    const coap_pdu_t *received,
57                                                    const coap_mid_t mid);
58 
59 /**
60  * Negative Acknowedge handler that is used as callback in coap_context_t.
61  *
62  * @param session CoAP session.
63  * @param sent The PDU that was transmitted.
64  * @param reason The reason for the NACK.
65  * @param mid CoAP message ID.
66  */
67 typedef void (*coap_nack_handler_t)(coap_session_t *session,
68                                     const coap_pdu_t *sent,
69                                     const coap_nack_reason_t reason,
70                                     const coap_mid_t mid);
71 
72 /**
73  * Received Ping handler that is used as callback in coap_context_t.
74  *
75  * @param session CoAP session.
76  * @param received The PDU that was received.
77  * @param mid CoAP message ID.
78  */
79 typedef void (*coap_ping_handler_t)(coap_session_t *session,
80                                     const coap_pdu_t *received,
81                                     const coap_mid_t mid);
82 
83 /**
84  * Received Pong handler that is used as callback in coap_context_t.
85  *
86  * @param session CoAP session.
87  * @param received The PDU that was received.
88  * @param mid CoAP message ID.
89  */
90 typedef void (*coap_pong_handler_t)(coap_session_t *session,
91                                     const coap_pdu_t *received,
92                                     const coap_mid_t mid);
93 
94 /**
95  * Registers a new message handler that is called whenever a response is
96  * received.
97  *
98  * @param context The context to register the handler for.
99  * @param handler The response handler to register.
100  */
101 void
102 coap_register_response_handler(coap_context_t *context,
103                                coap_response_handler_t handler);
104 
105 /**
106  * Registers a new message handler that is called whenever a confirmable
107  * message (request or response) is dropped after all retries have been
108  * exhausted, or a rst message was received, or a network or TLS level
109  * event was received that indicates delivering the message is not possible.
110  *
111  * @param context The context to register the handler for.
112  * @param handler The nack handler to register.
113  */
114 void
115 coap_register_nack_handler(coap_context_t *context,
116                            coap_nack_handler_t handler);
117 
118 /**
119  * Registers a new message handler that is called whenever a CoAP Ping
120  * message is received.
121  *
122  * @param context The context to register the handler for.
123  * @param handler The ping handler to register.
124  */
125 void
126 coap_register_ping_handler(coap_context_t *context,
127                            coap_ping_handler_t handler);
128 
129 /**
130  * Registers a new message handler that is called whenever a CoAP Pong
131  * message is received.
132  *
133  * @param context The context to register the handler for.
134  * @param handler The pong handler to register.
135  */
136 void
137 coap_register_pong_handler(coap_context_t *context,
138                            coap_pong_handler_t handler);
139 
140 /**
141  * Registers the option type @p type with the given context object @p ctx.
142  *
143  * @param ctx  The context to use.
144  * @param type The option type to register.
145  */
146 void
147 coap_register_option(coap_context_t *ctx, uint16_t type);
148 
149 /**
150  * Creates a new coap_context_t object that will hold the CoAP stack status.
151  */
152 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
153 
154 /**
155  * Set the context's default PSK hint and/or key for a server.
156  *
157  * @param context The current coap_context_t object.
158  * @param hint    The default PSK server hint sent to a client. If NULL, PSK
159  *                authentication is disabled. Empty string is a valid hint.
160  * @param key     The default PSK key. If NULL, PSK authentication will fail.
161  * @param key_len The default PSK key's length. If @p 0, PSK authentication will
162  *                fail.
163  *
164  * @return @c 1 if successful, else @c 0.
165  */
166 int coap_context_set_psk( coap_context_t *context, const char *hint,
167                            const uint8_t *key, size_t key_len );
168 
169 /**
170  * Set the context's default PSK hint and/or key for a server.
171  *
172  * @param context    The current coap_context_t object.
173  * @param setup_data If NULL, PSK authentication will fail. PSK
174  *                   information required.
175  *
176  * @return @c 1 if successful, else @c 0.
177  */
178 int coap_context_set_psk2(coap_context_t *context,
179                           coap_dtls_spsk_t *setup_data);
180 
181 /**
182  * Set the context's default PKI information for a server.
183  *
184  * @param context        The current coap_context_t object.
185  * @param setup_data     If NULL, PKI authentication will fail. Certificate
186  *                       information required.
187  *
188  * @return @c 1 if successful, else @c 0.
189  */
190 int
191 coap_context_set_pki(coap_context_t *context,
192                      const coap_dtls_pki_t *setup_data);
193 
194 /**
195  * Set the context's default Root CA information for a client or server.
196  *
197  * @param context        The current coap_context_t object.
198  * @param ca_file        If not NULL, is the full path name of a PEM encoded
199  *                       file containing all the Root CAs to be used.
200  * @param ca_dir         If not NULL, points to a directory containing PEM
201  *                       encoded files containing all the Root CAs to be used.
202  *
203  * @return @c 1 if successful, else @c 0.
204  */
205 int
206 coap_context_set_pki_root_cas(coap_context_t *context,
207                               const char *ca_file,
208                               const char *ca_dir);
209 
210 /**
211  * Set the context keepalive timer for sessions.
212  * A keepalive message will be sent after if a session has been inactive,
213  * i.e. no packet sent or received, for the given number of seconds.
214  * For unreliable protocols, a CoAP Empty message will be sent. If a
215  * CoAP RST is not received, the CoAP Empty messages will get resent based
216  * on the Confirmable retry parameters until there is a failure timeout,
217  * at which point the session will be considered as disconnected.
218  * For reliable protocols, a CoAP PING message will be sent. If a CoAP PONG
219  * has not been received before the next PING is due to be sent, the session
220  * will be considered as disconnected.
221  *
222  * @param context        The coap_context_t object.
223  * @param seconds        Number of seconds for the inactivity timer, or zero
224  *                       to disable CoAP-level keepalive messages.
225  */
226 void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
227 
228 /**
229  * Get the libcoap internal file descriptor for using in an application's
230  * select() or returned as an event in an application's epoll_wait() call.
231  *
232  * @param context        The coap_context_t object.
233  *
234  * @return The libcoap file descriptor or @c -1 if epoll is not available.
235  */
236 int coap_context_get_coap_fd(const coap_context_t *context);
237 
238 /**
239  * Set the maximum idle sessions count. The number of server sessions that
240  * are currently not in use. If this number is exceeded, the least recently
241  * used server session is completely removed.
242  * 0 (the default) means that the number is not monitored.
243  *
244  * @param context           The coap_context_t object.
245  * @param max_idle_sessions The maximum idle session count.
246  */
247 void
248 coap_context_set_max_idle_sessions(coap_context_t *context,
249                                    unsigned int max_idle_sessions);
250 
251 /**
252  * Get the maximum idle sessions count.
253  *
254  * @param context The coap_context_t object.
255  *
256  * @return The count of max idle sessions.
257  */
258 unsigned int
259 coap_context_get_max_idle_sessions(const coap_context_t *context);
260 
261 /**
262  * Set the session timeout value. The number of seconds of inactivity after
263  * which an unused server session will be closed.
264  * 0 means use default (300 secs).
265  *
266  * @param context         The coap_context_t object.
267  * @param session_timeout The session timeout value.
268  */
269 void
270 coap_context_set_session_timeout(coap_context_t *context,
271                                    unsigned int session_timeout);
272 
273 /**
274  * Get the session timeout value
275  *
276  * @param context The coap_context_t object.
277  *
278  * @return The session timeout value.
279  */
280 unsigned int
281 coap_context_get_session_timeout(const coap_context_t *context);
282 
283 /**
284  * Set the CSM timeout value. The number of seconds to wait for a (TCP) CSM
285  * negotiation response from the peer.
286  * 0 (the default) means use wait forever.
287  *
288  * @param context    The coap_context_t object.
289  * @param csm_tmeout The CSM timeout value.
290  */
291 void
292 coap_context_set_csm_timeout(coap_context_t *context,
293                              unsigned int csm_tmeout);
294 
295 /**
296  * Get the CSM timeout value
297  *
298  * @param context The coap_context_t object.
299  *
300  * @return The CSM timeout value.
301  */
302 unsigned int
303 coap_context_get_csm_timeout(const coap_context_t *context);
304 
305 /**
306  * Set the maximum number of sessions in (D)TLS handshake value. If this number
307  * is exceeded, the least recently used server session in handshake is
308  * completely removed.
309  * 0 (the default) means that the number is not monitored.
310  *
311  * @param context         The coap_context_t object.
312  * @param max_handshake_sessions The maximum number of sessions in handshake.
313  */
314 void
315 coap_context_set_max_handshake_sessions(coap_context_t *context,
316                                         unsigned int max_handshake_sessions);
317 
318 /**
319  * Get the session timeout value
320  *
321  * @param context The coap_context_t object.
322  *
323  * @return The maximim number of sessions in (D)TLS handshake value.
324  */
325 unsigned int
326 coap_context_get_max_handshake_sessions(const coap_context_t *context);
327 
328 /**
329  * Returns a new message id and updates @p session->tx_mid accordingly. The
330  * message id is returned in network byte order to make it easier to read in
331  * tracing tools.
332  *
333  * @param session The current coap_session_t object.
334  *
335  * @return        Incremented message id in network byte order.
336  */
337 uint16_t coap_new_message_id(coap_session_t *session);
338 
339 /**
340  * CoAP stack context must be released with coap_free_context(). This function
341  * clears all entries from the receive queue and send queue and deletes the
342  * resources that have been registered with @p context, and frees the attached
343  * endpoints.
344  *
345  * @param context The current coap_context_t object to free off.
346  */
347 void coap_free_context(coap_context_t *context);
348 
349 /**
350  * Stores @p data with the given CoAP context. This function
351  * overwrites any value that has previously been stored with @p
352  * context.
353  *
354  * @param context The CoAP context.
355  * @param data The data to store with wih the context. Note that this data
356  *             must be valid during the lifetime of @p context.
357  */
358 void coap_set_app_data(coap_context_t *context, void *data);
359 
360 /**
361  * Returns any application-specific data that has been stored with @p
362  * context using the function coap_set_app_data(). This function will
363  * return @c NULL if no data has been stored.
364  *
365  * @param context The CoAP context.
366  *
367  * @return The data previously stored or @c NULL if not data stored.
368  */
369 void *coap_get_app_data(const coap_context_t *context);
370 
371 /**
372  * Creates a new ACK PDU with specified error @p code. The options specified by
373  * the filter expression @p opts will be copied from the original request
374  * contained in @p request. Unless @c SHORT_ERROR_RESPONSE was defined at build
375  * time, the textual reason phrase for @p code will be added as payload, with
376  * Content-Type @c 0.
377  * This function returns a pointer to the new response message, or @c NULL on
378  * error. The storage allocated for the new message must be released with
379  * coap_free().
380  *
381  * @param request Specification of the received (confirmable) request.
382  * @param code    The error code to set.
383  * @param opts    An option filter that specifies which options to copy from
384  *                the original request in @p node.
385  *
386  * @return        A pointer to the new message or @c NULL on error.
387  */
388 coap_pdu_t *coap_new_error_response(const coap_pdu_t *request,
389                                     coap_pdu_code_t code,
390                                     coap_opt_filter_t *opts);
391 
392 /**
393  * Sends an error response with code @p code for request @p request to @p dst.
394  * @p opts will be passed to coap_new_error_response() to copy marked options
395  * from the request. This function returns the message id if the message was
396  * sent, or @c COAP_INVALID_MID otherwise.
397  *
398  * @param session         The CoAP session.
399  * @param request         The original request to respond to.
400  * @param code            The response code.
401  * @param opts            A filter that specifies the options to copy from the
402  *                        @p request.
403  *
404  * @return                The message id if the message was sent, or @c
405  *                        COAP_INVALID_MID otherwise.
406  */
407 coap_mid_t coap_send_error(coap_session_t *session,
408                            const coap_pdu_t *request,
409                            coap_pdu_code_t code,
410                            coap_opt_filter_t *opts);
411 
412 /**
413  * Helper function to create and send a message with @p type (usually ACK or
414  * RST). This function returns @c COAP_INVALID_MID when the message was not
415  * sent, a valid transaction id otherwise.
416  *
417  * @param session         The CoAP session.
418  * @param request         The request that should be responded to.
419  * @param type            Which type to set.
420  * @return                message id on success or @c COAP_INVALID_MID
421  *                        otherwise.
422  */
423 coap_mid_t
424 coap_send_message_type(coap_session_t *session, const coap_pdu_t *request,
425                        coap_pdu_type_t type);
426 
427 /**
428  * Sends an ACK message with code @c 0 for the specified @p request to @p dst.
429  * This function returns the corresponding message id if the message was
430  * sent or @c COAP_INVALID_MID on error.
431  *
432  * @param session         The CoAP session.
433  * @param request         The request to be acknowledged.
434  *
435  * @return                The message id if ACK was sent or @c
436  *                        COAP_INVALID_MID on error.
437  */
438 coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request);
439 
440 /**
441  * Sends an RST message with code @c 0 for the specified @p request to @p dst.
442  * This function returns the corresponding message id if the message was
443  * sent or @c COAP_INVALID_MID on error.
444  *
445  * @param session         The CoAP session.
446  * @param request         The request to be reset.
447  *
448  * @return                The message id if RST was sent or @c
449  *                        COAP_INVALID_MID on error.
450  */
451 COAP_STATIC_INLINE coap_mid_t
coap_send_rst(coap_session_t * session,const coap_pdu_t * request)452 coap_send_rst(coap_session_t *session, const coap_pdu_t *request) {
453   return coap_send_message_type(session, request, COAP_MESSAGE_RST);
454 }
455 
456 /**
457 * Sends a CoAP message to given peer. The memory that is
458 * allocated for the pdu will be released by coap_send().
459 * The caller must not use the pdu after calling coap_send().
460 *
461 * @param session         The CoAP session.
462 * @param pdu             The CoAP PDU to send.
463 *
464 * @return                The message id of the sent message or @c
465 *                        COAP_INVALID_MID on error.
466 */
467 coap_mid_t coap_send( coap_session_t *session, coap_pdu_t *pdu );
468 
469 #define coap_send_large(session, pdu) coap_send(session, pdu)
470 
471 /**
472  * Invokes the event handler of @p context for the given @p event and
473  * @p data.
474  *
475  * @param context The CoAP context whose event handler is to be called.
476  * @param event   The event to deliver.
477  * @param session The session related to @p event.
478  * @return The result from the associated event handler or 0 if none was
479  * registered.
480  */
481 int coap_handle_event(coap_context_t *context,
482                       coap_event_t event,
483                       coap_session_t *session);
484 /**
485  * Returns 1 if there are no messages to send or to dispatch in the context's
486  * queues. */
487 int coap_can_exit(coap_context_t *context);
488 
489 /**
490  * Returns the current value of an internal tick counter. The counter counts \c
491  * COAP_TICKS_PER_SECOND ticks every second.
492  */
493 void coap_ticks(coap_tick_t *);
494 
495 /**
496  * Function interface for joining a multicast group for listening for the
497  * currently defined endpoints that are UDP.
498  *
499  * @param ctx       The current context.
500  * @param groupname The name of the group that is to be joined for listening.
501  * @param ifname    Network interface to join the group on, or NULL if first
502  *                  appropriate interface is to be chosen by the O/S.
503  *
504  * @return       0 on success, -1 on error
505  */
506 int
507 coap_join_mcast_group_intf(coap_context_t *ctx, const char *groupname,
508                            const char *ifname);
509 
510 #define coap_join_mcast_group(ctx, groupname) \
511             (coap_join_mcast_group_intf(ctx, groupname, NULL))
512 
513 /**
514  * Function interface for defining the hop count (ttl) for sending
515  * multicast traffic
516  *
517  * @param session The current contexsion.
518  * @param hops    The number of hops (ttl) to use before the multicast
519  *                packet expires.
520  *
521  * @return       1 on success, 0 on error
522  */
523 int
524 coap_mcast_set_hops(coap_session_t *session, size_t hops);
525 
526 /**@}*/
527 
528 /**
529  * @defgroup app_io Application I/O Handling
530  * API functions for Application Input / Output
531  * @{
532  */
533 
534 #define COAP_IO_WAIT    0
535 #define COAP_IO_NO_WAIT ((uint32_t)-1)
536 
537 /**
538  * The main I/O processing function.  All pending network I/O is completed,
539  * and then optionally waits for the next input packet.
540  *
541  * This internally calls coap_io_prepare_io(), then select() for the appropriate
542  * sockets, updates COAP_SOCKET_CAN_xxx where appropriate and then calls
543  * coap_io_do_io() before returning with the time spent in the function.
544  *
545  * Alternatively, if libcoap is compiled with epoll support, this internally
546  * calls coap_io_prepare_epoll(), then epoll_wait() for waiting for any file
547  * descriptors that have (internally) been set up with epoll_ctl() and
548  * finally coap_io_do_epoll() before returning with the time spent in the
549  * function.
550  *
551  * @param ctx The CoAP context
552  * @param timeout_ms Minimum number of milliseconds to wait for new packets
553  *                   before returning after doing any processing.
554  *                   If COAP_IO_WAIT, the call will block until the next
555  *                   internal action (e.g. packet retransmit) if any, or block
556  *                   until the next packet is received whichever is the sooner
557  *                   and do the necessary processing.
558  *                   If COAP_IO_NO_WAIT, the function will return immediately
559  *                   after processing without waiting for any new input
560  *                   packets to arrive.
561  *
562  * @return Number of milliseconds spent in function or @c -1 if there was
563  *         an error
564  */
565 int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
566 
567 #ifndef RIOT_VERSION
568 /**
569  * The main message processing loop with additional fds for internal select.
570  *
571  * @param ctx The CoAP context
572  * @param timeout_ms Minimum number of milliseconds to wait for new packets
573  *                   before returning after doing any processing.
574  *                   If COAP_IO_WAIT, the call will block until the next
575  *                   internal action (e.g. packet retransmit) if any, or block
576  *                   until the next packet is received whichever is the sooner
577  *                   and do the necessary processing.
578  *                   If COAP_IO_NO_WAIT, the function will return immediately
579  *                   after processing without waiting for any new input
580  *                   packets to arrive.
581  * @param nfds      The maximum FD set in readfds, writefds or exceptfds
582  *                  plus one,
583  * @param readfds   Read FDs to additionally check for in internal select()
584  *                  or NULL if not required.
585  * @param writefds  Write FDs to additionally check for in internal select()
586  *                  or NULL if not required.
587  * @param exceptfds Except FDs to additionally check for in internal select()
588  *                  or NULL if not required.
589  *
590  *
591  * @return Number of milliseconds spent in coap_io_process_with_fds, or @c -1
592  *         if there was an error.  If defined, readfds, writefds, exceptfds
593  *         are updated as returned by the internal select() call.
594  */
595 int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms,
596                              int nfds, fd_set *readfds, fd_set *writefds,
597                              fd_set *exceptfds);
598 #endif /* !RIOT_VERSION */
599 
600 /**@}*/
601 
602 /**
603  * @defgroup app_io_internal Application I/O Handling (Internal)
604  * Internal API functions for Application Input / Output
605  * @{
606  */
607 
608 /**
609 * Iterates through all the coap_socket_t structures embedded in endpoints or
610 * sessions associated with the @p ctx to determine which are wanting any
611 * read, write, accept or connect I/O (COAP_SOCKET_WANT_xxx is set). If set,
612 * the coap_socket_t is added to the @p sockets.
613 *
614 * Any now timed out delayed packet is transmitted, along with any packets
615 * associated with requested observable response.
616 *
617 * In addition, it returns when the next expected I/O is expected to take place
618 * (e.g. a packet retransmit).
619 *
620 * Prior to calling coap_io_do_io(), the @p sockets must be tested to see
621 * if any of the COAP_SOCKET_WANT_xxx have the appropriate information and if
622 * so, COAP_SOCKET_CAN_xxx is set. This typically will be done after using a
623 * select() call.
624 *
625 * Note: If epoll support is compiled into libcoap, coap_io_prepare_epoll() must
626 * be used instead of coap_io_prepare_io().
627 *
628 * Internal function.
629 *
630 * @param ctx The CoAP context
631 * @param sockets Array of socket descriptors, filled on output
632 * @param max_sockets Size of socket array.
633 * @param num_sockets Pointer to the number of valid entries in the socket
634 *                    arrays on output.
635 * @param now Current time.
636 *
637 * @return timeout Maxmimum number of milliseconds that can be used by a
638 *                 select() to wait for network events or 0 if wait should be
639 *                 forever.
640 */
641 unsigned int
642 coap_io_prepare_io(coap_context_t *ctx,
643   coap_socket_t *sockets[],
644   unsigned int max_sockets,
645   unsigned int *num_sockets,
646   coap_tick_t now
647 );
648 
649 /**
650  * Processes any outstanding read, write, accept or connect I/O as indicated
651  * in the coap_socket_t structures (COAP_SOCKET_CAN_xxx set) embedded in
652  * endpoints or sessions associated with @p ctx.
653  *
654  * Note: If epoll support is compiled into libcoap, coap_io_do_epoll() must
655  * be used instead of coap_io_do_io().
656  *
657  * Internal function.
658  *
659  * @param ctx The CoAP context
660  * @param now Current time
661  */
662 void coap_io_do_io(coap_context_t *ctx, coap_tick_t now);
663 
664 /**
665  * Any now timed out delayed packet is transmitted, along with any packets
666  * associated with requested observable response.
667  *
668  * In addition, it returns when the next expected I/O is expected to take place
669  * (e.g. a packet retransmit).
670  *
671  * Note: If epoll support is compiled into libcoap, coap_io_prepare_epoll() must
672  * be used instead of coap_io_prepare_io().
673  *
674  * Internal function.
675  *
676  * @param ctx The CoAP context
677  * @param now Current time.
678  *
679  * @return timeout Maxmimum number of milliseconds that can be used by a
680  *                 epoll_wait() to wait for network events or 0 if wait should be
681  *                 forever.
682  */
683 unsigned int
684 coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now);
685 
686 struct epoll_event;
687 
688 /**
689  * Process all the epoll events
690  *
691  * Note: If epoll support is compiled into libcoap, coap_io_do_epoll() must
692  * be used instead of coap_io_do_io().
693  *
694  * Internal function
695  *
696  * @param ctx    The current CoAP context.
697  * @param events The list of events returned from an epoll_wait() call.
698  * @param nevents The number of events.
699  *
700  */
701 void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event* events,
702                       size_t nevents);
703 
704 /**@}*/
705 
706 /**
707  * @deprecated Use coap_io_process() instead.
708  *
709  * This function just calls coap_io_process().
710  *
711  * @param ctx The CoAP context
712  * @param timeout_ms Minimum number of milliseconds to wait for new packets
713  *                   before returning after doing any processing.
714  *                   If COAP_IO_WAIT, the call will block until the next
715  *                   internal action (e.g. packet retransmit) if any, or block
716  *                   until the next packet is received whichever is the sooner
717  *                   and do the necessary processing.
718  *                   If COAP_IO_NO_WAIT, the function will return immediately
719  *                   after processing without waiting for any new input
720  *                   packets to arrive.
721  *
722  * @return Number of milliseconds spent in function or @c -1 if there was
723  *         an error
724  */
725 COAP_STATIC_INLINE COAP_DEPRECATED int
coap_run_once(coap_context_t * ctx,uint32_t timeout_ms)726 coap_run_once(coap_context_t *ctx, uint32_t timeout_ms)
727 {
728   return coap_io_process(ctx, timeout_ms);
729 }
730 
731 /**
732 * @deprecated Use coap_io_prepare_io() instead.
733 *
734 * This function just calls coap_io_prepare_io().
735 *
736 * Internal function.
737 *
738 * @param ctx The CoAP context
739 * @param sockets Array of socket descriptors, filled on output
740 * @param max_sockets Size of socket array.
741 * @param num_sockets Pointer to the number of valid entries in the socket
742 *                    arrays on output.
743 * @param now Current time.
744 *
745 * @return timeout Maxmimum number of milliseconds that can be used by a
746 *                 select() to wait for network events or 0 if wait should be
747 *                 forever.
748 */
749 COAP_STATIC_INLINE COAP_DEPRECATED unsigned int
coap_write(coap_context_t * ctx,coap_socket_t * sockets[],unsigned int max_sockets,unsigned int * num_sockets,coap_tick_t now)750 coap_write(coap_context_t *ctx,
751   coap_socket_t *sockets[],
752   unsigned int max_sockets,
753   unsigned int *num_sockets,
754   coap_tick_t now
755 ) {
756   return coap_io_prepare_io(ctx, sockets, max_sockets, num_sockets, now);
757 }
758 
759 /**
760  * @deprecated Use coap_io_do_io() instead.
761  *
762  * This function just calls coap_io_do_io().
763  *
764  * Internal function.
765  *
766  * @param ctx The CoAP context
767  * @param now Current time
768  */
769 COAP_STATIC_INLINE COAP_DEPRECATED void
coap_read(coap_context_t * ctx,coap_tick_t now)770 coap_read(coap_context_t *ctx, coap_tick_t now
771 ) {
772   coap_io_do_io(ctx, now);
773 }
774 
775 /* Old definitions which may be hanging around in old code - be helpful! */
776 #define COAP_RUN_NONBLOCK COAP_RUN_NONBLOCK_deprecated_use_COAP_IO_NO_WAIT
777 #define COAP_RUN_BLOCK COAP_RUN_BLOCK_deprecated_use_COAP_IO_WAIT
778 
779 #endif /* COAP_NET_H_ */
780