PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/openssl/crypto/bio/bss_fd.c

https://gitlab.com/envieidoc/advancedtomato2
C | 330 lines | 222 code | 34 blank | 74 comment | 38 complexity | 6a3ed8103e405586c40f4e4b5c0d70a6 MD5 | raw file
  1. /* crypto/bio/bss_fd.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #define USE_SOCKETS
  61. #include "cryptlib.h"
  62. #if defined(OPENSSL_NO_POSIX_IO)
  63. /*
  64. * Dummy placeholder for BIO_s_fd...
  65. */
  66. BIO *BIO_new_fd(int fd, int close_flag)
  67. {
  68. return NULL;
  69. }
  70. int BIO_fd_non_fatal_error(int err)
  71. {
  72. return 0;
  73. }
  74. int BIO_fd_should_retry(int i)
  75. {
  76. return 0;
  77. }
  78. BIO_METHOD *BIO_s_fd(void)
  79. {
  80. return NULL;
  81. }
  82. #else
  83. /*
  84. * As for unconditional usage of "UPLINK" interface in this module.
  85. * Trouble is that unlike Unix file descriptors [which are indexes
  86. * in kernel-side per-process table], corresponding descriptors on
  87. * platforms which require "UPLINK" interface seem to be indexes
  88. * in a user-land, non-global table. Well, in fact they are indexes
  89. * in stdio _iob[], and recall that _iob[] was the very reason why
  90. * "UPLINK" interface was introduced in first place. But one way on
  91. * another. Neither libcrypto or libssl use this BIO meaning that
  92. * file descriptors can only be provided by application. Therefore
  93. * "UPLINK" calls are due...
  94. */
  95. # include "bio_lcl.h"
  96. static int fd_write(BIO *h, const char *buf, int num);
  97. static int fd_read(BIO *h, char *buf, int size);
  98. static int fd_puts(BIO *h, const char *str);
  99. static int fd_gets(BIO *h, char *buf, int size);
  100. static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  101. static int fd_new(BIO *h);
  102. static int fd_free(BIO *data);
  103. int BIO_fd_should_retry(int s);
  104. static BIO_METHOD methods_fdp = {
  105. BIO_TYPE_FD, "file descriptor",
  106. fd_write,
  107. fd_read,
  108. fd_puts,
  109. fd_gets,
  110. fd_ctrl,
  111. fd_new,
  112. fd_free,
  113. NULL,
  114. };
  115. BIO_METHOD *BIO_s_fd(void)
  116. {
  117. return (&methods_fdp);
  118. }
  119. BIO *BIO_new_fd(int fd, int close_flag)
  120. {
  121. BIO *ret;
  122. ret = BIO_new(BIO_s_fd());
  123. if (ret == NULL)
  124. return (NULL);
  125. BIO_set_fd(ret, fd, close_flag);
  126. return (ret);
  127. }
  128. static int fd_new(BIO *bi)
  129. {
  130. bi->init = 0;
  131. bi->num = -1;
  132. bi->ptr = NULL;
  133. bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */
  134. return (1);
  135. }
  136. static int fd_free(BIO *a)
  137. {
  138. if (a == NULL)
  139. return (0);
  140. if (a->shutdown) {
  141. if (a->init) {
  142. UP_close(a->num);
  143. }
  144. a->init = 0;
  145. a->flags = BIO_FLAGS_UPLINK;
  146. }
  147. return (1);
  148. }
  149. static int fd_read(BIO *b, char *out, int outl)
  150. {
  151. int ret = 0;
  152. if (out != NULL) {
  153. clear_sys_error();
  154. ret = UP_read(b->num, out, outl);
  155. BIO_clear_retry_flags(b);
  156. if (ret <= 0) {
  157. if (BIO_fd_should_retry(ret))
  158. BIO_set_retry_read(b);
  159. }
  160. }
  161. return (ret);
  162. }
  163. static int fd_write(BIO *b, const char *in, int inl)
  164. {
  165. int ret;
  166. clear_sys_error();
  167. ret = UP_write(b->num, in, inl);
  168. BIO_clear_retry_flags(b);
  169. if (ret <= 0) {
  170. if (BIO_fd_should_retry(ret))
  171. BIO_set_retry_write(b);
  172. }
  173. return (ret);
  174. }
  175. static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
  176. {
  177. long ret = 1;
  178. int *ip;
  179. switch (cmd) {
  180. case BIO_CTRL_RESET:
  181. num = 0;
  182. case BIO_C_FILE_SEEK:
  183. ret = (long)UP_lseek(b->num, num, 0);
  184. break;
  185. case BIO_C_FILE_TELL:
  186. case BIO_CTRL_INFO:
  187. ret = (long)UP_lseek(b->num, 0, 1);
  188. break;
  189. case BIO_C_SET_FD:
  190. fd_free(b);
  191. b->num = *((int *)ptr);
  192. b->shutdown = (int)num;
  193. b->init = 1;
  194. break;
  195. case BIO_C_GET_FD:
  196. if (b->init) {
  197. ip = (int *)ptr;
  198. if (ip != NULL)
  199. *ip = b->num;
  200. ret = b->num;
  201. } else
  202. ret = -1;
  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_DUP:
  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_puts(BIO *bp, const char *str)
  225. {
  226. int n, ret;
  227. n = strlen(str);
  228. ret = fd_write(bp, str, n);
  229. return (ret);
  230. }
  231. static int fd_gets(BIO *bp, char *buf, int size)
  232. {
  233. int ret = 0;
  234. char *ptr = buf;
  235. char *end = buf + size - 1;
  236. while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n'))
  237. ptr++;
  238. ptr[0] = '\0';
  239. if (buf[0] != '\0')
  240. ret = strlen(buf);
  241. return (ret);
  242. }
  243. int BIO_fd_should_retry(int i)
  244. {
  245. int err;
  246. if ((i == 0) || (i == -1)) {
  247. err = get_last_sys_error();
  248. # if defined(OPENSSL_SYS_WINDOWS) && 0/* more microsoft stupidity? perhaps
  249. * not? Ben 4/1/99 */
  250. if ((i == -1) && (err == 0))
  251. return (1);
  252. # endif
  253. return (BIO_fd_non_fatal_error(err));
  254. }
  255. return (0);
  256. }
  257. int BIO_fd_non_fatal_error(int err)
  258. {
  259. switch (err) {
  260. # ifdef EWOULDBLOCK
  261. # ifdef WSAEWOULDBLOCK
  262. # if WSAEWOULDBLOCK != EWOULDBLOCK
  263. case EWOULDBLOCK:
  264. # endif
  265. # else
  266. case EWOULDBLOCK:
  267. # endif
  268. # endif
  269. # if defined(ENOTCONN)
  270. case ENOTCONN:
  271. # endif
  272. # ifdef EINTR
  273. case EINTR:
  274. # endif
  275. # ifdef EAGAIN
  276. # if EWOULDBLOCK != EAGAIN
  277. case EAGAIN:
  278. # endif
  279. # endif
  280. # ifdef EPROTO
  281. case EPROTO:
  282. # endif
  283. # ifdef EINPROGRESS
  284. case EINPROGRESS:
  285. # endif
  286. # ifdef EALREADY
  287. case EALREADY:
  288. # endif
  289. return (1);
  290. /* break; */
  291. default:
  292. break;
  293. }
  294. return (0);
  295. }
  296. #endif