25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

277 lines
7.0 KiB

  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. #include <openssl/base.h>
  15. // TODO(davidben): bssl client does not work on Windows.
  16. #if !defined(OPENSSL_WINDOWS)
  17. #include <string>
  18. #include <vector>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #if !defined(OPENSSL_WINDOWS)
  25. #include <arpa/inet.h>
  26. #include <fcntl.h>
  27. #include <netdb.h>
  28. #include <netinet/in.h>
  29. #include <sys/select.h>
  30. #include <unistd.h>
  31. #else
  32. #include <WinSock2.h>
  33. #include <WS2tcpip.h>
  34. typedef int socklen_t;
  35. #endif
  36. #include <openssl/err.h>
  37. #include <openssl/ssl.h>
  38. #include "internal.h"
  39. // Connect sets |*out_sock| to be a socket connected to the destination given
  40. // in |hostname_and_port|, which should be of the form "www.example.com:123".
  41. // It returns true on success and false otherwise.
  42. bool Connect(int *out_sock, const std::string &hostname_and_port) {
  43. const size_t colon_offset = hostname_and_port.find_last_of(':');
  44. std::string hostname, port;
  45. if (colon_offset == std::string::npos) {
  46. hostname = hostname_and_port;
  47. port = "443";
  48. } else {
  49. hostname = hostname_and_port.substr(0, colon_offset);
  50. port = hostname_and_port.substr(colon_offset + 1);
  51. }
  52. struct addrinfo hint, *result;
  53. memset(&hint, 0, sizeof(hint));
  54. hint.ai_family = AF_UNSPEC;
  55. hint.ai_socktype = SOCK_STREAM;
  56. int ret = getaddrinfo(hostname.c_str(), port.c_str(), &hint, &result);
  57. if (ret != 0) {
  58. fprintf(stderr, "getaddrinfo returned: %s\n", gai_strerror(ret));
  59. return false;
  60. }
  61. bool ok = false;
  62. char buf[256];
  63. *out_sock =
  64. socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  65. if (*out_sock < 0) {
  66. perror("socket");
  67. goto out;
  68. }
  69. switch (result->ai_family) {
  70. case AF_INET: {
  71. struct sockaddr_in *sin =
  72. reinterpret_cast<struct sockaddr_in *>(result->ai_addr);
  73. fprintf(stderr, "Connecting to %s:%d\n",
  74. inet_ntop(result->ai_family, &sin->sin_addr, buf, sizeof(buf)),
  75. ntohs(sin->sin_port));
  76. break;
  77. }
  78. case AF_INET6: {
  79. struct sockaddr_in6 *sin6 =
  80. reinterpret_cast<struct sockaddr_in6 *>(result->ai_addr);
  81. fprintf(stderr, "Connecting to [%s]:%d\n",
  82. inet_ntop(result->ai_family, &sin6->sin6_addr, buf, sizeof(buf)),
  83. ntohs(sin6->sin6_port));
  84. break;
  85. }
  86. }
  87. if (connect(*out_sock, result->ai_addr, result->ai_addrlen) != 0) {
  88. perror("connect");
  89. goto out;
  90. }
  91. ok = true;
  92. out:
  93. freeaddrinfo(result);
  94. return ok;
  95. }
  96. bool Accept(int *out_sock, const std::string &port) {
  97. struct sockaddr_in addr, cli_addr;
  98. socklen_t cli_addr_len = sizeof(cli_addr);
  99. memset(&addr, 0, sizeof(addr));
  100. addr.sin_family = AF_INET;
  101. addr.sin_addr.s_addr = INADDR_ANY;
  102. addr.sin_port = htons(atoi(port.c_str()));
  103. bool ok = false;
  104. int server_sock = -1;
  105. server_sock =
  106. socket(addr.sin_family, SOCK_STREAM, 0);
  107. if (server_sock < 0) {
  108. perror("socket");
  109. goto out;
  110. }
  111. if (bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
  112. perror("connect");
  113. goto out;
  114. }
  115. listen(server_sock, 1);
  116. *out_sock = accept(server_sock, (struct sockaddr*)&cli_addr, &cli_addr_len);
  117. ok = true;
  118. out:
  119. close(server_sock);
  120. return ok;
  121. }
  122. void PrintConnectionInfo(const SSL *ssl) {
  123. const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
  124. fprintf(stderr, " Version: %s\n", SSL_get_version(ssl));
  125. fprintf(stderr, " Cipher: %s\n", SSL_CIPHER_get_name(cipher));
  126. fprintf(stderr, " Secure renegotiation: %s\n",
  127. SSL_get_secure_renegotiation_support(ssl) ? "yes" : "no");
  128. }
  129. bool SocketSetNonBlocking(int sock, bool is_non_blocking) {
  130. bool ok;
  131. #if defined(OPENSSL_WINDOWS)
  132. u_long arg = is_non_blocking;
  133. ok = 0 == ioctlsocket(sock, FIOBIO, &arg);
  134. #else
  135. int flags = fcntl(sock, F_GETFL, 0);
  136. if (flags < 0) {
  137. return false;
  138. }
  139. if (is_non_blocking) {
  140. flags |= O_NONBLOCK;
  141. } else {
  142. flags &= ~O_NONBLOCK;
  143. }
  144. ok = 0 == fcntl(sock, F_SETFL, flags);
  145. #endif
  146. if (!ok) {
  147. fprintf(stderr, "Failed to set socket non-blocking.\n");
  148. }
  149. return ok;
  150. }
  151. // PrintErrorCallback is a callback function from OpenSSL's
  152. // |ERR_print_errors_cb| that writes errors to a given |FILE*|.
  153. int PrintErrorCallback(const char *str, size_t len, void *ctx) {
  154. fwrite(str, len, 1, reinterpret_cast<FILE*>(ctx));
  155. return 1;
  156. }
  157. bool TransferData(SSL *ssl, int sock) {
  158. bool stdin_open = true;
  159. fd_set read_fds;
  160. FD_ZERO(&read_fds);
  161. if (!SocketSetNonBlocking(sock, true)) {
  162. return false;
  163. }
  164. for (;;) {
  165. if (stdin_open) {
  166. FD_SET(0, &read_fds);
  167. }
  168. FD_SET(sock, &read_fds);
  169. int ret = select(sock + 1, &read_fds, NULL, NULL, NULL);
  170. if (ret <= 0) {
  171. perror("select");
  172. return false;
  173. }
  174. if (FD_ISSET(0, &read_fds)) {
  175. uint8_t buffer[512];
  176. ssize_t n;
  177. do {
  178. n = read(0, buffer, sizeof(buffer));
  179. } while (n == -1 && errno == EINTR);
  180. if (n == 0) {
  181. FD_CLR(0, &read_fds);
  182. stdin_open = false;
  183. shutdown(sock, SHUT_WR);
  184. continue;
  185. } else if (n < 0) {
  186. perror("read from stdin");
  187. return false;
  188. }
  189. if (!SocketSetNonBlocking(sock, false)) {
  190. return false;
  191. }
  192. int ssl_ret = SSL_write(ssl, buffer, n);
  193. if (!SocketSetNonBlocking(sock, true)) {
  194. return false;
  195. }
  196. if (ssl_ret <= 0) {
  197. int ssl_err = SSL_get_error(ssl, ssl_ret);
  198. fprintf(stderr, "Error while writing: %d\n", ssl_err);
  199. ERR_print_errors_cb(PrintErrorCallback, stderr);
  200. return false;
  201. } else if (ssl_ret != n) {
  202. fprintf(stderr, "Short write from SSL_write.\n");
  203. return false;
  204. }
  205. }
  206. if (FD_ISSET(sock, &read_fds)) {
  207. uint8_t buffer[512];
  208. int ssl_ret = SSL_read(ssl, buffer, sizeof(buffer));
  209. if (ssl_ret < 0) {
  210. int ssl_err = SSL_get_error(ssl, ssl_ret);
  211. if (ssl_err == SSL_ERROR_WANT_READ) {
  212. continue;
  213. }
  214. fprintf(stderr, "Error while reading: %d\n", ssl_err);
  215. ERR_print_errors_cb(PrintErrorCallback, stderr);
  216. return false;
  217. } else if (ssl_ret == 0) {
  218. return true;
  219. }
  220. ssize_t n;
  221. do {
  222. n = write(1, buffer, ssl_ret);
  223. } while (n == -1 && errno == EINTR);
  224. if (n != ssl_ret) {
  225. fprintf(stderr, "Short write to stderr.\n");
  226. return false;
  227. }
  228. }
  229. }
  230. }
  231. #endif // !OPENSSL_WINDOWS