You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

220 lines
5.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. // ReadAll reads |len| bytes from |bio| into |out|. It returns 1 on success and
  26. // 0 or -1 on error.
  27. static int ReadAll(BIO *bio, uint8_t *out, size_t len) {
  28. while (len > 0) {
  29. int chunk_len = INT_MAX;
  30. if (len <= INT_MAX) {
  31. chunk_len = (int)len;
  32. }
  33. int ret = BIO_read(bio, out, chunk_len);
  34. if (ret <= 0) {
  35. return ret;
  36. }
  37. out += ret;
  38. len -= ret;
  39. }
  40. return 1;
  41. }
  42. static int PacketedWrite(BIO *bio, const char *in, int inl) {
  43. if (bio->next_bio == NULL) {
  44. return 0;
  45. }
  46. BIO_clear_retry_flags(bio);
  47. // Write the header.
  48. uint8_t header[5];
  49. header[0] = kOpcodePacket;
  50. header[1] = (inl >> 24) & 0xff;
  51. header[2] = (inl >> 16) & 0xff;
  52. header[3] = (inl >> 8) & 0xff;
  53. header[4] = inl & 0xff;
  54. int ret = BIO_write(bio->next_bio, header, sizeof(header));
  55. if (ret <= 0) {
  56. BIO_copy_next_retry(bio);
  57. return ret;
  58. }
  59. // Write the buffer.
  60. ret = BIO_write(bio->next_bio, in, inl);
  61. if (ret < 0 || (inl > 0 && ret == 0)) {
  62. BIO_copy_next_retry(bio);
  63. return ret;
  64. }
  65. assert(ret == inl);
  66. return ret;
  67. }
  68. static int PacketedRead(BIO *bio, char *out, int outl) {
  69. if (bio->next_bio == NULL) {
  70. return 0;
  71. }
  72. BIO_clear_retry_flags(bio);
  73. // Read the opcode.
  74. uint8_t opcode;
  75. int ret = ReadAll(bio->next_bio, &opcode, sizeof(opcode));
  76. if (ret <= 0) {
  77. BIO_copy_next_retry(bio);
  78. return ret;
  79. }
  80. if (opcode == kOpcodeTimeout) {
  81. // Process the timeout.
  82. uint8_t buf[8];
  83. ret = ReadAll(bio->next_bio, buf, sizeof(buf));
  84. if (ret <= 0) {
  85. BIO_copy_next_retry(bio);
  86. return ret;
  87. }
  88. uint64_t timeout = (static_cast<uint64_t>(buf[0]) << 56) |
  89. (static_cast<uint64_t>(buf[1]) << 48) |
  90. (static_cast<uint64_t>(buf[2]) << 40) |
  91. (static_cast<uint64_t>(buf[3]) << 32) |
  92. (static_cast<uint64_t>(buf[4]) << 24) |
  93. (static_cast<uint64_t>(buf[5]) << 16) |
  94. (static_cast<uint64_t>(buf[6]) << 8) |
  95. static_cast<uint64_t>(buf[7]);
  96. timeout /= 1000; // Convert nanoseconds to microseconds.
  97. OPENSSL_timeval *out_timeout =
  98. reinterpret_cast<OPENSSL_timeval *>(bio->ptr);
  99. assert(out_timeout->tv_usec == 0);
  100. assert(out_timeout->tv_sec == 0);
  101. out_timeout->tv_usec = timeout % 1000000;
  102. out_timeout->tv_sec = timeout / 1000000;
  103. // Send an ACK to the peer.
  104. ret = BIO_write(bio->next_bio, &kOpcodeTimeoutAck, 1);
  105. if (ret <= 0) {
  106. return ret;
  107. }
  108. assert(ret == 1);
  109. // Signal to the caller to retry the read, after processing the
  110. // new clock.
  111. BIO_set_retry_read(bio);
  112. return -1;
  113. }
  114. if (opcode != kOpcodePacket) {
  115. fprintf(stderr, "Unknown opcode, %u\n", opcode);
  116. return -1;
  117. }
  118. // Read the length prefix.
  119. uint8_t len_bytes[4];
  120. ret = ReadAll(bio->next_bio, len_bytes, sizeof(len_bytes));
  121. if (ret <= 0) {
  122. BIO_copy_next_retry(bio);
  123. return ret;
  124. }
  125. uint32_t len = (len_bytes[0] << 24) | (len_bytes[1] << 16) |
  126. (len_bytes[2] << 8) | len_bytes[3];
  127. uint8_t *buf = (uint8_t *)OPENSSL_malloc(len);
  128. if (buf == NULL) {
  129. return -1;
  130. }
  131. ret = ReadAll(bio->next_bio, buf, len);
  132. if (ret <= 0) {
  133. fprintf(stderr, "Packeted BIO was truncated\n");
  134. return -1;
  135. }
  136. if (outl > (int)len) {
  137. outl = len;
  138. }
  139. memcpy(out, buf, outl);
  140. OPENSSL_free(buf);
  141. return outl;
  142. }
  143. static long PacketedCtrl(BIO *bio, int cmd, long num, void *ptr) {
  144. if (bio->next_bio == NULL) {
  145. return 0;
  146. }
  147. BIO_clear_retry_flags(bio);
  148. int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr);
  149. BIO_copy_next_retry(bio);
  150. return ret;
  151. }
  152. static int PacketedNew(BIO *bio) {
  153. bio->init = 1;
  154. return 1;
  155. }
  156. static int PacketedFree(BIO *bio) {
  157. if (bio == NULL) {
  158. return 0;
  159. }
  160. bio->init = 0;
  161. return 1;
  162. }
  163. static long PacketedCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
  164. if (bio->next_bio == NULL) {
  165. return 0;
  166. }
  167. return BIO_callback_ctrl(bio->next_bio, cmd, fp);
  168. }
  169. const BIO_METHOD g_packeted_bio_method = {
  170. BIO_TYPE_FILTER,
  171. "packeted bio",
  172. PacketedWrite,
  173. PacketedRead,
  174. NULL /* puts */,
  175. NULL /* gets */,
  176. PacketedCtrl,
  177. PacketedNew,
  178. PacketedFree,
  179. PacketedCallbackCtrl,
  180. };
  181. } // namespace
  182. ScopedBIO PacketedBioCreate(OPENSSL_timeval *out_timeout) {
  183. ScopedBIO bio(BIO_new(&g_packeted_bio_method));
  184. if (!bio) {
  185. return nullptr;
  186. }
  187. bio->ptr = out_timeout;
  188. return bio;
  189. }