2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2008-01-10 19:52:56 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/event.h>
|
2011-03-24 22:45:04 +00:00
|
|
|
#include <event2/buffer.h>
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/bufferevent.h>
|
2011-03-24 22:45:04 +00:00
|
|
|
|
2012-05-30 17:47:29 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "bandwidth.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "net.h"
|
2010-05-26 15:23:21 +00:00
|
|
|
#include "peer-common.h" /* MAX_BLOCK_SIZE */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-io.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2011-10-08 23:53:27 +00:00
|
|
|
#include "tr-utp.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#undef EAGAIN
|
|
|
|
#define EAGAIN WSAEWOULDBLOCK
|
|
|
|
#undef EINTR
|
|
|
|
#define EINTR WSAEINTR
|
|
|
|
#undef EINPROGRESS
|
|
|
|
#define EINPROGRESS WSAEINPROGRESS
|
|
|
|
#undef EPIPE
|
|
|
|
#define EPIPE WSAECONNRESET
|
2010-06-30 21:24:36 +00:00
|
|
|
#endif
|
|
|
|
|
2011-02-18 00:36:05 +00:00
|
|
|
/* The amount of read bufferring that we allow for uTP sockets. */
|
|
|
|
|
|
|
|
#define UTP_READ_BUFFER_SIZE (256 * 1024)
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static size_t guessPacketOverhead(size_t d)
|
2008-11-06 02:56:51 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* http://sd.wareonearth.com/~phil/net/overhead/
|
|
|
|
*
|
|
|
|
* TCP over Ethernet:
|
|
|
|
* Assuming no header compression (e.g. not PPP)
|
|
|
|
* Add 20 IPv4 header or 40 IPv6 header (no options)
|
|
|
|
* Add 20 TCP header
|
|
|
|
* Add 12 bytes optional TCP timestamps
|
|
|
|
* Max TCP Payload data rates over ethernet are thus:
|
2012-12-05 17:29:46 +00:00
|
|
|
* (1500-40)/ (38+1500) = 94.9285 % IPv4, minimal headers
|
|
|
|
* (1500-52)/ (38+1500) = 94.1482 % IPv4, TCP timestamps
|
|
|
|
* (1500-52)/ (42+1500) = 93.9040 % 802.1q, IPv4, TCP timestamps
|
|
|
|
* (1500-60)/ (38+1500) = 93.6281 % IPv6, minimal headers
|
|
|
|
* (1500-72)/ (38+1500) = 92.8479 % IPv6, TCP timestamps
|
|
|
|
* (1500-72)/ (42+1500) = 92.6070 % 802.1q, IPv6, ICP timestamps
|
2008-11-06 02:56:51 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
double const assumed_payload_data_rate = 94.0;
|
2008-11-06 02:56:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return (unsigned int)(d * (100.0 / assumed_payload_data_rate) - d);
|
2008-11-06 02:56:51 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
#define dbgmsg(io, ...) \
|
2021-08-15 09:41:48 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2021-09-10 17:22:46 +00:00
|
|
|
if (tr_logGetDeepEnabled()) \
|
|
|
|
{ \
|
|
|
|
char addrstr[TR_ADDRSTRLEN]; \
|
|
|
|
tr_peerIoGetAddrStr(io, addrstr, sizeof(addrstr)); \
|
|
|
|
tr_logAddDeep(__FILE__, __LINE__, addrstr, __VA_ARGS__); \
|
|
|
|
} \
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (0)
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2011-04-10 05:21:51 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-11-17 04:00:57 +00:00
|
|
|
struct tr_datatype
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_datatype* next;
|
2011-04-10 05:21:51 +00:00
|
|
|
size_t length;
|
|
|
|
bool isPieceData;
|
2008-11-17 04:00:57 +00:00
|
|
|
};
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
static struct tr_datatype* datatype_pool = nullptr;
|
2011-04-10 05:21:51 +00:00
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
static struct tr_datatype* datatype_new()
|
2011-04-10 05:21:51 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
tr_datatype* ret = nullptr;
|
2011-04-10 05:21:51 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (datatype_pool == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = tr_new(struct tr_datatype, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-10 05:21:51 +00:00
|
|
|
ret = datatype_pool;
|
|
|
|
datatype_pool = datatype_pool->next;
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
*ret = {};
|
2011-04-10 05:21:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void datatype_free(struct tr_datatype* datatype)
|
2011-04-10 05:21:51 +00:00
|
|
|
{
|
|
|
|
datatype->next = datatype_pool;
|
|
|
|
datatype_pool = datatype;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void peer_io_pull_datatype(tr_peerIo* io)
|
2011-04-10 05:21:51 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto* const tmp = io->outbuf_datatypes;
|
2011-04-10 05:21:51 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
if (tmp != nullptr)
|
2011-04-10 05:21:51 +00:00
|
|
|
{
|
|
|
|
io->outbuf_datatypes = tmp->next;
|
2017-04-19 12:04:45 +00:00
|
|
|
datatype_free(tmp);
|
2011-04-10 05:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void peer_io_push_datatype(tr_peerIo* io, struct tr_datatype* datatype)
|
2011-04-10 05:21:51 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
tr_datatype* tmp = io->outbuf_datatypes;
|
2011-04-10 05:21:51 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
if (tmp != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
while (tmp->next != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-04-10 05:21:51 +00:00
|
|
|
tmp = tmp->next;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2011-04-10 05:21:51 +00:00
|
|
|
tmp->next = datatype;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-10 05:21:51 +00:00
|
|
|
io->outbuf_datatypes = datatype;
|
|
|
|
}
|
|
|
|
}
|
2011-01-29 18:59:23 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void didWriteWrapper(tr_peerIo* io, unsigned int bytes_transferred)
|
2007-12-01 23:08:34 +00:00
|
|
|
{
|
2021-09-17 04:21:50 +00:00
|
|
|
while (bytes_transferred != 0 && tr_isPeerIo(io) && io->outbuf_datatypes != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
struct tr_datatype* next = io->outbuf_datatypes;
|
2010-04-23 23:45:44 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
unsigned int const payload = std::min(uint64_t{ next->length }, uint64_t{ bytes_transferred });
|
2011-02-18 00:36:02 +00:00
|
|
|
/* For uTP sockets, the overhead is computed in utp_on_overhead. */
|
2017-06-28 15:46:06 +00:00
|
|
|
unsigned int const overhead = io->socket.type == TR_PEER_SOCKET_TYPE_TCP ? guessPacketOverhead(payload) : 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t const now = tr_time_msec();
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
io->bandwidth->notifyBandwidthConsumed(TR_UP, payload, next->isPieceData, now);
|
2008-11-26 15:58:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (overhead > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
io->bandwidth->notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->didWrite != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
io->didWrite(io, payload, next->isPieceData, io->userData);
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_isPeerIo(io))
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
|
|
|
bytes_transferred -= payload;
|
|
|
|
next->length -= payload;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (next->length == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
peer_io_pull_datatype(io);
|
|
|
|
}
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2007-12-01 23:08:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void canReadWrapper(tr_peerIo* io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "canRead");
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoRef(io);
|
2009-01-05 04:27:54 +00:00
|
|
|
|
2021-11-28 01:58:35 +00:00
|
|
|
tr_session const* const session = io->session;
|
2009-10-10 17:37:34 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
/* try to consume the input buffer */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->canRead != nullptr)
|
2007-10-02 02:59:07 +00:00
|
|
|
{
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = session->unique_lock();
|
2007-10-02 02:59:07 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
auto const now = tr_time_msec();
|
|
|
|
auto done = bool{ false };
|
|
|
|
auto err = bool{ false };
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while (!done && !err)
|
2007-10-02 02:59:07 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
size_t piece = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const oldLen = evbuffer_get_length(io->inbuf);
|
|
|
|
int const ret = io->canRead(io, io->userData, &piece);
|
|
|
|
size_t const used = oldLen - evbuffer_get_length(io->inbuf);
|
|
|
|
unsigned int const overhead = guessPacketOverhead(used);
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (piece != 0 || piece != used)
|
2010-10-24 01:08:08 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (piece != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
io->bandwidth->notifyBandwidthConsumed(TR_DOWN, piece, true, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (used != piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
io->bandwidth->notifyBandwidthConsumed(TR_DOWN, used - piece, false, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-10-24 01:08:08 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (overhead > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-09 12:52:09 +00:00
|
|
|
io->bandwidth->notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-20 00:31:46 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (ret)
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case READ_NOW:
|
2017-04-30 16:25:26 +00:00
|
|
|
if (evbuffer_get_length(io->inbuf) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case READ_LATER:
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case READ_ERR:
|
|
|
|
err = true;
|
|
|
|
break;
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2009-10-10 17:37:34 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-05 04:27:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoUnref(io);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void event_read_cb(evutil_socket_t fd, short /*event*/, void* vio)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2017-06-28 15:46:06 +00:00
|
|
|
TR_ASSERT(io->socket.type == TR_PEER_SOCKET_TYPE_TCP);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2008-12-23 17:11:31 +00:00
|
|
|
/* Limit the input buffer to 256K, so it doesn't grow too large */
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_direction const dir = TR_DOWN;
|
|
|
|
unsigned int const max = 256 * 1024;
|
2008-12-23 17:23:07 +00:00
|
|
|
|
2009-01-24 14:49:35 +00:00
|
|
|
io->pendingEvents &= ~EV_READ;
|
2009-01-19 13:55:41 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
unsigned int const curlen = evbuffer_get_length(io->inbuf);
|
|
|
|
unsigned int howmuch = curlen >= max ? 0 : max - curlen;
|
2021-10-09 12:52:09 +00:00
|
|
|
howmuch = io->bandwidth->clamp(TR_DOWN, howmuch);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "libevent says this peer is ready to read");
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
/* if we don't have any bandwidth left, stop reading */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (howmuch < 1)
|
|
|
|
{
|
|
|
|
tr_peerIoSetEnabled(io, dir, false);
|
2008-12-20 22:19:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
2021-10-22 13:51:36 +00:00
|
|
|
auto const res = evbuffer_read(io->inbuf, fd, (int)howmuch);
|
|
|
|
int const e = EVUTIL_SOCKET_ERROR();
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (res > 0)
|
2008-12-22 04:55:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoSetEnabled(io, dir, true);
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2008-12-22 04:55:07 +00:00
|
|
|
/* Invoke the user callback - must always be called last */
|
2017-04-19 12:04:45 +00:00
|
|
|
canReadWrapper(io);
|
2008-12-22 04:55:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
short what = BEV_EVENT_READING;
|
2008-12-22 04:55:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (res == 0) /* EOF */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_EOF;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (res == -1)
|
|
|
|
{
|
|
|
|
if (e == EAGAIN || e == EINTR)
|
|
|
|
{
|
|
|
|
tr_peerIoSetEnabled(io, dir, true);
|
2008-12-22 04:55:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_ERROR;
|
2008-12-22 04:55:07 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
char errstr[512];
|
2021-08-15 09:41:48 +00:00
|
|
|
dbgmsg(
|
|
|
|
io,
|
|
|
|
"event_read_cb got an error. res is %d, what is %hd, errno is %d (%s)",
|
|
|
|
res,
|
|
|
|
what,
|
|
|
|
e,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_net_strerror(errstr, sizeof(errstr), e));
|
2008-12-24 02:50:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->gotError != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
io->gotError(io, what, io->userData);
|
|
|
|
}
|
2008-12-22 04:55:07 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int tr_evbuffer_write(tr_peerIo* io, int fd, size_t howmuch)
|
2008-12-20 22:19:34 +00:00
|
|
|
{
|
2010-06-30 21:24:36 +00:00
|
|
|
char errstr[256];
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
2021-10-22 13:51:36 +00:00
|
|
|
int const n = evbuffer_write_atmost(io->outbuf, fd, howmuch);
|
|
|
|
int const e = EVUTIL_SOCKET_ERROR();
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "wrote %d to peer (%s)", n, (n == -1 ? tr_net_strerror(errstr, sizeof(errstr), e) : ""));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
return n;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void event_write_cb(evutil_socket_t fd, short /*event*/, void* vio)
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2017-06-28 15:46:06 +00:00
|
|
|
TR_ASSERT(io->socket.type == TR_PEER_SOCKET_TYPE_TCP);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
auto const dir = TR_UP;
|
|
|
|
auto res = int{ 0 };
|
|
|
|
auto what = short{ BEV_EVENT_WRITING };
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2009-01-24 14:49:35 +00:00
|
|
|
io->pendingEvents &= ~EV_WRITE;
|
2009-01-19 13:55:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "libevent says this peer is ready to write");
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* Write as much as possible, since the socket is non-blocking, write() will
|
2008-12-23 17:11:31 +00:00
|
|
|
* return if it can't write any more data without blocking */
|
2021-10-22 13:51:36 +00:00
|
|
|
size_t const howmuch = io->bandwidth->clamp(dir, evbuffer_get_length(io->outbuf));
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
/* if we don't have any bandwidth left, stop writing */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (howmuch < 1)
|
|
|
|
{
|
|
|
|
tr_peerIoSetEnabled(io, dir, false);
|
2008-12-20 22:19:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
|
|
|
res = tr_evbuffer_write(io, fd, howmuch);
|
2021-10-22 13:51:36 +00:00
|
|
|
int const e = EVUTIL_SOCKET_ERROR();
|
2008-12-24 02:50:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (res == -1)
|
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (e == 0 || e == EAGAIN || e == EINTR || e == EINPROGRESS)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto RESCHEDULE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
/* error case */
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_ERROR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (res == 0)
|
|
|
|
{
|
2008-12-20 22:19:34 +00:00
|
|
|
/* eof case */
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_EOF;
|
2008-12-20 22:19:34 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
if (res <= 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (evbuffer_get_length(io->outbuf) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_peerIoSetEnabled(io, dir, true);
|
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
didWriteWrapper(io, res);
|
2008-12-20 22:19:34 +00:00
|
|
|
return;
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
RESCHEDULE:
|
2017-04-30 16:25:26 +00:00
|
|
|
if (evbuffer_get_length(io->outbuf) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_peerIoSetEnabled(io, dir, true);
|
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2008-12-24 02:50:08 +00:00
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
FAIL:
|
2021-10-22 13:51:36 +00:00
|
|
|
char errstr[1024];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_net_strerror(errstr, sizeof(errstr), e);
|
|
|
|
dbgmsg(io, "event_write_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr);
|
2008-12-24 02:50:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->gotError != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
io->gotError(io, what, io->userData);
|
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-11-14 06:41:44 +00:00
|
|
|
static void maybeSetCongestionAlgorithm(tr_socket_t socket, std::string const& algorithm)
|
2010-04-22 02:04:43 +00:00
|
|
|
{
|
2021-11-14 06:41:44 +00:00
|
|
|
if (!std::empty(algorithm))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 06:41:44 +00:00
|
|
|
tr_netSetCongestionControl(socket, algorithm.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-22 02:04:43 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#ifdef WITH_UTP
|
2011-02-18 00:24:13 +00:00
|
|
|
/* UTP callbacks */
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void utp_on_read(void* vio, unsigned char const* buf, size_t buflen)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2011-02-18 00:24:22 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
int rc = evbuffer_add(io->inbuf, buf, buflen);
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "utp_on_read got %zu bytes", buflen);
|
2011-02-18 00:40:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UTP", "On read evbuffer_add");
|
2011-02-18 00:24:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoSetEnabled(io, TR_DOWN, true);
|
|
|
|
canReadWrapper(io);
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void utp_on_write(void* vio, unsigned char* buf, size_t buflen)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2011-02-18 00:24:22 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
int rc = evbuffer_remove(io->outbuf, buf, buflen);
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "utp_on_write sending %zu bytes... evbuffer_remove returned %d", buflen, rc);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(rc == (int)buflen); /* if this fails, we've corrupted our bookkeeping somewhere */
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (rc < (long)buflen)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UTP", "Short write: %d < %ld", rc, (long)buflen);
|
2011-02-18 00:24:22 +00:00
|
|
|
}
|
2011-02-18 00:36:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
didWriteWrapper(io, buflen);
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static size_t utp_get_rb_size(void* vio)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* const io = static_cast<tr_peerIo const*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2011-02-18 00:24:13 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
size_t bytes = io->bandwidth->clamp(TR_DOWN, UTP_READ_BUFFER_SIZE);
|
2011-02-18 00:36:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "utp_get_rb_size is saying it's ready to read %zu bytes", bytes);
|
2011-02-18 00:36:09 +00:00
|
|
|
return UTP_READ_BUFFER_SIZE - bytes;
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch);
|
2012-09-05 11:39:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void utp_on_writable(tr_peerIo* io)
|
2012-09-05 11:39:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "libutp says this peer is ready to write");
|
2012-09-05 11:39:57 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
int const n = tr_peerIoTryWrite(io, SIZE_MAX);
|
2017-07-02 10:26:26 +00:00
|
|
|
tr_peerIoSetEnabled(io, TR_UP, n != 0 && evbuffer_get_length(io->outbuf) != 0);
|
2012-09-05 11:39:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void utp_on_state_change(void* vio, int state)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2011-02-18 00:24:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (state == UTP_STATE_CONNECT)
|
|
|
|
{
|
|
|
|
dbgmsg(io, "utp_on_state_change -- changed to connected");
|
2011-03-22 15:19:54 +00:00
|
|
|
io->utpSupported = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (state == UTP_STATE_WRITABLE)
|
|
|
|
{
|
|
|
|
dbgmsg(io, "utp_on_state_change -- changed to writable");
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((io->pendingEvents & EV_WRITE) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
utp_on_writable(io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == UTP_STATE_EOF)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->gotError != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
io->gotError(io, BEV_EVENT_EOF, io->userData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == UTP_STATE_DESTROYING)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UTP", "Impossible state UTP_STATE_DESTROYING");
|
2011-02-18 00:24:22 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_logAddNamedError("UTP", "Unknown state %d", state);
|
2011-02-18 00:24:22 +00:00
|
|
|
}
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void utp_on_error(void* vio, int errcode)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2011-02-18 00:24:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "utp_on_error -- errcode is %d", errcode);
|
2011-02-18 00:40:22 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->gotError != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-18 00:24:22 +00:00
|
|
|
errno = errcode;
|
2017-04-19 12:04:45 +00:00
|
|
|
io->gotError(io, BEV_EVENT_ERROR, io->userData);
|
2011-02-18 00:24:22 +00:00
|
|
|
}
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void utp_on_overhead(void* vio, bool send, size_t count, int /*type*/)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2011-02-18 00:24:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "utp_on_overhead -- count is %zu", count);
|
2011-02-18 00:40:22 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
io->bandwidth->notifyBandwidthConsumed(send ? TR_UP : TR_DOWN, count, false, tr_time_msec());
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static auto utp_function_table = UTPFunctionTable{
|
|
|
|
utp_on_read, utp_on_write, utp_get_rb_size, utp_on_state_change, utp_on_error, utp_on_overhead,
|
2011-02-18 00:24:13 +00:00
|
|
|
};
|
|
|
|
|
2011-02-18 00:24:18 +00:00
|
|
|
/* Dummy UTP callbacks. */
|
|
|
|
/* We switch a UTP socket to use these after the associated peerIo has been
|
|
|
|
destroyed -- see io_dtor. */
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void dummy_read(void* /*closure*/, unsigned char const* /*buf*/, size_t /*buflen*/)
|
2011-02-18 00:24:18 +00:00
|
|
|
{
|
2022-01-20 18:27:56 +00:00
|
|
|
// This cannot happen, as far as I'm aware. */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedError("UTP", "On_read called on closed socket");
|
2011-02-18 00:24:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void dummy_write(void* /*closure*/, unsigned char* buf, size_t buflen)
|
2011-02-18 00:24:18 +00:00
|
|
|
{
|
2011-02-18 00:24:40 +00:00
|
|
|
/* This can very well happen if we've shut down a peer connection that
|
2022-01-20 18:27:56 +00:00
|
|
|
had unflushed buffers.Complain and send zeroes.*/
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedDbg("UTP", "On_write called on closed socket");
|
|
|
|
memset(buf, 0, buflen);
|
2011-02-18 00:24:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static size_t dummy_get_rb_size(void* /*closure*/)
|
2011-02-18 00:24:18 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void dummy_on_state_change(void* /*closure*/, int /*state*/)
|
2011-02-18 00:24:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void dummy_on_error(void* /*closure*/, int /*errcode*/)
|
2011-02-18 00:24:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void dummy_on_overhead(void* /*closure*/, bool /*send*/, size_t /*count*/, int /*type*/)
|
2011-02-18 00:24:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static auto dummy_utp_function_table = UTPFunctionTable{
|
|
|
|
dummy_read, dummy_write, dummy_get_rb_size, dummy_on_state_change, dummy_on_error, dummy_on_overhead,
|
2011-02-18 00:24:18 +00:00
|
|
|
};
|
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif /* #ifdef WITH_UTP */
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_peerIo* tr_peerIoNew(
|
|
|
|
tr_session* session,
|
2021-10-10 01:12:03 +00:00
|
|
|
Bandwidth* parent,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_address const* addr,
|
|
|
|
tr_port port,
|
2021-12-21 22:14:15 +00:00
|
|
|
tr_sha1_digest_t const* torrent_hash,
|
2021-12-25 17:22:12 +00:00
|
|
|
bool is_incoming,
|
2021-08-15 09:41:48 +00:00
|
|
|
bool isSeed,
|
|
|
|
struct tr_peer_socket const socket)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(session->events != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_amInEventThread(session));
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
TR_ASSERT(socket.type == TR_PEER_SOCKET_TYPE_TCP || socket.type == TR_PEER_SOCKET_TYPE_UTP);
|
|
|
|
#else
|
|
|
|
TR_ASSERT(socket.type == TR_PEER_SOCKET_TYPE_TCP);
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif
|
2009-01-28 19:35:39 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (socket.type == TR_PEER_SOCKET_TYPE_TCP)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-10 21:35:28 +00:00
|
|
|
session->setSocketTOS(socket.handle.tcp, addr->type);
|
2021-11-14 06:41:44 +00:00
|
|
|
maybeSetCongestionAlgorithm(socket.handle.tcp, session->peerCongestionAlgorithm());
|
2010-04-22 01:49:16 +00:00
|
|
|
}
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2021-12-25 17:22:12 +00:00
|
|
|
auto* io = new tr_peerIo{ session, torrent_hash, is_incoming, *addr, port, isSeed };
|
2008-09-17 19:44:24 +00:00
|
|
|
io->socket = socket;
|
2021-10-10 01:12:03 +00:00
|
|
|
io->bandwidth = new Bandwidth(parent);
|
2021-10-09 12:52:09 +00:00
|
|
|
io->bandwidth->setPeer(io);
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "bandwidth is %p; its parent is %p", (void*)&io->bandwidth, (void*)parent);
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
switch (socket.type)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
|
|
|
dbgmsg(io, "socket (tcp) is %" PRIdMAX, (intmax_t)socket.handle.tcp);
|
|
|
|
io->event_read = event_new(session->event_base, socket.handle.tcp, EV_READ, event_read_cb, io);
|
|
|
|
io->event_write = event_new(session->event_base, socket.handle.tcp, EV_WRITE, event_write_cb, io);
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#ifdef WITH_UTP
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
|
|
|
dbgmsg(io, "socket (utp) is %p", (void*)socket.handle.utp);
|
|
|
|
UTP_SetSockopt(socket.handle.utp, SO_RCVBUF, UTP_READ_BUFFER_SIZE);
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "%s", "calling UTP_SetCallbacks &utp_function_table");
|
2017-06-28 15:46:06 +00:00
|
|
|
UTP_SetCallbacks(socket.handle.utp, &utp_function_table, io);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-25 17:22:12 +00:00
|
|
|
if (!is_incoming)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
dbgmsg(io, "%s", "calling UTP_Connect");
|
2017-06-28 15:46:06 +00:00
|
|
|
UTP_Connect(socket.handle.utp);
|
2011-02-18 00:40:22 +00:00
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif
|
2011-02-18 00:23:51 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
default:
|
|
|
|
TR_ASSERT_MSG(false, "unsupported peer socket type %d", socket.type);
|
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
return io;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_peerIo* tr_peerIoNewIncoming(
|
|
|
|
tr_session* session,
|
2021-10-10 01:12:03 +00:00
|
|
|
Bandwidth* parent,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_address const* addr,
|
|
|
|
tr_port port,
|
2017-06-28 15:46:06 +00:00
|
|
|
struct tr_peer_socket const socket)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_peerIoNew(session, parent, addr, port, nullptr, true, false, socket);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_peerIo* tr_peerIoNewOutgoing(
|
|
|
|
tr_session* session,
|
2021-10-10 01:12:03 +00:00
|
|
|
Bandwidth* parent,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_address const* addr,
|
|
|
|
tr_port port,
|
2021-12-21 22:14:15 +00:00
|
|
|
tr_sha1_digest_t const& torrent_hash,
|
2021-08-15 09:41:48 +00:00
|
|
|
bool isSeed,
|
|
|
|
bool utp)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto socket = tr_peer_socket{};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (utp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
socket = tr_netOpenPeerUTPSocket(session, addr, port, isSeed);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 16:01:52 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (socket.type == TR_PEER_SOCKET_TYPE_NONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
socket = tr_netOpenPeerSocket(session, addr, port, isSeed);
|
2021-08-15 09:41:48 +00:00
|
|
|
dbgmsg(
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
"tr_netOpenPeerSocket returned fd %" PRIdMAX,
|
|
|
|
(intmax_t)(socket.type != TR_PEER_SOCKET_TYPE_NONE ? socket.handle.tcp : TR_BAD_SOCKET));
|
2011-02-18 00:36:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (socket.type == TR_PEER_SOCKET_TYPE_NONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-12-15 04:26:31 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
return tr_peerIoNew(session, parent, addr, port, &torrent_hash, false, isSeed, socket);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void event_enable(tr_peerIo* io, short event)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_amInEventThread(io->session));
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(io->session != nullptr);
|
|
|
|
TR_ASSERT(io->session->events != nullptr);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
bool const need_events = io->socket.type == TR_PEER_SOCKET_TYPE_TCP;
|
|
|
|
|
|
|
|
if (need_events)
|
2012-09-05 11:39:57 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(event_initialized(io->event_read));
|
|
|
|
TR_ASSERT(event_initialized(io->event_write));
|
2012-09-05 11:39:57 +00:00
|
|
|
}
|
2011-02-18 00:23:51 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_READ) != 0 && (io->pendingEvents & EV_READ) == 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "enabling ready-to-read polling");
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
event_add(io->event_read, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents |= EV_READ;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_WRITE) != 0 && (io->pendingEvents & EV_WRITE) == 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "enabling ready-to-write polling");
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
event_add(io->event_write, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents |= EV_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 21:54:16 +00:00
|
|
|
static void event_disable(tr_peerIo* io, short event)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_amInEventThread(io->session));
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(io->session != nullptr);
|
|
|
|
TR_ASSERT(io->session->events != nullptr);
|
2012-09-05 11:39:57 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
bool const need_events = io->socket.type == TR_PEER_SOCKET_TYPE_TCP;
|
|
|
|
|
|
|
|
if (need_events)
|
2012-09-05 11:39:57 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(event_initialized(io->event_read));
|
|
|
|
TR_ASSERT(event_initialized(io->event_write));
|
2012-09-05 11:39:57 +00:00
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_READ) != 0 && (io->pendingEvents & EV_READ) != 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "disabling ready-to-read polling");
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_del(io->event_read);
|
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents &= ~EV_READ;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_WRITE) != 0 && (io->pendingEvents & EV_WRITE) != 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "disabling ready-to-write polling");
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_del(io->event_write);
|
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents &= ~EV_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoSetEnabled(tr_peerIo* io, tr_direction dir, bool isEnabled)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
|
|
|
TR_ASSERT(tr_amInEventThread(io->session));
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(io->session->events != nullptr);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
short const event = dir == TR_UP ? EV_WRITE : EV_READ;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isEnabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_enable(io, event);
|
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_disable(io, event);
|
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
static void io_close_socket(tr_peerIo* io)
|
2011-02-18 00:45:44 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
switch (io->socket.type)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
|
|
|
tr_netClose(io->session, io->socket.handle.tcp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
|
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
2021-09-15 00:18:09 +00:00
|
|
|
UTP_SetCallbacks(io->socket.handle.utp, &dummy_utp_function_table, nullptr);
|
2017-06-28 15:46:06 +00:00
|
|
|
UTP_Close(io->socket.handle.utp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
TR_ASSERT_MSG(false, "unsupported peer socket type %d", io->socket.type);
|
2011-03-29 21:09:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
io->socket = {};
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->event_read != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_free(io->event_read);
|
2021-09-15 00:18:09 +00:00
|
|
|
io->event_read = nullptr;
|
2011-03-29 21:09:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->event_write != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_free(io->event_write);
|
2021-09-15 00:18:09 +00:00
|
|
|
io->event_write = nullptr;
|
2011-02-18 00:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void io_dtor(void* vio)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2009-01-26 02:51:50 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(tr_amInEventThread(io->session));
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(io->session->events != nullptr);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "in tr_peerIo destructor");
|
|
|
|
event_disable(io, EV_READ | EV_WRITE);
|
2021-10-09 12:52:09 +00:00
|
|
|
delete io->bandwidth;
|
2017-04-19 12:04:45 +00:00
|
|
|
io_close_socket(io);
|
2011-04-10 05:21:51 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
while (io->outbuf_datatypes != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
peer_io_pull_datatype(io);
|
|
|
|
}
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2021-10-11 21:54:16 +00:00
|
|
|
io->magic_number = ~0;
|
|
|
|
delete io;
|
2007-10-02 16:12:44 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_peerIoFree(tr_peerIo* io)
|
2007-10-02 16:12:44 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io != nullptr)
|
2007-10-02 16:12:44 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "in tr_peerIoFree");
|
2021-09-15 00:18:09 +00:00
|
|
|
io->canRead = nullptr;
|
|
|
|
io->didWrite = nullptr;
|
|
|
|
io->gotError = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread(io->session, io_dtor, io);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_peerIoRefImpl(char const* file, int line, tr_peerIo* io)
|
2009-01-05 04:27:54 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2009-01-05 04:27:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "%s:%d is incrementing the IO's refcount from %d to %d", file, line, io->refCount, io->refCount + 1);
|
2009-01-24 17:20:07 +00:00
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
++io->refCount;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_peerIoUnrefImpl(char const* file, int line, tr_peerIo* io)
|
2009-01-05 04:27:54 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2009-01-05 04:27:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "%s:%d is decrementing the IO's refcount from %d to %d", file, line, io->refCount, io->refCount - 1);
|
2009-01-24 17:20:07 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (--io->refCount == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_peerIoFree(io);
|
|
|
|
}
|
2009-01-05 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_address const* tr_peerIoGetAddress(tr_peerIo const* io, tr_port* port)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (port != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
*port = io->port;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
return &io->addr;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
char const* tr_peerIoGetAddrStr(tr_peerIo const* io, char* buf, size_t buflen)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2020-09-13 21:43:29 +00:00
|
|
|
if (tr_isPeerIo(io))
|
|
|
|
{
|
|
|
|
tr_address_and_port_to_string(buf, buflen, &io->addr, io->port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_strlcpy(buf, "error", buflen);
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
return buf;
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoSetIOFuncs(tr_peerIo* io, tr_can_read_cb readcb, tr_did_write_cb writecb, tr_net_error_cb errcb, void* userData)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
io->canRead = readcb;
|
2007-12-01 23:08:34 +00:00
|
|
|
io->didWrite = writecb;
|
2007-09-20 16:32:01 +00:00
|
|
|
io->gotError = errcb;
|
|
|
|
io->userData = userData;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoClear(tr_peerIo* io)
|
2009-01-05 18:20:47 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_peerIoSetIOFuncs(io, nullptr, nullptr, nullptr, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoSetEnabled(io, TR_UP, false);
|
|
|
|
tr_peerIoSetEnabled(io, TR_DOWN, false);
|
2009-01-05 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_peerIoReconnect(tr_peerIo* io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(!tr_peerIoIsIncoming(io));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_session* session = tr_peerIoGetSession(io);
|
2009-10-23 03:41:36 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
short int pendingEvents = io->pendingEvents;
|
2017-04-19 12:04:45 +00:00
|
|
|
event_disable(io, EV_READ | EV_WRITE);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
io_close_socket(io);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
io->socket = tr_netOpenPeerSocket(session, &io->addr, io->port, io->isSeed);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (io->socket.type != TR_PEER_SOCKET_TYPE_TCP)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
return -1;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
io->event_read = event_new(session->event_base, io->socket.handle.tcp, EV_READ, event_read_cb, io);
|
|
|
|
io->event_write = event_new(session->event_base, io->socket.handle.tcp, EV_WRITE, event_write_cb, io);
|
|
|
|
|
|
|
|
event_enable(io, pendingEvents);
|
2022-02-10 21:35:28 +00:00
|
|
|
io->session->setSocketTOS(io->socket.handle.tcp, io->addr.type);
|
2021-11-14 06:41:44 +00:00
|
|
|
maybeSetCongestionAlgorithm(io->socket.handle.tcp, session->peerCongestionAlgorithm());
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
void tr_peerIoSetTorrentHash(tr_peerIo* io, tr_sha1_digest_t const& info_hash)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
tr_cryptoSetTorrentHash(&io->crypto, info_hash);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
std::optional<tr_sha1_digest_t> tr_peerIoGetTorrentHash(tr_peerIo const* io)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_cryptoGetTorrentHash(&io->crypto);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static unsigned int getDesiredOutputBufferSize(tr_peerIo const* io, uint64_t now)
|
2008-11-28 16:00:29 +00:00
|
|
|
{
|
|
|
|
/* this is all kind of arbitrary, but what seems to work well is
|
2008-12-15 21:22:08 +00:00
|
|
|
* being large enough to hold the next 20 seconds' worth of input,
|
|
|
|
* or a few blocks, whichever is bigger.
|
2008-11-28 16:00:29 +00:00
|
|
|
* It's okay to tweak this as needed */
|
2021-10-12 06:04:22 +00:00
|
|
|
unsigned int const currentSpeed_Bps = io->bandwidth->getPieceSpeedBytesPerSecond(now, TR_UP);
|
2019-03-17 04:07:48 +00:00
|
|
|
unsigned int const period = 15U; /* arbitrary */
|
2010-07-03 00:25:22 +00:00
|
|
|
/* the 3 is arbitrary; the .5 is to leave room for messages */
|
2021-09-19 20:41:35 +00:00
|
|
|
static auto const ceiling = (unsigned int)(MAX_BLOCK_SIZE * 3.5);
|
|
|
|
return std::max(ceiling, currentSpeed_Bps * period);
|
2008-11-28 16:00:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t tr_peerIoGetWriteBufferSpace(tr_peerIo const* io, uint64_t now)
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const desiredLen = getDesiredOutputBufferSize(io, now);
|
|
|
|
size_t const currentLen = evbuffer_get_length(io->outbuf);
|
2008-11-24 04:21:23 +00:00
|
|
|
size_t freeSpace = 0;
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (desiredLen > currentLen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-09-17 19:44:24 +00:00
|
|
|
freeSpace = desiredLen - currentLen;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
|
|
|
return freeSpace;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoSetEncryption(tr_peerIo* io, tr_encryption_type encryption_type)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(encryption_type == PEER_ENCRYPTION_NONE || encryption_type == PEER_ENCRYPTION_RC4);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2011-03-31 16:41:52 +00:00
|
|
|
io->encryption_type = encryption_type;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static inline void processBuffer(
|
|
|
|
tr_crypto* crypto,
|
|
|
|
struct evbuffer* buffer,
|
|
|
|
size_t offset,
|
|
|
|
size_t size,
|
|
|
|
void (*callback)(tr_crypto*, size_t, void const*, void*))
|
2015-06-24 21:24:41 +00:00
|
|
|
{
|
|
|
|
struct evbuffer_ptr pos;
|
|
|
|
struct evbuffer_iovec iovec;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_ptr_set(buffer, &pos, offset, EVBUFFER_PTR_SET);
|
2015-06-24 21:24:41 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (evbuffer_peek(buffer, size, &pos, &iovec, 1) <= 0)
|
|
|
|
{
|
2015-06-24 21:24:41 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-06-24 21:24:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
callback(crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base);
|
2015-06-24 21:24:41 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(size >= iovec.iov_len);
|
2015-06-24 21:24:41 +00:00
|
|
|
size -= iovec.iov_len;
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (evbuffer_ptr_set(buffer, &pos, iovec.iov_len, EVBUFFER_PTR_ADD) == 0);
|
2015-06-24 21:24:41 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(size == 0);
|
2015-06-24 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void addDatatype(tr_peerIo* io, size_t byteCount, bool isPieceData)
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto* const d = datatype_new();
|
2013-08-24 18:08:38 +00:00
|
|
|
d->isPieceData = isPieceData;
|
2010-12-20 02:07:51 +00:00
|
|
|
d->length = byteCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
peer_io_push_datatype(io, d);
|
2010-12-20 02:07:51 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline void maybeEncryptBuffer(tr_peerIo* io, struct evbuffer* buf, size_t offset, size_t size)
|
2010-12-20 02:07:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (io->encryption_type == PEER_ENCRYPTION_RC4)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
processBuffer(&io->crypto, buf, offset, size, &tr_cryptoEncrypt);
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoWriteBuf(tr_peerIo* io, struct evbuffer* buf, bool isPieceData)
|
2009-01-22 14:32:29 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const byteCount = evbuffer_get_length(buf);
|
2017-04-19 12:04:45 +00:00
|
|
|
maybeEncryptBuffer(io, buf, 0, byteCount);
|
|
|
|
evbuffer_add_buffer(io->outbuf, buf);
|
|
|
|
addDatatype(io, byteCount, isPieceData);
|
2009-01-22 14:32:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_peerIoWriteBytes(tr_peerIo* io, void const* bytes, size_t byteCount, bool isPieceData)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2011-04-27 17:52:28 +00:00
|
|
|
struct evbuffer_iovec iovec;
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_reserve_space(io->outbuf, byteCount, &iovec, 1);
|
2011-04-27 17:52:28 +00:00
|
|
|
|
|
|
|
iovec.iov_len = byteCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (io->encryption_type == PEER_ENCRYPTION_RC4)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_cryptoEncrypt(&io->crypto, iovec.iov_len, bytes, iovec.iov_base);
|
|
|
|
}
|
2011-04-27 17:52:28 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
memcpy(iovec.iov_base, bytes, iovec.iov_len);
|
|
|
|
}
|
2011-04-27 17:52:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_commit_space(io->outbuf, &iovec, 1);
|
|
|
|
|
|
|
|
addDatatype(io, byteCount, isPieceData);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-09-17 04:21:50 +00:00
|
|
|
void evbuffer_add_uint8(struct evbuffer* outbuf, uint8_t addme)
|
2011-02-09 02:35:16 +00:00
|
|
|
{
|
2021-09-17 04:21:50 +00:00
|
|
|
evbuffer_add(outbuf, &addme, 1);
|
2011-02-09 02:35:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void evbuffer_add_uint16(struct evbuffer* outbuf, uint16_t addme_hs)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
uint16_t const ns = htons(addme_hs);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(outbuf, &ns, sizeof(ns));
|
2010-12-20 02:07:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void evbuffer_add_uint32(struct evbuffer* outbuf, uint32_t addme_hl)
|
2010-12-20 02:07:51 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
uint32_t const nl = htonl(addme_hl);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(outbuf, &nl, sizeof(nl));
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void evbuffer_add_uint64(struct evbuffer* outbuf, uint64_t addme_hll)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t const nll = tr_htonll(addme_hll);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(outbuf, &nll, sizeof(nll));
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
|
|
|
|
2007-11-17 17:49:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline void maybeDecryptBuffer(tr_peerIo* io, struct evbuffer* buf, size_t offset, size_t size)
|
2015-06-24 21:24:41 +00:00
|
|
|
{
|
|
|
|
if (io->encryption_type == PEER_ENCRYPTION_RC4)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
processBuffer(&io->crypto, buf, offset, size, &tr_cryptoDecrypt);
|
|
|
|
}
|
2015-06-24 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoReadBytesToBuf(tr_peerIo* io, struct evbuffer* inbuf, struct evbuffer* outbuf, size_t byteCount)
|
2011-04-04 04:45:41 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(evbuffer_get_length(inbuf) >= byteCount);
|
2011-04-04 04:45:41 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
size_t const old_length = evbuffer_get_length(outbuf);
|
|
|
|
|
2011-04-04 04:45:41 +00:00
|
|
|
/* append it to outbuf */
|
2017-06-13 02:24:09 +00:00
|
|
|
struct evbuffer* tmp = evbuffer_new();
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_remove_buffer(inbuf, tmp, byteCount);
|
|
|
|
evbuffer_add_buffer(outbuf, tmp);
|
|
|
|
evbuffer_free(tmp);
|
2011-04-04 04:45:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
maybeDecryptBuffer(io, outbuf, old_length, byteCount);
|
2011-04-04 04:45:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoReadBytes(tr_peerIo* io, struct evbuffer* inbuf, void* bytes, size_t byteCount)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(evbuffer_get_length(inbuf) >= byteCount);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (io->encryption_type)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case PEER_ENCRYPTION_NONE:
|
|
|
|
evbuffer_remove(inbuf, bytes, byteCount);
|
|
|
|
break;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case PEER_ENCRYPTION_RC4:
|
|
|
|
evbuffer_remove(inbuf, bytes, byteCount);
|
|
|
|
tr_cryptoDecrypt(&io->crypto, byteCount, bytes, bytes);
|
|
|
|
break;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT_MSG(false, "unhandled encryption type %d", (int)io->encryption_type);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoReadUint16(tr_peerIo* io, struct evbuffer* inbuf, uint16_t* setme)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto tmp = uint16_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoReadBytes(io, inbuf, &tmp, sizeof(uint16_t));
|
|
|
|
*setme = ntohs(tmp);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoReadUint32(tr_peerIo* io, struct evbuffer* inbuf, uint32_t* setme)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto tmp = uint32_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoReadBytes(io, inbuf, &tmp, sizeof(uint32_t));
|
|
|
|
*setme = ntohl(tmp);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_peerIoDrain(tr_peerIo* io, struct evbuffer* inbuf, size_t byteCount)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2011-04-04 04:45:41 +00:00
|
|
|
char buf[4096];
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const buflen = sizeof(buf);
|
2008-12-21 18:15:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while (byteCount > 0)
|
2008-12-29 19:01:47 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
size_t const thisPass = std::min(byteCount, buflen);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerIoReadBytes(io, inbuf, buf, thisPass);
|
2008-12-29 19:01:47 +00:00
|
|
|
byteCount -= thisPass;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-01-11 02:09:20 +00:00
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto res = int{};
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if ((howmuch = io->bandwidth->clamp(TR_DOWN, howmuch)) != 0)
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
switch (io->socket.type)
|
2011-02-18 00:39:33 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
2011-02-18 00:40:38 +00:00
|
|
|
/* UTP_RBDrained notifies libutp that your read buffer is emtpy.
|
|
|
|
* It opens up the congestion window by sending an ACK (soonish)
|
|
|
|
* if one was not going to be sent. */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (evbuffer_get_length(io->inbuf) == 0)
|
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
UTP_RBDrained(io->socket.handle.utp);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-30 21:24:36 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
break;
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
char err_buf[512];
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
|
|
|
res = evbuffer_read(io->inbuf, io->socket.handle.tcp, (int)howmuch);
|
2021-10-22 13:51:36 +00:00
|
|
|
int const e = EVUTIL_SOCKET_ERROR();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
dbgmsg(io, "read %d from peer (%s)", res, res == -1 ? tr_net_strerror(err_buf, sizeof(err_buf), e) : "");
|
|
|
|
|
2017-07-02 10:26:26 +00:00
|
|
|
if (evbuffer_get_length(io->inbuf) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
canReadWrapper(io);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (res <= 0 && io->gotError != nullptr && e != EAGAIN && e != EINTR && e != EINPROGRESS)
|
2017-06-28 15:46:06 +00:00
|
|
|
{
|
|
|
|
short what = BEV_EVENT_READING | BEV_EVENT_ERROR;
|
|
|
|
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
what |= BEV_EVENT_EOF;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
dbgmsg(
|
|
|
|
io,
|
|
|
|
"tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)",
|
|
|
|
res,
|
|
|
|
what,
|
|
|
|
e,
|
2017-06-28 15:46:06 +00:00
|
|
|
tr_net_strerror(err_buf, sizeof(err_buf), e));
|
2017-04-30 16:25:26 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
io->gotError(io, what, io->userData);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2011-02-18 00:39:33 +00:00
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
TR_ASSERT_MSG(false, "unsupported peer socket type %d", io->socket.type);
|
2008-12-24 02:50:08 +00:00
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch)
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto const old_len = size_t{ evbuffer_get_length(io->outbuf) };
|
|
|
|
auto n = int{};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(io, "in tr_peerIoTryWrite %zu", howmuch);
|
2011-02-18 00:40:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (howmuch > old_len)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-18 00:40:01 +00:00
|
|
|
howmuch = old_len;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if ((howmuch = io->bandwidth->clamp(TR_UP, howmuch)) != 0)
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
switch (io->socket.type)
|
2011-02-18 00:39:33 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
|
|
|
UTP_Write(io->socket.handle.utp, howmuch);
|
2017-04-19 12:04:45 +00:00
|
|
|
n = old_len - evbuffer_get_length(io->outbuf);
|
2017-06-28 15:46:06 +00:00
|
|
|
break;
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
|
|
|
n = tr_evbuffer_write(io, io->socket.handle.tcp, howmuch);
|
2021-10-22 13:51:36 +00:00
|
|
|
int const e = EVUTIL_SOCKET_ERROR();
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
didWriteWrapper(io, n);
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (n < 0 && io->gotError != nullptr && e != 0 && e != EPIPE && e != EAGAIN && e != EINTR && e != EINPROGRESS)
|
2017-06-28 15:46:06 +00:00
|
|
|
{
|
|
|
|
char errstr[512];
|
|
|
|
short const what = BEV_EVENT_WRITING | BEV_EVENT_ERROR;
|
2010-03-02 04:51:40 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
dbgmsg(
|
|
|
|
io,
|
|
|
|
"tr_peerIoTryWrite got an error. res is %d, what is %hd, errno is %d (%s)",
|
|
|
|
n,
|
|
|
|
what,
|
|
|
|
e,
|
2017-06-28 15:46:06 +00:00
|
|
|
tr_net_strerror(errstr, sizeof(errstr), e));
|
2011-02-18 00:39:33 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
io->gotError(io, what, io->userData);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2011-02-18 00:39:33 +00:00
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
TR_ASSERT_MSG(false, "unsupported peer socket type %d", io->socket.type);
|
2008-12-24 02:50:08 +00:00
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_peerIoFlush(tr_peerIo* io, tr_direction dir, size_t limit)
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
int bytesUsed = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (dir == TR_DOWN)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
bytesUsed = tr_peerIoTryRead(io, limit);
|
|
|
|
}
|
2011-01-13 01:58:57 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
bytesUsed = tr_peerIoTryWrite(io, limit);
|
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
dbgmsg(io, "flushing peer-io, direction %d, limit %zu, notifyBandwidthConsumedBytes %d", (int)dir, limit, bytesUsed);
|
2009-01-02 19:56:06 +00:00
|
|
|
return bytesUsed;
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_peerIoFlushOutgoingProtocolMsgs(tr_peerIo* io)
|
2009-04-21 16:18:51 +00:00
|
|
|
{
|
|
|
|
size_t byteCount = 0;
|
|
|
|
|
|
|
|
/* count up how many bytes are used by non-piece-data messages
|
|
|
|
at the front of our outbound queue */
|
2021-09-15 00:18:09 +00:00
|
|
|
for (struct tr_datatype const* it = io->outbuf_datatypes; it != nullptr; it = it->next)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (it->isPieceData)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-04-21 16:18:51 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
|
|
|
byteCount += it->length;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-21 16:18:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_peerIoFlush(io, TR_UP, byteCount);
|
2009-04-21 16:18:51 +00:00
|
|
|
}
|