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) */
60 static int bio_new(BIO *bio)
64 b = Malloc(sizeof *b);
69 b->size = 17*1024; /* enough for one TLS record (just a default) */
76 static int bio_free(BIO *bio)
87 bio_destroy_pair(bio);
101 static int bio_read(BIO *bio, char *buf, int size)
107 static int bio_write(BIO *bio, char *buf, int num)
113 static long bio_ctrl(BIO *bio, int cmd, long num, char *ptr)
116 struct bio_bio_st *b = bio->ptr;
122 /* XXX Additional commands: */
123 /* - Set buffer size */
126 /* - get number of bytes that the next write will accept */
138 case BIO_CTRL_GET_CLOSE:
142 case BIO_CTRL_SET_CLOSE:
143 bio->shutdown = (int) num;
147 case BIO_CTRL_PENDING:
150 struct bio_bio_st *peer_b =b->peer->ptr;
152 ret = (long) peer_b->len;
158 case BIO_CTRL_WPENDING:
178 static int bio_puts(BIO *bio, char *str)
180 return bio_write(bio, str, strlen(str));
185 static int bio_make_pair(BIO *bio1, BIO *bio2)
187 struct bio_bio_st *b1, *b2;
189 assert(bio1 != NULL);
190 assert(bio2 != NULL);
195 if (b1->peer != NULL || b2->peer != NULL)
197 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
203 b1->buf = Malloc(b1->size);
206 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
215 b2->buf = Malloc(b2->size);
218 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
234 static void bio_destroy_pair(BIO *bio)
236 struct bio_bio_st *b = bio->ptr;
240 BIO *peer_bio = b->peer;
242 if (peer_bio != NULL)
244 struct bio_bio_st *peer_b = peer_bio->ptr;
246 assert(peer_b != NULL);
247 assert(peer_b->peer == bio);
251 assert(peer_b->buf != NULL);
257 assert(b->buf != NULL);