fd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/bio.h>
  57. #include <errno.h>
  58. #include <string.h>
  59. #if !defined(OPENSSL_WINDOWS)
  60. #include <unistd.h>
  61. #else
  62. #include <io.h>
  63. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  64. #include <windows.h>
  65. OPENSSL_MSVC_PRAGMA(warning(pop))
  66. #endif
  67. #include <openssl/buf.h>
  68. #include <openssl/err.h>
  69. #include <openssl/mem.h>
  70. #include "internal.h"
  71. static int bio_fd_non_fatal_error(int err) {
  72. if (
  73. #ifdef EWOULDBLOCK
  74. err == EWOULDBLOCK ||
  75. #endif
  76. #ifdef WSAEWOULDBLOCK
  77. err == WSAEWOULDBLOCK ||
  78. #endif
  79. #ifdef ENOTCONN
  80. err == ENOTCONN ||
  81. #endif
  82. #ifdef EINTR
  83. err == EINTR ||
  84. #endif
  85. #ifdef EAGAIN
  86. err == EAGAIN ||
  87. #endif
  88. #ifdef EPROTO
  89. err == EPROTO ||
  90. #endif
  91. #ifdef EINPROGRESS
  92. err == EINPROGRESS ||
  93. #endif
  94. #ifdef EALREADY
  95. err == EALREADY ||
  96. #endif
  97. 0) {
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. #if defined(OPENSSL_WINDOWS)
  103. #define BORINGSSL_ERRNO (int)GetLastError()
  104. #define BORINGSSL_CLOSE _close
  105. #define BORINGSSL_LSEEK _lseek
  106. #define BORINGSSL_READ _read
  107. #define BORINGSSL_WRITE _write
  108. #else
  109. #define BORINGSSL_ERRNO errno
  110. #define BORINGSSL_CLOSE close
  111. #define BORINGSSL_LSEEK lseek
  112. #define BORINGSSL_READ read
  113. #define BORINGSSL_WRITE write
  114. #endif
  115. int bio_fd_should_retry(int i) {
  116. if (i == -1) {
  117. return bio_fd_non_fatal_error(BORINGSSL_ERRNO);
  118. }
  119. return 0;
  120. }
  121. BIO *BIO_new_fd(int fd, int close_flag) {
  122. BIO *ret = BIO_new(BIO_s_fd());
  123. if (ret == NULL) {
  124. return NULL;
  125. }
  126. BIO_set_fd(ret, fd, close_flag);
  127. return ret;
  128. }
  129. static int fd_new(BIO *bio) {
  130. // num is used to store the file descriptor.
  131. bio->num = -1;
  132. return 1;
  133. }
  134. static int fd_free(BIO *bio) {
  135. if (bio == NULL) {
  136. return 0;
  137. }
  138. if (bio->shutdown) {
  139. if (bio->init) {
  140. BORINGSSL_CLOSE(bio->num);
  141. }
  142. bio->init = 0;
  143. }
  144. return 1;
  145. }
  146. static int fd_read(BIO *b, char *out, int outl) {
  147. int ret = 0;
  148. ret = BORINGSSL_READ(b->num, out, outl);
  149. BIO_clear_retry_flags(b);
  150. if (ret <= 0) {
  151. if (bio_fd_should_retry(ret)) {
  152. BIO_set_retry_read(b);
  153. }
  154. }
  155. return ret;
  156. }
  157. static int fd_write(BIO *b, const char *in, int inl) {
  158. int ret = BORINGSSL_WRITE(b->num, in, inl);
  159. BIO_clear_retry_flags(b);
  160. if (ret <= 0) {
  161. if (bio_fd_should_retry(ret)) {
  162. BIO_set_retry_write(b);
  163. }
  164. }
  165. return ret;
  166. }
  167. static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
  168. long ret = 1;
  169. int *ip;
  170. switch (cmd) {
  171. case BIO_CTRL_RESET:
  172. num = 0;
  173. OPENSSL_FALLTHROUGH;
  174. case BIO_C_FILE_SEEK:
  175. ret = 0;
  176. if (b->init) {
  177. ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
  178. }
  179. break;
  180. case BIO_C_FILE_TELL:
  181. case BIO_CTRL_INFO:
  182. ret = 0;
  183. if (b->init) {
  184. ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
  185. }
  186. break;
  187. case BIO_C_SET_FD:
  188. fd_free(b);
  189. b->num = *((int *)ptr);
  190. b->shutdown = (int)num;
  191. b->init = 1;
  192. break;
  193. case BIO_C_GET_FD:
  194. if (b->init) {
  195. ip = (int *)ptr;
  196. if (ip != NULL) {
  197. *ip = b->num;
  198. }
  199. return b->num;
  200. } else {
  201. ret = -1;
  202. }
  203. break;
  204. case BIO_CTRL_GET_CLOSE:
  205. ret = b->shutdown;
  206. break;
  207. case BIO_CTRL_SET_CLOSE:
  208. b->shutdown = (int)num;
  209. break;
  210. case BIO_CTRL_PENDING:
  211. case BIO_CTRL_WPENDING:
  212. ret = 0;
  213. break;
  214. case BIO_CTRL_FLUSH:
  215. ret = 1;
  216. break;
  217. default:
  218. ret = 0;
  219. break;
  220. }
  221. return ret;
  222. }
  223. static int fd_gets(BIO *bp, char *buf, int size) {
  224. char *ptr = buf;
  225. char *end = buf + size - 1;
  226. if (size <= 0) {
  227. return 0;
  228. }
  229. while (ptr < end && fd_read(bp, ptr, 1) > 0 && ptr[0] != '\n') {
  230. ptr++;
  231. }
  232. ptr[0] = '\0';
  233. return ptr - buf;
  234. }
  235. static const BIO_METHOD methods_fdp = {
  236. BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */,
  237. fd_gets, fd_ctrl, fd_new, fd_free, NULL /* callback_ctrl */,
  238. };
  239. const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
  240. int BIO_set_fd(BIO *bio, int fd, int close_flag) {
  241. return BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
  242. }
  243. int BIO_get_fd(BIO *bio, int *out_fd) {
  244. return BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
  245. }