2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
2011-01-19 13:48:47 +00:00
|
|
|
* Copyright (c) Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2021-11-15 23:03:55 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstring>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <ws2tcpip.h>
|
2011-04-02 16:43:17 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <netinet/tcp.h> /* TCP_CONGESTION */
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
#include <event2/util.h>
|
2011-03-24 22:57:39 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstdint>
|
2012-05-30 17:47:29 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "fdlimit.h" /* tr_fdSocketClose() */
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "log.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2020-09-13 21:43:29 +00:00
|
|
|
#include "peer-socket.h" /* for struct tr_peer_socket */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "session.h" /* tr_sessionGetPublicAddress() */
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2020-08-18 11:09:30 +00:00
|
|
|
#include "tr-macros.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "tr-utp.h" /* tr_utpSendTo() */
|
|
|
|
#include "utils.h" /* tr_time(), tr_logAddDebug() */
|
2007-07-09 20:10:42 +00:00
|
|
|
|
2009-01-02 21:45:55 +00:00
|
|
|
#ifndef IN_MULTICAST
|
2021-08-15 09:41:48 +00:00
|
|
|
#define IN_MULTICAST(a) (((a)&0xf0000000) == 0xe0000000)
|
2009-01-02 21:45:55 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_address const tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } };
|
2017-06-24 10:17:04 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_address const tr_inaddr_any = { TR_AF_INET, { { { { INADDR_ANY } } } } };
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tr_net_strerror(char* buf, size_t buflen, int err)
|
2010-06-30 21:24:36 +00:00
|
|
|
{
|
|
|
|
*buf = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, 0, buf, buflen, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2015-07-01 00:54:41 +00:00
|
|
|
while (len > 0 && buf[len - 1] >= '\0' && buf[len - 1] <= ' ')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
buf[--len] = '\0';
|
|
|
|
}
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_strlcpy(buf, tr_strerror(err), buflen);
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
char const* tr_address_and_port_to_string(char* buf, size_t buflen, tr_address const* addr, tr_port port)
|
|
|
|
{
|
|
|
|
char addr_buf[INET6_ADDRSTRLEN];
|
|
|
|
tr_address_to_string_with_buf(addr, addr_buf, sizeof(addr_buf));
|
|
|
|
tr_snprintf(buf, buflen, "[%s]:%u", addr_buf, ntohs(port));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_address_to_string_with_buf(tr_address const* addr, char* buf, size_t buflen)
|
2008-12-16 21:06:47 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2021-10-14 19:26:38 +00:00
|
|
|
return addr->type == TR_AF_INET ? evutil_inet_ntop(AF_INET, &addr->addr, buf, buflen) :
|
|
|
|
evutil_inet_ntop(AF_INET6, &addr->addr, buf, buflen);
|
2009-08-10 20:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-04-21 07:40:57 +00:00
|
|
|
* Non-threadsafe version of tr_address_to_string_with_buf()
|
2011-03-25 05:34:26 +00:00
|
|
|
* and uses a static memory area for a buffer.
|
2009-08-10 20:04:08 +00:00
|
|
|
* This function is suitable to be called from libTransmission's networking code,
|
|
|
|
* which is single-threaded.
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_address_to_string(tr_address const* addr)
|
2009-08-10 20:04:08 +00:00
|
|
|
{
|
|
|
|
static char buf[INET6_ADDRSTRLEN];
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_address_to_string_with_buf(addr, buf, sizeof(buf));
|
2009-08-10 20:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_address_from_string(tr_address* dst, char const* src)
|
2009-08-10 20:04:08 +00:00
|
|
|
{
|
2020-08-27 23:41:26 +00:00
|
|
|
if (evutil_inet_pton(AF_INET, src, &dst->addr) == 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-08-10 20:04:08 +00:00
|
|
|
dst->type = TR_AF_INET;
|
2021-11-15 23:03:55 +00:00
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-11-15 23:03:55 +00:00
|
|
|
|
|
|
|
if (evutil_inet_pton(AF_INET6, src, &dst->addr) == 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-08-27 23:41:26 +00:00
|
|
|
dst->type = TR_AF_INET6;
|
2021-11-15 23:03:55 +00:00
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-25 05:34:26 +00:00
|
|
|
|
2021-11-15 23:03:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_address_from_string(tr_address* dst, std::string_view src)
|
|
|
|
{
|
|
|
|
// inet_pton() requires zero-terminated strings,
|
|
|
|
// so make a zero-terminated copy here on the stack.
|
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
if (std::size(src) >= std::size(buf))
|
|
|
|
{
|
|
|
|
// shouldn't ever be that large; malformed address
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*std::copy(std::begin(src), std::end(src), std::begin(buf)) = '\0';
|
|
|
|
|
|
|
|
return tr_address_from_string(dst, std::data(buf));
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
/*
|
|
|
|
* Compare two tr_address structures.
|
|
|
|
* Returns:
|
|
|
|
* <0 if a < b
|
|
|
|
* >0 if a > b
|
|
|
|
* 0 if a == b
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_address_compare(tr_address const* a, tr_address const* b)
|
2008-12-16 06:46:27 +00:00
|
|
|
{
|
2020-11-04 03:23:53 +00:00
|
|
|
// IPv6 addresses are always "greater than" IPv4
|
2012-12-05 17:29:46 +00:00
|
|
|
if (a->type != b->type)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-12-16 06:46:27 +00:00
|
|
|
return a->type == TR_AF_INET ? 1 : -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
return a->type == TR_AF_INET ? memcmp(&a->addr.addr4, &b->addr.addr4, sizeof(a->addr.addr4)) :
|
|
|
|
memcmp(&a->addr.addr6.s6_addr, &b->addr.addr6.s6_addr, sizeof(a->addr.addr6.s6_addr));
|
2009-08-10 20:04:08 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
2008-05-28 16:56:38 +00:00
|
|
|
* TCP sockets
|
2006-07-16 19:39:23 +00:00
|
|
|
**********************************************************************/
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
void tr_netSetTOS([[maybe_unused]] tr_socket_t s, [[maybe_unused]] int tos, tr_address_type type)
|
2008-04-12 00:29:49 +00:00
|
|
|
{
|
2017-08-02 10:01:39 +00:00
|
|
|
if (type == TR_AF_INET)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(IP_TOS) && !defined(_WIN32)
|
|
|
|
|
2017-08-02 10:01:39 +00:00
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_TOS, (void const*)&tos, sizeof(tos)) == -1)
|
|
|
|
{
|
|
|
|
char err_buf[512];
|
|
|
|
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno);
|
|
|
|
tr_logAddNamedInfo("Net", "Can't set TOS '%d': %s", tos, err_buf);
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-01 00:54:41 +00:00
|
|
|
}
|
2017-08-02 10:01:39 +00:00
|
|
|
else if (type == TR_AF_INET6)
|
|
|
|
{
|
|
|
|
#if defined(IPV6_TCLASS) && !defined(_WIN32)
|
2019-07-21 11:09:04 +00:00
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, (void const*)&tos, sizeof(tos)) == -1)
|
2017-08-02 10:01:39 +00:00
|
|
|
{
|
|
|
|
char err_buf[512];
|
|
|
|
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno);
|
2019-07-21 11:09:04 +00:00
|
|
|
tr_logAddNamedInfo("Net", "Can't set IPv6 QoS '%d': %s", tos, err_buf);
|
2017-08-02 10:01:39 +00:00
|
|
|
}
|
2008-04-12 00:29:49 +00:00
|
|
|
#endif
|
2017-08-02 10:01:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* program should never reach here! */
|
|
|
|
tr_logAddNamedInfo("Net", "Something goes wrong while setting TOS/Traffic-Class");
|
|
|
|
}
|
2008-04-12 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
void tr_netSetCongestionControl([[maybe_unused]] tr_socket_t s, [[maybe_unused]] char const* algorithm)
|
2010-04-22 01:49:16 +00:00
|
|
|
{
|
|
|
|
#ifdef TCP_CONGESTION
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_CONGESTION, (void const*)algorithm, strlen(algorithm) + 1) == -1)
|
2015-07-01 00:54:41 +00:00
|
|
|
{
|
|
|
|
char err_buf[512];
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddNamedInfo(
|
|
|
|
"Net",
|
|
|
|
"Can't set congestion control algorithm '%s': %s",
|
|
|
|
algorithm,
|
|
|
|
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
|
2015-07-01 00:54:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-22 01:49:16 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_address_from_sockaddr_storage(tr_address* setme_addr, tr_port* setme_port, struct sockaddr_storage const* from)
|
2011-03-04 21:00:52 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (from->ss_family == AF_INET)
|
2011-03-04 21:00:52 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
struct sockaddr_in const* sin = (struct sockaddr_in const*)from;
|
2011-03-04 21:00:52 +00:00
|
|
|
setme_addr->type = TR_AF_INET;
|
|
|
|
setme_addr->addr.addr4.s_addr = sin->sin_addr.s_addr;
|
|
|
|
*setme_port = sin->sin_port;
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2011-03-04 21:00:52 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (from->ss_family == AF_INET6)
|
2011-03-04 21:00:52 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
struct sockaddr_in6 const* sin6 = (struct sockaddr_in6 const*)from;
|
2011-03-04 21:00:52 +00:00
|
|
|
setme_addr->type = TR_AF_INET6;
|
|
|
|
setme_addr->addr.addr6 = sin6->sin6_addr;
|
|
|
|
*setme_port = sin6->sin6_port;
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2011-03-04 21:00:52 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2011-03-04 21:00:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static socklen_t setup_sockaddr(tr_address const* addr, tr_port port, struct sockaddr_storage* sockaddr)
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (addr->type == TR_AF_INET)
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2021-10-14 19:26:38 +00:00
|
|
|
sockaddr_in sock4 = {};
|
2017-04-19 12:04:45 +00:00
|
|
|
sock4.sin_family = AF_INET;
|
2008-12-15 00:17:08 +00:00
|
|
|
sock4.sin_addr.s_addr = addr->addr.addr4.s_addr;
|
2017-04-19 12:04:45 +00:00
|
|
|
sock4.sin_port = port;
|
|
|
|
memcpy(sockaddr, &sock4, sizeof(sock4));
|
|
|
|
return sizeof(struct sockaddr_in);
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
|
|
|
sockaddr_in6 sock6 = {};
|
|
|
|
sock6.sin6_family = AF_INET6;
|
|
|
|
sock6.sin6_port = port;
|
|
|
|
sock6.sin6_flowinfo = 0;
|
|
|
|
sock6.sin6_addr = addr->addr.addr6;
|
|
|
|
memcpy(sockaddr, &sock6, sizeof(sock6));
|
|
|
|
return sizeof(struct sockaddr_in6);
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
2008-12-02 03:41:58 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const* addr, tr_port port, bool clientIsSeed)
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto ret = tr_peer_socket{};
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int const domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
|
2008-12-15 00:17:08 +00:00
|
|
|
struct sockaddr_storage sock;
|
2009-04-15 21:05:58 +00:00
|
|
|
struct sockaddr_storage source_sock;
|
2015-07-01 00:54:41 +00:00
|
|
|
char err_buf[512];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_address_is_valid_for_peers(addr, port))
|
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
auto const s = tr_fdSocketCreate(session, domains[addr->type], SOCK_STREAM);
|
2015-03-18 07:34:26 +00:00
|
|
|
if (s == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-12-02 05:30:46 +00:00
|
|
|
/* seeds don't need much of a read buffer... */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (clientIsSeed)
|
|
|
|
{
|
2009-12-02 20:03:27 +00:00
|
|
|
int n = 8192;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&n), sizeof(n)) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddInfo(
|
|
|
|
"Unable to set SO_RCVBUF on socket %" PRIdMAX ": %s",
|
|
|
|
(intmax_t)s,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
|
|
|
|
}
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (evutil_make_socket_nonblocking(s) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_netClose(session, s);
|
2017-06-28 15:46:06 +00:00
|
|
|
return ret;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
2008-11-21 16:32:55 +00:00
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
socklen_t const addrlen = setup_sockaddr(addr, port, &sock);
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2009-04-15 21:05:58 +00:00
|
|
|
/* set source address */
|
2021-10-24 17:40:15 +00:00
|
|
|
tr_address const* const source_addr = tr_sessionGetPublicAddress(session, addr->type, nullptr);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(source_addr != nullptr);
|
2021-10-24 17:40:15 +00:00
|
|
|
socklen_t const sourcelen = setup_sockaddr(source_addr, 0, &source_sock);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (bind(s, (struct sockaddr*)&source_sock, sourcelen) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddError(
|
|
|
|
_("Couldn't set source address %s on %" PRIdMAX ": %s"),
|
|
|
|
tr_address_to_string(source_addr),
|
|
|
|
(intmax_t)s,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
|
|
|
|
tr_netClose(session, s);
|
2017-06-28 15:46:06 +00:00
|
|
|
return ret;
|
2009-04-15 21:05:58 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (connect(s, (struct sockaddr*)&sock, addrlen) == -1 &&
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-30 16:25:26 +00:00
|
|
|
sockerrno != WSAEWOULDBLOCK &&
|
2007-08-02 23:33:40 +00:00
|
|
|
#endif
|
2017-04-30 16:25:26 +00:00
|
|
|
sockerrno != EINPROGRESS)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
int const tmperrno = sockerrno;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2015-07-01 00:54:41 +00:00
|
|
|
if ((tmperrno != ENETUNREACH && tmperrno != EHOSTUNREACH) || addr->type == TR_AF_INET)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddError(
|
|
|
|
_("Couldn't connect socket %" PRIdMAX " to %s, port %d (errno %d - %s)"),
|
|
|
|
(intmax_t)s,
|
|
|
|
tr_address_to_string(addr),
|
|
|
|
(int)ntohs(port),
|
|
|
|
tmperrno,
|
|
|
|
tr_net_strerror(err_buf, sizeof(err_buf), tmperrno));
|
2015-07-01 00:54:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_netClose(session, s);
|
2017-06-28 15:46:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = tr_peer_socket_tcp_create(s);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 17:31:36 +00:00
|
|
|
if (tr_logGetDeepEnabled())
|
|
|
|
{
|
2020-09-13 21:43:29 +00:00
|
|
|
char addrstr[TR_ADDRSTRLEN];
|
|
|
|
tr_address_and_port_to_string(addrstr, sizeof(addrstr), addr, port);
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_logAddDeep(__FILE__, __LINE__, nullptr, "New OUTGOING connection %" PRIdMAX " (%s)", (intmax_t)s, addrstr);
|
2020-07-28 17:31:36 +00:00
|
|
|
}
|
2008-08-16 21:06:57 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-07-31 14:26:44 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
struct tr_peer_socket tr_netOpenPeerUTPSocket(tr_session* session, tr_address const* addr, tr_port port, bool /*clientIsSeed*/)
|
2011-02-18 00:36:19 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto ret = tr_peer_socket{};
|
2012-12-05 23:48:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_address_is_valid_for_peers(addr, port))
|
2012-12-06 00:50:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct sockaddr_storage ss;
|
2017-04-20 16:02:19 +00:00
|
|
|
socklen_t const sslen = setup_sockaddr(addr, port, &ss);
|
2017-06-28 15:46:06 +00:00
|
|
|
struct UTPSocket* const socket = UTP_Create(tr_utpSendTo, session, (struct sockaddr*)&ss, sslen);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (socket != nullptr)
|
2017-06-28 15:46:06 +00:00
|
|
|
{
|
|
|
|
ret = tr_peer_socket_utp_create(socket);
|
|
|
|
}
|
2012-12-06 00:50:40 +00:00
|
|
|
}
|
2011-02-18 00:36:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2011-02-18 00:36:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static tr_socket_t tr_netBindTCPImpl(tr_address const* addr, tr_port port, bool suppressMsgs, int* errOut)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int const domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
|
2008-12-15 00:17:08 +00:00
|
|
|
struct sockaddr_storage sock;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
tr_socket_t const fd = socket(domains[addr->type], SOCK_STREAM, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd == TR_BAD_SOCKET)
|
|
|
|
{
|
2009-07-09 18:14:33 +00:00
|
|
|
*errOut = sockerrno;
|
2015-03-18 07:34:26 +00:00
|
|
|
return TR_BAD_SOCKET;
|
2009-07-09 18:14:33 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (evutil_make_socket_nonblocking(fd) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-07-09 18:14:33 +00:00
|
|
|
*errOut = sockerrno;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_netCloseSocket(fd);
|
2015-03-18 07:34:26 +00:00
|
|
|
return TR_BAD_SOCKET;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
int optval = 1;
|
2021-09-12 17:41:49 +00:00
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<char const*>(&optval), sizeof(optval));
|
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char const*>(&optval), sizeof(optval));
|
2008-12-01 20:21:06 +00:00
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
#ifdef IPV6_V6ONLY
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-01 21:47:57 +00:00
|
|
|
if ((addr->type == TR_AF_INET6) &&
|
2021-09-12 17:41:49 +00:00
|
|
|
(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char const*>(&optval), sizeof(optval)) == -1) &&
|
2020-11-01 21:47:57 +00:00
|
|
|
(sockerrno != ENOPROTOOPT)) // if the kernel doesn't support it, ignore it
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
*errOut = sockerrno;
|
|
|
|
tr_netCloseSocket(fd);
|
|
|
|
return TR_BAD_SOCKET;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2009-02-07 00:34:10 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
int const addrlen = setup_sockaddr(addr, htons(port), &sock);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (bind(fd, (struct sockaddr*)&sock, addrlen) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const err = sockerrno;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!suppressMsgs)
|
2010-02-06 14:43:28 +00:00
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
char const* const hint = err == EADDRINUSE ? _("Is another copy of Transmission already running?") : nullptr;
|
2010-02-06 14:43:28 +00:00
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
char const* const fmt = hint == nullptr ? _("Couldn't bind port %d on %s: %s") :
|
|
|
|
_("Couldn't bind port %d on %s: %s (%s)");
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
char err_buf[512];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError(fmt, port, tr_address_to_string(addr), tr_net_strerror(err_buf, sizeof(err_buf), err), hint);
|
2010-02-06 14:43:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_netCloseSocket(fd);
|
2009-07-09 18:14:33 +00:00
|
|
|
*errOut = err;
|
2015-03-18 07:34:26 +00:00
|
|
|
return TR_BAD_SOCKET;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!suppressMsgs)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddDebug("Bound socket %" PRIdMAX " to port %d on %s", (intmax_t)fd, port, tr_address_to_string(addr));
|
|
|
|
}
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2017-02-18 00:47:11 +00:00
|
|
|
#ifdef TCP_FASTOPEN
|
|
|
|
|
|
|
|
#ifndef SOL_TCP
|
|
|
|
#define SOL_TCP IPPROTO_TCP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
optval = 5;
|
2021-09-12 17:41:49 +00:00
|
|
|
(void)setsockopt(fd, SOL_TCP, TCP_FASTOPEN, reinterpret_cast<char const*>(&optval), sizeof(optval));
|
2017-02-18 00:47:11 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-07-29 03:14:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (listen(fd, SOMAXCONN) == -1)
|
|
|
|
#else /* _WIN32 */
|
|
|
|
/* Listen queue backlog will be capped to the operating system's limit. */
|
|
|
|
if (listen(fd, INT_MAX) == -1)
|
|
|
|
#endif /* _WIN32 */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-07-09 18:14:33 +00:00
|
|
|
*errOut = sockerrno;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_netCloseSocket(fd);
|
2015-03-18 07:34:26 +00:00
|
|
|
return TR_BAD_SOCKET;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_socket_t tr_netBindTCP(tr_address const* addr, tr_port port, bool suppressMsgs)
|
2009-07-09 18:14:33 +00:00
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
int unused = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_netBindTCPImpl(addr, port, suppressMsgs, &unused);
|
2009-07-09 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_net_hasIPv6(tr_port port)
|
2009-07-09 18:14:33 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool result = false;
|
|
|
|
static bool alreadyDone = false;
|
2009-07-09 18:14:33 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!alreadyDone)
|
2009-07-09 18:14:33 +00:00
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
int err = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_socket_t fd = tr_netBindTCPImpl(&tr_in6addr_any, port, true, &err);
|
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (fd != TR_BAD_SOCKET || err != EAFNOSUPPORT) /* we support ipv6 */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
result = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 07:34:26 +00:00
|
|
|
if (fd != TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_netCloseSocket(fd);
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
alreadyDone = true;
|
2009-07-09 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_socket_t tr_netAccept(tr_session* session, tr_socket_t b, tr_address* addr, tr_port* port)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_socket_t fd = tr_fdSocketAccept(session, b, addr, port);
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2017-04-30 16:46:02 +00:00
|
|
|
if (fd != TR_BAD_SOCKET && evutil_make_socket_nonblocking(fd) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_netClose(session, fd);
|
2015-03-18 07:34:26 +00:00
|
|
|
fd = TR_BAD_SOCKET;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
2008-12-16 21:06:47 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
return fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_netCloseSocket(tr_socket_t fd)
|
2009-10-27 20:27:27 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_closesocket(fd);
|
2009-10-27 20:27:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_netClose(tr_session* session, tr_socket_t s)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_fdSocketClose(session, s);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2009-11-09 05:45:16 +00:00
|
|
|
|
|
|
|
/*
|
2017-04-21 07:40:57 +00:00
|
|
|
get_source_address() and global_unicast_address() were written by
|
2010-04-20 03:44:45 +00:00
|
|
|
Juliusz Chroboczek, and are covered under the same license as dht.c.
|
|
|
|
Please feel free to copy them into your software if it can help
|
|
|
|
unbreaking the double-stack Internet. */
|
2009-11-09 05:45:16 +00:00
|
|
|
|
2010-12-27 19:18:17 +00:00
|
|
|
/* Get the source address used for a given destination address. Since
|
2009-11-09 05:45:16 +00:00
|
|
|
there is no official interface to get this information, we create
|
|
|
|
a connected UDP socket (connected UDP... hmm...) and check its source
|
|
|
|
address. */
|
2017-04-20 16:02:19 +00:00
|
|
|
static int get_source_address(struct sockaddr const* dst, socklen_t dst_len, struct sockaddr* src, socklen_t* src_len)
|
2009-11-09 05:45:16 +00:00
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
tr_socket_t const s = socket(dst->sa_family, SOCK_DGRAM, 0);
|
2015-03-18 07:34:26 +00:00
|
|
|
if (s == TR_BAD_SOCKET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
// since it's a UDP socket, this doesn't actually send any packets
|
|
|
|
if (connect(s, dst, dst_len) == 0 && getsockname(s, src, src_len) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 17:40:15 +00:00
|
|
|
evutil_closesocket(s);
|
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-09 05:45:16 +00:00
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
int save = errno;
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_closesocket(s);
|
2009-11-09 05:45:16 +00:00
|
|
|
errno = save;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We all hate NATs. */
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
static int global_unicast_address(struct sockaddr_storage* ss)
|
2009-11-09 05:45:16 +00:00
|
|
|
{
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
if (ss->ss_family == AF_INET)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
unsigned char const* a = (unsigned char*)&((struct sockaddr_in*)ss)->sin_addr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (a[0] == 0 || a[0] == 127 || a[0] >= 224 || a[0] == 10 || (a[0] == 172 && a[1] >= 16 && a[1] <= 31) ||
|
|
|
|
(a[0] == 192 && a[1] == 168))
|
|
|
|
{
|
2009-11-09 05:45:16 +00:00
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 05:45:16 +00:00
|
|
|
return 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
|
|
|
if (ss->ss_family == AF_INET6)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
unsigned char const* a = (unsigned char*)&((struct sockaddr_in6*)ss)->sin6_addr;
|
2009-11-09 05:45:16 +00:00
|
|
|
/* 2000::/3 */
|
2017-04-30 16:25:26 +00:00
|
|
|
return (a[0] & 0xE0) == 0x20 ? 1 : 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
|
|
|
errno = EAFNOSUPPORT;
|
|
|
|
return -1;
|
2009-11-09 05:45:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int tr_globalAddress(int af, void* addr, int* addr_len)
|
2009-11-09 05:45:16 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
2017-04-19 12:04:45 +00:00
|
|
|
socklen_t sslen = sizeof(ss);
|
2010-04-20 03:44:45 +00:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in6 sin6;
|
2021-10-24 17:40:15 +00:00
|
|
|
struct sockaddr const* sa = nullptr;
|
|
|
|
socklen_t salen = 0;
|
2009-11-09 05:45:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (af)
|
|
|
|
{
|
2010-04-20 03:44:45 +00:00
|
|
|
case AF_INET:
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
2010-04-20 03:44:45 +00:00
|
|
|
sin.sin_family = AF_INET;
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_inet_pton(AF_INET, "91.121.74.28", &sin.sin_addr);
|
|
|
|
sin.sin_port = htons(6969);
|
2020-11-09 03:31:02 +00:00
|
|
|
sa = (struct sockaddr const*)&sin;
|
2017-04-19 12:04:45 +00:00
|
|
|
salen = sizeof(sin);
|
2010-04-20 03:44:45 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-20 03:44:45 +00:00
|
|
|
case AF_INET6:
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
2010-04-20 03:44:45 +00:00
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
/* In order for address selection to work right, this should be
|
|
|
|
a native IPv6 address, not Teredo or 6to4. */
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_inet_pton(AF_INET6, "2001:1890:1112:1::20", &sin6.sin6_addr);
|
|
|
|
sin6.sin6_port = htons(6969);
|
2020-11-09 03:31:02 +00:00
|
|
|
sa = (struct sockaddr const*)&sin6;
|
2017-04-19 12:04:45 +00:00
|
|
|
salen = sizeof(sin6);
|
2010-04-20 03:44:45 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-20 03:44:45 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-10-24 17:40:15 +00:00
|
|
|
int const rc = get_source_address(sa, salen, (struct sockaddr*)&ss, &sslen);
|
2009-11-09 05:45:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (rc < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-09 05:45:16 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-09 05:45:16 +00:00
|
|
|
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
if (global_unicast_address(&ss) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-09 05:45:16 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-09 05:45:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (af)
|
|
|
|
{
|
2009-11-09 05:45:16 +00:00
|
|
|
case AF_INET:
|
2012-12-05 17:29:46 +00:00
|
|
|
if (*addr_len < 4)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-09 05:45:16 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(addr, &((struct sockaddr_in*)&ss)->sin_addr, 4);
|
2009-11-09 05:45:16 +00:00
|
|
|
*addr_len = 4;
|
|
|
|
return 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2009-11-09 05:45:16 +00:00
|
|
|
case AF_INET6:
|
2012-12-05 17:29:46 +00:00
|
|
|
if (*addr_len < 16)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-11-09 05:45:16 +00:00
|
|
|
return -1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(addr, &((struct sockaddr_in6*)&ss)->sin6_addr, 16);
|
2009-11-09 05:45:16 +00:00
|
|
|
*addr_len = 16;
|
|
|
|
return 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2009-11-09 05:45:16 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2009-11-10 17:03:23 +00:00
|
|
|
|
|
|
|
/* Return our global IPv6 address, with caching. */
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned char const* tr_globalIPv6(void)
|
2009-11-10 17:03:23 +00:00
|
|
|
{
|
|
|
|
static unsigned char ipv6[16];
|
|
|
|
static time_t last_time = 0;
|
2014-11-30 19:38:47 +00:00
|
|
|
static bool have_ipv6 = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2009-11-10 17:03:23 +00:00
|
|
|
|
|
|
|
/* Re-check every half hour */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (last_time < now - 1800)
|
2009-11-10 17:03:23 +00:00
|
|
|
{
|
|
|
|
int addrlen = 16;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const rc = tr_globalAddress(AF_INET6, ipv6, &addrlen);
|
2017-04-30 16:25:26 +00:00
|
|
|
have_ipv6 = rc >= 0 && addrlen == 16;
|
2009-11-10 17:03:23 +00:00
|
|
|
last_time = now;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return have_ipv6 ? ipv6 : nullptr;
|
2009-11-10 17:03:23 +00:00
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
2009-12-15 17:39:19 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool isIPv4MappedAddress(tr_address const* addr)
|
2009-12-15 17:39:19 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return addr->type == TR_AF_INET6 && IN6_IS_ADDR_V4MAPPED(&addr->addr.addr6);
|
2009-12-15 17:39:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool isIPv6LinkLocalAddress(tr_address const* addr)
|
2009-12-15 17:39:19 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return addr->type == TR_AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&addr->addr.addr6);
|
2009-12-15 17:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* isMartianAddr was written by Juliusz Chroboczek,
|
|
|
|
and is covered under the same license as third-party/dht/dht.c. */
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool isMartianAddr(struct tr_address const* a)
|
2009-12-14 02:07:47 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(a));
|
2009-12-15 17:39:19 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static unsigned char const zeroes[16] = {};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (a->type)
|
2009-12-14 02:07:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_AF_INET:
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned char const* address = (unsigned char const*)&a->addr.addr4;
|
2017-04-30 16:25:26 +00:00
|
|
|
return address[0] == 0 || address[0] == 127 || (address[0] & 0xE0) == 0xE0;
|
2009-12-15 17:39:19 +00:00
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_AF_INET6:
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned char const* address = (unsigned char const*)&a->addr.addr6;
|
2017-04-30 16:25:26 +00:00
|
|
|
return address[0] == 0xFF || (memcmp(address, zeroes, 15) == 0 && (address[15] == 0 || address[15] == 1));
|
2009-12-15 17:39:19 +00:00
|
|
|
}
|
2009-12-14 02:07:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
return true;
|
2009-12-14 02:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 17:39:19 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_address_is_valid_for_peers(tr_address const* addr, tr_port port)
|
2009-12-15 17:39:19 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return port != 0 && tr_address_is_valid(addr) && !isIPv6LinkLocalAddress(addr) && !isIPv4MappedAddress(addr) &&
|
2019-02-10 11:05:16 +00:00
|
|
|
!isMartianAddr(addr);
|
2009-12-15 17:39:19 +00:00
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
struct tr_peer_socket tr_peer_socket_tcp_create(tr_socket_t const handle)
|
|
|
|
{
|
|
|
|
TR_ASSERT(handle != TR_BAD_SOCKET);
|
2021-09-12 17:41:49 +00:00
|
|
|
|
|
|
|
return { TR_PEER_SOCKET_TYPE_TCP, { handle } };
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct tr_peer_socket tr_peer_socket_utp_create(struct UTPSocket* const handle)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2021-09-12 17:41:49 +00:00
|
|
|
|
|
|
|
auto ret = tr_peer_socket{ TR_PEER_SOCKET_TYPE_UTP, {} };
|
|
|
|
ret.handle.utp = handle;
|
2020-08-11 18:11:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|