libcoap  4.2.0rc2
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 _SESSION_H_
10 #define _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 
33 #define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
34 #define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
35 
40 #define COAP_SESSION_TYPE_CLIENT 1
41 #define COAP_SESSION_TYPE_SERVER 2
42 #define COAP_SESSION_TYPE_HELLO 3
44 typedef uint8_t coap_session_state_t;
45 
48 #define COAP_SESSION_STATE_NONE 0
49 #define COAP_SESSION_STATE_CONNECTING 1
50 #define COAP_SESSION_STATE_HANDSHAKE 2
51 #define COAP_SESSION_STATE_CSM 3
52 #define COAP_SESSION_STATE_ESTABLISHED 4
53 
54 typedef struct coap_session_t {
59  unsigned ref;
60  unsigned tls_overhead;
61  unsigned mtu;
65  int ifindex;
69  void *tls;
70  uint16_t tx_mid;
73  size_t partial_write;
74  uint8_t read_header[8];
75  size_t partial_read;
85  size_t psk_key_len;
86  void *app;
87  unsigned int max_retransmit;
90  unsigned int dtls_timeout_count;
91  int dtls_event;
93 
101 
110 
116 
123 
131 
138 
145 
153 void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
154 
162 
173  struct coap_context_t *ctx,
174  const coap_address_t *local_if,
175  const coap_address_t *server,
176  coap_proto_t proto
177 );
178 
192  struct coap_context_t *ctx,
193  const coap_address_t *local_if,
194  const coap_address_t *server,
195  coap_proto_t proto,
196  const char *identity,
197  const uint8_t *key,
198  unsigned key_len
199 );
200 
201 struct coap_dtls_pki_t;
202 
219  struct coap_context_t *ctx,
220  const coap_address_t *local_if,
221  const coap_address_t *server,
222  coap_proto_t proto,
223  struct coap_dtls_pki_t *setup_data
224 );
225 
234  struct coap_context_t *ctx,
235  struct coap_endpoint_t *ep
236 );
237 
250 ssize_t coap_session_send(coap_session_t *session,
251  const uint8_t *data, size_t datalen);
252 
266 ssize_t coap_session_write(coap_session_t *session,
267  const uint8_t *data, size_t datalen);
268 
280 ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu);
281 
282 
290 const char *coap_session_str(const coap_session_t *session);
291 
292 ssize_t
294  struct coap_queue_t *node);
299 typedef struct coap_endpoint_t {
303  uint16_t default_mtu;
309 
318 coap_endpoint_t *coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
319 
328 
330 
331 
339 const char *coap_endpoint_str(const coap_endpoint_t *endpoint);
340 
351  const struct coap_packet_t *packet, coap_tick_t now);
352 
365  const struct coap_packet_t *packet, coap_tick_t now);
366 
368  const struct coap_address_t *remote_addr, int ifindex);
369 
370 void coap_session_free(coap_session_t *session);
371 
385 #define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
386 
392 #define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
393 
398 #define COAP_DEFAULT_MAX_RETRANSMIT 4
399 
405 #define COAP_DEFAULT_NSTART 1
406 
419  unsigned int value);
420 
432  coap_fixed_point_t value);
433 
445  coap_fixed_point_t value);
446 
456 unsigned int coap_session_get_max_transmit(coap_session_t *session);
457 
469 
481 
489 
490 #endif /* _SESSION_H */
unsigned mtu
path or CSM mtu
Definition: coap_session.h:61
struct coap_session_t coap_session_t
uint8_t * psk_identity
Definition: coap_session.h:82
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint&#39;s default MTU.
Definition: coap_session.c:862
coap_tick_t last_rx_tx
Definition: coap_session.h:77
uint8_t con_active
Active CON request sent.
Definition: coap_session.h:71
coap_tid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:313
void * app
application-specific data
Definition: coap_session.h:86
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:776
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:287
struct coap_context_t * context
session&#39;s context
Definition: coap_session.h:68
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:215
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2 secs)
Definition: coap_session.h:88
struct coap_endpoint_t * next
Definition: coap_session.h:300
void * tls
security parameters
Definition: coap_session.h:69
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:203
coap_session_t * sessions
list of active sessions
Definition: coap_session.h:306
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:70
unsigned ref
reference count from queues
Definition: coap_session.h:59
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:631
unsigned tls_overhead
overhead of TLS layer
Definition: coap_session.h:60
Clock Handling.
uint16_t default_mtu
default mtu for this interface
Definition: coap_session.h:303
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:62
struct coap_session_t * next
Definition: coap_session.h:55
int dtls_event
Tracking any (D)TLS events on this sesison.
Definition: coap_session.h:91
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:746
struct coap_context_t * context
endpoint&#39;s context
Definition: coap_session.h:301
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
Definition: coap_session.c:946
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session.h:299
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition: coap_session.c:909
coap_address_t local_addr
local address and port
Definition: coap_session.h:64
size_t coap_session_max_pdu_size(coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:186
unsigned int max_retransmit
maximum re-transmit count (default 4)
Definition: coap_session.h:87
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:81
coap_tick_t last_ping
Definition: coap_session.h:79
coap_address_t remote_addr
remote address and port
Definition: coap_session.h:63
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:72
coap_proto_t proto
protocol used
Definition: coap_session.h:56
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:706
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:307
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:75
coap_pdu_t * partial_pdu
incomplete incoming pdu
Definition: coap_session.h:76
coap_socket_t sock
socket object for the session, if any
Definition: coap_session.h:66
coap_proto_t proto
protocol used on this interface
Definition: coap_session.h:302
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:57
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:58
size_t psk_identity_len
Definition: coap_session.h:83
coap_tick_t last_tx_rst
Definition: coap_session.h:78
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
Definition: coap_session.h:89
int ifindex
interface index
Definition: coap_session.h:65
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:73
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:147
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition: coap_session.c:323
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, struct coap_queue_t *node)
Definition: coap_session.c:250
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:236
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:190
uint8_t coap_session_type_t
Definition: coap_session.h:36
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:867
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:85
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:304
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:67
coap_tick_t last_pong
Definition: coap_session.h:80
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:90
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:44
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:380
coap_address_t bind_addr
local interface address
Definition: coap_session.h:305
uint8_t * psk_key
Definition: coap_session.h:84
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:646
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