14 #ifndef COAP_SESSION_H_
15 #define COAP_SESSION_H_
35 #define COAP_DEFAULT_SESSION_TIMEOUT 300
36 #define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
37 #define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
39 #define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
40 #define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
46 #define COAP_SESSION_TYPE_CLIENT 1
47 #define COAP_SESSION_TYPE_SERVER 2
48 #define COAP_SESSION_TYPE_HELLO 3
54 #define COAP_SESSION_STATE_NONE 0
55 #define COAP_SESSION_STATE_CONNECTING 1
56 #define COAP_SESSION_STATE_HANDSHAKE 2
57 #define COAP_SESSION_STATE_CSM 3
58 #define COAP_SESSION_STATE_ESTABLISHED 4
228 const char *identity,
330 const uint8_t *data,
size_t datalen);
346 const uint8_t *data,
size_t datalen);
453 #define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
462 #define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
470 #define COAP_DEFAULT_MAX_RETRANSMIT 4
477 #define COAP_DEFAULT_NSTART 1
485 #define COAP_MAX_TRANSMIT_SPAN(s) \
486 ((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
487 ((1 << (s->max_retransmit)) -1) * \
488 (s->ack_random_factor.integer_part * 1000 + \
489 s->ack_random_factor.fractional_part) \
498 #define COAP_MAX_TRANSMIT_WAIT(s) \
499 ((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
500 ((1 << (s->max_retransmit + 1)) -1) * \
501 (s->ack_random_factor.integer_part * 1000 + \
502 s->ack_random_factor.fractional_part) \
509 #define COAP_MAX_LATENCY 100
517 #define COAP_PROCESSING_DELAY(s) \
518 ((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part + 500) \
527 #define COAP_MAX_RTT(s) \
528 ((2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
536 #define COAP_EXCHANGE_LIFETIME(s) \
537 (COAP_MAX_TRANSMIT_SPAN(s) + (2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
545 #define COAP_NON_LIFETIME(s) \
546 (COAP_MAX_TRANSMIT_SPAN(s) + COAP_MAX_LATENCY)
631 #define SESSIONS_ADD(e, obj) \
632 HASH_ADD(hh, (e), addr_info, sizeof((obj)->addr_info), (obj))
634 #define SESSIONS_DELETE(e, obj) \
635 HASH_DELETE(hh, (e), (obj))
637 #define SESSIONS_ITER(e, el, rtmp) \
638 HASH_ITER(hh, (e), el, rtmp)
640 #define SESSIONS_ITER_SAFE(e, el, rtmp) \
641 for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
643 #define SESSIONS_FIND(e, k, res) { \
644 HASH_FIND(hh, (e), &(k), sizeof(k), (res)); \
COAP forward definitions.
void coap_session_set_max_retransmit(coap_session_t *session, unsigned int value)
Set the CoAP maximum retransmit count before failure.
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, struct coap_queue_t *node)
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...
void coap_free_endpoint(coap_endpoint_t *ep)
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
int coap_session_refresh_psk_key(coap_session_t *session, const struct coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
coap_session_t * coap_session_get_by_peer(struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex)
uint8_t coap_session_state_t
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.
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.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
unsigned int coap_session_get_max_transmit(coap_session_t *session)
Get the CoAP maximum retransmit before failure.
uint8_t coap_session_type_t
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
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.
int coap_session_refresh_psk_hint(coap_session_t *session, const struct coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
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.
void coap_session_set_app_data(coap_session_t *session, void *data)
Stores data with the given session.
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
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.
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.
coap_session_t * coap_new_client_session_psk2(struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, struct coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
void coap_session_free(coap_session_t *session)
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
void coap_session_mfree(coap_session_t *session)
coap_session_t * coap_new_server_session(struct coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
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.
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
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.
struct coap_session_t coap_session_t
coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session)
Get the CoAP ack randomize factor.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
const char * coap_session_str(const coap_session_t *session)
Get session description.
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
Pre-defined constants that reflect defaults for CoAP.
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
multi-purpose address abstraction
CoAP binary data definition with const data.
The CoAP stack's global state is stored in a coap_context_t object.
The structure used for defining the Client PSK setup data to be used.
The structure used for defining the PKI setup data to be used.
Abstraction of virtual endpoint that can be attached to coap_context_t.
Abstraction of a fixed point number that can be used where necessary instead of a float.
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
uint16_t integer_part
Integer part of fixed point variable.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int dtls_timeout_count
dtls setup retry counter
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
coap_endpoint_t * endpoint
session's endpoint
coap_socket_t sock
socket object for the session, if any
unsigned int max_retransmit
maximum re-transmit count (default 4)
coap_pdu_t * partial_pdu
incomplete incoming pdu
unsigned tls_overhead
overhead of TLS layer
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
coap_session_state_t state
current state of relationaship with peer
uint8_t read_header[8]
storage space for header of incoming message header
coap_addr_tuple_t addr_info
key: remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
unsigned ref
reference count from queues
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
struct coap_context_t * context
session's context
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this sesison.
void * tls
security parameters
uint8_t csm_block_supported
CSM TCP blocks supported.
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t con_active
Active CON request sent.
void * app
application-specific data
coap_address_t local_if
optional local interface address
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2 secs)
coap_session_type_t type
client or server side socket
uint64_t mtu
path or CSM mtu
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
int ifindex
interface index