libcoap 4.3.5-develop-19cef11
coap_net_internal.h
Go to the documentation of this file.
1/*
2 * coap_net_internal.h -- CoAP context internal information
3 * exposed to application programming
4 *
5 * Copyright (C) 2010-2024 Olaf Bergmann <bergmann@tzi.org>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
18#ifndef COAP_NET_INTERNAL_H_
19#define COAP_NET_INTERNAL_H_
20
21#include "coap_internal.h"
22#include "coap_subscribe.h"
24
38 unsigned char retransmit_cnt;
40 uint8_t is_mcast;
41 unsigned int timeout;
45};
46
52#if COAP_SERVER_SUPPORT
62#endif /* COAP_SERVER_SUPPORT */
63
64#if COAP_ASYNC_SUPPORT
67 coap_async_t *async_state;
68#endif /* COAP_ASYNC_SUPPORT */
69
75#if COAP_SERVER_SUPPORT
77#endif /* COAP_SERVER_SUPPORT */
78#if COAP_CLIENT_SUPPORT
80#endif /* COAP_CLIENT_SUPPORT */
81
82#ifdef WITH_CONTIKI
83 struct uip_udp_conn *conn;
84 struct ctimer io_timer;
86#endif /* WITH_CONTIKI */
87
88#ifdef WITH_LWIP
90 void *input_arg;
91 uint8_t timer_configured;
94#endif /* WITH_LWIP */
95#ifdef RIOT_VERSION
96 thread_t *selecting_thread;
97#endif /* RIOT_VERSION */
98#if COAP_OSCORE_SUPPORT
99 struct oscore_ctx_t *p_osc_ctx;
100#endif /* COAP_OSCORE_SUPPORT */
101
102#if COAP_CLIENT_SUPPORT
105#endif /* COAP_CLIENT_SUPPORT */
112#if COAP_SERVER_SUPPORT
126#if COAP_WITH_OBSERVE_PERSIST
127 coap_bin_const_t *dyn_resource_save_file;
130 coap_bin_const_t *obs_cnt_save_file;
132 coap_bin_const_t *observe_save_file;
133 coap_pdu_t *unknown_pdu;
134 coap_session_t *unknown_session;
135#endif /* COAP_WITH_OBSERVE_PERSIST */
136#endif /* COAP_SERVER_SUPPORT */
137
143
145
146#if COAP_SERVER_SUPPORT
149#endif /* COAP_SERVER_SUPPORT */
150
151 unsigned int session_timeout;
154 unsigned int max_idle_sessions;
160 unsigned int ping_timeout;
163 uint32_t csm_timeout_ms;
166 uint64_t etag;
168#if COAP_SERVER_SUPPORT
174#endif /* COAP_SERVER_SUPPORT */
175 void *app;
176 uint32_t max_token_size;
177#ifdef COAP_EPOLL_SUPPORT
178 int epfd;
179 int eptimerfd;
180 coap_tick_t next_timeout;
181#else /* ! COAP_EPOLL_SUPPORT */
182#if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
187 unsigned int num_sockets;
188#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */
189#endif /* ! COAP_EPOLL_SUPPORT */
190#if COAP_SERVER_SUPPORT
196#endif /* COAP_SERVER_SUPPORT */
197#if COAP_PROXY_SUPPORT
198 coap_proxy_list_t *proxy_list;
199 size_t proxy_list_count;
200#endif /* COAP_PROXY_SUPPORT */
201#if COAP_CLIENT_SUPPORT
202 uint8_t testing_cids;
203#endif /* COAP_CLIENT_SUPPORT */
204 uint32_t block_mode;
205};
206
215int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
216
227
238
246void coap_delete_all(coap_queue_t *queue);
247
254
260unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
261
266
271
282
296int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len);
297
315 coap_session_t *session,
316 coap_mid_t id,
317 coap_queue_t **node);
318
320 coap_queue_t *node);
321
331 coap_session_t *session,
332 coap_bin_const_t *token);
333
342 coap_session_t *session,
343 coap_nack_reason_t reason);
344
356uint16_t coap_new_message_id_lkd(coap_session_t *session);
357
361void coap_dispatch(coap_context_t *context, coap_session_t *session,
362 coap_pdu_t *pdu);
363
396 coap_pdu_t *pdu,
397 coap_opt_filter_t *unknown);
398
411 coap_session_t *session,
412 coap_pdu_t *request);
413
427unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
428
438
455
465
477
491 coap_event_t event,
492 coap_session_t *session);
493
507
521int coap_join_mcast_group_intf_lkd(coap_context_t *ctx, const char *groupname,
522 const char *ifname);
523
532void coap_register_option_lkd(coap_context_t *ctx, uint16_t type);
533
546 const coap_dtls_pki_t *setup_data);
547
562 const char *ca_file,
563 const char *ca_dir);
564
581int coap_context_set_psk_lkd(coap_context_t *context, const char *hint,
582 const uint8_t *key, size_t key_len);
583
596 coap_dtls_spsk_t *setup_data);
597
621
635void coap_io_do_epoll_lkd(coap_context_t *ctx, struct epoll_event *events,
636 size_t nevents);
637
653
674
708unsigned int coap_io_prepare_io_lkd(coap_context_t *ctx,
709 coap_socket_t *sockets[],
710 unsigned int max_sockets,
711 unsigned int *num_sockets,
712 coap_tick_t now
713 );
714
746int coap_io_process_lkd(coap_context_t *ctx, uint32_t timeout_ms);
747
748#if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
778int coap_io_process_with_fds_lkd(coap_context_t *ctx, uint32_t timeout_ms,
779 int nfds, fd_set *readfds, fd_set *writefds,
780 fd_set *exceptfds);
781#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */
782
797
798/*
799 * Send a request PDU and wait for the response PDU.
800 *
801 * Note: This function must be called in the locked state.
802 *
803 * @param session The CoAP session.
804 * @param request_pdu The requesting PDU. If this PDU contains the Observe
805 * option, the unsolocited responses will get handled by the
806 * defined response handler. This PDU must be freed off by the
807 * caller after processing.
808 * @param response_pdu If there is a response, the response PDU is put here.
809 * This PDU must be freed off by the caller after processing.
810 * @param timeout_ms Positive maximum number of milliseconds to wait for response
811 * packet following the request. If there is a large block transfer
812 * this timeout is for between each request and response.
813 *
814 * @return 0 or +ve Time in function in ms after successful transfer (which can be
815 * bigger than timeout_ms).
816 * -1 Invalid timeout parameter
817 * -2 Failed to transmit PDU
818 * -3 Nack or Event handler invoked, cancelling request
819 * -4 coap_io_process returned error (fail to re-lock or select())
820 * -5 Response not received in the given time
821 * -6 Terminated by user
822 * -7 Client mode code not enabled
823 */
824int coap_send_recv_lkd(coap_session_t *session, coap_pdu_t *request_pdu,
825 coap_pdu_t **response_pdu, uint32_t timeout_ms);
826
845 const coap_pdu_t *request,
846 coap_pdu_code_t code,
847 coap_opt_filter_t *opts);
848
863 coap_pdu_type_t type);
864
878coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request);
879
893coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request);
894
897extern int coap_started;
898
899#endif /* COAP_NET_INTERNAL_H_ */
struct coap_async_t coap_async_t
Async Entry information.
Pulls together all the internal only header files.
coap_nack_reason_t
Definition: coap_io.h:62
#define COAP_API
int coap_started
Definition: coap_net.c:4606
Defines the application visible subscribe information.
CoAP mapping of locking functions.
void coap_io_do_epoll_lkd(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
Definition: coap_net.c:2523
coap_mid_t coap_send_rst_lkd(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:971
coap_mid_t coap_send_message_type_lkd(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:1095
int coap_io_pending_lkd(coap_context_t *context)
Check to see if there is any i/o pending for the context.
Definition: coap_io.c:1839
coap_mid_t coap_send_error_lkd(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:1066
void coap_io_do_io_lkd(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:2458
int coap_send_recv_lkd(coap_session_t *session, coap_pdu_t *request_pdu, coap_pdu_t **response_pdu, uint32_t timeout_ms)
Definition: coap_net.c:1937
int coap_io_process_lkd(coap_context_t *ctx, uint32_t timeout_ms)
The main I/O processing function.
Definition: coap_io.c:1591
int coap_io_process_with_fds_lkd(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:1609
unsigned int coap_io_prepare_io_lkd(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:1321
unsigned int coap_io_prepare_epoll_lkd(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:1252
coap_mid_t coap_send_lkd(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: coap_net.c:1356
coap_mid_t coap_send_ack_lkd(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:986
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_resource_release_userdata_handler_t)(void *user_data)
Definition of release resource user_data callback function.
void coap_register_option_lkd(coap_context_t *ctx, uint16_t type)
Registers the option type type with the given context object ctx.
Definition: coap_net.c:4716
int coap_handle_event_lkd(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:4491
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
Set sendqueue_basetime in the given context object ctx to now.
Definition: coap_net.c:130
int coap_delete_node_lkd(coap_queue_t *node)
Destroys specified node.
Definition: coap_net.c:227
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: coap_net.c:247
int coap_context_set_psk2_lkd(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_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition: coap_net.c:2673
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: coap_net.c:270
coap_pdu_t * coap_wellknown_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *request)
Creates a new response for given request with the contents of .well-known/core.
COAP_API int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: coap_net.c:204
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
Definition: coap_net.c:1226
int coap_context_set_psk_lkd(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_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
Definition: coap_net.c:278
void coap_dispatch(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
Dispatches the PDUs from the receive queue in given context.
Definition: coap_net.c:4019
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: coap_net.c:1679
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by variable t in node.
Definition: coap_net.c:167
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r)
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout',...
Definition: coap_net.c:1123
int coap_join_mcast_group_intf_lkd(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_free_context_lkd(coap_context_t *context)
CoAP stack context must be released with coap_free_context_lkd().
Definition: coap_net.c:755
int coap_can_exit_lkd(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:4553
coap_mid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: coap_net.c:2053
int coap_check_code_class(coap_session_t *session, coap_pdu_t *pdu)
Check whether the pdu contains a valid code class.
Definition: coap_net.c:1294
int coap_context_set_pki_root_cas_lkd(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:448
int coap_option_check_critical(coap_session_t *session, coap_pdu_t *pdu, coap_opt_filter_t *unknown)
Verifies that pdu contains no unknown critical options.
Definition: coap_net.c:846
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: coap_net.c:1149
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: coap_net.c:256
void coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_nack_reason_t reason)
Cancels all outstanding messages for session session.
Definition: coap_net.c:2718
int coap_context_set_pki_lkd(coap_context_t *context, const coap_dtls_pki_t *setup_data)
Set the context's default PKI information for a server.
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len)
Parses and interprets a CoAP datagram with context ctx.
Definition: coap_net.c:2628
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, coap_bin_const_t *token)
Cancels all outstanding messages for session session that have the specified token.
Definition: coap_net.c:2757
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
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
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_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
coap_event_t
Scalar type to represent different events, e.g.
Definition: coap_event.h:34
int(* coap_event_handler_t)(coap_session_t *session, const coap_event_t event)
Type for event handler functions that can be registered with a CoAP context using the function coap_s...
Definition: coap_event.h:146
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
int(* coap_dyn_resource_added_t)(coap_session_t *session, coap_str_const_t *resource_name, coap_bin_const_t *raw_packet, void *user_data)
Callback handler definition called when a dynamic resource is getting created, as defined in coap_per...
int(* coap_resource_deleted_t)(coap_context_t *context, coap_str_const_t *resource_name, void *user_data)
Callback handler definition called when resource is removed, as defined in coap_persist_track_funcs()...
int(* coap_observe_added_t)(coap_session_t *session, coap_subscription_t *observe_key, coap_proto_t e_proto, coap_address_t *e_listen_addr, coap_addr_tuple_t *s_addr_info, coap_bin_const_t *raw_packet, coap_bin_const_t *oscore_info, void *user_data)
Callback handler definition called when a new observe has been set up, as defined in coap_persist_tra...
int(* coap_track_observe_value_t)(coap_context_t *context, coap_str_const_t *resource_name, uint32_t observe_num, void *user_data)
Callback handler definition called when an observe unsolicited response is being sent,...
int(* coap_observe_deleted_t)(coap_session_t *session, coap_subscription_t *observe_key, void *user_data)
Callback handler definition called when an observe is being removed, as defined in coap_persist_track...
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
CoAP binary data definition with const data.
Definition: coap_str.h:64
The CoAP stack's global state is stored in a coap_context_t object.
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
coap_pong_handler_t pong_handler
Called when a ping response is received.
uint64_t etag
Next ETag to use.
coap_socket_t * sockets[64]
Track different socket information in coap_io_process_with_fds_lkd()
coap_observe_added_t observe_added
Called when there is a new observe subscription request.
void * app
application-specific data
unsigned int num_sockets
Number of sockets being tracked.
coap_session_t * sessions
client sessions
fd_set exceptfds
Used for select call in coap_io_process_with_fds_lkd()
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
coap_resource_t * resources
hash table or list of known resources
uint16_t * cache_ignore_options
CoAP options to ignore when creating a cache-key.
coap_dyn_resource_added_t dyn_resource_added
Callback to save dynamic resource when created.
coap_opt_filter_t known_options
coap_resource_release_userdata_handler_t release_userdata
function to release user_data when resource is deleted
coap_ping_handler_t ping_handler
Called when a CoAP ping is received.
uint32_t csm_max_message_size
Value for CSM Max-Message-Size.
void * observe_user_data
App provided data for use in observe_added or observe_deleted.
size_t cache_ignore_count
The number of CoAP options to ignore when creating a cache-key.
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
coap_queue_t * sendqueue
uint32_t max_token_size
Largest token size supported RFC8974.
coap_response_handler_t response_handler
Called when a response is received.
coap_cache_entry_t * cache
CoAP cache-entry cache.
coap_track_observe_value_t track_observe_value
Callback to save observe value when updated.
uint8_t mcast_per_resource
Mcast controlled on a per resource basis.
uint8_t observe_pending
Observe response pending.
uint8_t testing_cids
Change client's source port every testing_cids.
coap_endpoint_t * endpoint
the endpoints used for listening
uint32_t csm_timeout_ms
Timeout for waiting for a CSM from the remote side.
coap_event_handler_t handle_event
Callback function that is used to signal events to the application.
uint32_t observe_save_freq
How frequently to update observe value.
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
uint8_t observe_no_clear
Observe 4.04 not to be sent on deleting resource.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
coap_observe_deleted_t observe_deleted
Called when there is a observe subscription de-register request.
coap_resource_t * unknown_resource
can be used for handling unknown resources
coap_resource_deleted_t resource_deleted
Invoked when resource is deleted.
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
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
Abstraction of virtual endpoint that can be attached to coap_context_t.
structure for CoAP PDUs
Queue entry.
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int timeout
the randomized timeout value
uint8_t is_mcast
Set if this is a queued mcast response.
struct coap_queue_t * next
coap_mid_t id
CoAP message id.
coap_tick_t t
when to send PDU for the next time
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Abstraction of resource that can be attached to coap_context_t.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...