2
0

bss_sock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 1995-2016 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_lcl.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. }
  92. }
  93. return ret;
  94. }
  95. static int sock_write(BIO *b, const char *in, int inl)
  96. {
  97. int ret;
  98. clear_socket_error();
  99. ret = writesocket(b->num, in, inl);
  100. BIO_clear_retry_flags(b);
  101. if (ret <= 0) {
  102. if (BIO_sock_should_retry(ret))
  103. BIO_set_retry_write(b);
  104. }
  105. return ret;
  106. }
  107. static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
  108. {
  109. long ret = 1;
  110. int *ip;
  111. switch (cmd) {
  112. case BIO_C_SET_FD:
  113. sock_free(b);
  114. b->num = *((int *)ptr);
  115. b->shutdown = (int)num;
  116. b->init = 1;
  117. break;
  118. case BIO_C_GET_FD:
  119. if (b->init) {
  120. ip = (int *)ptr;
  121. if (ip != NULL)
  122. *ip = b->num;
  123. ret = b->num;
  124. } else
  125. ret = -1;
  126. break;
  127. case BIO_CTRL_GET_CLOSE:
  128. ret = b->shutdown;
  129. break;
  130. case BIO_CTRL_SET_CLOSE:
  131. b->shutdown = (int)num;
  132. break;
  133. case BIO_CTRL_DUP:
  134. case BIO_CTRL_FLUSH:
  135. ret = 1;
  136. break;
  137. default:
  138. ret = 0;
  139. break;
  140. }
  141. return ret;
  142. }
  143. static int sock_puts(BIO *bp, const char *str)
  144. {
  145. int n, ret;
  146. n = strlen(str);
  147. ret = sock_write(bp, str, n);
  148. return ret;
  149. }
  150. int BIO_sock_should_retry(int i)
  151. {
  152. int err;
  153. if ((i == 0) || (i == -1)) {
  154. err = get_last_socket_error();
  155. return BIO_sock_non_fatal_error(err);
  156. }
  157. return 0;
  158. }
  159. int BIO_sock_non_fatal_error(int err)
  160. {
  161. switch (err) {
  162. # if defined(OPENSSL_SYS_WINDOWS)
  163. # if defined(WSAEWOULDBLOCK)
  164. case WSAEWOULDBLOCK:
  165. # endif
  166. # endif
  167. # ifdef EWOULDBLOCK
  168. # ifdef WSAEWOULDBLOCK
  169. # if WSAEWOULDBLOCK != EWOULDBLOCK
  170. case EWOULDBLOCK:
  171. # endif
  172. # else
  173. case EWOULDBLOCK:
  174. # endif
  175. # endif
  176. # if defined(ENOTCONN)
  177. case ENOTCONN:
  178. # endif
  179. # ifdef EINTR
  180. case EINTR:
  181. # endif
  182. # ifdef EAGAIN
  183. # if EWOULDBLOCK != EAGAIN
  184. case EAGAIN:
  185. # endif
  186. # endif
  187. # ifdef EPROTO
  188. case EPROTO:
  189. # endif
  190. # ifdef EINPROGRESS
  191. case EINPROGRESS:
  192. # endif
  193. # ifdef EALREADY
  194. case EALREADY:
  195. # endif
  196. return 1;
  197. default:
  198. break;
  199. }
  200. return 0;
  201. }
  202. #endif /* #ifndef OPENSSL_NO_SOCK */