1
+ #ifndef DTLSSOCKET_H
2
+ #define DTLSSOCKET_H
3
+
4
+ #include " DTLSSocketWrapper.h"
5
+ #include " SocketAddress.h"
6
+ #include " UDPSocket.h"
7
+
8
+ // This class requires Mbed TLS SSL/TLS client code
9
+ #if defined(MBEDTLS_SSL_CLI_C)
10
+
11
+ class DTLSSocket : public DTLSSocketWrapper {
12
+ public:
13
+ /* * Create an uninitialized DTLS socket
14
+ *
15
+ * Must call open to initialize the socket on a network stack.
16
+ */
17
+ DTLSSocket () : DTLSSocketWrapper(&_udp_socket) {}
18
+
19
+ /* * Destroy the DTLSSocket and closes the transport.
20
+ */
21
+ virtual ~DTLSSocket ();
22
+
23
+ /* * Create a socket on a network interface
24
+ *
25
+ * Creates and opens a socket on the network stack of the given
26
+ * network interface.
27
+ * If hostname is also given, user is not required to call set_hostname() later.
28
+ *
29
+ * @param stack Network stack as target for socket
30
+ * @param hostname Hostname used for certificate verification
31
+ */
32
+ template <typename S>
33
+ DTLSSocket (S *stack, const char *hostname = NULL ) : DTLSSocketWrapper(&_udp_socket, hostname)
34
+ {
35
+ nsapi_error_t ret = _udp_socket.open (stack);
36
+ MBED_ASSERT (ret == NSAPI_ERROR_OK);
37
+ }
38
+
39
+ /* * Opens a socket
40
+ *
41
+ * Creates a network socket on the network stack of the given
42
+ * network interface. Not needed if stack is passed to the
43
+ * socket's constructor.
44
+ *
45
+ * @param stack Network stack as target for socket
46
+ * @return 0 on success, negative error code on failure
47
+ */
48
+ virtual nsapi_error_t open (NetworkStack *stack) {
49
+ _stack = stack;
50
+ return _udp_socket.open (stack);
51
+ }
52
+
53
+ template <typename S>
54
+ nsapi_error_t open (S *stack) {
55
+ return open (nsapi_create_stack (stack));
56
+ }
57
+
58
+ using DTLSSocketWrapper::connect;
59
+
60
+ /* * Connects TCP socket to a remote host
61
+ *
62
+ * Initiates a connection to a remote server specified by either
63
+ * a domain name or an IP address and a port.
64
+ *
65
+ * @param host Hostname of the remote host
66
+ * @param port Port of the remote host
67
+ * @return 0 on success, negative error code on failure
68
+ */
69
+ nsapi_error_t connect (const char *host, uint16_t port);
70
+
71
+ private:
72
+ UDPSocket _udp_socket;
73
+ NetworkStack *_stack;
74
+ SocketAddress _remote_address;
75
+ };
76
+
77
+ #endif
78
+ #endif
0 commit comments