fd.c
Go to the documentation of this file.
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 
57 #include <openssl/bio.h>
58 
59 #if !defined(OPENSSL_TRUSTY)
60 
61 #include <errno.h>
62 #include <string.h>
63 
64 #if !defined(OPENSSL_WINDOWS)
65 #include <unistd.h>
66 #else
67 #include <io.h>
69 #include <windows.h>
71 #endif
72 
73 #include <openssl/err.h>
74 #include <openssl/mem.h>
75 
76 #include "internal.h"
77 #include "../internal.h"
78 
79 
80 static int bio_fd_non_fatal_error(int err) {
81  if (
82 #ifdef EWOULDBLOCK
83  err == EWOULDBLOCK ||
84 #endif
85 #ifdef WSAEWOULDBLOCK
86  err == WSAEWOULDBLOCK ||
87 #endif
88 #ifdef ENOTCONN
89  err == ENOTCONN ||
90 #endif
91 #ifdef EINTR
92  err == EINTR ||
93 #endif
94 #ifdef EAGAIN
95  err == EAGAIN ||
96 #endif
97 #ifdef EPROTO
98  err == EPROTO ||
99 #endif
100 #ifdef EINPROGRESS
101  err == EINPROGRESS ||
102 #endif
103 #ifdef EALREADY
104  err == EALREADY ||
105 #endif
106  0) {
107  return 1;
108  }
109  return 0;
110 }
111 
112 #if defined(OPENSSL_WINDOWS)
113  #define BORINGSSL_ERRNO (int)GetLastError()
114  #define BORINGSSL_CLOSE _close
115  #define BORINGSSL_LSEEK _lseek
116  #define BORINGSSL_READ _read
117  #define BORINGSSL_WRITE _write
118 #else
119  #define BORINGSSL_ERRNO errno
120  #define BORINGSSL_CLOSE close
121  #define BORINGSSL_LSEEK lseek
122  #define BORINGSSL_READ read
123  #define BORINGSSL_WRITE write
124 #endif
125 
127  if (i == -1) {
129  }
130  return 0;
131 }
132 
133 BIO *BIO_new_fd(int fd, int close_flag) {
134  BIO *ret = BIO_new(BIO_s_fd());
135  if (ret == NULL) {
136  return NULL;
137  }
138  BIO_set_fd(ret, fd, close_flag);
139  return ret;
140 }
141 
142 static int fd_new(BIO *bio) {
143  // num is used to store the file descriptor.
144  bio->num = -1;
145  return 1;
146 }
147 
148 static int fd_free(BIO *bio) {
149  if (bio->shutdown) {
150  if (bio->init) {
151  BORINGSSL_CLOSE(bio->num);
152  }
153  bio->init = 0;
154  }
155  return 1;
156 }
157 
158 static int fd_read(BIO *b, char *out, int outl) {
159  int ret = 0;
160 
161  ret = BORINGSSL_READ(b->num, out, outl);
163  if (ret <= 0) {
164  if (bio_fd_should_retry(ret)) {
166  }
167  }
168 
169  return ret;
170 }
171 
172 static int fd_write(BIO *b, const char *in, int inl) {
173  int ret = BORINGSSL_WRITE(b->num, in, inl);
175  if (ret <= 0) {
176  if (bio_fd_should_retry(ret)) {
178  }
179  }
180 
181  return ret;
182 }
183 
184 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
185  long ret = 1;
186  int *ip;
187 
188  switch (cmd) {
189  case BIO_CTRL_RESET:
190  num = 0;
192  case BIO_C_FILE_SEEK:
193  ret = 0;
194  if (b->init) {
195  ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
196  }
197  break;
198  case BIO_C_FILE_TELL:
199  case BIO_CTRL_INFO:
200  ret = 0;
201  if (b->init) {
202  ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
203  }
204  break;
205  case BIO_C_SET_FD:
206  fd_free(b);
207  b->num = *((int *)ptr);
208  b->shutdown = (int)num;
209  b->init = 1;
210  break;
211  case BIO_C_GET_FD:
212  if (b->init) {
213  ip = (int *)ptr;
214  if (ip != NULL) {
215  *ip = b->num;
216  }
217  return b->num;
218  } else {
219  ret = -1;
220  }
221  break;
222  case BIO_CTRL_GET_CLOSE:
223  ret = b->shutdown;
224  break;
225  case BIO_CTRL_SET_CLOSE:
226  b->shutdown = (int)num;
227  break;
228  case BIO_CTRL_PENDING:
229  case BIO_CTRL_WPENDING:
230  ret = 0;
231  break;
232  case BIO_CTRL_FLUSH:
233  ret = 1;
234  break;
235  default:
236  ret = 0;
237  break;
238  }
239 
240  return ret;
241 }
242 
243 static int fd_gets(BIO *bp, char *buf, int size) {
244  char *ptr = buf;
245  char *end = buf + size - 1;
246 
247  if (size <= 0) {
248  return 0;
249  }
250 
251  while (ptr < end && fd_read(bp, ptr, 1) > 0 && ptr[0] != '\n') {
252  ptr++;
253  }
254 
255  ptr[0] = '\0';
256 
257  return ptr - buf;
258 }
259 
260 static const BIO_METHOD methods_fdp = {
261  BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */,
262  fd_gets, fd_ctrl, fd_new, fd_free, NULL /* callback_ctrl */,
263 };
264 
265 const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
266 
267 int BIO_set_fd(BIO *bio, int fd, int close_flag) {
268  return BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
269 }
270 
271 int BIO_get_fd(BIO *bio, int *out_fd) {
272  return BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
273 }
274 
275 #endif // OPENSSL_TRUSTY
BIO_s_fd
const BIO_METHOD * BIO_s_fd(void)
Definition: fd.c:265
bio_method_st
Definition: bio.h:808
BIO_C_FILE_TELL
#define BIO_C_FILE_TELL
Definition: bio.h:877
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
bio_fd_should_retry
int bio_fd_should_retry(int i)
Definition: fd.c:126
BIO_CTRL_WPENDING
#define BIO_CTRL_WPENDING
Definition: bio.h:715
fd_ctrl
static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
Definition: fd.c:184
BIO_CTRL_INFO
#define BIO_CTRL_INFO
Definition: bio.h:698
internal.h
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
BORINGSSL_LSEEK
#define BORINGSSL_LSEEK
Definition: fd.c:121
BIO_int_ctrl
#define BIO_int_ctrl
Definition: boringssl_prefix_symbols.h:802
fd_read
static int fd_read(BIO *b, char *out, int outl)
Definition: fd.c:158
bio_st
Definition: bio.h:822
BIO_C_GET_FD
#define BIO_C_GET_FD
Definition: bio.h:850
BIO_set_retry_write
#define BIO_set_retry_write
Definition: boringssl_prefix_symbols.h:856
SEEK_CUR
#define SEEK_CUR
Definition: bloaty/third_party/zlib/contrib/minizip/zip.c:80
check_version.warning
string warning
Definition: check_version.py:46
bio.h
BIO_C_SET_FD
#define BIO_C_SET_FD
Definition: bio.h:849
string.h
bio_st::init
int init
Definition: bio.h:826
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
BORINGSSL_READ
#define BORINGSSL_READ
Definition: fd.c:122
error_ref_leak.err
err
Definition: error_ref_leak.py:35
fd_new
static int fd_new(BIO *bio)
Definition: fd.c:142
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
BIO_set_fd
int BIO_set_fd(BIO *bio, int fd, int close_flag)
Definition: fd.c:267
xds_interop_client.int
int
Definition: xds_interop_client.py:113
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
BORINGSSL_WRITE
#define BORINGSSL_WRITE
Definition: fd.c:123
BIO_ctrl
#define BIO_ctrl
Definition: boringssl_prefix_symbols.h:779
BORINGSSL_CLOSE
#define BORINGSSL_CLOSE
Definition: fd.c:120
BIO_C_FILE_SEEK
#define BIO_C_FILE_SEEK
Definition: bio.h:872
regen-readme.cmd
cmd
Definition: regen-readme.py:21
err.h
OPENSSL_MSVC_PRAGMA
OPENSSL_MSVC_PRAGMA(warning(disable:4702))
Definition: e_aes.c:69
BIO_new
#define BIO_new
Definition: boringssl_prefix_symbols.h:814
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
BORINGSSL_ERRNO
#define BORINGSSL_ERRNO
Definition: fd.c:119
bio_st::shutdown
int shutdown
Definition: bio.h:831
push
int push(void *desc, unsigned char *buf, unsigned len)
Definition: bloaty/third_party/zlib/test/infcover.c:463
BIO_CTRL_GET_CLOSE
#define BIO_CTRL_GET_CLOSE
Definition: bio.h:702
bio_st::num
int num
Definition: bio.h:836
BIO_CTRL_FLUSH
#define BIO_CTRL_FLUSH
Definition: bio.h:712
BIO_CTRL_PENDING
#define BIO_CTRL_PENDING
Definition: bio.h:709
fd_write
static int fd_write(BIO *b, const char *in, int inl)
Definition: fd.c:172
BIO_CTRL_SET_CLOSE
#define BIO_CTRL_SET_CLOSE
Definition: bio.h:706
fd_gets
static int fd_gets(BIO *bp, char *buf, int size)
Definition: fd.c:243
BIO_get_fd
int BIO_get_fd(BIO *bio, int *out_fd)
Definition: fd.c:271
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
xds_manager.num
num
Definition: xds_manager.py:56
methods_fdp
static const BIO_METHOD methods_fdp
Definition: fd.c:260
BIO_CTRL_RESET
#define BIO_CTRL_RESET
Definition: bio.h:690
fd_free
static int fd_free(BIO *bio)
Definition: fd.c:148
BIO_TYPE_FD
#define BIO_TYPE_FD
Definition: bio.h:777
mem.h
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
BIO_new_fd
BIO * BIO_new_fd(int fd, int close_flag)
Definition: fd.c:133
BIO_set_retry_read
#define BIO_set_retry_read
Definition: boringssl_prefix_symbols.h:853
SEEK_SET
#define SEEK_SET
Definition: bloaty/third_party/zlib/contrib/minizip/zip.c:88
OPENSSL_FALLTHROUGH
#define OPENSSL_FALLTHROUGH
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:201
errno.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
BIO_clear_retry_flags
#define BIO_clear_retry_flags
Definition: boringssl_prefix_symbols.h:777
google::protobuf.internal.decoder.long
long
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/decoder.py:89
bio_fd_non_fatal_error
static int bio_fd_non_fatal_error(int err)
Definition: fd.c:80


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:19