]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bss_fd.c
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
)
152 case BIO_C_FILE_SEEK
:
153 ret
= (long)UP_lseek(b
->num
, num
, 0);
155 case BIO_C_FILE_TELL
:
157 ret
= (long)UP_lseek(b
->num
, 0, 1);
161 b
->num
= *((int *)ptr
);
162 b
->shutdown
= (int)num
;
174 case BIO_CTRL_GET_CLOSE
:
177 case BIO_CTRL_SET_CLOSE
:
178 b
->shutdown
= (int)num
;
180 case BIO_CTRL_PENDING
:
181 case BIO_CTRL_WPENDING
:
195 static int fd_puts(BIO
*bp
, const char *str
)
200 ret
= fd_write(bp
, str
, n
);
204 static int fd_gets(BIO
*bp
, char *buf
, int size
)
208 char *end
= buf
+ size
- 1;
210 while ((ptr
< end
) && (fd_read(bp
, ptr
, 1) > 0) && (ptr
[0] != '\n'))
220 int BIO_fd_should_retry(int i
)
224 if ((i
== 0) || (i
== -1)) {
225 err
= get_last_sys_error();
227 return (BIO_fd_non_fatal_error(err
));
232 int BIO_fd_non_fatal_error(int err
)
237 # ifdef WSAEWOULDBLOCK
238 # if WSAEWOULDBLOCK != EWOULDBLOCK
246 # if defined(ENOTCONN)
255 # if EWOULDBLOCK != EAGAIN