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:06:42 +00:00
|
|
|
#include <assert.h>
|
2011-02-18 00:23:47 +00:00
|
|
|
|
2011-02-18 01:25:32 +00:00
|
|
|
#include <event2/event.h>
|
2011-03-24 21:49:42 +00:00
|
|
|
|
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"
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h" /* tr_rand_int_weak () */
|
2011-02-18 00:23:58 +00:00
|
|
|
#include "peer-mgr.h"
|
2011-02-18 00:23:47 +00:00
|
|
|
#include "tr-utp.h"
|
2011-02-18 01:06:42 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define MY_NAME "UTP"
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(...) \
|
2011-02-18 01:06:42 +00:00
|
|
|
do { \
|
2013-01-25 23:34:20 +00:00
|
|
|
if (tr_logGetDeepEnabled ()) \
|
|
|
|
tr_logAddDeep (__FILE__, __LINE__, MY_NAME, __VA_ARGS__); \
|
2012-12-05 17:29:46 +00:00
|
|
|
} while (0)
|
2011-02-18 00:23:47 +00:00
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#ifndef WITH_UTP
|
|
|
|
|
2011-02-18 01:06:42 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
UTP_Close (struct UTPSocket * socket)
|
2011-02-18 01:06:42 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedError (MY_NAME, "UTP_Close (%p) was called.", socket);
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg ("UTP_Close (%p) was called.", socket);
|
|
|
|
assert (0); /* FIXME: this is too much for the long term, but probably needed in the short term */
|
2011-02-18 01:06:42 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 01:18:51 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
UTP_RBDrained (struct UTPSocket *socket)
|
2011-02-18 01:18:51 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedError (MY_NAME, "UTP_RBDrained (%p) was called.", socket);
|
2012-12-05 17:29:46 +00:00
|
|
|
dbgmsg ("UTP_RBDrained (%p) was called.", socket);
|
|
|
|
assert (0); /* FIXME: this is too much for the long term, but probably needed in the short term */
|
2011-02-18 01:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
UTP_Write (struct UTPSocket *socket, size_t count)
|
2011-02-18 01:18:51 +00:00
|
|
|
{
|
2013-09-08 17:32:09 +00:00
|
|
|
tr_logAddNamedError (MY_NAME, "UTP_RBDrained (%p, %"TR_PRIuSIZE") was called.", socket, count);
|
|
|
|
dbgmsg ("UTP_RBDrained (%p, %"TR_PRIuSIZE") was called.", socket, count);
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (0); /* FIXME: this is too much for the long term, but probably needed in the short term */
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2011-02-18 01:18:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_utpPacket (const unsigned char *buf UNUSED, size_t buflen UNUSED,
|
2011-02-18 00:45:44 +00:00
|
|
|
const struct sockaddr *from UNUSED, socklen_t fromlen UNUSED,
|
|
|
|
tr_session *ss UNUSED) { return -1; }
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
struct UTPSocket *UTP_Create (SendToProc *send_to_proc UNUSED,
|
2011-02-18 16:01:52 +00:00
|
|
|
void *send_to_userdata UNUSED,
|
|
|
|
const struct sockaddr *addr UNUSED,
|
|
|
|
socklen_t addrlen UNUSED)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_utpClose (tr_session * ss UNUSED) { }
|
2011-02-18 00:45:44 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_utpSendTo (void *closure UNUSED,
|
2011-02-18 00:45:44 +00:00
|
|
|
const unsigned char *buf UNUSED, size_t buflen UNUSED,
|
|
|
|
const struct sockaddr *to UNUSED, socklen_t tolen UNUSED) { }
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2011-02-18 00:24:04 +00:00
|
|
|
/* Greg says 50ms works for them. */
|
|
|
|
|
|
|
|
#define UTP_INTERVAL_US 50000
|
|
|
|
|
2011-02-18 00:23:47 +00:00
|
|
|
static struct event *utp_timer = NULL;
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
incoming (void *closure, struct UTPSocket *s)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2011-03-03 21:51:11 +00:00
|
|
|
tr_session *ss = closure;
|
2011-02-18 00:23:58 +00:00
|
|
|
struct sockaddr_storage from_storage;
|
|
|
|
struct sockaddr *from = (struct sockaddr*)&from_storage;
|
2012-12-05 17:29:46 +00:00
|
|
|
socklen_t fromlen = sizeof (from_storage);
|
2011-02-18 00:23:58 +00:00
|
|
|
tr_address addr;
|
|
|
|
tr_port port;
|
2011-02-18 00:35:56 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_sessionIsUTPEnabled (ss)) {
|
|
|
|
UTP_Close (s);
|
2011-02-18 00:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2012-12-05 17:29:46 +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
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedError ("UTP", "Unknown socket family");
|
2012-12-05 17:29:46 +00:00
|
|
|
UTP_Close (s);
|
2011-02-18 00:23:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_peerMgrAddIncoming (ss->peerMgr, &addr, port, -1, s);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 00:36:16 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_utpSendTo (void *closure, const unsigned char *buf, size_t buflen,
|
2011-02-18 00:36:16 +00:00
|
|
|
const struct sockaddr *to, socklen_t tolen)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2011-03-03 21:51:11 +00:00
|
|
|
tr_session *ss = closure;
|
2011-02-18 00:23:47 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (to->sa_family == AF_INET && ss->udp_socket)
|
2015-01-02 04:02:13 +00:00
|
|
|
sendto (ss->udp_socket, (const void *) buf, buflen, 0, to, tolen);
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (to->sa_family == AF_INET6 && ss->udp_socket)
|
2015-01-02 04:02:13 +00:00
|
|
|
sendto (ss->udp6_socket, (const void *) buf, buflen, 0, to, tolen);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 00:33:11 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
reset_timer (tr_session *ss)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2011-02-18 00:43:27 +00:00
|
|
|
int sec;
|
|
|
|
int usec;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tr_sessionIsUTPEnabled (ss)) {
|
2011-02-18 00:43:27 +00:00
|
|
|
sec = 0;
|
2014-12-04 11:27:38 +00:00
|
|
|
usec = UTP_INTERVAL_US / 2 + tr_rand_int_weak (UTP_INTERVAL_US);
|
2011-02-18 00:43:27 +00:00
|
|
|
} else {
|
|
|
|
/* 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;
|
2014-12-04 11:27:38 +00:00
|
|
|
usec = tr_rand_int_weak (1000000);
|
2011-02-18 00:43:27 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_timerAdd (utp_timer, sec, usec);
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 00:23:47 +00:00
|
|
|
static void
|
2014-12-13 15:22:39 +00:00
|
|
|
timer_callback (evutil_socket_t s UNUSED, short type UNUSED, void *closure)
|
2011-02-18 00:23:47 +00:00
|
|
|
{
|
2011-02-18 00:43:27 +00:00
|
|
|
tr_session *ss = closure;
|
2012-12-05 17:29:46 +00:00
|
|
|
UTP_CheckTimeouts ();
|
|
|
|
reset_timer (ss);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_utpPacket (const unsigned char *buf, size_t buflen,
|
2011-02-18 00:23:47 +00:00
|
|
|
const struct sockaddr *from, socklen_t fromlen,
|
|
|
|
tr_session *ss)
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ss->isClosed && !utp_timer)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
utp_timer = evtimer_new (ss->event_base, timer_callback, ss);
|
|
|
|
if (utp_timer == NULL)
|
2011-02-18 00:23:47 +00:00
|
|
|
return -1;
|
2012-12-05 17:29:46 +00:00
|
|
|
reset_timer (ss);
|
2011-02-18 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return UTP_IsIncomingUTP (incoming, tr_utpSendTo, ss,
|
2011-02-18 00:23:47 +00:00
|
|
|
buf, buflen, from, fromlen);
|
|
|
|
}
|
|
|
|
|
2011-02-18 00:33:11 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_utpClose (tr_session * session UNUSED)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (utp_timer)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
evtimer_del (utp_timer);
|
2011-02-18 00:33:11 +00:00
|
|
|
utp_timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-02-18 00:45:44 +00:00
|
|
|
|
|
|
|
#endif /* #ifndef WITH_UTP ... else */
|