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. */
51 /* This is for what we write (i.e. reading uses peer's struct): */
52 int closed; /* valid iff peer != NULL */
53 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
54 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
56 char *buf; /* "size" elements (if != NULL) */
59 static int bio_new(BIO *bio)
63 b = Malloc(sizeof *b);
68 b->size = 17*1024; /* enough for one TLS record (just a default) */
75 static int bio_free(BIO *bio)
86 bio_destroy_pair(bio);
100 static int bio_read(BIO *bio, char *buf, int size)
106 static int bio_write(BIO *bio, char *buf, int num)
112 static long bio_ctrl(BIO *bio, int cmd, long num, char *ptr)
115 struct bio_bio_st *b = bio->ptr;
121 /* XXX Additional commands: */
122 /* - Set buffer size */
125 /* - get number of bytes that the next write will accept */
137 case BIO_CTRL_GET_CLOSE:
141 case BIO_CTRL_SET_CLOSE:
142 bio->shutdown = (int) num;
146 case BIO_CTRL_PENDING:
149 struct bio_bio_st *peer_b =b->peer->ptr;
151 ret = (long) peer_b->len;
157 case BIO_CTRL_WPENDING:
177 static int bio_puts(BIO *bio, char *str)
179 return bio_write(bio, str, strlen(str));
184 static int bio_make_pair(BIO *bio1, BIO *bio2)
186 struct bio_bio_st *b1, *b2;
188 assert(bio1 != NULL);
189 assert(bio2 != NULL);
194 if (b1->peer != NULL || b2->peer != NULL)
196 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
202 b1->buf = Malloc(b1->size);
205 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
214 b2->buf = Malloc(b2->size);
217 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
230 static void bio_destroy_pair(BIO *bio)
232 struct bio_bio_st *b = bio->ptr;
236 BIO *peer_bio = b->peer;
238 if (peer_bio != NULL)
240 struct bio_bio_st *peer_b = peer_bio->ptr;
242 assert(peer_b != NULL);
243 assert(peer_b->peer == bio);
247 assert(peer_b->buf != NULL);
253 assert(b->buf != NULL);