diff options
author | 2016-04-07 10:03:04 +0200 | |
---|---|---|
committer | 2016-04-07 10:03:04 +0200 | |
commit | 31b9fa4f384115e400c0a49b35730b42d07b6ff6 (patch) | |
tree | 64aab794df9732c8430b44cb010a63195b1dc0a5 /third_party/boringssl/src/ssl/test/async_bio.cc | |
parent | 4baa56c99c14f5c637f89ba8ed641ec099d5c028 (diff) |
Add BoringSSL to third_party .
As usual, README.md contains reproduction instructions.
It would be better to directly use the source tree under `boringssl.googlesource.com`, but for the time being, this will do.
Change-Id: I72ef030251c41eaed459c4abd93228f23223783b
Diffstat (limited to 'third_party/boringssl/src/ssl/test/async_bio.cc')
-rw-r--r-- | third_party/boringssl/src/ssl/test/async_bio.cc | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/third_party/boringssl/src/ssl/test/async_bio.cc b/third_party/boringssl/src/ssl/test/async_bio.cc new file mode 100644 index 0000000000..05348457d8 --- /dev/null +++ b/third_party/boringssl/src/ssl/test/async_bio.cc @@ -0,0 +1,180 @@ +/* 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> +#include <string.h> + +#include <openssl/mem.h> + + +namespace { + +extern const BIO_METHOD g_async_bio_method; + +struct AsyncBio { + bool datagram; + size_t read_quota; + size_t write_quota; +}; + +AsyncBio *GetData(BIO *bio) { + if (bio->method != &g_async_bio_method) { + return NULL; + } + return (AsyncBio *)bio->ptr; +} + +static int AsyncWrite(BIO *bio, const char *in, int inl) { + AsyncBio *a = GetData(bio); + if (a == NULL || bio->next_bio == NULL) { + return 0; + } + + if (a->datagram) { + // Perform writes synchronously; the DTLS implementation drops any packets + // that failed to send. + return BIO_write(bio->next_bio, in, inl); + } + + BIO_clear_retry_flags(bio); + + if (a->write_quota == 0) { + BIO_set_retry_write(bio); + errno = EAGAIN; + return -1; + } + + if (!a->datagram && (size_t)inl > a->write_quota) { + inl = a->write_quota; + } + int ret = BIO_write(bio->next_bio, in, inl); + if (ret <= 0) { + BIO_copy_next_retry(bio); + } else { + a->write_quota -= (a->datagram ? 1 : ret); + } + return ret; +} + +static int AsyncRead(BIO *bio, char *out, int outl) { + AsyncBio *a = GetData(bio); + 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; + } + + if (!a->datagram && (size_t)outl > a->read_quota) { + outl = a->read_quota; + } + int ret = BIO_read(bio->next_bio, out, outl); + if (ret <= 0) { + BIO_copy_next_retry(bio); + } else { + a->read_quota -= (a->datagram ? 1 : ret); + } + return ret; +} + +static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) { + 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; +} + +static int AsyncNew(BIO *bio) { + AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a)); + if (a == NULL) { + return 0; + } + memset(a, 0, sizeof(*a)); + bio->init = 1; + bio->ptr = (char *)a; + return 1; +} + +static int AsyncFree(BIO *bio) { + if (bio == NULL) { + return 0; + } + + OPENSSL_free(bio->ptr); + bio->ptr = NULL; + bio->init = 0; + bio->flags = 0; + return 1; +} + +static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) { + if (bio->next_bio == NULL) { + return 0; + } + return BIO_callback_ctrl(bio->next_bio, cmd, fp); +} + +const BIO_METHOD g_async_bio_method = { + BIO_TYPE_FILTER, + "async bio", + AsyncWrite, + AsyncRead, + NULL /* puts */, + NULL /* gets */, + AsyncCtrl, + AsyncNew, + AsyncFree, + AsyncCallbackCtrl, +}; + +} // namespace + +ScopedBIO AsyncBioCreate() { + return ScopedBIO(BIO_new(&g_async_bio_method)); +} + +ScopedBIO AsyncBioCreateDatagram() { + ScopedBIO ret(BIO_new(&g_async_bio_method)); + if (!ret) { + return nullptr; + } + GetData(ret.get())->datagram = true; + return ret; +} + +void AsyncBioAllowRead(BIO *bio, size_t count) { + AsyncBio *a = GetData(bio); + if (a == NULL) { + return; + } + a->read_quota += count; +} + +void AsyncBioAllowWrite(BIO *bio, size_t count) { + AsyncBio *a = GetData(bio); + if (a == NULL) { + return; + } + a->write_quota += count; +} |