1 // Copyright 2014 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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