Mbed OS Reference
Loading...
Searching...
No Matches
DTLSSocket.h
Go to the documentation of this file.
1/** @file DTLSSocket.h DTLSSocket */
2/*
3 * Copyright (c) 2018 ARM Limited
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/** @addtogroup NetSocket
19* @{
20*/
21
22#ifndef DTLSSOCKET_H
23#define DTLSSOCKET_H
24
25#include "DTLSSocketWrapper.h"
26#include "SocketAddress.h"
27#include "UDPSocket.h"
28
29// This class requires Mbed TLS SSL/TLS client code
30#if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
31
32/**
33 * \brief DTLSSocket implement DTLS stream over UDP Socket.
34 *
35 * This is a helper class that uses DTLSSocketWrapper with
36 * internal UDPSocket.
37 */
38
40public:
41 /** Create an uninitialized DTLS socket.
42 *
43 * Must call open to initialize the socket on a network stack.
44 */
45 DTLSSocket() : DTLSSocketWrapper(&_udp_socket) {}
46
47 /** Destroy the DTLSSocket and closes the transport.
48 */
49 ~DTLSSocket() override;
50
51 /** Create a socket on a network interface.
52 *
53 * Creates and opens a socket on the network stack of the given
54 * network interface.
55 * If hostname is also given, user is not required to call set_hostname() later.
56 *
57 * @param stack Network stack as target for socket.
58 * @param hostname Hostname used for certificate verification.
59 */
60 template <typename S>
61 DTLSSocket(S *stack, const char *hostname = NULL) : DTLSSocketWrapper(&_udp_socket, hostname)
62 {
63 nsapi_error_t ret = _udp_socket.open(stack);
65 }
66
67 /** Opens a socket.
68 *
69 * Creates a network socket on the network stack of the given
70 * network interface. Not needed if stack is passed to the
71 * socket's constructor.
72 *
73 * @param stack Network stack as target for socket.
74 * @return NSAPI_ERROR_OK on success, negative error code on failure.
75 * See @ref UDPSocket::open.
76 */
78 {
79 return _udp_socket.open(stack);
80 }
81
82 template <typename S>
83 nsapi_error_t open(S *stack)
84 {
85 return open(nsapi_create_stack(stack));
86 }
87
89
90private:
91 UDPSocket _udp_socket;
92};
93
94#endif
95#endif
96/** @} */
DTLSSocketWrapper.
SocketAddress class.
DTLSSocket implement DTLS stream over UDP Socket.
Definition: DTLSSocket.h:39
~DTLSSocket() override
Destroy the DTLSSocket and closes the transport.
nsapi_error_t open(NetworkStack *stack)
Opens a socket.
Definition: DTLSSocket.h:77
DTLSSocket(S *stack, const char *hostname=NULL)
Create a socket on a network interface.
Definition: DTLSSocket.h:61
DTLSSocket()
Create an uninitialized DTLS socket.
Definition: DTLSSocket.h:45
DTLSSocketWrapper implement DTLS stream over the existing Socket transport.
nsapi_error_t open(NetworkStack *stack)
Open a network socket on the network stack of the given network interface.
NetworkStack class.
Definition: NetworkStack.h:42
virtual nsapi_error_t connect(const SocketAddress &address)=0
Connects socket to a remote address.
UDP socket implementation.
Definition: UDPSocket.h:34
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
NetworkStack * nsapi_create_stack(nsapi_stack_t *stack)
Convert a raw nsapi_stack_t object into a C++ NetworkStack object.
@ NSAPI_ERROR_OK
Definition: nsapi_types.h:85
#define MBED_ASSERT(expr)
MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false.
Definition: mbed_assert.h:66