2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #if defined(OPENSSL_NO_POSIX_IO)
17 * Dummy placeholder for BIO_s_fd...
19 BIO *BIO_new_fd(int fd, int close_flag)
24 int BIO_fd_non_fatal_error(int err)
29 int BIO_fd_should_retry(int i)
34 const BIO_METHOD *BIO_s_fd(void)
40 * As for unconditional usage of "UPLINK" interface in this module.
41 * Trouble is that unlike Unix file descriptors [which are indexes
42 * in kernel-side per-process table], corresponding descriptors on
43 * platforms which require "UPLINK" interface seem to be indexes
44 * in a user-land, non-global table. Well, in fact they are indexes
45 * in stdio _iob[], and recall that _iob[] was the very reason why
46 * "UPLINK" interface was introduced in first place. But one way on
47 * another. Neither libcrypto or libssl use this BIO meaning that
48 * file descriptors can only be provided by application. Therefore
49 * "UPLINK" calls are due...
51 static int fd_write(BIO *h, const char *buf, int num);
52 static int fd_read(BIO *h, char *buf, int size);
53 static int fd_puts(BIO *h, const char *str);
54 static int fd_gets(BIO *h, char *buf, int size);
55 static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
56 static int fd_new(BIO *h);
57 static int fd_free(BIO *data);
58 int BIO_fd_should_retry(int s);
60 static const BIO_METHOD methods_fdp = {
61 BIO_TYPE_FD, "file descriptor",
62 /* TODO: Convert to new style write function */
65 /* TODO: Convert to new style read function */
76 const BIO_METHOD *BIO_s_fd(void)
78 return (&methods_fdp);
81 BIO *BIO_new_fd(int fd, int close_flag)
84 ret = BIO_new(BIO_s_fd());
87 BIO_set_fd(ret, fd, close_flag);
91 static int fd_new(BIO *bi)
96 bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */
100 static int fd_free(BIO *a)
109 a->flags = BIO_FLAGS_UPLINK;
114 static int fd_read(BIO *b, char *out, int outl)
120 ret = UP_read(b->num, out, outl);
121 BIO_clear_retry_flags(b);
123 if (BIO_fd_should_retry(ret))
124 BIO_set_retry_read(b);
130 static int fd_write(BIO *b, const char *in, int inl)
134 ret = UP_write(b->num, in, inl);
135 BIO_clear_retry_flags(b);
137 if (BIO_fd_should_retry(ret))
138 BIO_set_retry_write(b);
143 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
151 case BIO_C_FILE_SEEK:
152 ret = (long)UP_lseek(b->num, num, 0);
154 case BIO_C_FILE_TELL:
156 ret = (long)UP_lseek(b->num, 0, 1);
160 b->num = *((int *)ptr);
161 b->shutdown = (int)num;
173 case BIO_CTRL_GET_CLOSE:
176 case BIO_CTRL_SET_CLOSE:
177 b->shutdown = (int)num;
179 case BIO_CTRL_PENDING:
180 case BIO_CTRL_WPENDING:
194 static int fd_puts(BIO *bp, const char *str)
199 ret = fd_write(bp, str, n);
203 static int fd_gets(BIO *bp, char *buf, int size)
207 char *end = buf + size - 1;
209 while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n'))
219 int BIO_fd_should_retry(int i)
223 if ((i == 0) || (i == -1)) {
224 err = get_last_sys_error();
226 return (BIO_fd_non_fatal_error(err));
231 int BIO_fd_non_fatal_error(int err)
236 # ifdef WSAEWOULDBLOCK
237 # if WSAEWOULDBLOCK != EWOULDBLOCK
245 # if defined(ENOTCONN)
254 # if EWOULDBLOCK != EAGAIN