Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 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. |
| 6 | * |
| 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. */ |
| 14 | |
| 15 | #include "async_bio.h" |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <string.h> |
| 19 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 20 | #include <openssl/bio.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 21 | #include <openssl/mem.h> |
| 22 | |
| 23 | |
| 24 | namespace { |
| 25 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 26 | extern const BIO_METHOD g_async_bio_method; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 27 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 28 | struct AsyncBio { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 29 | bool datagram; |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 30 | bool enforce_write_quota; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 31 | size_t read_quota; |
| 32 | size_t write_quota; |
| 33 | }; |
| 34 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 35 | AsyncBio *GetData(BIO *bio) { |
| 36 | if (bio->method != &g_async_bio_method) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 37 | return NULL; |
| 38 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 39 | return (AsyncBio *)bio->ptr; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 42 | static int AsyncWrite(BIO *bio, const char *in, int inl) { |
| 43 | AsyncBio *a = GetData(bio); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 44 | if (a == NULL || bio->next_bio == NULL) { |
| 45 | return 0; |
| 46 | } |
| 47 | |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 48 | if (!a->enforce_write_quota) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 49 | return BIO_write(bio->next_bio, in, inl); |
| 50 | } |
| 51 | |
| 52 | BIO_clear_retry_flags(bio); |
| 53 | |
| 54 | if (a->write_quota == 0) { |
| 55 | BIO_set_retry_write(bio); |
| 56 | errno = EAGAIN; |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | if (!a->datagram && (size_t)inl > a->write_quota) { |
| 61 | inl = a->write_quota; |
| 62 | } |
| 63 | int ret = BIO_write(bio->next_bio, in, inl); |
| 64 | if (ret <= 0) { |
| 65 | BIO_copy_next_retry(bio); |
| 66 | } else { |
| 67 | a->write_quota -= (a->datagram ? 1 : ret); |
| 68 | } |
| 69 | return ret; |
| 70 | } |
| 71 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 72 | static int AsyncRead(BIO *bio, char *out, int outl) { |
| 73 | AsyncBio *a = GetData(bio); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 74 | if (a == NULL || bio->next_bio == NULL) { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | BIO_clear_retry_flags(bio); |
| 79 | |
| 80 | if (a->read_quota == 0) { |
| 81 | BIO_set_retry_read(bio); |
| 82 | errno = EAGAIN; |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | if (!a->datagram && (size_t)outl > a->read_quota) { |
| 87 | outl = a->read_quota; |
| 88 | } |
| 89 | int ret = BIO_read(bio->next_bio, out, outl); |
| 90 | if (ret <= 0) { |
| 91 | BIO_copy_next_retry(bio); |
| 92 | } else { |
| 93 | a->read_quota -= (a->datagram ? 1 : ret); |
| 94 | } |
| 95 | return ret; |
| 96 | } |
| 97 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 98 | static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 99 | if (bio->next_bio == NULL) { |
| 100 | return 0; |
| 101 | } |
| 102 | BIO_clear_retry_flags(bio); |
| 103 | int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 104 | BIO_copy_next_retry(bio); |
| 105 | return ret; |
| 106 | } |
| 107 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 108 | static int AsyncNew(BIO *bio) { |
| 109 | AsyncBio *a = (AsyncBio *)OPENSSL_malloc(sizeof(*a)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 110 | if (a == NULL) { |
| 111 | return 0; |
| 112 | } |
| 113 | memset(a, 0, sizeof(*a)); |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 114 | a->enforce_write_quota = true; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 115 | bio->init = 1; |
| 116 | bio->ptr = (char *)a; |
| 117 | return 1; |
| 118 | } |
| 119 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 120 | static int AsyncFree(BIO *bio) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 121 | if (bio == NULL) { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | OPENSSL_free(bio->ptr); |
| 126 | bio->ptr = NULL; |
| 127 | bio->init = 0; |
| 128 | bio->flags = 0; |
| 129 | return 1; |
| 130 | } |
| 131 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 132 | static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 133 | if (bio->next_bio == NULL) { |
| 134 | return 0; |
| 135 | } |
| 136 | return BIO_callback_ctrl(bio->next_bio, cmd, fp); |
| 137 | } |
| 138 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 139 | const BIO_METHOD g_async_bio_method = { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 140 | BIO_TYPE_FILTER, |
| 141 | "async bio", |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 142 | AsyncWrite, |
| 143 | AsyncRead, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 144 | NULL /* puts */, |
| 145 | NULL /* gets */, |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 146 | AsyncCtrl, |
| 147 | AsyncNew, |
| 148 | AsyncFree, |
| 149 | AsyncCallbackCtrl, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | } // namespace |
| 153 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 154 | bssl::UniquePtr<BIO> AsyncBioCreate() { |
| 155 | return bssl::UniquePtr<BIO>(BIO_new(&g_async_bio_method)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 156 | } |
| 157 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 158 | bssl::UniquePtr<BIO> AsyncBioCreateDatagram() { |
| 159 | bssl::UniquePtr<BIO> ret(BIO_new(&g_async_bio_method)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 160 | if (!ret) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 161 | return nullptr; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 163 | GetData(ret.get())->datagram = true; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 167 | void AsyncBioAllowRead(BIO *bio, size_t count) { |
| 168 | AsyncBio *a = GetData(bio); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 169 | if (a == NULL) { |
| 170 | return; |
| 171 | } |
| 172 | a->read_quota += count; |
| 173 | } |
| 174 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 175 | void AsyncBioAllowWrite(BIO *bio, size_t count) { |
| 176 | AsyncBio *a = GetData(bio); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 177 | if (a == NULL) { |
| 178 | return; |
| 179 | } |
| 180 | a->write_quota += count; |
| 181 | } |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 182 | |
| 183 | void AsyncBioEnforceWriteQuota(BIO *bio, bool enforce) { |
| 184 | AsyncBio *a = GetData(bio); |
| 185 | if (a == NULL) { |
| 186 | return; |
| 187 | } |
| 188 | a->enforce_write_quota = enforce; |
| 189 | } |