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 = {
73 const BIO_METHOD *BIO_s_fd(void)
75 return (&methods_fdp);
78 BIO *BIO_new_fd(int fd, int close_flag)
81 ret = BIO_new(BIO_s_fd());
84 BIO_set_fd(ret, fd, close_flag);
88 static int fd_new(BIO *bi)
93 bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */
97 static int fd_free(BIO *a)
106 a->flags = BIO_FLAGS_UPLINK;
111 static int fd_read(BIO *b, char *out, int outl)
117 ret = UP_read(b->num, out, outl);
118 BIO_clear_retry_flags(b);
120 if (BIO_fd_should_retry(ret))
121 BIO_set_retry_read(b);
127 static int fd_write(BIO *b, const char *in, int inl)
131 ret = UP_write(b->num, in, inl);
132 BIO_clear_retry_flags(b);
134 if (BIO_fd_should_retry(ret))
135 BIO_set_retry_write(b);
140 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
149 case BIO_C_FILE_SEEK:
150 ret = (long)UP_lseek(b->num, num, 0);
152 case BIO_C_FILE_TELL:
154 ret = (long)UP_lseek(b->num, 0, 1);
158 b->num = *((int *)ptr);
159 b->shutdown = (int)num;
171 case BIO_CTRL_GET_CLOSE:
174 case BIO_CTRL_SET_CLOSE:
175 b->shutdown = (int)num;
177 case BIO_CTRL_PENDING:
178 case BIO_CTRL_WPENDING:
192 static int fd_puts(BIO *bp, const char *str)
197 ret = fd_write(bp, str, n);
201 static int fd_gets(BIO *bp, char *buf, int size)
205 char *end = buf + size - 1;
207 while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n'))
217 int BIO_fd_should_retry(int i)
221 if ((i == 0) || (i == -1)) {
222 err = get_last_sys_error();
224 return (BIO_fd_non_fatal_error(err));
229 int BIO_fd_non_fatal_error(int err)
234 # ifdef WSAEWOULDBLOCK
235 # if WSAEWOULDBLOCK != EWOULDBLOCK
243 # if defined(ENOTCONN)
252 # if EWOULDBLOCK != EAGAIN