transport_common.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H
  15. #define OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H
  16. #include <openssl/ssl.h>
  17. #include <string.h>
  18. #include <string>
  19. // InitSocketLibrary calls the Windows socket init functions, if needed.
  20. bool InitSocketLibrary();
  21. // Connect sets |*out_sock| to be a socket connected to the destination given
  22. // in |hostname_and_port|, which should be of the form "www.example.com:123".
  23. // It returns true on success and false otherwise.
  24. bool Connect(int *out_sock, const std::string &hostname_and_port);
  25. class Listener {
  26. public:
  27. Listener() {}
  28. ~Listener();
  29. // Init initializes the listener to listen on |port|, which should be of the
  30. // form "123".
  31. bool Init(const std::string &port);
  32. // Accept sets |*out_sock| to be a socket connected to the listener.
  33. bool Accept(int *out_sock);
  34. private:
  35. int server_sock_ = -1;
  36. Listener(const Listener &) = delete;
  37. Listener &operator=(const Listener &) = delete;
  38. };
  39. bool VersionFromString(uint16_t *out_version, const std::string &version);
  40. void PrintConnectionInfo(BIO *bio, const SSL *ssl);
  41. bool SocketSetNonBlocking(int sock, bool is_non_blocking);
  42. // PrintSSLError prints information about the most recent SSL error to stderr.
  43. // |ssl_err| must be the output of |SSL_get_error| and the |SSL| object must be
  44. // connected to socket from |Connect|.
  45. void PrintSSLError(FILE *file, const char *msg, int ssl_err, int ret);
  46. bool TransferData(SSL *ssl, int sock);
  47. // DoSMTPStartTLS performs the SMTP STARTTLS mini-protocol over |sock|. It
  48. // returns true on success and false otherwise.
  49. bool DoSMTPStartTLS(int sock);
  50. // DoHTTPTunnel sends an HTTP CONNECT request over |sock|. It returns true on
  51. // success and false otherwise.
  52. bool DoHTTPTunnel(int sock, const std::string &hostname_and_port);
  53. #endif // !OPENSSL_HEADER_TOOL_TRANSPORT_COMMON_H