2
0

bss_fd.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #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; /* 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;
  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. }
  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. default:
  174. ret = 0;
  175. break;
  176. }
  177. return ret;
  178. }
  179. static int fd_puts(BIO *bp, const char *str)
  180. {
  181. int n, ret;
  182. n = strlen(str);
  183. ret = fd_write(bp, str, n);
  184. return ret;
  185. }
  186. static int fd_gets(BIO *bp, char *buf, int size)
  187. {
  188. int ret = 0;
  189. char *ptr = buf;
  190. char *end = buf + size - 1;
  191. while (ptr < end && fd_read(bp, ptr, 1) > 0) {
  192. if (*ptr++ == '\n')
  193. break;
  194. }
  195. ptr[0] = '\0';
  196. if (buf[0] != '\0')
  197. ret = strlen(buf);
  198. return ret;
  199. }
  200. int BIO_fd_should_retry(int i)
  201. {
  202. int err;
  203. if ((i == 0) || (i == -1)) {
  204. err = get_last_sys_error();
  205. return BIO_fd_non_fatal_error(err);
  206. }
  207. return 0;
  208. }
  209. int BIO_fd_non_fatal_error(int err)
  210. {
  211. switch (err) {
  212. # ifdef EWOULDBLOCK
  213. # ifdef WSAEWOULDBLOCK
  214. # if WSAEWOULDBLOCK != EWOULDBLOCK
  215. case EWOULDBLOCK:
  216. # endif
  217. # else
  218. case EWOULDBLOCK:
  219. # endif
  220. # endif
  221. # if defined(ENOTCONN)
  222. case ENOTCONN:
  223. # endif
  224. # ifdef EINTR
  225. case EINTR:
  226. # endif
  227. # ifdef EAGAIN
  228. # if EWOULDBLOCK != EAGAIN
  229. case EAGAIN:
  230. # endif
  231. # endif
  232. # ifdef EPROTO
  233. case EPROTO:
  234. # endif
  235. # ifdef EINPROGRESS
  236. case EINPROGRESS:
  237. # endif
  238. # ifdef EALREADY
  239. case EALREADY:
  240. # endif
  241. return 1;
  242. default:
  243. break;
  244. }
  245. return 0;
  246. }
  247. #endif