libcoap 4.3.2rc2

coap_dtls.c
Go to the documentation of this file.
1/*
2 * coap_dtls.c -- (D)TLS functions for libcoap
3 *
4 * Copyright (C) 2023 Olaf Bergmann <bergmann@tzi.org>
5 * Copyright (C) 2023 Jon Shallow <supjps-libcoap@jpshallow.com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
18#include "coap3/coap_internal.h"
19
20 void
21 coap_dtls_establish(coap_session_t *session) {
22 session->state = COAP_SESSION_STATE_HANDSHAKE;
23#if COAP_CLIENT_SUPPORT
24 if (session->type == COAP_SESSION_TYPE_CLIENT)
25 session->tls = coap_dtls_new_client_session(session);
26#endif /* COAP_CLIENT_SUPPORT */
27#if COAP_SERVER_SUPPORT
28 if (session->type != COAP_SESSION_TYPE_CLIENT)
29 session->tls = coap_dtls_new_server_session(session);
30#endif /* COAP_SERVER_SUPPORT */
31
32 if (!session->tls) {
33 coap_session_disconnected(session, COAP_NACK_TLS_LAYER_FAILED);
34 return;
35 }
36 coap_ticks(&session->last_rx_tx);
37}
38
39 void
40 coap_dtls_close(coap_session_t *session) {
41 if (session->tls) {
42 coap_dtls_free_session(session);
43 session->tls = NULL;
44 }
45 session->sock.lfunc[COAP_LAYER_TLS].l_close(session);
46}
47
48#if !COAP_DISABLE_TCP
49 void
50 coap_tls_establish(coap_session_t *session) {
51 session->state = COAP_SESSION_STATE_HANDSHAKE;
52#if COAP_CLIENT_SUPPORT
53 if (session->type == COAP_SESSION_TYPE_CLIENT)
54 session->tls = coap_tls_new_client_session(session);
55#endif /* COAP_CLIENT_SUPPORT */
56#if COAP_SERVER_SUPPORT
57 if (session->type != COAP_SESSION_TYPE_CLIENT)
58 session->tls = coap_tls_new_server_session(session);
59#endif /* COAP_SERVER_SUPPORT */
60
61 if (!session->tls) {
62 coap_session_disconnected(session, COAP_NACK_TLS_LAYER_FAILED);
63 return;
64 }
65 coap_ticks(&session->last_rx_tx);
66}
67
68 void
69 coap_tls_close(coap_session_t *session) {
70 if (session->tls) {
71 coap_tls_free_session(session);
72 session->tls = NULL;
73 }
74 session->sock.lfunc[COAP_LAYER_TLS].l_close(session);
75}
76#endif /* !COAP_DISABLE_TCP */
coap_internal.h
Pulls together all the internal only header files.
COAP_NACK_TLS_LAYER_FAILED
@ COAP_NACK_TLS_LAYER_FAILED
Definition: coap_io.h:76
COAP_LAYER_TLS
@ COAP_LAYER_TLS
Definition: coap_layers_internal.h:25
coap_dtls_free_session
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:167
coap_tls_free_session
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:243
coap_ticks
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
coap_tls_new_server_session
void * coap_tls_new_server_session(coap_session_t *coap_session)
Create a TLS new server-side session.
coap_dtls_new_client_session
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
coap_tls_establish
void coap_tls_establish(coap_session_t *session)
Layer function interface for layer below TLS accept/connect being established.
Definition: coap_dtls.c:50
coap_dtls_establish
void coap_dtls_establish(coap_session_t *session)
Layer function interface for layer below DTLS connect being established.
Definition: coap_dtls.c:21
coap_dtls_new_server_session
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
coap_tls_new_client_session
void * coap_tls_new_client_session(coap_session_t *coap_session)
Create a new TLS client-side session.
coap_tls_close
void coap_tls_close(coap_session_t *session)
Layer function interface for TLS close for a session.
Definition: coap_dtls.c:69
coap_dtls_close
void coap_dtls_close(coap_session_t *session)
Layer function interface for DTLS close for a session.
Definition: coap_dtls.c:40
coap_session_disconnected
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:848
COAP_SESSION_TYPE_CLIENT
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition: coap_session.h:45
COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:57
coap_layer_func_t::l_close
coap_layer_close_t l_close
Definition: coap_layers_internal.h:110
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition: coap_session_internal.h:68
coap_session_t::last_rx_tx
coap_tick_t last_rx_tx
Definition: coap_session_internal.h:111
coap_session_t::sock
coap_socket_t sock
socket object for the session, if any
Definition: coap_session_internal.h:81
coap_session_t::state
coap_session_state_t state
current state of relationship with peer
Definition: coap_session_internal.h:71
coap_session_t::tls
void * tls
security parameters
Definition: coap_session_internal.h:87
coap_session_t::type
coap_session_type_t type
client or server side socket
Definition: coap_session_internal.h:70
coap_socket_t::lfunc
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
Definition: coap_io_internal.h:55