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. */
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/crypto.h>
18 static int bio_new(BIO *bio);
19 static int bio_free(BIO *bio);
20 static int bio_read(BIO *bio, char *buf, int size);
21 static int bio_write(BIO *bio, char *buf, int num);
22 static long bio_ctrl(BIO *bio, int cmd, long num, char *ptr);
23 static int bio_puts(BIO *bio, char *str);
25 static int bio_make_pair(BIO *bio1, BIO *bio2);
26 static void bio_destroy_pair(BIO *bio);
28 static BIO_METHOD methods_biop =
35 NULL /* no bio_gets */,
41 BIO_METHOD *BIO_s_bio(void)
48 BIO *peer; /* NULL if buf == NULL.
49 * If peer != NULL, then peer->ptr is also a bio_bio_st,
50 * and its "peer" member points back to us.
51 * peer != NULL iff init != 0 in the BIO. */
53 /* This is for what we write (i.e. reading uses peer's struct): */
54 int closed; /* valid iff peer != NULL */
55 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
56 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
58 char *buf; /* "size" elements (if != NULL) */
60 size_t request; /* valid iff peer != NULL; 0 if len != 0;
61 * otherwise set by peer to number of bytes
62 * it (unsuccesfully) tried to read. */
65 static int bio_new(BIO *bio)
69 b = Malloc(sizeof *b);
74 b->size = 17*1024; /* enough for one TLS record (just a default) */
81 static int bio_free(BIO *bio)
92 bio_destroy_pair(bio);
106 static int bio_read(BIO *bio, char *buf, int size)
112 static int bio_write(BIO *bio, char *buf, int num)
118 static long bio_ctrl(BIO *bio, int cmd, long num, char *ptr)
121 struct bio_bio_st *b = bio->ptr;
127 /* XXX Additional commands: */
128 /* - Set buffer size */
131 /* - get number of bytes that the next write will accept */
132 /* - get number of bytes requested by peer */
144 case BIO_CTRL_GET_CLOSE:
148 case BIO_CTRL_SET_CLOSE:
149 bio->shutdown = (int) num;
153 case BIO_CTRL_PENDING:
156 struct bio_bio_st *peer_b =b->peer->ptr;
158 ret = (long) peer_b->len;
164 case BIO_CTRL_WPENDING:
186 static int bio_puts(BIO *bio, char *str)
188 return bio_write(bio, str, strlen(str));
191 /* Until bio_make_pair is used, make a dummy function use it for -pedantic */
192 void dummy() { bio_make_pair(NULL,NULL); }
194 static int bio_make_pair(BIO *bio1, BIO *bio2)
196 struct bio_bio_st *b1, *b2;
198 assert(bio1 != NULL);
199 assert(bio2 != NULL);
204 if (b1->peer != NULL || b2->peer != NULL)
206 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
212 b1->buf = Malloc(b1->size);
215 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
224 b2->buf = Malloc(b2->size);
227 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
247 static void bio_destroy_pair(BIO *bio)
249 struct bio_bio_st *b = bio->ptr;
253 BIO *peer_bio = b->peer;
255 if (peer_bio != NULL)
257 struct bio_bio_st *peer_b = peer_bio->ptr;
259 assert(peer_b != NULL);
260 assert(peer_b->peer == bio);
264 assert(peer_b->buf != NULL);
270 assert(b->buf != NULL);