Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #ifndef OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H |
| 16 | #define OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H |
| 17 | |
| 18 | #include <openssl/ssl.h> |
| 19 | #include <string.h> |
| 20 | |
David Benjamin | ee7aa02 | 2017-07-07 16:47:59 -0400 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Brian Smith | 33970e6 | 2015-01-27 22:32:08 -0800 | [diff] [blame] | 23 | // InitSocketLibrary calls the Windows socket init functions, if needed. |
| 24 | bool InitSocketLibrary(); |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 25 | |
| 26 | // Connect sets |*out_sock| to be a socket connected to the destination given |
| 27 | // in |hostname_and_port|, which should be of the form "www.example.com:123". |
| 28 | // It returns true on success and false otherwise. |
| 29 | bool Connect(int *out_sock, const std::string &hostname_and_port); |
| 30 | |
David Benjamin | 2b0444e | 2017-06-27 17:29:27 -0400 | [diff] [blame] | 31 | class Listener { |
| 32 | public: |
| 33 | Listener() {} |
| 34 | ~Listener(); |
| 35 | |
| 36 | // Init initializes the listener to listen on |port|, which should be of the |
| 37 | // form "123". |
| 38 | bool Init(const std::string &port); |
| 39 | |
| 40 | // Accept sets |*out_sock| to be a socket connected to the listener. |
| 41 | bool Accept(int *out_sock); |
| 42 | |
| 43 | private: |
| 44 | int server_sock_ = -1; |
| 45 | |
| 46 | Listener(const Listener &) = delete; |
| 47 | Listener &operator=(const Listener &) = delete; |
| 48 | }; |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 49 | |
David Benjamin | 225e5ad | 2016-07-16 14:51:58 +0200 | [diff] [blame] | 50 | bool VersionFromString(uint16_t *out_version, const std::string &version); |
| 51 | |
Peter Wu | 5663b63 | 2017-09-15 15:09:03 +0100 | [diff] [blame] | 52 | void PrintConnectionInfo(BIO *bio, const SSL *ssl); |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 53 | |
| 54 | bool SocketSetNonBlocking(int sock, bool is_non_blocking); |
| 55 | |
David Benjamin | 3c37d0a | 2018-05-05 00:42:23 -0400 | [diff] [blame] | 56 | // PrintSSLError prints information about the most recent SSL error to stderr. |
| 57 | // |ssl_err| must be the output of |SSL_get_error| and the |SSL| object must be |
| 58 | // connected to socket from |Connect|. |
| 59 | void PrintSSLError(FILE *file, const char *msg, int ssl_err, int ret); |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 60 | |
| 61 | bool TransferData(SSL *ssl, int sock); |
| 62 | |
Adam Langley | 403c52a | 2016-07-07 14:52:02 -0700 | [diff] [blame] | 63 | // DoSMTPStartTLS performs the SMTP STARTTLS mini-protocol over |sock|. It |
| 64 | // returns true on success and false otherwise. |
| 65 | bool DoSMTPStartTLS(int sock); |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 66 | |
David Benjamin | ee7aa02 | 2017-07-07 16:47:59 -0400 | [diff] [blame] | 67 | // DoHTTPTunnel sends an HTTP CONNECT request over |sock|. It returns true on |
| 68 | // success and false otherwise. |
| 69 | bool DoHTTPTunnel(int sock, const std::string &hostname_and_port); |
| 70 | |
David Benjamin | e2daba6 | 2017-08-17 20:12:08 -0400 | [diff] [blame] | 71 | #endif // !OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H |