1 /* Copyright (c) 2014, Google Inc.
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
15 #include "async_bio.h"
20 #include <openssl/bio.h>
21 #include <openssl/crypto.h>
28 bool enforce_write_quota;
33 AsyncBio *GetData(BIO *bio) {
34 return (AsyncBio *)BIO_get_data(bio);
37 static int AsyncWrite(BIO *bio, const char *in, int inl) {
38 AsyncBio *a = GetData(bio);
39 if (a == NULL || BIO_next(bio) == NULL) {
43 if (!a->enforce_write_quota) {
44 return BIO_write(BIO_next(bio), in, inl);
47 BIO_clear_retry_flags(bio);
49 if (a->write_quota == 0) {
50 BIO_set_retry_write(bio);
55 if (!a->datagram && (size_t)inl > a->write_quota) {
58 int ret = BIO_write(BIO_next(bio), in, inl);
60 BIO_copy_next_retry(bio);
62 a->write_quota -= (a->datagram ? 1 : ret);
67 static int AsyncRead(BIO *bio, char *out, int outl) {
68 AsyncBio *a = GetData(bio);
69 if (a == NULL || BIO_next(bio) == NULL) {
73 BIO_clear_retry_flags(bio);
75 if (a->read_quota == 0) {
76 BIO_set_retry_read(bio);
81 if (!a->datagram && (size_t)outl > a->read_quota) {
84 int ret = BIO_read(BIO_next(bio), out, outl);
86 BIO_copy_next_retry(bio);
88 a->read_quota -= (a->datagram ? 1 : ret);
93 static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) {
94 if (BIO_next(bio) == NULL) {
97 BIO_clear_retry_flags(bio);
98 int ret = BIO_ctrl(BIO_next(bio), cmd, num, ptr);
99 BIO_copy_next_retry(bio);
103 static int AsyncNew(BIO *bio) {
104 AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a));
108 memset(a, 0, sizeof(*a));
109 a->enforce_write_quota = true;
110 BIO_set_init(bio, 1);
111 BIO_set_data(bio, a);
115 static int AsyncFree(BIO *bio) {
120 OPENSSL_free(BIO_get_data(bio));
121 BIO_set_data(bio, NULL);
122 BIO_set_init(bio, 0);
126 static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
127 if (BIO_next(bio) == NULL) {
130 return BIO_callback_ctrl(BIO_next(bio), cmd, fp);
133 static BIO_METHOD *g_async_bio_method = NULL;
135 static const BIO_METHOD *AsyncMethod(void)
137 if (g_async_bio_method == NULL) {
138 g_async_bio_method = BIO_meth_new(BIO_TYPE_FILTER, "async bio");
139 if ( g_async_bio_method == NULL
140 || !BIO_meth_set_write(g_async_bio_method, AsyncWrite)
141 || !BIO_meth_set_read(g_async_bio_method, AsyncRead)
142 || !BIO_meth_set_ctrl(g_async_bio_method, AsyncCtrl)
143 || !BIO_meth_set_create(g_async_bio_method, AsyncNew)
144 || !BIO_meth_set_destroy(g_async_bio_method, AsyncFree)
145 || !BIO_meth_set_callback_ctrl(g_async_bio_method, AsyncCallbackCtrl))
148 return g_async_bio_method;
153 bssl::UniquePtr<BIO> AsyncBioCreate() {
154 return bssl::UniquePtr<BIO>(BIO_new(AsyncMethod()));
157 bssl::UniquePtr<BIO> AsyncBioCreateDatagram() {
158 bssl::UniquePtr<BIO> ret(BIO_new(AsyncMethod()));
162 GetData(ret.get())->datagram = true;
166 void AsyncBioAllowRead(BIO *bio, size_t count) {
167 AsyncBio *a = GetData(bio);
171 a->read_quota += count;
174 void AsyncBioAllowWrite(BIO *bio, size_t count) {
175 AsyncBio *a = GetData(bio);
179 a->write_quota += count;
182 void AsyncBioEnforceWriteQuota(BIO *bio, bool enforce) {
183 AsyncBio *a = GetData(bio);
187 a->enforce_write_quota = enforce;