bss_sock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include "bio_local.h"
  12. #include "internal/cryptlib.h"
  13. #ifndef OPENSSL_NO_SOCK
  14. # include <openssl/bio.h>
  15. # ifdef WATT32
  16. /* Watt-32 uses same names */
  17. # undef sock_write
  18. # undef sock_read
  19. # undef sock_puts
  20. # define sock_write SockWrite
  21. # define sock_read SockRead
  22. # define sock_puts SockPuts
  23. # endif
  24. static int sock_write(BIO *h, const char *buf, int num);
  25. static int sock_read(BIO *h, char *buf, int size);
  26. static int sock_puts(BIO *h, const char *str);
  27. static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  28. static int sock_new(BIO *h);
  29. static int sock_free(BIO *data);
  30. int BIO_sock_should_retry(int s);
  31. static const BIO_METHOD methods_sockp = {
  32. BIO_TYPE_SOCKET,
  33. "socket",
  34. /* TODO: Convert to new style write function */
  35. bwrite_conv,
  36. sock_write,
  37. /* TODO: Convert to new style read function */
  38. bread_conv,
  39. sock_read,
  40. sock_puts,
  41. NULL, /* sock_gets, */
  42. sock_ctrl,
  43. sock_new,
  44. sock_free,
  45. NULL, /* sock_callback_ctrl */
  46. };
  47. const BIO_METHOD *BIO_s_socket(void)
  48. {
  49. return &methods_sockp;
  50. }
  51. BIO *BIO_new_socket(int fd, int close_flag)
  52. {
  53. BIO *ret;
  54. ret = BIO_new(BIO_s_socket());
  55. if (ret == NULL)
  56. return NULL;
  57. BIO_set_fd(ret, fd, close_flag);
  58. return ret;
  59. }
  60. static int sock_new(BIO *bi)
  61. {
  62. bi->init = 0;
  63. bi->num = 0;
  64. bi->ptr = NULL;
  65. bi->flags = 0;
  66. return 1;
  67. }
  68. static int sock_free(BIO *a)
  69. {
  70. if (a == NULL)
  71. return 0;
  72. if (a->shutdown) {
  73. if (a->init) {
  74. BIO_closesocket(a->num);
  75. }
  76. a->init = 0;
  77. a->flags = 0;
  78. }
  79. return 1;
  80. }
  81. static int sock_read(BIO *b, char *out, int outl)
  82. {
  83. int ret = 0;
  84. if (out != NULL) {
  85. clear_socket_error();
  86. ret = readsocket(b->num, out, outl);
  87. BIO_clear_retry_flags(b);
  88. if (ret <= 0) {
  89. if (BIO_sock_should_retry(ret))
  90. BIO_set_retry_read(b);
  91. else if (ret == 0)
  92. b->flags |= BIO_FLAGS_IN_EOF;
  93. }
  94. }
  95. return ret;
  96. }
  97. static int sock_write(BIO *b, const char *in, int inl)
  98. {
  99. int ret;
  100. clear_socket_error();
  101. ret = writesocket(b->num, in, inl);
  102. BIO_clear_retry_flags(b);
  103. if (ret <= 0) {
  104. if (BIO_sock_should_retry(ret))
  105. BIO_set_retry_write(b);
  106. }
  107. return ret;
  108. }
  109. static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
  110. {
  111. long ret = 1;
  112. int *ip;
  113. switch (cmd) {
  114. case BIO_C_SET_FD:
  115. sock_free(b);
  116. b->num = *((int *)ptr);
  117. b->shutdown = (int)num;
  118. b->init = 1;
  119. break;
  120. case BIO_C_GET_FD:
  121. if (b->init) {
  122. ip = (int *)ptr;
  123. if (ip != NULL)
  124. *ip = b->num;
  125. ret = b->num;
  126. } else
  127. ret = -1;
  128. break;
  129. case BIO_CTRL_GET_CLOSE:
  130. ret = b->shutdown;
  131. break;
  132. case BIO_CTRL_SET_CLOSE:
  133. b->shutdown = (int)num;
  134. break;
  135. case BIO_CTRL_DUP:
  136. case BIO_CTRL_FLUSH:
  137. ret = 1;
  138. break;
  139. case BIO_CTRL_EOF:
  140. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
  141. break;
  142. default:
  143. ret = 0;
  144. break;
  145. }
  146. return ret;
  147. }
  148. static int sock_puts(BIO *bp, const char *str)
  149. {
  150. int n, ret;
  151. n = strlen(str);
  152. ret = sock_write(bp, str, n);
  153. return ret;
  154. }
  155. int BIO_sock_should_retry(int i)
  156. {
  157. int err;
  158. if ((i == 0) || (i == -1)) {
  159. err = get_last_socket_error();
  160. return BIO_sock_non_fatal_error(err);
  161. }
  162. return 0;
  163. }
  164. int BIO_sock_non_fatal_error(int err)
  165. {
  166. switch (err) {
  167. # if defined(OPENSSL_SYS_WINDOWS)
  168. # if defined(WSAEWOULDBLOCK)
  169. case WSAEWOULDBLOCK:
  170. # endif
  171. # endif
  172. # ifdef EWOULDBLOCK
  173. # ifdef WSAEWOULDBLOCK
  174. # if WSAEWOULDBLOCK != EWOULDBLOCK
  175. case EWOULDBLOCK:
  176. # endif
  177. # else
  178. case EWOULDBLOCK:
  179. # endif
  180. # endif
  181. # if defined(ENOTCONN)
  182. case ENOTCONN:
  183. # endif
  184. # ifdef EINTR
  185. case EINTR:
  186. # endif
  187. # ifdef EAGAIN
  188. # if EWOULDBLOCK != EAGAIN
  189. case EAGAIN:
  190. # endif
  191. # endif
  192. # ifdef EPROTO
  193. case EPROTO:
  194. # endif
  195. # ifdef EINPROGRESS
  196. case EINPROGRESS:
  197. # endif
  198. # ifdef EALREADY
  199. case EALREADY:
  200. # endif
  201. return 1;
  202. default:
  203. break;
  204. }
  205. return 0;
  206. }
  207. #endif /* #ifndef OPENSSL_NO_SOCK */