]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bf_nbio.c
2 * Copyright 1995-2018 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",
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
) {
61 BIOerr(BIO_F_NBIOF_NEW
, ERR_R_MALLOC_FAILURE
);
71 static int nbiof_free(BIO
*a
)
82 static int nbiof_read(BIO
*b
, char *out
, int outl
)
90 if (b
->next_bio
== NULL
)
93 BIO_clear_retry_flags(b
);
94 if (RAND_priv_bytes(&n
, 1) <= 0)
103 BIO_set_retry_read(b
);
105 ret
= BIO_read(b
->next_bio
, out
, outl
);
107 BIO_copy_next_retry(b
);
112 static int nbiof_write(BIO
*b
, const char *in
, int inl
)
119 if ((in
== NULL
) || (inl
<= 0))
121 if (b
->next_bio
== NULL
)
123 nt
= (NBIO_TEST
*)b
->ptr
;
125 BIO_clear_retry_flags(b
);
131 if (RAND_priv_bytes(&n
, 1) <= 0)
141 BIO_set_retry_write(b
);
143 ret
= BIO_write(b
->next_bio
, in
, inl
);
145 BIO_copy_next_retry(b
);
152 static long nbiof_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
156 if (b
->next_bio
== NULL
)
159 case BIO_C_DO_STATE_MACHINE
:
160 BIO_clear_retry_flags(b
);
161 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
162 BIO_copy_next_retry(b
);
168 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
174 static long nbiof_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
178 if (b
->next_bio
== NULL
)
182 ret
= BIO_callback_ctrl(b
->next_bio
, cmd
, fp
);
188 static int nbiof_gets(BIO
*bp
, char *buf
, int size
)
190 if (bp
->next_bio
== NULL
)
192 return BIO_gets(bp
->next_bio
, buf
, size
);
195 static int nbiof_puts(BIO
*bp
, const char *str
)
197 if (bp
->next_bio
== NULL
)
199 return BIO_puts(bp
->next_bio
, str
);