libcoap  4.2.0
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 
9 #ifndef COAP_SESSION_H_
10 #define COAP_SESSION_H_
11 
12 
13 #include "coap_io.h"
14 #include "coap_time.h"
15 #include "pdu.h"
16 
17 struct coap_endpoint_t;
18 struct coap_context_t;
19 struct coap_queue_t;
20 
25 typedef struct coap_fixed_point_t {
26  uint16_t integer_part;
27  uint16_t fractional_part;
30 
31 #define COAP_DEFAULT_SESSION_TIMEOUT 300
32 #define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
33 #define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
34 
35 #define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
36 #define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
37 
42 #define COAP_SESSION_TYPE_CLIENT 1
43 #define COAP_SESSION_TYPE_SERVER 2
44 #define COAP_SESSION_TYPE_HELLO 3
46 typedef uint8_t coap_session_state_t;
47 
50 #define COAP_SESSION_STATE_NONE 0
51 #define COAP_SESSION_STATE_CONNECTING 1
52 #define COAP_SESSION_STATE_HANDSHAKE 2
53 #define COAP_SESSION_STATE_CSM 3
54 #define COAP_SESSION_STATE_ESTABLISHED 4
55 
56 typedef struct coap_session_t {
61  unsigned ref;
62  unsigned tls_overhead;
63  unsigned mtu;
67  int ifindex;
71  void *tls;
72  uint16_t tx_mid;
75  size_t partial_write;
76  uint8_t read_header[8];
77  size_t partial_read;
87  size_t psk_key_len;
88  void *app;
89  unsigned int max_retransmit;
92  unsigned int dtls_timeout_count;
93  int dtls_event;
95 
103 
112 
118 
125 
133 
140 
147 
155 void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
156 
164 
175  struct coap_context_t *ctx,
176  const coap_address_t *local_if,
177  const coap_address_t *server,
178  coap_proto_t proto
179 );
180 
194  struct coap_context_t *ctx,
195  const coap_address_t *local_if,
196  const coap_address_t *server,
197  coap_proto_t proto,
198  const char *identity,
199  const uint8_t *key,
200  unsigned key_len
201 );
202 
203 struct coap_dtls_pki_t;
204 
221  struct coap_context_t *ctx,
222  const coap_address_t *local_if,
223  const coap_address_t *server,
224  coap_proto_t proto,
225  struct coap_dtls_pki_t *setup_data
226 );
227 
236  struct coap_context_t *ctx,
237  struct coap_endpoint_t *ep
238 );
239 
252 ssize_t coap_session_send(coap_session_t *session,
253  const uint8_t *data, size_t datalen);
254 
268 ssize_t coap_session_write(coap_session_t *session,
269  const uint8_t *data, size_t datalen);
270 
282 ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu);
283 
284 
292 const char *coap_session_str(const coap_session_t *session);
293 
294 ssize_t
296  struct coap_queue_t *node);
301 typedef struct coap_endpoint_t {
305  uint16_t default_mtu;
311 
320 coap_endpoint_t *coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
321 
330 
332 
333 
341 const char *coap_endpoint_str(const coap_endpoint_t *endpoint);
342 
353  const struct coap_packet_t *packet, coap_tick_t now);
354 
367  const struct coap_packet_t *packet, coap_tick_t now);
368 
370  const struct coap_address_t *remote_addr, int ifindex);
371 
372 void coap_session_free(coap_session_t *session);
373 void coap_session_mfree(coap_session_t *session);
374 
388 #define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
389 
395 #define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
396 
401 #define COAP_DEFAULT_MAX_RETRANSMIT 4
402 
408 #define COAP_DEFAULT_NSTART 1
409 
422  unsigned int value);
423 
435  coap_fixed_point_t value);
436 
448  coap_fixed_point_t value);
449 
459 unsigned int coap_session_get_max_transmit(coap_session_t *session);
460 
472 
484 
492 
493 #endif /* COAP_SESSION_H */
unsigned mtu
path or CSM mtu
Definition: coap_session.h:63
struct coap_session_t coap_session_t
uint8_t * psk_identity
Definition: coap_session.h:84
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint&#39;s default MTU.
Definition: coap_session.c:896
coap_tick_t last_rx_tx
Definition: coap_session.h:79
uint8_t con_active
Active CON request sent.
Definition: coap_session.h:73
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:316
void * app
application-specific data
Definition: coap_session.h:88
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:810
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:25
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:290
struct coap_context_t * context
session&#39;s context
Definition: coap_session.h:70
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:218
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2 secs)
Definition: coap_session.h:90
struct coap_endpoint_t * next
Definition: coap_session.h:302
void * tls
security parameters
Definition: coap_session.h:71
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:206
coap_session_t * sessions
list of active sessions
Definition: coap_session.h:308
coap_endpoint_t * endpoint
the endpoints used for listening
Definition: net.h:166
unsigned int coap_session_get_max_transmit(coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:56
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:72
unsigned ref
reference count from queues
Definition: coap_session.h:61
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:665
unsigned tls_overhead
overhead of TLS layer
Definition: coap_session.h:62
Clock Handling.
uint16_t default_mtu
default mtu for this interface
Definition: coap_session.h:305
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:61
coap_address_t local_if
optional local interface address
Definition: coap_session.h:64
struct coap_session_t * next
Definition: coap_session.h:57
int dtls_event
Tracking any (D)TLS events on this sesison.
Definition: coap_session.h:93
coap_session_t * coap_endpoint_new_dtls_session(coap_endpoint_t *endpoint, const struct coap_packet_t *packet, coap_tick_t now)
Create a new DTLS session for the endpoint.
coap_nack_reason_t
Definition: coap_io.h:206
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:780
struct coap_context_t * context
endpoint&#39;s context
Definition: coap_session.h:303
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
Definition: coap_session.c:980
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session.h:301
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition: coap_session.c:943
coap_address_t local_addr
local address and port
Definition: coap_session.h:66
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:147
unsigned int max_retransmit
maximum re-transmit count (default 4)
Definition: coap_session.h:89
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:27
coap_tick_t csm_tx
Definition: coap_session.h:83
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:189
coap_tick_t last_ping
Definition: coap_session.h:81
coap_address_t remote_addr
remote address and port
Definition: coap_session.h:65
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:74
coap_proto_t proto
protocol used
Definition: coap_session.h:58
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:740
void coap_session_set_app_data(coap_session_t *session, void *data)
Stores data with the given session.
Definition: coap_session.c:88
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:85
coap_session_t hello
special session of DTLS hello messages
Definition: coap_session.h:309
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:627
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
Definition: coap_session.h:77
coap_pdu_t * partial_pdu
incomplete incoming pdu
Definition: coap_session.h:78
coap_socket_t sock
socket object for the session, if any
Definition: coap_session.h:68
coap_proto_t proto
protocol used on this interface
Definition: coap_session.h:304
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:35
coap_session_type_t type
client or server side socket
Definition: coap_session.h:59
coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:66
coap_session_state_t state
current state of relationaship with peer
Definition: coap_session.h:60
size_t psk_identity_len
Definition: coap_session.h:85
coap_tick_t last_tx_rst
Definition: coap_session.h:80
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
Definition: coap_session.h:91
int ifindex
interface index
Definition: coap_session.h:67
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:75
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:170
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition: coap_session.c:326
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, struct coap_queue_t *node)
Definition: coap_session.c:253
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:239
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:191
uint8_t coap_session_type_t
Definition: coap_session.h:38
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:45
struct coap_endpoint_t coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
void coap_free_endpoint(coap_endpoint_t *ep)
Definition: coap_session.c:901
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:71
size_t psk_key_len
Definition: coap_session.h:87
coap_session_t * coap_session_get_by_peer(struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex)
coap_socket_t sock
socket object for the interface, if any
Definition: coap_session.h:306
coap_session_t * session
the CoAP session
Definition: net.h:45
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:69
coap_tick_t last_pong
Definition: coap_session.h:82
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:26
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:77
unsigned int dtls_timeout_count
dtls setup retry counter
Definition: coap_session.h:92
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:26
Queue entry.
Definition: net.h:39
uint8_t coap_session_state_t
Definition: coap_session.h:46
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:383
coap_address_t bind_addr
local interface address
Definition: coap_session.h:307
uint8_t * psk_key
Definition: coap_session.h:86
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:148
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:680
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:94