coap_session.h
Go to the documentation of this file.
/* coap_session.h -- Session management for libcoap
*
* Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#ifndef COAP_SESSION_H_
#define COAP_SESSION_H_
#include "coap_forward_decls.h"
#include "coap_io.h"
#include "coap_time.h"
#include "pdu.h"
#include "uthash.h"
typedef struct coap_fixed_point_t {
uint16_t integer_part;
uint16_t fractional_part;
} coap_fixed_point_t;
#define COAP_DEFAULT_SESSION_TIMEOUT 300
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
#define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
#define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
typedef uint8_t coap_session_type_t;
#define COAP_SESSION_TYPE_CLIENT 1
#define COAP_SESSION_TYPE_SERVER 2
#define COAP_SESSION_TYPE_HELLO 3
typedef uint8_t coap_session_state_t;
#define COAP_SESSION_STATE_NONE 0
#define COAP_SESSION_STATE_CONNECTING 1
#define COAP_SESSION_STATE_HANDSHAKE 2
#define COAP_SESSION_STATE_CSM 3
#define COAP_SESSION_STATE_ESTABLISHED 4
typedef struct coap_session_t {
coap_proto_t proto;
coap_session_type_t type;
coap_session_state_t state;
unsigned ref;
unsigned tls_overhead;
unsigned mtu;
coap_address_t local_if;
UT_hash_handle hh;
coap_addr_tuple_t addr_info;
int ifindex;
coap_socket_t sock;
struct coap_endpoint_t *endpoint;
struct coap_context_t *context;
void *tls;
uint16_t tx_mid;
uint8_t con_active;
coap_tid_t last_ping_mid;
struct coap_queue_t *delayqueue;
size_t partial_write;
uint8_t read_header[8];
size_t partial_read;
coap_pdu_t *partial_pdu;
coap_tick_t last_rx_tx;
coap_tick_t last_tx_rst;
coap_tick_t last_ping;
coap_tick_t last_pong;
coap_tick_t csm_tx;
uint8_t *psk_identity;
size_t psk_identity_len;
uint8_t *psk_key;
size_t psk_key_len;
void *app;
unsigned int max_retransmit;
coap_fixed_point_t ack_timeout;
coap_fixed_point_t ack_random_factor;
unsigned int dtls_timeout_count;
int dtls_event;
} coap_session_t;
coap_session_t *coap_session_reference(coap_session_t *session);
void coap_session_release(coap_session_t *session);
void coap_session_set_app_data(coap_session_t *session, void *data);
void *coap_session_get_app_data(const coap_session_t *session);
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason);
void coap_session_send_csm(coap_session_t *session);
void coap_session_connected(coap_session_t *session);
void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
size_t coap_session_max_pdu_size(const coap_session_t *session);
coap_session_t *coap_new_client_session(
struct coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto
);
coap_session_t *coap_new_client_session_psk(
struct coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
const char *identity,
const uint8_t *key,
unsigned key_len
);
coap_session_t *coap_new_client_session_pki(
struct coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
struct coap_dtls_pki_t *setup_data
);
coap_session_t *coap_new_server_session(
struct coap_context_t *ctx,
struct coap_endpoint_t *ep
);
ssize_t coap_session_send(coap_session_t *session,
const uint8_t *data, size_t datalen);
ssize_t coap_session_write(coap_session_t *session,
const uint8_t *data, size_t datalen);
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu);
const char *coap_session_str(const coap_session_t *session);
ssize_t
coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
struct coap_queue_t *node);
coap_endpoint_t *coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu);
void coap_free_endpoint(coap_endpoint_t *ep);
const char *coap_endpoint_str(const coap_endpoint_t *endpoint);
coap_session_t *coap_endpoint_get_session(coap_endpoint_t *endpoint,
const struct coap_packet_t *packet, coap_tick_t now);
coap_session_t *coap_session_new_dtls_session(coap_session_t *session,
coap_tick_t now);
coap_session_t *coap_session_get_by_peer(struct coap_context_t *ctx,
const struct coap_address_t *remote_addr, int ifindex);
void coap_session_free(coap_session_t *session);
void coap_session_mfree(coap_session_t *session);
#define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
#define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
#define COAP_DEFAULT_MAX_RETRANSMIT 4
#define COAP_DEFAULT_NSTART 1
void coap_session_set_max_retransmit(coap_session_t *session,
unsigned int value);
void coap_session_set_ack_timeout(coap_session_t *session,
coap_fixed_point_t value);
void coap_session_set_ack_random_factor(coap_session_t *session,
coap_fixed_point_t value);
unsigned int coap_session_get_max_transmit(coap_session_t *session);
coap_fixed_point_t coap_session_get_ack_timeout(coap_session_t *session);
coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session);
coap_tid_t coap_session_send_ping(coap_session_t *session);
#define SESSIONS_ADD(e, obj) \
HASH_ADD(hh, (e), addr_info, sizeof((obj)->addr_info), (obj))
#define SESSIONS_DELETE(e, obj) \
HASH_DELETE(hh, (e), (obj))
#define SESSIONS_ITER(e, el, rtmp) \
HASH_ITER(hh, (e), el, rtmp)
#define SESSIONS_ITER_SAFE(e, el, rtmp) \
for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
#define SESSIONS_FIND(e, k, res) { \
HASH_FIND(hh, (e), &(k), sizeof(k), (res)); \
}
#endif /* COAP_SESSION_H */
coap_session_t::mtu
unsigned mtu
path or CSM mtu
Definition: coap_session.h:65
coap_session_t
struct coap_session_t coap_session_t
coap_session_t::psk_identity
uint8_t * psk_identity
Definition: coap_session.h:87
coap_endpoint_set_default_mtu
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint&#39;s default MTU.
Definition: coap_session.c:1001
coap_session_t::last_rx_tx
coap_tick_t last_rx_tx
Definition: coap_session.h:82
coap_session_t::last_ping_mid
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
Definition: coap_session.h:76
coap_session_t
Definition: coap_session.h:59
coap_session_t::con_active
uint8_t con_active
Active CON request sent.
Definition: coap_session.h:75
coap_session_send_ping
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:314
coap_session_t::app
void * app
application-specific data
Definition: coap_session.h:91
coap_new_endpoint
coap_endpoint_t * coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
Definition: coap_session.c:918
coap_proto_t
uint8_t coap_proto_t
Definition: pdu.h:339
coap_fixed_point_t
Abstraction of a fixed point number that can be used where necessary instead of a float...
Definition: coap_session.h:28
coap_session_send_csm
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
Definition: coap_session.c:288
coap_session_t::context
struct coap_context_t * context
session&#39;s context
Definition: coap_session.h:72
coap_session_send
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
Definition: coap_session.c:216
coap_session_t::ack_timeout
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2 secs)
Definition: coap_session.h:93
coap_session_t::tls
void * tls
security parameters
Definition: coap_session.h:73
coap_session_set_mtu
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:204
coap_session_get_max_transmit
unsigned int coap_session_get_max_transmit(coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:53
coap_socket_t
Definition: coap_io.h:54
coap_fixed_point_t
struct coap_fixed_point_t coap_fixed_point_t
Abstraction of a fixed point number that can be used where necessary instead of a float...
coap_address_t
multi-purpose address abstraction
Definition: address.h:62
coap_session_t::tx_mid
uint16_t tx_mid
the last message id that was used in this session
Definition: coap_session.h:74
coap_session_t::ref
unsigned ref
reference count from queues
Definition: coap_session.h:63
coap_tid_t
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:238
coap_new_client_session
coap_session_t * coap_new_client_session(struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
Definition: coap_session.c:766
coap_session_t::tls_overhead
unsigned tls_overhead
overhead of TLS layer
Definition: coap_session.h:64
coap_session_new_dtls_session
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
Definition: coap_session.c:592
coap_time.h
Clock Handling.
coap_session_get_ack_timeout
coap_fixed_point_t coap_session_get_ack_timeout(coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:58
coap_packet_t
Definition: coap_io.h:217
coap_session_t::local_if
coap_address_t local_if
optional local interface address
Definition: coap_session.h:66
coap_session_t::dtls_event
int dtls_event
Tracking any (D)TLS events on this sesison.
Definition: coap_session.h:96
UT_hash_handle
Definition: uthash.h:1097
coap_nack_reason_t
coap_nack_reason_t
Definition: coap_io.h:226
coap_new_server_session
coap_session_t * coap_new_server_session(struct coap_context_t *ctx, struct coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
Definition: coap_session.c:881
coap_session_t::hh
UT_hash_handle hh
Definition: coap_session.h:67
coap_endpoint_str
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
Definition: coap_session.c:1088
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session_internal.h:29
coap_session_str
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition: coap_session.c:1049
coap_session_mfree
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:145
coap_session_t::max_retransmit
unsigned int max_retransmit
maximum re-transmit count (default 4)
Definition: coap_session.h:92
coap_fixed_point_t::fractional_part
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:30
coap_session_t::csm_tx
coap_tick_t csm_tx
Definition: coap_session.h:86
coap_queue_t::pdu
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:46
coap_session_max_pdu_size
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:187
coap_session_t::last_ping
coap_tick_t last_ping
Definition: coap_session.h:84
coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
coap_session_t::delayqueue
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
Definition: coap_session.h:77
coap_session_t::proto
coap_proto_t proto
protocol used
Definition: coap_session.h:60
coap_new_client_session_pki
coap_session_t * coap_new_client_session_pki(struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, struct coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
Definition: coap_session.c:841
coap_session_set_app_data
void coap_session_set_app_data(coap_session_t *session, void *data)
Stores data with the given session.
Definition: coap_session.c:85
coap_session_t::addr_info
coap_addr_tuple_t addr_info
key: remote/local address info
Definition: coap_session.h:68
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:93
coap_forward_decls.h
COAP forward definitions.
coap_session_send_pdu
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session&#39;s protocol.
Definition: net.c:683
coap_session_t::partial_read
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
Definition: coap_session.h:80
coap_session_t::partial_pdu
coap_pdu_t * partial_pdu
incomplete incoming pdu
Definition: coap_session.h:81
coap_session_t::sock
coap_socket_t sock
socket object for the session, if any
Definition: coap_session.h:70
coap_session_set_ack_timeout
void coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:32
coap_session_t::type
coap_session_type_t type
client or server side socket
Definition: coap_session.h:61
coap_session_get_ack_random_factor
coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:63
coap_session_t::state
coap_session_state_t state
current state of relationaship with peer
Definition: coap_session.h:62
coap_session_t::psk_identity_len
size_t psk_identity_len
Definition: coap_session.h:88
coap_session_t::last_tx_rst
coap_tick_t last_tx_rst
Definition: coap_session.h:83
coap_session_t::ack_random_factor
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
Definition: coap_session.h:94
coap_session_t::ifindex
int ifindex
interface index
Definition: coap_session.h:69
pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_session_t::partial_write
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue ...
Definition: coap_session.h:78
coap_addr_tuple_t
Definition: coap_io.h:49
coap_session_free
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:168
coap_session_connected
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition: coap_session.c:330
coap_session_delay_pdu
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, struct coap_queue_t *node)
Definition: coap_session.c:251
coap_io.h
coap_session_write
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
Definition: coap_session.c:237
coap_dtls_pki_t
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:193
coap_session_type_t
uint8_t coap_session_type_t
Definition: coap_session.h:41
coap_session_set_ack_random_factor
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
Definition: coap_session.c:42
coap_free_endpoint
void coap_free_endpoint(coap_endpoint_t *ep)
Definition: coap_session.c:1006
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
coap_session_t::psk_key_len
size_t psk_key_len
Definition: coap_session.h:90
coap_session_get_by_peer
coap_session_t * coap_session_get_by_peer(struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex)
coap_queue_t::session
coap_session_t * session
the CoAP session
Definition: net.h:44
coap_endpoint_get_session
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const struct coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one...
uint8_t
unsigned char uint8_t
Definition: uthash.h:79
coap_session_t::endpoint
struct coap_endpoint_t * endpoint
session&#39;s endpoint
Definition: coap_session.h:71
coap_session_t::last_pong
coap_tick_t last_pong
Definition: coap_session.h:85
coap_session_set_max_retransmit
void coap_session_set_max_retransmit(coap_session_t *session, unsigned int value)
Set the CoAP maximum retransmit count before failure.
Definition: coap_session.c:23
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
coap_session_t::dtls_timeout_count
unsigned int dtls_timeout_count
dtls setup retry counter
Definition: coap_session.h:95
coap_fixed_point_t::integer_part
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:29
coap_queue_t
Queue entry.
Definition: net.h:38
coap_session_state_t
uint8_t coap_session_state_t
Definition: coap_session.h:49
coap_session_disconnected
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:387
coap_session_t::psk_key
uint8_t * psk_key
Definition: coap_session.h:89
coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:147
coap_new_client_session_psk
coap_session_t * coap_new_client_session_psk(struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, unsigned key_len)
Creates a new client session to the designated server with PSK credentials.
Definition: coap_session.c:781
coap_session_get_app_data
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
Definition: coap_session.c:91
uthash.h