1 /*
2 * net.h -- CoAP network interface
3 *
4 * Copyright (C) 2010-2015 Olaf Bergmann <bergmann@tzi.org>
5 *
6 * This file is part of the CoAP library libcoap. Please see README for terms
7 * of use.
8 */
9
10 #ifndef COAP_NET_H_
11 #define COAP_NET_H_
12
13 #include <stdlib.h>
14 #include <string.h>
15 #ifndef _WIN32
16 #include <sys/time.h>
17 #endif
18 #include <time.h>
19
20 #ifdef WITH_LWIP
21 #include <lwip/ip_addr.h>
22 #endif
23
24 #include "coap_io.h"
25 #include "coap_dtls.h"
26 #include "coap_event.h"
27 #include "coap_time.h"
28 #include "option.h"
29 #include "pdu.h"
30 #include "prng.h"
31 #include "coap_session.h"
32
33 struct coap_queue_t;
34
35 /**
36 * Queue entry
37 */
38 typedef struct coap_queue_t {
39 struct coap_queue_t *next;
40 coap_tick_t t; /**< when to send PDU for the next time */
41 unsigned char retransmit_cnt; /**< retransmission counter, will be removed
42 * when zero */
43 unsigned int timeout; /**< the randomized timeout value */
44 coap_session_t *session; /**< the CoAP session */
45 coap_tid_t id; /**< CoAP transaction id */
46 coap_pdu_t *pdu; /**< the CoAP PDU to send */
47 } coap_queue_t;
48
49 /**
50 * Adds @p node to given @p queue, ordered by variable t in @p node.
51 *
52 * @param queue Queue to add to.
53 * @param node Node entry to add to Queue.
54 *
55 * @return @c 1 added to queue, @c 0 failure.
56 */
57 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
58
59 /**
60 * Destroys specified @p node.
61 *
62 * @param node Node entry to remove.
63 *
64 * @return @c 1 node deleted from queue, @c 0 failure.
65 */
66 int coap_delete_node(coap_queue_t *node);
67
68 /**
69 * Removes all items from given @p queue and frees the allocated storage.
70 *
71 * @param queue The queue to delete.
72 */
73 void coap_delete_all(coap_queue_t *queue);
74
75 /**
76 * Creates a new node suitable for adding to the CoAP sendqueue.
77 *
78 * @return New node entry, or @c NULL if failure.
79 */
80 coap_queue_t *coap_new_node(void);
81
82 struct coap_resource_t;
83 struct coap_context_t;
84 #ifndef WITHOUT_ASYNC
85 struct coap_async_state_t;
86 #endif
87
88 /**
89 * Response handler that is used as call-back in coap_context_t.
90 *
91 * @param context CoAP session.
92 * @param session CoAP session.
93 * @param sent The PDU that was transmitted.
94 * @param received The PDU that was received.
95 * @param id CoAP transaction ID.
96 */
97 typedef void (*coap_response_handler_t)(struct coap_context_t *context,
98 coap_session_t *session,
99 coap_pdu_t *sent,
100 coap_pdu_t *received,
101 const coap_tid_t id);
102
103 /**
104 * Negative Acknowedge handler that is used as call-back in coap_context_t.
105 *
106 * @param context CoAP session.
107 * @param session CoAP session.
108 * @param sent The PDU that was transmitted.
109 * @param reason The reason for the NACK.
110 * @param id CoAP transaction ID.
111 */
112 typedef void (*coap_nack_handler_t)(struct coap_context_t *context,
113 coap_session_t *session,
114 coap_pdu_t *sent,
115 coap_nack_reason_t reason,
116 const coap_tid_t id);
117
118 /**
119 * Recieved Ping handler that is used as call-back in coap_context_t.
120 *
121 * @param context CoAP session.
122 * @param session CoAP session.
123 * @param received The PDU that was received.
124 * @param id CoAP transaction ID.
125 */
126 typedef void (*coap_ping_handler_t)(struct coap_context_t *context,
127 coap_session_t *session,
128 coap_pdu_t *received,
129 const coap_tid_t id);
130
131 /**
132 * Recieved Pong handler that is used as call-back in coap_context_t.
133 *
134 * @param context CoAP session.
135 * @param session CoAP session.
136 * @param received The PDU that was received.
137 * @param id CoAP transaction ID.
138 */
139 typedef void (*coap_pong_handler_t)(struct coap_context_t *context,
140 coap_session_t *session,
141 coap_pdu_t *received,
142 const coap_tid_t id);
143
144 /**
145 * The CoAP stack's global state is stored in a coap_context_t object.
146 */
147 typedef struct coap_context_t {
148 coap_opt_filter_t known_options;
149 struct coap_resource_t *resources; /**< hash table or list of known
150 resources */
151 struct coap_resource_t *unknown_resource; /**< can be used for handling
152 unknown resources */
153
154 #ifndef WITHOUT_ASYNC
155 /**
156 * list of asynchronous transactions */
157 struct coap_async_state_t *async_state;
158 #endif /* WITHOUT_ASYNC */
159
160 /**
161 * The time stamp in the first element of the sendqeue is relative
162 * to sendqueue_basetime. */
163 coap_tick_t sendqueue_basetime;
164 coap_queue_t *sendqueue;
165 coap_endpoint_t *endpoint; /**< the endpoints used for listening */
166 coap_session_t *sessions; /**< client sessions */
167
168 #ifdef WITH_CONTIKI
169 struct uip_udp_conn *conn; /**< uIP connection object */
170 struct etimer retransmit_timer; /**< fires when the next packet must be sent */
171 struct etimer notify_timer; /**< used to check resources periodically */
172 #endif /* WITH_CONTIKI */
173
174 #ifdef WITH_LWIP
175 uint8_t timer_configured; /**< Set to 1 when a retransmission is
176 * scheduled using lwIP timers for this
177 * context, otherwise 0. */
178 #endif /* WITH_LWIP */
179
180 coap_response_handler_t response_handler;
181 coap_nack_handler_t nack_handler;
182 coap_ping_handler_t ping_handler;
183 coap_pong_handler_t pong_handler;
184
185 /**
186 * Callback function that is used to signal events to the
187 * application. This field is set by coap_set_event_handler().
188 */
189 coap_event_handler_t handle_event;
190
191 ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen);
192
193 ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t *packet);
194
195 size_t(*get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len);
196 size_t(*get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len);
197 size_t(*get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len);
198
199 void *dtls_context;
200 uint8_t *psk_hint;
201 size_t psk_hint_len;
202 uint8_t *psk_key;
203 size_t psk_key_len;
204
205 unsigned int session_timeout; /**< Number of seconds of inactivity after which an unused session will be closed. 0 means use default. */
206 unsigned int max_idle_sessions; /**< Maximum number of simultaneous unused sessions per endpoint. 0 means no maximum. */
207 unsigned int max_handshake_sessions; /**< Maximum number of simultaneous negotating sessions per endpoint. 0 means use default. */
208 unsigned int ping_timeout; /**< Minimum inactivity time before sending a ping message. 0 means disabled. */
209 unsigned int csm_timeout; /**< Timeout for waiting for a CSM from the remote side. 0 means disabled. */
210
211 void *app; /**< application-specific data */
212 #ifdef COAP_EPOLL_SUPPORT
213 int epfd; /**< External FD for epoll */
214 int eptimerfd; /**< Internal FD for timeout */
215 coap_tick_t next_timeout; /**< When the next timeout is to occur */
216 #endif /* COAP_EPOLL_SUPPORT */
217 } coap_context_t;
218
219 /**
220 * Registers a new message handler that is called whenever a response was
221 * received that matches an ongoing transaction.
222 *
223 * @param context The context to register the handler for.
224 * @param handler The response handler to register.
225 */
226 COAP_STATIC_INLINE void
coap_register_response_handler(coap_context_t * context,coap_response_handler_t handler)227 coap_register_response_handler(coap_context_t *context,
228 coap_response_handler_t handler) {
229 context->response_handler = handler;
230 }
231
232 /**
233 * Registers a new message handler that is called whenever a confirmable
234 * message (request or response) is dropped after all retries have been
235 * exhausted, or a rst message was received, or a network or TLS level
236 * event was received that indicates delivering the message is not possible.
237 *
238 * @param context The context to register the handler for.
239 * @param handler The nack handler to register.
240 */
241 COAP_STATIC_INLINE void
coap_register_nack_handler(coap_context_t * context,coap_nack_handler_t handler)242 coap_register_nack_handler(coap_context_t *context,
243 coap_nack_handler_t handler) {
244 context->nack_handler = handler;
245 }
246
247 /**
248 * Registers a new message handler that is called whenever a CoAP Ping
249 * message is received.
250 *
251 * @param context The context to register the handler for.
252 * @param handler The ping handler to register.
253 */
254 COAP_STATIC_INLINE void
coap_register_ping_handler(coap_context_t * context,coap_ping_handler_t handler)255 coap_register_ping_handler(coap_context_t *context,
256 coap_ping_handler_t handler) {
257 context->ping_handler = handler;
258 }
259
260 /**
261 * Registers a new message handler that is called whenever a CoAP Pong
262 * message is received.
263 *
264 * @param context The context to register the handler for.
265 * @param handler The pong handler to register.
266 */
267 COAP_STATIC_INLINE void
coap_register_pong_handler(coap_context_t * context,coap_pong_handler_t handler)268 coap_register_pong_handler(coap_context_t *context,
269 coap_pong_handler_t handler) {
270 context->pong_handler = handler;
271 }
272
273 /**
274 * Registers the option type @p type with the given context object @p ctx.
275 *
276 * @param ctx The context to use.
277 * @param type The option type to register.
278 */
279 COAP_STATIC_INLINE void
coap_register_option(coap_context_t * ctx,uint16_t type)280 coap_register_option(coap_context_t *ctx, uint16_t type) {
281 coap_option_setb(ctx->known_options, type);
282 }
283
284 /**
285 * Set sendqueue_basetime in the given context object @p ctx to @p now. This
286 * function returns the number of elements in the queue head that have timed
287 * out.
288 */
289 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
290
291 /**
292 * Returns the next pdu to send without removing from sendqeue.
293 */
294 coap_queue_t *coap_peek_next( coap_context_t *context );
295
296 /**
297 * Returns the next pdu to send and removes it from the sendqeue.
298 */
299 coap_queue_t *coap_pop_next( coap_context_t *context );
300
301 /**
302 * Creates a new coap_context_t object that will hold the CoAP stack status.
303 */
304 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
305
306 /**
307 * Set the context's default PSK hint and/or key for a server.
308 *
309 * @param context The current coap_context_t object.
310 * @param hint The default PSK server hint sent to a client. If @p NULL, PSK
311 * authentication is disabled. Empty string is a valid hint.
312 * @param key The default PSK key. If @p NULL, PSK authentication will fail.
313 * @param key_len The default PSK key's length. If @p 0, PSK authentication will
314 * fail.
315 *
316 * @return @c 1 if successful, else @c 0.
317 */
318 int coap_context_set_psk( coap_context_t *context, const char *hint,
319 const uint8_t *key, size_t key_len );
320
321 /**
322 * Set the context's default PKI information for a server.
323 *
324 * @param context The current coap_context_t object.
325 * @param setup_data If @p NULL, PKI authentication will fail. Certificate
326 * information required.
327 *
328 * @return @c 1 if successful, else @c 0.
329 */
330 int
331 coap_context_set_pki(coap_context_t *context,
332 coap_dtls_pki_t *setup_data);
333
334 /**
335 * Set the context's default Root CA information for a client or server.
336 *
337 * @param context The current coap_context_t object.
338 * @param ca_file If not @p NULL, is the full path name of a PEM encoded
339 * file containing all the Root CAs to be used.
340 * @param ca_dir If not @p NULL, points to a directory containing PEM
341 * encoded files containing all the Root CAs to be used.
342 *
343 * @return @c 1 if successful, else @c 0.
344 */
345 int
346 coap_context_set_pki_root_cas(coap_context_t *context,
347 const char *ca_file,
348 const char *ca_dir);
349
350 /**
351 * Set the context keepalive timer for sessions.
352 * A keepalive message will be sent after if a session has been inactive,
353 * i.e. no packet sent or received, for the given number of seconds.
354 * For unreliable protocols, a CoAP Empty message will be sent. If a
355 * CoAP RST is not received, the CoAP Empty messages will get resent based
356 * on the Confirmable retry parameters until there is a failure timeout,
357 * at which point the session will be considered as disconnected.
358 * For reliable protocols, a CoAP PING message will be sent. If a CoAP PONG
359 * has not been received before the next PING is due to be sent, the session
360 * will be considered as disconnected.
361 *
362 * @param context The coap_context_t object.
363 * @param seconds Number of seconds for the inactivity timer, or zero
364 * to disable CoAP-level keepalive messages.
365 *
366 * @return 1 if successful, else 0
367 */
368 void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
369
370 /**
371 * Get the libcoap internal file descriptor for using in an application's
372 * select() or returned as an event in an application's epoll_wait() call.
373 *
374 * @param context The coap_context_t object.
375 *
376 * @return The libcoap file descriptor or @c -1 if epoll is not available.
377 */
378 int coap_context_get_coap_fd(coap_context_t *context);
379
380 /**
381 * Returns a new message id and updates @p session->tx_mid accordingly. The
382 * message id is returned in network byte order to make it easier to read in
383 * tracing tools.
384 *
385 * @param session The current coap_session_t object.
386 *
387 * @return Incremented message id in network byte order.
388 */
389 COAP_STATIC_INLINE uint16_t
coap_new_message_id(coap_session_t * session)390 coap_new_message_id(coap_session_t *session) {
391 return ++session->tx_mid;
392 }
393
394 /**
395 * CoAP stack context must be released with coap_free_context(). This function
396 * clears all entries from the receive queue and send queue and deletes the
397 * resources that have been registered with @p context, and frees the attached
398 * endpoints.
399 *
400 * @param context The current coap_context_t object to free off.
401 */
402 void coap_free_context(coap_context_t *context);
403
404 /**
405 * Stores @p data with the given CoAP context. This function
406 * overwrites any value that has previously been stored with @p
407 * context.
408 *
409 * @param context The CoAP context.
410 * @param data The data to store with wih the context. Note that this data
411 * must be valid during the lifetime of @p context.
412 */
413 void coap_set_app_data(coap_context_t *context, void *data);
414
415 /**
416 * Returns any application-specific data that has been stored with @p
417 * context using the function coap_set_app_data(). This function will
418 * return @c NULL if no data has been stored.
419 *
420 * @param context The CoAP context.
421 *
422 * @return The data previously stored or @c NULL if not data stored.
423 */
424 void *coap_get_app_data(const coap_context_t *context);
425
426 /**
427 * Creates a new ACK PDU with specified error @p code. The options specified by
428 * the filter expression @p opts will be copied from the original request
429 * contained in @p request. Unless @c SHORT_ERROR_RESPONSE was defined at build
430 * time, the textual reason phrase for @p code will be added as payload, with
431 * Content-Type @c 0.
432 * This function returns a pointer to the new response message, or @c NULL on
433 * error. The storage allocated for the new message must be relased with
434 * coap_free().
435 *
436 * @param request Specification of the received (confirmable) request.
437 * @param code The error code to set.
438 * @param opts An option filter that specifies which options to copy from
439 * the original request in @p node.
440 *
441 * @return A pointer to the new message or @c NULL on error.
442 */
443 coap_pdu_t *coap_new_error_response(coap_pdu_t *request,
444 unsigned char code,
445 coap_opt_filter_t opts);
446
447 /**
448 * Sends an error response with code @p code for request @p request to @p dst.
449 * @p opts will be passed to coap_new_error_response() to copy marked options
450 * from the request. This function returns the transaction id if the message was
451 * sent, or @c COAP_INVALID_TID otherwise.
452 *
453 * @param session The CoAP session.
454 * @param request The original request to respond to.
455 * @param code The response code.
456 * @param opts A filter that specifies the options to copy from the
457 * @p request.
458 *
459 * @return The transaction id if the message was sent, or @c
460 * COAP_INVALID_TID otherwise.
461 */
462 coap_tid_t coap_send_error(coap_session_t *session,
463 coap_pdu_t *request,
464 unsigned char code,
465 coap_opt_filter_t opts);
466
467 /**
468 * Helper funktion to create and send a message with @p type (usually ACK or
469 * RST). This function returns @c COAP_INVALID_TID when the message was not
470 * sent, a valid transaction id otherwise.
471 *
472 * @param session The CoAP session.
473 * @param request The request that should be responded to.
474 * @param type Which type to set.
475 * @return transaction id on success or @c COAP_INVALID_TID
476 * otherwise.
477 */
478 coap_tid_t
479 coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type);
480
481 /**
482 * Sends an ACK message with code @c 0 for the specified @p request to @p dst.
483 * This function returns the corresponding transaction id if the message was
484 * sent or @c COAP_INVALID_TID on error.
485 *
486 * @param session The CoAP session.
487 * @param request The request to be acknowledged.
488 *
489 * @return The transaction id if ACK was sent or @c
490 * COAP_INVALID_TID on error.
491 */
492 coap_tid_t coap_send_ack(coap_session_t *session, coap_pdu_t *request);
493
494 /**
495 * Sends an RST message with code @c 0 for the specified @p request to @p dst.
496 * This function returns the corresponding transaction id if the message was
497 * sent or @c COAP_INVALID_TID on error.
498 *
499 * @param session The CoAP session.
500 * @param request The request to be reset.
501 *
502 * @return The transaction id if RST was sent or @c
503 * COAP_INVALID_TID on error.
504 */
505 COAP_STATIC_INLINE coap_tid_t
coap_send_rst(coap_session_t * session,coap_pdu_t * request)506 coap_send_rst(coap_session_t *session, coap_pdu_t *request) {
507 return coap_send_message_type(session, request, COAP_MESSAGE_RST);
508 }
509
510 /**
511 * Sends a CoAP message to given peer. The memory that is
512 * allocated by pdu will be released by coap_send().
513 * The caller must not use the pdu after calling coap_send().
514 *
515 * @param session The CoAP session.
516 * @param pdu The CoAP PDU to send.
517 *
518 * @return The message id of the sent message or @c
519 * COAP_INVALID_TID on error.
520 */
521 coap_tid_t coap_send( coap_session_t *session, coap_pdu_t *pdu );
522
523 /**
524 * Handles retransmissions of confirmable messages
525 *
526 * @param context The CoAP context.
527 * @param node The node to retransmit.
528 *
529 * @return The message id of the sent message or @c
530 * COAP_INVALID_TID on error.
531 */
532 coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node);
533
534 /**
535 * For applications with their own message loop, send all pending retransmits and
536 * return the list of sockets with events to wait for and the next timeout
537 * The application should call coap_read, then coap_write again when any condition below is true:
538 * - data is available on any of the sockets with the COAP_SOCKET_WANT_READ
539 * - an incoming connection is pending in the listen queue and the COAP_SOCKET_WANT_ACCEPT flag is set
540 * - at least some data can be written without blocking on any of the sockets with the COAP_SOCKET_WANT_WRITE flag set
541 * - a connection event occured (success or failure) and the COAP_SOCKET_WANT_CONNECT flag is set
542 * - the timeout has expired
543 * Before calling coap_read or coap_write again, the application should position COAP_SOCKET_CAN_READ and COAP_SOCKET_CAN_WRITE flags as applicable.
544 *
545 * @param ctx The CoAP context
546 * @param sockets array of socket descriptors, filled on output
547 * @param max_sockets size of socket array.
548 * @param num_sockets pointer to the number of valid entries in the socket arrays on output
549 * @param now Current time.
550 *
551 * @return timeout as maxmimum number of milliseconds that the application should wait for network events or 0 if the application should wait forever.
552 */
553
554 unsigned int
555 coap_write(coap_context_t *ctx,
556 coap_socket_t *sockets[],
557 unsigned int max_sockets,
558 unsigned int *num_sockets,
559 coap_tick_t now
560 );
561
562 /**
563 * For applications with their own message loop, reads all data from the network.
564 *
565 * @param ctx The CoAP context
566 * @param now Current time
567 */
568 void coap_read(coap_context_t *ctx, coap_tick_t now);
569
570 #define COAP_RUN_BLOCK 0
571 #define COAP_RUN_NONBLOCK 1
572
573 /**
574 * The main message processing loop.
575 *
576 * @param ctx The CoAP context
577 * @param timeout_ms Minimum number of milliseconds to wait for new packets
578 * before returning. If COAP_RUN_BLOCK, the call will block
579 * until at least one new packet is received. If
580 * COAP_RUN_NONBLOCK, the function will return immediately
581 * following without waiting for any new input not already
582 * available.
583 *
584 * @return Number of milliseconds spent in coap_run_once, or @c -1 if there
585 * was an error
586 */
587
588 int coap_run_once( coap_context_t *ctx, unsigned int timeout_ms );
589
590 /**
591 * Any now timed out delayed packet is transmitted, along with any packets
592 * associated with requested observable response.
593 *
594 * In addition, it returns when the next expected I/O is expected to take place
595 * (e.g. a packet retransmit).
596 *
597 * Note: If epoll support is compiled into libcoap, coap_io_prepare_epoll() must
598 * be used instead of coap_write().
599 *
600 * Internal function.
601 *
602 * @param ctx The CoAP context
603 * @param now Current time.
604 *
605 * @return timeout Maxmimum number of milliseconds that can be used by a
606 * epoll_wait() to wait for network events or 0 if wait should be
607 * forever.
608 */
609 unsigned int
610 coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now);
611
612 struct epoll_event;
613 /**
614 * Process all the epoll events
615 *
616 * Internal function
617 *
618 * @param ctx The current CoAP context.
619 * @param events The list of events returned from an epoll_wait() call.
620 * @param nevents The number of events.
621 *
622 */
623 void coap_io_do_events(coap_context_t *ctx, struct epoll_event* events,
624 size_t nevents);
625
626 /**
627 * Parses and interprets a CoAP datagram with context @p ctx. This function
628 * returns @c 0 if the datagram was handled, or a value less than zero on
629 * error.
630 *
631 * @param ctx The current CoAP context.
632 * @param session The current CoAP session.
633 * @param data The received packet'd data.
634 * @param data_len The received packet'd data length.
635 *
636 * @return @c 0 if message was handled successfully, or less than zero on
637 * error.
638 */
639 int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len);
640
641 /**
642 * Invokes the event handler of @p context for the given @p event and
643 * @p data.
644 *
645 * @param context The CoAP context whose event handler is to be called.
646 * @param event The event to deliver.
647 * @param session The session related to @p event.
648 * @return The result from the associated event handler or 0 if none was
649 * registered.
650 */
651 int coap_handle_event(coap_context_t *context,
652 coap_event_t event,
653 coap_session_t *session);
654 /**
655 * This function removes the element with given @p id from the list given list.
656 * If @p id was found, @p node is updated to point to the removed element. Note
657 * that the storage allocated by @p node is @b not released. The caller must do
658 * this manually using coap_delete_node(). This function returns @c 1 if the
659 * element with id @p id was found, @c 0 otherwise. For a return value of @c 0,
660 * the contents of @p node is undefined.
661 *
662 * @param queue The queue to search for @p id.
663 * @param session The session to look for.
664 * @param id The transaction id to look for.
665 * @param node If found, @p node is updated to point to the removed node. You
666 * must release the storage pointed to by @p node manually.
667 *
668 * @return @c 1 if @p id was found, @c 0 otherwise.
669 */
670 int coap_remove_from_queue(coap_queue_t **queue,
671 coap_session_t *session,
672 coap_tid_t id,
673 coap_queue_t **node);
674
675 coap_tid_t
676 coap_wait_ack( coap_context_t *context, coap_session_t *session,
677 coap_queue_t *node);
678
679 /**
680 * Retrieves transaction from the queue.
681 *
682 * @param queue The transaction queue to be searched.
683 * @param session The session to find.
684 * @param id The transaction id to find.
685 *
686 * @return A pointer to the transaction object or @c NULL if not found.
687 */
688 coap_queue_t *coap_find_transaction(coap_queue_t *queue, coap_session_t *session, coap_tid_t id);
689
690 /**
691 * Cancels all outstanding messages for session @p session that have the specified
692 * token.
693 *
694 * @param context The context in use.
695 * @param session Session of the messages to remove.
696 * @param token Message token.
697 * @param token_length Actual length of @p token.
698 */
699 void coap_cancel_all_messages(coap_context_t *context,
700 coap_session_t *session,
701 const uint8_t *token,
702 size_t token_length);
703
704 /**
705 * Cancels all outstanding messages for session @p session.
706 *
707 * @param context The context in use.
708 * @param session Session of the messages to remove.
709 * @param reason The reasion for the session cancellation
710 */
711 void
712 coap_cancel_session_messages(coap_context_t *context,
713 coap_session_t *session,
714 coap_nack_reason_t reason);
715
716 /**
717 * Dispatches the PDUs from the receive queue in given context.
718 */
719 void coap_dispatch(coap_context_t *context, coap_session_t *session,
720 coap_pdu_t *pdu);
721
722 /**
723 * Returns 1 if there are no messages to send or to dispatch in the context's
724 * queues. */
725 int coap_can_exit(coap_context_t *context);
726
727 /**
728 * Returns the current value of an internal tick counter. The counter counts \c
729 * COAP_TICKS_PER_SECOND ticks every second.
730 */
731 void coap_ticks(coap_tick_t *);
732
733 /**
734 * Verifies that @p pdu contains no unknown critical options. Options must be
735 * registered at @p ctx, using the function coap_register_option(). A basic set
736 * of options is registered automatically by coap_new_context(). This function
737 * returns @c 1 if @p pdu is ok, @c 0 otherwise. The given filter object @p
738 * unknown will be updated with the unknown options. As only @c COAP_MAX_OPT
739 * options can be signalled this way, remaining options must be examined
740 * manually.
741 *
742 * @code
743 coap_opt_filter_t f = COAP_OPT_NONE;
744 coap_opt_iterator_t opt_iter;
745
746 if (coap_option_check_critical(ctx, pdu, f) == 0) {
747 coap_option_iterator_init(pdu, &opt_iter, f);
748
749 while (coap_option_next(&opt_iter)) {
750 if (opt_iter.type & 0x01) {
751 ... handle unknown critical option in opt_iter ...
752 }
753 }
754 }
755 @endcode
756 *
757 * @param ctx The context where all known options are registered.
758 * @param pdu The PDU to check.
759 * @param unknown The output filter that will be updated to indicate the
760 * unknown critical options found in @p pdu.
761 *
762 * @return @c 1 if everything was ok, @c 0 otherwise.
763 */
764 int coap_option_check_critical(coap_context_t *ctx,
765 coap_pdu_t *pdu,
766 coap_opt_filter_t unknown);
767
768 /**
769 * Creates a new response for given @p request with the contents of @c
770 * .well-known/core. The result is NULL on error or a newly allocated PDU that
771 * must be either sent with coap_sent() or released by coap_delete_pdu().
772 *
773 * @param context The current coap context to use.
774 * @param session The CoAP session.
775 * @param request The request for @c .well-known/core .
776 *
777 * @return A new 2.05 response for @c .well-known/core or NULL on error.
778 */
779 coap_pdu_t *coap_wellknown_response(coap_context_t *context,
780 coap_session_t *session,
781 coap_pdu_t *request);
782
783 /**
784 * Calculates the initial timeout based on the session CoAP transmission
785 * parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND.
786 * The calculation requires 'ack_timeout' and 'ack_random_factor' to be in
787 * Qx.FRAC_BITS fixed point notation, whereas the passed parameter @p r
788 * is interpreted as the fractional part of a Q0.MAX_BITS random value.
789 *
790 * @param session session timeout is associated with
791 * @param r random value as fractional part of a Q0.MAX_BITS fixed point
792 * value
793 * @return COAP_TICKS_PER_SECOND * 'ack_timeout' *
794 * (1 + ('ack_random_factor' - 1) * r)
795 */
796 unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
797
798 /**
799 * Function interface for joining a multicast group for listening
800 *
801 * @param ctx The current context
802 * @param groupname The name of the group that is to be joined for listening
803 *
804 * @return 0 on success, -1 on error
805 */
806 int
807 coap_join_mcast_group(coap_context_t *ctx, const char *groupname);
808
809 #endif /* COAP_NET_H_ */
810