Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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