..
TLSSocket.h@0:5f745af3ec9b, 2019-08-23 (annotated)
- Committer:
- ImranBilalButt
- Date:
- Fri Aug 23 13:29:35 2019 +0000
- Revision:
- 0:5f745af3ec9b
..
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ImranBilalButt | 0:5f745af3ec9b | 1 | /* |
ImranBilalButt | 0:5f745af3ec9b | 2 | * PackageLicenseDeclared: Apache-2.0 |
ImranBilalButt | 0:5f745af3ec9b | 3 | * Copyright (c) 2017 ARM Limited |
ImranBilalButt | 0:5f745af3ec9b | 4 | * |
ImranBilalButt | 0:5f745af3ec9b | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ImranBilalButt | 0:5f745af3ec9b | 6 | * you may not use this file except in compliance with the License. |
ImranBilalButt | 0:5f745af3ec9b | 7 | * You may obtain a copy of the License at |
ImranBilalButt | 0:5f745af3ec9b | 8 | * |
ImranBilalButt | 0:5f745af3ec9b | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
ImranBilalButt | 0:5f745af3ec9b | 10 | * |
ImranBilalButt | 0:5f745af3ec9b | 11 | * Unless required by applicable law or agreed to in writing, software |
ImranBilalButt | 0:5f745af3ec9b | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
ImranBilalButt | 0:5f745af3ec9b | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ImranBilalButt | 0:5f745af3ec9b | 14 | * See the License for the specific language governing permissions and |
ImranBilalButt | 0:5f745af3ec9b | 15 | * limitations under the License. |
ImranBilalButt | 0:5f745af3ec9b | 16 | */ |
ImranBilalButt | 0:5f745af3ec9b | 17 | |
ImranBilalButt | 0:5f745af3ec9b | 18 | #ifndef _MBED_HTTPS_TLS_TCP_SOCKET_H_ |
ImranBilalButt | 0:5f745af3ec9b | 19 | #define _MBED_HTTPS_TLS_TCP_SOCKET_H_ |
ImranBilalButt | 0:5f745af3ec9b | 20 | |
ImranBilalButt | 0:5f745af3ec9b | 21 | //#include "netsocket/TCPSocket.h" |
ImranBilalButt | 0:5f745af3ec9b | 22 | #include "netsocket/UDPSocket.h" |
ImranBilalButt | 0:5f745af3ec9b | 23 | #include "TLSSocketWrapper.h" |
ImranBilalButt | 0:5f745af3ec9b | 24 | |
ImranBilalButt | 0:5f745af3ec9b | 25 | //#include "mbedtls/platform.h" |
ImranBilalButt | 0:5f745af3ec9b | 26 | //#include "mbedtls/ssl.h" |
ImranBilalButt | 0:5f745af3ec9b | 27 | //#include "mbedtls/entropy.h" |
ImranBilalButt | 0:5f745af3ec9b | 28 | //#include "mbedtls/ctr_drbg.h" |
ImranBilalButt | 0:5f745af3ec9b | 29 | //#include "mbedtls/error.h" |
ImranBilalButt | 0:5f745af3ec9b | 30 | |
ImranBilalButt | 0:5f745af3ec9b | 31 | /** |
ImranBilalButt | 0:5f745af3ec9b | 32 | * \brief TLSSocket a wrapper around TCPSocket for interacting with TLS servers |
ImranBilalButt | 0:5f745af3ec9b | 33 | */ |
ImranBilalButt | 0:5f745af3ec9b | 34 | class TLSSocket : public TLSSocketWrapper { |
ImranBilalButt | 0:5f745af3ec9b | 35 | public: |
ImranBilalButt | 0:5f745af3ec9b | 36 | |
ImranBilalButt | 0:5f745af3ec9b | 37 | /** Create an uninitialized socket |
ImranBilalButt | 0:5f745af3ec9b | 38 | * |
ImranBilalButt | 0:5f745af3ec9b | 39 | * Must call open to initialize the socket on a network stack. |
ImranBilalButt | 0:5f745af3ec9b | 40 | */ |
ImranBilalButt | 0:5f745af3ec9b | 41 | TLSSocket() : TLSSocketWrapper(&udp_socket) { |
ImranBilalButt | 0:5f745af3ec9b | 42 | |
ImranBilalButt | 0:5f745af3ec9b | 43 | } |
ImranBilalButt | 0:5f745af3ec9b | 44 | |
ImranBilalButt | 0:5f745af3ec9b | 45 | /** Create a socket on a network interface |
ImranBilalButt | 0:5f745af3ec9b | 46 | * |
ImranBilalButt | 0:5f745af3ec9b | 47 | * Creates and opens a socket on the network stack of the given |
ImranBilalButt | 0:5f745af3ec9b | 48 | * network interface. |
ImranBilalButt | 0:5f745af3ec9b | 49 | * |
ImranBilalButt | 0:5f745af3ec9b | 50 | * @param stack Network stack as target for socket |
ImranBilalButt | 0:5f745af3ec9b | 51 | */ |
ImranBilalButt | 0:5f745af3ec9b | 52 | template <typename S> |
ImranBilalButt | 0:5f745af3ec9b | 53 | TLSSocket(S *stack, const char *hostname = NULL) : TLSSocketWrapper(&udp_socket, hostname) |
ImranBilalButt | 0:5f745af3ec9b | 54 | { |
ImranBilalButt | 0:5f745af3ec9b | 55 | nsapi_error_t ret; |
ImranBilalButt | 0:5f745af3ec9b | 56 | if((ret = udp_socket.open(stack)) < 0) |
ImranBilalButt | 0:5f745af3ec9b | 57 | printf("[.] UDP Socket Opening Failure\n"); |
ImranBilalButt | 0:5f745af3ec9b | 58 | else { |
ImranBilalButt | 0:5f745af3ec9b | 59 | MBED_ASSERT(ret == NSAPI_ERROR_OK); |
ImranBilalButt | 0:5f745af3ec9b | 60 | printf("[.] UDP Socket Opening Successful\n"); |
ImranBilalButt | 0:5f745af3ec9b | 61 | } |
ImranBilalButt | 0:5f745af3ec9b | 62 | } |
ImranBilalButt | 0:5f745af3ec9b | 63 | |
ImranBilalButt | 0:5f745af3ec9b | 64 | /** Opens a socket |
ImranBilalButt | 0:5f745af3ec9b | 65 | * |
ImranBilalButt | 0:5f745af3ec9b | 66 | * Creates a network socket on the network stack of the given |
ImranBilalButt | 0:5f745af3ec9b | 67 | * network interface. Not needed if stack is passed to the |
ImranBilalButt | 0:5f745af3ec9b | 68 | * socket's constructor. |
ImranBilalButt | 0:5f745af3ec9b | 69 | * |
ImranBilalButt | 0:5f745af3ec9b | 70 | * @param stack Network stack as target for socket |
ImranBilalButt | 0:5f745af3ec9b | 71 | * @return 0 on success, negative error code on failure |
ImranBilalButt | 0:5f745af3ec9b | 72 | */ |
ImranBilalButt | 0:5f745af3ec9b | 73 | virtual nsapi_error_t open(NetworkStack *stack) { |
ImranBilalButt | 0:5f745af3ec9b | 74 | return udp_socket.open(stack); |
ImranBilalButt | 0:5f745af3ec9b | 75 | } |
ImranBilalButt | 0:5f745af3ec9b | 76 | |
ImranBilalButt | 0:5f745af3ec9b | 77 | template <typename S> |
ImranBilalButt | 0:5f745af3ec9b | 78 | nsapi_error_t open(S *stack) { |
ImranBilalButt | 0:5f745af3ec9b | 79 | return open(nsapi_create_stack(stack)); |
ImranBilalButt | 0:5f745af3ec9b | 80 | } |
ImranBilalButt | 0:5f745af3ec9b | 81 | |
ImranBilalButt | 0:5f745af3ec9b | 82 | using TLSSocketWrapper::connect; |
ImranBilalButt | 0:5f745af3ec9b | 83 | |
ImranBilalButt | 0:5f745af3ec9b | 84 | /** Connects TCP socket to a remote host |
ImranBilalButt | 0:5f745af3ec9b | 85 | * |
ImranBilalButt | 0:5f745af3ec9b | 86 | * Initiates a connection to a remote server specified by either |
ImranBilalButt | 0:5f745af3ec9b | 87 | * a domain name or an IP address and a port. |
ImranBilalButt | 0:5f745af3ec9b | 88 | * |
ImranBilalButt | 0:5f745af3ec9b | 89 | * @param host Hostname of the remote host |
ImranBilalButt | 0:5f745af3ec9b | 90 | * @param port Port of the remote host |
ImranBilalButt | 0:5f745af3ec9b | 91 | * @return 0 on success, negative error code on failure |
ImranBilalButt | 0:5f745af3ec9b | 92 | */ |
ImranBilalButt | 0:5f745af3ec9b | 93 | nsapi_error_t connect(const char *host, uint16_t port); |
ImranBilalButt | 0:5f745af3ec9b | 94 | //SocketAddress getsock(); |
ImranBilalButt | 0:5f745af3ec9b | 95 | SocketAddress sock_addr; |
ImranBilalButt | 0:5f745af3ec9b | 96 | private: |
ImranBilalButt | 0:5f745af3ec9b | 97 | UDPSocket udp_socket; |
ImranBilalButt | 0:5f745af3ec9b | 98 | }; |
ImranBilalButt | 0:5f745af3ec9b | 99 | |
ImranBilalButt | 0:5f745af3ec9b | 100 | #endif // _MBED_HTTPS_TLS_TCP_SOCKET_H_ |