libcoap 4.3.2rc2

coap_net.h
Go to the documentation of this file.
1/*
2 * coap_net.h -- CoAP context interface
3 *
4 * Copyright (C) 2010-2023 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
17#ifndef COAP_NET_H_
18#define COAP_NET_H_
19
20#include <stdlib.h>
21#include <string.h>
22#ifndef _WIN32
23#include <sys/select.h>
24#include <sys/time.h>
25#endif
26#include <time.h>
27
28#ifdef WITH_LWIP
29#include <lwip/ip_addr.h>
30#endif
31
32#include "coap_io.h"
33#include "coap_dtls.h"
34#include "coap_event.h"
35#include "coap_pdu.h"
36#include "coap_session.h"
37#include "coap_debug.h"
38
46 typedef enum coap_response_t {
47 COAP_RESPONSE_FAIL,
48 COAP_RESPONSE_OK
49} coap_response_t;
50
62 typedef coap_response_t (*coap_response_handler_t)(coap_session_t *session,
63 const coap_pdu_t *sent,
64 const coap_pdu_t *received,
65 const coap_mid_t mid);
66
75 typedef void (*coap_nack_handler_t)(coap_session_t *session,
76 const coap_pdu_t *sent,
77 const coap_nack_reason_t reason,
78 const coap_mid_t mid);
79
87 typedef void (*coap_ping_handler_t)(coap_session_t *session,
88 const coap_pdu_t *received,
89 const coap_mid_t mid);
90
98 typedef void (*coap_pong_handler_t)(coap_session_t *session,
99 const coap_pdu_t *received,
100 const coap_mid_t mid);
101
109 void coap_register_response_handler(coap_context_t *context,
110 coap_response_handler_t handler);
111
121 void coap_register_nack_handler(coap_context_t *context,
122 coap_nack_handler_t handler);
123
131 void coap_register_ping_handler(coap_context_t *context,
132 coap_ping_handler_t handler);
133
141 void coap_register_pong_handler(coap_context_t *context,
142 coap_pong_handler_t handler);
143
150 void coap_register_option(coap_context_t *ctx, uint16_t type);
151
155 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
156
171 int coap_context_set_psk(coap_context_t *context, const char *hint,
172 const uint8_t *key, size_t key_len);
173
183 int coap_context_set_psk2(coap_context_t *context,
184 coap_dtls_spsk_t *setup_data);
185
195 int coap_context_set_pki(coap_context_t *context,
196 const coap_dtls_pki_t *setup_data);
197
209 int coap_context_set_pki_root_cas(coap_context_t *context,
210 const char *ca_file,
211 const char *ca_dir);
212
229 void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
230
238 void coap_context_set_max_token_size(coap_context_t *context,
239 size_t max_token_size);
240
249 int coap_context_get_coap_fd(const coap_context_t *context);
250
260 void coap_context_set_max_idle_sessions(coap_context_t *context,
261 unsigned int max_idle_sessions);
262
270 unsigned int coap_context_get_max_idle_sessions(const coap_context_t *context);
271
280 void coap_context_set_session_timeout(coap_context_t *context,
281 unsigned int session_timeout);
282
290 unsigned int coap_context_get_session_timeout(const coap_context_t *context);
291
300 void coap_context_set_csm_timeout(coap_context_t *context,
301 unsigned int csm_tmeout);
302
310 unsigned int coap_context_get_csm_timeout(const coap_context_t *context);
311
318 void coap_context_set_csm_max_message_size(coap_context_t *context,
319 uint32_t csm_max_message_size);
320
328uint32_t coap_context_get_csm_max_message_size(const coap_context_t *context);
329
339 void coap_context_set_max_handshake_sessions(coap_context_t *context,
340 unsigned int max_handshake_sessions);
341
349 unsigned int coap_context_get_max_handshake_sessions(const coap_context_t *context);
350
360uint16_t coap_new_message_id(coap_session_t *session);
361
370 void coap_free_context(coap_context_t *context);
371
381 void coap_set_app_data(coap_context_t *context, void *data);
382
392 void *coap_get_app_data(const coap_context_t *context);
393
411 coap_pdu_t *coap_new_error_response(const coap_pdu_t *request,
412 coap_pdu_code_t code,
413 coap_opt_filter_t *opts);
414
430 coap_mid_t coap_send_error(coap_session_t *session,
431 const coap_pdu_t *request,
432 coap_pdu_code_t code,
433 coap_opt_filter_t *opts);
434
446 coap_mid_t coap_send_message_type(coap_session_t *session, const coap_pdu_t *request,
447 coap_pdu_type_t type);
448
460 coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request);
461
473 COAP_STATIC_INLINE coap_mid_t
474 coap_send_rst(coap_session_t *session, const coap_pdu_t *request) {
475 return coap_send_message_type(session, request, COAP_MESSAGE_RST);
476}
477
489 coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu);
490
491#define coap_send_large(session, pdu) coap_send(session, pdu)
492
503 int coap_handle_event(coap_context_t *context,
504 coap_event_t event,
505 coap_session_t *session);
516 int coap_can_exit(coap_context_t *context);
517
522 void coap_ticks(coap_tick_t *);
523
535 int coap_join_mcast_group_intf(coap_context_t *ctx, const char *groupname,
536 const char *ifname);
537
538#define coap_join_mcast_group(ctx, groupname) \
539 (coap_join_mcast_group_intf(ctx, groupname, NULL))
540
551 int coap_mcast_set_hops(coap_session_t *session, size_t hops);
552
560 void coap_mcast_per_resource(coap_context_t *context);
561
571#define COAP_IO_WAIT 0
572#define COAP_IO_NO_WAIT ((uint32_t)-1)
573
602 int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
603
631 int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms,
632 int nfds, fd_set *readfds, fd_set *writefds,
633 fd_set *exceptfds);
634
647 int coap_io_pending(coap_context_t *context);
648
691 unsigned int coap_io_prepare_io(coap_context_t *ctx,
692 coap_socket_t *sockets[],
693 unsigned int max_sockets,
694 unsigned int *num_sockets,
695 coap_tick_t now
696 );
697
711 void coap_io_do_io(coap_context_t *ctx, coap_tick_t now);
712
732 unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now);
733
734 struct epoll_event;
735
749 void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events,
750 size_t nevents);
751
770 void coap_lwip_dump_memory_pools(coap_log_t log_level);
771
783 typedef int (*coap_lwip_input_wait_handler_t)(void *arg, uint32_t milli_secs);
784
794 void coap_lwip_set_input_wait_handler(coap_context_t *context,
795 coap_lwip_input_wait_handler_t handler,
796 void *input_arg);
797
819 COAP_STATIC_INLINE COAP_DEPRECATED int
820 coap_run_once(coap_context_t *ctx, uint32_t timeout_ms) {
821 return coap_io_process(ctx, timeout_ms);
822}
823
842 COAP_STATIC_INLINE COAP_DEPRECATED unsigned int
843 coap_write(coap_context_t *ctx,
844 coap_socket_t *sockets[],
845 unsigned int max_sockets,
846 unsigned int *num_sockets,
847 coap_tick_t now
848 ) {
849 return coap_io_prepare_io(ctx, sockets, max_sockets, num_sockets, now);
850}
851
862 COAP_STATIC_INLINE COAP_DEPRECATED void
863 coap_read(coap_context_t *ctx, coap_tick_t now
864 ) {
865 coap_io_do_io(ctx, now);
866}
867
868/* Old definitions which may be hanging around in old code - be helpful! */
869#define COAP_RUN_NONBLOCK COAP_RUN_NONBLOCK_deprecated_use_COAP_IO_NO_WAIT
870#define COAP_RUN_BLOCK COAP_RUN_BLOCK_deprecated_use_COAP_IO_WAIT
871
872#endif /* COAP_NET_H_ */
coap_debug.h
CoAP Logging support.
coap_dtls.h
CoAP DTLS support.
coap_event.h
Event handling.
coap_io.h
Default network I/O functions.
coap_nack_reason_t
coap_nack_reason_t
Definition: coap_io.h:69
coap_write
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)
Definition: coap_net.h:843
coap_run_once
COAP_STATIC_INLINE COAP_DEPRECATED int coap_run_once(coap_context_t *ctx, uint32_t timeout_ms)
Definition: coap_net.h:820
coap_read
COAP_STATIC_INLINE COAP_DEPRECATED void coap_read(coap_context_t *ctx, coap_tick_t now)
Definition: coap_net.h:863
coap_pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_session.h
Defines the application visible session information.
coap_io_do_io
void coap_io_do_io(coap_context_t *ctx, coap_tick_t now)
Processes any outstanding read, write, accept or connect I/O as indicated in the coap_socket_t struct...
Definition: coap_net.c:2039
coap_io_prepare_epoll
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:1220
coap_io_do_epoll
void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
Definition: coap_net.c:2096
coap_io_prepare_io
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:1270
coap_io_process
int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
Definition: coap_io.c:1524
coap_io_process_with_fds
int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds)
The main message processing loop with additional fds for internal select.
Definition: coap_io.c:1529
coap_io_pending
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:1708
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:144
coap_send_ack
coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition: coap_net.c:770
coap_context_set_session_timeout
void coap_context_set_session_timeout(coap_context_t *context, unsigned int session_timeout)
Set the session timeout value.
Definition: coap_net.c:443
coap_context_set_psk2
int coap_context_set_psk2(coap_context_t *context, coap_dtls_spsk_t *setup_data)
Set the context's default PSK hint and/or key for a server.
coap_context_get_max_handshake_sessions
unsigned int coap_context_get_max_handshake_sessions(const coap_context_t *context)
Get the session timeout value.
Definition: coap_net.c:415
coap_new_message_id
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
Definition: coap_session.c:1590
coap_pong_handler_t
void(* coap_pong_handler_t)(coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid)
Received Pong handler that is used as callback in coap_context_t.
Definition: coap_net.h:98
coap_context_get_max_idle_sessions
unsigned int coap_context_get_max_idle_sessions(const coap_context_t *context)
Get the maximum idle sessions count.
Definition: coap_net.c:404
coap_new_context
coap_context_t * coap_new_context(const coap_address_t *listen_addr)
Creates a new coap_context_t object that will hold the CoAP stack status.
Definition: coap_net.c:464
coap_can_exit
int coap_can_exit(coap_context_t *context)
Returns 1 if there are no messages to send or to dispatch in the context's queues.
Definition: coap_net.c:3928
coap_mcast_per_resource
void coap_mcast_per_resource(coap_context_t *context)
Function interface to enable processing mcast requests on a per resource basis.
coap_response_handler_t
coap_response_t(* coap_response_handler_t)(coap_session_t *session, const coap_pdu_t *sent, const coap_pdu_t *received, const coap_mid_t mid)
Response handler that is used as callback in coap_context_t.
Definition: coap_net.h:62
coap_context_set_csm_max_message_size
void coap_context_set_csm_max_message_size(coap_context_t *context, uint32_t csm_max_message_size)
Set the CSM max session size value.
Definition: coap_net.c:431
coap_register_response_handler
void coap_register_response_handler(coap_context_t *context, coap_response_handler_t handler)
Registers a new message handler that is called whenever a response is received.
Definition: coap_net.c:4058
coap_new_error_response
coap_pdu_t * coap_new_error_response(const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Creates a new ACK PDU with specified error code.
Definition: coap_net.c:2356
coap_free_context
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: coap_net.c:566
coap_context_set_max_handshake_sessions
void coap_context_set_max_handshake_sessions(coap_context_t *context, unsigned int max_handshake_sessions)
Set the maximum number of sessions in (D)TLS handshake value.
Definition: coap_net.c:409
coap_context_get_coap_fd
int coap_context_get_coap_fd(const coap_context_t *context)
Get the libcoap internal file descriptor for using in an application's select() or returned as an eve...
Definition: coap_net.c:454
coap_handle_event
int coap_handle_event(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:3917
coap_context_set_psk
int coap_context_set_psk(coap_context_t *context, const char *hint, const uint8_t *key, size_t key_len)
Set the context's default PSK hint and/or key for a server.
coap_mcast_set_hops
int coap_mcast_set_hops(coap_session_t *session, size_t hops)
Function interface for defining the hop count (ttl) for sending multicast traffic.
coap_response_t
coap_response_t
Definition: coap_net.h:46
coap_ping_handler_t
void(* coap_ping_handler_t)(coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid)
Received Ping handler that is used as callback in coap_context_t.
Definition: coap_net.h:87
coap_ticks
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
coap_context_set_pki_root_cas
int coap_context_set_pki_root_cas(coap_context_t *context, const char *ca_file, const char *ca_dir)
Set the context's default Root CA information for a client or server.
Definition: coap_net.c:375
coap_nack_handler_t
void(* coap_nack_handler_t)(coap_session_t *session, const coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
Negative Acknowedge handler that is used as callback in coap_context_t.
Definition: coap_net.h:75
coap_send_rst
COAP_STATIC_INLINE coap_mid_t coap_send_rst(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition: coap_net.h:474
coap_send_message_type
coap_mid_t coap_send_message_type(coap_session_t *session, const coap_pdu_t *request, coap_pdu_type_t type)
Helper function to create and send a message with type (usually ACK or RST).
Definition: coap_net.c:854
coap_context_get_csm_max_message_size
uint32_t coap_context_get_csm_max_message_size(const coap_context_t *context)
Get the CSM max session size value.
Definition: coap_net.c:438
coap_context_get_session_timeout
unsigned int coap_context_get_session_timeout(const coap_context_t *context)
Get the session timeout value.
Definition: coap_net.c:449
coap_send_error
coap_mid_t coap_send_error(coap_session_t *session, const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Sends an error response with code code for request request to dst.
Definition: coap_net.c:836
coap_context_set_pki
int coap_context_set_pki(coap_context_t *context, const coap_dtls_pki_t *setup_data)
Set the context's default PKI information for a server.
coap_register_ping_handler
void coap_register_ping_handler(coap_context_t *context, coap_ping_handler_t handler)
Registers a new message handler that is called whenever a CoAP Ping message is received.
Definition: coap_net.c:4075
coap_register_option
void coap_register_option(coap_context_t *ctx, uint16_t type)
Registers the option type type with the given context object ctx.
Definition: coap_net.c:4087
coap_join_mcast_group_intf
int coap_join_mcast_group_intf(coap_context_t *ctx, const char *groupname, const char *ifname)
Function interface for joining a multicast group for listening for the currently defined endpoints th...
coap_get_app_data
void * coap_get_app_data(const coap_context_t *context)
Returns any application-specific data that has been stored with context using the function coap_set_a...
Definition: coap_net.c:560
coap_context_set_max_idle_sessions
void coap_context_set_max_idle_sessions(coap_context_t *context, unsigned int max_idle_sessions)
Set the maximum idle sessions count.
Definition: coap_net.c:398
coap_context_set_keepalive
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
Definition: coap_net.c:385
coap_set_app_data
void coap_set_app_data(coap_context_t *context, void *data)
Stores data with the given CoAP context.
Definition: coap_net.c:554
coap_context_get_csm_timeout
unsigned int coap_context_get_csm_timeout(const coap_context_t *context)
Get the CSM timeout value.
Definition: coap_net.c:426
coap_register_pong_handler
void coap_register_pong_handler(coap_context_t *context, coap_pong_handler_t handler)
Registers a new message handler that is called whenever a CoAP Pong message is received.
Definition: coap_net.c:4081
coap_context_set_max_token_size
void coap_context_set_max_token_size(coap_context_t *context, size_t max_token_size)
Set the maximum token size (RFC8974).
Definition: coap_net.c:390
coap_send
coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: coap_net.c:1032
coap_register_nack_handler
void coap_register_nack_handler(coap_context_t *context, coap_nack_handler_t handler)
Registers a new message handler that is called whenever a confirmable message (request or response) i...
Definition: coap_net.c:4069
coap_context_set_csm_timeout
void coap_context_set_csm_timeout(coap_context_t *context, unsigned int csm_tmeout)
Set the CSM timeout value.
Definition: coap_net.c:420
COAP_RESPONSE_FAIL
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition: coap_net.h:47
COAP_RESPONSE_OK
@ COAP_RESPONSE_OK
Response is fine.
Definition: coap_net.h:48
coap_event_t
coap_event_t
Scalar type to represent different events, e.g.
Definition: coap_event.h:34
coap_log_t
coap_log_t
Logging type.
Definition: coap_debug.h:50
coap_lwip_set_input_wait_handler
void coap_lwip_set_input_wait_handler(coap_context_t *context, coap_lwip_input_wait_handler_t handler, void *input_arg)
Set up a wait / timeout callback handler for use when the application calls coap_io_process().
coap_lwip_input_wait_handler_t
int(* coap_lwip_input_wait_handler_t)(void *arg, uint32_t milli_secs)
LwIP callback handler that can be used to wait / timeout for the next input packet.
Definition: coap_net.h:783
coap_lwip_dump_memory_pools
void coap_lwip_dump_memory_pools(coap_log_t log_level)
Dump the current state of the LwIP memory pools.
coap_mid_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: coap_pdu.h:255
coap_pdu_code_t
coap_pdu_code_t
Set of codes available for a PDU.
Definition: coap_pdu.h:318
coap_pdu_type_t
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: coap_pdu.h:64
COAP_MESSAGE_RST
@ COAP_MESSAGE_RST
Definition: coap_pdu.h:68
COAP_DEPRECATED
#define COAP_DEPRECATED
Definition: libcoap.h:61
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:53
coap_address_t
Multi-purpose address abstraction.
Definition: coap_address.h:109
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: coap_net_internal.h:49
coap_dtls_pki_t
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:284
coap_dtls_spsk_t
The structure used for defining the Server PSK setup data to be used.
Definition: coap_dtls.h:465
coap_opt_filter_t
Definition: coap_option.h:101
coap_pdu_t
structure for CoAP PDUs
Definition: coap_pdu_internal.h:119
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition: coap_session_internal.h:68
coap_socket_t
Definition: coap_io_internal.h:34