diff options
| author | Hanjie Wu <wu@x-nagi.com> | 2023-12-16 01:13:23 -0800 |
|---|---|---|
| committer | Hanjie Wu <wu@x-nagi.com> | 2023-12-16 01:13:23 -0800 |
| commit | 58054f460e6bb04d6fd28a881c0b010df778bf5d (patch) | |
| tree | 1ee1b9297f6a891155dc7b5deeda103e400bb6c6 /src/libutp/include | |
| parent | 5c87f1b7b6345b895caf9f9b29a2c3c7ca94a0af (diff) | |
add uTP support for outbound connections
Diffstat (limited to 'src/libutp/include')
| -rw-r--r-- | src/libutp/include/libutp/utp.h | 182 | ||||
| -rw-r--r-- | src/libutp/include/libutp/utp_types.h | 115 |
2 files changed, 297 insertions, 0 deletions
diff --git a/src/libutp/include/libutp/utp.h b/src/libutp/include/libutp/utp.h new file mode 100644 index 00000000..9f0b70b1 --- /dev/null +++ b/src/libutp/include/libutp/utp.h @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2010-2013 BitTorrent, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __UTP_H__ +#define __UTP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdarg.h> +#include "utp_types.h" + +typedef struct UTPSocket utp_socket; +typedef struct struct_utp_context utp_context; + +enum { + UTP_UDP_DONTFRAG = 2 // Used to be a #define as UDP_IP_DONTFRAG +}; + +enum { + // socket has reveived syn-ack (notification only for outgoing connection completion) + // this implies writability + UTP_STATE_CONNECT = 1, + + // socket is able to send more data + UTP_STATE_WRITABLE = 2, + + // connection closed + UTP_STATE_EOF = 3, + + // socket is being destroyed, meaning all data has been sent if possible. + // it is not valid to refer to the socket after this state change occurs + UTP_STATE_DESTROYING = 4 +}; + +extern const char *utp_state_names[]; + +// Errors codes that can be passed to UTP_ON_ERROR callback +enum { + UTP_ECONNREFUSED = 0, + UTP_ECONNRESET, + UTP_ETIMEDOUT +}; + +extern const char *utp_error_code_names[]; + +enum { + // callback names + UTP_ON_FIREWALL = 0, + UTP_ON_ACCEPT, + UTP_ON_CONNECT, + UTP_ON_ERROR, + UTP_ON_READ, + UTP_ON_OVERHEAD_STATISTICS, + UTP_ON_STATE_CHANGE, + UTP_GET_READ_BUFFER_SIZE, + UTP_ON_DELAY_SAMPLE, + UTP_GET_UDP_MTU, + UTP_GET_UDP_OVERHEAD, + UTP_GET_MILLISECONDS, + UTP_GET_MICROSECONDS, + UTP_GET_RANDOM, + UTP_LOG, + UTP_SENDTO, + + // context and socket options that may be set/queried + UTP_LOG_NORMAL, + UTP_LOG_MTU, + UTP_LOG_DEBUG, + UTP_SNDBUF, + UTP_RCVBUF, + UTP_TARGET_DELAY, + + UTP_ARRAY_SIZE // must be last +}; + +extern const char *utp_callback_names[]; + +typedef struct { + utp_context *context; + utp_socket *socket; + size_t len; + uint32 flags; + int callback_type; + const byte *buf; + + union { + const struct sockaddr *address; + int send; + int sample_ms; + int error_code; + int state; + }; + + union { + socklen_t address_len; + int type; + }; +} utp_callback_arguments; + +typedef uint64 utp_callback_t(utp_callback_arguments *); + +// Returned by utp_get_context_stats() +typedef struct { + uint32 _nraw_recv[5]; // total packets recieved less than 300/600/1200/MTU bytes fpr all connections (context-wide) + uint32 _nraw_send[5]; // total packets sent less than 300/600/1200/MTU bytes for all connections (context-wide) +} utp_context_stats; + +// Returned by utp_get_stats() +typedef struct { + uint64 nbytes_recv; // total bytes received + uint64 nbytes_xmit; // total bytes transmitted + uint32 rexmit; // retransmit counter + uint32 fastrexmit; // fast retransmit counter + uint32 nxmit; // transmit counter + uint32 nrecv; // receive counter (total) + uint32 nduprecv; // duplicate receive counter + uint32 mtu_guess; // Best guess at MTU +} utp_socket_stats; + +#define UTP_IOV_MAX 1024 + +// For utp_writev, to writes data from multiple buffers +struct utp_iovec { + void *iov_base; + size_t iov_len; +}; + +// Public Functions +utp_context* utp_init (int version); +void utp_destroy (utp_context *ctx); +void utp_set_callback (utp_context *ctx, int callback_name, utp_callback_t *proc); +void* utp_context_set_userdata (utp_context *ctx, void *userdata); +void* utp_context_get_userdata (utp_context *ctx); +int utp_context_set_option (utp_context *ctx, int opt, int val); +int utp_context_get_option (utp_context *ctx, int opt); +int utp_process_udp (utp_context *ctx, const byte *buf, size_t len, const struct sockaddr *to, socklen_t tolen); +int utp_process_icmp_error (utp_context *ctx, const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen); +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); +void utp_check_timeouts (utp_context *ctx); +void utp_issue_deferred_acks (utp_context *ctx); +utp_context_stats* utp_get_context_stats (utp_context *ctx); +utp_socket* utp_create_socket (utp_context *ctx); +void* utp_set_userdata (utp_socket *s, void *userdata); +void* utp_get_userdata (utp_socket *s); +int utp_setsockopt (utp_socket *s, int opt, int val); +int utp_getsockopt (utp_socket *s, int opt); +int utp_connect (utp_socket *s, const struct sockaddr *to, socklen_t tolen); +ssize_t utp_write (utp_socket *s, void *buf, size_t count); +ssize_t utp_writev (utp_socket *s, struct utp_iovec *iovec, size_t num_iovecs); +int utp_getpeername (utp_socket *s, struct sockaddr *addr, socklen_t *addrlen); +void utp_read_drained (utp_socket *s); +int utp_get_delays (utp_socket *s, uint32 *ours, uint32 *theirs, uint32 *age); +utp_socket_stats* utp_get_stats (utp_socket *s); +utp_context* utp_get_context (utp_socket *s); +void utp_close (utp_socket *s); + +#ifdef __cplusplus +} +#endif + +#endif //__UTP_H__ diff --git a/src/libutp/include/libutp/utp_types.h b/src/libutp/include/libutp/utp_types.h new file mode 100644 index 00000000..a1193906 --- /dev/null +++ b/src/libutp/include/libutp/utp_types.h @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2010-2013 BitTorrent, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __UTP_TYPES_H__ +#define __UTP_TYPES_H__ + +#include <inttypes.h> + +// Allow libutp consumers or prerequisites to override PACKED_ATTRIBUTE +#ifndef PACKED_ATTRIBUTE +#if defined BROKEN_GCC_STRUCTURE_PACKING && defined __GNUC__ + // Used for gcc tool chains accepting but not supporting pragma pack + // See http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html + #define PACKED_ATTRIBUTE __attribute__((__packed__)) +#else + #define PACKED_ATTRIBUTE +#endif // defined BROKEN_GCC_STRUCTURE_PACKING && defined __GNUC__ +#endif // ndef PACKED_ATTRIBUTE + +#ifdef __GNUC__ + #define ALIGNED_ATTRIBUTE(x) __attribute__((aligned (x))) +#else + #define ALIGNED_ATTRIBUTE(x) +#endif + +// hash.cpp needs socket definitions, which is why this networking specific +// code is inclued in utypes.h +#ifdef WIN32 + #undef _CRT_SECURE_NO_DEPRECATE + #define _CRT_SECURE_NO_DEPRECATE + #undef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #include <windows.h> + #include <winsock2.h> + #include <ws2tcpip.h> + #define IP_OPT_DONTFRAG IP_DONTFRAGMENT +#else + #include <netinet/in.h> + #include <arpa/inet.h> + #include <unistd.h> + #include <sys/socket.h> + + #ifdef IP_DONTFRAG + #define IP_OPT_DONTFRAG IP_DONTFRAG + #elif defined IP_DONTFRAGMENT + #define IP_OPT_DONTFRAG IP_DONTFRAGMENT + #else + //#warning "I don't know how to set DF bit on this system" + #endif +#endif + +#ifdef _MSC_VER + #include <BaseTsd.h> + typedef SSIZE_T ssize_t; +#endif + +#ifdef POSIX + typedef struct sockaddr_storage SOCKADDR_STORAGE; +#endif + +#ifdef WIN32 + #define I64u "%I64u" +#else + #define I64u "%Lu" +#endif + +// standard types +typedef unsigned char byte; +typedef unsigned char uint8; +typedef signed char int8; +typedef unsigned short uint16; +typedef signed short int16; +typedef unsigned int uint; +typedef unsigned int uint32; +typedef signed int int32; + +typedef uint64_t uint64; +typedef int64_t int64; + +/* compile-time assert */ +#ifndef CASSERT +#define CASSERT( exp, name ) typedef int is_not_##name [ (exp ) ? 1 : -1 ]; +#endif + +CASSERT(8 == sizeof(uint64), sizeof_uint64_is_8) +CASSERT(8 == sizeof(int64), sizeof_int64_is_8) + +// always ANSI +typedef const char * cstr; +typedef char * str; + +#ifndef __cplusplus +typedef uint8 bool; +#endif + +#endif //__UTP_TYPES_H__ |
