1 /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */
3 /* *** Not yet finished (or even tested). *** */
5 /* Special method for a BIO where the other endpoint is also a BIO
6 * of this kind, handled by the same thread.
7 * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces
8 * for which no specific BIO method is available. */
13 #include <openssl/bio.h>
14 #include <openssl/err.h>
15 #include <openssl/crypto.h>
17 static int bio_new(BIO *bio);
18 static int bio_free(BIO *bio);
19 static int bio_read(BIO *bio, char *buf, int size);
20 static int bio_write(BIO *bio, char *buf, int num);
21 static long bio_ctrl(BIO *bio, int cmd, long num, char *ptr);
22 static int bio_puts(BIO *bio, char *str);
24 static int bio_make_pair(BIO *bio1, BIO *bio2);
25 static void bio_destroy_pair(BIO *bio);
27 static BIO_METHOD methods_biop =
34 NULL /* no bio_gets */,
40 BIO_METHOD *BIO_s_bio(void)
47 BIO *peer; /* NULL if buf == NULL.
48 * If peer != NULL, then peer->ptr is also a bio_bio_st,
49 * and its "peer" member points back to us.
50 * peer != NULL iff init != 0 in the BIO. */
52 /* This is for what we write (i.e. reading uses peer's struct): */
53 int closed; /* valid iff peer != NULL */
54 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
55 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
57 char *buf; /* "size" elements (if != NULL) */
59 size_t request; /* valid iff peer != NULL; 0 if len != 0;
60 * otherwise set by peer to number of bytes
61 * it (unsuccesfully) tried to read. */
64 static int bio_new(BIO *bio)
68 b = Malloc(sizeof *b);
73 b->size = 17*1024; /* enough for one TLS record (just a default) */
80 static int bio_free(BIO *bio)
91 bio_destroy_pair(bio);
105 static int bio_read(BIO *bio, char *buf, int size)
111 static int bio_write(BIO *bio, char *buf, int num)
117 static long bio_ctrl(BIO *bio, int cmd, long num, char *ptr)
120 struct bio_bio_st *b = bio->ptr;
126 /* XXX Additional commands: */
127 /* - Set buffer size */
130 /* - get number of bytes that the next write will accept */
131 /* - get number of bytes requested by peer */
143 case BIO_CTRL_GET_CLOSE:
147 case BIO_CTRL_SET_CLOSE:
148 bio->shutdown = (int) num;
152 case BIO_CTRL_PENDING:
155 struct bio_bio_st *peer_b =b->peer->ptr;
157 ret = (long) peer_b->len;
163 case BIO_CTRL_WPENDING:
185 static int bio_puts(BIO *bio, char *str)
187 return bio_write(bio, str, strlen(str));
192 static int bio_make_pair(BIO *bio1, BIO *bio2)
194 struct bio_bio_st *b1, *b2;
196 assert(bio1 != NULL);
197 assert(bio2 != NULL);
202 if (b1->peer != NULL || b2->peer != NULL)
204 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
210 b1->buf = Malloc(b1->size);
213 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
222 b2->buf = Malloc(b2->size);
225 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
245 static void bio_destroy_pair(BIO *bio)
247 struct bio_bio_st *b = bio->ptr;
251 BIO *peer_bio = b->peer;
253 if (peer_bio != NULL)
255 struct bio_bio_st *peer_b = peer_bio->ptr;
257 assert(peer_b != NULL);
258 assert(peer_b->peer == bio);
262 assert(peer_b->buf != NULL);
268 assert(b->buf != NULL);