Mistake on this page?
Report an issue in GitHub or email us
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 
39 class DTLSSocket : public DTLSSocketWrapper {
40 public:
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  virtual ~DTLSSocket();
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 0 on success, negative error code on failure.
75  */
77  {
78  return _udp_socket.open(stack);
79  }
80 
81  template <typename S>
82  nsapi_error_t open(S *stack)
83  {
84  return open(nsapi_create_stack(stack));
85  }
86 
88 
89  /** Connects TCP socket to a remote host.
90  *
91  * Initiates a connection to a remote server specified by either
92  * a domain name or an IP address and a port.
93  *
94  * @param host Hostname of the remote host.
95  * @param port Port of the remote host.
96  * @return 0 on success, negative error code on failure.
97  */
98  nsapi_error_t connect(const char *host, uint16_t port);
99 
100 private:
101  UDPSocket _udp_socket;
102 };
103 
104 #endif
105 #endif
106 /** @} */
nsapi_error_t open(NetworkStack *stack)
Open a network socket on the network stack of the given network interface.
SocketAddress class.
DTLSSocket implement DTLS stream over UDP Socket.
Definition: DTLSSocket.h:39
NetworkStack * nsapi_create_stack(nsapi_stack_t *stack)
Convert a raw nsapi_stack_t object into a C++ NetworkStack object.
nsapi_error_t connect(const char *host, uint16_t port)
Connects TCP socket to a remote host.
virtual nsapi_error_t open(NetworkStack *stack)
Opens a socket.
Definition: DTLSSocket.h:76
virtual ~DTLSSocket()
Destroy the DTLSSocket and closes the transport.
NetworkStack class.
Definition: NetworkStack.h:40
UDP socket implementation.
Definition: UDPSocket.h:33
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
DTLSSocket()
Create an uninitialized DTLS socket.
Definition: DTLSSocket.h:45
DTLSSocketWrapper.
virtual nsapi_error_t connect(const SocketAddress &address=SocketAddress())
Connects socket to a remote address.
#define MBED_ASSERT(expr)
MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false.
Definition: mbed_assert.h:65
DTLSSocket(S *stack, const char *hostname=NULL)
Create a socket on a network interface.
Definition: DTLSSocket.h:61
DTLSSocketWrapper implement DTLS stream over the existing Socket transport.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.