libcoap  4.2.1
coap_session.h
Go to the documentation of this file.
1 /* coap_session.h -- Session management for libcoap
2 *
3 * Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
4 *
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
7 */
8 
14 #ifndef COAP_SESSION_H_
15 #define COAP_SESSION_H_
16 
17 
18 #include "coap_forward_decls.h"
19 #include "coap_io.h"
20 #include "coap_time.h"
21 #include "pdu.h"
22 #include "uthash.h"
23 
28 typedef struct coap_fixed_point_t {
29  uint16_t integer_part;
30  uint16_t fractional_part;
33 
34 #define COAP_DEFAULT_SESSION_TIMEOUT 300
35 #define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
36 #define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
37 
38 #define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
39 #define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
40 
45 #define COAP_SESSION_TYPE_CLIENT 1
46 #define COAP_SESSION_TYPE_SERVER 2
47 #define COAP_SESSION_TYPE_HELLO 3
49 typedef uint8_t coap_session_state_t;
50 
53 #define COAP_SESSION_STATE_NONE 0
54 #define COAP_SESSION_STATE_CONNECTING 1
55 #define COAP_SESSION_STATE_HANDSHAKE 2
56 #define COAP_SESSION_STATE_CSM 3
57 #define COAP_SESSION_STATE_ESTABLISHED 4
58 
59 typedef struct coap_session_t {
63  unsigned ref;
64  unsigned tls_overhead;
65  unsigned mtu;
69  int ifindex;
73  void *tls;
74  uint16_t tx_mid;
78  size_t partial_write;
79  uint8_t read_header[8];
80  size_t partial_read;
90  size_t psk_key_len;
91  void *app;
92  unsigned int max_retransmit;
95  unsigned int dtls_timeout_count;
96  int dtls_event;
98 
106 
115 
121 
128 
136 
143 
150 
158 void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
159 
167 
178  struct coap_context_t *ctx,
179  const coap_address_t *local_if,
180  const coap_address_t *server,
181  coap_proto_t proto
182 );
183 
197  struct coap_context_t *ctx,
198  const coap_address_t *local_if,
199  const coap_address_t *server,
200  coap_proto_t proto,
201  const char *identity,
202  const uint8_t *key,
203  unsigned key_len
204 );
205 
222  struct coap_context_t *ctx,
223  const coap_address_t *local_if,
224  const coap_address_t *server,
225  coap_proto_t proto,
226  struct coap_dtls_pki_t *setup_data
227 );
228 
237  struct coap_context_t *ctx,
238  struct coap_endpoint_t *ep
239 );
240 
254  const uint8_t *data, size_t datalen);
255 
270  const uint8_t *data, size_t datalen);
271 
284 
285 
293 const char *coap_session_str(const coap_session_t *session);
294 
295 ssize_t
297  struct coap_queue_t *node);
298 
307 coap_endpoint_t *coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
308 
316 void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu);
317 
319 
320 
328 const char *coap_endpoint_str(const coap_endpoint_t *endpoint);
329 
340  const struct coap_packet_t *packet, coap_tick_t now);
341 
354  coap_tick_t now);
355 
357  const struct coap_address_t *remote_addr, int ifindex);
358 
361 
375 #define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
376 
382 #define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
383 
388 #define COAP_DEFAULT_MAX_RETRANSMIT 4
389 
395 #define COAP_DEFAULT_NSTART 1
396 
409  unsigned int value);
410 
422  coap_fixed_point_t value);
423 
435  coap_fixed_point_t value);
436 
447 
459 
471 
479 
480 #define SESSIONS_ADD(e, obj) \
481  HASH_ADD(hh, (e), addr_info, sizeof((obj)->addr_info), (obj))
482 
483 #define SESSIONS_DELETE(e, obj) \
484  HASH_DELETE(hh, (e), (obj))
485 
486 #define SESSIONS_ITER(e, el, rtmp) \
487  HASH_ITER(hh, (e), el, rtmp)
488 
489 #define SESSIONS_ITER_SAFE(e, el, rtmp) \
490 for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
491 
492 #define SESSIONS_FIND(e, k, res) { \
493  HASH_FIND(hh, (e), &(k), sizeof(k), (res)); \
494  }
495 
496 #endif /* COAP_SESSION_H */
unsigned mtu
path or CSM mtu
Definition: coap_session.h:65
struct coap_session_t coap_session_t
uint8_t * psk_identity
Definition: coap_session.h:87
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint&#39;s default MTU.
coap_tick_t last_rx_tx
Definition: coap_session.h:82
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
Definition: coap_session.h:76
uint8_t con_active
Active CON request sent.
Definition: coap_session.h:75
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:314
void * app
application-specific data
Definition: coap_session.h:91
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
uint8_t coap_proto_t
Definition: pdu.h:339
Abstraction of a fixed point number that can be used where necessary instead of a float...
Definition: coap_session.h:28
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
struct coap_context_t * context
session&#39;s context
Definition: coap_session.h:72
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_fixed_point_t ack_timeout
timeout waiting for ack (default 2 secs)
Definition: coap_session.h:93
void * tls
security parameters
Definition: coap_session.h:73
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:204
unsigned int coap_session_get_max_transmit(coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:53
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...
multi-purpose address abstraction
Definition: address.h:62
uint16_t tx_mid
the last message id that was used in this session
Definition: coap_session.h:74
unsigned ref
reference count from queues
Definition: coap_session.h:63
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:238
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
unsigned tls_overhead
overhead of TLS layer
Definition: coap_session.h:64
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
Clock Handling.
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_address_t local_if
optional local interface address
Definition: coap_session.h:66
int dtls_event
Tracking any (D)TLS events on this sesison.
Definition: coap_session.h:96
coap_nack_reason_t
Definition: coap_io.h:226
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
UT_hash_handle hh
Definition: coap_session.h:67
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
Abstraction of virtual endpoint that can be attached to coap_context_t.
const char * coap_session_str(const coap_session_t *session)
Get session description.
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:145
unsigned int max_retransmit
maximum re-transmit count (default 4)
Definition: coap_session.h:92
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:30
coap_tick_t csm_tx
Definition: coap_session.h:86
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:46
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:187
coap_tick_t last_ping
Definition: coap_session.h:84
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
Definition: coap_session.h:77
coap_proto_t proto
protocol used
Definition: coap_session.h:60
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
void coap_session_set_app_data(coap_session_t *session, void *data)
Stores data with the given session.
Definition: coap_session.c:85
coap_addr_tuple_t addr_info
key: remote/local address info
Definition: coap_session.h:68
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 definitions.
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
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
Definition: coap_session.h:80
coap_pdu_t * partial_pdu
incomplete incoming pdu
Definition: coap_session.h:81
coap_socket_t sock
socket object for the session, if any
Definition: coap_session.h:70
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_type_t type
client or server side socket
Definition: coap_session.h:61
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_state_t state
current state of relationaship with peer
Definition: coap_session.h:62
size_t psk_identity_len
Definition: coap_session.h:88
coap_tick_t last_tx_rst
Definition: coap_session.h:83
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
Definition: coap_session.h:94
int ifindex
interface index
Definition: coap_session.h:69
Pre-defined constants that reflect defaults for CoAP.
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
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:168
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition: coap_session.c:330
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, struct coap_queue_t *node)
Definition: coap_session.c:251
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
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:193
uint8_t coap_session_type_t
Definition: coap_session.h:41
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
void coap_free_endpoint(coap_endpoint_t *ep)
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
size_t psk_key_len
Definition: coap_session.h:90
coap_session_t * coap_session_get_by_peer(struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex)
coap_session_t * session
the CoAP session
Definition: net.h:44
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...
unsigned char uint8_t
Definition: uthash.h:79
struct coap_endpoint_t * endpoint
session&#39;s endpoint
Definition: coap_session.h:71
coap_tick_t last_pong
Definition: coap_session.h:85
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
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
unsigned int dtls_timeout_count
dtls setup retry counter
Definition: coap_session.h:95
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:29
Queue entry.
Definition: net.h:38
uint8_t coap_session_state_t
Definition: coap_session.h:49
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:387
uint8_t * psk_key
Definition: coap_session.h:89
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:147
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
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