Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

305 строки
7.3 KiB

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