bss_fd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #if defined(OPENSSL_NO_POSIX_IO)
  13. /*
  14. * Dummy placeholder for BIO_s_fd...
  15. */
  16. BIO *BIO_new_fd(int fd, int close_flag)
  17. {
  18. return NULL;
  19. }
  20. int BIO_fd_non_fatal_error(int err)
  21. {
  22. return 0;
  23. }
  24. int BIO_fd_should_retry(int i)
  25. {
  26. return 0;
  27. }
  28. const BIO_METHOD *BIO_s_fd(void)
  29. {
  30. return NULL;
  31. }
  32. #else
  33. /*
  34. * As for unconditional usage of "UPLINK" interface in this module.
  35. * Trouble is that unlike Unix file descriptors [which are indexes
  36. * in kernel-side per-process table], corresponding descriptors on
  37. * platforms which require "UPLINK" interface seem to be indexes
  38. * in a user-land, non-global table. Well, in fact they are indexes
  39. * in stdio _iob[], and recall that _iob[] was the very reason why
  40. * "UPLINK" interface was introduced in first place. But one way on
  41. * another. Neither libcrypto or libssl use this BIO meaning that
  42. * file descriptors can only be provided by application. Therefore
  43. * "UPLINK" calls are due...
  44. */
  45. static int fd_write(BIO *h, const char *buf, int num);
  46. static int fd_read(BIO *h, char *buf, int size);
  47. static int fd_puts(BIO *h, const char *str);
  48. static int fd_gets(BIO *h, char *buf, int size);
  49. static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  50. static int fd_new(BIO *h);
  51. static int fd_free(BIO *data);
  52. int BIO_fd_should_retry(int s);
  53. static const BIO_METHOD methods_fdp = {
  54. BIO_TYPE_FD,
  55. "file descriptor",
  56. bwrite_conv,
  57. fd_write,
  58. bread_conv,
  59. fd_read,
  60. fd_puts,
  61. fd_gets,
  62. fd_ctrl,
  63. fd_new,
  64. fd_free,
  65. NULL, /* fd_callback_ctrl */
  66. };
  67. const BIO_METHOD *BIO_s_fd(void)
  68. {
  69. return &methods_fdp;
  70. }
  71. BIO *BIO_new_fd(int fd, int close_flag)
  72. {
  73. BIO *ret;
  74. ret = BIO_new(BIO_s_fd());
  75. if (ret == NULL)
  76. return NULL;
  77. BIO_set_fd(ret, fd, close_flag);
  78. return ret;
  79. }
  80. static int fd_new(BIO *bi)
  81. {
  82. bi->init = 0;
  83. bi->num = -1;
  84. bi->ptr = NULL;
  85. bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* essentially redundant */
  86. return 1;
  87. }
  88. static int fd_free(BIO *a)
  89. {
  90. if (a == NULL)
  91. return 0;
  92. if (a->shutdown) {
  93. if (a->init) {
  94. UP_close(a->num);
  95. }
  96. a->init = 0;
  97. a->flags = BIO_FLAGS_UPLINK_INTERNAL;
  98. }
  99. return 1;
  100. }
  101. static int fd_read(BIO *b, char *out, int outl)
  102. {
  103. int ret = 0;
  104. if (out != NULL) {
  105. clear_sys_error();
  106. ret = UP_read(b->num, out, outl);
  107. BIO_clear_retry_flags(b);
  108. if (ret <= 0) {
  109. if (BIO_fd_should_retry(ret))
  110. BIO_set_retry_read(b);
  111. else if (ret == 0)
  112. b->flags |= BIO_FLAGS_IN_EOF;
  113. }
  114. }
  115. return ret;
  116. }
  117. static int fd_write(BIO *b, const char *in, int inl)
  118. {
  119. int ret;
  120. clear_sys_error();
  121. ret = UP_write(b->num, in, inl);
  122. BIO_clear_retry_flags(b);
  123. if (ret <= 0) {
  124. if (BIO_fd_should_retry(ret))
  125. BIO_set_retry_write(b);
  126. }
  127. return ret;
  128. }
  129. static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
  130. {
  131. long ret = 1;
  132. int *ip;
  133. switch (cmd) {
  134. case BIO_CTRL_RESET:
  135. num = 0;
  136. /* fall thru */
  137. case BIO_C_FILE_SEEK:
  138. ret = (long)UP_lseek(b->num, num, 0);
  139. break;
  140. case BIO_C_FILE_TELL:
  141. case BIO_CTRL_INFO:
  142. ret = (long)UP_lseek(b->num, 0, 1);
  143. break;
  144. case BIO_C_SET_FD:
  145. fd_free(b);
  146. b->num = *((int *)ptr);
  147. b->shutdown = (int)num;
  148. b->init = 1;
  149. break;
  150. case BIO_C_GET_FD:
  151. if (b->init) {
  152. ip = (int *)ptr;
  153. if (ip != NULL)
  154. *ip = b->num;
  155. ret = b->num;
  156. } else
  157. ret = -1;
  158. break;
  159. case BIO_CTRL_GET_CLOSE:
  160. ret = b->shutdown;
  161. break;
  162. case BIO_CTRL_SET_CLOSE:
  163. b->shutdown = (int)num;
  164. break;
  165. case BIO_CTRL_PENDING:
  166. case BIO_CTRL_WPENDING:
  167. ret = 0;
  168. break;
  169. case BIO_CTRL_DUP:
  170. case BIO_CTRL_FLUSH:
  171. ret = 1;
  172. break;
  173. case BIO_CTRL_EOF:
  174. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
  175. break;
  176. default:
  177. ret = 0;
  178. break;
  179. }
  180. return ret;
  181. }
  182. static int fd_puts(BIO *bp, const char *str)
  183. {
  184. int n, ret;
  185. n = strlen(str);
  186. ret = fd_write(bp, str, n);
  187. return ret;
  188. }
  189. static int fd_gets(BIO *bp, char *buf, int size)
  190. {
  191. int ret = 0;
  192. char *ptr = buf;
  193. char *end = buf + size - 1;
  194. while (ptr < end && fd_read(bp, ptr, 1) > 0) {
  195. if (*ptr++ == '\n')
  196. break;
  197. }
  198. ptr[0] = '\0';
  199. if (buf[0] != '\0')
  200. ret = strlen(buf);
  201. return ret;
  202. }
  203. int BIO_fd_should_retry(int i)
  204. {
  205. int err;
  206. if ((i == 0) || (i == -1)) {
  207. err = get_last_sys_error();
  208. return BIO_fd_non_fatal_error(err);
  209. }
  210. return 0;
  211. }
  212. int BIO_fd_non_fatal_error(int err)
  213. {
  214. switch (err) {
  215. # ifdef EWOULDBLOCK
  216. # ifdef WSAEWOULDBLOCK
  217. # if WSAEWOULDBLOCK != EWOULDBLOCK
  218. case EWOULDBLOCK:
  219. # endif
  220. # else
  221. case EWOULDBLOCK:
  222. # endif
  223. # endif
  224. # if defined(ENOTCONN)
  225. case ENOTCONN:
  226. # endif
  227. # ifdef EINTR
  228. case EINTR:
  229. # endif
  230. # ifdef EAGAIN
  231. # if EWOULDBLOCK != EAGAIN
  232. case EAGAIN:
  233. # endif
  234. # endif
  235. # ifdef EPROTO
  236. case EPROTO:
  237. # endif
  238. # ifdef EINPROGRESS
  239. case EINPROGRESS:
  240. # endif
  241. # ifdef EALREADY
  242. case EALREADY:
  243. # endif
  244. return 1;
  245. default:
  246. break;
  247. }
  248. return 0;
  249. }
  250. #endif