bss_fd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright 1995-2020 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. /* TODO: Convert to new style write function */
  57. bwrite_conv,
  58. fd_write,
  59. /* TODO: Convert to new style read function */
  60. bread_conv,
  61. fd_read,
  62. fd_puts,
  63. fd_gets,
  64. fd_ctrl,
  65. fd_new,
  66. fd_free,
  67. NULL, /* fd_callback_ctrl */
  68. };
  69. const BIO_METHOD *BIO_s_fd(void)
  70. {
  71. return &methods_fdp;
  72. }
  73. BIO *BIO_new_fd(int fd, int close_flag)
  74. {
  75. BIO *ret;
  76. ret = BIO_new(BIO_s_fd());
  77. if (ret == NULL)
  78. return NULL;
  79. BIO_set_fd(ret, fd, close_flag);
  80. return ret;
  81. }
  82. static int fd_new(BIO *bi)
  83. {
  84. bi->init = 0;
  85. bi->num = -1;
  86. bi->ptr = NULL;
  87. bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* essentially redundant */
  88. return 1;
  89. }
  90. static int fd_free(BIO *a)
  91. {
  92. if (a == NULL)
  93. return 0;
  94. if (a->shutdown) {
  95. if (a->init) {
  96. UP_close(a->num);
  97. }
  98. a->init = 0;
  99. a->flags = BIO_FLAGS_UPLINK_INTERNAL;
  100. }
  101. return 1;
  102. }
  103. static int fd_read(BIO *b, char *out, int outl)
  104. {
  105. int ret = 0;
  106. if (out != NULL) {
  107. clear_sys_error();
  108. ret = UP_read(b->num, out, outl);
  109. BIO_clear_retry_flags(b);
  110. if (ret <= 0) {
  111. if (BIO_fd_should_retry(ret))
  112. BIO_set_retry_read(b);
  113. else if (ret == 0)
  114. b->flags |= BIO_FLAGS_IN_EOF;
  115. }
  116. }
  117. return ret;
  118. }
  119. static int fd_write(BIO *b, const char *in, int inl)
  120. {
  121. int ret;
  122. clear_sys_error();
  123. ret = UP_write(b->num, in, inl);
  124. BIO_clear_retry_flags(b);
  125. if (ret <= 0) {
  126. if (BIO_fd_should_retry(ret))
  127. BIO_set_retry_write(b);
  128. }
  129. return ret;
  130. }
  131. static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
  132. {
  133. long ret = 1;
  134. int *ip;
  135. switch (cmd) {
  136. case BIO_CTRL_RESET:
  137. num = 0;
  138. /* fall thru */
  139. case BIO_C_FILE_SEEK:
  140. ret = (long)UP_lseek(b->num, num, 0);
  141. break;
  142. case BIO_C_FILE_TELL:
  143. case BIO_CTRL_INFO:
  144. ret = (long)UP_lseek(b->num, 0, 1);
  145. break;
  146. case BIO_C_SET_FD:
  147. fd_free(b);
  148. b->num = *((int *)ptr);
  149. b->shutdown = (int)num;
  150. b->init = 1;
  151. break;
  152. case BIO_C_GET_FD:
  153. if (b->init) {
  154. ip = (int *)ptr;
  155. if (ip != NULL)
  156. *ip = b->num;
  157. ret = b->num;
  158. } else
  159. ret = -1;
  160. break;
  161. case BIO_CTRL_GET_CLOSE:
  162. ret = b->shutdown;
  163. break;
  164. case BIO_CTRL_SET_CLOSE:
  165. b->shutdown = (int)num;
  166. break;
  167. case BIO_CTRL_PENDING:
  168. case BIO_CTRL_WPENDING:
  169. ret = 0;
  170. break;
  171. case BIO_CTRL_DUP:
  172. case BIO_CTRL_FLUSH:
  173. ret = 1;
  174. break;
  175. case BIO_CTRL_EOF:
  176. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
  177. break;
  178. default:
  179. ret = 0;
  180. break;
  181. }
  182. return ret;
  183. }
  184. static int fd_puts(BIO *bp, const char *str)
  185. {
  186. int n, ret;
  187. n = strlen(str);
  188. ret = fd_write(bp, str, n);
  189. return ret;
  190. }
  191. static int fd_gets(BIO *bp, char *buf, int size)
  192. {
  193. int ret = 0;
  194. char *ptr = buf;
  195. char *end = buf + size - 1;
  196. while (ptr < end && fd_read(bp, ptr, 1) > 0) {
  197. if (*ptr++ == '\n')
  198. break;
  199. }
  200. ptr[0] = '\0';
  201. if (buf[0] != '\0')
  202. ret = strlen(buf);
  203. return ret;
  204. }
  205. int BIO_fd_should_retry(int i)
  206. {
  207. int err;
  208. if ((i == 0) || (i == -1)) {
  209. err = get_last_sys_error();
  210. return BIO_fd_non_fatal_error(err);
  211. }
  212. return 0;
  213. }
  214. int BIO_fd_non_fatal_error(int err)
  215. {
  216. switch (err) {
  217. # ifdef EWOULDBLOCK
  218. # ifdef WSAEWOULDBLOCK
  219. # if WSAEWOULDBLOCK != EWOULDBLOCK
  220. case EWOULDBLOCK:
  221. # endif
  222. # else
  223. case EWOULDBLOCK:
  224. # endif
  225. # endif
  226. # if defined(ENOTCONN)
  227. case ENOTCONN:
  228. # endif
  229. # ifdef EINTR
  230. case EINTR:
  231. # endif
  232. # ifdef EAGAIN
  233. # if EWOULDBLOCK != EAGAIN
  234. case EAGAIN:
  235. # endif
  236. # endif
  237. # ifdef EPROTO
  238. case EPROTO:
  239. # endif
  240. # ifdef EINPROGRESS
  241. case EINPROGRESS:
  242. # endif
  243. # ifdef EALREADY
  244. case EALREADY:
  245. # endif
  246. return 1;
  247. default:
  248. break;
  249. }
  250. return 0;
  251. }
  252. #endif