2011-02-18 00:23:47 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2010 by Juliusz Chroboczek
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-02-18 01:25:32 +00:00
|
|
|
#include <event2/event.h>
|
2011-03-24 21:49:42 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstdint>
|
2012-05-30 17:47:29 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "transmission.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "session.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_rand_int_weak() */
|
2011-02-18 00:23:58 +00:00
|
|
|
#include "peer-mgr.h"
|
2017-06-28 15:46:06 +00:00
|
|
|
#include "peer-socket.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "tr-utp.h"
|
2011-02-18 01:06:42 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#ifndef WITH_UTP
|
|
|
|
|
2011-02-18 01:06:42 +00:00
|
|
|
#define MY_NAME "UTP"
|
|
|
|
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed(MY_NAME, __VA_ARGS__)
|
2011-02-18 00:23:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void UTP_Close(struct UTPSocket* socket)
|
2011-02-18 01:06:42 +00:00
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
tr_logAddNamedError(MY_NAME, "UTP_Close(%p) was called.", socket);
|
|
|
|
dbgmsg("UTP_Close(%p) was called.", socket);
|
2011-02-18 01:06:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void UTP_RBDrained(struct UTPSocket* socket)
|
2011-02-18 01:18:51 +00:00
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
tr_logAddNamedError(MY_NAME, "UTP_RBDrained(%p) was called.", socket);
|
|
|
|
dbgmsg("UTP_RBDrained(%p) was called.", socket);
|
2011-02-18 01:18:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool UTP_Write(struct UTPSocket* socket, size_t count)
|
2011-02-18 01:18:51 +00:00
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
tr_logAddNamedError(MY_NAME, "UTP_RBDrained(%p, %zu) was called.", socket, count);
|
|
|
|
dbgmsg("UTP_RBDrained(%p, %zu) was called.", socket, count);
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2011-02-18 01:18:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
int tr_utpPacket(
|
2021-10-24 16:41:54 +00:00
|
|
|
unsigned char const* /*buf*/,
|
|
|
|
size_t /*buflen*/,
|
|
|
|
sockaddr const* /*from*/,
|
|
|
|
socklen_t /*fromlen*/,
|
|
|
|
tr_session* /*ss*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
struct UTPSocket* UTP_Create(
|
2021-10-24 16:41:54 +00:00
|
|
|
SendToProc* /*send_to_proc*/,
|
|
|
|
void* /*send_to_userdata*/,
|
|
|
|
sockaddr const* /*addr*/,
|
|
|
|
socklen_t /*addrlen*/)
|
2011-02-18 16:01:52 +00:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2011-02-18 16:01:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
void tr_utpClose(tr_session* /*ss*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
void tr_utpSendTo(
|
2021-10-24 16:41:54 +00:00
|
|
|
void* /*closure*/,
|
|
|
|
unsigned char const* /*buf*/,
|
|
|
|
size_t /*buflen*/,
|
|
|
|
struct sockaddr const* /*to*/,
|
|
|
|
socklen_t /*tolen*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2011-02-18 00:24:04 +00:00
|
|
|
/* Greg says 50ms works for them. */
|
|
|
|
|
|
|
|
#define UTP_INTERVAL_US 50000
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void incoming(void* vsession, struct UTPSocket* s)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2011-02-18 00:23:58 +00:00
|
|
|
struct sockaddr_storage from_storage;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct sockaddr* from = (struct sockaddr*)&from_storage;
|
|
|
|
socklen_t fromlen = sizeof(from_storage);
|
2011-02-18 00:23:58 +00:00
|
|
|
tr_address addr;
|
2021-10-23 15:43:15 +00:00
|
|
|
tr_port port = 0;
|
2011-02-18 00:35:56 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (!tr_sessionIsUTPEnabled(session))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
UTP_Close(s);
|
2011-02-18 00:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
UTP_GetPeerName(s, from, &fromlen);
|
|
|
|
|
|
|
|
if (!tr_address_from_sockaddr_storage(&addr, &port, &from_storage))
|
2011-03-04 21:00:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedError("UTP", "Unknown socket family");
|
|
|
|
UTP_Close(s);
|
2011-02-18 00:23:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_peerMgrAddIncoming(session->peerMgr, &addr, port, tr_peer_socket_utp_create(s));
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_utpSendTo(void* closure, unsigned char const* buf, size_t buflen, struct sockaddr const* to, socklen_t tolen)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* const ss = static_cast<tr_session const*>(closure);
|
2011-02-18 00:23:47 +00:00
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (to->sa_family == AF_INET && ss->udp_socket != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
sendto(ss->udp_socket, reinterpret_cast<char const*>(buf), buflen, 0, to, tolen);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-03-18 07:34:26 +00:00
|
|
|
else if (to->sa_family == AF_INET6 && ss->udp6_socket != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
sendto(ss->udp6_socket, reinterpret_cast<char const*>(buf), buflen, 0, to, tolen);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void reset_timer(tr_session* ss)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
int sec = 0;
|
|
|
|
int usec = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_sessionIsUTPEnabled(ss))
|
|
|
|
{
|
2011-02-18 00:43:27 +00:00
|
|
|
sec = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
usec = UTP_INTERVAL_US / 2 + tr_rand_int_weak(UTP_INTERVAL_US);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-18 00:43:27 +00:00
|
|
|
/* If somebody has disabled uTP, then we still want to run
|
|
|
|
UTP_CheckTimeouts, in order to let closed sockets finish
|
|
|
|
gracefully and so on. However, since we're not particularly
|
|
|
|
interested in that happening in a timely manner, we might as
|
|
|
|
well use a large timeout. */
|
|
|
|
sec = 2;
|
2017-04-19 12:04:45 +00:00
|
|
|
usec = tr_rand_int_weak(1000000);
|
2011-02-18 00:43:27 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-07-02 10:31:33 +00:00
|
|
|
tr_timerAdd(ss->utp_timer, sec, usec);
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void timer_callback(evutil_socket_t /*s*/, short /*type*/, void* vsession)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2017-04-19 12:04:45 +00:00
|
|
|
UTP_CheckTimeouts();
|
2021-09-12 17:41:49 +00:00
|
|
|
reset_timer(session);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_utpPacket(unsigned char const* buf, size_t buflen, struct sockaddr const* from, socklen_t fromlen, tr_session* ss)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!ss->isClosed && ss->utp_timer == nullptr)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2017-07-02 10:31:33 +00:00
|
|
|
ss->utp_timer = evtimer_new(ss->event_base, timer_callback, ss);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ss->utp_timer == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-18 00:23:47 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reset_timer(ss);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return UTP_IsIncomingUTP(incoming, tr_utpSendTo, ss, buf, buflen, from, fromlen);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 10:31:33 +00:00
|
|
|
void tr_utpClose(tr_session* session)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session->utp_timer != nullptr)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2017-07-02 10:31:33 +00:00
|
|
|
evtimer_del(session->utp_timer);
|
2021-09-15 00:18:09 +00:00
|
|
|
session->utp_timer = nullptr;
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
|
|
|
#endif /* #ifndef WITH_UTP ... else */
|