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",
37 /* TODO: Convert to new style write function */
40 /* TODO: Convert to new style read function */
51 const BIO_METHOD *BIO_f_nbio_test(void)
53 return &methods_nbiof;
56 static int nbiof_new(BIO *bi)
60 if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
69 static int nbiof_free(BIO *a)
80 static int nbiof_read(BIO *b, char *out, int outl)
88 if (b->next_bio == NULL)
91 BIO_clear_retry_flags(b);
92 if (RAND_bytes(&n, 1) <= 0)
101 BIO_set_retry_read(b);
103 ret = BIO_read(b->next_bio, out, outl);
105 BIO_copy_next_retry(b);
110 static int nbiof_write(BIO *b, const char *in, int inl)
117 if ((in == NULL) || (inl <= 0))
119 if (b->next_bio == NULL)
121 nt = (NBIO_TEST *)b->ptr;
123 BIO_clear_retry_flags(b);
129 if (RAND_bytes(&n, 1) <= 0)
139 BIO_set_retry_write(b);
141 ret = BIO_write(b->next_bio, in, inl);
143 BIO_copy_next_retry(b);
150 static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
154 if (b->next_bio == NULL)
157 case BIO_C_DO_STATE_MACHINE:
158 BIO_clear_retry_flags(b);
159 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
160 BIO_copy_next_retry(b);
166 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
172 static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
176 if (b->next_bio == NULL)
180 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
186 static int nbiof_gets(BIO *bp, char *buf, int size)
188 if (bp->next_bio == NULL)
190 return BIO_gets(bp->next_bio, buf, size);
193 static int nbiof_puts(BIO *bp, const char *str)
195 if (bp->next_bio == NULL)
197 return BIO_puts(bp->next_bio, str);