fd.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include "../internal.h"
  72. static int bio_fd_non_fatal_error(int err) {
  73. if (
  74. #ifdef EWOULDBLOCK
  75. err == EWOULDBLOCK ||
  76. #endif
  77. #ifdef WSAEWOULDBLOCK
  78. err == WSAEWOULDBLOCK ||
  79. #endif
  80. #ifdef ENOTCONN
  81. err == ENOTCONN ||
  82. #endif
  83. #ifdef EINTR
  84. err == EINTR ||
  85. #endif
  86. #ifdef EAGAIN
  87. err == EAGAIN ||
  88. #endif
  89. #ifdef EPROTO
  90. err == EPROTO ||
  91. #endif
  92. #ifdef EINPROGRESS
  93. err == EINPROGRESS ||
  94. #endif
  95. #ifdef EALREADY
  96. err == EALREADY ||
  97. #endif
  98. 0) {
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. #if defined(OPENSSL_WINDOWS)
  104. #define BORINGSSL_ERRNO (int)GetLastError()
  105. #define BORINGSSL_CLOSE _close
  106. #define BORINGSSL_LSEEK _lseek
  107. #define BORINGSSL_READ _read
  108. #define BORINGSSL_WRITE _write
  109. #else
  110. #define BORINGSSL_ERRNO errno
  111. #define BORINGSSL_CLOSE close
  112. #define BORINGSSL_LSEEK lseek
  113. #define BORINGSSL_READ read
  114. #define BORINGSSL_WRITE write
  115. #endif
  116. int bio_fd_should_retry(int i) {
  117. if (i == -1) {
  118. return bio_fd_non_fatal_error(BORINGSSL_ERRNO);
  119. }
  120. return 0;
  121. }
  122. BIO *BIO_new_fd(int fd, int close_flag) {
  123. BIO *ret = BIO_new(BIO_s_fd());
  124. if (ret == NULL) {
  125. return NULL;
  126. }
  127. BIO_set_fd(ret, fd, close_flag);
  128. return ret;
  129. }
  130. static int fd_new(BIO *bio) {
  131. // num is used to store the file descriptor.
  132. bio->num = -1;
  133. return 1;
  134. }
  135. static int fd_free(BIO *bio) {
  136. if (bio == NULL) {
  137. return 0;
  138. }
  139. if (bio->shutdown) {
  140. if (bio->init) {
  141. BORINGSSL_CLOSE(bio->num);
  142. }
  143. bio->init = 0;
  144. }
  145. return 1;
  146. }
  147. static int fd_read(BIO *b, char *out, int outl) {
  148. int ret = 0;
  149. ret = BORINGSSL_READ(b->num, out, outl);
  150. BIO_clear_retry_flags(b);
  151. if (ret <= 0) {
  152. if (bio_fd_should_retry(ret)) {
  153. BIO_set_retry_read(b);
  154. }
  155. }
  156. return ret;
  157. }
  158. static int fd_write(BIO *b, const char *in, int inl) {
  159. int ret = BORINGSSL_WRITE(b->num, in, inl);
  160. BIO_clear_retry_flags(b);
  161. if (ret <= 0) {
  162. if (bio_fd_should_retry(ret)) {
  163. BIO_set_retry_write(b);
  164. }
  165. }
  166. return ret;
  167. }
  168. static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
  169. long ret = 1;
  170. int *ip;
  171. switch (cmd) {
  172. case BIO_CTRL_RESET:
  173. num = 0;
  174. OPENSSL_FALLTHROUGH;
  175. case BIO_C_FILE_SEEK:
  176. ret = 0;
  177. if (b->init) {
  178. ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
  179. }
  180. break;
  181. case BIO_C_FILE_TELL:
  182. case BIO_CTRL_INFO:
  183. ret = 0;
  184. if (b->init) {
  185. ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
  186. }
  187. break;
  188. case BIO_C_SET_FD:
  189. fd_free(b);
  190. b->num = *((int *)ptr);
  191. b->shutdown = (int)num;
  192. b->init = 1;
  193. break;
  194. case BIO_C_GET_FD:
  195. if (b->init) {
  196. ip = (int *)ptr;
  197. if (ip != NULL) {
  198. *ip = b->num;
  199. }
  200. return b->num;
  201. } else {
  202. ret = -1;
  203. }
  204. break;
  205. case BIO_CTRL_GET_CLOSE:
  206. ret = b->shutdown;
  207. break;
  208. case BIO_CTRL_SET_CLOSE:
  209. b->shutdown = (int)num;
  210. break;
  211. case BIO_CTRL_PENDING:
  212. case BIO_CTRL_WPENDING:
  213. ret = 0;
  214. break;
  215. case BIO_CTRL_FLUSH:
  216. ret = 1;
  217. break;
  218. default:
  219. ret = 0;
  220. break;
  221. }
  222. return ret;
  223. }
  224. static int fd_gets(BIO *bp, char *buf, int size) {
  225. char *ptr = buf;
  226. char *end = buf + size - 1;
  227. if (size <= 0) {
  228. return 0;
  229. }
  230. while (ptr < end && fd_read(bp, ptr, 1) > 0 && ptr[0] != '\n') {
  231. ptr++;
  232. }
  233. ptr[0] = '\0';
  234. return ptr - buf;
  235. }
  236. static const BIO_METHOD methods_fdp = {
  237. BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */,
  238. fd_gets, fd_ctrl, fd_new, fd_free, NULL /* callback_ctrl */,
  239. };
  240. const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
  241. int BIO_set_fd(BIO *bio, int fd, int close_flag) {
  242. return BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
  243. }
  244. int BIO_get_fd(BIO *bio, int *out_fd) {
  245. return BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
  246. }