| 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 "packeted_bio.h" |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <limits.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <openssl/mem.h> |
| 23 |
|
| 24 |
|
| 25 |
namespace { |
| 26 |
|
| 27 |
extern const BIO_METHOD g_packeted_bio_method; |
| 28 |
|
| 29 |
const uint8_t kOpcodePacket = 'P'; |
| 30 |
const uint8_t kOpcodeTimeout = 'T'; |
| 31 |
const uint8_t kOpcodeTimeoutAck = 't'; |
| 32 |
|
| 33 |
struct PacketedBio { |
| 34 |
explicit PacketedBio(bool advance_clock_arg) |
| 35 |
: advance_clock(advance_clock_arg) { |
| 36 |
memset(&timeout, 0, sizeof(timeout)); |
| 37 |
memset(&clock, 0, sizeof(clock)); |
| 38 |
memset(&read_deadline, 0, sizeof(read_deadline)); |
| 39 |
} |
| 40 |
|
| 41 |
bool HasTimeout() const { |
| 42 |
return timeout.tv_sec != 0 || timeout.tv_usec != 0; |
| 43 |
} |
| 44 |
|
| 45 |
bool CanRead() const { |
| 46 |
if (read_deadline.tv_sec == 0 && read_deadline.tv_usec == 0) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
if (clock.tv_sec == read_deadline.tv_sec) { |
| 51 |
return clock.tv_usec < read_deadline.tv_usec; |
| 52 |
} |
| 53 |
return clock.tv_sec < read_deadline.tv_sec; |
| 54 |
} |
| 55 |
|
| 56 |
timeval timeout; |
| 57 |
timeval clock; |
| 58 |
timeval read_deadline; |
| 59 |
bool advance_clock; |
| 60 |
}; |
| 61 |
|
| 62 |
PacketedBio *GetData(BIO *bio) { |
| 63 |
if (bio->method != &g_packeted_bio_method) { |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
return (PacketedBio *)bio->ptr; |
| 67 |
} |
| 68 |
|
| 69 |
const PacketedBio *GetData(const BIO *bio) { |
| 70 |
return GetData(const_cast<BIO*>(bio)); |
| 71 |
} |
| 72 |
|
| 73 |
// ReadAll reads |len| bytes from |bio| into |out|. It returns 1 on success and |
| 74 |
// 0 or -1 on error. |
| 75 |
static int ReadAll(BIO *bio, uint8_t *out, size_t len) { |
| 76 |
while (len > 0) { |
| 77 |
int chunk_len = INT_MAX; |
| 78 |
if (len <= INT_MAX) { |
| 79 |
chunk_len = (int)len; |
| 80 |
} |
| 81 |
int ret = BIO_read(bio, out, chunk_len); |
| 82 |
if (ret <= 0) { |
| 83 |
return ret; |
| 84 |
} |
| 85 |
out += ret; |
| 86 |
len -= ret; |
| 87 |
} |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
static int PacketedWrite(BIO *bio, const char *in, int inl) { |
| 92 |
if (bio->next_bio == NULL) { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
BIO_clear_retry_flags(bio); |
| 97 |
|
| 98 |
// Write the header. |
| 99 |
uint8_t header[5]; |
| 100 |
header[0] = kOpcodePacket; |
| 101 |
header[1] = (inl >> 24) & 0xff; |
| 102 |
header[2] = (inl >> 16) & 0xff; |
| 103 |
header[3] = (inl >> 8) & 0xff; |
| 104 |
header[4] = inl & 0xff; |
| 105 |
int ret = BIO_write(bio->next_bio, header, sizeof(header)); |
| 106 |
if (ret <= 0) { |
| 107 |
BIO_copy_next_retry(bio); |
| 108 |
return ret; |
| 109 |
} |
| 110 |
|
| 111 |
// Write the buffer. |
| 112 |
ret = BIO_write(bio->next_bio, in, inl); |
| 113 |
if (ret < 0 || (inl > 0 && ret == 0)) { |
| 114 |
BIO_copy_next_retry(bio); |
| 115 |
return ret; |
| 116 |
} |
| 117 |
assert(ret == inl); |
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
static int PacketedRead(BIO *bio, char *out, int outl) { |
| 122 |
PacketedBio *data = GetData(bio); |
| 123 |
if (bio->next_bio == NULL) { |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
BIO_clear_retry_flags(bio); |
| 128 |
|
| 129 |
for (;;) { |
| 130 |
// Check if the read deadline has passed. |
| 131 |
if (!data->CanRead()) { |
| 132 |
BIO_set_retry_read(bio); |
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
// Read the opcode. |
| 137 |
uint8_t opcode; |
| 138 |
int ret = ReadAll(bio->next_bio, &opcode, sizeof(opcode)); |
| 139 |
if (ret <= 0) { |
| 140 |
BIO_copy_next_retry(bio); |
| 141 |
return ret; |
| 142 |
} |
| 143 |
|
| 144 |
if (opcode == kOpcodeTimeout) { |
| 145 |
// The caller is required to advance any pending timeouts before |
| 146 |
// continuing. |
| 147 |
if (data->HasTimeout()) { |
| 148 |
fprintf(stderr, "Unprocessed timeout!\n"); |
| 149 |
return -1; |
| 150 |
} |
| 151 |
|
| 152 |
// Process the timeout. |
| 153 |
uint8_t buf[8]; |
| 154 |
ret = ReadAll(bio->next_bio, buf, sizeof(buf)); |
| 155 |
if (ret <= 0) { |
| 156 |
BIO_copy_next_retry(bio); |
| 157 |
return ret; |
| 158 |
} |
| 159 |
uint64_t timeout = (static_cast<uint64_t>(buf[0]) << 56) | |
| 160 |
(static_cast<uint64_t>(buf[1]) << 48) | |
| 161 |
(static_cast<uint64_t>(buf[2]) << 40) | |
| 162 |
(static_cast<uint64_t>(buf[3]) << 32) | |
| 163 |
(static_cast<uint64_t>(buf[4]) << 24) | |
| 164 |
(static_cast<uint64_t>(buf[5]) << 16) | |
| 165 |
(static_cast<uint64_t>(buf[6]) << 8) | |
| 166 |
static_cast<uint64_t>(buf[7]); |
| 167 |
timeout /= 1000; // Convert nanoseconds to microseconds. |
| 168 |
|
| 169 |
data->timeout.tv_usec = timeout % 1000000; |
| 170 |
data->timeout.tv_sec = timeout / 1000000; |
| 171 |
|
| 172 |
// Send an ACK to the peer. |
| 173 |
ret = BIO_write(bio->next_bio, &kOpcodeTimeoutAck, 1); |
| 174 |
if (ret <= 0) { |
| 175 |
return ret; |
| 176 |
} |
| 177 |
assert(ret == 1); |
| 178 |
|
| 179 |
if (!data->advance_clock) { |
| 180 |
// Signal to the caller to retry the read, after advancing the clock. |
| 181 |
BIO_set_retry_read(bio); |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
PacketedBioAdvanceClock(bio); |
| 186 |
continue; |
| 187 |
} |
| 188 |
|
| 189 |
if (opcode != kOpcodePacket) { |
| 190 |
fprintf(stderr, "Unknown opcode, %u\n", opcode); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
// Read the length prefix. |
| 195 |
uint8_t len_bytes[4]; |
| 196 |
ret = ReadAll(bio->next_bio, len_bytes, sizeof(len_bytes)); |
| 197 |
if (ret <= 0) { |
| 198 |
BIO_copy_next_retry(bio); |
| 199 |
return ret; |
| 200 |
} |
| 201 |
|
| 202 |
uint32_t len = (len_bytes[0] << 24) | (len_bytes[1] << 16) | |
| 203 |
(len_bytes[2] << 8) | len_bytes[3]; |
| 204 |
uint8_t *buf = (uint8_t *)OPENSSL_malloc(len); |
| 205 |
if (buf == NULL) { |
| 206 |
return -1; |
| 207 |
} |
| 208 |
ret = ReadAll(bio->next_bio, buf, len); |
| 209 |
if (ret <= 0) { |
| 210 |
fprintf(stderr, "Packeted BIO was truncated\n"); |
| 211 |
return -1; |
| 212 |
} |
| 213 |
|
| 214 |
if (outl > (int)len) { |
| 215 |
outl = len; |
| 216 |
} |
| 217 |
memcpy(out, buf, outl); |
| 218 |
OPENSSL_free(buf); |
| 219 |
return outl; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
static long PacketedCtrl(BIO *bio, int cmd, long num, void *ptr) { |
| 224 |
if (cmd == BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT) { |
| 225 |
memcpy(&GetData(bio)->read_deadline, ptr, sizeof(timeval)); |
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
if (bio->next_bio == NULL) { |
| 230 |
return 0; |
| 231 |
} |
| 232 |
BIO_clear_retry_flags(bio); |
| 233 |
int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 234 |
BIO_copy_next_retry(bio); |
| 235 |
return ret; |
| 236 |
} |
| 237 |
|
| 238 |
static int PacketedNew(BIO *bio) { |
| 239 |
bio->init = 1; |
| 240 |
return 1; |
| 241 |
} |
| 242 |
|
| 243 |
static int PacketedFree(BIO *bio) { |
| 244 |
if (bio == NULL) { |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
delete GetData(bio); |
| 249 |
bio->init = 0; |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
static long PacketedCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) { |
| 254 |
if (bio->next_bio == NULL) { |
| 255 |
return 0; |
| 256 |
} |
| 257 |
return BIO_callback_ctrl(bio->next_bio, cmd, fp); |
| 258 |
} |
| 259 |
|
| 260 |
const BIO_METHOD g_packeted_bio_method = { |
| 261 |
BIO_TYPE_FILTER, |
| 262 |
"packeted bio", |
| 263 |
PacketedWrite, |
| 264 |
PacketedRead, |
| 265 |
NULL /* puts */, |
| 266 |
NULL /* gets */, |
| 267 |
PacketedCtrl, |
| 268 |
PacketedNew, |
| 269 |
PacketedFree, |
| 270 |
PacketedCallbackCtrl, |
| 271 |
}; |
| 272 |
|
| 273 |
} // namespace |
| 274 |
|
| 275 |
ScopedBIO PacketedBioCreate(bool advance_clock) { |
| 276 |
ScopedBIO bio(BIO_new(&g_packeted_bio_method)); |
| 277 |
if (!bio) { |
| 278 |
return nullptr; |
| 279 |
} |
| 280 |
bio->ptr = new PacketedBio(advance_clock); |
| 281 |
return bio; |
| 282 |
} |
| 283 |
|
| 284 |
timeval PacketedBioGetClock(const BIO *bio) { |
| 285 |
return GetData(bio)->clock; |
| 286 |
} |
| 287 |
|
| 288 |
bool PacketedBioAdvanceClock(BIO *bio) { |
| 289 |
PacketedBio *data = GetData(bio); |
| 290 |
if (data == nullptr) { |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
if (!data->HasTimeout()) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
data->clock.tv_usec += data->timeout.tv_usec; |
| 299 |
data->clock.tv_sec += data->clock.tv_usec / 1000000; |
| 300 |
data->clock.tv_usec %= 1000000; |
| 301 |
data->clock.tv_sec += data->timeout.tv_sec; |
| 302 |
memset(&data->timeout, 0, sizeof(data->timeout)); |
| 303 |
return true; |
| 304 |
} |