25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

178 satır
3.8 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 "async_bio.h"
  15. #include <errno.h>
  16. #include <openssl/mem.h>
  17. namespace {
  18. extern const BIO_METHOD async_bio_method;
  19. struct async_bio {
  20. bool datagram;
  21. size_t read_quota;
  22. size_t write_quota;
  23. };
  24. async_bio *get_data(BIO *bio) {
  25. if (bio->method != &async_bio_method) {
  26. return NULL;
  27. }
  28. return (async_bio *)bio->ptr;
  29. }
  30. static int async_write(BIO *bio, const char *in, int inl) {
  31. async_bio *a = get_data(bio);
  32. if (a == NULL || bio->next_bio == NULL) {
  33. return 0;
  34. }
  35. if (a->datagram) {
  36. // Perform writes synchronously; the DTLS implementation drops any packets
  37. // that failed to send.
  38. return BIO_write(bio->next_bio, in, inl);
  39. }
  40. BIO_clear_retry_flags(bio);
  41. if (a->write_quota == 0) {
  42. BIO_set_retry_write(bio);
  43. errno = EAGAIN;
  44. return -1;
  45. }
  46. if (!a->datagram && (size_t)inl > a->write_quota) {
  47. inl = a->write_quota;
  48. }
  49. int ret = BIO_write(bio->next_bio, in, inl);
  50. if (ret <= 0) {
  51. BIO_copy_next_retry(bio);
  52. } else {
  53. a->write_quota -= ret;
  54. }
  55. return ret;
  56. }
  57. static int async_read(BIO *bio, char *out, int outl) {
  58. async_bio *a = get_data(bio);
  59. if (a == NULL || bio->next_bio == NULL) {
  60. return 0;
  61. }
  62. BIO_clear_retry_flags(bio);
  63. if (a->read_quota == 0) {
  64. BIO_set_retry_read(bio);
  65. errno = EAGAIN;
  66. return -1;
  67. }
  68. if (!a->datagram && (size_t)outl > a->read_quota) {
  69. outl = a->read_quota;
  70. }
  71. int ret = BIO_read(bio->next_bio, out, outl);
  72. if (ret <= 0) {
  73. BIO_copy_next_retry(bio);
  74. } else {
  75. a->read_quota -= (a->datagram ? 1 : ret);
  76. }
  77. return ret;
  78. }
  79. static long async_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  80. if (bio->next_bio == NULL) {
  81. return 0;
  82. }
  83. BIO_clear_retry_flags(bio);
  84. int ret = BIO_ctrl(bio->next_bio, cmd, num, ptr);
  85. BIO_copy_next_retry(bio);
  86. return ret;
  87. }
  88. static int async_new(BIO *bio) {
  89. async_bio *a = (async_bio *)OPENSSL_malloc(sizeof(*a));
  90. if (a == NULL) {
  91. return 0;
  92. }
  93. memset(a, 0, sizeof(*a));
  94. bio->init = 1;
  95. bio->ptr = (char *)a;
  96. return 1;
  97. }
  98. static int async_free(BIO *bio) {
  99. if (bio == NULL) {
  100. return 0;
  101. }
  102. OPENSSL_free(bio->ptr);
  103. bio->ptr = NULL;
  104. bio->init = 0;
  105. bio->flags = 0;
  106. return 1;
  107. }
  108. static long async_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  109. if (bio->next_bio == NULL) {
  110. return 0;
  111. }
  112. return BIO_callback_ctrl(bio->next_bio, cmd, fp);
  113. }
  114. const BIO_METHOD async_bio_method = {
  115. BIO_TYPE_FILTER,
  116. "async bio",
  117. async_write,
  118. async_read,
  119. NULL /* puts */,
  120. NULL /* gets */,
  121. async_ctrl,
  122. async_new,
  123. async_free,
  124. async_callback_ctrl,
  125. };
  126. } // namespace
  127. BIO *async_bio_create() {
  128. return BIO_new(&async_bio_method);
  129. }
  130. BIO *async_bio_create_datagram() {
  131. BIO *ret = BIO_new(&async_bio_method);
  132. if (!ret) {
  133. return NULL;
  134. }
  135. get_data(ret)->datagram = true;
  136. return ret;
  137. }
  138. void async_bio_allow_read(BIO *bio, size_t count) {
  139. async_bio *a = get_data(bio);
  140. if (a == NULL) {
  141. return;
  142. }
  143. a->read_quota += count;
  144. }
  145. void async_bio_allow_write(BIO *bio, size_t count) {
  146. async_bio *a = get_data(bio);
  147. if (a == NULL) {
  148. return;
  149. }
  150. a->write_quota += count;
  151. }