blob: 7d45d1c7070ff6ab5989c053e1b9216413a6aa38 [file] [log] [blame]
Dave Tapuskab8a824d2014-12-10 19:09:52 -05001/* 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 Benjaminee7aa022017-07-07 16:47:59 -040021#include <string>
22
Brian Smith33970e62015-01-27 22:32:08 -080023// InitSocketLibrary calls the Windows socket init functions, if needed.
24bool InitSocketLibrary();
Dave Tapuskab8a824d2014-12-10 19:09:52 -050025
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.
29bool Connect(int *out_sock, const std::string &hostname_and_port);
30
David Benjamin2b0444e2017-06-27 17:29:27 -040031class 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 Tapuskab8a824d2014-12-10 19:09:52 -050049
David Benjamin225e5ad2016-07-16 14:51:58 +020050bool VersionFromString(uint16_t *out_version, const std::string &version);
51
Peter Wu5663b632017-09-15 15:09:03 +010052void PrintConnectionInfo(BIO *bio, const SSL *ssl);
Dave Tapuskab8a824d2014-12-10 19:09:52 -050053
54bool SocketSetNonBlocking(int sock, bool is_non_blocking);
55
David Benjamin3c37d0a2018-05-05 00:42:23 -040056// 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|.
59void PrintSSLError(FILE *file, const char *msg, int ssl_err, int ret);
Dave Tapuskab8a824d2014-12-10 19:09:52 -050060
61bool TransferData(SSL *ssl, int sock);
62
Adam Langley403c52a2016-07-07 14:52:02 -070063// DoSMTPStartTLS performs the SMTP STARTTLS mini-protocol over |sock|. It
64// returns true on success and false otherwise.
65bool DoSMTPStartTLS(int sock);
Dave Tapuskab8a824d2014-12-10 19:09:52 -050066
David Benjaminee7aa022017-07-07 16:47:59 -040067// DoHTTPTunnel sends an HTTP CONNECT request over |sock|. It returns true on
68// success and false otherwise.
69bool DoHTTPTunnel(int sock, const std::string &hostname_and_port);
70
David Benjamine2daba62017-08-17 20:12:08 -040071#endif // !OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H