/* coap_session.h -- Session management for libcoap * * Copyright (C) 2017 Jean-Claue Michelou * * This file is part of the CoAP library libcoap. Please see * README for terms of use. */ #ifndef _SESSION_H_ #define _SESSION_H_ #include "coap_io.h" #include "coap_time.h" #include "pdu.h" struct coap_endpoint_t; struct coap_context_t; struct coap_queue_t; /** * Abstraction of a fixed point number that can be used where necessary instead * of a float. 1,000 fractional bits equals one integer */ typedef struct coap_fixed_point_t { uint16_t integer_part; /**< Integer part of fixed point variable */ uint16_t fractional_part; /**< Fractional part of fixed point variable 1/1000 (3 points) precision */ } coap_fixed_point_t; #define COAP_DEFAULT_SESSION_TIMEOUT 300 #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 uint8_t coap_session_type_t; /** * coap_session_type_t values */ #define COAP_SESSION_TYPE_CLIENT 1 /**< client-side */ #define COAP_SESSION_TYPE_SERVER 2 /**< server-side */ #define COAP_SESSION_TYPE_HELLO 3 /**< server-side ephemeral session for responding to a client hello */ typedef uint8_t coap_session_state_t; /** * coap_session_state_t values */ #define COAP_SESSION_STATE_NONE 0 #define COAP_SESSION_STATE_CONNECTING 1 #define COAP_SESSION_STATE_HANDSHAKE 2 #define COAP_SESSION_STATE_CSM 3 #define COAP_SESSION_STATE_ESTABLISHED 4 typedef struct coap_session_t { struct coap_session_t *next; coap_proto_t proto; /**< protocol used */ coap_session_type_t type; /**< client or server side socket */ coap_session_state_t state; /**< current state of relationaship with peer */ unsigned ref; /**< reference count from queues */ unsigned tls_overhead; /**< overhead of TLS layer */ unsigned mtu; /**< path or CSM mtu */ coap_address_t local_if; /**< optional local interface address */ coap_address_t remote_addr; /**< remote address and port */ coap_address_t local_addr; /**< local address and port */ int ifindex; /**< interface index */ coap_socket_t sock; /**< socket object for the session, if any */ struct coap_endpoint_t *endpoint; /**< session's endpoint */ struct coap_context_t *context; /**< session's context */ void *tls; /**< security parameters */ uint16_t tx_mid; /**< the last message id that was used in this session */ struct coap_queue_t *sendqueue; /**< list of messages waiting to be sent */ size_t partial_write; /**< if > 0 indicates number of bytes already written from the pdu at the head of sendqueue */ uint8_t read_header[8]; /**< storage space for header of incoming message header */ size_t partial_read; /**< if > 0 indicates number of bytes already read for an incoming message */ coap_pdu_t *partial_pdu; /**< incomplete incoming pdu */ coap_tick_t last_rx_tx; coap_tick_t last_tx_rst; uint8_t *psk_identity; size_t psk_identity_len; uint8_t *psk_key; size_t psk_key_len; void *app; /**< application-specific data */ unsigned int max_retransmit; /**< maximum re-transmit count (default 4) */ coap_fixed_point_t ack_timeout; /**< timeout waiting for ack (default 2 secs) */ coap_fixed_point_t ack_random_factor; /**< ack random factor backoff (default 1.5) */ unsigned int dtls_timeout_count; /**