socket_helper.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #undef _POSIX_C_SOURCE
  15. #define _POSIX_C_SOURCE 200112L
  16. #include <openssl/bio.h>
  17. #include <openssl/err.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #if !defined(OPENSSL_WINDOWS)
  22. #include <netdb.h>
  23. #include <unistd.h>
  24. #else
  25. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. OPENSSL_MSVC_PRAGMA(warning(pop))
  29. #endif
  30. #include "internal.h"
  31. #include "../internal.h"
  32. int bio_ip_and_port_to_socket_and_addr(int *out_sock,
  33. struct sockaddr_storage *out_addr,
  34. socklen_t *out_addr_length,
  35. const char *hostname,
  36. const char *port_str) {
  37. struct addrinfo hint, *result, *cur;
  38. int ret;
  39. *out_sock = -1;
  40. OPENSSL_memset(&hint, 0, sizeof(hint));
  41. hint.ai_family = AF_UNSPEC;
  42. hint.ai_socktype = SOCK_STREAM;
  43. ret = getaddrinfo(hostname, port_str, &hint, &result);
  44. if (ret != 0) {
  45. OPENSSL_PUT_ERROR(SYS, 0);
  46. ERR_add_error_data(1, gai_strerror(ret));
  47. return 0;
  48. }
  49. ret = 0;
  50. for (cur = result; cur; cur = cur->ai_next) {
  51. if ((size_t) cur->ai_addrlen > sizeof(struct sockaddr_storage)) {
  52. continue;
  53. }
  54. OPENSSL_memset(out_addr, 0, sizeof(struct sockaddr_storage));
  55. OPENSSL_memcpy(out_addr, cur->ai_addr, cur->ai_addrlen);
  56. *out_addr_length = cur->ai_addrlen;
  57. *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
  58. if (*out_sock < 0) {
  59. OPENSSL_PUT_SYSTEM_ERROR();
  60. goto out;
  61. }
  62. ret = 1;
  63. break;
  64. }
  65. out:
  66. freeaddrinfo(result);
  67. return ret;
  68. }
  69. int bio_socket_nbio(int sock, int on) {
  70. #if defined(OPENSSL_WINDOWS)
  71. u_long arg = on;
  72. return 0 == ioctlsocket(sock, FIONBIO, &arg);
  73. #else
  74. int flags = fcntl(sock, F_GETFL, 0);
  75. if (flags < 0) {
  76. return 0;
  77. }
  78. if (!on) {
  79. flags &= ~O_NONBLOCK;
  80. } else {
  81. flags |= O_NONBLOCK;
  82. }
  83. return fcntl(sock, F_SETFL, flags) == 0;
  84. #endif
  85. }
  86. void bio_clear_socket_error(void) {}
  87. int bio_sock_error(int sock) {
  88. int error;
  89. socklen_t error_size = sizeof(error);
  90. if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &error_size) < 0) {
  91. return 1;
  92. }
  93. return error;
  94. }