utp_internal.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2010-2013 BitTorrent, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef __UTP_INTERNAL_H__
  23. #define __UTP_INTERNAL_H__
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include "utp.h"
  29. #include "utp_callbacks.h"
  30. #include "utp_templates.h"
  31. #include "utp_hash.h"
  32. #include "utp_hash.h"
  33. #include "utp_packedsockaddr.h"
  34. /* These originally lived in utp_config.h */
  35. #define CCONTROL_TARGET (100 * 1000) // us
  36. enum bandwidth_type_t {
  37. payload_bandwidth, connect_overhead,
  38. close_overhead, ack_overhead,
  39. header_overhead, retransmit_overhead
  40. };
  41. #ifdef WIN32
  42. #ifdef _MSC_VER
  43. #include "libutp_inet_ntop.h"
  44. #endif
  45. // newer versions of MSVC define these in errno.h
  46. #ifndef ECONNRESET
  47. #define ECONNRESET WSAECONNRESET
  48. #define EMSGSIZE WSAEMSGSIZE
  49. #define ECONNREFUSED WSAECONNREFUSED
  50. #define ETIMEDOUT WSAETIMEDOUT
  51. #endif
  52. #endif
  53. struct PACKED_ATTRIBUTE RST_Info {
  54. PackedSockAddr addr;
  55. uint32 connid;
  56. uint16 ack_nr;
  57. uint64 timestamp;
  58. };
  59. // It's really important that we don't have duplicate keys in the hash table.
  60. // If we do, we'll eventually crash. if we try to remove the second instance
  61. // of the key, we'll accidentally remove the first instead. then later,
  62. // checkTimeouts will try to access the second one's already freed memory.
  63. void UTP_FreeAll(struct UTPSocketHT *utp_sockets);
  64. struct UTPSocketKey {
  65. PackedSockAddr addr;
  66. uint32 recv_id; // "conn_seed", "conn_id"
  67. UTPSocketKey(const PackedSockAddr& _addr, uint32 _recv_id) {
  68. memset((void*)this, 0, sizeof(*this));
  69. addr = _addr;
  70. recv_id = _recv_id;
  71. }
  72. bool operator == (const UTPSocketKey &other) const {
  73. return recv_id == other.recv_id && addr == other.addr;
  74. }
  75. uint32 compute_hash() const {
  76. return recv_id ^ addr.compute_hash();
  77. }
  78. };
  79. struct UTPSocketKeyData {
  80. UTPSocketKey key;
  81. UTPSocket *socket;
  82. utp_link_t link;
  83. };
  84. #define UTP_SOCKET_BUCKETS 79
  85. #define UTP_SOCKET_INIT 15
  86. struct UTPSocketHT : utpHashTable<UTPSocketKey, UTPSocketKeyData> {
  87. UTPSocketHT() {
  88. const int buckets = UTP_SOCKET_BUCKETS;
  89. const int initial = UTP_SOCKET_INIT;
  90. this->Create(buckets, initial);
  91. }
  92. ~UTPSocketHT() {
  93. UTP_FreeAll(this);
  94. this->Free();
  95. }
  96. };
  97. struct struct_utp_context {
  98. void *userdata;
  99. utp_callback_t* callbacks[UTP_ARRAY_SIZE];
  100. uint64 current_ms;
  101. utp_context_stats context_stats;
  102. UTPSocket *last_utp_socket;
  103. Array<UTPSocket*> ack_sockets;
  104. Array<RST_Info> rst_info;
  105. UTPSocketHT *utp_sockets;
  106. size_t target_delay;
  107. size_t opt_sndbuf;
  108. size_t opt_rcvbuf;
  109. uint64 last_check;
  110. struct_utp_context();
  111. ~struct_utp_context();
  112. void log(int level, utp_socket *socket, char const *fmt, ...);
  113. void log_unchecked(utp_socket *socket, char const *fmt, ...);
  114. bool would_log(int level);
  115. bool log_normal:1; // log normal events?
  116. bool log_mtu:1; // log MTU related events?
  117. bool log_debug:1; // log debugging events? (Must also compile with UTP_DEBUG_LOGGING defined)
  118. };
  119. #endif //__UTP_INTERNAL_H__