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>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#ifndef COAP_SESSION_H_
#define COAP_SESSION_H_
typedef struct coap_fixed_point_t {
uint16_t integer_part;
uint16_t fractional_part;
} coap_fixed_point_t;
#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 enum coap_session_type_t {
COAP_SESSION_TYPE_NONE = 0,
COAP_SESSION_TYPE_CLIENT,
COAP_SESSION_TYPE_SERVER,
COAP_SESSION_TYPE_HELLO,
} coap_session_type_t;
typedef enum coap_session_state_t {
COAP_SESSION_STATE_NONE = 0,
COAP_SESSION_STATE_CONNECTING,
COAP_SESSION_STATE_HANDSHAKE,
COAP_SESSION_STATE_CSM,
COAP_SESSION_STATE_ESTABLISHED,
} coap_session_state_t;
coap_session_t *coap_session_reference(coap_session_t *session);
void coap_session_release(coap_session_t *session);
void coap_session_disconnected(coap_session_t *session,
coap_nack_reason_t reason);
void coap_session_set_app_data(coap_session_t *session, void *data);
void *coap_session_get_app_data(const coap_session_t *session);
const coap_address_t *coap_session_get_addr_remote(
const coap_session_t *session);
const coap_address_t *coap_session_get_addr_local(
const coap_session_t *session);
coap_proto_t coap_session_get_proto(const coap_session_t *session);
coap_session_type_t coap_session_get_type(const coap_session_t *session);
coap_session_state_t coap_session_get_state(const coap_session_t *session);
int coap_session_get_ifindex(const coap_session_t *session);
void *coap_session_get_tls(const coap_session_t *session,
coap_tls_library_t *tls_lib);
coap_context_t *coap_session_get_context(const coap_session_t *session);
int coap_session_set_type_client(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(
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(
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_psk2(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_cpsk_t *setup_data
);
const coap_bin_const_t * coap_session_get_psk_hint(
const coap_session_t *session);
const coap_bin_const_t * coap_session_get_psk_key(
const coap_session_t *session);
coap_session_t *coap_new_client_session_pki(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_pki_t *setup_data
);
void coap_session_init_token(coap_session_t *session, size_t length,
const uint8_t *token);
void coap_session_new_token(coap_session_t *session, size_t *length,
uint8_t *token);
const char *coap_session_str(const coap_session_t *session);
coap_endpoint_t *coap_new_endpoint(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_session_get_by_peer(const coap_context_t *ctx,
const coap_address_t *remote_addr, int ifindex);
#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
#ifndef COAP_DEFAULT_LEISURE
#define COAP_DEFAULT_LEISURE (5U)
#endif /* COAP_DEFAULT_LEISURE */
#define COAP_MAX_TRANSMIT_SPAN(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
((1 << (s->max_retransmit)) -1) * \
(s->ack_random_factor.integer_part * 1000 + \
s->ack_random_factor.fractional_part) \
/ 1000000)
#define COAP_MAX_TRANSMIT_WAIT(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
((1 << (s->max_retransmit + 1)) -1) * \
(s->ack_random_factor.integer_part * 1000 + \
s->ack_random_factor.fractional_part) \
/ 1000000)
#define COAP_MAX_LATENCY 100
#define COAP_PROCESSING_DELAY(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part + 500) \
/ 1000)
#define COAP_MAX_RTT(s) \
((2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
#define COAP_EXCHANGE_LIFETIME(s) \
(COAP_MAX_TRANSMIT_SPAN(s) + (2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
#define COAP_NON_LIFETIME(s) \
(COAP_MAX_TRANSMIT_SPAN(s) + COAP_MAX_LATENCY)
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_retransmit(const coap_session_t *session);
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session);
coap_fixed_point_t coap_session_get_ack_random_factor(
const coap_session_t *session);
coap_mid_t coap_session_send_ping(coap_session_t *session);
#endif /* COAP_SESSION_H */
coap_nack_reason_t
coap_nack_reason_t
Definition: coap_io.h:64
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:25
coap_session_send_ping
coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:381
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:44
coap_session_get_ack_random_factor
coap_fixed_point_t coap_session_get_ack_random_factor(const coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:65
coap_session_get_by_peer
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
Definition: coap_session.c:1360
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:34
coap_session_get_ack_timeout
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:60
coap_session_get_max_retransmit
unsigned int coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:55
coap_tls_library_t
coap_tls_library_t
Definition: coap_dtls.h:52
coap_session_str
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition: coap_session.c:1380
coap_endpoint_str
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
Definition: coap_session.c:1419
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_proto_t
coap_proto_t
CoAP protocol types.
Definition: pdu.h:280
coap_session_get_tls
void * coap_session_get_tls(const coap_session_t *session, coap_tls_library_t *tls_lib)
Get the session TLS security ptr (TLS type dependent)
Definition: coap_session.c:1220
coap_session_type_t
coap_session_type_t
coap_session_type_t values
Definition: coap_session.h:41
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:104
coap_free_endpoint
void coap_free_endpoint(coap_endpoint_t *ep)
Definition: coap_session.c:1324
coap_session_get_addr_local
const coap_address_t * coap_session_get_addr_local(const coap_session_t *session)
Get the local IP address from the session.
Definition: coap_session.c:1170
coap_session_set_mtu
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:265
coap_session_get_psk_key
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
Definition: coap_session.c:1061
coap_session_get_proto
coap_proto_t coap_session_get_proto(const coap_session_t *session)
Get the session protocol type.
Definition: coap_session.c:1184
coap_session_get_context
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
Definition: coap_session.c:1177
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:244
coap_session_get_state
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
Definition: coap_session.c:1208
coap_session_state_t
coap_session_state_t
coap_session_state_t values
Definition: coap_session.h:52
coap_new_client_session_psk
coap_session_t * coap_new_client_session_psk(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:913
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:70
coap_new_client_session
coap_session_t * coap_new_client_session(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:898
coap_session_init_token
void coap_session_init_token(coap_session_t *session, size_t length, const uint8_t *token)
Initializes the token value to use as a starting point.
Definition: coap_session.c:1146
coap_session_new_token
void coap_session_new_token(coap_session_t *session, size_t *length, uint8_t *token)
Creates a new token for use.
Definition: coap_session.c:1151
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:98
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:76
coap_new_client_session_pki
coap_session_t * coap_new_client_session_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
Definition: coap_session.c:1067
coap_endpoint_set_default_mtu
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
Definition: coap_session.c:1319
coap_session_get_addr_remote
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address from the session.
Definition: coap_session.c:1163
coap_session_get_psk_hint
const coap_bin_const_t * coap_session_get_psk_hint(const coap_session_t *session)
Get the server session's current Identity Hint (PSK).
Definition: coap_session.c:1054
coap_new_endpoint
coap_endpoint_t * coap_new_endpoint(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:1229
coap_session_set_type_client
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
Definition: coap_session.c:1198
coap_new_client_session_psk2
coap_session_t * coap_new_client_session_psk2(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
Definition: coap_session.c:940
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:456
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_session_get_ifindex
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
Definition: coap_session.c:1214
coap_session_get_type
coap_session_type_t coap_session_get_type(const coap_session_t *session)
Get the session type.
Definition: coap_session.c:1191
COAP_SESSION_TYPE_NONE
@ COAP_SESSION_TYPE_NONE
Not defined.
Definition: coap_session.h:42
COAP_SESSION_TYPE_HELLO
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
Definition: coap_session.h:45
COAP_SESSION_TYPE_SERVER
@ COAP_SESSION_TYPE_SERVER
server-side
Definition: coap_session.h:44
COAP_SESSION_TYPE_CLIENT
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition: coap_session.h:43
COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:55
COAP_SESSION_STATE_CSM
@ COAP_SESSION_STATE_CSM
Definition: coap_session.h:56
COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:57
COAP_SESSION_STATE_NONE
@ COAP_SESSION_STATE_NONE
Definition: coap_session.h:53
COAP_SESSION_STATE_CONNECTING
@ COAP_SESSION_STATE_CONNECTING
Definition: coap_session.h:54
coap_address_t
multi-purpose address abstraction
Definition: address.h:96
coap_bin_const_t
CoAP binary data definition with const data.
Definition: str.h:58
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_cpsk_t
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:338
coap_dtls_pki_t
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:244
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session_internal.h:136
coap_fixed_point_t
Abstraction of a fixed point number that can be used where necessary instead of a float.
Definition: coap_session.h:29
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:31
coap_fixed_point_t::integer_part
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:30
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