From 98f466572b9b85274bd0e4406982fa4e57e38ad4 Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Sat, 10 Jun 2023 12:40:24 -0600 Subject: [PATCH 1/6] added utp-client-interop --- utp-client-interop/Makefile | 34 + utp-client-interop/libutp | 1 + utp-client-interop/nim-utp/nim_utp.nimble | 16 + utp-client-interop/nim-utp/src/recv.nim | 50 ++ utp-client-interop/nim-utp/src/send.nim | 58 ++ utp-client-interop/rust-utp/.gitignore | 1 + utp-client-interop/rust-utp/Cargo.lock | 770 ++++++++++++++++++++++ utp-client-interop/rust-utp/Cargo.toml | 15 + utp-client-interop/rust-utp/src/main.rs | 95 +++ 9 files changed, 1040 insertions(+) create mode 100644 utp-client-interop/Makefile create mode 160000 utp-client-interop/libutp create mode 100644 utp-client-interop/nim-utp/nim_utp.nimble create mode 100644 utp-client-interop/nim-utp/src/recv.nim create mode 100644 utp-client-interop/nim-utp/src/send.nim create mode 100644 utp-client-interop/rust-utp/.gitignore create mode 100644 utp-client-interop/rust-utp/Cargo.lock create mode 100644 utp-client-interop/rust-utp/Cargo.toml create mode 100644 utp-client-interop/rust-utp/src/main.rs diff --git a/utp-client-interop/Makefile b/utp-client-interop/Makefile new file mode 100644 index 0000000..1c06b52 --- /dev/null +++ b/utp-client-interop/Makefile @@ -0,0 +1,34 @@ +# r - means run +# rxy - x means which language c=c r=rust n=nim +# rxy - y is either recv or send which will either setup the library to receive data or send it +# goal? as tooling is built up till it will possibly allow for test automation? + +# instructions run a recv then a sender + +rcrecv: + cd libutp && make + ./libutp/ucat-static -ddddd -l -p 9078 + +rcsend: + cd libutp && make + ./libutp/portal-test + +rrsend: + cd rust-utp && RUST_LOG=trace cargo run -- send + +rrrecv: + cd rust-utp && RUST_LOG=trace cargo run -- recv + +rnsend: + cd nim-utp && nimble c src/send.nim + ./nim-utp/src/send + +# currently doesn't work I need to understand nim syntax more +rnrecv: + cd nim-utp && nimble c src/recv.nim + ./nim-utp/src/recv + +clean: + rm nim-utp/src/send nim-utp/src/recv -f + rm rust-utp/target -rf + cd libutp && make clean \ No newline at end of file diff --git a/utp-client-interop/libutp b/utp-client-interop/libutp new file mode 160000 index 0000000..2b364cb --- /dev/null +++ b/utp-client-interop/libutp @@ -0,0 +1 @@ +Subproject commit 2b364cbb0650bdab64a5de2abb4518f9f228ec44 diff --git a/utp-client-interop/nim-utp/nim_utp.nimble b/utp-client-interop/nim-utp/nim_utp.nimble new file mode 100644 index 0000000..9cdac1b --- /dev/null +++ b/utp-client-interop/nim-utp/nim_utp.nimble @@ -0,0 +1,16 @@ +# Package + +version = "0.1.0" +author = "KolbyML" +description = "A new awesome nimble package" +license = "MIT" +srcDir = "src" +bin = @["nim_utp"] + + +# Dependencies + +requires "nim >= 1.6.12", + "chronos", + "eth" + diff --git a/utp-client-interop/nim-utp/src/recv.nim b/utp-client-interop/nim-utp/src/recv.nim new file mode 100644 index 0000000..f73f72e --- /dev/null +++ b/utp-client-interop/nim-utp/src/recv.nim @@ -0,0 +1,50 @@ +# Copyright (c) 2020-2023 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +{.push raises: [].} + +import + chronos, stew/[results, byteutils], + eth/utp/utp_router, + eth/utp/utp_socket, + eth/utp/utp_protocol + +# Example application to interact with the reference implementation server +# to be able to test against the reference implementation. +# To run libutp server: +# 1. git clone https://github.com/bittorrent/libutp.git +# 2. cd libutp +# 3. make +# 4. ./ucat -ddddd -l -p 9078 - it will run utp server on port 9078 +when isMainModule: + proc echoIncomingSocketCallBack(): AcceptConnectionCallback[TransportAddress] = + return ( + proc (server: UtpRouter[TransportAddress], + client: UtpSocket[TransportAddress]): + Future[void] {.gcsafe, raises: [].} = + echo "received incoming connection" + let fakeFuture = newFuture[void]() + fakeFuture.complete() + return fakeFuture + ) + # TODO read client/server ports and address from cmd line or config file + let localAddress = initTAddress("0.0.0.0", 9078) + let utpProt = UtpProtocol.new(echoIncomingSocketCallBack(), localAddress) + + waitFor(sleepAsync(milliseconds(1000))) + + let bytes = await utpProt.read(10) + echo "Hi, ", bytes, "!" + + # discard waitFor soc.write(bytes) + + # waitFor(sleepAsync(milliseconds(1000))) + + # discard waitFor soc.write(bytes) + + runForever() + + waitFor utpProt.shutdownWait() \ No newline at end of file diff --git a/utp-client-interop/nim-utp/src/send.nim b/utp-client-interop/nim-utp/src/send.nim new file mode 100644 index 0000000..629332a --- /dev/null +++ b/utp-client-interop/nim-utp/src/send.nim @@ -0,0 +1,58 @@ +# Copyright (c) 2020-2023 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +{.push raises: [].} + +import + chronos, stew/[results, byteutils], + eth/utp/utp_router, + eth/utp/utp_socket, + eth/utp/utp_protocol + +# Example application to interact with the reference implementation server +# to be able to test against the reference implementation. +# To run libutp server: +# 1. git clone https://github.com/bittorrent/libutp.git +# 2. cd libutp +# 3. make +# 4. ./ucat -ddddd -l -p 9078 - it will run utp server on port 9078 +when isMainModule: + proc echoIncomingSocketCallBack(): AcceptConnectionCallback[TransportAddress] = + return ( + proc (server: UtpRouter[TransportAddress], + client: UtpSocket[TransportAddress]): + Future[void] {.gcsafe, raises: [].} = + echo "received incoming connection" + let fakeFuture = newFuture[void]() + fakeFuture.complete() + return fakeFuture + ) + # TODO read client/server ports and address from cmd line or config file + let localAddress = initTAddress("0.0.0.0", 9077) + let utpProt = UtpProtocol.new(echoIncomingSocketCallBack(), localAddress) + + let remoteServer = initTAddress("127.0.0.1", 9078) + let socResult = waitFor utpProt.connectTo(remoteServer) + let soc = socResult.get() + + doAssert(soc.numPacketsInOutGoingBuffer() == 0) + + let helloUtp = "Hello from nim implementation" + let bytes = helloUtp.toBytes() + + discard waitFor soc.write(bytes) + + waitFor(sleepAsync(milliseconds(1000))) + + # discard waitFor soc.write(bytes) + + # waitFor(sleepAsync(milliseconds(1000))) + + # discard waitFor soc.write(bytes) + + runForever() + + waitFor utpProt.shutdownWait() \ No newline at end of file diff --git a/utp-client-interop/rust-utp/.gitignore b/utp-client-interop/rust-utp/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/utp-client-interop/rust-utp/.gitignore @@ -0,0 +1 @@ +/target diff --git a/utp-client-interop/rust-utp/Cargo.lock b/utp-client-interop/rust-utp/Cargo.lock new file mode 100644 index 0000000..b62a05a --- /dev/null +++ b/utp-client-interop/rust-utp/Cargo.lock @@ -0,0 +1,770 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anstream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" + +[[package]] +name = "anstyle-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "async-trait" +version = "0.1.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "401a4694d2bf92537b6867d94de48c4842089645fdcdf6c71865b175d836e9c2" +dependencies = [ + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72394f3339a76daf211e57d4bcb374410f3965dcc606dd0e03738c7888766980" +dependencies = [ + "anstream", + "anstyle", + "bitflags", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8cd2b2a819ad6eec39e8f1d6b53001af1e5469f8c177579cdaeb313115b825f" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + +[[package]] +name = "delay_map" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c4d75d3abfe4830dcbf9bcb1b926954e121669f74dd1ca7aa0183b1755d83f6" +dependencies = [ + "futures", + "tokio-util", +] + +[[package]] +name = "errno" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" + +[[package]] +name = "futures-executor" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" + +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + +[[package]] +name = "futures-task" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" + +[[package]] +name = "futures-util" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + +[[package]] +name = "hermit-abi" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" + +[[package]] +name = "io-lifetimes" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" +dependencies = [ + "hermit-abi 0.3.1", + "libc", + "windows-sys", +] + +[[package]] +name = "is-terminal" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" +dependencies = [ + "hermit-abi 0.3.1", + "io-lifetimes", + "rustix", + "windows-sys", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc86cde3ff845662b8f4ef6cb50ea0e20c524eb3d29ae048287e06a1b3fa6a81" + +[[package]] +name = "linux-raw-sys" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" + +[[package]] +name = "log" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de" + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "mio" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + +[[package]] +name = "num_cpus" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +dependencies = [ + "hermit-abi 0.2.6", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rust-utp" +version = "0.1.0" +dependencies = [ + "clap", + "futures", + "tokio", + "tracing", + "tracing-subscriber", + "utp-rs", +] + +[[package]] +name = "rustix" +version = "0.37.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "slab" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + +[[package]] +name = "socket2" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tokio" +version = "1.28.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94d7b1cfd2aa4011f2de74c2c4c63665e27a71006b0a192dcd2710272e73dfa2" +dependencies = [ + "autocfg", + "bytes", + "libc", + "mio", + "num_cpus", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-util" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36943ee01a6d67977dd3f84a5a1d2efeb4ada3a1ae771cadfaa535d9d9fc6507" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "slab", + "tokio", +] + +[[package]] +name = "tracing" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +dependencies = [ + "cfg-if", + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unicode-ident" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "utp-rs" +version = "0.1.0-alpha.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a29f56696d12604e7ce844c299a2f4807583480836201ac1a46d8639ade963c6" +dependencies = [ + "async-trait", + "delay_map", + "futures", + "rand", + "tokio", + "tracing", +] + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" diff --git a/utp-client-interop/rust-utp/Cargo.toml b/utp-client-interop/rust-utp/Cargo.toml new file mode 100644 index 0000000..0fad9bc --- /dev/null +++ b/utp-client-interop/rust-utp/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "rust-utp" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { version = "4.2.1", features = ["derive"] } +futures = "0.3.26" +tokio = { version = "1.25.0", features = ["io-util", "rt-multi-thread", "macros", "net", "sync", "time"] } +utp-rs = { version = "0.1.0-alpha.5"} +#utp-rs = { git = 'https://github.com/KolbyML/utp', branch = 'time-weird' } +tracing = { version = "0.1.37", features = ["std", "attributes", "log"] } +tracing-subscriber = "0.3.16" diff --git a/utp-client-interop/rust-utp/src/main.rs b/utp-client-interop/rust-utp/src/main.rs new file mode 100644 index 0000000..500dec2 --- /dev/null +++ b/utp-client-interop/rust-utp/src/main.rs @@ -0,0 +1,95 @@ +use std::env; +use std::net::SocketAddr; +use clap::{arg, Args, Parser, Subcommand}; +use utp_rs::conn::ConnectionConfig; +use utp_rs::socket::UtpSocket; +use utp_rs::udp::AsyncUdpSocket; +use std::sync::Arc; +use std::io::Write; +use std::time::Duration; +use utp_rs::cid; + +#[derive(Parser, Debug, PartialEq, Clone)] +#[command()] +pub struct TestParams { + #[command(subcommand)] + pub command: Option, +} + +#[derive(Subcommand, Debug, Clone, PartialEq)] +#[allow(clippy::enum_variant_names)] +pub enum Commands { + Send, + Recv, +} + +#[tokio::main] +async fn main() { + tracing_subscriber::fmt::init(); + + let config = TestParams::try_parse_from(env::args_os()).unwrap(); + + match config.command { + Some(comm) => match comm { + Commands::Send => sender().await, + Commands::Recv => receiver().await, + }, + None => panic!("no command given"), + } +} + +async fn sender() { + let recv_addr = SocketAddr::from(([127, 0, 0, 1], 9078)); + let send_addr = SocketAddr::from(([127, 0, 0, 1], 9077)); + + let send = UtpSocket::bind(send_addr).await.unwrap(); + let send = Arc::new(send); + + // start 50 transfers, step by two to avoid cid collisions + let conn_config = ConnectionConfig::default(); + + let data = "Hello from nim implementation".as_bytes(); + print!("hi {:?}",data.clone().to_vec()); + std::io::stdout().flush().unwrap(); + + + let send_handle = tokio::spawn(async move { + + let mut stream = send.connect(recv_addr, conn_config).await.expect("buckets"); + + let n = stream.write(&data).await.unwrap(); + //assert_eq!(send_handle.unwrap(), data.len()); + print!("hi"); + + //stream.shutdown().unwrap(); + + }); + + send_handle.await; + + loop { + } +} + +async fn receiver() { + let conn_config = ConnectionConfig::default(); + let recv_addr = SocketAddr::from(([127, 0, 0, 1], 9078)); + let recv = UtpSocket::bind(recv_addr).await.unwrap(); + let recv = Arc::new(recv); + + let recv_arc = Arc::clone(&recv); + let recv_one_handle = tokio::spawn(async move { + let mut stream = recv_arc + .accept(conn_config) + .await + .unwrap(); + let mut buf = vec![]; + let n = stream.read_to_eof(&mut buf).await.unwrap(); + print!("\n\n\nsize: {} :: buf {:?}\n\n\n", n, buf) + }); + + recv_one_handle.await; + + loop { + } +} \ No newline at end of file From f4c1a244a674d4360e2aa15aedbee83f7125faac Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Sat, 10 Jun 2023 12:52:27 -0600 Subject: [PATCH 2/6] Create README.md --- utp-client-interop/README.md | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 utp-client-interop/README.md diff --git a/utp-client-interop/README.md b/utp-client-interop/README.md new file mode 100644 index 0000000..70999ae --- /dev/null +++ b/utp-client-interop/README.md @@ -0,0 +1,41 @@ +# utp interop testing + +if you are trying to use C, Rust, Nim's client it will be assumed you have there respective development depends installed. + +### Test ethereum/utp send to bittorrent/libutp + +Terminal 1 +``` +make rcrecv +``` + +Terminal 2 +``` +make rrsend +``` + +### Test bittorrent/libutp send to ethereum/utp + +Terminal 1 +``` +make rrrecv +``` + +Terminal 2 +``` +make rcsend +``` + +### Test bittorrent/libutp send to bittorrent/libutp + +Terminal 1 +``` +make rcrecv +``` + +Terminal 2 +``` +make rcsend +``` + +### etc, etc \ No newline at end of file From 8d1fc23a7367c53d3f3f19fbfbfe0cadf7e58dbf Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Sat, 10 Jun 2023 12:53:31 -0600 Subject: [PATCH 3/6] Adding files --- utp-client-interop/libutp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utp-client-interop/libutp b/utp-client-interop/libutp index 2b364cb..a0cd2d8 160000 --- a/utp-client-interop/libutp +++ b/utp-client-interop/libutp @@ -1 +1 @@ -Subproject commit 2b364cbb0650bdab64a5de2abb4518f9f228ec44 +Subproject commit a0cd2d8a2aeaf94460c2ac7b438d039970ffbe7e From 7546e9e336ba9949e7801d0e0bdb76179b35f652 Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Sat, 10 Jun 2023 12:56:40 -0600 Subject: [PATCH 4/6] Add libutp --- utp-client-interop/libutp (copy)/LICENSE | 19 + utp-client-interop/libutp (copy)/Makefile | 51 + utp-client-interop/libutp (copy)/README.md | 68 + .../libutp (copy)/libutp-2012.vcxproj.filters | 65 + .../libutp (copy)/libutp.vcxproj | 258 ++ .../libutp (copy)/libutp_inet_ntop.cpp | 108 + .../libutp (copy)/libutp_inet_ntop.h | 68 + utp-client-interop/libutp (copy)/parse_log.py | 288 ++ .../libutp (copy)/portal-test.c | 554 +++ .../prop_sheets/RunTimeDebug.props | 12 + .../prop_sheets/RunTimeRelease.props | 12 + .../prop_sheets/debug-2012.props | 46 + .../prop_sheets/release-2012.props | 51 + .../prop_sheets/win32-2012.props | 16 + .../libutp (copy)/prop_sheets/x64-2012.props | 8 + utp-client-interop/libutp (copy)/ucat.c | 636 +++ utp-client-interop/libutp (copy)/utp.h | 183 + utp-client-interop/libutp (copy)/utp_api.cpp | 139 + .../libutp (copy)/utp_callbacks.cpp | 208 + .../libutp (copy)/utp_callbacks.h | 47 + utp-client-interop/libutp (copy)/utp_hash.cpp | 246 ++ utp-client-interop/libutp (copy)/utp_hash.h | 146 + .../libutp (copy)/utp_internal.cpp | 3490 +++++++++++++++++ .../libutp (copy)/utp_internal.h | 140 + .../libutp (copy)/utp_packedsockaddr.cpp | 139 + .../libutp (copy)/utp_packedsockaddr.h | 60 + .../libutp (copy)/utp_templates.h | 195 + utp-client-interop/libutp (copy)/utp_types.h | 123 + .../libutp (copy)/utp_utils.cpp | 254 ++ utp-client-interop/libutp (copy)/utp_utils.h | 27 + 30 files changed, 7657 insertions(+) create mode 100644 utp-client-interop/libutp (copy)/LICENSE create mode 100644 utp-client-interop/libutp (copy)/Makefile create mode 100644 utp-client-interop/libutp (copy)/README.md create mode 100644 utp-client-interop/libutp (copy)/libutp-2012.vcxproj.filters create mode 100644 utp-client-interop/libutp (copy)/libutp.vcxproj create mode 100644 utp-client-interop/libutp (copy)/libutp_inet_ntop.cpp create mode 100644 utp-client-interop/libutp (copy)/libutp_inet_ntop.h create mode 100644 utp-client-interop/libutp (copy)/parse_log.py create mode 100644 utp-client-interop/libutp (copy)/portal-test.c create mode 100644 utp-client-interop/libutp (copy)/prop_sheets/RunTimeDebug.props create mode 100644 utp-client-interop/libutp (copy)/prop_sheets/RunTimeRelease.props create mode 100644 utp-client-interop/libutp (copy)/prop_sheets/debug-2012.props create mode 100644 utp-client-interop/libutp (copy)/prop_sheets/release-2012.props create mode 100644 utp-client-interop/libutp (copy)/prop_sheets/win32-2012.props create mode 100644 utp-client-interop/libutp (copy)/prop_sheets/x64-2012.props create mode 100644 utp-client-interop/libutp (copy)/ucat.c create mode 100644 utp-client-interop/libutp (copy)/utp.h create mode 100644 utp-client-interop/libutp (copy)/utp_api.cpp create mode 100644 utp-client-interop/libutp (copy)/utp_callbacks.cpp create mode 100644 utp-client-interop/libutp (copy)/utp_callbacks.h create mode 100644 utp-client-interop/libutp (copy)/utp_hash.cpp create mode 100644 utp-client-interop/libutp (copy)/utp_hash.h create mode 100644 utp-client-interop/libutp (copy)/utp_internal.cpp create mode 100644 utp-client-interop/libutp (copy)/utp_internal.h create mode 100644 utp-client-interop/libutp (copy)/utp_packedsockaddr.cpp create mode 100644 utp-client-interop/libutp (copy)/utp_packedsockaddr.h create mode 100644 utp-client-interop/libutp (copy)/utp_templates.h create mode 100644 utp-client-interop/libutp (copy)/utp_types.h create mode 100644 utp-client-interop/libutp (copy)/utp_utils.cpp create mode 100644 utp-client-interop/libutp (copy)/utp_utils.h diff --git a/utp-client-interop/libutp (copy)/LICENSE b/utp-client-interop/libutp (copy)/LICENSE new file mode 100644 index 0000000..7f6e16c --- /dev/null +++ b/utp-client-interop/libutp (copy)/LICENSE @@ -0,0 +1,19 @@ +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. diff --git a/utp-client-interop/libutp (copy)/Makefile b/utp-client-interop/libutp (copy)/Makefile new file mode 100644 index 0000000..8a19cfa --- /dev/null +++ b/utp-client-interop/libutp (copy)/Makefile @@ -0,0 +1,51 @@ +OBJS = utp_internal.o utp_utils.o utp_hash.o utp_callbacks.o utp_api.o utp_packedsockaddr.o +CFLAGS = -Wall -DPOSIX -g -fno-exceptions $(OPT) +OPT ?= -O3 +CXXFLAGS = $(CFLAGS) -fPIC -fno-rtti +CC = gcc +CXX = g++ + +CXXFLAGS += -Wno-sign-compare +CXXFLAGS += -fpermissive + +# Uncomment to enable utp_get_stats(), and a few extra sanity checks +#CFLAGS += -D_DEBUG + +# Uncomment to enable debug logging +#CFLAGS += -DUTP_DEBUG_LOGGING + +# Dynamically determine if librt is available. If so, assume we need to link +# against it for clock_gettime(2). This is required for clean builds on OSX; +# see for more. This should +# probably be ported to CMake at some point, but is suitable for now. +lrt := $(shell echo 'int main() {}' | $(CC) -xc -o /dev/null - -lrt >/dev/null 2>&1; echo $$?) +ifeq ($(strip $(lrt)),0) + LDFLAGS += -lrt +endif + +all: libutp.so libutp.a ucat ucat-static portal-test + +libutp.so: $(OBJS) + $(CXX) $(CXXFLAGS) -o libutp.so -shared $(OBJS) + +libutp.a: $(OBJS) + ar rvs libutp.a $(OBJS) + +ucat: ucat.o libutp.so + $(CC) $(CFLAGS) -o ucat ucat.o -L. -lutp $(LDFLAGS) + +ucat-static: ucat.o libutp.a + $(CXX) $(CXXFLAGS) -o ucat-static ucat.o libutp.a $(LDFLAGS) + +portal-test: portal-test.o libutp.a + $(CXX) $(CXXFLAGS) -o portal-test portal-test.o libutp.a $(LDFLAGS) + +clean: + rm -f *.o libutp.so libutp.a ucat ucat-static portal-test + +tags: $(shell ls *.cpp *.h) + rm -f tags + ctags *.cpp *.h + +anyway: clean all +.PHONY: clean all anyway diff --git a/utp-client-interop/libutp (copy)/README.md b/utp-client-interop/libutp (copy)/README.md new file mode 100644 index 0000000..a8e20ec --- /dev/null +++ b/utp-client-interop/libutp (copy)/README.md @@ -0,0 +1,68 @@ +# libutp - The uTorrent Transport Protocol library. +Copyright (c) 2010 BitTorrent, Inc. + +uTP is a TCP-like implementation of [LEDBAT][ledbat] documented as a BitTorrent +extension in [BEP-29][bep29]. uTP provides reliable, ordered delivery +while maintaining minimum extra delay. It is implemented on top of UDP to be +cross-platform and functional today. As a result, uTP is the primary transport +for uTorrent peer-to-peer connections. + +uTP is written in C++, but the external interface is strictly C (ANSI C89). + +## The Interface + +The uTP socket interface is a bit different from the Berkeley socket API to +avoid the need for our own select() implementation, and to make it easier to +write event-based code with minimal buffering. + +When you create a uTP socket, you register a set of callbacks. Most notably, the +on_read callback is a reactive callback which occurs when bytes arrive off the +network. The write side of the socket is proactive, and you call UTP_Write to +indicate the number of bytes you wish to write. As packets are created, the +on_write callback is called for each packet, so you can fill the buffers with +data. + +The libutp interface is not thread-safe. It was designed for use in a +single-threaded asyncronous context, although with proper synchronization +it may be used from a multi-threaded environment as well. + +See utp.h for more details and other API documentation. + +## Example + +See ucat.c. Build with: + + make ucat + +## Building + +uTP has been known to build on Windows with MSVC and on linux and OS X with gcc. +On Windows, use the MSVC project files (utp.sln, and friends). On other platforms, +building the shared library is as simple as: + + make + +To build one of the examples, which will statically link in everything it needs +from libutp: + + cd utp_test && make + +## Packaging and API + +The libutp API is considered unstable, and probably always will be. We encourage +you to test with the version of libutp you have, and be mindful when upgrading. +For this reason, it is probably also a good idea to bundle libutp with your +application. + +## License + +libutp is released under the [MIT][lic] license. + +## Related Work + +Research and analysis of congestion control mechanisms can be found [here.][survey] + +[ledbat]: http://datatracker.ietf.org/wg/ledbat/charter/ +[bep29]: http://www.bittorrent.org/beps/bep_0029.html +[lic]: http://www.opensource.org/licenses/mit-license.php +[survey]: http://datatracker.ietf.org/doc/draft-ietf-ledbat-survey/ diff --git a/utp-client-interop/libutp (copy)/libutp-2012.vcxproj.filters b/utp-client-interop/libutp (copy)/libutp-2012.vcxproj.filters new file mode 100644 index 0000000..d0eac57 --- /dev/null +++ b/utp-client-interop/libutp (copy)/libutp-2012.vcxproj.filters @@ -0,0 +1,65 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/libutp.vcxproj b/utp-client-interop/libutp (copy)/libutp.vcxproj new file mode 100644 index 0000000..580c7a6 --- /dev/null +++ b/utp-client-interop/libutp (copy)/libutp.vcxproj @@ -0,0 +1,258 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + WinRTDebug + Win32 + + + WinRTDebug + x64 + + + WinRTRelease + Win32 + + + WinRTRelease + x64 + + + + + + + + + + + + + + + + + + + + + + + + {5984D5CD-6ADD-4EB7-82E7-A555888FBBBD} + libutp2012 + libutp + + + + StaticLibrary + true + v140_xp + Unicode + + + StaticLibrary + true + v120_xp + Unicode + + + StaticLibrary + true + v120 + Unicode + + + StaticLibrary + true + v120 + Unicode + + + StaticLibrary + false + true + Unicode + v140_xp + + + StaticLibrary + false + true + Unicode + v120_xp + + + StaticLibrary + false + v120 + true + Unicode + + + StaticLibrary + false + v120 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + Sync + 4996 + + + true + + + + + Level3 + Disabled + MultiThreadedDebugDLL + + + true + + + + + Level3 + Disabled + + + true + + + + + Level3 + Disabled + + + true + + + + + Level3 + MaxSpeed + true + true + _WIN32_WINNT=0x501;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + Sync + + + true + true + true + + + + + Level3 + MaxSpeed + true + true + %(PreprocessorDefinitions) + MultiThreadedDLL + + + true + true + true + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/libutp_inet_ntop.cpp b/utp-client-interop/libutp (copy)/libutp_inet_ntop.cpp new file mode 100644 index 0000000..b0edf80 --- /dev/null +++ b/utp-client-interop/libutp (copy)/libutp_inet_ntop.cpp @@ -0,0 +1,108 @@ +/* + * 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. + */ + +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include "libutp_inet_ntop.h" + + +//###################################################################### +const char *libutp::inet_ntop(int af, const void *src, char *dest, size_t length) +{ + if (af != AF_INET && af != AF_INET6) + { + return NULL; + } + + SOCKADDR_STORAGE address; + DWORD address_length; + + if (af == AF_INET) + { + address_length = sizeof(sockaddr_in); + sockaddr_in* ipv4_address = (sockaddr_in*)(&address); + ipv4_address->sin_family = AF_INET; + ipv4_address->sin_port = 0; + memcpy(&ipv4_address->sin_addr, src, sizeof(in_addr)); + } + else // AF_INET6 + { + address_length = sizeof(sockaddr_in6); + sockaddr_in6* ipv6_address = (sockaddr_in6*)(&address); + ipv6_address->sin6_family = AF_INET6; + ipv6_address->sin6_port = 0; + ipv6_address->sin6_flowinfo = 0; + // hmmm + ipv6_address->sin6_scope_id = 0; + memcpy(&ipv6_address->sin6_addr, src, sizeof(in6_addr)); + } + + DWORD string_length = (DWORD)(length); + int result; + result = WSAAddressToStringA((sockaddr*)(&address), + address_length, 0, dest, + &string_length); + + // one common reason for this to fail is that ipv6 is not installed + + return result == SOCKET_ERROR ? NULL : dest; +} + +//###################################################################### +int libutp::inet_pton(int af, const char* src, void* dest) +{ + if (af != AF_INET && af != AF_INET6) + { + return -1; + } + + SOCKADDR_STORAGE address; + int address_length = sizeof(SOCKADDR_STORAGE); + int result = WSAStringToAddressA((char*)(src), af, 0, + (sockaddr*)(&address), + &address_length); + + if (af == AF_INET) + { + if (result != SOCKET_ERROR) + { + sockaddr_in* ipv4_address =(sockaddr_in*)(&address); + memcpy(dest, &ipv4_address->sin_addr, sizeof(in_addr)); + } + else if (strcmp(src, "255.255.255.255") == 0) + { + ((in_addr*)(dest))->s_addr = INADDR_NONE; + } + } + else // AF_INET6 + { + if (result != SOCKET_ERROR) + { + sockaddr_in6* ipv6_address = (sockaddr_in6*)(&address); + memcpy(dest, &ipv6_address->sin6_addr, sizeof(in6_addr)); + } + } + + return result == SOCKET_ERROR ? -1 : 1; +} diff --git a/utp-client-interop/libutp (copy)/libutp_inet_ntop.h b/utp-client-interop/libutp (copy)/libutp_inet_ntop.h new file mode 100644 index 0000000..33881d6 --- /dev/null +++ b/utp-client-interop/libutp (copy)/libutp_inet_ntop.h @@ -0,0 +1,68 @@ +#ifndef LIBUTP_INET_NTOP_H +#define LIBUTP_INET_NTOP_H + +/* + * 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. + */ + +// About us linking the system inet_pton and inet_ntop symbols: +// 1) These symbols are usually defined on POSIX systems +// 2) They are not defined on Windows versions earlier than Vista +// Defined in: +// ut_utils/src/sockaddr.cpp +// libutp/win32_inet_ntop.obj +// +// When we drop support for XP we can just #include , and use the system functions +// For now, we will always use our functions on windows, on all builds +// The reason is: we would like the debug build to behave as much as the release build as possible +// It is much better to catch a problem in the debug build, than to link the system version +// in debug, and our version int he wild. + +#if defined(_WIN32_WINNT) +#if _WIN32_WINNT >= 0x600 // Win32, post-XP +#include // for inet_ntop, inet_pton +#define INET_NTOP inet_ntop +#define INET_PTON inet_pton +#else +#define INET_NTOP libutp::inet_ntop // Win32, pre-XP: Use ours +#define INET_PTON libutp::inet_pton +#endif +#else // not WIN32 +#include // for inet_ntop, inet_pton +#define INET_NTOP inet_ntop +#define INET_PTON inet_pton +#endif + +//###################################################################### +//###################################################################### +namespace libutp { + + +//###################################################################### +const char *inet_ntop(int af, const void *src, char *dest, size_t length); + +//###################################################################### +int inet_pton(int af, const char* src, void* dest); + + +} //namespace libutp + +#endif // LIBUTP_INET_NTOP_H \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/parse_log.py b/utp-client-interop/libutp (copy)/parse_log.py new file mode 100644 index 0000000..44f6925 --- /dev/null +++ b/utp-client-interop/libutp (copy)/parse_log.py @@ -0,0 +1,288 @@ +import os, sys, time + +# usage: parse_log.py log-file [socket-index to focus on] + + +socket_filter = None +if len(sys.argv) >= 3: + socket_filter = sys.argv[2].strip() + +if socket_filter == None: + print "scanning for socket with the most packets" + file = open(sys.argv[1], 'rb') + + sockets = {} + + for l in file: + if not 'our_delay' in l: continue + + try: + a = l.strip().split(" ") + socket_index = a[1][:-1] + except: + continue + + # msvc's runtime library doesn't prefix pointers + # with '0x' +# if socket_index[:2] != '0x': +# continue + + if socket_index in sockets: + sockets[socket_index] += 1 + else: + sockets[socket_index] = 1 + + items = sockets.items() + items.sort(lambda x, y: y[1] - x[1]) + + count = 0 + for i in items: + print '%s: %d' % (i[0], i[1]) + count += 1 + if count > 5: break + + file.close() + socket_filter = items[0][0] + print '\nfocusing on socket %s' % socket_filter + +file = open(sys.argv[1], 'rb') +out_file = 'utp.out%s' % socket_filter; +out = open(out_file, 'wb') + +delay_samples = 'dots lc rgb "blue"' +delay_base = 'steps lw 2 lc rgb "purple"' +target_delay = 'steps lw 2 lc rgb "red"' +off_target = 'dots lc rgb "blue"' +cwnd = 'steps lc rgb "green"' +window_size = 'steps lc rgb "sea-green"' +rtt = 'lines lc rgb "light-blue"' + +metrics = { + 'our_delay':['our delay (ms)', 'x1y2', delay_samples], + 'upload_rate':['send rate (B/s)', 'x1y1', 'lines'], + 'max_window':['cwnd (B)', 'x1y1', cwnd], + 'target_delay':['target delay (ms)', 'x1y2', target_delay], + 'cur_window':['bytes in-flight (B)', 'x1y1', window_size], + 'cur_window_packets':['number of packets in-flight', 'x1y2', 'steps'], + 'packet_size':['current packet size (B)', 'x1y2', 'steps'], + 'rtt':['rtt (ms)', 'x1y2', rtt], + 'off_target':['off-target (ms)', 'x1y2', off_target], + 'delay_sum':['delay sum (ms)', 'x1y2', 'steps'], + 'their_delay':['their delay (ms)', 'x1y2', delay_samples], + 'get_microseconds':['clock (us)', 'x1y1', 'steps'], + 'wnduser':['advertised window size (B)', 'x1y1', 'steps'], + + 'delay_base':['delay base (us)', 'x1y1', delay_base], + 'their_delay_base':['their delay base (us)', 'x1y1', delay_base], + 'their_actual_delay':['their actual delay (us)', 'x1y1', delay_samples], + 'actual_delay':['actual_delay (us)', 'x1y1', delay_samples] +} + +histogram_quantization = 1 +socket_index = None + +columns = [] + +begin = None + +title = "-" +packet_loss = 0 +packet_timeout = 0 + +delay_histogram = {} +window_size = {'0': 0, '1': 0} + +# [35301484] 0x00ec1190: actual_delay:1021583 our_delay:102 their_delay:-1021345 off_target:297 max_window:2687 upload_rate:18942 delay_base:1021481154 delay_sum:-1021242 target_delay:400 acked_bytes:1441 cur_window:2882 scaled_gain:2.432 + +counter = 0 + +print "reading log file" + +for l in file: + if "UTP_Connect" in l: + title = l[:-2] + if socket_filter != None: + title += ' socket: %s' % socket_filter + else: + title += ' sum of all sockets' + continue + + try: + a = l.strip().split(" ") + t = a[0][1:-1] + socket_index = a[1][:-1] + except: + continue +# if socket_index[:2] != '0x': +# continue + + if socket_filter != None and socket_index != socket_filter: + continue + + counter += 1 + if (counter % 300 == 0): + print "\r%d " % counter, + + if "lost." in l: + packet_loss = packet_loss + 1 + continue + if "Packet timeout" in l: + packet_timeout = packet_timeout + 1 + continue + if "our_delay:" not in l: + continue + +# used for Logf timestamps +# t, m = t.split(".") +# t = time.strptime(t, "%H:%M:%S") +# t = list(t) +# t[0] += 107 +# t = tuple(t) +# m = float(m) +# m /= 1000.0 +# t = time.mktime(t) + m + +# used for tick count timestamps + t = int(t) + + if begin is None: + begin = t + t = t - begin + # print time. Convert from milliseconds to seconds + print >>out, '%f\t' % (float(t)/1000.), + + #if t > 200000: + # break + + fill_columns = not columns + for i in a[2:]: + try: + n, v = i.split(':') + except: + continue + v = float(v) + if n == "our_delay": + bucket = v / histogram_quantization + delay_histogram[bucket] = 1 + delay_histogram.get(bucket, 0) + if not n in metrics: continue + if fill_columns: + columns.append(n) + if n == "max_window": + window_size[socket_index] = v + print >>out, '%f\t' % int(reduce(lambda a,b: a+b, window_size.values())), + else: + print >>out, '%f\t' % v, + print >>out, float(packet_loss * 8000), float(packet_timeout * 8000) + packet_loss = 0 + packet_timeout = 0 + +out.close() + +out = open('%s.histogram' % out_file, 'wb') +for d,f in delay_histogram.iteritems(): + print >>out, float(d*histogram_quantization) + histogram_quantization / 2, f +out.close() + + +plot = [ + { + 'data': ['upload_rate', 'max_window', 'cur_window', 'wnduser', 'cur_window_packets', 'packet_size', 'rtt'], + 'title': 'send-packet-size', + 'y1': 'Bytes', + 'y2': 'Time (ms)' + }, + { + 'data': ['our_delay', 'max_window', 'target_delay', 'cur_window', 'wnduser', 'cur_window_packets'], + 'title': 'uploading', + 'y1': 'Bytes', + 'y2': 'Time (ms)' + }, + { + 'data': ['our_delay', 'max_window', 'target_delay', 'cur_window', 'cur_window_packets'], + 'title': 'uploading_packets', + 'y1': 'Bytes', + 'y2': 'Time (ms)' + }, + { + 'data': ['get_microseconds'], + 'title': 'timer', + 'y1': 'Time microseconds', + 'y2': 'Time (ms)' + }, + { + 'data': ['their_delay', 'target_delay', 'rtt'], + 'title': 'their_delay', + 'y1': '', + 'y2': 'Time (ms)' + }, + { + 'data': ['their_actual_delay','their_delay_base'], + 'title': 'their_delay_base', + 'y1': 'Time (us)', + 'y2': '' + }, + { + 'data': ['our_delay', 'target_delay', 'rtt'], + 'title': 'our-delay', + 'y1': '', + 'y2': 'Time (ms)' + }, + { + 'data': ['actual_delay', 'delay_base'], + 'title': 'our_delay_base', + 'y1': 'Time (us)', + 'y2': '' + } +] + +out = open('utp.gnuplot', 'w+') + +files = '' + +#print >>out, 'set xtics 0, 20' +print >>out, "set term png size 1280,800" +print >>out, 'set output "%s.delays.png"' % out_file +print >>out, 'set xrange [0:250]' +print >>out, 'set xlabel "delay (ms)"' +print >>out, 'set boxwidth 1' +print >>out, 'set style fill solid' +print >>out, 'set ylabel "number of packets"' +print >>out, 'plot "%s.histogram" using 1:2 with boxes' % out_file + +print >>out, "set style data steps" +#print >>out, "set yrange [0:*]" +print >>out, "set y2range [*:*]" +files += out_file + '.delays.png ' +#set hidden3d +#set title "Peer bandwidth distribution" +#set xlabel "Ratio" + +for p in plot: + print >>out, 'set title "%s %s"' % (p['title'], title) + print >>out, 'set xlabel "time (s)"' + print >>out, 'set ylabel "%s"' % p['y1'] + print >>out, "set tics nomirror" + print >>out, 'set y2tics' + print >>out, 'set y2label "%s"' % p['y2'] + print >>out, 'set xrange [0:*]' + print >>out, "set key box" + print >>out, "set term png size 1280,800" + print >>out, 'set output "%s-%s.png"' % (out_file, p['title']) + files += '%s-%s.png ' % (out_file, p['title']) + + comma = '' + print >>out, "plot", + + for c in p['data']: + if not c in metrics: continue + i = columns.index(c) + print >>out, '%s"%s" using 1:%d title "%s-%s" axes %s with %s' % (comma, out_file, i + 2, metrics[c][0], metrics[c][1], metrics[c][1], metrics[c][2]), + comma = ', ' + print >>out, '' + +out.close() + +os.system("gnuplot utp.gnuplot") + +os.system("open %s" % files) + diff --git a/utp-client-interop/libutp (copy)/portal-test.c b/utp-client-interop/libutp (copy)/portal-test.c new file mode 100644 index 0000000..11dcf23 --- /dev/null +++ b/utp-client-interop/libutp (copy)/portal-test.c @@ -0,0 +1,554 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ + #include + #include +#endif + +#include "utp.h" + +// options +int o_debug = 10; +char *o_local_address, *o_local_port, + *o_remote_address, *o_remote_port; +int o_listen; +int o_buf_size = 4096; +int o_numeric; + +utp_context *ctx; +utp_socket *s; + +int fd; +int buf_len = 0; +unsigned char *buf, *p; +int eof_flag, utp_eof_flag, utp_shutdown_flag, quit_flag, exit_code; + +void die(char *fmt, ...) +{ + va_list ap; + fflush(stdout); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); +} + +void debug(char *fmt, ...) +{ + va_list ap; + if (o_debug) { + fflush(stdout); + fprintf(stderr, "debug: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fflush(stderr); + } +} + +void pdie(char *err) +{ + debug("errno %d\n", errno); + fflush(stdout); + perror(err); + exit(1); +} + +void hexdump(const void *p, size_t len) +{ + int count = 1; + + while (len--) { + if (count == 1) + fprintf(stderr, " %p: ", p); + + fprintf(stderr, " %02x", *(unsigned char*)p++ & 0xff); + + if (count++ == 16) { + fprintf(stderr, "\n"); + count = 1; + } + } + + if (count != 1) + fprintf(stderr, "\n"); +} + +void write_data(void) +{ + if (!s) + goto out; + + while (p < buf+buf_len) { + size_t sent; + + sent = utp_write(s, p, buf+buf_len-p); + if (sent == 0) { + debug("socket no longer writable\n"); + return; + } + + p += sent; + + if (p == buf+buf_len) { + debug("wrote %zd bytes; buffer now empty\n", sent); + p = buf; + buf_len = 0; + } + else + debug("wrote %zd bytes; %d bytes left in buffer\n", sent, buf+buf_len-p); + } + + out: + if (buf_len == 0 && eof_flag) { + if (s) { + debug("Buffer empty, and previously found EOF. Shutdown socket\n"); + utp_shutdown_flag = 1; + if (!utp_eof_flag) { + utp_shutdown(s, SHUT_WR); + } else { + utp_close(s); + } + } + else { + quit_flag = 1; + } + } +} + +uint64 callback_on_read(utp_callback_arguments *a) +{ + const unsigned char *p; + ssize_t len, left; + + left = a->len; + p = a->buf; + + while (left) { + len = write(STDOUT_FILENO, p, left); + left -= len; + p += len; + debug("Wrote %d bytes, %d left\n", len, left); + } + utp_read_drained(a->socket); + return 0; +} + +uint64 callback_on_firewall(utp_callback_arguments *a) +{ + if (! o_listen) { + debug("Firewalling unexpected inbound connection in non-listen mode\n"); + return 1; + } + + if (s) { + debug("Firewalling unexpected second inbound connection\n"); + return 1; + } + + debug("Firewall allowing inbound connection\n"); + return 0; +} + +uint64 callback_on_accept(utp_callback_arguments *a) +{ + assert(!s); + s = a->socket; + debug("Accepted inbound socket %p\n", s); + write_data(); + return 0; +} + +uint64 callback_on_error(utp_callback_arguments *a) +{ + fprintf(stderr, "Error: %s\n", utp_error_code_names[a->error_code]); + utp_close(s); + s = NULL; + quit_flag = 1; + exit_code++; + return 0; +} + +uint64 callback_on_state_change(utp_callback_arguments *a) +{ + debug("state %d: %s\n", a->state, utp_state_names[a->state]); + utp_socket_stats *stats; + + switch (a->state) { + case UTP_STATE_CONNECT: + case UTP_STATE_WRITABLE: + write_data(); + break; + + case UTP_STATE_EOF: + debug("Received EOF from socket\n"); + utp_eof_flag = 1; + if (utp_shutdown_flag) { + utp_close(a->socket); + } + break; + + case UTP_STATE_DESTROYING: + debug("UTP socket is being destroyed; exiting\n"); + + stats = utp_get_stats(a->socket); + if (stats) { + debug("Socket Statistics:\n"); + debug(" Bytes sent: %d\n", stats->nbytes_xmit); + debug(" Bytes received: %d\n", stats->nbytes_recv); + debug(" Packets received: %d\n", stats->nrecv); + debug(" Packets sent: %d\n", stats->nxmit); + debug(" Duplicate receives: %d\n", stats->nduprecv); + debug(" Retransmits: %d\n", stats->rexmit); + debug(" Fast Retransmits: %d\n", stats->fastrexmit); + debug(" Best guess at MTU: %d\n", stats->mtu_guess); + } + else { + debug("No socket statistics available\n"); + } + + s = NULL; + quit_flag = 1; + break; + } + + return 0; +} + +uint64 callback_sendto(utp_callback_arguments *a) +{ + struct sockaddr_in *sin = (struct sockaddr_in *) a->address; + + debug("sendto: %zd byte packet to %s:%d%s\n", a->len, inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), + (a->flags & UTP_UDP_DONTFRAG) ? " (DF bit requested, but not yet implemented)" : ""); + + if (o_debug >= 3) + hexdump(a->buf, a->len); + + sendto(fd, a->buf, a->len, 0, a->address, a->address_len); + return 0; +} + +uint64 callback_log(utp_callback_arguments *a) +{ + fprintf(stderr, "log: %s\n", a->buf); + return 0; +} + +void handler(int number) +{ + debug("caught signal\n"); + if (s) + utp_close(s); + quit_flag = 1; + exit_code++; +} + +#ifdef __linux__ +void handle_icmp() +{ + while (1) { + unsigned char vec_buf[4096], ancillary_buf[4096]; + struct iovec iov = { vec_buf, sizeof(vec_buf) }; + struct sockaddr_in remote; + struct msghdr msg; + ssize_t len; + struct cmsghdr *cmsg; + struct sock_extended_err *e; + struct sockaddr *icmp_addr; + struct sockaddr_in *icmp_sin; + + memset(&msg, 0, sizeof(msg)); + + msg.msg_name = &remote; + msg.msg_namelen = sizeof(remote); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_flags = 0; + msg.msg_control = ancillary_buf; + msg.msg_controllen = sizeof(ancillary_buf); + + len = recvmsg(fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT); + + if (len < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) + break; + else + pdie("recvmsg"); + } + + for (cmsg = CMSG_FIRSTHDR(&msg); + cmsg; + cmsg = CMSG_NXTHDR(&msg, cmsg)) + { + if (cmsg->cmsg_type != IP_RECVERR) { + debug("Unhandled errqueue type: %d\n", cmsg->cmsg_type); + continue; + } + + if (cmsg->cmsg_level != SOL_IP) { + debug("Unhandled errqueue level: %d\n", cmsg->cmsg_level); + continue; + } + + debug("errqueue: IP_RECVERR, SOL_IP, len %zd\n", cmsg->cmsg_len); + + if (remote.sin_family != AF_INET) { + debug("Address family is %d, not AF_INET? Ignoring\n", remote.sin_family); + continue; + } + + debug("Remote host: %s:%d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port)); + + e = (struct sock_extended_err *) CMSG_DATA(cmsg); + + if (!e) { + debug("errqueue: sock_extended_err is NULL?\n"); + continue; + } + + if (e->ee_origin != SO_EE_ORIGIN_ICMP) { + debug("errqueue: Unexpected origin: %d\n", e->ee_origin); + continue; + } + + debug(" ee_errno: %d\n", e->ee_errno); + debug(" ee_origin: %d\n", e->ee_origin); + debug(" ee_type: %d\n", e->ee_type); + debug(" ee_code: %d\n", e->ee_code); + debug(" ee_info: %d\n", e->ee_info); // discovered MTU for EMSGSIZE errors + debug(" ee_data: %d\n", e->ee_data); + + // "Node that caused the error" + // "Node that generated the error" + icmp_addr = (struct sockaddr *) SO_EE_OFFENDER(e); + icmp_sin = (struct sockaddr_in *) icmp_addr; + + if (icmp_addr->sa_family != AF_INET) { + debug("ICMP's address family is %d, not AF_INET?\n", icmp_addr->sa_family); + continue; + } + + if (icmp_sin->sin_port != 0) { + debug("ICMP's 'port' is not 0?\n"); + continue; + } + + debug("msg_flags: %d", msg.msg_flags); + if (o_debug) { + if (msg.msg_flags & MSG_TRUNC) fprintf(stderr, " MSG_TRUNC"); + if (msg.msg_flags & MSG_CTRUNC) fprintf(stderr, " MSG_CTRUNC"); + if (msg.msg_flags & MSG_EOR) fprintf(stderr, " MSG_EOR"); + if (msg.msg_flags & MSG_OOB) fprintf(stderr, " MSG_OOB"); + if (msg.msg_flags & MSG_ERRQUEUE) fprintf(stderr, " MSG_ERRQUEUE"); + fprintf(stderr, "\n"); + } + + if (o_debug >= 3) + hexdump(vec_buf, len); + + if (e->ee_type == 3 && e->ee_code == 4) { + debug("ICMP type 3, code 4: Fragmentation error, discovered MTU %d\n", e->ee_info); + utp_process_icmp_fragmentation(ctx, vec_buf, len, (struct sockaddr *)&remote, sizeof(remote), e->ee_info); + } + else { + debug("ICMP type %d, code %d\n", e->ee_type, e->ee_code); + utp_process_icmp_error(ctx, vec_buf, len, (struct sockaddr *)&remote, sizeof(remote)); + } + } + } +} +#endif + +void network_loop(void) +{ + unsigned char socket_data[4096]; + struct sockaddr_in src_addr; + socklen_t addrlen = sizeof(src_addr); + ssize_t len; + int ret; + + struct pollfd p[2]; + + p[0].fd = STDIN_FILENO; + p[0].events = (o_buf_size-buf_len && !eof_flag) ? POLLIN : 0; + + p[1].fd = fd; + p[1].events = POLLIN; + + ret = poll(p, 2, 500); + if (ret < 0) { + if (errno == EINTR) + debug("poll() returned EINTR\n"); + else + pdie("poll"); + } + else if (ret == 0) { + if (o_debug >= 3) + debug("poll() timeout\n"); + } + else { + if ((p[0].revents & POLLIN) == POLLIN) { + len = read(STDIN_FILENO, buf+buf_len, o_buf_size-buf_len); + if (len < 0 && errno != EINTR) + pdie("read stdin"); + if (len == 0) { + debug("EOF from file\n"); + eof_flag = 1; + close(STDIN_FILENO); + } + else { + buf_len += len; + debug("Read %d bytes, buffer now %d bytes long\n", len, buf_len); + } + write_data(); + } + +#ifdef __linux__ + if ((p[1].revents & POLLERR) == POLLERR) + handle_icmp(); +#endif + + if ((p[1].revents & POLLIN) == POLLIN) { + while (1) { + len = recvfrom(fd, socket_data, sizeof(socket_data), MSG_DONTWAIT, (struct sockaddr *)&src_addr, &addrlen); + if (len < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + utp_issue_deferred_acks(ctx); + break; + } + else + pdie("recv"); + } + + debug("Received %zd byte UDP packet from %s:%d\n", len, inet_ntoa(src_addr.sin_addr), ntohs(src_addr.sin_port)); + if (o_debug >= 3) + hexdump(socket_data, len); + + if (! utp_process_udp(ctx, socket_data, len, (struct sockaddr *)&src_addr, addrlen)) + debug("UDP packet not handled by UTP. Ignoring.\n"); + } + } + } + + utp_check_timeouts(ctx); +} + +int main(int argc, char *argv[]) +{ + o_local_address = "0.0.0.0"; + + // setup start + struct addrinfo hints, *res; + struct sockaddr_in sin, *sinp; + int error; + struct sigaction sigIntHandler; + + sigIntHandler.sa_handler = handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + + sigaction(SIGINT, &sigIntHandler, NULL); + + p = buf = malloc(o_buf_size); + if (!buf) + pdie("malloc"); + debug("Allocatd %d buffer\n", o_buf_size); + + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (fd < 0) + pdie("socket"); + +#ifdef __linux__ + int on = 1; + if (setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on)) != 0) + pdie("setsockopt"); +#endif + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_protocol = IPPROTO_UDP; + if (o_numeric) + hints.ai_flags |= AI_NUMERICHOST; + + o_local_address = "127.0.0.1"; + o_local_port = "9079"; + + if ((error = getaddrinfo(o_local_address, o_local_port, &hints, &res))) + die("getaddrinfo: %s\n", gai_strerror(error)); + + if (bind(fd, res->ai_addr, res->ai_addrlen) != 0) + pdie("bind"); + + freeaddrinfo(res); + + socklen_t len = sizeof(sin); + if (getsockname(fd, (struct sockaddr *) &sin, &len) != 0) + pdie("getsockname"); + debug("Bound to local %s:%d\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); + + ctx = utp_init(2); + assert(ctx); + debug("UTP context %p\n", ctx); + + utp_set_callback(ctx, UTP_LOG, &callback_log); + utp_set_callback(ctx, UTP_SENDTO, &callback_sendto); + utp_set_callback(ctx, UTP_ON_ERROR, &callback_on_error); + utp_set_callback(ctx, UTP_ON_STATE_CHANGE, &callback_on_state_change); + utp_set_callback(ctx, UTP_ON_READ, &callback_on_read); + utp_set_callback(ctx, UTP_ON_FIREWALL, &callback_on_firewall); + utp_set_callback(ctx, UTP_ON_ACCEPT, &callback_on_accept); + + if (o_debug >= 2) { + utp_context_set_option(ctx, UTP_LOG_NORMAL, 1); + utp_context_set_option(ctx, UTP_LOG_MTU, 1); + utp_context_set_option(ctx, UTP_LOG_DEBUG, 1); + } + + s = utp_create_socket(ctx); + assert(s); + debug("UTP socket %p\n", s); + + // node we are connecting to + o_remote_address = "127.0.0.1"; + o_remote_port = "9078"; + if ((error = getaddrinfo(o_remote_address, o_remote_port, &hints, &res))) + die("getaddrinfo: %s\n", gai_strerror(error)); + + sinp = (struct sockaddr_in *)res->ai_addr; + debug("Connecting to %s:%d\n", inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port)); + debug("bing2\n"); + int connect_number = utp_connect(s, res->ai_addr, res->ai_addrlen); + debug("connect_number %zd\n", connect_number); + freeaddrinfo(res); + // setup end + + // write code comment out to observe keep alive + unsigned char * bob = "big breadBBAABBAA\0"; + strcpy((char*) p, (const char*) bob); // compiles (but bad practice) + strcpy((char*) buf, (const char*) bob); // compiles (but bad practice) + buf_len = 19; + // write code end + + while (!quit_flag) + network_loop(); + + debug("Destroying context\n"); + utp_destroy(ctx); + return exit_code; +} diff --git a/utp-client-interop/libutp (copy)/prop_sheets/RunTimeDebug.props b/utp-client-interop/libutp (copy)/prop_sheets/RunTimeDebug.props new file mode 100644 index 0000000..be505a5 --- /dev/null +++ b/utp-client-interop/libutp (copy)/prop_sheets/RunTimeDebug.props @@ -0,0 +1,12 @@ + + + + + + + + MultiThreadedDebug + + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/prop_sheets/RunTimeRelease.props b/utp-client-interop/libutp (copy)/prop_sheets/RunTimeRelease.props new file mode 100644 index 0000000..9751a81 --- /dev/null +++ b/utp-client-interop/libutp (copy)/prop_sheets/RunTimeRelease.props @@ -0,0 +1,12 @@ + + + + + + + + MultiThreaded + + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/prop_sheets/debug-2012.props b/utp-client-interop/libutp (copy)/prop_sheets/debug-2012.props new file mode 100644 index 0000000..4a46b12 --- /dev/null +++ b/utp-client-interop/libutp (copy)/prop_sheets/debug-2012.props @@ -0,0 +1,46 @@ + + + + + + + + $(SolutionDir)Build\$(PlatformName)\$(Configuration)\ + $(OutDir)$(ProjectName)\ + + + + Level3 + _DEBUG;WIN32;ENABLE_I18N;ENABLE_SRP=1;%(PreprocessorDefinitions) + false + false + false + Default + $(SolutionDir);$(SolutionDir)\yajl\src;$(SolutionDir)\ut_core\src;$(SolutionDir)\verification_lib;$(SolutionDir)\..\libtomcrypt\src\headers + true + false + false + c:\temp\$(ProjectName)-$(ConfigurationName)-$(PlatformName)-master.pch + true + false + + + true + + + true + false + + + true + + + BRANDED_UTORRENT;%(PreprocessorDefinitions) + + + + $(SolutionDir)\ut_core\src;%(AdditionalIncludeDirectories) + + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/prop_sheets/release-2012.props b/utp-client-interop/libutp (copy)/prop_sheets/release-2012.props new file mode 100644 index 0000000..59073ae --- /dev/null +++ b/utp-client-interop/libutp (copy)/prop_sheets/release-2012.props @@ -0,0 +1,51 @@ + + + + + + + + $(SolutionDir)Build\$(PlatformName)\$(Configuration)\ + $(OutDir)$(ProjectName)\ + + + + Level3 + $(SolutionDir);$(SolutionDir)\yajl\src;$(SolutionDir)\ut_core\src;$(SolutionDir)\verification_lib;$(SolutionDir)\..\libtomcrypt\src\headers + MinSpace + AnySuitable + true + Size + true + true + false + false + false + true + false + true + StdCall + Default + false + NDEBUG;WIN32;ENABLE_I18N;ENABLE_SRP=1;%(PreprocessorDefinitions) + false + c:\temp\$(ProjectName)-$(ConfigurationName)-$(PlatformName)-master.pch + true + + + + true + + + BRANDED_UTORRENT;%(PreprocessorDefinitions) + + + + $(SolutionDir)\ut_core\src;%(AdditionalIncludeDirectories) + + + false + + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/prop_sheets/win32-2012.props b/utp-client-interop/libutp (copy)/prop_sheets/win32-2012.props new file mode 100644 index 0000000..cafd7f6 --- /dev/null +++ b/utp-client-interop/libutp (copy)/prop_sheets/win32-2012.props @@ -0,0 +1,16 @@ + + + + + + + + MachineX86 + + + 4Bytes + NoExtensions + + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/prop_sheets/x64-2012.props b/utp-client-interop/libutp (copy)/prop_sheets/x64-2012.props new file mode 100644 index 0000000..42b6beb --- /dev/null +++ b/utp-client-interop/libutp (copy)/prop_sheets/x64-2012.props @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/utp-client-interop/libutp (copy)/ucat.c b/utp-client-interop/libutp (copy)/ucat.c new file mode 100644 index 0000000..a8fdfc3 --- /dev/null +++ b/utp-client-interop/libutp (copy)/ucat.c @@ -0,0 +1,636 @@ +// vim:set ts=4 sw=4 ai: + +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ + #include + #include +#endif + +#include "utp.h" + +// options +int o_debug = 10; +char *o_local_address, *o_local_port, + *o_remote_address, *o_remote_port; +int o_listen; +int o_buf_size = 4096; +int o_numeric; + +utp_context *ctx; +utp_socket *s; + +int fd; +int buf_len = 0; +unsigned char *buf, *p; +int eof_flag, utp_eof_flag, utp_shutdown_flag, quit_flag, exit_code; + +void die(char *fmt, ...) +{ + va_list ap; + fflush(stdout); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); +} + +void debug(char *fmt, ...) +{ + va_list ap; + if (o_debug) { + fflush(stdout); + fprintf(stderr, "debug: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fflush(stderr); + } +} + +void pdie(char *err) +{ + debug("errno %d\n", errno); + fflush(stdout); + perror(err); + exit(1); +} + +void hexdump(const void *p, size_t len) +{ + int count = 1; + + while (len--) { + if (count == 1) + fprintf(stderr, " %p: ", p); + + fprintf(stderr, " %02x", *(unsigned char*)p++ & 0xff); + + if (count++ == 16) { + fprintf(stderr, "\n"); + count = 1; + } + } + + if (count != 1) + fprintf(stderr, "\n"); +} + +void handler(int number) +{ + debug("caught signal\n"); + if (s) + utp_close(s); + quit_flag = 1; + exit_code++; +} + +void write_data(void) +{ + if (! s) + goto out; + + while (p < buf+buf_len) { + size_t sent; + + sent = utp_write(s, p, buf+buf_len-p); + if (sent == 0) { + debug("socket no longer writable\n"); + return; + } + + p += sent; + + if (p == buf+buf_len) { + debug("wrote %zd bytes; buffer now empty\n", sent); + p = buf; + buf_len = 0; + } + else + debug("wrote %zd bytes; %d bytes left in buffer\n", sent, buf+buf_len-p); + } + +out: + if (buf_len == 0 && eof_flag) { + if (s) { + debug("Buffer empty, and previously found EOF. Shutdown socket\n"); + utp_shutdown_flag = 1; + if (!utp_eof_flag) { + utp_shutdown(s, SHUT_WR); + } else { + utp_close(s); + } + } + else { + quit_flag = 1; + } + } +} + +uint64 callback_on_read(utp_callback_arguments *a) +{ + const unsigned char *p; + ssize_t len, left; + + left = a->len; + p = a->buf; + + while (left) { + len = write(STDOUT_FILENO, p, left); + left -= len; + p += len; + debug("Wrote %d bytes, %d left\n", len, left); + } + utp_read_drained(a->socket); + return 0; +} + +uint64 callback_on_firewall(utp_callback_arguments *a) +{ + if (! o_listen) { + debug("Firewalling unexpected inbound connection in non-listen mode\n"); + return 1; + } + + if (s) { + debug("Firewalling unexpected second inbound connection\n"); + return 1; + } + + debug("Firewall allowing inbound connection\n"); + return 0; +} + +uint64 callback_on_accept(utp_callback_arguments *a) +{ + assert(!s); + s = a->socket; + debug("Accepted inbound socket %p\n", s); + write_data(); + return 0; +} + +uint64 callback_on_error(utp_callback_arguments *a) +{ + fprintf(stderr, "Error: %s\n", utp_error_code_names[a->error_code]); + utp_close(s); + s = NULL; + quit_flag = 1; + exit_code++; + return 0; +} + +uint64 callback_on_state_change(utp_callback_arguments *a) +{ + debug("state %d: %s\n", a->state, utp_state_names[a->state]); + utp_socket_stats *stats; + + switch (a->state) { + case UTP_STATE_CONNECT: + case UTP_STATE_WRITABLE: + write_data(); + break; + + case UTP_STATE_EOF: + debug("Received EOF from socket\n"); + utp_eof_flag = 1; + if (utp_shutdown_flag) { + utp_close(a->socket); + } + break; + + case UTP_STATE_DESTROYING: + debug("UTP socket is being destroyed; exiting\n"); + + stats = utp_get_stats(a->socket); + if (stats) { + debug("Socket Statistics:\n"); + debug(" Bytes sent: %d\n", stats->nbytes_xmit); + debug(" Bytes received: %d\n", stats->nbytes_recv); + debug(" Packets received: %d\n", stats->nrecv); + debug(" Packets sent: %d\n", stats->nxmit); + debug(" Duplicate receives: %d\n", stats->nduprecv); + debug(" Retransmits: %d\n", stats->rexmit); + debug(" Fast Retransmits: %d\n", stats->fastrexmit); + debug(" Best guess at MTU: %d\n", stats->mtu_guess); + } + else { + debug("No socket statistics available\n"); + } + + s = NULL; + quit_flag = 1; + break; + } + + return 0; +} + +uint64 callback_sendto(utp_callback_arguments *a) +{ + struct sockaddr_in *sin = (struct sockaddr_in *) a->address; + + debug("sendto: %zd byte packet to %s:%d%s\n", a->len, inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), + (a->flags & UTP_UDP_DONTFRAG) ? " (DF bit requested, but not yet implemented)" : ""); + + if (o_debug >= 3) + hexdump(a->buf, a->len); + + sendto(fd, a->buf, a->len, 0, a->address, a->address_len); + return 0; +} + +uint64 callback_log(utp_callback_arguments *a) +{ + fprintf(stderr, "log: %s\n", a->buf); + return 0; +} + +void setup(void) +{ + struct addrinfo hints, *res; + struct sockaddr_in sin, *sinp; + int error; + struct sigaction sigIntHandler; + + sigIntHandler.sa_handler = handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + + sigaction(SIGINT, &sigIntHandler, NULL); + + p = buf = malloc(o_buf_size); + if (!buf) + pdie("malloc"); + debug("Allocatd %d buffer\n", o_buf_size); + + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (fd < 0) + pdie("socket"); + + #ifdef __linux__ + int on = 1; + if (setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on)) != 0) + pdie("setsockopt"); + #endif + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_protocol = IPPROTO_UDP; + if (o_numeric) + hints.ai_flags |= AI_NUMERICHOST; + + if ((error = getaddrinfo(o_local_address, o_local_port, &hints, &res))) + die("getaddrinfo: %s\n", gai_strerror(error)); + + if (bind(fd, res->ai_addr, res->ai_addrlen) != 0) + pdie("bind"); + + freeaddrinfo(res); + + socklen_t len = sizeof(sin); + if (getsockname(fd, (struct sockaddr *) &sin, &len) != 0) + pdie("getsockname"); + debug("Bound to local %s:%d\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); + + ctx = utp_init(2); + assert(ctx); + debug("UTP context %p\n", ctx); + + utp_set_callback(ctx, UTP_LOG, &callback_log); + utp_set_callback(ctx, UTP_SENDTO, &callback_sendto); + utp_set_callback(ctx, UTP_ON_ERROR, &callback_on_error); + utp_set_callback(ctx, UTP_ON_STATE_CHANGE, &callback_on_state_change); + utp_set_callback(ctx, UTP_ON_READ, &callback_on_read); + utp_set_callback(ctx, UTP_ON_FIREWALL, &callback_on_firewall); + utp_set_callback(ctx, UTP_ON_ACCEPT, &callback_on_accept); + + if (o_debug >= 2) { + utp_context_set_option(ctx, UTP_LOG_NORMAL, 1); + utp_context_set_option(ctx, UTP_LOG_MTU, 1); + utp_context_set_option(ctx, UTP_LOG_DEBUG, 1); + } + + if (! o_listen) { + s = utp_create_socket(ctx); + assert(s); + debug("UTP socket %p\n", s); + + if ((error = getaddrinfo(o_remote_address, o_remote_port, &hints, &res))) + die("getaddrinfo: %s\n", gai_strerror(error)); + + sinp = (struct sockaddr_in *)res->ai_addr; + debug("Connecting to %s:%d\n", inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port)); + + utp_connect(s, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); + } +} + +#ifdef __linux__ +void handle_icmp() +{ + while (1) { + unsigned char vec_buf[4096], ancillary_buf[4096]; + struct iovec iov = { vec_buf, sizeof(vec_buf) }; + struct sockaddr_in remote; + struct msghdr msg; + ssize_t len; + struct cmsghdr *cmsg; + struct sock_extended_err *e; + struct sockaddr *icmp_addr; + struct sockaddr_in *icmp_sin; + + memset(&msg, 0, sizeof(msg)); + + msg.msg_name = &remote; + msg.msg_namelen = sizeof(remote); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_flags = 0; + msg.msg_control = ancillary_buf; + msg.msg_controllen = sizeof(ancillary_buf); + + len = recvmsg(fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT); + + if (len < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) + break; + else + pdie("recvmsg"); + } + + for (cmsg = CMSG_FIRSTHDR(&msg); + cmsg; + cmsg = CMSG_NXTHDR(&msg, cmsg)) + { + if (cmsg->cmsg_type != IP_RECVERR) { + debug("Unhandled errqueue type: %d\n", cmsg->cmsg_type); + continue; + } + + if (cmsg->cmsg_level != SOL_IP) { + debug("Unhandled errqueue level: %d\n", cmsg->cmsg_level); + continue; + } + + debug("errqueue: IP_RECVERR, SOL_IP, len %zd\n", cmsg->cmsg_len); + + if (remote.sin_family != AF_INET) { + debug("Address family is %d, not AF_INET? Ignoring\n", remote.sin_family); + continue; + } + + debug("Remote host: %s:%d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port)); + + e = (struct sock_extended_err *) CMSG_DATA(cmsg); + + if (!e) { + debug("errqueue: sock_extended_err is NULL?\n"); + continue; + } + + if (e->ee_origin != SO_EE_ORIGIN_ICMP) { + debug("errqueue: Unexpected origin: %d\n", e->ee_origin); + continue; + } + + debug(" ee_errno: %d\n", e->ee_errno); + debug(" ee_origin: %d\n", e->ee_origin); + debug(" ee_type: %d\n", e->ee_type); + debug(" ee_code: %d\n", e->ee_code); + debug(" ee_info: %d\n", e->ee_info); // discovered MTU for EMSGSIZE errors + debug(" ee_data: %d\n", e->ee_data); + + // "Node that caused the error" + // "Node that generated the error" + icmp_addr = (struct sockaddr *) SO_EE_OFFENDER(e); + icmp_sin = (struct sockaddr_in *) icmp_addr; + + if (icmp_addr->sa_family != AF_INET) { + debug("ICMP's address family is %d, not AF_INET?\n", icmp_addr->sa_family); + continue; + } + + if (icmp_sin->sin_port != 0) { + debug("ICMP's 'port' is not 0?\n"); + continue; + } + + debug("msg_flags: %d", msg.msg_flags); + if (o_debug) { + if (msg.msg_flags & MSG_TRUNC) fprintf(stderr, " MSG_TRUNC"); + if (msg.msg_flags & MSG_CTRUNC) fprintf(stderr, " MSG_CTRUNC"); + if (msg.msg_flags & MSG_EOR) fprintf(stderr, " MSG_EOR"); + if (msg.msg_flags & MSG_OOB) fprintf(stderr, " MSG_OOB"); + if (msg.msg_flags & MSG_ERRQUEUE) fprintf(stderr, " MSG_ERRQUEUE"); + fprintf(stderr, "\n"); + } + + if (o_debug >= 3) + hexdump(vec_buf, len); + + if (e->ee_type == 3 && e->ee_code == 4) { + debug("ICMP type 3, code 4: Fragmentation error, discovered MTU %d\n", e->ee_info); + utp_process_icmp_fragmentation(ctx, vec_buf, len, (struct sockaddr *)&remote, sizeof(remote), e->ee_info); + } + else { + debug("ICMP type %d, code %d\n", e->ee_type, e->ee_code); + utp_process_icmp_error(ctx, vec_buf, len, (struct sockaddr *)&remote, sizeof(remote)); + } + } + } +} +#endif + +void network_loop(void) +{ + unsigned char socket_data[4096]; + struct sockaddr_in src_addr; + socklen_t addrlen = sizeof(src_addr); + ssize_t len; + int ret; + + struct pollfd p[2]; + + p[0].fd = STDIN_FILENO; + p[0].events = (o_buf_size-buf_len && !eof_flag) ? POLLIN : 0; + + p[1].fd = fd; + p[1].events = POLLIN; + + ret = poll(p, 2, 500); + if (ret < 0) { + if (errno == EINTR) + debug("poll() returned EINTR\n"); + else + pdie("poll"); + } + else if (ret == 0) { + if (o_debug >= 3) + debug("poll() timeout\n"); + } + else { + if ((p[0].revents & POLLIN) == POLLIN) { + len = read(STDIN_FILENO, buf+buf_len, o_buf_size-buf_len); + if (len < 0 && errno != EINTR) + pdie("read stdin"); + if (len == 0) { + debug("EOF from file\n"); + eof_flag = 1; + close(STDIN_FILENO); + } + else { + buf_len += len; + debug("Read %d bytes, buffer now %d bytes long\n", len, buf_len); + } + write_data(); + } + + #ifdef __linux__ + if ((p[1].revents & POLLERR) == POLLERR) + handle_icmp(); + #endif + + if ((p[1].revents & POLLIN) == POLLIN) { + while (1) { + len = recvfrom(fd, socket_data, sizeof(socket_data), MSG_DONTWAIT, (struct sockaddr *)&src_addr, &addrlen); + if (len < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + utp_issue_deferred_acks(ctx); + break; + } + else + pdie("recv"); + } + + debug("Received %zd byte UDP packet from %s:%d\n", len, inet_ntoa(src_addr.sin_addr), ntohs(src_addr.sin_port)); + if (o_debug >= 3) + hexdump(socket_data, len); + + if (! utp_process_udp(ctx, socket_data, len, (struct sockaddr *)&src_addr, addrlen)) + debug("UDP packet not handled by UTP. Ignoring.\n"); + } + } + } + + utp_check_timeouts(ctx); +} + +void usage(char *name) +{ + fprintf(stderr, "\nUsage:\n"); + fprintf(stderr, " %s [options] \n", name); + fprintf(stderr, " %s [options] -l -p \n", name); + fprintf(stderr, "\n"); + fprintf(stderr, "Options:\n"); + fprintf(stderr, " -h Help\n"); + fprintf(stderr, " -d Debug mode; use multiple times to increase verbosity.\n"); + fprintf(stderr, " -l Listen mode\n"); + fprintf(stderr, " -p Local port\n"); + fprintf(stderr, " -s Source IP\n"); + fprintf(stderr, " -B Buffer size\n"); + fprintf(stderr, " -n Don't resolve hostnames\n"); + fprintf(stderr, "\n"); + exit(1); +} + +int main(int argc, char *argv[]) +{ + int i; + + o_local_address = "0.0.0.0"; + + while (1) { + int c = getopt (argc, argv, "hdlp:B:s:n"); + if (c == -1) break; + switch(c) { + case 'h': usage(argv[0]); break; + case 'd': o_debug++; break; + case 'l': o_listen++; break; + case 'p': o_local_port = optarg; break; + case 'B': o_buf_size = atoi(optarg); break; + case 's': o_local_address = optarg; break; + case 'n': o_numeric++; break; + //case 'w': break; // timeout for connects and final net reads + default: + die("Unhandled argument: %c\n", c); + } + } + + for (i = optind; i < argc; i++) { + switch(i - optind) { + case 0: o_remote_address = argv[i]; break; + case 1: o_remote_port = argv[i]; break; + } + } + + if (o_listen && (o_remote_port || o_remote_address)) + usage(argv[0]); + + if (! o_listen && (!o_remote_port || !o_remote_address)) + usage(argv[0]); + + setup(); + while (!quit_flag) + network_loop(); + + if (buf_len) { + fprintf(stderr, "Warning: send buffer not empty\n"); + exit_code++; + } + + utp_context_stats *stats = utp_get_context_stats(ctx); + + if (stats) { + debug(" Bucket size: <23 <373 <723 <1400 >1400\n"); + debug("Number of packets sent: %5d %5d %5d %5d %5d\n", + stats->_nraw_send[0], stats->_nraw_send[1], stats->_nraw_send[2], stats->_nraw_send[3], stats->_nraw_send[4]); + debug("Number of packets recv: %5d %5d %5d %5d %5d\n", + stats->_nraw_recv[0], stats->_nraw_recv[1], stats->_nraw_recv[2], stats->_nraw_recv[3], stats->_nraw_recv[4]); + } + else { + debug("utp_get_context_stats() failed?\n"); + } + + debug("Destroying context\n"); + utp_destroy(ctx); + return exit_code; +} diff --git a/utp-client-interop/libutp (copy)/utp.h b/utp-client-interop/libutp (copy)/utp.h new file mode 100644 index 0000000..01ae70b --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp.h @@ -0,0 +1,183 @@ +/* + * 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 +#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_shutdown (utp_socket *s, int how); +void utp_close (utp_socket *s); + +#ifdef __cplusplus +} +#endif + +#endif //__UTP_H__ diff --git a/utp-client-interop/libutp (copy)/utp_api.cpp b/utp-client-interop/libutp (copy)/utp_api.cpp new file mode 100644 index 0000000..63aff18 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_api.cpp @@ -0,0 +1,139 @@ +// vim:set ts=4 sw=4 ai: + +/* + * 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. + */ + +#include +#include "utp_internal.h" +#include "utp_utils.h" + +extern "C" { + +const char * utp_callback_names[] = { + "UTP_ON_FIREWALL", + "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", +}; + +const char * utp_error_code_names[] = { + "UTP_ECONNREFUSED", + "UTP_ECONNRESET", + "UTP_ETIMEDOUT", +}; + +const char *utp_state_names[] = { + NULL, + "UTP_STATE_CONNECT", + "UTP_STATE_WRITABLE", + "UTP_STATE_EOF", + "UTP_STATE_DESTROYING", +}; + +struct_utp_context::struct_utp_context() + : userdata(NULL) + , current_ms(0) + , last_utp_socket(NULL) + , log_normal(false) + , log_mtu(false) + , log_debug(false) +{ + memset(&context_stats, 0, sizeof(context_stats)); + memset(callbacks, 0, sizeof(callbacks)); + target_delay = CCONTROL_TARGET; + utp_sockets = new UTPSocketHT; + + callbacks[UTP_GET_UDP_MTU] = &utp_default_get_udp_mtu; + callbacks[UTP_GET_UDP_OVERHEAD] = &utp_default_get_udp_overhead; + callbacks[UTP_GET_MILLISECONDS] = &utp_default_get_milliseconds; + callbacks[UTP_GET_MICROSECONDS] = &utp_default_get_microseconds; + callbacks[UTP_GET_RANDOM] = &utp_default_get_random; + + // 1 MB of receive buffer (i.e. max bandwidth delay product) + // means that from a peer with 200 ms RTT, we cannot receive + // faster than 5 MB/s + // from a peer with 10 ms RTT, we cannot receive faster than + // 100 MB/s. This is assumed to be good enough, since bandwidth + // often is proportional to RTT anyway + // when setting a download rate limit, all sockets should have + // their receive buffer set much lower, to say 60 kiB or so + opt_rcvbuf = opt_sndbuf = 1024 * 1024; + last_check = 0; +} + +struct_utp_context::~struct_utp_context() { + delete this->utp_sockets; +} + +utp_context* utp_init (int version) +{ + assert(version == 2); + if (version != 2) + return NULL; + utp_context *ctx = new utp_context; + return ctx; +} + +void utp_destroy(utp_context *ctx) { + assert(ctx); + if (ctx) delete ctx; +} + +void utp_set_callback(utp_context *ctx, int callback_name, utp_callback_t *proc) { + assert(ctx); + if (ctx) ctx->callbacks[callback_name] = proc; +} + +void* utp_context_set_userdata(utp_context *ctx, void *userdata) { + assert(ctx); + if (ctx) ctx->userdata = userdata; + return ctx ? ctx->userdata : NULL; +} + +void* utp_context_get_userdata(utp_context *ctx) { + assert(ctx); + return ctx ? ctx->userdata : NULL; +} + +utp_context_stats* utp_get_context_stats(utp_context *ctx) { + assert(ctx); + return ctx ? &ctx->context_stats : NULL; +} + +ssize_t utp_write(utp_socket *socket, void *buf, size_t len) { + struct utp_iovec iovec = { buf, len }; + return utp_writev(socket, &iovec, 1); +} + +} diff --git a/utp-client-interop/libutp (copy)/utp_callbacks.cpp b/utp-client-interop/libutp (copy)/utp_callbacks.cpp new file mode 100644 index 0000000..9540d8c --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_callbacks.cpp @@ -0,0 +1,208 @@ +// vim:set ts=4 sw=4 ai: + +/* + * 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. + */ + +#include "utp_callbacks.h" + +int utp_call_on_firewall(utp_context *ctx, const struct sockaddr *address, socklen_t address_len) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_FIREWALL]) return 0; + args.callback_type = UTP_ON_FIREWALL; + args.context = ctx; + args.socket = NULL; + args.address = address; + args.address_len = address_len; + return (int)ctx->callbacks[UTP_ON_FIREWALL](&args); +} + +void utp_call_on_accept(utp_context *ctx, utp_socket *socket, const struct sockaddr *address, socklen_t address_len) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_ACCEPT]) return; + args.callback_type = UTP_ON_ACCEPT; + args.context = ctx; + args.socket = socket; + args.address = address; + args.address_len = address_len; + ctx->callbacks[UTP_ON_ACCEPT](&args); +} + +void utp_call_on_connect(utp_context *ctx, utp_socket *socket) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_CONNECT]) return; + args.callback_type = UTP_ON_CONNECT; + args.context = ctx; + args.socket = socket; + ctx->callbacks[UTP_ON_CONNECT](&args); +} + +void utp_call_on_error(utp_context *ctx, utp_socket *socket, int error_code) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_ERROR]) return; + args.callback_type = UTP_ON_ERROR; + args.context = ctx; + args.socket = socket; + args.error_code = error_code; + ctx->callbacks[UTP_ON_ERROR](&args); +} + +void utp_call_on_read(utp_context *ctx, utp_socket *socket, const byte *buf, size_t len) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_READ]) return; + args.callback_type = UTP_ON_READ; + args.context = ctx; + args.socket = socket; + args.buf = buf; + args.len = len; + ctx->callbacks[UTP_ON_READ](&args); +} + +void utp_call_on_overhead_statistics(utp_context *ctx, utp_socket *socket, int send, size_t len, int type) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_OVERHEAD_STATISTICS]) return; + args.callback_type = UTP_ON_OVERHEAD_STATISTICS; + args.context = ctx; + args.socket = socket; + args.send = send; + args.len = len; + args.type = type; + ctx->callbacks[UTP_ON_OVERHEAD_STATISTICS](&args); +} + +void utp_call_on_delay_sample(utp_context *ctx, utp_socket *socket, int sample_ms) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_DELAY_SAMPLE]) return; + args.callback_type = UTP_ON_DELAY_SAMPLE; + args.context = ctx; + args.socket = socket; + args.sample_ms = sample_ms; + ctx->callbacks[UTP_ON_DELAY_SAMPLE](&args); +} + +void utp_call_on_state_change(utp_context *ctx, utp_socket *socket, int state) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_ON_STATE_CHANGE]) return; + args.callback_type = UTP_ON_STATE_CHANGE; + args.context = ctx; + args.socket = socket; + args.state = state; + ctx->callbacks[UTP_ON_STATE_CHANGE](&args); +} + +uint16 utp_call_get_udp_mtu(utp_context *ctx, utp_socket *socket, const struct sockaddr *address, socklen_t address_len) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_GET_UDP_MTU]) return 0; + args.callback_type = UTP_GET_UDP_MTU; + args.context = ctx; + args.socket = socket; + args.address = address; + args.address_len = address_len; + return (uint16)ctx->callbacks[UTP_GET_UDP_MTU](&args); +} + +uint16 utp_call_get_udp_overhead(utp_context *ctx, utp_socket *socket, const struct sockaddr *address, socklen_t address_len) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_GET_UDP_OVERHEAD]) return 0; + args.callback_type = UTP_GET_UDP_OVERHEAD; + args.context = ctx; + args.socket = socket; + args.address = address; + args.address_len = address_len; + return (uint16)ctx->callbacks[UTP_GET_UDP_OVERHEAD](&args); +} + +uint64 utp_call_get_milliseconds(utp_context *ctx, utp_socket *socket) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_GET_MILLISECONDS]) return 0; + args.callback_type = UTP_GET_MILLISECONDS; + args.context = ctx; + args.socket = socket; + return ctx->callbacks[UTP_GET_MILLISECONDS](&args); +} + +uint64 utp_call_get_microseconds(utp_context *ctx, utp_socket *socket) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_GET_MICROSECONDS]) return 0; + args.callback_type = UTP_GET_MICROSECONDS; + args.context = ctx; + args.socket = socket; + return ctx->callbacks[UTP_GET_MICROSECONDS](&args); +} + +uint32 utp_call_get_random(utp_context *ctx, utp_socket *socket) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_GET_RANDOM]) return 0; + args.callback_type = UTP_GET_RANDOM; + args.context = ctx; + args.socket = socket; + return (uint32)ctx->callbacks[UTP_GET_RANDOM](&args); +} + +size_t utp_call_get_read_buffer_size(utp_context *ctx, utp_socket *socket) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_GET_READ_BUFFER_SIZE]) return 0; + args.callback_type = UTP_GET_READ_BUFFER_SIZE; + args.context = ctx; + args.socket = socket; + return (size_t)ctx->callbacks[UTP_GET_READ_BUFFER_SIZE](&args); +} + +void utp_call_log(utp_context *ctx, utp_socket *socket, const byte *buf) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_LOG]) return; + args.callback_type = UTP_LOG; + args.context = ctx; + args.socket = socket; + args.buf = buf; + ctx->callbacks[UTP_LOG](&args); +} + +void utp_call_sendto(utp_context *ctx, utp_socket *socket, const byte *buf, size_t len, const struct sockaddr *address, socklen_t address_len, uint32 flags) +{ + utp_callback_arguments args; + if (!ctx->callbacks[UTP_SENDTO]) return; + args.callback_type = UTP_SENDTO; + args.context = ctx; + args.socket = socket; + args.buf = buf; + args.len = len; + args.address = address; + args.address_len = address_len; + args.flags = flags; + ctx->callbacks[UTP_SENDTO](&args); +} + diff --git a/utp-client-interop/libutp (copy)/utp_callbacks.h b/utp-client-interop/libutp (copy)/utp_callbacks.h new file mode 100644 index 0000000..649e7e1 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_callbacks.h @@ -0,0 +1,47 @@ +/* + * 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_CALLBACKS_H__ +#define __UTP_CALLBACKS_H__ + +#include "utp.h" +#include "utp_internal.h" + +// Generated by running: grep ^[a-z] utp_callbacks.cpp | sed 's/$/;/' +int utp_call_on_firewall(utp_context *ctx, const struct sockaddr *address, socklen_t address_len); +void utp_call_on_accept(utp_context *ctx, utp_socket *s, const struct sockaddr *address, socklen_t address_len); +void utp_call_on_connect(utp_context *ctx, utp_socket *s); +void utp_call_on_error(utp_context *ctx, utp_socket *s, int error_code); +void utp_call_on_read(utp_context *ctx, utp_socket *s, const byte *buf, size_t len); +void utp_call_on_overhead_statistics(utp_context *ctx, utp_socket *s, int send, size_t len, int type); +void utp_call_on_delay_sample(utp_context *ctx, utp_socket *s, int sample_ms); +void utp_call_on_state_change(utp_context *ctx, utp_socket *s, int state); +uint16 utp_call_get_udp_mtu(utp_context *ctx, utp_socket *s, const struct sockaddr *address, socklen_t address_len); +uint16 utp_call_get_udp_overhead(utp_context *ctx, utp_socket *s, const struct sockaddr *address, socklen_t address_len); +uint64 utp_call_get_milliseconds(utp_context *ctx, utp_socket *s); +uint64 utp_call_get_microseconds(utp_context *ctx, utp_socket *s); +uint32 utp_call_get_random(utp_context *ctx, utp_socket *s); +size_t utp_call_get_read_buffer_size(utp_context *ctx, utp_socket *s); +void utp_call_log(utp_context *ctx, utp_socket *s, const byte *buf); +void utp_call_sendto(utp_context *ctx, utp_socket *s, const byte *buf, size_t len, const struct sockaddr *address, socklen_t address_len, uint32 flags); + +#endif // __UTP_CALLBACKS_H__ diff --git a/utp-client-interop/libutp (copy)/utp_hash.cpp b/utp-client-interop/libutp (copy)/utp_hash.cpp new file mode 100644 index 0000000..a4a71d9 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_hash.cpp @@ -0,0 +1,246 @@ +/* + * 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. + */ + +#include "utp_hash.h" +#include "utp_types.h" + +#define LIBUTP_HASH_UNUSED ((utp_link_t)-1) + +#ifdef STRICT_ALIGN +inline uint32 Read32(const void *p) +{ + uint32 tmp; + memcpy(&tmp, p, sizeof tmp); + return tmp; +} + +#else +inline uint32 Read32(const void *p) { return *(uint32*)p; } +#endif + + +// Get the amount of memory required for the hash parameters and the bucket set +// Waste a space for an unused bucket in order to ensure the following managed memory have 32-bit aligned addresses +// TODO: make this 64-bit clean +#define BASE_SIZE(bc) (sizeof(utp_hash_t) + sizeof(utp_link_t) * ((bc) + 1)) + +// Get a pointer to the base of the structure array managed by the hash table +#define get_bep(h) ((byte*)(h)) + BASE_SIZE((h)->N) + +// Get the address of the information associated with a specific structure in the array, +// given the address of the base of the structure. +// This assumes a utp_link_t link member is at the end of the structure. +// Given compilers filling out the memory to a 32-bit clean value, this may mean that +// the location named in the structure may not be the location actually used by the hash table, +// since the compiler may have padded the end of the structure with 2 bytes after the utp_link_t member. +// TODO: this macro should not require that the variable pointing at the hash table be named 'hash' +#define ptr_to_link(p) (utp_link_t *) (((byte *) (p)) + hash->E - sizeof(utp_link_t)) + +// Calculate how much to allocate for a hash table with bucket count, total size, and structure count +// TODO: make this 64-bit clean +#define ALLOCATION_SIZE(bc, ts, sc) (BASE_SIZE((bc)) + (ts) * (sc)) + +utp_hash_t *utp_hash_create(int N, int key_size, int total_size, int initial, utp_hash_compute_t hashfun, utp_hash_equal_t compfun) +{ + // Must have odd number of hash buckets (prime number is best) + assert(N % 2); + // Ensure structures will be at aligned memory addresses + // TODO: make this 64-bit clean + assert(0 == (total_size % 4)); + + int size = ALLOCATION_SIZE(N, total_size, initial); + utp_hash_t *hash = (utp_hash_t *) malloc( size ); + memset( hash, 0, size ); + + for (int i = 0; i < N + 1; ++i) + hash->inits[i] = LIBUTP_HASH_UNUSED; + hash->N = N; + hash->K = key_size; + hash->E = total_size; + hash->hash_compute = hashfun; + hash->hash_equal = compfun; + hash->allocated = initial; + hash->count = 0; + hash->used = 0; + hash->free = LIBUTP_HASH_UNUSED; + return hash; +} + +uint utp_hash_mem(const void *keyp, size_t keysize) +{ + uint hash = 0; + uint n = keysize; + while (n >= 4) { + hash ^= Read32(keyp); + keyp = (byte*)keyp + sizeof(uint32); + hash = (hash << 13) | (hash >> 19); + n -= 4; + } + while (n != 0) { + hash ^= *(byte*)keyp; + keyp = (byte*)keyp + sizeof(byte); + hash = (hash << 8) | (hash >> 24); + n--; + } + return hash; +} + +uint utp_hash_mkidx(utp_hash_t *hash, const void *keyp) +{ + // Generate a key from the hash + return hash->hash_compute(keyp, hash->K) % hash->N; +} + +static inline bool compare(byte *a, byte *b,int n) +{ + assert(n >= 4); + if (Read32(a) != Read32(b)) return false; + return memcmp(a+4, b+4, n-4) == 0; +} + +#define COMPARE(h,k1,k2,ks) (((h)->hash_equal) ? (h)->hash_equal((void*)k1,(void*)k2,ks) : compare(k1,k2,ks)) + +// Look-up a key in the hash table. +// Returns NULL if not found +void *utp_hash_lookup(utp_hash_t *hash, const void *key) +{ + utp_link_t idx = utp_hash_mkidx(hash, key); + + // base pointer + byte *bep = get_bep(hash); + + utp_link_t cur = hash->inits[idx]; + while (cur != LIBUTP_HASH_UNUSED) { + byte *key2 = bep + (cur * hash->E); + if (COMPARE(hash, (byte*)key, key2, hash->K)) + return key2; + cur = *ptr_to_link(key2); + } + + return NULL; +} + +// Add a new element to the hash table. +// Returns a pointer to the new element. +// This assumes the element is not already present! +void *utp_hash_add(utp_hash_t **hashp, const void *key) +{ + //Allocate a new entry + byte *elemp; + utp_link_t elem; + utp_hash_t *hash = *hashp; + utp_link_t idx = utp_hash_mkidx(hash, key); + + if ((elem=hash->free) == LIBUTP_HASH_UNUSED) { + utp_link_t all = hash->allocated; + if (hash->used == all) { + utp_hash_t *nhash; + if (all <= (LIBUTP_HASH_UNUSED/2)) { + all *= 2; + } else if (all != LIBUTP_HASH_UNUSED) { + all = LIBUTP_HASH_UNUSED; + } else { + // too many items! can't grow! + assert(0); + return NULL; + } + // otherwise need to allocate. + nhash = (utp_hash_t*)realloc(hash, ALLOCATION_SIZE(hash->N, hash->E, all)); + if (!nhash) { + // out of memory (or too big to allocate) + assert(nhash); + return NULL; + } + hash = *hashp = nhash; + hash->allocated = all; + } + + elem = hash->used++; + elemp = get_bep(hash) + elem * hash->E; + } else { + elemp = get_bep(hash) + elem * hash->E; + hash->free = *ptr_to_link(elemp); + } + + *ptr_to_link(elemp) = hash->inits[idx]; + hash->inits[idx] = elem; + hash->count++; + + // copy key into it + memcpy(elemp, key, hash->K); + return elemp; +} + +// Delete an element from the utp_hash_t +// Returns a pointer to the already deleted element. +void *utp_hash_del(utp_hash_t *hash, const void *key) +{ + utp_link_t idx = utp_hash_mkidx(hash, key); + + // base pointer + byte *bep = get_bep(hash); + + utp_link_t *curp = &hash->inits[idx]; + utp_link_t cur; + while ((cur=*curp) != LIBUTP_HASH_UNUSED) { + byte *key2 = bep + (cur * hash->E); + if (COMPARE(hash,(byte*)key,(byte*)key2, hash->K )) { + // found an item that matched. unlink it + *curp = *ptr_to_link(key2); + // Insert into freelist + *ptr_to_link(key2) = hash->free; + hash->free = cur; + hash->count--; + return key2; + } + curp = ptr_to_link(key2); + } + + return NULL; +} + +void *utp_hash_iterate(utp_hash_t *hash, utp_hash_iterator_t *iter) +{ + utp_link_t elem; + + if ((elem=iter->elem) == LIBUTP_HASH_UNUSED) { + // Find a bucket with an element + utp_link_t buck = iter->bucket + 1; + for(;;) { + if (buck >= hash->N) + return NULL; + if ((elem = hash->inits[buck]) != LIBUTP_HASH_UNUSED) + break; + buck++; + } + iter->bucket = buck; + } + + byte *elemp = get_bep(hash) + (elem * hash->E); + iter->elem = *ptr_to_link(elemp); + return elemp; +} + +void utp_hash_free_mem(utp_hash_t* hash) +{ + free(hash); +} diff --git a/utp-client-interop/libutp (copy)/utp_hash.h b/utp-client-interop/libutp (copy)/utp_hash.h new file mode 100644 index 0000000..72c17e3 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_hash.h @@ -0,0 +1,146 @@ +/* + * 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_HASH_H__ +#define __UTP_HASH_H__ + +#include // memset +#include // malloc + +#include "utp_types.h" +#include "utp_templates.h" + +// TODO: make utp_link_t a template parameter to HashTable +typedef uint32 utp_link_t; + +#ifdef _MSC_VER +// Silence the warning about the C99-compliant zero-length array at the end of the structure +#pragma warning (disable: 4200) +#endif + +typedef uint32 (*utp_hash_compute_t)(const void *keyp, size_t keysize); +typedef uint (*utp_hash_equal_t)(const void *key_a, const void *key_b, size_t keysize); + +// In memory the HashTable is laid out as follows: +// ---------------------------- low +// | hash table data members | +// ---------------------------- _ +// | indices | ^ +// | . | | utp_link_t indices into the key-values. +// | . | . +// ---------------------------- - <----- bep +// | keys and values | each key-value pair has size total_size +// | . | +// | . | +// ---------------------------- high +// +// The code depends on the ability of the compiler to pad the length +// of the hash table data members structure to +// a length divisible by 32-bits with no remainder. +// +// Since the number of hash buckets (indices) should be odd, the code +// asserts this and adds one to the hash bucket count to ensure that the +// following key-value pairs array starts on a 32-bit boundary. +// +// The key-value pairs array should start on a 32-bit boundary, otherwise +// processors like the ARM will silently mangle 32-bit data in these structures +// (e.g., turning 0xABCD into 0XCDAB when moving a value from memory to register +// when the memory address is 16 bits offset from a 32-bit boundary), +// also, the value will be stored at an address two bytes lower than the address +// value would ordinarily indicate. +// +// The key-value pair is of type T. The first field in T must +// be the key, i.e., the first K bytes of T contains the key. +// total_size = sizeof(T) and thus sizeof(T) >= sizeof(K) +// +// N is the number of buckets. +// +struct utp_hash_t { + utp_link_t N; + byte K; + byte E; + size_t count; + utp_hash_compute_t hash_compute; + utp_hash_equal_t hash_equal; + utp_link_t allocated; + utp_link_t used; + utp_link_t free; + utp_link_t inits[0]; +}; + +#ifdef _MSC_VER +#pragma warning (default: 4200) +#endif + +struct utp_hash_iterator_t { + utp_link_t bucket; + utp_link_t elem; + + utp_hash_iterator_t() : bucket(0xffffffff), elem(0xffffffff) {} +}; + +uint utp_hash_mem(const void *keyp, size_t keysize); +uint utp_hash_comp(const void *key_a, const void *key_b, size_t keysize); + +utp_hash_t *utp_hash_create(int N, int key_size, int total_size, int initial, utp_hash_compute_t hashfun = utp_hash_mem, utp_hash_equal_t eqfun = NULL); +void *utp_hash_lookup(utp_hash_t *hash, const void *key); +void *utp_hash_add(utp_hash_t **hashp, const void *key); +void *utp_hash_del(utp_hash_t *hash, const void *key); + +void *utp_hash_iterate(utp_hash_t *hash, utp_hash_iterator_t *iter); +void utp_hash_free_mem(utp_hash_t *hash); + +/* + This HashTable requires that T have at least sizeof(K)+sizeof(utp_link_t) bytes. + Usually done like this: + + struct K { + int whatever; + }; + + struct T { + K wtf; + utp_link_t link; // also wtf + }; +*/ + +template class utpHashTable { + utp_hash_t *hash; +public: + static uint compare(const void *k1, const void *k2, size_t ks) { + return *((K*)k1) == *((K*)k2); + } + static uint32 compute_hash(const void *k, size_t ks) { + return ((K*)k)->compute_hash(); + } + void Init() { hash = NULL; } + bool Allocated() { return (hash != NULL); } + void Free() { utp_hash_free_mem(hash); hash = NULL; } + void Create(int N, int initial) { hash = utp_hash_create(N, sizeof(K), sizeof(T), initial, &compute_hash, &compare); } + T *Lookup(const K &key) { return (T*)utp_hash_lookup(hash, &key); } + T *Add(const K &key) { return (T*)utp_hash_add(&hash, &key); } + T *Delete(const K &key) { return (T*)utp_hash_del(hash, &key); } + T *Iterate(utp_hash_iterator_t &iterator) { return (T*)utp_hash_iterate(hash, &iterator); } + size_t GetCount() { return hash->count; } +}; + +#endif //__UTP_HASH_H__ diff --git a/utp-client-interop/libutp (copy)/utp_internal.cpp b/utp-client-interop/libutp (copy)/utp_internal.cpp new file mode 100644 index 0000000..7687004 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_internal.cpp @@ -0,0 +1,3490 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include // for UINT_MAX +#include + +#include "utp_types.h" +#include "utp_packedsockaddr.h" +#include "utp_internal.h" +#include "utp_hash.h" + +#define TIMEOUT_CHECK_INTERVAL 500 +#define UTP_DEBUG_LOGGING 0 + +// number of bytes to increase max window size by, per RTT. This is +// scaled down linearly proportional to off_target. i.e. if all packets +// in one window have 0 delay, window size will increase by this number. +// Typically it's less. TCP increases one MSS per RTT, which is 1500 +#define MAX_CWND_INCREASE_BYTES_PER_RTT 3000 +#define CUR_DELAY_SIZE 3 +// experiments suggest that a clock skew of 10 ms per 325 seconds +// is not impossible. Reset delay_base every 13 minutes. The clock +// skew is dealt with by observing the delay base in the other +// direction, and adjusting our own upwards if the opposite direction +// delay base keeps going down +#define DELAY_BASE_HISTORY 13 +#define MAX_WINDOW_DECAY 100 // ms + +#define REORDER_BUFFER_SIZE 32 +#define REORDER_BUFFER_MAX_SIZE 1024 +#define OUTGOING_BUFFER_MAX_SIZE 1024 + +#define PACKET_SIZE 1435 + +// this is the minimum max_window value. It can never drop below this +#define MIN_WINDOW_SIZE 10 + +// if we receive 4 or more duplicate acks, we resend the packet +// that hasn't been acked yet +#define DUPLICATE_ACKS_BEFORE_RESEND 3 + +// Allow a reception window of at least 3 ack_nrs behind seq_nr +// A non-SYN packet with an ack_nr difference greater than this is +// considered suspicious and ignored +#define ACK_NR_ALLOWED_WINDOW DUPLICATE_ACKS_BEFORE_RESEND + +#define RST_INFO_TIMEOUT 10000 +#define RST_INFO_LIMIT 1000 +// 29 seconds determined from measuring many home NAT devices +#define KEEPALIVE_INTERVAL 29000 + + +#define SEQ_NR_MASK 0xFFFF +#define ACK_NR_MASK 0xFFFF +#define TIMESTAMP_MASK 0xFFFFFFFF + +#define DIV_ROUND_UP(num, denom) ((num + denom - 1) / denom) + +// The totals are derived from the following data: +// 45: IPv6 address including embedded IPv4 address +// 11: Scope Id +// 2: Brackets around IPv6 address when port is present +// 6: Port (including colon) +// 1: Terminating null byte +char addrbuf[65]; +#define addrfmt(x, s) x.fmt(s, sizeof(s)) + + +#if (defined(__SVR4) && defined(__sun)) + #pragma pack(1) +#else + #pragma pack(push,1) +#endif + + +// these packet sizes are including the uTP header wich +// is either 20 or 23 bytes depending on version +#define PACKET_SIZE_EMPTY_BUCKET 0 +#define PACKET_SIZE_EMPTY 23 +#define PACKET_SIZE_SMALL_BUCKET 1 +#define PACKET_SIZE_SMALL 373 +#define PACKET_SIZE_MID_BUCKET 2 +#define PACKET_SIZE_MID 723 +#define PACKET_SIZE_BIG_BUCKET 3 +#define PACKET_SIZE_BIG 1400 +#define PACKET_SIZE_HUGE_BUCKET 4 + +struct PACKED_ATTRIBUTE PacketFormatV1 { + // packet_type (4 high bits) + // protocol version (4 low bits) + byte ver_type; + byte version() const { return ver_type & 0xf; } + byte type() const { return ver_type >> 4; } + void set_version(byte v) { ver_type = (ver_type & 0xf0) | (v & 0xf); } + void set_type(byte t) { ver_type = (ver_type & 0xf) | (t << 4); } + + // Type of the first extension header + byte ext; + // connection ID + uint16_big connid; + uint32_big tv_usec; + uint32_big reply_micro; + // receive window size in bytes + uint32_big windowsize; + // Sequence number + uint16_big seq_nr; + // Acknowledgment number + uint16_big ack_nr; +}; + +struct PACKED_ATTRIBUTE PacketFormatAckV1 { + PacketFormatV1 pf; + byte ext_next; + byte ext_len; + byte acks[4]; +}; + +#if (defined(__SVR4) && defined(__sun)) + #pragma pack(0) +#else + #pragma pack(pop) +#endif + +enum { + ST_DATA = 0, // Data packet. + ST_FIN = 1, // Finalize the connection. This is the last packet. + ST_STATE = 2, // State packet. Used to transmit an ACK with no data. + ST_RESET = 3, // Terminate connection forcefully. + ST_SYN = 4, // Connect SYN + ST_NUM_STATES, // used for bounds checking +}; + +static const cstr flagnames[] = { + "ST_DATA","ST_FIN","ST_STATE","ST_RESET","ST_SYN" +}; + +enum CONN_STATE { + CS_UNINITIALIZED = 0, + CS_IDLE, + CS_SYN_SENT, + CS_SYN_RECV, + CS_CONNECTED, + CS_CONNECTED_FULL, + CS_RESET, + CS_DESTROY +}; + +static const cstr statenames[] = { + "UNINITIALIZED", "IDLE","SYN_SENT", "SYN_RECV", "CONNECTED","CONNECTED_FULL","DESTROY_DELAY","RESET","DESTROY" +}; + +struct OutgoingPacket { + size_t length; + size_t payload; + uint64 time_sent; // microseconds + uint transmissions:31; + bool need_resend:1; + byte data[1]; +}; + +struct SizableCircularBuffer { + // This is the mask. Since it's always a power of 2, adding 1 to this value will return the size. + size_t mask; + // This is the elements that the circular buffer points to + void **elements; + + void *get(size_t i) const { assert(elements); return elements ? elements[i & mask] : NULL; } + void put(size_t i, void *data) { assert(elements); elements[i&mask] = data; } + + void grow(size_t item, size_t index); + void ensure_size(size_t item, size_t index) { if (index > mask) grow(item, index); } + size_t size() { return mask + 1; } +}; + +// Item contains the element we want to make space for +// index is the index in the list. +void SizableCircularBuffer::grow(size_t item, size_t index) +{ + // Figure out the new size. + size_t size = mask + 1; + do size *= 2; while (index >= size); + + // Allocate the new buffer + void **buf = (void**)calloc(size, sizeof(void*)); + + size--; + + // Copy elements from the old buffer to the new buffer + for (size_t i = 0; i <= mask; i++) { + buf[(item - index + i) & size] = get(item - index + i); + } + + // Swap to the newly allocated buffer + mask = size; + free(elements); + elements = buf; +} + +// compare if lhs is less than rhs, taking wrapping +// into account. if lhs is close to UINT_MAX and rhs +// is close to 0, lhs is assumed to have wrapped and +// considered smaller +bool wrapping_compare_less(uint32 lhs, uint32 rhs, uint32 mask) +{ + // distance walking from lhs to rhs, downwards + const uint32 dist_down = (lhs - rhs) & mask; + // distance walking from lhs to rhs, upwards + const uint32 dist_up = (rhs - lhs) & mask; + + // if the distance walking up is shorter, lhs + // is less than rhs. If the distance walking down + // is shorter, then rhs is less than lhs + return dist_up < dist_down; +} + +struct DelayHist { + uint32 delay_base; + + // this is the history of delay samples, + // normalized by using the delay_base. These + // values are always greater than 0 and measures + // the queuing delay in microseconds + uint32 cur_delay_hist[CUR_DELAY_SIZE]; + size_t cur_delay_idx; + + // this is the history of delay_base. It's + // a number that doesn't have an absolute meaning + // only relative. It doesn't make sense to initialize + // it to anything other than values relative to + // what's been seen in the real world. + uint32 delay_base_hist[DELAY_BASE_HISTORY]; + size_t delay_base_idx; + // the time when we last stepped the delay_base_idx + uint64 delay_base_time; + + bool delay_base_initialized; + + void clear(uint64 current_ms) + { + delay_base_initialized = false; + delay_base = 0; + cur_delay_idx = 0; + delay_base_idx = 0; + delay_base_time = current_ms; + for (size_t i = 0; i < CUR_DELAY_SIZE; i++) { + cur_delay_hist[i] = 0; + } + for (size_t i = 0; i < DELAY_BASE_HISTORY; i++) { + delay_base_hist[i] = 0; + } + } + + void shift(const uint32 offset) + { + // the offset should never be "negative" + // assert(offset < 0x10000000); + + // increase all of our base delays by this amount + // this is used to take clock skew into account + // by observing the other side's changes in its base_delay + for (size_t i = 0; i < DELAY_BASE_HISTORY; i++) { + delay_base_hist[i] += offset; + } + delay_base += offset; + } + + void add_sample(const uint32 sample, uint64 current_ms) + { + // The two clocks (in the two peers) are assumed not to + // progress at the exact same rate. They are assumed to be + // drifting, which causes the delay samples to contain + // a systematic error, either they are under- + // estimated or over-estimated. This is why we update the + // delay_base every two minutes, to adjust for this. + + // This means the values will keep drifting and eventually wrap. + // We can cross the wrapping boundry in two directions, either + // going up, crossing the highest value, or going down, crossing 0. + + // if the delay_base is close to the max value and sample actually + // wrapped on the other end we would see something like this: + // delay_base = 0xffffff00, sample = 0x00000400 + // sample - delay_base = 0x500 which is the correct difference + + // if the delay_base is instead close to 0, and we got an even lower + // sample (that will eventually update the delay_base), we may see + // something like this: + // delay_base = 0x00000400, sample = 0xffffff00 + // sample - delay_base = 0xfffffb00 + // this needs to be interpreted as a negative number and the actual + // recorded delay should be 0. + + // It is important that all arithmetic that assume wrapping + // is done with unsigned intergers. Signed integers are not guaranteed + // to wrap the way unsigned integers do. At least GCC takes advantage + // of this relaxed rule and won't necessarily wrap signed ints. + + // remove the clock offset and propagation delay. + // delay base is min of the sample and the current + // delay base. This min-operation is subject to wrapping + // and care needs to be taken to correctly choose the + // true minimum. + + // specifically the problem case is when delay_base is very small + // and sample is very large (because it wrapped past zero), sample + // needs to be considered the smaller + + if (!delay_base_initialized) { + // delay_base being 0 suggests that we haven't initialized + // it or its history with any real measurements yet. Initialize + // everything with this sample. + for (size_t i = 0; i < DELAY_BASE_HISTORY; i++) { + // if we don't have a value, set it to the current sample + delay_base_hist[i] = sample; + continue; + } + delay_base = sample; + delay_base_initialized = true; + } + + if (wrapping_compare_less(sample, delay_base_hist[delay_base_idx], TIMESTAMP_MASK)) { + // sample is smaller than the current delay_base_hist entry + // update it + delay_base_hist[delay_base_idx] = sample; + } + + // is sample lower than delay_base? If so, update delay_base + if (wrapping_compare_less(sample, delay_base, TIMESTAMP_MASK)) { + // sample is smaller than the current delay_base + // update it + delay_base = sample; + } + + // this operation may wrap, and is supposed to + const uint32 delay = sample - delay_base; + // sanity check. If this is triggered, something fishy is going on + // it means the measured sample was greater than 32 seconds! + //assert(delay < 0x2000000); + + cur_delay_hist[cur_delay_idx] = delay; + cur_delay_idx = (cur_delay_idx + 1) % CUR_DELAY_SIZE; + + // once every minute + if (current_ms - delay_base_time > 60 * 1000) { + delay_base_time = current_ms; + delay_base_idx = (delay_base_idx + 1) % DELAY_BASE_HISTORY; + // clear up the new delay base history spot by initializing + // it to the current sample, then update it + delay_base_hist[delay_base_idx] = sample; + delay_base = delay_base_hist[0]; + // Assign the lowest delay in the last 2 minutes to delay_base + for (size_t i = 0; i < DELAY_BASE_HISTORY; i++) { + if (wrapping_compare_less(delay_base_hist[i], delay_base, TIMESTAMP_MASK)) + delay_base = delay_base_hist[i]; + } + } + } + + uint32 get_value() + { + uint32 value = UINT_MAX; + for (size_t i = 0; i < CUR_DELAY_SIZE; i++) { + value = min(cur_delay_hist[i], value); + } + // value could be UINT_MAX if we have no samples yet... + return value; + } +}; + +struct UTPSocket { + ~UTPSocket(); + + PackedSockAddr addr; + utp_context *ctx; + + int ida; //for ack socket list + + uint16 retransmit_count; + + uint16 reorder_count; + byte duplicate_ack; + + // the number of packets in the send queue. Packets that haven't + // yet been sent count as well as packets marked as needing resend + // the oldest un-acked packet in the send queue is seq_nr - cur_window_packets + uint16 cur_window_packets; + + // how much of the window is used, number of bytes in-flight + // packets that have not yet been sent do not count, packets + // that are marked as needing to be re-sent (due to a timeout) + // don't count either + size_t cur_window; + // maximum window size, in bytes + size_t max_window; + // UTP_SNDBUF setting, in bytes + size_t opt_sndbuf; + // UTP_RCVBUF setting, in bytes + size_t opt_rcvbuf; + + // this is the target delay, in microseconds + // for this socket. defaults to 100000. + size_t target_delay; + + // Is a FIN packet in the reassembly buffer? + bool got_fin:1; + // Have we reached the FIN? + bool got_fin_reached:1; + + // Have we sent our FIN? + bool fin_sent:1; + // Has our fin been ACKed? + bool fin_sent_acked:1; + + // Reading is disabled + bool read_shutdown:1; + // User called utp_close() + bool close_requested:1; + + // Timeout procedure + bool fast_timeout:1; + + // max receive window for other end, in bytes + size_t max_window_user; + CONN_STATE state; + // TickCount when we last decayed window (wraps) + int64 last_rwin_decay; + + // the sequence number of the FIN packet. This field is only set + // when we have received a FIN, and the flag field has the FIN flag set. + // it is used to know when it is safe to destroy the socket, we must have + // received all packets up to this sequence number first. + uint16 eof_pkt; + + // All sequence numbers up to including this have been properly received + // by us + uint16 ack_nr; + // This is the sequence number for the next packet to be sent. + uint16 seq_nr; + + uint16 timeout_seq_nr; + + // This is the sequence number of the next packet we're allowed to + // do a fast resend with. This makes sure we only do a fast-resend + // once per packet. We can resend the packet with this sequence number + // or any later packet (with a higher sequence number). + uint16 fast_resend_seq_nr; + + uint32 reply_micro; + + uint64 last_got_packet; + uint64 last_sent_packet; + uint64 last_measured_delay; + + // timestamp of the last time the cwnd was full + // this is used to prevent the congestion window + // from growing when we're not sending at capacity + mutable uint64 last_maxed_out_window; + + void *userdata; + + // Round trip time + uint rtt; + // Round trip time variance + uint rtt_var; + // Round trip timeout + uint rto; + DelayHist rtt_hist; + uint retransmit_timeout; + // The RTO timer will timeout here. + uint64 rto_timeout; + // When the window size is set to zero, start this timer. It will send a new packet every 30secs. + uint64 zerowindow_time; + + uint32 conn_seed; + // Connection ID for packets I receive + uint32 conn_id_recv; + // Connection ID for packets I send + uint32 conn_id_send; + // Last rcv window we advertised, in bytes + size_t last_rcv_win; + + DelayHist our_hist; + DelayHist their_hist; + + // extension bytes from SYN packet + byte extensions[8]; + + // MTU Discovery + // time when we should restart the MTU discovery + uint64 mtu_discover_time; + // ceiling and floor of binary search. last is the mtu size + // we're currently using + uint32 mtu_ceiling, mtu_floor, mtu_last; + // we only ever have a single probe in flight at any given time. + // this is the sequence number of that probe, and the size of + // that packet + uint32 mtu_probe_seq, mtu_probe_size; + + // this is the average delay samples, as compared to the initial + // sample. It's averaged over 5 seconds + int32 average_delay; + // this is the sum of all the delay samples + // we've made recently. The important distinction + // of these samples is that they are all made compared + // to the initial sample, this is to deal with + // wrapping in a simple way. + int64 current_delay_sum; + // number of sample ins current_delay_sum + int current_delay_samples; + // initialized to 0, set to the first raw delay sample + // each sample that's added to current_delay_sum + // is subtracted from the value first, to make it + // a delay relative to this sample + uint32 average_delay_base; + // the next time we should add an average delay + // sample into average_delay_hist + uint64 average_sample_time; + // the estimated clock drift between our computer + // and the endpoint computer. The unit is microseconds + // per 5 seconds + int32 clock_drift; + // just used for logging + int32 clock_drift_raw; + + SizableCircularBuffer inbuf, outbuf; + + #ifdef _DEBUG + // Public per-socket statistics, returned by utp_get_stats() + utp_socket_stats _stats; + #endif + + // true if we're in slow-start (exponential growth) phase + bool slow_start; + + // the slow-start threshold, in bytes + size_t ssthresh; + + void log(int level, char const *fmt, ...) + { + va_list va; + char buf[4096], buf2[4096]; + + // don't bother with vsnprintf() etc calls if we're not going to log. + if (!ctx->would_log(level)) { + return; + } + + va_start(va, fmt); + vsnprintf(buf, 4096, fmt, va); + va_end(va); + buf[4095] = '\0'; + + snprintf(buf2, 4096, "%p %s %06u %s", this, addrfmt(addr, addrbuf), conn_id_recv, buf); + buf2[4095] = '\0'; + + ctx->log_unchecked(this, buf2); + } + + void schedule_ack(); + + // called every time mtu_floor or mtu_ceiling are adjusted + void mtu_search_update(); + void mtu_reset(); + + // Calculates the current receive window + size_t get_rcv_window() + { + // Trim window down according to what's already in buffer. + const size_t numbuf = utp_call_get_read_buffer_size(this->ctx, this); + assert((int)numbuf >= 0); + return opt_rcvbuf > numbuf ? opt_rcvbuf - numbuf : 0; + } + + // Test if we're ready to decay max_window + // XXX this breaks when spaced by > INT_MAX/2, which is 49 + // days; the failure mode in that case is we do an extra decay + // or fail to do one when we really shouldn't. + bool can_decay_win(int64 msec) const + { + return (msec - last_rwin_decay) >= MAX_WINDOW_DECAY; + } + + // If we can, decay max window, returns true if we actually did so + void maybe_decay_win(uint64 current_ms) + { + if (can_decay_win(current_ms)) { + // TCP uses 0.5 + max_window = (size_t)(max_window * .5); + last_rwin_decay = current_ms; + if (max_window < MIN_WINDOW_SIZE) + max_window = MIN_WINDOW_SIZE; + slow_start = false; + ssthresh = max_window; + } + } + + size_t get_header_size() const + { + return sizeof(PacketFormatV1); + } + + size_t get_udp_mtu() + { + socklen_t len; + SOCKADDR_STORAGE sa = addr.get_sockaddr_storage(&len); + return utp_call_get_udp_mtu(this->ctx, this, (const struct sockaddr *)&sa, len); + } + + size_t get_udp_overhead() + { + socklen_t len; + SOCKADDR_STORAGE sa = addr.get_sockaddr_storage(&len); + return utp_call_get_udp_overhead(this->ctx, this, (const struct sockaddr *)&sa, len); + } + + size_t get_overhead() + { + return get_udp_overhead() + get_header_size(); + } + + void send_data(byte* b, size_t length, bandwidth_type_t type, uint32 flags = 0); + + void send_ack(bool synack = false); + + void send_keep_alive(); + + static void send_rst(utp_context *ctx, + const PackedSockAddr &addr, uint32 conn_id_send, + uint16 ack_nr, uint16 seq_nr); + + void send_packet(OutgoingPacket *pkt); + + bool is_full(int bytes = -1); + bool flush_packets(); + void write_outgoing_packet(size_t payload, uint flags, struct utp_iovec *iovec, size_t num_iovecs); + + #ifdef _DEBUG + void check_invariant(); + #endif + + void check_timeouts(); + int ack_packet(uint16 seq); + size_t selective_ack_bytes(uint base, const byte* mask, byte len, int64& min_rtt); + void selective_ack(uint base, const byte *mask, byte len); + void apply_ccontrol(size_t bytes_acked, uint32 actual_delay, int64 min_rtt); + size_t get_packet_size() const; +}; + +void removeSocketFromAckList(UTPSocket *conn) +{ + if (conn->ida >= 0) + { + UTPSocket *last = conn->ctx->ack_sockets[conn->ctx->ack_sockets.GetCount() - 1]; + + assert(last->ida < (int)(conn->ctx->ack_sockets.GetCount())); + assert(conn->ctx->ack_sockets[last->ida] == last); + last->ida = conn->ida; + conn->ctx->ack_sockets[conn->ida] = last; + conn->ida = -1; + + // Decrease the count + conn->ctx->ack_sockets.SetCount(conn->ctx->ack_sockets.GetCount() - 1); + } +} + +static void utp_register_sent_packet(utp_context *ctx, size_t length) +{ + if (length <= PACKET_SIZE_MID) { + if (length <= PACKET_SIZE_EMPTY) { + ctx->context_stats._nraw_send[PACKET_SIZE_EMPTY_BUCKET]++; + } else if (length <= PACKET_SIZE_SMALL) { + ctx->context_stats._nraw_send[PACKET_SIZE_SMALL_BUCKET]++; + } else + ctx->context_stats._nraw_send[PACKET_SIZE_MID_BUCKET]++; + } else { + if (length <= PACKET_SIZE_BIG) { + ctx->context_stats._nraw_send[PACKET_SIZE_BIG_BUCKET]++; + } else + ctx->context_stats._nraw_send[PACKET_SIZE_HUGE_BUCKET]++; + } +} + +void send_to_addr(utp_context *ctx, const byte *p, size_t len, const PackedSockAddr &addr, int flags = 0) +{ + socklen_t tolen; + SOCKADDR_STORAGE to = addr.get_sockaddr_storage(&tolen); + utp_register_sent_packet(ctx, len); + utp_call_sendto(ctx, NULL, p, len, (const struct sockaddr *)&to, tolen, flags); +} + +void UTPSocket::schedule_ack() +{ + if (ida == -1){ + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "schedule_ack"); + #endif + ida = ctx->ack_sockets.Append(this); + } else { + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "schedule_ack: already in list"); + #endif + } +} + +void UTPSocket::send_data(byte* b, size_t length, bandwidth_type_t type, uint32 flags) +{ + // time stamp this packet with local time, the stamp goes into + // the header of every packet at the 8th byte for 8 bytes : + // two integers, check packet.h for more + uint64 time = utp_call_get_microseconds(ctx, this); + + PacketFormatV1* b1 = (PacketFormatV1*)b; + b1->tv_usec = (uint32)time; + b1->reply_micro = reply_micro; + + last_sent_packet = ctx->current_ms; + + #ifdef _DEBUG + _stats.nbytes_xmit += length; + ++_stats.nxmit; + #endif + + if (ctx->callbacks[UTP_ON_OVERHEAD_STATISTICS]) { + size_t n; + if (type == payload_bandwidth) { + // if this packet carries payload, just + // count the header as overhead + type = header_overhead; + n = get_overhead(); + } else { + n = length + get_udp_overhead(); + } + utp_call_on_overhead_statistics(ctx, this, true, n, type); + } +#if UTP_DEBUG_LOGGING + int flags2 = b1->type(); + uint16 seq_nr = b1->seq_nr; + uint16 ack_nr = b1->ack_nr; + log(UTP_LOG_DEBUG, "send %s len:%u id:%u timestamp:" I64u " reply_micro:%u flags:%s seq_nr:%u ack_nr:%u", + addrfmt(addr, addrbuf), (uint)length, conn_id_send, time, reply_micro, flagnames[flags2], + seq_nr, ack_nr); +#endif + send_to_addr(ctx, b, length, addr, flags); + removeSocketFromAckList(this); +} + +void UTPSocket::send_ack(bool synack) +{ + PacketFormatAckV1 pfa; + zeromem(&pfa); + + size_t len; + last_rcv_win = get_rcv_window(); + pfa.pf.set_version(1); + pfa.pf.set_type(ST_STATE); + pfa.pf.ext = 0; + pfa.pf.connid = conn_id_send; + pfa.pf.ack_nr = ack_nr; + pfa.pf.seq_nr = seq_nr; + pfa.pf.windowsize = (uint32)last_rcv_win; + len = sizeof(PacketFormatV1); + + // we never need to send EACK for connections + // that are shutting down + if (reorder_count != 0 && !got_fin_reached) { + // if reorder count > 0, send an EACK. + // reorder count should always be 0 + // for synacks, so this should not be + // as synack + assert(!synack); + pfa.pf.ext = 1; + pfa.ext_next = 0; + pfa.ext_len = 4; + uint m = 0; + + // reorder count should only be non-zero + // if the packet ack_nr + 1 has not yet + // been received + assert(inbuf.get(ack_nr + 1) == NULL); + size_t window = min(14+16, inbuf.size()); + // Generate bit mask of segments received. + for (size_t i = 0; i < window; i++) { + if (inbuf.get(ack_nr + i + 2) != NULL) { + m |= 1 << i; + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "EACK packet [%u]", ack_nr + i + 2); + #endif + } + } + pfa.acks[0] = (byte)m; + pfa.acks[1] = (byte)(m >> 8); + pfa.acks[2] = (byte)(m >> 16); + pfa.acks[3] = (byte)(m >> 24); + len += 4 + 2; + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "Sending EACK %u [%u] bits:[%032b]", ack_nr, conn_id_send, m); + #endif + } else { + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "Sending ACK %u [%u]", ack_nr, conn_id_send); + #endif + } + + send_data((byte*)&pfa, len, ack_overhead); + removeSocketFromAckList(this); +} + +void UTPSocket::send_keep_alive() +{ + ack_nr--; + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "Sending KeepAlive ACK %u [%u]", ack_nr, conn_id_send); + #endif + + send_ack(); + ack_nr++; +} + +void UTPSocket::send_rst(utp_context *ctx, + const PackedSockAddr &addr, uint32 conn_id_send, uint16 ack_nr, uint16 seq_nr) +{ + PacketFormatV1 pf1; + zeromem(&pf1); + + size_t len; + pf1.set_version(1); + pf1.set_type(ST_RESET); + pf1.ext = 0; + pf1.connid = conn_id_send; + pf1.ack_nr = ack_nr; + pf1.seq_nr = seq_nr; + pf1.windowsize = 0; + len = sizeof(PacketFormatV1); + +// LOG_DEBUG("%s: Sending RST id:%u seq_nr:%u ack_nr:%u", addrfmt(addr, addrbuf), conn_id_send, seq_nr, ack_nr); +// LOG_DEBUG("send %s len:%u id:%u", addrfmt(addr, addrbuf), (uint)len, conn_id_send); + send_to_addr(ctx, (const byte*)&pf1, len, addr); +} + +void UTPSocket::send_packet(OutgoingPacket *pkt) +{ + // only count against the quota the first time we + // send the packet. Don't enforce quota when closing + // a socket. Only enforce the quota when we're sending + // at slow rates (max window < packet size) + + //size_t max_send = min(max_window, opt_sndbuf, max_window_user); + time_t cur_time = utp_call_get_milliseconds(this->ctx, this); + + if (pkt->transmissions == 0 || pkt->need_resend) { + cur_window += pkt->payload; + } + + pkt->need_resend = false; + + PacketFormatV1* p1 = (PacketFormatV1*)pkt->data; + p1->ack_nr = ack_nr; + pkt->time_sent = utp_call_get_microseconds(this->ctx, this); + + //socklen_t salen; + //SOCKADDR_STORAGE sa = addr.get_sockaddr_storage(&salen); + bool use_as_mtu_probe = false; + + // TODO: this is subject to nasty wrapping issues! Below as well + if (mtu_discover_time < (uint64)cur_time) { + // it's time to reset our MTU assupmtions + // and trigger a new search + mtu_reset(); + } + + // don't use packets that are larger then mtu_ceiling + // as probes, since they were probably used as probes + // already and failed, now we need it to fragment + // just to get it through + // if seq_nr == 1, the probe would end up being 0 + // which is a magic number representing no-probe + // that why we don't send a probe for a packet with + // sequence number 0 + if (mtu_floor < mtu_ceiling + && pkt->length > mtu_floor + && pkt->length <= mtu_ceiling + && mtu_probe_seq == 0 + && seq_nr != 1 + && pkt->transmissions == 0) { + + // we've already incremented seq_nr + // for this packet + mtu_probe_seq = (seq_nr - 1) & ACK_NR_MASK; + mtu_probe_size = pkt->length; + assert(pkt->length >= mtu_floor); + assert(pkt->length <= mtu_ceiling); + use_as_mtu_probe = true; + log(UTP_LOG_MTU, "MTU [PROBE] floor:%d ceiling:%d current:%d" + , mtu_floor, mtu_ceiling, mtu_probe_size); + } + + pkt->transmissions++; + send_data((byte*)pkt->data, pkt->length, + (state == CS_SYN_SENT) ? connect_overhead + : (pkt->transmissions == 1) ? payload_bandwidth + : retransmit_overhead, use_as_mtu_probe ? UTP_UDP_DONTFRAG : 0); +} + +bool UTPSocket::is_full(int bytes) +{ + size_t packet_size = get_packet_size(); + if (bytes < 0) bytes = packet_size; + else if (bytes > (int)packet_size) bytes = (int)packet_size; + size_t max_send = min(max_window, opt_sndbuf, max_window_user); + + // subtract one to save space for the FIN packet + if (cur_window_packets >= OUTGOING_BUFFER_MAX_SIZE - 1) { + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "is_full:false cur_window_packets:%d MAX:%d", cur_window_packets, OUTGOING_BUFFER_MAX_SIZE - 1); + #endif + + last_maxed_out_window = ctx->current_ms; + return true; + } + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "is_full:%s. cur_window:%u pkt:%u max:%u cur_window_packets:%u max_window:%u" + , (cur_window + bytes > max_send) ? "true" : "false" + , cur_window, bytes, max_send, cur_window_packets + , max_window); + #endif + + if (cur_window + bytes > max_send) { + last_maxed_out_window = ctx->current_ms; + return true; + } + return false; +} + +bool UTPSocket::flush_packets() +{ + size_t packet_size = get_packet_size(); + + // send packets that are waiting on the pacer to be sent + // i has to be an unsigned 16 bit counter to wrap correctly + // signed types are not guaranteed to wrap the way you expect + for (uint16 i = seq_nr - cur_window_packets; i != seq_nr; ++i) { + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(i); + if (pkt == 0 || (pkt->transmissions > 0 && pkt->need_resend == false)) continue; + // have we run out of quota? + if (is_full()) return true; + + // Nagle check + // don't send the last packet if we have one packet in-flight + // and the current packet is still smaller than packet_size. + if (i != ((seq_nr - 1) & ACK_NR_MASK) || + cur_window_packets == 1 || + pkt->payload >= packet_size) { + send_packet(pkt); + } + } + return false; +} + +// @payload: number of bytes to send +// @flags: either ST_DATA, or ST_FIN +// @iovec: base address of iovec array +// @num_iovecs: number of iovecs in array +void UTPSocket::write_outgoing_packet(size_t payload, uint flags, struct utp_iovec *iovec, size_t num_iovecs) +{ + // Setup initial timeout timer + if (cur_window_packets == 0) { + retransmit_timeout = rto; + rto_timeout = ctx->current_ms + retransmit_timeout; + assert(cur_window == 0); + } + + size_t packet_size = get_packet_size(); + do { + assert(cur_window_packets < OUTGOING_BUFFER_MAX_SIZE); + assert(flags == ST_DATA || flags == ST_FIN); + + size_t added = 0; + + OutgoingPacket *pkt = NULL; + + if (cur_window_packets > 0) { + pkt = (OutgoingPacket*)outbuf.get(seq_nr - 1); + } + + const size_t header_size = get_header_size(); + bool append = true; + + // if there's any room left in the last packet in the window + // and it hasn't been sent yet, fill that frame first + if (payload && pkt && !pkt->transmissions && pkt->payload < packet_size) { + // Use the previous unsent packet + added = min(payload + pkt->payload, max(packet_size, pkt->payload)) - pkt->payload; + pkt = (OutgoingPacket*)realloc(pkt, + (sizeof(OutgoingPacket) - 1) + + header_size + + pkt->payload + added); + outbuf.put(seq_nr - 1, pkt); + append = false; + assert(!pkt->need_resend); + } else { + // Create the packet to send. + added = payload; + pkt = (OutgoingPacket*)malloc((sizeof(OutgoingPacket) - 1) + + header_size + + added); + pkt->payload = 0; + pkt->transmissions = 0; + pkt->need_resend = false; + } + + if (added) { + assert(flags == ST_DATA); + + // Fill it with data from the upper layer. + unsigned char *p = pkt->data + header_size + pkt->payload; + size_t needed = added; + + /* + while (needed) { + *p = *(char*)iovec[0].iov_base; + p++; + iovec[0].iov_base = (char *)iovec[0].iov_base + 1; + needed--; + } + */ + + for (size_t i = 0; i < num_iovecs && needed; i++) { + if (iovec[i].iov_len == 0) + continue; + + size_t num = min(needed, iovec[i].iov_len); + memcpy(p, iovec[i].iov_base, num); + + p += num; + + iovec[i].iov_len -= num; + iovec[i].iov_base = (byte*)iovec[i].iov_base + num; // iovec[i].iov_base += num, but without void* pointers + needed -= num; + } + + assert(needed == 0); + } + pkt->payload += added; + pkt->length = header_size + pkt->payload; + + last_rcv_win = get_rcv_window(); + + PacketFormatV1* p1 = (PacketFormatV1*)pkt->data; + p1->set_version(1); + p1->set_type(flags); + p1->ext = 0; + p1->connid = conn_id_send; + p1->windowsize = (uint32)last_rcv_win; + p1->ack_nr = ack_nr; + + if (append) { + // Remember the message in the outgoing queue. + outbuf.ensure_size(seq_nr, cur_window_packets); + outbuf.put(seq_nr, pkt); + p1->seq_nr = seq_nr; + seq_nr++; + cur_window_packets++; + } + + payload -= added; + + } while (payload); + + flush_packets(); +} + +#ifdef _DEBUG +void UTPSocket::check_invariant() +{ + if (reorder_count > 0) { + assert(inbuf.get(ack_nr + 1) == NULL); + } + + size_t outstanding_bytes = 0; + for (int i = 0; i < cur_window_packets; ++i) { + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(seq_nr - i - 1); + if (pkt == 0 || pkt->transmissions == 0 || pkt->need_resend) continue; + outstanding_bytes += pkt->payload; + } + assert(outstanding_bytes == cur_window); +} +#endif + +void UTPSocket::check_timeouts() +{ + #ifdef _DEBUG + check_invariant(); + #endif + + // this invariant should always be true + assert(cur_window_packets == 0 || outbuf.get(seq_nr - cur_window_packets)); + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "CheckTimeouts timeout:%d max_window:%u cur_window:%u " + "state:%s cur_window_packets:%u", + (int)(rto_timeout - ctx->current_ms), (uint)max_window, (uint)cur_window, + statenames[state], cur_window_packets); + #endif + + if (state != CS_DESTROY) flush_packets(); + + switch (state) { + case CS_SYN_SENT: + case CS_SYN_RECV: + case CS_CONNECTED_FULL: + case CS_CONNECTED: { + + // Reset max window... + if ((int)(ctx->current_ms - zerowindow_time) >= 0 && max_window_user == 0) { + max_window_user = PACKET_SIZE; + } + + if ((int)(ctx->current_ms - rto_timeout) >= 0 + && rto_timeout > 0) { + + bool ignore_loss = false; + + if (cur_window_packets == 1 + && ((seq_nr - 1) & ACK_NR_MASK) == mtu_probe_seq + && mtu_probe_seq != 0) { + // we only had a single outstanding packet that timed out, and it was the probe + mtu_ceiling = mtu_probe_size - 1; + mtu_search_update(); + // this packet was most likely dropped because the packet size being + // too big and not because congestion. To accelerate the binary search for + // the MTU, resend immediately and don't reset the window size + ignore_loss = true; + log(UTP_LOG_MTU, "MTU [PROBE-TIMEOUT] floor:%d ceiling:%d current:%d" + , mtu_floor, mtu_ceiling, mtu_last); + } + // we dropepd the probe, clear these fields to + // allow us to send a new one + mtu_probe_seq = mtu_probe_size = 0; + log(UTP_LOG_MTU, "MTU [TIMEOUT]"); + + /* + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(seq_nr - cur_window_packets); + + // If there were a lot of retransmissions, force recomputation of round trip time + if (pkt->transmissions >= 4) + rtt = 0; + */ + + // Increase RTO + const uint new_timeout = ignore_loss ? retransmit_timeout : retransmit_timeout * 2; + + // They initiated the connection but failed to respond before the rto. + // A malicious client can also spoof the destination address of a ST_SYN bringing us to this state. + // Kill the connection and do not notify the upper layer + if (state == CS_SYN_RECV) { + state = CS_DESTROY; + utp_call_on_error(ctx, this, UTP_ETIMEDOUT); + return; + } + + // We initiated the connection but the other side failed to respond before the rto + if (retransmit_count >= 4 || (state == CS_SYN_SENT && retransmit_count >= 2)) { + // 4 consecutive transmissions have timed out. Kill it. If we + // haven't even connected yet, give up after only 2 consecutive + // failed transmissions. + if (close_requested) + state = CS_DESTROY; + else + state = CS_RESET; + utp_call_on_error(ctx, this, UTP_ETIMEDOUT); + return; + } + + retransmit_timeout = new_timeout; + rto_timeout = ctx->current_ms + new_timeout; + + if (!ignore_loss) { + // On Timeout + duplicate_ack = 0; + + int packet_size = get_packet_size(); + + if ((cur_window_packets == 0) && ((int)max_window > packet_size)) { + // we don't have any packets in-flight, even though + // we could. This implies that the connection is just + // idling. No need to be aggressive about resetting the + // congestion window. Just let it decay by a 3:rd. + // don't set it any lower than the packet size though + max_window = max(max_window * 2 / 3, size_t(packet_size)); + } else { + // our delay was so high that our congestion window + // was shrunk below one packet, preventing us from + // sending anything for one time-out period. Now, reset + // the congestion window to fit one packet, to start over + // again + max_window = packet_size; + slow_start = true; + } + } + + // every packet should be considered lost + for (int i = 0; i < cur_window_packets; ++i) { + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(seq_nr - i - 1); + if (pkt == 0 || pkt->transmissions == 0 || pkt->need_resend) continue; + pkt->need_resend = true; + assert(cur_window >= pkt->payload); + cur_window -= pkt->payload; + } + + if (cur_window_packets > 0) { + retransmit_count++; + // used in parse_log.py + log(UTP_LOG_NORMAL, "Packet timeout. Resend. seq_nr:%u. timeout:%u " + "max_window:%u cur_window_packets:%d" + , seq_nr - cur_window_packets, retransmit_timeout + , (uint)max_window, int(cur_window_packets)); + + fast_timeout = true; + timeout_seq_nr = seq_nr; + + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(seq_nr - cur_window_packets); + assert(pkt); + + // Re-send the packet. + send_packet(pkt); + } + } + + // Mark the socket as writable. If the cwnd has grown, or if the number of + // bytes in-flight is lower than cwnd, we need to make the socket writable again + // in case it isn't + if (state == CS_CONNECTED_FULL && !is_full()) { + state = CS_CONNECTED; + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "Socket writable. max_window:%u cur_window:%u packet_size:%u", + (uint)max_window, (uint)cur_window, (uint)get_packet_size()); + #endif + utp_call_on_state_change(this->ctx, this, UTP_STATE_WRITABLE); + } + + if (state >= CS_CONNECTED && !fin_sent) { + if ((int)(ctx->current_ms - last_sent_packet) >= KEEPALIVE_INTERVAL) { + send_keep_alive(); + } + } + break; + } + + // prevent warning + case CS_UNINITIALIZED: + case CS_IDLE: + case CS_RESET: + case CS_DESTROY: + break; + } +} + +// this should be called every time we change mtu_floor or mtu_ceiling +void UTPSocket::mtu_search_update() +{ + assert(mtu_floor <= mtu_ceiling); + + // binary search + mtu_last = (mtu_floor + mtu_ceiling) / 2; + + // enable a new probe to be sent + mtu_probe_seq = mtu_probe_size = 0; + + // if the floor and ceiling are close enough, consider the + // MTU binary search complete. We set the current value + // to floor since that's the only size we know can go through + // also set the ceiling to floor to terminate the searching + if (mtu_ceiling - mtu_floor <= 16) { + mtu_last = mtu_floor; + log(UTP_LOG_MTU, "MTU [DONE] floor:%d ceiling:%d current:%d" + , mtu_floor, mtu_ceiling, mtu_last); + mtu_ceiling = mtu_floor; + assert(mtu_floor <= mtu_ceiling); + // Do another search in 30 minutes + mtu_discover_time = utp_call_get_milliseconds(this->ctx, this) + 30 * 60 * 1000; + } +} + +void UTPSocket::mtu_reset() +{ + mtu_ceiling = get_udp_mtu(); + // Less would not pass TCP... + mtu_floor = 576; + log(UTP_LOG_MTU, "MTU [RESET] floor:%d ceiling:%d current:%d" + , mtu_floor, mtu_ceiling, mtu_last); + assert(mtu_floor <= mtu_ceiling); + mtu_discover_time = utp_call_get_milliseconds(this->ctx, this) + 30 * 60 * 1000; +} + +// returns: +// 0: the packet was acked. +// 1: it means that the packet had already been acked +// 2: the packet has not been sent yet +int UTPSocket::ack_packet(uint16 seq) +{ + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(seq); + + // the packet has already been acked (or not sent) + if (pkt == NULL) { + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "got ack for:%u (already acked, or never sent)", seq); + #endif + + return 1; + } + + // can't ack packets that haven't been sent yet! + if (pkt->transmissions == 0) { + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "got ack for:%u (never sent, pkt_size:%u need_resend:%u)", + seq, (uint)pkt->payload, pkt->need_resend); + #endif + + return 2; + } + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "got ack for:%u (pkt_size:%u need_resend:%u)", + seq, (uint)pkt->payload, pkt->need_resend); + #endif + + outbuf.put(seq, NULL); + + // if we never re-sent the packet, update the RTT estimate + if (pkt->transmissions == 1) { + // Estimate the round trip time. + const uint32 ertt = (uint32)((utp_call_get_microseconds(this->ctx, this) - pkt->time_sent) / 1000); + if (rtt == 0) { + // First round trip time sample + rtt = ertt; + rtt_var = ertt / 2; + // sanity check. rtt should never be more than 6 seconds +// assert(rtt < 6000); + } else { + // Compute new round trip times + const int delta = (int)rtt - ertt; + rtt_var = rtt_var + (int)(abs(delta) - rtt_var) / 4; + rtt = rtt - rtt/8 + ertt/8; + // sanity check. rtt should never be more than 6 seconds +// assert(rtt < 6000); + rtt_hist.add_sample(ertt, ctx->current_ms); + } + rto = max(rtt + rtt_var * 4, 1000); + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "rtt:%u avg:%u var:%u rto:%u", + ertt, rtt, rtt_var, rto); + #endif + + } + retransmit_timeout = rto; + rto_timeout = ctx->current_ms + rto; + // if need_resend is set, this packet has already + // been considered timed-out, and is not included in + // the cur_window anymore + if (!pkt->need_resend) { + assert(cur_window >= pkt->payload); + cur_window -= pkt->payload; + } + free(pkt); + retransmit_count = 0; + return 0; +} + +// count the number of bytes that were acked by the EACK header +size_t UTPSocket::selective_ack_bytes(uint base, const byte* mask, byte len, int64& min_rtt) +{ + if (cur_window_packets == 0) return 0; + + size_t acked_bytes = 0; + int bits = len * 8; + uint64 now = utp_call_get_microseconds(this->ctx, this); + + do { + uint v = base + bits; + + // ignore bits that haven't been sent yet + // see comment in UTPSocket::selective_ack + if (((seq_nr - v - 1) & ACK_NR_MASK) >= (uint16)(cur_window_packets - 1)) + continue; + + // ignore bits that represents packets we haven't sent yet + // or packets that have already been acked + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(v); + if (!pkt || pkt->transmissions == 0) + continue; + + // Count the number of segments that were successfully received past it. + if (bits >= 0 && mask[bits>>3] & (1 << (bits & 7))) { + assert((int)(pkt->payload) >= 0); + acked_bytes += pkt->payload; + if (pkt->time_sent < now) + min_rtt = min(min_rtt, now - pkt->time_sent); + else + min_rtt = min(min_rtt, 50000); + continue; + } + } while (--bits >= -1); + return acked_bytes; +} + +enum { MAX_EACK = 128 }; + +void UTPSocket::selective_ack(uint base, const byte *mask, byte len) +{ + if (cur_window_packets == 0) return; + + // the range is inclusive [0, 31] bits + int bits = len * 8 - 1; + + int count = 0; + + // resends is a stack of sequence numbers we need to resend. Since we + // iterate in reverse over the acked packets, at the end, the top packets + // are the ones we want to resend + int resends[MAX_EACK]; + int nr = 0; + +#if UTP_DEBUG_LOGGING + char bitmask[1024] = {0}; + int counter = bits; + for (int i = 0; i <= bits; ++i) { + bool bit_set = counter >= 0 && mask[counter>>3] & (1 << (counter & 7)); + bitmask[i] = bit_set ? '1' : '0'; + --counter; + } + + log(UTP_LOG_DEBUG, "Got EACK [%s] base:%u", bitmask, base); +#endif + + do { + // we're iterating over the bits from higher sequence numbers + // to lower (kind of in reverse order, wich might not be very + // intuitive) + uint v = base + bits; + + // ignore bits that haven't been sent yet + // and bits that fall below the ACKed sequence number + // this can happen if an EACK message gets + // reordered and arrives after a packet that ACKs up past + // the base for thie EACK message + + // this is essentially the same as: + // if v >= seq_nr || v <= seq_nr - cur_window_packets + // but it takes wrapping into account + + // if v == seq_nr the -1 will make it wrap. if v > seq_nr + // it will also wrap (since it will fall further below 0) + // and be > cur_window_packets. + // if v == seq_nr - cur_window_packets, the result will be + // seq_nr - (seq_nr - cur_window_packets) - 1 + // == seq_nr - seq_nr + cur_window_packets - 1 + // == cur_window_packets - 1 which will be caught by the + // test. If v < seq_nr - cur_window_packets the result will grow + // fall furhter outside of the cur_window_packets range. + + // sequence number space: + // + // rejected < accepted > rejected + // <============+--------------+============> + // ^ ^ + // | | + // (seq_nr-wnd) seq_nr + + if (((seq_nr - v - 1) & ACK_NR_MASK) >= (uint16)(cur_window_packets - 1)) + continue; + + // this counts as a duplicate ack, even though we might have + // received an ack for this packet previously (in another EACK + // message for instance) + bool bit_set = bits >= 0 && mask[bits>>3] & (1 << (bits & 7)); + + // if this packet is acked, it counts towards the duplicate ack counter + if (bit_set) count++; + + // ignore bits that represents packets we haven't sent yet + // or packets that have already been acked + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(v); + if (!pkt || pkt->transmissions == 0) { + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "skipping %u. pkt:%08x transmissions:%u %s", + v, pkt, pkt?pkt->transmissions:0, pkt?"(not sent yet?)":"(already acked?)"); + #endif + continue; + } + + // Count the number of segments that were successfully received past it. + if (bit_set) { + // the selective ack should never ACK the packet we're waiting for to decrement cur_window_packets + assert((v & outbuf.mask) != ((seq_nr - cur_window_packets) & outbuf.mask)); + ack_packet(v); + continue; + } + + // Resend segments + // if count is less than our re-send limit, we haven't seen enough + // acked packets in front of this one to warrant a re-send. + // if count == 0, we're still going through the tail of zeroes + if (((v - fast_resend_seq_nr) & ACK_NR_MASK) <= OUTGOING_BUFFER_MAX_SIZE && + count >= DUPLICATE_ACKS_BEFORE_RESEND) { + // resends is a stack, and we're mostly interested in the top of it + // if we're full, just throw away the lower half + if (nr >= MAX_EACK - 2) { + memmove(resends, &resends[MAX_EACK/2], MAX_EACK/2 * sizeof(resends[0])); + nr -= MAX_EACK / 2; + } + resends[nr++] = v; + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "no ack for %u", v); + #endif + + } else { + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "not resending %u count:%d dup_ack:%u fast_resend_seq_nr:%u", + v, count, duplicate_ack, fast_resend_seq_nr); + #endif + } + } while (--bits >= -1); + + if (((base - 1 - fast_resend_seq_nr) & ACK_NR_MASK) <= OUTGOING_BUFFER_MAX_SIZE && + count >= DUPLICATE_ACKS_BEFORE_RESEND) { + // if we get enough duplicate acks to start + // resending, the first packet we should resend + // is base-1 + resends[nr++] = (base - 1) & ACK_NR_MASK; + + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "no ack for %u", (base - 1) & ACK_NR_MASK); + #endif + + } else { + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "not resending %u count:%d dup_ack:%u fast_resend_seq_nr:%u", + base - 1, count, duplicate_ack, fast_resend_seq_nr); + #endif + } + + bool back_off = false; + int i = 0; + while (nr > 0) { + uint v = resends[--nr]; + // don't consider the tail of 0:es to be lost packets + // only unacked packets with acked packets after should + // be considered lost + OutgoingPacket *pkt = (OutgoingPacket*)outbuf.get(v); + + // this may be an old (re-ordered) packet, and some of the + // packets in here may have been acked already. In which + // case they will not be in the send queue anymore + if (!pkt) continue; + + // used in parse_log.py + log(UTP_LOG_NORMAL, "Packet %u lost. Resending", v); + + // On Loss + back_off = true; + + #ifdef _DEBUG + ++_stats.rexmit; + #endif + + send_packet(pkt); + fast_resend_seq_nr = (v + 1) & ACK_NR_MASK; + + // Re-send max 4 packets. + if (++i >= 4) break; + } + + if (back_off) + maybe_decay_win(ctx->current_ms); + + duplicate_ack = count; +} + +void UTPSocket::apply_ccontrol(size_t bytes_acked, uint32 actual_delay, int64 min_rtt) +{ + // the delay can never be greater than the rtt. The min_rtt + // variable is the RTT in microseconds + + assert(min_rtt >= 0); + int32 our_delay = min(our_hist.get_value(), uint32(min_rtt)); + assert(our_delay != INT_MAX); + assert(our_delay >= 0); + + utp_call_on_delay_sample(this->ctx, this, our_delay / 1000); + + // This test the connection under heavy load from foreground + // traffic. Pretend that our delays are very high to force the + // connection to use sub-packet size window sizes + //our_delay *= 4; + + // target is microseconds + int target = target_delay; + if (target <= 0) target = 100000; + + // this is here to compensate for very large clock drift that affects + // the congestion controller into giving certain endpoints an unfair + // share of the bandwidth. We have an estimate of the clock drift + // (clock_drift). The unit of this is microseconds per 5 seconds. + // empirically, a reasonable cut-off appears to be about 200000 + // (which is pretty high). The main purpose is to compensate for + // people trying to "cheat" uTP by making their clock run slower, + // and this definitely catches that without any risk of false positives + // if clock_drift < -200000 start applying a penalty delay proportional + // to how far beoynd -200000 the clock drift is + int32 penalty = 0; + if (clock_drift < -200000) { + penalty = (-clock_drift - 200000) / 7; + our_delay += penalty; + } + + double off_target = target - our_delay; + + // this is the same as: + // + // (min(off_target, target) / target) * (bytes_acked / max_window) * MAX_CWND_INCREASE_BYTES_PER_RTT + // + // so, it's scaling the max increase by the fraction of the window this ack represents, and the fraction + // of the target delay the current delay represents. + // The min() around off_target protects against crazy values of our_delay, which may happen when th + // timestamps wraps, or by just having a malicious peer sending garbage. This caps the increase + // of the window size to MAX_CWND_INCREASE_BYTES_PER_RTT per rtt. + // as for large negative numbers, this direction is already capped at the min packet size further down + // the min around the bytes_acked protects against the case where the window size was recently + // shrunk and the number of acked bytes exceeds that. This is considered no more than one full + // window, in order to keep the gain within sane boundries. + + assert(bytes_acked > 0); + double window_factor = (double)min(bytes_acked, max_window) / (double)max(max_window, bytes_acked); + + double delay_factor = off_target / target; + double scaled_gain = MAX_CWND_INCREASE_BYTES_PER_RTT * window_factor * delay_factor; + + // since MAX_CWND_INCREASE_BYTES_PER_RTT is a cap on how much the window size (max_window) + // may increase per RTT, we may not increase the window size more than that proportional + // to the number of bytes that were acked, so that once one window has been acked (one rtt) + // the increase limit is not exceeded + // the +1. is to allow for floating point imprecision + assert(scaled_gain <= 1. + MAX_CWND_INCREASE_BYTES_PER_RTT * (double)min(bytes_acked, max_window) / (double)max(max_window, bytes_acked)); + + if (scaled_gain > 0 && ctx->current_ms - last_maxed_out_window > 1000) { + // if it was more than 1 second since we tried to send a packet + // and stopped because we hit the max window, we're most likely rate + // limited (which prevents us from ever hitting the window size) + // if this is the case, we cannot let the max_window grow indefinitely + scaled_gain = 0; + } + + size_t ledbat_cwnd = (max_window + scaled_gain < MIN_WINDOW_SIZE) ? MIN_WINDOW_SIZE : (size_t)(max_window + scaled_gain); + + if (slow_start) { + size_t ss_cwnd = (size_t)(max_window + window_factor*get_packet_size()); + if (ss_cwnd > ssthresh) { + slow_start = false; + } else if (our_delay > target*0.9) { + // even if we're a little under the target delay, we conservatively + // discontinue the slow start phase + slow_start = false; + ssthresh = max_window; + } else { + max_window = max(ss_cwnd, ledbat_cwnd); + } + } else { + max_window = ledbat_cwnd; + } + + + // make sure that the congestion window is below max + // make sure that we don't shrink our window too small + max_window = clamp(max_window, MIN_WINDOW_SIZE, opt_sndbuf); + + // used in parse_log.py + log(UTP_LOG_NORMAL, "actual_delay:%u our_delay:%d their_delay:%u off_target:%d max_window:%u " + "delay_base:%u delay_sum:%d target_delay:%d acked_bytes:%u cur_window:%u " + "scaled_gain:%f rtt:%u rate:%u wnduser:%u rto:%u timeout:%d get_microseconds:" I64u " " + "cur_window_packets:%u packet_size:%u their_delay_base:%u their_actual_delay:%u " + "average_delay:%d clock_drift:%d clock_drift_raw:%d delay_penalty:%d current_delay_sum:" I64u + "current_delay_samples:%d average_delay_base:%d last_maxed_out_window:" I64u " opt_sndbuf:%d " + "current_ms:" I64u "", + actual_delay, our_delay / 1000, their_hist.get_value() / 1000, + int(off_target / 1000), uint(max_window), uint32(our_hist.delay_base), + int((our_delay + their_hist.get_value()) / 1000), int(target / 1000), uint(bytes_acked), + (uint)(cur_window - bytes_acked), (float)(scaled_gain), rtt, + (uint)(max_window * 1000 / (rtt_hist.delay_base?rtt_hist.delay_base:50)), + (uint)max_window_user, rto, (int)(rto_timeout - ctx->current_ms), + utp_call_get_microseconds(this->ctx, this), cur_window_packets, (uint)get_packet_size(), + their_hist.delay_base, their_hist.delay_base + their_hist.get_value(), + average_delay, clock_drift, clock_drift_raw, penalty / 1000, + current_delay_sum, current_delay_samples, average_delay_base, + uint64(last_maxed_out_window), int(opt_sndbuf), uint64(ctx->current_ms)); +} + +static void utp_register_recv_packet(UTPSocket *conn, size_t len) +{ + #ifdef _DEBUG + ++conn->_stats.nrecv; + conn->_stats.nbytes_recv += len; + #endif + + if (len <= PACKET_SIZE_MID) { + if (len <= PACKET_SIZE_EMPTY) { + conn->ctx->context_stats._nraw_recv[PACKET_SIZE_EMPTY_BUCKET]++; + } else if (len <= PACKET_SIZE_SMALL) { + conn->ctx->context_stats._nraw_recv[PACKET_SIZE_SMALL_BUCKET]++; + } else + conn->ctx->context_stats._nraw_recv[PACKET_SIZE_MID_BUCKET]++; + } else { + if (len <= PACKET_SIZE_BIG) { + conn->ctx->context_stats._nraw_recv[PACKET_SIZE_BIG_BUCKET]++; + } else + conn->ctx->context_stats._nraw_recv[PACKET_SIZE_HUGE_BUCKET]++; + } +} + +// returns the max number of bytes of payload the uTP +// connection is allowed to send +size_t UTPSocket::get_packet_size() const +{ + int header_size = sizeof(PacketFormatV1); + size_t mtu = mtu_last ? mtu_last : mtu_ceiling; + return mtu - header_size; +} + +// Process an incoming packet +// syn is true if this is the first packet received. It will cut off parsing +// as soon as the header is done +size_t utp_process_incoming(UTPSocket *conn, const byte *packet, size_t len, bool syn = false) +{ + utp_register_recv_packet(conn, len); + + conn->ctx->current_ms = utp_call_get_milliseconds(conn->ctx, conn); + + const PacketFormatV1 *pf1 = (PacketFormatV1*)packet; + const byte *packet_end = packet + len; + + uint16 pk_seq_nr = pf1->seq_nr; + uint16 pk_ack_nr = pf1->ack_nr; + uint8 pk_flags = pf1->type(); + + if (pk_flags >= ST_NUM_STATES) return 0; + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Got %s. seq_nr:%u ack_nr:%u state:%s timestamp:" I64u " reply_micro:%u" + , flagnames[pk_flags], pk_seq_nr, pk_ack_nr, statenames[conn->state] + , uint64(pf1->tv_usec), (uint32)(pf1->reply_micro)); + #endif + + // mark receipt time + uint64 time = utp_call_get_microseconds(conn->ctx, conn); + + // window packets size is used to calculate a minimum + // permissible range for received acks. connections with acks falling + // out of this range are dropped + const uint16 curr_window = max(conn->cur_window_packets + ACK_NR_ALLOWED_WINDOW, ACK_NR_ALLOWED_WINDOW); + + // ignore packets whose ack_nr is invalid. This would imply a spoofed address + // or a malicious attempt to attach the uTP implementation. + // acking a packet that hasn't been sent yet! + // SYN packets have an exception, since there are no previous packets + if ((pk_flags != ST_SYN || conn->state != CS_SYN_RECV) && + (wrapping_compare_less(conn->seq_nr - 1, pk_ack_nr, ACK_NR_MASK) + || wrapping_compare_less(pk_ack_nr, conn->seq_nr - 1 - curr_window, ACK_NR_MASK))) { +#if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Invalid ack_nr: %u. our seq_nr: %u last unacked: %u" + , pk_ack_nr, conn->seq_nr, (conn->seq_nr - conn->cur_window_packets) & ACK_NR_MASK); +#endif + return 0; + } + + // RSTs are handled earlier, since the connid matches the send id not the recv id + assert(pk_flags != ST_RESET); + + // TODO: maybe send a ST_RESET if we're in CS_RESET? + + const byte *selack_ptr = NULL; + + // Unpack UTP packet options + // Data pointer + const byte *data = (const byte*)pf1 + conn->get_header_size(); + if (conn->get_header_size() > len) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Invalid packet size (less than header size)"); + #endif + + return 0; + } + // Skip the extension headers + uint extension = pf1->ext; + if (extension != 0) { + do { + // Verify that the packet is valid. + data += 2; + + if ((int)(packet_end - data) < 0 || (int)(packet_end - data) < data[-1]) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Invalid len of extensions"); + #endif + + return 0; + } + + switch(extension) { + case 1: // Selective Acknowledgment + selack_ptr = data; + break; + case 2: // extension bits + if (data[-1] != 8) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Invalid len of extension bits header"); + #endif + + return 0; + } + memcpy(conn->extensions, data, 8); + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "got extension bits:%02x%02x%02x%02x%02x%02x%02x%02x", + conn->extensions[0], conn->extensions[1], conn->extensions[2], conn->extensions[3], + conn->extensions[4], conn->extensions[5], conn->extensions[6], conn->extensions[7]); + #endif + } + extension = data[-2]; + data += data[-1]; + } while (extension); + } + + if (conn->state == CS_SYN_SENT) { + // if this is a syn-ack, initialize our ack_nr + // to match the sequence number we got from + // the other end + conn->ack_nr = (pk_seq_nr - 1) & SEQ_NR_MASK; + } + + conn->last_got_packet = conn->ctx->current_ms; + + if (syn) { + return 0; + } + + // seqnr is the number of packets past the expected + // packet this is. ack_nr is the last acked, seq_nr is the + // current. Subtracring 1 makes 0 mean "this is the next + // expected packet". + const uint seqnr = (pk_seq_nr - conn->ack_nr - 1) & SEQ_NR_MASK; + + // Getting an invalid sequence number? + if (seqnr >= REORDER_BUFFER_MAX_SIZE) { + if (seqnr >= (SEQ_NR_MASK + 1) - REORDER_BUFFER_MAX_SIZE && pk_flags != ST_STATE) { + conn->schedule_ack(); + } + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, " Got old Packet/Ack (%u/%u)=%u" + , pk_seq_nr, conn->ack_nr, seqnr); + #endif + return 0; + } + + // Process acknowledgment + // acks is the number of packets that was acked + int acks = (pk_ack_nr - (conn->seq_nr - 1 - conn->cur_window_packets)) & ACK_NR_MASK; + + // this happens when we receive an old ack nr + if (acks > conn->cur_window_packets) acks = 0; + + // if we get the same ack_nr as in the last packet + // increase the duplicate_ack counter, otherwise reset + // it to 0. + // It's important to only count ACKs in ST_STATE packets. Any other + // packet (primarily ST_DATA) is likely to have been sent because of the + // other end having new outgoing data, not in response to incoming data. + // For instance, if we're receiving a steady stream of payload with no + // outgoing data, and we suddently have a few bytes of payload to send (say, + // a bittorrent HAVE message), we're very likely to see 3 duplicate ACKs + // immediately after sending our payload packet. This effectively disables + // the fast-resend on duplicate-ack logic for bi-directional connections + // (except in the case of a selective ACK). This is in line with BSD4.4 TCP + // implementation. + if (conn->cur_window_packets > 0) { + if (pk_ack_nr == ((conn->seq_nr - conn->cur_window_packets - 1) & ACK_NR_MASK) + && conn->cur_window_packets > 0 + && pk_flags == ST_STATE) { + ++conn->duplicate_ack; + if (conn->duplicate_ack == DUPLICATE_ACKS_BEFORE_RESEND && conn->mtu_probe_seq) { + // It's likely that the probe was rejected due to its size, but we haven't got an + // ICMP report back yet + if (pk_ack_nr == ((conn->mtu_probe_seq - 1) & ACK_NR_MASK)) { + conn->mtu_ceiling = conn->mtu_probe_size - 1; + conn->mtu_search_update(); + conn->log(UTP_LOG_MTU, "MTU [DUPACK] floor:%d ceiling:%d current:%d" + , conn->mtu_floor, conn->mtu_ceiling, conn->mtu_last); + } else { + // A non-probe was blocked before our probe. + // Can't conclude much, send a new probe + conn->mtu_probe_seq = conn->mtu_probe_size = 0; + } + } + } else { + conn->duplicate_ack = 0; + } + + // TODO: if duplicate_ack == DUPLICATE_ACK_BEFORE_RESEND + // and fast_resend_seq_nr <= ack_nr + 1 + // resend ack_nr + 1 + // also call maybe_decay_win() + } + + // figure out how many bytes were acked + size_t acked_bytes = 0; + + // the minimum rtt of all acks + // this is the upper limit on the delay we get back + // from the other peer. Our delay cannot exceed + // the rtt of the packet. If it does, clamp it. + // this is done in apply_ledbat_ccontrol() + int64 min_rtt = INT64_MAX; + + uint64 now = utp_call_get_microseconds(conn->ctx, conn); + + for (int i = 0; i < acks; ++i) { + int seq = (conn->seq_nr - conn->cur_window_packets + i) & ACK_NR_MASK; + OutgoingPacket *pkt = (OutgoingPacket*)conn->outbuf.get(seq); + if (pkt == 0 || pkt->transmissions == 0) continue; + assert((int)(pkt->payload) >= 0); + acked_bytes += pkt->payload; + if (conn->mtu_probe_seq && seq == conn->mtu_probe_seq) { + conn->mtu_floor = conn->mtu_probe_size; + conn->mtu_search_update(); + conn->log(UTP_LOG_MTU, "MTU [ACK] floor:%d ceiling:%d current:%d" + , conn->mtu_floor, conn->mtu_ceiling, conn->mtu_last); + } + + // in case our clock is not monotonic + if (pkt->time_sent < now) + min_rtt = min(min_rtt, now - pkt->time_sent); + else + min_rtt = min(min_rtt, 50000); + } + + // count bytes acked by EACK + if (selack_ptr != NULL) { + acked_bytes += conn->selective_ack_bytes((pk_ack_nr + 2) & ACK_NR_MASK, + selack_ptr, selack_ptr[-1], min_rtt); + } + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "acks:%d acked_bytes:%u seq_nr:%d cur_window:%u cur_window_packets:%u relative_seqnr:%u max_window:%u min_rtt:%u rtt:%u", + acks, (uint)acked_bytes, conn->seq_nr, (uint)conn->cur_window, conn->cur_window_packets, + seqnr, (uint)conn->max_window, (uint)(min_rtt / 1000), conn->rtt); + #endif + + uint64 p = pf1->tv_usec; + + conn->last_measured_delay = conn->ctx->current_ms; + + // get delay in both directions + // record the delay to report back + const uint32 their_delay = (uint32)(p == 0 ? 0 : time - p); + conn->reply_micro = their_delay; + uint32 prev_delay_base = conn->their_hist.delay_base; + if (their_delay != 0) conn->their_hist.add_sample(their_delay, conn->ctx->current_ms); + + // if their new delay base is less than their previous one + // we should shift our delay base in the other direction in order + // to take the clock skew into account + if (prev_delay_base != 0 && + wrapping_compare_less(conn->their_hist.delay_base, prev_delay_base, TIMESTAMP_MASK)) { + // never adjust more than 10 milliseconds + if (prev_delay_base - conn->their_hist.delay_base <= 10000) { + conn->our_hist.shift(prev_delay_base - conn->their_hist.delay_base); + } + } + + const uint32 actual_delay = (uint32(pf1->reply_micro)==INT_MAX?0:uint32(pf1->reply_micro)); + + // if the actual delay is 0, it means the other end + // hasn't received a sample from us yet, and doesn't + // know what it is. We can't update out history unless + // we have a true measured sample + if (actual_delay != 0) { + conn->our_hist.add_sample(actual_delay, conn->ctx->current_ms); + + // this is keeping an average of the delay samples + // we've recevied within the last 5 seconds. We sum + // all the samples and increase the count in order to + // calculate the average every 5 seconds. The samples + // are based off of the average_delay_base to deal with + // wrapping counters. + if (conn->average_delay_base == 0) conn->average_delay_base = actual_delay; + int64 average_delay_sample = 0; + // distance walking from lhs to rhs, downwards + const uint32 dist_down = conn->average_delay_base - actual_delay; + // distance walking from lhs to rhs, upwards + const uint32 dist_up = actual_delay - conn->average_delay_base; + + if (dist_down > dist_up) { +// assert(dist_up < INT_MAX / 4); + // average_delay_base < actual_delay, we should end up + // with a positive sample + average_delay_sample = dist_up; + } else { +// assert(-int64(dist_down) < INT_MAX / 4); + // average_delay_base >= actual_delay, we should end up + // with a negative sample + average_delay_sample = -int64(dist_down); + } + conn->current_delay_sum += average_delay_sample; + ++conn->current_delay_samples; + + if (conn->ctx->current_ms > conn->average_sample_time) { + + int32 prev_average_delay = conn->average_delay; + + assert(conn->current_delay_sum / conn->current_delay_samples < INT_MAX); + assert(conn->current_delay_sum / conn->current_delay_samples > -INT_MAX); + // write the new average + conn->average_delay = (int32)(conn->current_delay_sum / conn->current_delay_samples); + // each slot represents 5 seconds + conn->average_sample_time += 5000; + + conn->current_delay_sum = 0; + conn->current_delay_samples = 0; + + // this makes things very confusing when logging the average delay +//#if !g_log_utp + // normalize the average samples + // since we're only interested in the slope + // of the curve formed by the average delay samples, + // we can cancel out the actual offset to make sure + // we won't have problems with wrapping. + int min_sample = min(prev_average_delay, conn->average_delay); + int max_sample = max(prev_average_delay, conn->average_delay); + + // normalize around zero. Try to keep the min <= 0 and max >= 0 + int adjust = 0; + if (min_sample > 0) { + // adjust all samples (and the baseline) down by min_sample + adjust = -min_sample; + } else if (max_sample < 0) { + // adjust all samples (and the baseline) up by -max_sample + adjust = -max_sample; + } + if (adjust) { + conn->average_delay_base -= adjust; + conn->average_delay += adjust; + prev_average_delay += adjust; + } +//#endif + + // update the clock drift estimate + // the unit is microseconds per 5 seconds + // what we're doing is just calculating the average of the + // difference between each slot. Since each slot is 5 seconds + // and the timestamps unit are microseconds, we'll end up with + // the average slope across our history. If there is a consistent + // trend, it will show up in this value + + //int64 slope = 0; + int32 drift = conn->average_delay - prev_average_delay; + + // clock_drift is a rolling average + conn->clock_drift = (int64(conn->clock_drift) * 7 + drift) / 8; + conn->clock_drift_raw = drift; + } + } + + // if our new delay base is less than our previous one + // we should shift the other end's delay base in the other + // direction in order to take the clock skew into account + // This is commented out because it creates bad interactions + // with our adjustment in the other direction. We don't really + // need our estimates of the other peer to be very accurate + // anyway. The problem with shifting here is that we're more + // likely shift it back later because of a low latency. This + // second shift back would cause us to shift our delay base + // which then get's into a death spiral of shifting delay bases +/* if (prev_delay_base != 0 && + wrapping_compare_less(conn->our_hist.delay_base, prev_delay_base)) { + // never adjust more than 10 milliseconds + if (prev_delay_base - conn->our_hist.delay_base <= 10000) { + conn->their_hist.Shift(prev_delay_base - conn->our_hist.delay_base); + } + } +*/ + + // if the delay estimate exceeds the RTT, adjust the base_delay to + // compensate + assert(min_rtt >= 0); + if (int64(conn->our_hist.get_value()) > min_rtt) { + conn->our_hist.shift((uint32)(conn->our_hist.get_value() - min_rtt)); + } + + // only apply the congestion controller on acks + // if we don't have a delay measurement, there's + // no point in invoking the congestion control + if (actual_delay != 0 && acked_bytes >= 1) + conn->apply_ccontrol(acked_bytes, actual_delay, min_rtt); + + // sanity check, the other end should never ack packets + // past the point we've sent + if (acks <= conn->cur_window_packets) { + conn->max_window_user = pf1->windowsize; + + // If max user window is set to 0, then we startup a timer + // That will reset it to 1 after 15 seconds. + if (conn->max_window_user == 0) + // Reset max_window_user to 1 every 15 seconds. + conn->zerowindow_time = conn->ctx->current_ms + 15000; + + // Respond to connect message + // Switch to CONNECTED state. + // If this is an ack and we're in still handshaking + // transition over to the connected state. + + // Incoming connection completion + if (pk_flags == ST_DATA && conn->state == CS_SYN_RECV) { + conn->state = CS_CONNECTED; + } + + // Outgoing connection completion + if (pk_flags == ST_STATE && conn->state == CS_SYN_SENT) { + conn->state = CS_CONNECTED; + + // If the user has defined the ON_CONNECT callback, use that to + // notify the user that the socket is now connected. If ON_CONNECT + // has not been defined, notify the user via ON_STATE_CHANGE. + if (conn->ctx->callbacks[UTP_ON_CONNECT]) + utp_call_on_connect(conn->ctx, conn); + else + utp_call_on_state_change(conn->ctx, conn, UTP_STATE_CONNECT); + + // We've sent a fin, and everything was ACKed (including the FIN). + // cur_window_packets == acks means that this packet acked all + // the remaining packets that were in-flight. + } else if (conn->fin_sent && conn->cur_window_packets == acks) { + conn->fin_sent_acked = true; + if (conn->close_requested) { + conn->state = CS_DESTROY; + } + } + + // Update fast resend counter + if (wrapping_compare_less(conn->fast_resend_seq_nr + , (pk_ack_nr + 1) & ACK_NR_MASK, ACK_NR_MASK)) + conn->fast_resend_seq_nr = (pk_ack_nr + 1) & ACK_NR_MASK; + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "fast_resend_seq_nr:%u", conn->fast_resend_seq_nr); + #endif + + for (int i = 0; i < acks; ++i) { + int ack_status = conn->ack_packet(conn->seq_nr - conn->cur_window_packets); + // if ack_status is 0, the packet was acked. + // if acl_stauts is 1, it means that the packet had already been acked + // if it's 2, the packet has not been sent yet + // We need to break this loop in the latter case. This could potentially + // happen if we get an ack_nr that does not exceed what we have stuffed + // into the outgoing buffer, but does exceed what we have sent + if (ack_status == 2) { + #ifdef _DEBUG + OutgoingPacket* pkt = (OutgoingPacket*)conn->outbuf.get(conn->seq_nr - conn->cur_window_packets); + assert(pkt->transmissions == 0); + #endif + + break; + } + conn->cur_window_packets--; + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "decementing cur_window_packets:%u", conn->cur_window_packets); + #endif + + } + + #ifdef _DEBUG + if (conn->cur_window_packets == 0) + assert(conn->cur_window == 0); + #endif + + // packets in front of this may have been acked by a + // selective ack (EACK). Keep decreasing the window packet size + // until we hit a packet that is still waiting to be acked + // in the send queue + // this is especially likely to happen when the other end + // has the EACK send bug older versions of uTP had + while (conn->cur_window_packets > 0 && !conn->outbuf.get(conn->seq_nr - conn->cur_window_packets)) { + conn->cur_window_packets--; + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "decementing cur_window_packets:%u", conn->cur_window_packets); + #endif + + } + + #ifdef _DEBUG + if (conn->cur_window_packets == 0) + assert(conn->cur_window == 0); + #endif + + // this invariant should always be true + assert(conn->cur_window_packets == 0 || conn->outbuf.get(conn->seq_nr - conn->cur_window_packets)); + + // flush Nagle + if (conn->cur_window_packets == 1) { + OutgoingPacket *pkt = (OutgoingPacket*)conn->outbuf.get(conn->seq_nr - 1); + // do we still have quota? + if (pkt->transmissions == 0) { + conn->send_packet(pkt); + } + } + + // Fast timeout-retry + if (conn->fast_timeout) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Fast timeout %u,%u,%u?", (uint)conn->cur_window, conn->seq_nr - conn->timeout_seq_nr, conn->timeout_seq_nr); + #endif + + // if the fast_resend_seq_nr is not pointing to the oldest outstanding packet, it suggests that we've already + // resent the packet that timed out, and we should leave the fast-timeout mode. + if (((conn->seq_nr - conn->cur_window_packets) & ACK_NR_MASK) != conn->fast_resend_seq_nr) { + conn->fast_timeout = false; + } else { + // resend the oldest packet and increment fast_resend_seq_nr + // to not allow another fast resend on it again + OutgoingPacket *pkt = (OutgoingPacket*)conn->outbuf.get(conn->seq_nr - conn->cur_window_packets); + if (pkt && pkt->transmissions > 0) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Packet %u fast timeout-retry.", conn->seq_nr - conn->cur_window_packets); + #endif + + #ifdef _DEBUG + ++conn->_stats.fastrexmit; + #endif + + conn->fast_resend_seq_nr++; + conn->send_packet(pkt); + } + } + } + } + + // Process selective acknowledgent + if (selack_ptr != NULL) { + conn->selective_ack(pk_ack_nr + 2, selack_ptr, selack_ptr[-1]); + } + + // this invariant should always be true + assert(conn->cur_window_packets == 0 || conn->outbuf.get(conn->seq_nr - conn->cur_window_packets)); + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "acks:%d acked_bytes:%u seq_nr:%u cur_window:%u cur_window_packets:%u ", + acks, (uint)acked_bytes, conn->seq_nr, (uint)conn->cur_window, conn->cur_window_packets); + #endif + + // In case the ack dropped the current window below + // the max_window size, Mark the socket as writable + if (conn->state == CS_CONNECTED_FULL && !conn->is_full()) { + conn->state = CS_CONNECTED; + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Socket writable. max_window:%u cur_window:%u packet_size:%u", + (uint)conn->max_window, (uint)conn->cur_window, (uint)conn->get_packet_size()); + #endif + utp_call_on_state_change(conn->ctx, conn, UTP_STATE_WRITABLE); + } + + if (pk_flags == ST_STATE) { + // This is a state packet only. + return 0; + } + + // The connection is not in a state that can accept data? + if (conn->state != CS_CONNECTED && + conn->state != CS_CONNECTED_FULL) { + return 0; + } + + // Is this a finalize packet? + if (pk_flags == ST_FIN && !conn->got_fin) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Got FIN eof_pkt:%u", pk_seq_nr); + #endif + + conn->got_fin = true; + conn->eof_pkt = pk_seq_nr; + // at this point, it is possible for the + // other end to have sent packets with + // sequence numbers higher than seq_nr. + // if this is the case, our reorder_count + // is out of sync. This case is dealt with + // when we re-order and hit the eof_pkt. + // we'll just ignore any packets with + // sequence numbers past this + } + + // Getting an in-order packet? + if (seqnr == 0) { + size_t count = packet_end - data; + if (count > 0 && !conn->read_shutdown) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Got Data len:%u (rb:%u)", (uint)count, (uint)utp_call_get_read_buffer_size(conn->ctx, conn)); + #endif + + // Post bytes to the upper layer + utp_call_on_read(conn->ctx, conn, data, count); + } + conn->ack_nr++; + + // Check if the next packet has been received too, but waiting + // in the reorder buffer. + for (;;) { + + if (!conn->got_fin_reached && conn->got_fin && conn->eof_pkt == conn->ack_nr) { + conn->got_fin_reached = true; + conn->rto_timeout = conn->ctx->current_ms + min(conn->rto * 3, 60); + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Posting EOF"); + #endif + + utp_call_on_state_change(conn->ctx, conn, UTP_STATE_EOF); + + // if the other end wants to close, ack + conn->send_ack(); + + // reorder_count is not necessarily 0 at this point. + // even though it is most of the time, the other end + // may have sent packets with higher sequence numbers + // than what later end up being eof_pkt + // since we have received all packets up to eof_pkt + // just ignore the ones after it. + conn->reorder_count = 0; + } + + // Quick get-out in case there is nothing to reorder + if (conn->reorder_count == 0) + break; + + // Check if there are additional buffers in the reorder buffers + // that need delivery. + byte *p = (byte*)conn->inbuf.get(conn->ack_nr+1); + if (p == NULL) + break; + conn->inbuf.put(conn->ack_nr+1, NULL); + count = *(uint*)p; + if (count > 0 && !conn->read_shutdown) { + // Pass the bytes to the upper layer + utp_call_on_read(conn->ctx, conn, p + sizeof(uint), count); + } + conn->ack_nr++; + + // Free the element from the reorder buffer + free(p); + assert(conn->reorder_count > 0); + conn->reorder_count--; + } + + conn->schedule_ack(); + } else { + // Getting an out of order packet. + // The packet needs to be remembered and rearranged later. + + // if we have received a FIN packet, and the EOF-sequence number + // is lower than the sequence number of the packet we just received + // something is wrong. + if (conn->got_fin && pk_seq_nr > conn->eof_pkt) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Got an invalid packet sequence number, past EOF " + "reorder_count:%u len:%u (rb:%u)", + conn->reorder_count, (uint)(packet_end - data), (uint)utp_call_get_read_buffer_size(conn->ctx, conn)); + #endif + return 0; + } + + // if the sequence number is entirely off the expected + // one, just drop it. We can't allocate buffer space in + // the inbuf entirely based on untrusted input + if (seqnr > 0x3ff) { + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "0x%08x: Got an invalid packet sequence number, too far off " + "reorder_count:%u len:%u (rb:%u)", + conn->reorder_count, (uint)(packet_end - data), (uint)utp_call_get_read_buffer_size(conn->ctx, conn)); + #endif + return 0; + } + + // we need to grow the circle buffer before we + // check if the packet is already in here, so that + // we don't end up looking at an older packet (since + // the indices wraps around). + conn->inbuf.ensure_size(pk_seq_nr + 1, seqnr + 1); + + // Has this packet already been received? (i.e. a duplicate) + // If that is the case, just discard it. + if (conn->inbuf.get(pk_seq_nr) != NULL) { + #ifdef _DEBUG + ++conn->_stats.nduprecv; + #endif + + return 0; + } + + // Allocate memory to fit the packet that needs to re-ordered + byte *mem = (byte*)malloc((packet_end - data) + sizeof(uint)); + *(uint*)mem = (uint)(packet_end - data); + memcpy(mem + sizeof(uint), data, packet_end - data); + + // Insert into reorder buffer and increment the count + // of # of packets to be reordered. + // we add one to seqnr in order to leave the last + // entry empty, that way the assert in send_ack + // is valid. we have to add one to seqnr too, in order + // to make the circular buffer grow around the correct + // point (which is conn->ack_nr + 1). + assert(conn->inbuf.get(pk_seq_nr) == NULL); + assert((pk_seq_nr & conn->inbuf.mask) != ((conn->ack_nr+1) & conn->inbuf.mask)); + conn->inbuf.put(pk_seq_nr, mem); + conn->reorder_count++; + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "0x%08x: Got out of order data reorder_count:%u len:%u (rb:%u)", + conn->reorder_count, (uint)(packet_end - data), (uint)utp_call_get_read_buffer_size(conn->ctx, conn)); + #endif + + conn->schedule_ack(); + } + + return (size_t)(packet_end - data); +} + +inline byte UTP_Version(PacketFormatV1 const* pf) +{ + return (pf->type() < ST_NUM_STATES && pf->ext < 3 ? pf->version() : 0); +} + +UTPSocket::~UTPSocket() +{ + #if UTP_DEBUG_LOGGING + log(UTP_LOG_DEBUG, "Killing socket"); + #endif + + utp_call_on_state_change(ctx, this, UTP_STATE_DESTROYING); + + if (ctx->last_utp_socket == this) { + ctx->last_utp_socket = NULL; + } + + // Remove object from the global hash table + UTPSocketKeyData* kd = ctx->utp_sockets->Delete(UTPSocketKey(addr, conn_id_recv)); + assert(kd); + + // remove the socket from ack_sockets if it was there also + removeSocketFromAckList(this); + + // Free all memory occupied by the socket object. + for (size_t i = 0; i <= inbuf.mask; i++) { + free(inbuf.elements[i]); + } + for (size_t i = 0; i <= outbuf.mask; i++) { + free(outbuf.elements[i]); + } + // TODO: The circular buffer should have a destructor + free(inbuf.elements); + free(outbuf.elements); +} + +void UTP_FreeAll(struct UTPSocketHT *utp_sockets) { + utp_hash_iterator_t it; + UTPSocketKeyData* keyData; + while ((keyData = utp_sockets->Iterate(it))) { + delete keyData->socket; + } +} + +void utp_initialize_socket( utp_socket *conn, + const struct sockaddr *addr, + socklen_t addrlen, + bool need_seed_gen, + uint32 conn_seed, + uint32 conn_id_recv, + uint32 conn_id_send) +{ + PackedSockAddr psaddr = PackedSockAddr((const SOCKADDR_STORAGE*)addr, addrlen); + + if (need_seed_gen) { + do { + conn_seed = utp_call_get_random(conn->ctx, conn); + // we identify v1 and higher by setting the first two bytes to 0x0001 + conn_seed &= 0xffff; + } while (conn->ctx->utp_sockets->Lookup(UTPSocketKey(psaddr, conn_seed))); + + conn_id_recv += conn_seed; + conn_id_send += conn_seed; + } + + conn->state = CS_IDLE; + conn->conn_seed = conn_seed; + conn->conn_id_recv = conn_id_recv; + conn->conn_id_send = conn_id_send; + conn->addr = psaddr; + conn->ctx->current_ms = utp_call_get_milliseconds(conn->ctx, NULL); + conn->last_got_packet = conn->ctx->current_ms; + conn->last_sent_packet = conn->ctx->current_ms; + conn->last_measured_delay = conn->ctx->current_ms + 0x70000000; + conn->average_sample_time = conn->ctx->current_ms + 5000; + conn->last_rwin_decay = conn->ctx->current_ms - MAX_WINDOW_DECAY; + + conn->our_hist.clear(conn->ctx->current_ms); + conn->their_hist.clear(conn->ctx->current_ms); + conn->rtt_hist.clear(conn->ctx->current_ms); + + // initialize MTU floor and ceiling + conn->mtu_reset(); + conn->mtu_last = conn->mtu_ceiling; + + conn->ctx->utp_sockets->Add(UTPSocketKey(conn->addr, conn->conn_id_recv))->socket = conn; + + // we need to fit one packet in the window when we start the connection + conn->max_window = conn->get_packet_size(); + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP socket initialized"); + #endif +} + +utp_socket* utp_create_socket(utp_context *ctx) +{ + assert(ctx); + if (!ctx) return NULL; + + UTPSocket *conn = new UTPSocket; // TODO: UTPSocket should have a constructor + + conn->state = CS_UNINITIALIZED; + conn->ctx = ctx; + conn->userdata = NULL; + conn->reorder_count = 0; + conn->duplicate_ack = 0; + conn->timeout_seq_nr = 0; + conn->last_rcv_win = 0; + conn->got_fin = false; + conn->got_fin_reached = false; + conn->fin_sent = false; + conn->fin_sent_acked = false; + conn->read_shutdown = false; + conn->close_requested = false; + conn->fast_timeout = false; + conn->rtt = 0; + conn->retransmit_timeout = 0; + conn->rto_timeout = 0; + conn->zerowindow_time = 0; + conn->average_delay = 0; + conn->current_delay_samples = 0; + conn->cur_window = 0; + conn->eof_pkt = 0; + conn->last_maxed_out_window = 0; + conn->mtu_probe_seq = 0; + conn->mtu_probe_size = 0; + conn->current_delay_sum = 0; + conn->average_delay_base = 0; + conn->retransmit_count = 0; + conn->rto = 3000; + conn->rtt_var = 800; + conn->seq_nr = 1; + conn->ack_nr = 0; + conn->max_window_user = 255 * PACKET_SIZE; + conn->cur_window_packets = 0; + conn->fast_resend_seq_nr = conn->seq_nr; + conn->target_delay = ctx->target_delay; + conn->reply_micro = 0; + conn->opt_sndbuf = ctx->opt_sndbuf; + conn->opt_rcvbuf = ctx->opt_rcvbuf; + conn->slow_start = true; + conn->ssthresh = conn->opt_sndbuf; + conn->clock_drift = 0; + conn->clock_drift_raw = 0; + conn->outbuf.mask = 15; + conn->inbuf.mask = 15; + conn->outbuf.elements = (void**)calloc(16, sizeof(void*)); + conn->inbuf.elements = (void**)calloc(16, sizeof(void*)); + conn->ida = -1; // set the index of every new socket in ack_sockets to + // -1, which also means it is not in ack_sockets yet + + memset(conn->extensions, 0, sizeof(conn->extensions)); + + #ifdef _DEBUG + memset(&conn->_stats, 0, sizeof(utp_socket_stats)); + #endif + + return conn; +} + +int utp_context_set_option(utp_context *ctx, int opt, int val) +{ + assert(ctx); + if (!ctx) return -1; + + switch (opt) { + case UTP_LOG_NORMAL: + ctx->log_normal = val ? true : false; + return 0; + + case UTP_LOG_MTU: + ctx->log_mtu = val ? true : false; + return 0; + + case UTP_LOG_DEBUG: + ctx->log_debug = val ? true : false; + return 0; + + case UTP_TARGET_DELAY: + ctx->target_delay = val; + return 0; + + case UTP_SNDBUF: + assert(val >= 1); + ctx->opt_sndbuf = val; + return 0; + + case UTP_RCVBUF: + assert(val >= 1); + ctx->opt_rcvbuf = val; + return 0; + } + return -1; +} + +int utp_context_get_option(utp_context *ctx, int opt) +{ + assert(ctx); + if (!ctx) return -1; + + switch (opt) { + case UTP_LOG_NORMAL: return ctx->log_normal ? 1 : 0; + case UTP_LOG_MTU: return ctx->log_mtu ? 1 : 0; + case UTP_LOG_DEBUG: return ctx->log_debug ? 1 : 0; + case UTP_TARGET_DELAY: return ctx->target_delay; + case UTP_SNDBUF: return ctx->opt_sndbuf; + case UTP_RCVBUF: return ctx->opt_rcvbuf; + } + return -1; +} + + +int utp_setsockopt(UTPSocket* conn, int opt, int val) +{ + assert(conn); + if (!conn) return -1; + + switch (opt) { + + case UTP_SNDBUF: + assert(val >= 1); + conn->opt_sndbuf = val; + return 0; + + case UTP_RCVBUF: + assert(val >= 1); + conn->opt_rcvbuf = val; + return 0; + + case UTP_TARGET_DELAY: + conn->target_delay = val; + return 0; + } + + return -1; +} + +int utp_getsockopt(UTPSocket* conn, int opt) +{ + assert(conn); + if (!conn) return -1; + + switch (opt) { + case UTP_SNDBUF: return conn->opt_sndbuf; + case UTP_RCVBUF: return conn->opt_rcvbuf; + case UTP_TARGET_DELAY: return conn->target_delay; + } + + return -1; +} + +// Try to connect to a specified host. +int utp_connect(utp_socket *conn, const struct sockaddr *to, socklen_t tolen) +{ + assert(conn); + if (!conn) return -1; + + assert(conn->state == CS_UNINITIALIZED); + if (conn->state != CS_UNINITIALIZED) { + conn->state = CS_DESTROY; + return -1; + } + + utp_initialize_socket(conn, to, tolen, true, 0, 0, 1); + + assert(conn->cur_window_packets == 0); + assert(conn->outbuf.get(conn->seq_nr) == NULL); + assert(sizeof(PacketFormatV1) == 20); + + conn->state = CS_SYN_SENT; + conn->ctx->current_ms = utp_call_get_milliseconds(conn->ctx, conn); + + // Create and send a connect message + + // used in parse_log.py + conn->log(UTP_LOG_NORMAL, "UTP_Connect conn_seed:%u packet_size:%u (B) " + "target_delay:%u (ms) delay_history:%u " + "delay_base_history:%u (minutes)", + conn->conn_seed, PACKET_SIZE, conn->target_delay / 1000, + CUR_DELAY_SIZE, DELAY_BASE_HISTORY); + + // Setup initial timeout timer. + conn->retransmit_timeout = 3000; + conn->rto_timeout = conn->ctx->current_ms + conn->retransmit_timeout; + conn->last_rcv_win = conn->get_rcv_window(); + + // if you need compatibiltiy with 1.8.1, use this. it increases attackability though. + //conn->seq_nr = 1; + conn->seq_nr = utp_call_get_random(conn->ctx, conn); + + // Create the connect packet. + const size_t header_size = sizeof(PacketFormatV1); + + OutgoingPacket *pkt = (OutgoingPacket*)malloc(sizeof(OutgoingPacket) - 1 + header_size); + PacketFormatV1* p1 = (PacketFormatV1*)pkt->data; + + memset(p1, 0, header_size); + // SYN packets are special, and have the receive ID in the connid field, + // instead of conn_id_send. + p1->set_version(1); + p1->set_type(ST_SYN); + p1->ext = 0; + p1->connid = conn->conn_id_recv; + p1->windowsize = (uint32)conn->last_rcv_win; + p1->seq_nr = conn->seq_nr; + pkt->transmissions = 0; + pkt->length = header_size; + pkt->payload = 0; + + /* + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Sending connect %s [%u].", + addrfmt(conn->addr, addrbuf), conn_seed); + #endif + */ + + // Remember the message in the outgoing queue. + conn->outbuf.ensure_size(conn->seq_nr, conn->cur_window_packets); + conn->outbuf.put(conn->seq_nr, pkt); + conn->seq_nr++; + conn->cur_window_packets++; + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "incrementing cur_window_packets:%u", conn->cur_window_packets); + #endif + + conn->send_packet(pkt); + return 0; +} + +// Returns 1 if the UDP payload was recognized as a UTP packet, or 0 if it was not +int utp_process_udp(utp_context *ctx, const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen) +{ + assert(ctx); + if (!ctx) return 0; + + assert(buffer); + if (!buffer) return 0; + + assert(to); + if (!to) return 0; + + const PackedSockAddr addr((const SOCKADDR_STORAGE*)to, tolen); + + if (len < sizeof(PacketFormatV1)) { + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv %s len:%u too small", addrfmt(addr, addrbuf), (uint)len); + #endif + return 0; + } + + const PacketFormatV1 *pf1 = (PacketFormatV1*)buffer; + const byte version = UTP_Version(pf1); + const uint32 id = uint32(pf1->connid); + + if (version != 1) { + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv %s len:%u version:%u unsupported version", addrfmt(addr, addrbuf), (uint)len, version); + #endif + + return 0; + } + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv %s len:%u id:%u", addrfmt(addr, addrbuf), (uint)len, id); + ctx->log(UTP_LOG_DEBUG, NULL, "recv id:%u seq_nr:%u ack_nr:%u", id, (uint)pf1->seq_nr, (uint)pf1->ack_nr); + #endif + + const byte flags = pf1->type(); + + if (flags == ST_RESET) { + // id is either our recv id or our send id + // if it's our send id, and we initiated the connection, our recv id is id + 1 + // if it's our send id, and we did not initiate the connection, our recv id is id - 1 + // we have to check every case + + UTPSocketKeyData* keyData; + if ( (keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id))) || + ((keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id + 1))) && keyData->socket->conn_id_send == id) || + ((keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id - 1))) && keyData->socket->conn_id_send == id)) + { + UTPSocket* conn = keyData->socket; + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv RST for existing connection"); + #endif + + if (conn->close_requested) + conn->state = CS_DESTROY; + else + conn->state = CS_RESET; + + utp_call_on_overhead_statistics(conn->ctx, conn, false, len + conn->get_udp_overhead(), close_overhead); + const int err = (conn->state == CS_SYN_SENT) ? UTP_ECONNREFUSED : UTP_ECONNRESET; + utp_call_on_error(conn->ctx, conn, err); + } + else { + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv RST for unknown connection"); + #endif + } + return 1; + } + else if (flags != ST_SYN) { + UTPSocket* conn = NULL; + + if (ctx->last_utp_socket && ctx->last_utp_socket->addr == addr && ctx->last_utp_socket->conn_id_recv == id) { + conn = ctx->last_utp_socket; + } else { + UTPSocketKeyData* keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id)); + if (keyData) { + conn = keyData->socket; + ctx->last_utp_socket = conn; + } + } + + if (conn) { + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv processing"); + #endif + + const size_t read = utp_process_incoming(conn, buffer, len); + utp_call_on_overhead_statistics(conn->ctx, conn, false, (len - read) + conn->get_udp_overhead(), header_overhead); + return 1; + } + } + + // We have not found a matching utp_socket, and this isn't a SYN. Reject it. + const uint32 seq_nr = pf1->seq_nr; + if (flags != ST_SYN) { + ctx->current_ms = utp_call_get_milliseconds(ctx, NULL); + + for (size_t i = 0; i < ctx->rst_info.GetCount(); i++) { + if ((ctx->rst_info[i].connid == id) && + (ctx->rst_info[i].addr == addr) && + (ctx->rst_info[i].ack_nr == seq_nr)) + { + ctx->rst_info[i].timestamp = ctx->current_ms; + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv not sending RST to non-SYN (stored)"); + #endif + + return 1; + } + } + + if (ctx->rst_info.GetCount() > RST_INFO_LIMIT) { + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv not sending RST to non-SYN (limit at %u stored)", (uint)ctx->rst_info.GetCount()); + #endif + + return 1; + } + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv send RST to non-SYN (%u stored)", (uint)ctx->rst_info.GetCount()); + #endif + + RST_Info &r = ctx->rst_info.Append(); + r.addr = addr; + r.connid = id; + r.ack_nr = seq_nr; + r.timestamp = ctx->current_ms; + + UTPSocket::send_rst(ctx, addr, id, seq_nr, utp_call_get_random(ctx, NULL)); + return 1; + } + + if (ctx->callbacks[UTP_ON_ACCEPT]) { + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "Incoming connection from %s", addrfmt(addr, addrbuf)); + #endif + + UTPSocketKeyData* keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id + 1)); + if (keyData) { + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "rejected incoming connection, connection already exists"); + #endif + + return 1; + } + + if (ctx->utp_sockets->GetCount() > 3000) { + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "rejected incoming connection, too many uTP sockets %d", ctx->utp_sockets->GetCount()); + #endif + + return 1; + } + // true means yes, block connection. false means no, don't block. + if (utp_call_on_firewall(ctx, to, tolen)) { + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "rejected incoming connection, firewall callback returned true"); + #endif + + return 1; + } + + // Create a new UTP socket to handle this new connection + UTPSocket *conn = utp_create_socket(ctx); + utp_initialize_socket(conn, to, tolen, false, id, id+1, id); + conn->ack_nr = seq_nr; + conn->seq_nr = utp_call_get_random(ctx, NULL); + conn->fast_resend_seq_nr = conn->seq_nr; + conn->state = CS_SYN_RECV; + + const size_t read = utp_process_incoming(conn, buffer, len, true); + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "recv send connect ACK"); + #endif + + conn->send_ack(true); + + utp_call_on_accept(ctx, conn, to, tolen); + + // we report overhead after on_accept(), because the callbacks are setup now + utp_call_on_overhead_statistics(conn->ctx, conn, false, (len - read) + conn->get_udp_overhead(), header_overhead); // SYN + utp_call_on_overhead_statistics(conn->ctx, conn, true, conn->get_overhead(), ack_overhead); // SYNACK + } + else { + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "rejected incoming connection, UTP_ON_ACCEPT callback not set"); + #endif + + } + + return 1; +} + +// Called by utp_process_icmp_fragmentation() and utp_process_icmp_error() below +static UTPSocket* parse_icmp_payload(utp_context *ctx, const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen) +{ + assert(ctx); + if (!ctx) return NULL; + + assert(buffer); + if (!buffer) return NULL; + + assert(to); + if (!to) return NULL; + + const PackedSockAddr addr((const SOCKADDR_STORAGE*)to, tolen); + + // ICMP packets are only required to quote the first 8 bytes of the layer4 + // payload. The UDP payload is 8 bytes, and the UTP header is another 20 + // bytes. So, in order to find the entire UTP header, we need the ICMP + // packet to quote 28 bytes. + if (len < sizeof(PacketFormatV1)) { + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "Ignoring ICMP from %s: runt length %d", addrfmt(addr, addrbuf), len); + #endif + return NULL; + } + + const PacketFormatV1 *pf = (PacketFormatV1*)buffer; + const byte version = UTP_Version(pf); + const uint32 id = uint32(pf->connid); + + if (version != 1) { + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "Ignoring ICMP from %s: not UTP version 1", addrfmt(addr, addrbuf)); + #endif + return NULL; + } + + UTPSocketKeyData* keyData; + + if ( (keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id))) || + ((keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id + 1))) && keyData->socket->conn_id_send == id) || + ((keyData = ctx->utp_sockets->Lookup(UTPSocketKey(addr, id - 1))) && keyData->socket->conn_id_send == id)) + { + return keyData->socket; + } + + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "Ignoring ICMP from %s: No matching connection found for id %u", addrfmt(addr, addrbuf), id); + #endif + return NULL; +} + +// Should be called when an ICMP Type 3, Code 4 packet (fragmentation needed) is received, to adjust the MTU +// +// Returns 1 if the UDP payload (delivered in the ICMP packet) was recognized as a UTP packet, or 0 if it was not +// +// @ctx: utp_context +// @buf: Contents of the original UDP payload, which the ICMP packet quoted. *Not* the ICMP packet itself. +// @len: buffer length +// @to: destination address of the original UDP pakcet +// @tolen: address length +// @next_hop_mtu: +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) +{ + UTPSocket* conn = parse_icmp_payload(ctx, buffer, len, to, tolen); + if (!conn) return 0; + + // Constrain the next_hop_mtu to sane values. It might not be initialized or sent properly + if (next_hop_mtu >= 576 && next_hop_mtu < 0x2000) { + conn->mtu_ceiling = min(next_hop_mtu, conn->mtu_ceiling); + conn->mtu_search_update(); + // this is something of a speecial case, where we don't set mtu_last + // to the value in between the floor and the ceiling. We can update the + // floor, because there might be more network segments after the one + // that sent this ICMP with smaller MTUs. But we want to test this + // MTU size first. If the next probe gets through, mtu_floor is updated + conn->mtu_last = conn->mtu_ceiling; + } else { + // Otherwise, binary search. At this point we don't actually know + // what size the packet that failed was, and apparently we can't + // trust the next hop mtu either. It seems reasonably conservative + // to just lower the ceiling. This should not happen on working networks + // anyway. + conn->mtu_ceiling = (conn->mtu_floor + conn->mtu_ceiling) / 2; + conn->mtu_search_update(); + } + + conn->log(UTP_LOG_MTU, "MTU [ICMP] floor:%d ceiling:%d current:%d", conn->mtu_floor, conn->mtu_ceiling, conn->mtu_last); + return 1; +} + +// Should be called when an ICMP message is received that should tear down the connection. +// +// Returns 1 if the UDP payload (delivered in the ICMP packet) was recognized as a UTP packet, or 0 if it was not +// +// @ctx: utp_context +// @buf: Contents of the original UDP payload, which the ICMP packet quoted. *Not* the ICMP packet itself. +// @len: buffer length +// @to: destination address of the original UDP pakcet +// @tolen: address length +int utp_process_icmp_error(utp_context *ctx, const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen) +{ + UTPSocket* conn = parse_icmp_payload(ctx, buffer, len, to, tolen); + if (!conn) return 0; + + const int err = (conn->state == CS_SYN_SENT) ? UTP_ECONNREFUSED : UTP_ECONNRESET; + const PackedSockAddr addr((const SOCKADDR_STORAGE*)to, tolen); + + switch(conn->state) { + // Don't pass on errors for idle/closed connections + case CS_IDLE: + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "ICMP from %s in state CS_IDLE, ignoring", addrfmt(addr, addrbuf)); + #endif + return 1; + + default: + if (conn->close_requested) { + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "ICMP from %s after close, setting state to CS_DESTROY and causing error %d", addrfmt(addr, addrbuf), err); + #endif + conn->state = CS_DESTROY; + } else { + #if UTP_DEBUG_LOGGING + ctx->log(UTP_LOG_DEBUG, NULL, "ICMP from %s, setting state to CS_RESET and causing error %d", addrfmt(addr, addrbuf), err); + #endif + conn->state = CS_RESET; + } + break; + } + + utp_call_on_error(conn->ctx, conn, err); + return 1; +} + +// Write bytes to the UTP socket. Returns the number of bytes written. +// 0 indicates the socket is no longer writable, -1 indicates an error +ssize_t utp_writev(utp_socket *conn, struct utp_iovec *iovec_input, size_t num_iovecs) +{ + static utp_iovec iovec[UTP_IOV_MAX]; + + assert(conn); + if (!conn) return -1; + + assert(iovec_input); + if (!iovec_input) return -1; + + assert(num_iovecs); + if (!num_iovecs) return -1; + + if (num_iovecs > UTP_IOV_MAX) + num_iovecs = UTP_IOV_MAX; + + memcpy(iovec, iovec_input, sizeof(struct utp_iovec)*num_iovecs); + + size_t bytes = 0; + size_t sent = 0; + for (size_t i = 0; i < num_iovecs; i++) + bytes += iovec[i].iov_len; + + #if UTP_DEBUG_LOGGING + size_t param = bytes; + #endif + + if (conn->state != CS_CONNECTED) { + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP_Write %u bytes = false (not CS_CONNECTED)", (uint)bytes); + #endif + return 0; + } + + if (conn->fin_sent) { + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP_Write %u bytes = false (fin_sent already)", (uint)bytes); + #endif + return 0; + } + + conn->ctx->current_ms = utp_call_get_milliseconds(conn->ctx, conn); + + // don't send unless it will all fit in the window + size_t packet_size = conn->get_packet_size(); + size_t num_to_send = min(bytes, packet_size); + while (!conn->is_full(num_to_send)) { + // Send an outgoing packet. + // Also add it to the outgoing of packets that have been sent but not ACKed. + + bytes -= num_to_send; + sent += num_to_send; + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Sending packet. seq_nr:%u ack_nr:%u wnd:%u/%u/%u rcv_win:%u size:%u cur_window_packets:%u", + conn->seq_nr, conn->ack_nr, + (uint)(conn->cur_window + num_to_send), + (uint)conn->max_window, (uint)conn->max_window_user, + (uint)conn->last_rcv_win, num_to_send, + conn->cur_window_packets); + #endif + conn->write_outgoing_packet(num_to_send, ST_DATA, iovec, num_iovecs); + num_to_send = min(bytes, packet_size); + + if (num_to_send == 0) { + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP_Write %u bytes = true", (uint)param); + #endif + return sent; + } + } + + bool full = conn->is_full(); + if (full) { + // mark the socket as not being writable. + conn->state = CS_CONNECTED_FULL; + } + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP_Write %u bytes = %s", (uint)bytes, full ? "false" : "true"); + #endif + + // returns whether or not the socket is still writable + // if the congestion window is not full, we can still write to it + //return !full; + return sent; +} + +void utp_read_drained(utp_socket *conn) +{ + assert(conn); + if (!conn) return; + + assert(conn->state != CS_UNINITIALIZED); + if (conn->state == CS_UNINITIALIZED) return; + + const size_t rcvwin = conn->get_rcv_window(); + + if (rcvwin > conn->last_rcv_win) { + // If last window was 0 send ACK immediately, otherwise should set timer + if (conn->last_rcv_win == 0) { + conn->send_ack(); + } else { + conn->ctx->current_ms = utp_call_get_milliseconds(conn->ctx, conn); + conn->schedule_ack(); + } + } +} + +// Should be called each time the UDP socket is drained +void utp_issue_deferred_acks(utp_context *ctx) +{ + assert(ctx); + if (!ctx) return; + + for (size_t i = 0; i < ctx->ack_sockets.GetCount(); i++) { + UTPSocket *conn = ctx->ack_sockets[i]; + conn->send_ack(); + i--; + } +} + +// Should be called every 500ms +void utp_check_timeouts(utp_context *ctx) +{ + assert(ctx); + if (!ctx) return; + + ctx->current_ms = utp_call_get_milliseconds(ctx, NULL); + + if (ctx->current_ms - ctx->last_check < TIMEOUT_CHECK_INTERVAL) + return; + + ctx->last_check = ctx->current_ms; + + for (size_t i = 0; i < ctx->rst_info.GetCount(); i++) { + if ((int)(ctx->current_ms - ctx->rst_info[i].timestamp) >= RST_INFO_TIMEOUT) { + ctx->rst_info.MoveUpLast(i); + i--; + } + } + if (ctx->rst_info.GetCount() != ctx->rst_info.GetAlloc()) { + ctx->rst_info.Compact(); + } + + utp_hash_iterator_t it; + UTPSocketKeyData* keyData; + while ((keyData = ctx->utp_sockets->Iterate(it))) { + UTPSocket *conn = keyData->socket; + conn->check_timeouts(); + + // Check if the object was deleted + if (conn->state == CS_DESTROY) { + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "Destroying"); + #endif + delete conn; + } + } +} + +int utp_getpeername(utp_socket *conn, struct sockaddr *addr, socklen_t *addrlen) +{ + assert(addr); + if (!addr) return -1; + + assert(addrlen); + if (!addrlen) return -1; + + assert(conn); + if (!conn) return -1; + + assert(conn->state != CS_UNINITIALIZED); + if (conn->state == CS_UNINITIALIZED) return -1; + + socklen_t len; + const SOCKADDR_STORAGE sa = conn->addr.get_sockaddr_storage(&len); + *addrlen = min(len, *addrlen); + memcpy(addr, &sa, *addrlen); + return 0; +} + +int utp_get_delays(UTPSocket *conn, uint32 *ours, uint32 *theirs, uint32 *age) +{ + assert(conn); + if (!conn) return -1; + + assert(conn->state != CS_UNINITIALIZED); + if (conn->state == CS_UNINITIALIZED) { + if (ours) *ours = 0; + if (theirs) *theirs = 0; + if (age) *age = 0; + return -1; + } + + if (ours) *ours = conn->our_hist.get_value(); + if (theirs) *theirs = conn->their_hist.get_value(); + if (age) *age = (uint32)(conn->ctx->current_ms - conn->last_measured_delay); + return 0; +} + +// Close the UTP socket. +// It is not valid for the upper layer to refer to socket after it is closed. +// Data will keep to try being delivered after the close. +void utp_close(UTPSocket *conn) +{ + assert(conn); + if (!conn) return; + + assert(conn->state != CS_UNINITIALIZED + && conn->state != CS_DESTROY); + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP_Close in state:%s", statenames[conn->state]); + #endif + + switch(conn->state) { + case CS_CONNECTED: + case CS_CONNECTED_FULL: + conn->read_shutdown = true; + conn->close_requested = true; + if (!conn->fin_sent) { + conn->fin_sent = true; + conn->write_outgoing_packet(0, ST_FIN, NULL, 0); + } else if (conn->fin_sent_acked) { + conn->state = CS_DESTROY; + } + break; + + case CS_SYN_SENT: + conn->rto_timeout = utp_call_get_milliseconds(conn->ctx, conn) + min(conn->rto * 2, 60); + // fall through + case CS_SYN_RECV: + // fall through + default: + conn->state = CS_DESTROY; + break; + } + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP_Close end in state:%s", statenames[conn->state]); + #endif +} + +void utp_shutdown(UTPSocket *conn, int how) +{ + assert(conn); + if (!conn) return; + + assert(conn->state != CS_UNINITIALIZED + && conn->state != CS_DESTROY); + + #if UTP_DEBUG_LOGGING + conn->log(UTP_LOG_DEBUG, "UTP_shutdown(%d) in state:%s", how, statenames[conn->state]); + #endif + + if (how != SHUT_WR) { + conn->read_shutdown = true; + } + if (how != SHUT_RD) { + switch(conn->state) { + case CS_CONNECTED: + case CS_CONNECTED_FULL: + if (!conn->fin_sent) { + conn->fin_sent = true; + conn->write_outgoing_packet(0, ST_FIN, NULL, 0); + } + break; + case CS_SYN_SENT: + conn->rto_timeout = utp_call_get_milliseconds(conn->ctx, conn) + min(conn->rto * 2, 60); + default: + break; + } + } +} + +utp_context* utp_get_context(utp_socket *socket) { + assert(socket); + return socket ? socket->ctx : NULL; +} + +void* utp_set_userdata(utp_socket *socket, void *userdata) { + assert(socket); + if (socket) socket->userdata = userdata; + return socket ? socket->userdata : NULL; +} + +void* utp_get_userdata(utp_socket *socket) { + assert(socket); + return socket ? socket->userdata : NULL; +} + +void struct_utp_context::log(int level, utp_socket *socket, char const *fmt, ...) +{ + if (!would_log(level)) { + return; + } + + va_list va; + va_start(va, fmt); + log_unchecked(socket, fmt, va); + va_end(va); +} + +void struct_utp_context::log_unchecked(utp_socket *socket, char const *fmt, ...) +{ + va_list va; + char buf[4096]; + + va_start(va, fmt); + vsnprintf(buf, 4096, fmt, va); + buf[4095] = '\0'; + va_end(va); + + utp_call_log(this, socket, (const byte *)buf); +} + +inline bool struct_utp_context::would_log(int level) +{ + if (level == UTP_LOG_NORMAL) return log_normal; + if (level == UTP_LOG_MTU) return log_mtu; + if (level == UTP_LOG_DEBUG) return log_debug; + return true; +} + +utp_socket_stats* utp_get_stats(utp_socket *socket) +{ + #ifdef _DEBUG + assert(socket); + if (!socket) return NULL; + socket->_stats.mtu_guess = socket->mtu_last ? socket->mtu_last : socket->mtu_ceiling; + return &socket->_stats; + #else + return NULL; + #endif +} diff --git a/utp-client-interop/libutp (copy)/utp_internal.h b/utp-client-interop/libutp (copy)/utp_internal.h new file mode 100644 index 0000000..8c22254 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_internal.h @@ -0,0 +1,140 @@ +/* + * 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_INTERNAL_H__ +#define __UTP_INTERNAL_H__ + +#include +#include +#include +#include + +#include "utp.h" +#include "utp_callbacks.h" +#include "utp_templates.h" +#include "utp_hash.h" +#include "utp_hash.h" +#include "utp_packedsockaddr.h" + +/* These originally lived in utp_config.h */ +#define CCONTROL_TARGET (100 * 1000) // us + +enum bandwidth_type_t { + payload_bandwidth, connect_overhead, + close_overhead, ack_overhead, + header_overhead, retransmit_overhead +}; + +#ifdef WIN32 + #ifdef _MSC_VER + #include "libutp_inet_ntop.h" + #endif + + // newer versions of MSVC define these in errno.h + #ifndef ECONNRESET + #define ECONNRESET WSAECONNRESET + #define EMSGSIZE WSAEMSGSIZE + #define ECONNREFUSED WSAECONNREFUSED + #define ETIMEDOUT WSAETIMEDOUT + #endif +#endif + +struct PACKED_ATTRIBUTE RST_Info { + PackedSockAddr addr; + uint32 connid; + uint16 ack_nr; + uint64 timestamp; +}; + +// It's really important that we don't have duplicate keys in the hash table. +// If we do, we'll eventually crash. if we try to remove the second instance +// of the key, we'll accidentally remove the first instead. then later, +// checkTimeouts will try to access the second one's already freed memory. +void UTP_FreeAll(struct UTPSocketHT *utp_sockets); + +struct UTPSocketKey { + PackedSockAddr addr; + uint32 recv_id; // "conn_seed", "conn_id" + + UTPSocketKey(const PackedSockAddr& _addr, uint32 _recv_id) { + memset(this, 0, sizeof(*this)); + addr = _addr; + recv_id = _recv_id; + } + + bool operator == (const UTPSocketKey &other) const { + return recv_id == other.recv_id && addr == other.addr; + } + + uint32 compute_hash() const { + return recv_id ^ addr.compute_hash(); + } +}; + +struct UTPSocketKeyData { + UTPSocketKey key; + UTPSocket *socket; + utp_link_t link; +}; + +#define UTP_SOCKET_BUCKETS 79 +#define UTP_SOCKET_INIT 15 + +struct UTPSocketHT : utpHashTable { + UTPSocketHT() { + const int buckets = UTP_SOCKET_BUCKETS; + const int initial = UTP_SOCKET_INIT; + this->Create(buckets, initial); + } + ~UTPSocketHT() { + UTP_FreeAll(this); + this->Free(); + } +}; + +struct struct_utp_context { + void *userdata; + utp_callback_t* callbacks[UTP_ARRAY_SIZE]; + + uint64 current_ms; + utp_context_stats context_stats; + UTPSocket *last_utp_socket; + Array ack_sockets; + Array rst_info; + UTPSocketHT *utp_sockets; + size_t target_delay; + size_t opt_sndbuf; + size_t opt_rcvbuf; + uint64 last_check; + + struct_utp_context(); + ~struct_utp_context(); + + void log(int level, utp_socket *socket, char const *fmt, ...); + void log_unchecked(utp_socket *socket, char const *fmt, ...); + bool would_log(int level); + + bool log_normal:1; // log normal events? + bool log_mtu:1; // log MTU related events? + bool log_debug:1; // log debugging events? (Must also compile with UTP_DEBUG_LOGGING defined) +}; +#endif //__UTP_INTERNAL_H__ diff --git a/utp-client-interop/libutp (copy)/utp_packedsockaddr.cpp b/utp-client-interop/libutp (copy)/utp_packedsockaddr.cpp new file mode 100644 index 0000000..ab65ae5 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_packedsockaddr.cpp @@ -0,0 +1,139 @@ +// vim:set ts=4 sw=4 ai: + +/* + * 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. + */ + +#include +#include +#include + +#include "utp_types.h" +#include "utp_hash.h" +#include "utp_packedsockaddr.h" + +#include "libutp_inet_ntop.h" + +byte PackedSockAddr::get_family() const +{ + #if defined(__sh__) + return ((_sin6d[0] == 0) && (_sin6d[1] == 0) && (_sin6d[2] == htonl(0xffff)) != 0) ? + AF_INET : AF_INET6; + #else + return (IN6_IS_ADDR_V4MAPPED(&_in._in6addr) != 0) ? AF_INET : AF_INET6; + #endif // defined(__sh__) +} + +bool PackedSockAddr::operator==(const PackedSockAddr& rhs) const +{ + if (&rhs == this) + return true; + if (_port != rhs._port) + return false; + return memcmp(_sin6, rhs._sin6, sizeof(_sin6)) == 0; +} + +bool PackedSockAddr::operator!=(const PackedSockAddr& rhs) const +{ + return !(*this == rhs); +} + +uint32 PackedSockAddr::compute_hash() const { + return utp_hash_mem(&_in, sizeof(_in)) ^ _port; +} + +void PackedSockAddr::set(const SOCKADDR_STORAGE* sa, socklen_t len) +{ + if (sa->ss_family == AF_INET) { + assert(len >= sizeof(sockaddr_in)); + const sockaddr_in *sin = (sockaddr_in*)sa; + _sin6w[0] = 0; + _sin6w[1] = 0; + _sin6w[2] = 0; + _sin6w[3] = 0; + _sin6w[4] = 0; + _sin6w[5] = 0xffff; + _sin4 = sin->sin_addr.s_addr; + _port = ntohs(sin->sin_port); + } else { + assert(len >= sizeof(sockaddr_in6)); + const sockaddr_in6 *sin6 = (sockaddr_in6*)sa; + _in._in6addr = sin6->sin6_addr; + _port = ntohs(sin6->sin6_port); + } +} + +PackedSockAddr::PackedSockAddr(const SOCKADDR_STORAGE* sa, socklen_t len) +{ + set(sa, len); +} + +PackedSockAddr::PackedSockAddr(void) +{ + SOCKADDR_STORAGE sa; + socklen_t len = sizeof(SOCKADDR_STORAGE); + memset(&sa, 0, len); + sa.ss_family = AF_INET; + set(&sa, len); +} + +SOCKADDR_STORAGE PackedSockAddr::get_sockaddr_storage(socklen_t *len = NULL) const +{ + SOCKADDR_STORAGE sa; + const byte family = get_family(); + if (family == AF_INET) { + sockaddr_in *sin = (sockaddr_in*)&sa; + if (len) *len = sizeof(sockaddr_in); + memset(sin, 0, sizeof(sockaddr_in)); + sin->sin_family = family; + sin->sin_port = htons(_port); + sin->sin_addr.s_addr = _sin4; + } else { + sockaddr_in6 *sin6 = (sockaddr_in6*)&sa; + memset(sin6, 0, sizeof(sockaddr_in6)); + if (len) *len = sizeof(sockaddr_in6); + sin6->sin6_family = family; + sin6->sin6_addr = _in._in6addr; + sin6->sin6_port = htons(_port); + } + return sa; +} + +// #define addrfmt(x, s) x.fmt(s, sizeof(s)) +cstr PackedSockAddr::fmt(str s, size_t len) const +{ + memset(s, 0, len); + const byte family = get_family(); + str i; + if (family == AF_INET) { + INET_NTOP(family, (uint32*)&_sin4, s, len); + i = s; + while (*++i) {} + } else { + i = s; + *i++ = '['; + INET_NTOP(family, (in6_addr*)&_in._in6addr, i, len-1); + while (*++i) {} + *i++ = ']'; + } + snprintf(i, len - (i-s), ":%u", _port); + return s; +} diff --git a/utp-client-interop/libutp (copy)/utp_packedsockaddr.h b/utp-client-interop/libutp (copy)/utp_packedsockaddr.h new file mode 100644 index 0000000..76e8acc --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_packedsockaddr.h @@ -0,0 +1,60 @@ +/* + * 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_PACKEDSOCKADDR_H__ +#define __UTP_PACKEDSOCKADDR_H__ + +#include "utp_types.h" + +struct PACKED_ATTRIBUTE PackedSockAddr { + // The values are always stored here in network byte order + union { + byte _in6[16]; // IPv6 + uint16 _in6w[8]; // IPv6, word based (for convenience) + uint32 _in6d[4]; // Dword access + in6_addr _in6addr; // For convenience + } _in; + + // Host byte order + uint16 _port; + + #define _sin4 _in._in6d[3] // IPv4 is stored where it goes if mapped + + #define _sin6 _in._in6 + #define _sin6w _in._in6w + #define _sin6d _in._in6d + + byte get_family() const; + bool operator==(const PackedSockAddr& rhs) const; + bool operator!=(const PackedSockAddr& rhs) const; + void set(const SOCKADDR_STORAGE* sa, socklen_t len); + + PackedSockAddr(const SOCKADDR_STORAGE* sa, socklen_t len); + PackedSockAddr(void); + + SOCKADDR_STORAGE get_sockaddr_storage(socklen_t *len) const; + cstr fmt(str s, size_t len) const; + + uint32 compute_hash() const; +} ALIGNED_ATTRIBUTE(4); + +#endif //__UTP_PACKEDSOCKADDR_H__ diff --git a/utp-client-interop/libutp (copy)/utp_templates.h b/utp-client-interop/libutp (copy)/utp_templates.h new file mode 100644 index 0000000..8f88f5c --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_templates.h @@ -0,0 +1,195 @@ +/* + * 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 __TEMPLATES_H__ +#define __TEMPLATES_H__ + +#include "utp_types.h" +#include + +#if defined(POSIX) +/* Allow over-writing FORCEINLINE from makefile because gcc 3.4.4 for buffalo + doesn't seem to support __attribute__((always_inline)) in -O0 build + (strangely, it works in -Os build) */ +#ifndef FORCEINLINE +// The always_inline attribute asks gcc to inline the function even if no optimization is being requested. +// This macro should be used exclusive-or with the inline directive (use one or the other but not both) +// since Microsoft uses __forceinline to also mean inline, +// and this code is following a Microsoft compatibility model. +// Just setting the attribute without also specifying the inline directive apparently won't inline the function, +// as evidenced by multiply-defined symbols found at link time. +#define FORCEINLINE inline __attribute__((always_inline)) +#endif +#endif + +// Utility templates +#undef min +#undef max + +template static inline T min(T a, T b) { if (a < b) return a; return b; } +template static inline T max(T a, T b) { if (a > b) return a; return b; } + +template static inline T min(T a, T b, T c) { return min(min(a,b),c); } +template static inline T max(T a, T b, T c) { return max(max(a,b),c); } +template static inline T clamp(T v, T mi, T ma) +{ + if (v > ma) v = ma; + if (v < mi) v = mi; + return v; +} + +#if (defined(__SVR4) && defined(__sun)) + #pragma pack(1) +#else + #pragma pack(push,1) +#endif + + +namespace aux +{ + FORCEINLINE uint16 host_to_network(uint16 i) { return htons(i); } + FORCEINLINE uint32 host_to_network(uint32 i) { return htonl(i); } + FORCEINLINE int32 host_to_network(int32 i) { return htonl(i); } + FORCEINLINE uint16 network_to_host(uint16 i) { return ntohs(i); } + FORCEINLINE uint32 network_to_host(uint32 i) { return ntohl(i); } + FORCEINLINE int32 network_to_host(int32 i) { return ntohl(i); } +} + +template +struct PACKED_ATTRIBUTE big_endian +{ + T operator=(T i) { m_integer = aux::host_to_network(i); return i; } + operator T() const { return aux::network_to_host(m_integer); } +private: + T m_integer; +}; + +typedef big_endian int32_big; +typedef big_endian uint32_big; +typedef big_endian uint16_big; + +#if (defined(__SVR4) && defined(__sun)) + #pragma pack(0) +#else + #pragma pack(pop) +#endif + +template static inline void zeromem(T *a, size_t count = 1) { memset(a, 0, count * sizeof(T)); } + +typedef int SortCompareProc(const void *, const void *); + +template static FORCEINLINE void QuickSortT(T *base, size_t num, int (*comp)(const T *, const T *)) { qsort(base, num, sizeof(T), (SortCompareProc*)comp); } + + +// WARNING: The template parameter MUST be a POD type! +template class Array { +protected: + T *mem; + size_t alloc,count; + +public: + Array(size_t init) { Init(init); } + Array() { Init(); } + ~Array() { Free(); } + + void inline Init() { mem = NULL; alloc = count = 0; } + void inline Init(size_t init) { Init(); if (init) Resize(init); } + size_t inline GetCount() const { return count; } + size_t inline GetAlloc() const { return alloc; } + void inline SetCount(size_t c) { count = c; } + + inline T& operator[](size_t offset) { assert(offset ==0 || offset(minsize, alloc * 2)); } + + inline size_t Append(const T &t) { + if (count >= alloc) Grow(); + size_t r=count++; + mem[r] = t; + return r; + } + + T inline &Append() { + if (count >= alloc) Grow(); + return mem[count++]; + } + + void inline Compact() { + Resize(count); + } + + void inline Free() { + free(mem); + Init(); + } + + void inline Clear() { + count = 0; + } + + bool inline MoveUpLast(size_t index) { + assert(index < count); + size_t c = --count; + if (index != c) { + mem[index] = mem[c]; + return true; + } + return false; + } + + bool inline MoveUpLastExist(const T &v) { + return MoveUpLast(LookupElementExist(v)); + } + + size_t inline LookupElement(const T &v) const { + for(size_t i = 0; i != count; i++) + if (mem[i] == v) + return i; + return (size_t) -1; + } + + bool inline HasElement(const T &v) const { + return LookupElement(v) != -1; + } + + typedef int SortCompareProc(const T *a, const T *b); + + void Sort(SortCompareProc* proc, size_t start, size_t end) { + QuickSortT(&mem[start], end - start, proc); + } + + void Sort(SortCompareProc* proc, size_t start) { + Sort(proc, start, count); + } + + void Sort(SortCompareProc* proc) { + Sort(proc, 0, count); + } +}; + +#endif //__TEMPLATES_H__ diff --git a/utp-client-interop/libutp (copy)/utp_types.h b/utp-client-interop/libutp (copy)/utp_types.h new file mode 100644 index 0000000..9799ad1 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_types.h @@ -0,0 +1,123 @@ +/* + * 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__ + +// 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 + #define _CRT_SECURE_NO_DEPRECATE + #define WIN32_LEAN_AND_MEAN + #include + #include + #include + #define IP_OPT_DONTFRAG IP_DONTFRAGMENT + #define SHUT_RD SD_RECEIVE + #define SHUT_WR SD_SEND + #define SHUT_RDWR SD_BOTH +#else + #include + #include + #include + #include + + #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 + 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; + +#ifdef _MSC_VER +typedef unsigned __int64 uint64; +typedef signed __int64 int64; +#else +typedef unsigned long long uint64; +typedef long long int64; +#endif + +/* 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) + +#ifndef INT64_MAX +#define INT64_MAX 0x7fffffffffffffffLL +#endif + +// always ANSI +typedef const char * cstr; +typedef char * str; + +#ifndef __cplusplus +typedef uint8 bool; +#endif + +#endif //__UTP_TYPES_H__ diff --git a/utp-client-interop/libutp (copy)/utp_utils.cpp b/utp-client-interop/libutp (copy)/utp_utils.cpp new file mode 100644 index 0000000..f2c57ab --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_utils.cpp @@ -0,0 +1,254 @@ +/* + * 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. + */ + +#include +#include +#include "utp.h" +#include "utp_types.h" + +#ifdef WIN32 + #define WIN32_LEAN_AND_MEAN + #include + #include + #include +#else //!WIN32 + #include + #include // Linux needs both time.h and sys/time.h +#endif + +#if defined(__APPLE__) + #include +#endif + +#include "utp_utils.h" + +#ifdef WIN32 + +typedef ULONGLONG (WINAPI GetTickCount64Proc)(void); +static GetTickCount64Proc *pt2GetTickCount64; +static GetTickCount64Proc *pt2RealGetTickCount; + +static uint64 startPerformanceCounter; +static uint64 startGetTickCount; +// MSVC 6 standard doesn't like division with uint64s +static double counterPerMicrosecond; + +static uint64 UTGetTickCount64() +{ + if (pt2GetTickCount64) { + return pt2GetTickCount64(); + } + if (pt2RealGetTickCount) { + uint64 v = pt2RealGetTickCount(); + // fix return value from GetTickCount + return (DWORD)v | ((v >> 0x18) & 0xFFFFFFFF00000000); + } + return (uint64)GetTickCount(); +} + +static void Time_Initialize() +{ + HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); + pt2GetTickCount64 = (GetTickCount64Proc*)GetProcAddress(kernel32, "GetTickCount64"); + // not a typo. GetTickCount actually returns 64 bits + pt2RealGetTickCount = (GetTickCount64Proc*)GetProcAddress(kernel32, "GetTickCount"); + + uint64 frequency; + QueryPerformanceCounter((LARGE_INTEGER*)&startPerformanceCounter); + QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); + counterPerMicrosecond = (double)frequency / 1000000.0f; + startGetTickCount = UTGetTickCount64(); +} + +static int64 abs64(int64 x) { return x < 0 ? -x : x; } + +static uint64 __GetMicroseconds() +{ + static bool time_init = false; + if (!time_init) { + time_init = true; + Time_Initialize(); + } + + uint64 counter; + uint64 tick; + + QueryPerformanceCounter((LARGE_INTEGER*) &counter); + tick = UTGetTickCount64(); + + // unfortunately, QueryPerformanceCounter is not guaranteed + // to be monotonic. Make it so. + int64 ret = (int64)(((int64)counter - (int64)startPerformanceCounter) / counterPerMicrosecond); + // if the QPC clock leaps more than one second off GetTickCount64() + // something is seriously fishy. Adjust QPC to stay monotonic + int64 tick_diff = tick - startGetTickCount; + if (abs64(ret / 100000 - tick_diff / 100) > 10) { + startPerformanceCounter -= (uint64)((int64)(tick_diff * 1000 - ret) * counterPerMicrosecond); + ret = (int64)((counter - startPerformanceCounter) / counterPerMicrosecond); + } + return ret; +} + +static inline uint64 UTP_GetMilliseconds() +{ + return GetTickCount(); +} + +#else //!WIN32 + +static inline uint64 UTP_GetMicroseconds(void); +static inline uint64 UTP_GetMilliseconds() +{ + return UTP_GetMicroseconds() / 1000; +} + +#if defined(__APPLE__) + +static uint64 __GetMicroseconds() +{ + // http://developer.apple.com/mac/library/qa/qa2004/qa1398.html + // http://www.macresearch.org/tutorial_performance_and_time + static mach_timebase_info_data_t sTimebaseInfo; + static uint64_t start_tick = 0; + uint64_t tick; + // Returns a counter in some fraction of a nanoseconds + tick = mach_absolute_time(); + if (sTimebaseInfo.denom == 0) { + // Get the timer ratio to convert mach_absolute_time to nanoseconds + mach_timebase_info(&sTimebaseInfo); + start_tick = tick; + } + // Calculate the elapsed time, convert it to microseconds and return it. + return ((tick - start_tick) * sTimebaseInfo.numer) / (sTimebaseInfo.denom * 1000); +} + +#else // !__APPLE__ + +#if ! (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)) + #warning "Using non-monotonic function gettimeofday() in UTP_GetMicroseconds()" +#endif + +/* Unfortunately, #ifdef CLOCK_MONOTONIC is not enough to make sure that + POSIX clocks work -- we could be running a recent libc with an ancient + kernel (think OpenWRT). -- jch */ + +static uint64_t __GetMicroseconds() +{ + struct timeval tv; + + #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC) + static int have_posix_clocks = -1; + int rc; + + if (have_posix_clocks < 0) { + struct timespec ts; + rc = clock_gettime(CLOCK_MONOTONIC, &ts); + if (rc < 0) { + have_posix_clocks = 0; + } else { + have_posix_clocks = 1; + } + } + + if (have_posix_clocks) { + struct timespec ts; + rc = clock_gettime(CLOCK_MONOTONIC, &ts); + return uint64(ts.tv_sec) * 1000000 + uint64(ts.tv_nsec) / 1000; + } + #endif + + gettimeofday(&tv, NULL); + return uint64(tv.tv_sec) * 1000000 + tv.tv_usec; +} + +#endif //!__APPLE__ + +#endif //!WIN32 + +/* + * Whew. Okay. After that #ifdef maze above, we now know we have a working + * __GetMicroseconds() implementation on all platforms. + * + * Because there are a number of assertions in libutp that will cause a crash + * if monotonic time isn't monotonic, now apply some safety checks. While in + * principle we're already protecting ourselves in cases where non-monotonic + * time is likely to happen, this protects all versions. + */ + +static inline uint64 UTP_GetMicroseconds() +{ + static uint64 offset = 0, previous = 0; + + uint64 now = __GetMicroseconds() + offset; + if (previous > now) { + /* Eek! */ + offset += previous - now; + now = previous; + } + previous = now; + return now; +} + +#define ETHERNET_MTU 1500 +#define IPV4_HEADER_SIZE 20 +#define IPV6_HEADER_SIZE 40 +#define UDP_HEADER_SIZE 8 +#define GRE_HEADER_SIZE 24 +#define PPPOE_HEADER_SIZE 8 +#define MPPE_HEADER_SIZE 2 +// packets have been observed in the wild that were fragmented +// with a payload of 1416 for the first fragment +// There are reports of routers that have MTU sizes as small as 1392 +#define FUDGE_HEADER_SIZE 36 +#define TEREDO_MTU 1280 + +#define UDP_IPV4_OVERHEAD (IPV4_HEADER_SIZE + UDP_HEADER_SIZE) +#define UDP_IPV6_OVERHEAD (IPV6_HEADER_SIZE + UDP_HEADER_SIZE) +#define UDP_TEREDO_OVERHEAD (UDP_IPV4_OVERHEAD + UDP_IPV6_OVERHEAD) + +#define UDP_IPV4_MTU (ETHERNET_MTU - IPV4_HEADER_SIZE - UDP_HEADER_SIZE - GRE_HEADER_SIZE - PPPOE_HEADER_SIZE - MPPE_HEADER_SIZE - FUDGE_HEADER_SIZE) +#define UDP_IPV6_MTU (ETHERNET_MTU - IPV6_HEADER_SIZE - UDP_HEADER_SIZE - GRE_HEADER_SIZE - PPPOE_HEADER_SIZE - MPPE_HEADER_SIZE - FUDGE_HEADER_SIZE) +#define UDP_TEREDO_MTU (TEREDO_MTU - IPV6_HEADER_SIZE - UDP_HEADER_SIZE) + +uint64 utp_default_get_udp_mtu(utp_callback_arguments *args) { + // Since we don't know the local address of the interface, + // be conservative and assume all IPv6 connections are Teredo. + return (args->address->sa_family == AF_INET6) ? UDP_TEREDO_MTU : UDP_IPV4_MTU; +} + +uint64 utp_default_get_udp_overhead(utp_callback_arguments *args) { + // Since we don't know the local address of the interface, + // be conservative and assume all IPv6 connections are Teredo. + return (args->address->sa_family == AF_INET6) ? UDP_TEREDO_OVERHEAD : UDP_IPV4_OVERHEAD; +} + +uint64 utp_default_get_random(utp_callback_arguments *args) { + return rand(); +} + +uint64 utp_default_get_milliseconds(utp_callback_arguments *args) { + return UTP_GetMilliseconds(); +} + +uint64 utp_default_get_microseconds(utp_callback_arguments *args) { + return UTP_GetMicroseconds(); +} diff --git a/utp-client-interop/libutp (copy)/utp_utils.h b/utp-client-interop/libutp (copy)/utp_utils.h new file mode 100644 index 0000000..7eb0c55 --- /dev/null +++ b/utp-client-interop/libutp (copy)/utp_utils.h @@ -0,0 +1,27 @@ +/* + * 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. + */ + +uint64 utp_default_get_udp_mtu(utp_callback_arguments *args); +uint64 utp_default_get_udp_overhead(utp_callback_arguments *args); +uint64 utp_default_get_random(utp_callback_arguments *args); +uint64 utp_default_get_milliseconds(utp_callback_arguments *args); +uint64 utp_default_get_microseconds(utp_callback_arguments *args); From 961f395c03b0e91354b41bc9cd6862e0b9a0dc0d Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Sat, 10 Jun 2023 12:56:45 -0600 Subject: [PATCH 5/6] Delete libutp --- utp-client-interop/libutp | 1 - 1 file changed, 1 deletion(-) delete mode 160000 utp-client-interop/libutp diff --git a/utp-client-interop/libutp b/utp-client-interop/libutp deleted file mode 160000 index a0cd2d8..0000000 --- a/utp-client-interop/libutp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a0cd2d8a2aeaf94460c2ac7b438d039970ffbe7e From 60932478ba603c644a8e0c689c710075cc320f7c Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Sat, 10 Jun 2023 12:57:05 -0600 Subject: [PATCH 6/6] Make libutp not submodule --- utp-client-interop/{libutp (copy) => libutp}/LICENSE | 0 utp-client-interop/{libutp (copy) => libutp}/Makefile | 0 utp-client-interop/{libutp (copy) => libutp}/README.md | 0 .../{libutp (copy) => libutp}/libutp-2012.vcxproj.filters | 0 utp-client-interop/{libutp (copy) => libutp}/libutp.vcxproj | 0 utp-client-interop/{libutp (copy) => libutp}/libutp_inet_ntop.cpp | 0 utp-client-interop/{libutp (copy) => libutp}/libutp_inet_ntop.h | 0 utp-client-interop/{libutp (copy) => libutp}/parse_log.py | 0 utp-client-interop/{libutp (copy) => libutp}/portal-test.c | 0 .../{libutp (copy) => libutp}/prop_sheets/RunTimeDebug.props | 0 .../{libutp (copy) => libutp}/prop_sheets/RunTimeRelease.props | 0 .../{libutp (copy) => libutp}/prop_sheets/debug-2012.props | 0 .../{libutp (copy) => libutp}/prop_sheets/release-2012.props | 0 .../{libutp (copy) => libutp}/prop_sheets/win32-2012.props | 0 .../{libutp (copy) => libutp}/prop_sheets/x64-2012.props | 0 utp-client-interop/{libutp (copy) => libutp}/ucat.c | 0 utp-client-interop/{libutp (copy) => libutp}/utp.h | 0 utp-client-interop/{libutp (copy) => libutp}/utp_api.cpp | 0 utp-client-interop/{libutp (copy) => libutp}/utp_callbacks.cpp | 0 utp-client-interop/{libutp (copy) => libutp}/utp_callbacks.h | 0 utp-client-interop/{libutp (copy) => libutp}/utp_hash.cpp | 0 utp-client-interop/{libutp (copy) => libutp}/utp_hash.h | 0 utp-client-interop/{libutp (copy) => libutp}/utp_internal.cpp | 0 utp-client-interop/{libutp (copy) => libutp}/utp_internal.h | 0 .../{libutp (copy) => libutp}/utp_packedsockaddr.cpp | 0 utp-client-interop/{libutp (copy) => libutp}/utp_packedsockaddr.h | 0 utp-client-interop/{libutp (copy) => libutp}/utp_templates.h | 0 utp-client-interop/{libutp (copy) => libutp}/utp_types.h | 0 utp-client-interop/{libutp (copy) => libutp}/utp_utils.cpp | 0 utp-client-interop/{libutp (copy) => libutp}/utp_utils.h | 0 30 files changed, 0 insertions(+), 0 deletions(-) rename utp-client-interop/{libutp (copy) => libutp}/LICENSE (100%) rename utp-client-interop/{libutp (copy) => libutp}/Makefile (100%) rename utp-client-interop/{libutp (copy) => libutp}/README.md (100%) rename utp-client-interop/{libutp (copy) => libutp}/libutp-2012.vcxproj.filters (100%) rename utp-client-interop/{libutp (copy) => libutp}/libutp.vcxproj (100%) rename utp-client-interop/{libutp (copy) => libutp}/libutp_inet_ntop.cpp (100%) rename utp-client-interop/{libutp (copy) => libutp}/libutp_inet_ntop.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/parse_log.py (100%) rename utp-client-interop/{libutp (copy) => libutp}/portal-test.c (100%) rename utp-client-interop/{libutp (copy) => libutp}/prop_sheets/RunTimeDebug.props (100%) rename utp-client-interop/{libutp (copy) => libutp}/prop_sheets/RunTimeRelease.props (100%) rename utp-client-interop/{libutp (copy) => libutp}/prop_sheets/debug-2012.props (100%) rename utp-client-interop/{libutp (copy) => libutp}/prop_sheets/release-2012.props (100%) rename utp-client-interop/{libutp (copy) => libutp}/prop_sheets/win32-2012.props (100%) rename utp-client-interop/{libutp (copy) => libutp}/prop_sheets/x64-2012.props (100%) rename utp-client-interop/{libutp (copy) => libutp}/ucat.c (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_api.cpp (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_callbacks.cpp (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_callbacks.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_hash.cpp (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_hash.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_internal.cpp (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_internal.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_packedsockaddr.cpp (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_packedsockaddr.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_templates.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_types.h (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_utils.cpp (100%) rename utp-client-interop/{libutp (copy) => libutp}/utp_utils.h (100%) diff --git a/utp-client-interop/libutp (copy)/LICENSE b/utp-client-interop/libutp/LICENSE similarity index 100% rename from utp-client-interop/libutp (copy)/LICENSE rename to utp-client-interop/libutp/LICENSE diff --git a/utp-client-interop/libutp (copy)/Makefile b/utp-client-interop/libutp/Makefile similarity index 100% rename from utp-client-interop/libutp (copy)/Makefile rename to utp-client-interop/libutp/Makefile diff --git a/utp-client-interop/libutp (copy)/README.md b/utp-client-interop/libutp/README.md similarity index 100% rename from utp-client-interop/libutp (copy)/README.md rename to utp-client-interop/libutp/README.md diff --git a/utp-client-interop/libutp (copy)/libutp-2012.vcxproj.filters b/utp-client-interop/libutp/libutp-2012.vcxproj.filters similarity index 100% rename from utp-client-interop/libutp (copy)/libutp-2012.vcxproj.filters rename to utp-client-interop/libutp/libutp-2012.vcxproj.filters diff --git a/utp-client-interop/libutp (copy)/libutp.vcxproj b/utp-client-interop/libutp/libutp.vcxproj similarity index 100% rename from utp-client-interop/libutp (copy)/libutp.vcxproj rename to utp-client-interop/libutp/libutp.vcxproj diff --git a/utp-client-interop/libutp (copy)/libutp_inet_ntop.cpp b/utp-client-interop/libutp/libutp_inet_ntop.cpp similarity index 100% rename from utp-client-interop/libutp (copy)/libutp_inet_ntop.cpp rename to utp-client-interop/libutp/libutp_inet_ntop.cpp diff --git a/utp-client-interop/libutp (copy)/libutp_inet_ntop.h b/utp-client-interop/libutp/libutp_inet_ntop.h similarity index 100% rename from utp-client-interop/libutp (copy)/libutp_inet_ntop.h rename to utp-client-interop/libutp/libutp_inet_ntop.h diff --git a/utp-client-interop/libutp (copy)/parse_log.py b/utp-client-interop/libutp/parse_log.py similarity index 100% rename from utp-client-interop/libutp (copy)/parse_log.py rename to utp-client-interop/libutp/parse_log.py diff --git a/utp-client-interop/libutp (copy)/portal-test.c b/utp-client-interop/libutp/portal-test.c similarity index 100% rename from utp-client-interop/libutp (copy)/portal-test.c rename to utp-client-interop/libutp/portal-test.c diff --git a/utp-client-interop/libutp (copy)/prop_sheets/RunTimeDebug.props b/utp-client-interop/libutp/prop_sheets/RunTimeDebug.props similarity index 100% rename from utp-client-interop/libutp (copy)/prop_sheets/RunTimeDebug.props rename to utp-client-interop/libutp/prop_sheets/RunTimeDebug.props diff --git a/utp-client-interop/libutp (copy)/prop_sheets/RunTimeRelease.props b/utp-client-interop/libutp/prop_sheets/RunTimeRelease.props similarity index 100% rename from utp-client-interop/libutp (copy)/prop_sheets/RunTimeRelease.props rename to utp-client-interop/libutp/prop_sheets/RunTimeRelease.props diff --git a/utp-client-interop/libutp (copy)/prop_sheets/debug-2012.props b/utp-client-interop/libutp/prop_sheets/debug-2012.props similarity index 100% rename from utp-client-interop/libutp (copy)/prop_sheets/debug-2012.props rename to utp-client-interop/libutp/prop_sheets/debug-2012.props diff --git a/utp-client-interop/libutp (copy)/prop_sheets/release-2012.props b/utp-client-interop/libutp/prop_sheets/release-2012.props similarity index 100% rename from utp-client-interop/libutp (copy)/prop_sheets/release-2012.props rename to utp-client-interop/libutp/prop_sheets/release-2012.props diff --git a/utp-client-interop/libutp (copy)/prop_sheets/win32-2012.props b/utp-client-interop/libutp/prop_sheets/win32-2012.props similarity index 100% rename from utp-client-interop/libutp (copy)/prop_sheets/win32-2012.props rename to utp-client-interop/libutp/prop_sheets/win32-2012.props diff --git a/utp-client-interop/libutp (copy)/prop_sheets/x64-2012.props b/utp-client-interop/libutp/prop_sheets/x64-2012.props similarity index 100% rename from utp-client-interop/libutp (copy)/prop_sheets/x64-2012.props rename to utp-client-interop/libutp/prop_sheets/x64-2012.props diff --git a/utp-client-interop/libutp (copy)/ucat.c b/utp-client-interop/libutp/ucat.c similarity index 100% rename from utp-client-interop/libutp (copy)/ucat.c rename to utp-client-interop/libutp/ucat.c diff --git a/utp-client-interop/libutp (copy)/utp.h b/utp-client-interop/libutp/utp.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp.h rename to utp-client-interop/libutp/utp.h diff --git a/utp-client-interop/libutp (copy)/utp_api.cpp b/utp-client-interop/libutp/utp_api.cpp similarity index 100% rename from utp-client-interop/libutp (copy)/utp_api.cpp rename to utp-client-interop/libutp/utp_api.cpp diff --git a/utp-client-interop/libutp (copy)/utp_callbacks.cpp b/utp-client-interop/libutp/utp_callbacks.cpp similarity index 100% rename from utp-client-interop/libutp (copy)/utp_callbacks.cpp rename to utp-client-interop/libutp/utp_callbacks.cpp diff --git a/utp-client-interop/libutp (copy)/utp_callbacks.h b/utp-client-interop/libutp/utp_callbacks.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp_callbacks.h rename to utp-client-interop/libutp/utp_callbacks.h diff --git a/utp-client-interop/libutp (copy)/utp_hash.cpp b/utp-client-interop/libutp/utp_hash.cpp similarity index 100% rename from utp-client-interop/libutp (copy)/utp_hash.cpp rename to utp-client-interop/libutp/utp_hash.cpp diff --git a/utp-client-interop/libutp (copy)/utp_hash.h b/utp-client-interop/libutp/utp_hash.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp_hash.h rename to utp-client-interop/libutp/utp_hash.h diff --git a/utp-client-interop/libutp (copy)/utp_internal.cpp b/utp-client-interop/libutp/utp_internal.cpp similarity index 100% rename from utp-client-interop/libutp (copy)/utp_internal.cpp rename to utp-client-interop/libutp/utp_internal.cpp diff --git a/utp-client-interop/libutp (copy)/utp_internal.h b/utp-client-interop/libutp/utp_internal.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp_internal.h rename to utp-client-interop/libutp/utp_internal.h diff --git a/utp-client-interop/libutp (copy)/utp_packedsockaddr.cpp b/utp-client-interop/libutp/utp_packedsockaddr.cpp similarity index 100% rename from utp-client-interop/libutp (copy)/utp_packedsockaddr.cpp rename to utp-client-interop/libutp/utp_packedsockaddr.cpp diff --git a/utp-client-interop/libutp (copy)/utp_packedsockaddr.h b/utp-client-interop/libutp/utp_packedsockaddr.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp_packedsockaddr.h rename to utp-client-interop/libutp/utp_packedsockaddr.h diff --git a/utp-client-interop/libutp (copy)/utp_templates.h b/utp-client-interop/libutp/utp_templates.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp_templates.h rename to utp-client-interop/libutp/utp_templates.h diff --git a/utp-client-interop/libutp (copy)/utp_types.h b/utp-client-interop/libutp/utp_types.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp_types.h rename to utp-client-interop/libutp/utp_types.h diff --git a/utp-client-interop/libutp (copy)/utp_utils.cpp b/utp-client-interop/libutp/utp_utils.cpp similarity index 100% rename from utp-client-interop/libutp (copy)/utp_utils.cpp rename to utp-client-interop/libutp/utp_utils.cpp diff --git a/utp-client-interop/libutp (copy)/utp_utils.h b/utp-client-interop/libutp/utp_utils.h similarity index 100% rename from utp-client-interop/libutp (copy)/utp_utils.h rename to utp-client-interop/libutp/utp_utils.h