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