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/crypto.h>
27 bool enforce_write_quota;
32 AsyncBio *GetData(BIO *bio) {
34 * TODO: Missing accessor? This probably needs a BIO_get_method() in OpenSSL
35 * For now skip this check
38 if (bio->method != &g_async_bio_method) {
42 return (AsyncBio *)BIO_get_data(bio);
45 static int AsyncWrite(BIO *bio, const char *in, int inl) {
46 AsyncBio *a = GetData(bio);
47 if (a == NULL || BIO_next(bio) == NULL) {
51 if (!a->enforce_write_quota) {
52 return BIO_write(BIO_next(bio), in, inl);
55 BIO_clear_retry_flags(bio);
57 if (a->write_quota == 0) {
58 BIO_set_retry_write(bio);
63 if (!a->datagram && (size_t)inl > a->write_quota) {
66 int ret = BIO_write(BIO_next(bio), in, inl);
68 BIO_copy_next_retry(bio);
70 a->write_quota -= (a->datagram ? 1 : ret);
75 static int AsyncRead(BIO *bio, char *out, int outl) {
76 AsyncBio *a = GetData(bio);
77 if (a == NULL || BIO_next(bio) == NULL) {
81 BIO_clear_retry_flags(bio);
83 if (a->read_quota == 0) {
84 BIO_set_retry_read(bio);
89 if (!a->datagram && (size_t)outl > a->read_quota) {
92 int ret = BIO_read(BIO_next(bio), out, outl);
94 BIO_copy_next_retry(bio);
96 a->read_quota -= (a->datagram ? 1 : ret);
101 static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) {
102 if (BIO_next(bio) == NULL) {
105 BIO_clear_retry_flags(bio);
106 int ret = BIO_ctrl(BIO_next(bio), cmd, num, ptr);
107 BIO_copy_next_retry(bio);
111 static int AsyncNew(BIO *bio) {
112 AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a));
116 memset(a, 0, sizeof(*a));
117 a->enforce_write_quota = true;
118 BIO_set_init(bio, 1);
119 BIO_set_data(bio, a);
123 static int AsyncFree(BIO *bio) {
128 OPENSSL_free(BIO_get_data(bio));
129 BIO_set_data(bio, NULL);
130 BIO_set_init(bio, 0);
134 static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
135 if (BIO_next(bio) == NULL) {
138 return BIO_callback_ctrl(BIO_next(bio), cmd, fp);
141 static BIO_METHOD *g_async_bio_method = NULL;
143 static const BIO_METHOD *AsyncMethod(void)
145 if (g_async_bio_method == NULL) {
146 g_async_bio_method = BIO_meth_new(BIO_TYPE_FILTER, "async bio");
147 if ( g_async_bio_method == NULL
148 || !BIO_meth_set_write(g_async_bio_method, AsyncWrite)
149 || !BIO_meth_set_read(g_async_bio_method, AsyncRead)
150 || !BIO_meth_set_ctrl(g_async_bio_method, AsyncCtrl)
151 || !BIO_meth_set_create(g_async_bio_method, AsyncNew)
152 || !BIO_meth_set_destroy(g_async_bio_method, AsyncFree)
153 || !BIO_meth_set_callback_ctrl(g_async_bio_method, AsyncCallbackCtrl))
156 return g_async_bio_method;
161 ScopedBIO AsyncBioCreate() {
162 return ScopedBIO(BIO_new(AsyncMethod()));
165 ScopedBIO AsyncBioCreateDatagram() {
166 ScopedBIO ret(BIO_new(AsyncMethod()));
170 GetData(ret.get())->datagram = true;
174 void AsyncBioAllowRead(BIO *bio, size_t count) {
175 AsyncBio *a = GetData(bio);
179 a->read_quota += count;
182 void AsyncBioAllowWrite(BIO *bio, size_t count) {
183 AsyncBio *a = GetData(bio);
187 a->write_quota += count;
190 void AsyncBioEnforceWriteQuota(BIO *bio, bool enforce) {
191 AsyncBio *a = GetData(bio);
195 a->enforce_write_quota = enforce;