Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

219 řádky
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. timeval *out_timeout = reinterpret_cast<timeval *>(bio->ptr);
  98. assert(out_timeout->tv_usec == 0);
  99. assert(out_timeout->tv_sec == 0);
  100. out_timeout->tv_usec = timeout % 1000000;
  101. out_timeout->tv_sec = timeout / 1000000;
  102. // Send an ACK to the peer.
  103. ret = BIO_write(bio->next_bio, &kOpcodeTimeoutAck, 1);
  104. if (ret <= 0) {
  105. return ret;
  106. }
  107. assert(ret == 1);
  108. // Signal to the caller to retry the read, after processing the
  109. // new clock.
  110. BIO_set_retry_read(bio);
  111. return -1;
  112. }
  113. if (opcode != kOpcodePacket) {
  114. fprintf(stderr, "Unknown opcode, %u\n", opcode);
  115. return -1;
  116. }
  117. // Read the length prefix.
  118. uint8_t len_bytes[4];
  119. ret = ReadAll(bio->next_bio, len_bytes, sizeof(len_bytes));
  120. if (ret <= 0) {
  121. BIO_copy_next_retry(bio);
  122. return ret;
  123. }
  124. uint32_t len = (len_bytes[0] << 24) | (len_bytes[1] << 16) |
  125. (len_bytes[2] << 8) | len_bytes[3];
  126. uint8_t *buf = (uint8_t *)OPENSSL_malloc(len);
  127. if (buf == NULL) {
  128. return -1;
  129. }
  130. ret = ReadAll(bio->next_bio, buf, len);
  131. if (ret <= 0) {
  132. fprintf(stderr, "Packeted BIO was truncated\n");
  133. return -1;
  134. }
  135. if (outl > (int)len) {
  136. outl = len;
  137. }
  138. memcpy(out, buf, outl);
  139. OPENSSL_free(buf);
  140. return outl;
  141. }
  142. static long PacketedCtrl(BIO *bio, int cmd, long num, void *ptr) {
  143. if (bio->next_bio == NULL) {
  144. return 0;
  145. }
  146. BIO_clear_retry_flags(bio);
  147. int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr);
  148. BIO_copy_next_retry(bio);
  149. return ret;
  150. }
  151. static int PacketedNew(BIO *bio) {
  152. bio->init = 1;
  153. return 1;
  154. }
  155. static int PacketedFree(BIO *bio) {
  156. if (bio == NULL) {
  157. return 0;
  158. }
  159. bio->init = 0;
  160. return 1;
  161. }
  162. static long PacketedCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) {
  163. if (bio->next_bio == NULL) {
  164. return 0;
  165. }
  166. return BIO_callback_ctrl(bio->next_bio, cmd, fp);
  167. }
  168. const BIO_METHOD g_packeted_bio_method = {
  169. BIO_TYPE_FILTER,
  170. "packeted bio",
  171. PacketedWrite,
  172. PacketedRead,
  173. NULL /* puts */,
  174. NULL /* gets */,
  175. PacketedCtrl,
  176. PacketedNew,
  177. PacketedFree,
  178. PacketedCallbackCtrl,
  179. };
  180. } // namespace
  181. ScopedBIO PacketedBioCreate(timeval *out_timeout) {
  182. ScopedBIO bio(BIO_new(&g_packeted_bio_method));
  183. if (!bio) {
  184. return nullptr;
  185. }
  186. bio->ptr = out_timeout;
  187. return bio;
  188. }