25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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