2014-08-05 07:28:57 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include "async_bio.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
#include <openssl/bio.h>
|
2014-08-05 07:28:57 +01:00
|
|
|
#include <openssl/mem.h>
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../../crypto/internal.h"
|
|
|
|
|
2015-01-31 01:08:37 +00:00
|
|
|
|
2014-08-05 07:28:57 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
extern const BIO_METHOD g_async_bio_method;
|
2014-08-05 07:28:57 +01:00
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
struct AsyncBio {
|
2014-08-11 23:43:38 +01:00
|
|
|
bool datagram;
|
2015-11-02 22:16:13 +00:00
|
|
|
bool enforce_write_quota;
|
2014-08-05 07:28:57 +01:00
|
|
|
size_t read_quota;
|
|
|
|
size_t write_quota;
|
|
|
|
};
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
AsyncBio *GetData(BIO *bio) {
|
|
|
|
if (bio->method != &g_async_bio_method) {
|
2014-08-05 07:28:57 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-02-09 17:59:46 +00:00
|
|
|
return (AsyncBio *)bio->ptr;
|
2014-08-05 07:28:57 +01:00
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
static int AsyncWrite(BIO *bio, const char *in, int inl) {
|
|
|
|
AsyncBio *a = GetData(bio);
|
2014-08-05 07:28:57 +01:00
|
|
|
if (a == NULL || bio->next_bio == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-02 22:16:13 +00:00
|
|
|
if (!a->enforce_write_quota) {
|
2014-08-11 23:43:38 +01:00
|
|
|
return BIO_write(bio->next_bio, in, inl);
|
|
|
|
}
|
|
|
|
|
2014-08-05 07:28:57 +01:00
|
|
|
BIO_clear_retry_flags(bio);
|
|
|
|
|
|
|
|
if (a->write_quota == 0) {
|
|
|
|
BIO_set_retry_write(bio);
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-08-11 23:43:38 +01:00
|
|
|
if (!a->datagram && (size_t)inl > a->write_quota) {
|
2014-08-05 07:28:57 +01:00
|
|
|
inl = a->write_quota;
|
|
|
|
}
|
|
|
|
int ret = BIO_write(bio->next_bio, in, inl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
BIO_copy_next_retry(bio);
|
|
|
|
} else {
|
2015-01-12 00:50:31 +00:00
|
|
|
a->write_quota -= (a->datagram ? 1 : ret);
|
2014-08-05 07:28:57 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
static int AsyncRead(BIO *bio, char *out, int outl) {
|
|
|
|
AsyncBio *a = GetData(bio);
|
2014-08-05 07:28:57 +01:00
|
|
|
if (a == NULL || bio->next_bio == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_clear_retry_flags(bio);
|
|
|
|
|
|
|
|
if (a->read_quota == 0) {
|
|
|
|
BIO_set_retry_read(bio);
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-08-11 23:43:38 +01:00
|
|
|
if (!a->datagram && (size_t)outl > a->read_quota) {
|
2014-08-05 07:28:57 +01:00
|
|
|
outl = a->read_quota;
|
|
|
|
}
|
|
|
|
int ret = BIO_read(bio->next_bio, out, outl);
|
|
|
|
if (ret <= 0) {
|
|
|
|
BIO_copy_next_retry(bio);
|
|
|
|
} else {
|
2014-08-11 23:43:38 +01:00
|
|
|
a->read_quota -= (a->datagram ? 1 : ret);
|
2014-08-05 07:28:57 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) {
|
2014-08-05 07:28:57 +01:00
|
|
|
if (bio->next_bio == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
BIO_clear_retry_flags(bio);
|
|
|
|
int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr);
|
|
|
|
BIO_copy_next_retry(bio);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
static int AsyncNew(BIO *bio) {
|
|
|
|
AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a));
|
2014-08-05 07:28:57 +01:00
|
|
|
if (a == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(a, 0, sizeof(*a));
|
2015-11-02 22:16:13 +00:00
|
|
|
a->enforce_write_quota = true;
|
2014-08-05 07:28:57 +01:00
|
|
|
bio->init = 1;
|
|
|
|
bio->ptr = (char *)a;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
static int AsyncFree(BIO *bio) {
|
2014-08-05 07:28:57 +01:00
|
|
|
if (bio == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
OPENSSL_free(bio->ptr);
|
|
|
|
bio->ptr = NULL;
|
|
|
|
bio->init = 0;
|
|
|
|
bio->flags = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
|
2014-08-05 07:28:57 +01:00
|
|
|
if (bio->next_bio == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return BIO_callback_ctrl(bio->next_bio, cmd, fp);
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
const BIO_METHOD g_async_bio_method = {
|
2014-08-05 07:28:57 +01:00
|
|
|
BIO_TYPE_FILTER,
|
|
|
|
"async bio",
|
2015-02-09 17:59:46 +00:00
|
|
|
AsyncWrite,
|
|
|
|
AsyncRead,
|
2014-08-05 07:28:57 +01:00
|
|
|
NULL /* puts */,
|
|
|
|
NULL /* gets */,
|
2015-02-09 17:59:46 +00:00
|
|
|
AsyncCtrl,
|
|
|
|
AsyncNew,
|
|
|
|
AsyncFree,
|
|
|
|
AsyncCallbackCtrl,
|
2014-08-05 07:28:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<BIO> AsyncBioCreate() {
|
|
|
|
return bssl::UniquePtr<BIO>(BIO_new(&g_async_bio_method));
|
2014-08-05 07:28:57 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<BIO> AsyncBioCreateDatagram() {
|
|
|
|
bssl::UniquePtr<BIO> ret(BIO_new(&g_async_bio_method));
|
2014-08-11 23:43:38 +01:00
|
|
|
if (!ret) {
|
2015-02-09 07:37:18 +00:00
|
|
|
return nullptr;
|
2014-08-11 23:43:38 +01:00
|
|
|
}
|
2015-02-09 17:59:46 +00:00
|
|
|
GetData(ret.get())->datagram = true;
|
2014-08-11 23:43:38 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
void AsyncBioAllowRead(BIO *bio, size_t count) {
|
|
|
|
AsyncBio *a = GetData(bio);
|
2014-08-05 07:28:57 +01:00
|
|
|
if (a == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-11 23:43:38 +01:00
|
|
|
a->read_quota += count;
|
2014-08-05 07:28:57 +01:00
|
|
|
}
|
|
|
|
|
2015-02-09 17:59:46 +00:00
|
|
|
void AsyncBioAllowWrite(BIO *bio, size_t count) {
|
|
|
|
AsyncBio *a = GetData(bio);
|
2014-08-05 07:28:57 +01:00
|
|
|
if (a == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-11 23:43:38 +01:00
|
|
|
a->write_quota += count;
|
2014-08-05 07:28:57 +01:00
|
|
|
}
|
2015-11-02 22:16:13 +00:00
|
|
|
|
|
|
|
void AsyncBioEnforceWriteQuota(BIO *bio, bool enforce) {
|
|
|
|
AsyncBio *a = GetData(bio);
|
|
|
|
if (a == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
a->enforce_write_quota = enforce;
|
|
|
|
}
|