Mistake on this page?
Report an issue in GitHub or email us
TLSSocket.h
Go to the documentation of this file.
1 /** @file TLSSocket.h TLSSocket */
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 _MBED_HTTPS_TLS_TCP_SOCKET_H_
23 #define _MBED_HTTPS_TLS_TCP_SOCKET_H_
24 
25 #include "netsocket/TCPSocket.h"
26 #include "TLSSocketWrapper.h"
27 
28 #include "mbedtls/platform.h"
29 #include "mbedtls/ssl.h"
30 #include "mbedtls/entropy.h"
31 #include "mbedtls/ctr_drbg.h"
32 #include "mbedtls/error.h"
33 
34 // This class requires Mbed TLS SSL/TLS client code
35 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
36 
37 /**
38  * \brief TLSSocket is a wrapper around TCPSocket for interacting with TLS servers.
39  *
40  * TLSSocket uses the TLSSocketWrapper with internal TCP socket.
41  * This is a helper for creating commonly used TLS connections over TCP.
42  *
43  */
44 class TLSSocket : public TLSSocketWrapper {
45 public:
46  /** Create an uninitialized socket.
47  *
48  * Must call open to initialize the socket on a network stack.
49  */
50  TLSSocket() : TLSSocketWrapper(&tcp_socket) {}
51 
52  /** Destroy the TLSSocket and closes the transport.
53  */
54  virtual ~TLSSocket();
55 
56  /** Opens a socket.
57  *
58  * Creates a network socket on the network stack of the given
59  * network interface.
60  *
61  * @note TLSSocket cannot be reopened after closing. It should be destructed to
62  * clear internal TLS memory structures.
63  *
64  * @param stack Network stack as target for socket.
65  * @return 0 on success, negative error code on failure.
66  */
68  {
69  return tcp_socket.open(stack);
70  }
71 
72  template <typename S>
73  nsapi_error_t open(S *stack)
74  {
75  return open(nsapi_create_stack(stack));
76  }
77 
79 
80  /** Connects TCP socket to a remote host.
81  *
82  * Initiates a connection to a remote server specified by either
83  * a domain name or an IP address and port.
84  *
85  * @note: In case connect() returns NSAPI_ERROR_AUTH_FAILURE,
86  * the socket must be freed either by calling close() or destroying it.
87  *
88  * @param host Hostname of the remote host.
89  * @param port Port of the remote host.
90  * @return 0 on success, negative error code on failure.
91  */
92  nsapi_error_t connect(const char *host, uint16_t port);
93 
94 private:
95  TCPSocket tcp_socket;
96 };
97 
98 #endif // MBEDTLS_SSL_CLI_C
99 #endif // _MBED_HTTPS_TLS_TCP_SOCKET_H_
100 
101 /** @} */
nsapi_error_t open(NetworkStack *stack)
Open a network socket on the network stack of the given network interface.
NetworkStack * nsapi_create_stack(nsapi_stack_t *stack)
Convert a raw nsapi_stack_t object into a C++ NetworkStack object.
NetworkStack class.
Definition: NetworkStack.h:40
TLSSocket is a wrapper around Socket for interacting with TLS servers.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
virtual nsapi_error_t connect(const SocketAddress &address=SocketAddress())
Connects socket to a remote address.
virtual ~TLSSocket()
Destroy the TLSSocket and closes the transport.
virtual nsapi_error_t open(NetworkStack *stack)
Opens a socket.
Definition: TLSSocket.h:67
TLSSocket is a wrapper around TCPSocket for interacting with TLS servers.
Definition: TLSSocket.h:44
TCP socket connection.
Definition: TCPSocket.h:32
TLSSocket()
Create an uninitialized socket.
Definition: TLSSocket.h:50
TLSSocketWrapper.
TCPSocket class.
nsapi_error_t connect(const char *host, uint16_t port)
Connects TCP socket to a remote host.
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.