25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

268 lines
6.8 KiB

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