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 
21 #include <string>
22 
23 // InitSocketLibrary calls the Windows socket init functions, if needed.
24 bool InitSocketLibrary();
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 
31 class Listener {
32  public:
Listener()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 };
49 
50 bool VersionFromString(uint16_t *out_version, const std::string &version);
51 
52 void PrintConnectionInfo(BIO *bio, const SSL *ssl);
53 
54 bool SocketSetNonBlocking(int sock, bool is_non_blocking);
55 
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);
60 
61 bool TransferData(SSL *ssl, int sock);
62 
63 // DoSMTPStartTLS performs the SMTP STARTTLS mini-protocol over |sock|. It
64 // returns true on success and false otherwise.
65 bool DoSMTPStartTLS(int sock);
66 
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 
71 #endif  // !OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H
72