libcoap 4.3.5-develop-19cef11
coap_net.h
Go to the documentation of this file.
1/*
2 * coap_net.h -- CoAP context interface
3 *
4 * Copyright (C) 2010-2024 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#ifdef HAVE_SYS_SELECT_H
24#include <sys/select.h>
25#endif /* HAVE_SYS_SELECT_H */
26#include <sys/time.h>
27#endif
28#include <time.h>
29
30#ifdef WITH_LWIP
31#include <lwip/ip_addr.h>
32#endif
33
34#include "coap_io.h"
35#include "coap_dtls.h"
36#include "coap_event.h"
37#include "coap_pdu.h"
38#include "coap_session.h"
39#include "coap_debug.h"
40
48typedef enum coap_response_t {
52
65 const coap_pdu_t *sent,
66 const coap_pdu_t *received,
67 const coap_mid_t mid);
68
77typedef void (*coap_nack_handler_t)(coap_session_t *session,
78 const coap_pdu_t *sent,
79 const coap_nack_reason_t reason,
80 const coap_mid_t mid);
81
89typedef void (*coap_ping_handler_t)(coap_session_t *session,
90 const coap_pdu_t *received,
91 const coap_mid_t mid);
92
100typedef void (*coap_pong_handler_t)(coap_session_t *session,
101 const coap_pdu_t *received,
102 const coap_mid_t mid);
103
113
124 coap_nack_handler_t handler);
125
134 coap_ping_handler_t handler);
135
144 coap_pong_handler_t handler);
145
152COAP_API void coap_register_option(coap_context_t *ctx, uint16_t type);
153
158
173COAP_API int coap_context_set_psk(coap_context_t *context, const char *hint,
174 const uint8_t *key, size_t key_len);
175
186 coap_dtls_spsk_t *setup_data);
187
198 const coap_dtls_pki_t *setup_data);
199
212 const char *ca_file,
213 const char *ca_dir);
214
231void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
232
241int coap_context_set_cid_tuple_change(coap_context_t *context, uint8_t every);
242
251 size_t max_token_size);
252
261int coap_context_get_coap_fd(const coap_context_t *context);
262
273 unsigned int max_idle_sessions);
274
282unsigned int coap_context_get_max_idle_sessions(const coap_context_t *context);
283
293 unsigned int session_timeout);
294
302unsigned int coap_context_get_session_timeout(const coap_context_t *context);
303
315 unsigned int csm_timeout);
316
327
338 unsigned int csm_timeout_ms);
339
347unsigned int coap_context_get_csm_timeout_ms(const coap_context_t *context);
348
356 uint32_t csm_max_message_size);
357
366
377 unsigned int max_handshake_sessions);
378
387
398
408
420void coap_set_app_data(coap_context_t *context, void *data);
421
433void *coap_get_app_data(const coap_context_t *context);
434
453 coap_pdu_code_t code,
454 coap_opt_filter_t *opts);
455
472 const coap_pdu_t *request,
473 coap_pdu_code_t code,
474 coap_opt_filter_t *opts);
475
488 coap_pdu_type_t type);
489
502
515
528
529#define coap_send_large(session, pdu) coap_send(session, pdu)
530
531/*
532 * Send a request PDU and wait for the response PDU.
533 *
534 * @param session The CoAP session.
535 * @param request_pdu The requesting PDU. If this PDU contains the Observe
536 * option, the unsolocited responses will get handled by the
537 * defined response handler. This PDU must be freed off by the
538 * caller after processing.
539 * @param response_pdu If there is a response, the response PDU is put here.
540 * This PDU must be freed off by the caller after processing.
541 * @param timeout_ms Positive maximum number of milliseconds to wait for response
542 * packet following the request. If there is a large block transfer
543 * this timeout is for between each request and response.
544 *
545 * @return 0 or +ve Time in function in ms after successful transfer (which can be
546 * bigger than timeout_ms).
547 * -1 Invalid timeout parameter
548 * -2 Failed to transmit PDU
549 * -3 Nack or Event handler invoked, cancelling request
550 * -4 coap_io_process returned error (fail to re-lock or select())
551 * -5 Response not received in the given time
552 * -6 Terminated by user
553 * -7 Client mode code not enabled
554 */
555COAP_API int coap_send_recv(coap_session_t *session, coap_pdu_t *request_pdu,
556 coap_pdu_t **response_pdu, uint32_t timeout_ms);
557
561void coap_send_recv_terminate(void);
562
574 coap_event_t event,
575 coap_session_t *session);
587
593
605COAP_API int coap_join_mcast_group_intf(coap_context_t *ctx, const char *groupname,
606 const char *ifname);
607
608#define coap_join_mcast_group(ctx, groupname) \
609 (coap_join_mcast_group_intf(ctx, groupname, NULL))
610
622int coap_mcast_set_hops(coap_session_t *session, size_t hops);
623
632
640void coap_context_set_app_data(coap_context_t *context, void *data);
641
651void *coap_context_get_app_data(const coap_context_t *context);
652
662#define COAP_IO_WAIT 0
663#define COAP_IO_NO_WAIT ((uint32_t)-1)
664
693COAP_API int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
694
695#if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
723COAP_API int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms,
724 int nfds, fd_set *readfds, fd_set *writefds,
725 fd_set *exceptfds);
726#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */
727
741
776 coap_socket_t *sockets[],
777 unsigned int max_sockets,
778 unsigned int *num_sockets,
779 coap_tick_t now
780 );
781
796
817
818struct epoll_event;
819
833COAP_API void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events,
834 size_t nevents);
835
838#if defined(WITH_LWIP) || defined(WITH_LWIP_MAN_CHECK) || defined(__DOXYGEN__)
856
868typedef int (*coap_lwip_input_wait_handler_t)(void *arg, uint32_t milli_secs);
869
881 void *input_arg);
882
884#endif /* WITH_LWIP || WITH_LWIP_MAN_CHECK || defined(__DOXYGEN__) */
885
905#define coap_run_once(ctx, timeout_ms) coap_io_process(ctx, timeout_ms)
906
925#define coap_write(ctx, sockets, max_sockets, num_sockets, now) \
926 coap_io_prepare_io(ctx, sockets, max_sockets, num_sockets, now)
927
938#define coap_read(ctx, now) coap_io_do_io(ctx, now)
939
940/* Old definitions which may be hanging around in old code - be helpful! */
941#define COAP_RUN_NONBLOCK COAP_RUN_NONBLOCK_deprecated_use_COAP_IO_NO_WAIT
942#define COAP_RUN_BLOCK COAP_RUN_BLOCK_deprecated_use_COAP_IO_WAIT
943
944#endif /* COAP_NET_H_ */
CoAP Logging support.
CoAP DTLS support.
Event handling.
Default network I/O functions.
coap_nack_reason_t
Definition: coap_io.h:62
#define COAP_API
Pre-defined constants that reflect defaults for CoAP.
Defines the application visible session information.
COAP_API int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
Definition: coap_io.c:1581
COAP_API int coap_io_pending(coap_context_t *context)
Check to see if there is any i/o pending for the context.
Definition: coap_io.c:1825
COAP_API unsigned int coap_io_prepare_io(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets, unsigned int *num_sockets, coap_tick_t now)
Iterates through all the coap_socket_t structures embedded in endpoints or sessions associated with t...
Definition: coap_io.c:1303
COAP_API unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
Definition: coap_io.c:1242
COAP_API 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:1596
COAP_API 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:2512
COAP_API 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:2451
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:143
void coap_context_set_session_timeout(coap_context_t *context, unsigned int session_timeout)
Set the session timeout value.
Definition: coap_net.c:546
unsigned int coap_context_get_max_handshake_sessions(const coap_context_t *context)
Get the session timeout value.
Definition: coap_net.c:499
COAP_API 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...
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:100
unsigned int coap_context_get_max_idle_sessions(const coap_context_t *context)
Get the maximum idle sessions count.
Definition: coap_net.c:488
COAP_API int coap_send_recv(coap_session_t *session, coap_pdu_t *request_pdu, coap_pdu_t **response_pdu, uint32_t timeout_ms)
Definition: coap_net.c:1916
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:642
COAP_API coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: coap_net.c:1346
COAP_API 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.
void coap_mcast_per_resource(coap_context_t *context)
Function interface to enable processing mcast requests on a per resource basis.
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:64
COAP_API 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:1053
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:534
COAP_DEPRECATED void coap_context_set_csm_timeout(coap_context_t *context, unsigned int csm_timeout)
Set the CSM timeout value.
Definition: coap_net.c:506
void coap_send_recv_terminate(void)
Terminate any active coap_send_recv() sessions.
Definition: coap_net.c:1911
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:4680
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:2790
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:493
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:557
int coap_mcast_set_hops(coap_session_t *session, size_t hops)
Function interface for defining the hop count (ttl) for sending multicast traffic.
void coap_context_set_app_data(coap_context_t *context, void *data)
Stores data with the given context.
Definition: coap_net.c:630
coap_response_t
Definition: coap_net.h:48
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:89
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
COAP_API void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: coap_net.c:746
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:77
void * coap_context_get_app_data(const coap_context_t *context)
Returns any application-specific data that has been stored with context using the function coap_conte...
Definition: coap_net.c:636
COAP_API 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:436
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:541
unsigned int coap_context_get_session_timeout(const coap_context_t *context)
Get the session timeout value.
Definition: coap_net.c:552
COAP_API 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_API 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:4709
COAP_API 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:976
unsigned int coap_context_get_csm_timeout_ms(const coap_context_t *context)
Get the CSM timeout value.
Definition: coap_net.c:529
COAP_API uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
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:4697
COAP_API 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.
int coap_context_set_cid_tuple_change(coap_context_t *context, uint8_t every)
Set the Connection ID client tuple frequency change for testing CIDs.
Definition: coap_net.c:463
void * coap_get_app_data(const coap_context_t *context)
Definition: coap_net.c:740
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:482
COAP_API 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:1084
COAP_API 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.c:961
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
Definition: coap_net.c:458
void coap_set_app_data(coap_context_t *context, void *data)
Definition: coap_net.c:734
COAP_API 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:4543
COAP_DEPRECATED unsigned int coap_context_get_csm_timeout(const coap_context_t *context)
Get the CSM timeout value.
Definition: coap_net.c:513
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:4703
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:474
COAP_API 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:4480
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:4691
void coap_context_set_csm_timeout_ms(coap_context_t *context, unsigned int csm_timeout_ms)
Set the CSM timeout value.
Definition: coap_net.c:519
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition: coap_net.h:49
@ COAP_RESPONSE_OK
Response is fine.
Definition: coap_net.h:50
coap_event_t
Scalar type to represent different events, e.g.
Definition: coap_event.h:34
coap_log_t
Logging type.
Definition: coap_debug.h:50
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().
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:868
void coap_lwip_dump_memory_pools(coap_log_t log_level)
Dump the current state of the LwIP memory pools.
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: coap_pdu.h:263
coap_pdu_code_t
Set of codes available for a PDU.
Definition: coap_pdu.h:326
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: coap_pdu.h:68
#define COAP_DEPRECATED
Definition: libcoap.h:62
Multi-purpose address abstraction.
Definition: coap_address.h:148
The CoAP stack's global state is stored in a coap_context_t object.
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:312
The structure used for defining the Server PSK setup data to be used.
Definition: coap_dtls.h:501
structure for CoAP PDUs
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...