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
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",
47 const BIO_METHOD *BIO_f_nbio_test(void)
49 return (&methods_nbiof);
52 static int nbiof_new(BIO *bi)
56 if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
65 static int nbiof_free(BIO *a)
76 static int nbiof_read(BIO *b, char *out, int outl)
84 if (b->next_bio == NULL)
87 BIO_clear_retry_flags(b);
88 if (RAND_bytes(&n, 1) <= 0)
97 BIO_set_retry_read(b);
99 ret = BIO_read(b->next_bio, out, outl);
101 BIO_copy_next_retry(b);
106 static int nbiof_write(BIO *b, const char *in, int inl)
113 if ((in == NULL) || (inl <= 0))
115 if (b->next_bio == NULL)
117 nt = (NBIO_TEST *)b->ptr;
119 BIO_clear_retry_flags(b);
125 if (RAND_bytes(&n, 1) <= 0)
135 BIO_set_retry_write(b);
137 ret = BIO_write(b->next_bio, in, inl);
139 BIO_copy_next_retry(b);
146 static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
150 if (b->next_bio == NULL)
153 case BIO_C_DO_STATE_MACHINE:
154 BIO_clear_retry_flags(b);
155 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
156 BIO_copy_next_retry(b);
162 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
168 static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
172 if (b->next_bio == NULL)
176 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
182 static int nbiof_gets(BIO *bp, char *buf, int size)
184 if (bp->next_bio == NULL)
186 return (BIO_gets(bp->next_bio, buf, size));
189 static int nbiof_puts(BIO *bp, const char *str)
191 if (bp->next_bio == NULL)
193 return (BIO_puts(bp->next_bio, str));