2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include "bio_local.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/rand.h>
17 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
20 static int nbiof_write(BIO *h, const char *buf, int num);
21 static int nbiof_read(BIO *h, char *buf, int size);
22 static int nbiof_puts(BIO *h, const char *str);
23 static int nbiof_gets(BIO *h, char *str, int size);
24 static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25 static int nbiof_new(BIO *h);
26 static int nbiof_free(BIO *data);
27 static long nbiof_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
28 typedef struct nbio_test_st {
29 /* only set if we sent a 'should retry' error */
34 static const BIO_METHOD methods_nbiof = {
36 "non-blocking IO test filter",
49 const BIO_METHOD *BIO_f_nbio_test(void)
51 return &methods_nbiof;
54 static int nbiof_new(BIO *bi)
58 if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
67 static int nbiof_free(BIO *a)
78 static int nbiof_read(BIO *b, char *out, int outl)
86 if (b->next_bio == NULL)
89 BIO_clear_retry_flags(b);
90 if (RAND_priv_bytes(&n, 1) <= 0)
99 BIO_set_retry_read(b);
101 ret = BIO_read(b->next_bio, out, outl);
103 BIO_copy_next_retry(b);
108 static int nbiof_write(BIO *b, const char *in, int inl)
115 if ((in == NULL) || (inl <= 0))
117 if (b->next_bio == NULL)
119 nt = (NBIO_TEST *)b->ptr;
121 BIO_clear_retry_flags(b);
127 if (RAND_priv_bytes(&n, 1) <= 0)
137 BIO_set_retry_write(b);
139 ret = BIO_write(b->next_bio, in, inl);
141 BIO_copy_next_retry(b);
148 static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
152 if (b->next_bio == NULL)
155 case BIO_C_DO_STATE_MACHINE:
156 BIO_clear_retry_flags(b);
157 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
158 BIO_copy_next_retry(b);
164 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
170 static long nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
172 if (b->next_bio == NULL)
174 return BIO_callback_ctrl(b->next_bio, cmd, fp);
177 static int nbiof_gets(BIO *bp, char *buf, int size)
179 if (bp->next_bio == NULL)
181 return BIO_gets(bp->next_bio, buf, size);
184 static int nbiof_puts(BIO *bp, const char *str)
186 if (bp->next_bio == NULL)
188 return BIO_puts(bp->next_bio, str);