net.h
Go to the documentation of this file.
/*
* net.h -- CoAP network interface
*
* Copyright (C) 2010-2021 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.
*/
#ifndef COAP_NET_H_
#define COAP_NET_H_
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <time.h>
#ifdef WITH_LWIP
#include <lwip/ip_addr.h>
#endif
#include "coap_io.h"
#include "coap_dtls.h"
#include "coap_event.h"
#include "pdu.h"
#include "coap_session.h"
typedef enum coap_response_t {
COAP_RESPONSE_FAIL,
COAP_RESPONSE_OK
} coap_response_t;
typedef 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);
typedef 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);
typedef void (*coap_ping_handler_t)(coap_session_t *session,
const coap_pdu_t *received,
const coap_mid_t mid);
typedef void (*coap_pong_handler_t)(coap_session_t *session,
const coap_pdu_t *received,
const coap_mid_t mid);
void
coap_register_response_handler(coap_context_t *context,
coap_response_handler_t handler);
void
coap_register_nack_handler(coap_context_t *context,
coap_nack_handler_t handler);
void
coap_register_ping_handler(coap_context_t *context,
coap_ping_handler_t handler);
void
coap_register_pong_handler(coap_context_t *context,
coap_pong_handler_t handler);
void
coap_register_option(coap_context_t *ctx, uint16_t type);
coap_context_t *coap_new_context(const coap_address_t *listen_addr);
int coap_context_set_psk( coap_context_t *context, const char *hint,
const uint8_t *key, size_t key_len );
int coap_context_set_psk2(coap_context_t *context,
coap_dtls_spsk_t *setup_data);
int
coap_context_set_pki(coap_context_t *context,
const coap_dtls_pki_t *setup_data);
int
coap_context_set_pki_root_cas(coap_context_t *context,
const char *ca_file,
const char *ca_dir);
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
int coap_context_get_coap_fd(const coap_context_t *context);
void
coap_context_set_max_idle_sessions(coap_context_t *context,
unsigned int max_idle_sessions);
unsigned int
coap_context_get_max_idle_sessions(const coap_context_t *context);
void
coap_context_set_session_timeout(coap_context_t *context,
unsigned int session_timeout);
unsigned int
coap_context_get_session_timeout(const coap_context_t *context);
void
coap_context_set_csm_timeout(coap_context_t *context,
unsigned int csm_tmeout);
unsigned int
coap_context_get_csm_timeout(const coap_context_t *context);
void
coap_context_set_max_handshake_sessions(coap_context_t *context,
unsigned int max_handshake_sessions);
unsigned int
coap_context_get_max_handshake_sessions(const coap_context_t *context);
uint16_t coap_new_message_id(coap_session_t *session);
void coap_free_context(coap_context_t *context);
void coap_set_app_data(coap_context_t *context, void *data);
void *coap_get_app_data(const coap_context_t *context);
coap_pdu_t *coap_new_error_response(const coap_pdu_t *request,
coap_pdu_code_t code,
coap_opt_filter_t *opts);
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);
coap_mid_t
coap_send_message_type(coap_session_t *session, const coap_pdu_t *request,
coap_pdu_type_t type);
coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request);
COAP_STATIC_INLINE coap_mid_t
coap_send_rst(coap_session_t *session, const coap_pdu_t *request) {
return coap_send_message_type(session, request, COAP_MESSAGE_RST);
}
coap_mid_t coap_send( coap_session_t *session, coap_pdu_t *pdu );
#define coap_send_large(session, pdu) coap_send(session, pdu)
int coap_handle_event(coap_context_t *context,
coap_event_t event,
coap_session_t *session);
int coap_can_exit(coap_context_t *context);
void coap_ticks(coap_tick_t *);
int
coap_join_mcast_group_intf(coap_context_t *ctx, const char *groupname,
const char *ifname);
#define coap_join_mcast_group(ctx, groupname) \
(coap_join_mcast_group_intf(ctx, groupname, NULL))
int
coap_mcast_set_hops(coap_session_t *session, size_t hops);
#define COAP_IO_WAIT 0
#define COAP_IO_NO_WAIT ((uint32_t)-1)
int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
#ifndef RIOT_VERSION
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);
#endif /* !RIOT_VERSION */
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
);
void coap_io_do_io(coap_context_t *ctx, coap_tick_t now);
unsigned int
coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now);
struct epoll_event;
void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event* events,
size_t nevents);
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_run_once(coap_context_t *ctx, uint32_t timeout_ms)
{
return coap_io_process(ctx, timeout_ms);
}
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
) {
return coap_io_prepare_io(ctx, sockets, max_sockets, num_sockets, now);
}
COAP_STATIC_INLINE COAP_DEPRECATED void
coap_read(coap_context_t *ctx, coap_tick_t now
) {
coap_io_do_io(ctx, now);
}
/* Old definitions which may be hanging around in old code - be helpful! */
#define COAP_RUN_NONBLOCK COAP_RUN_NONBLOCK_deprecated_use_COAP_IO_NO_WAIT
#define COAP_RUN_BLOCK COAP_RUN_BLOCK_deprecated_use_COAP_IO_WAIT
#endif /* COAP_NET_H_ */
coap_dtls.h
coap_event.h
coap_io.h
coap_nack_reason_t
coap_nack_reason_t
Definition: coap_io.h:64
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: net.c:1738
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:1007
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: net.c:1799
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:1059
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:1261
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:1266
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:122
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: net.c:752
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: net.c:485
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.
Definition: net.c:408
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: net.c:469
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:1158
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: net.h:90
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: net.c:458
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: net.c:3174
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: net.h:54
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: net.c:3250
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: net.c:625
coap_free_context
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:631
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: net.c:463
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: net.c:495
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: net.c:3163
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.
Definition: net.c:388
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.
Definition: net.c:3503
coap_response_t
coap_response_t
Definition: net.h:38
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: net.h:79
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: net.c:437
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: net.h:67
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: net.h:452
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: net.c:505
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: net.c:920
coap_context_get_session_timeout
unsigned int coap_context_get_session_timeout(const coap_context_t *context)
Get the session timeout value.
Definition: net.c:491
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: net.c:902
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.
Definition: net.c:422
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: net.c:3262
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: net.c:3274
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...
Definition: net.c:3280
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: net.c:452
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: net.c:2057
coap_context_set_keepalive
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
Definition: net.c:447
coap_set_app_data
void coap_set_app_data(coap_context_t *context, void *data)
Stores data with the given CoAP context.
Definition: net.c:619
coap_context_get_csm_timeout
unsigned int coap_context_get_csm_timeout(const coap_context_t *context)
Get the CSM timeout value.
Definition: net.c:480
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: net.c:3268
coap_send
coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1057
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: net.c:3256
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: net.c:474
COAP_RESPONSE_FAIL
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
Definition: net.h:39
COAP_RESPONSE_OK
@ COAP_RESPONSE_OK
Response is fine.
Definition: net.h:40
coap_event_t
unsigned int coap_event_t
Scalar type to represent different events, e.g.
Definition: coap_event.h:27
coap_mid_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:231
coap_pdu_code_t
coap_pdu_code_t
Set of codes available for a PDU.
Definition: pdu.h:291
coap_pdu_type_t
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:56
COAP_MESSAGE_RST
@ COAP_MESSAGE_RST
Definition: pdu.h:60
COAP_DEPRECATED
#define COAP_DEPRECATED
Definition: libcoap.h:48
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:40
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: net.h:750
coap_run_once
COAP_STATIC_INLINE COAP_DEPRECATED int coap_run_once(coap_context_t *ctx, uint32_t timeout_ms)
Definition: net.h:726
coap_read
COAP_STATIC_INLINE COAP_DEPRECATED void coap_read(coap_context_t *ctx, coap_tick_t now)
Definition: net.h:770
pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_address_t
multi-purpose address abstraction
Definition: address.h:96
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: coap_net_internal.h:45
coap_dtls_pki_t
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:244
coap_dtls_spsk_t
The structure used for defining the Server PSK setup data to be used.
Definition: coap_dtls.h:425
coap_opt_filter_t
Definition: option.h:100
coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: coap_pdu_internal.h:94
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:48
coap_socket_t
Definition: coap_io_internal.h:23