utp.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_H__
  23. #define __UTP_H__
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #ifdef _WIN32
  28. #include <stdint.h>
  29. #endif
  30. #include <stdarg.h>
  31. #include "utp_types.h"
  32. typedef struct UTPSocket utp_socket;
  33. typedef struct struct_utp_context utp_context;
  34. enum {
  35. UTP_UDP_DONTFRAG = 2, // Used to be a #define as UDP_IP_DONTFRAG
  36. };
  37. enum {
  38. // socket has reveived syn-ack (notification only for outgoing connection completion)
  39. // this implies writability
  40. UTP_STATE_CONNECT = 1,
  41. // socket is able to send more data
  42. UTP_STATE_WRITABLE = 2,
  43. // connection closed
  44. UTP_STATE_EOF = 3,
  45. // socket is being destroyed, meaning all data has been sent if possible.
  46. // it is not valid to refer to the socket after this state change occurs
  47. UTP_STATE_DESTROYING = 4,
  48. };
  49. extern const char *utp_state_names[];
  50. // Errors codes that can be passed to UTP_ON_ERROR callback
  51. enum {
  52. UTP_ECONNREFUSED = 0,
  53. UTP_ECONNRESET,
  54. UTP_ETIMEDOUT,
  55. };
  56. extern const char *utp_error_code_names[];
  57. enum {
  58. // callback names
  59. UTP_ON_FIREWALL = 0,
  60. UTP_ON_ACCEPT,
  61. UTP_ON_CONNECT,
  62. UTP_ON_ERROR,
  63. UTP_ON_READ,
  64. UTP_ON_OVERHEAD_STATISTICS,
  65. UTP_ON_STATE_CHANGE,
  66. UTP_GET_READ_BUFFER_SIZE,
  67. UTP_ON_DELAY_SAMPLE,
  68. UTP_GET_UDP_MTU,
  69. UTP_GET_UDP_OVERHEAD,
  70. UTP_GET_MILLISECONDS,
  71. UTP_GET_MICROSECONDS,
  72. UTP_GET_RANDOM,
  73. UTP_LOG,
  74. UTP_SENDTO,
  75. // context and socket options that may be set/queried
  76. UTP_LOG_NORMAL,
  77. UTP_LOG_MTU,
  78. UTP_LOG_DEBUG,
  79. UTP_SNDBUF,
  80. UTP_RCVBUF,
  81. UTP_TARGET_DELAY,
  82. UTP_ARRAY_SIZE, // must be last
  83. };
  84. extern const char *utp_callback_names[];
  85. typedef struct {
  86. utp_context *context;
  87. utp_socket *socket;
  88. size_t len;
  89. uint32 flags;
  90. int callback_type;
  91. const byte *buf;
  92. union {
  93. const struct sockaddr *address;
  94. int send;
  95. int sample_ms;
  96. int error_code;
  97. int state;
  98. };
  99. union {
  100. socklen_t address_len;
  101. int type;
  102. };
  103. } utp_callback_arguments;
  104. typedef uint64 utp_callback_t(utp_callback_arguments *);
  105. // Returned by utp_get_context_stats()
  106. typedef struct {
  107. uint32 _nraw_recv[5]; // total packets recieved less than 300/600/1200/MTU bytes fpr all connections (context-wide)
  108. uint32 _nraw_send[5]; // total packets sent less than 300/600/1200/MTU bytes for all connections (context-wide)
  109. } utp_context_stats;
  110. // Returned by utp_get_stats()
  111. typedef struct {
  112. uint64 nbytes_recv; // total bytes received
  113. uint64 nbytes_xmit; // total bytes transmitted
  114. uint32 rexmit; // retransmit counter
  115. uint32 fastrexmit; // fast retransmit counter
  116. uint32 nxmit; // transmit counter
  117. uint32 nrecv; // receive counter (total)
  118. uint32 nduprecv; // duplicate receive counter
  119. uint32 mtu_guess; // Best guess at MTU
  120. } utp_socket_stats;
  121. #define UTP_IOV_MAX 1024
  122. // For utp_writev, to writes data from multiple buffers
  123. struct utp_iovec {
  124. void *iov_base;
  125. size_t iov_len;
  126. };
  127. // Public Functions
  128. utp_context* utp_init (int version);
  129. void utp_destroy (utp_context *ctx);
  130. void utp_set_callback (utp_context *ctx, int callback_name, utp_callback_t *proc);
  131. void* utp_context_set_userdata (utp_context *ctx, void *userdata);
  132. void* utp_context_get_userdata (utp_context *ctx);
  133. int utp_context_set_option (utp_context *ctx, int opt, int val);
  134. int utp_context_get_option (utp_context *ctx, int opt);
  135. int utp_process_udp (utp_context *ctx, const byte *buf, size_t len, const struct sockaddr *to, socklen_t tolen);
  136. int utp_process_icmp_error (utp_context *ctx, const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen);
  137. int utp_process_icmp_fragmentation (utp_context *ctx, const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen, uint16 next_hop_mtu);
  138. void utp_check_timeouts (utp_context *ctx);
  139. void utp_issue_deferred_acks (utp_context *ctx);
  140. utp_context_stats* utp_get_context_stats (utp_context *ctx);
  141. utp_socket* utp_create_socket (utp_context *ctx);
  142. void* utp_set_userdata (utp_socket *s, void *userdata);
  143. void* utp_get_userdata (utp_socket *s);
  144. int utp_setsockopt (utp_socket *s, int opt, int val);
  145. int utp_getsockopt (utp_socket *s, int opt);
  146. int utp_connect (utp_socket *s, const struct sockaddr *to, socklen_t tolen);
  147. ssize_t utp_write (utp_socket *s, void *buf, size_t count);
  148. ssize_t utp_writev (utp_socket *s, struct utp_iovec *iovec, size_t num_iovecs);
  149. int utp_getpeername (utp_socket *s, struct sockaddr *addr, socklen_t *addrlen);
  150. void utp_read_drained (utp_socket *s);
  151. int utp_get_delays (utp_socket *s, uint32 *ours, uint32 *theirs, uint32 *age);
  152. utp_socket_stats* utp_get_stats (utp_socket *s);
  153. utp_context* utp_get_context (utp_socket *s);
  154. void utp_shutdown (utp_socket *s, int how);
  155. void utp_close (utp_socket *s);
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif //__UTP_H__