Define and use tr_socket_t and TR_BAD_SOCKET instead of int and -1.

Test socket validity by comparing to TR_BAD_SOCKET instead of various
(and sometimes wrong) other tests like `x >= 0`, `x != -1`, `x > 0`,
`x > -1`, `x` (valid), and `x < 0`, `x == -1` (invalid).
This commit is contained in:
Mike Gelfand 2015-03-18 07:34:26 +00:00
parent 5cdd84fd28
commit 949e02b933
17 changed files with 194 additions and 155 deletions

View File

@ -53,16 +53,16 @@ tau_sendto (tr_session * session,
struct evutil_addrinfo * ai, tr_port port,
const void * buf, size_t buflen)
{
int sockfd;
tr_socket_t sockfd;
if (ai->ai_addr->sa_family == AF_INET)
sockfd = session->udp_socket;
else if (ai->ai_addr->sa_family == AF_INET6)
sockfd = session->udp6_socket;
else
sockfd = -1;
sockfd = TR_BAD_SOCKET;
if (sockfd < 0) {
if (sockfd == TR_BAD_SOCKET) {
errno = EAFNOSUPPORT;
return -1;
}

View File

@ -503,10 +503,12 @@ tr_fdFileCheckout (tr_session * session,
****
***/
int
tr_fdSocketCreate (tr_session * session, int domain, int type)
tr_socket_t
tr_fdSocketCreate (tr_session * session,
int domain,
int type)
{
int s = -1;
tr_socket_t s = TR_BAD_SOCKET;
struct tr_fdInfo * gFd;
assert (tr_isSession (session));
@ -514,16 +516,16 @@ tr_fdSocketCreate (tr_session * session, int domain, int type)
gFd = session->fdInfo;
if (gFd->peerCount < session->peerLimit)
if ((s = socket (domain, type, 0)) < 0)
if ((s = socket (domain, type, 0)) == TR_BAD_SOCKET)
if (sockerrno != EAFNOSUPPORT)
tr_logAddError (_("Couldn't create socket: %s"), tr_strerror (sockerrno));
if (s > -1)
if (s != TR_BAD_SOCKET)
++gFd->peerCount;
assert (gFd->peerCount >= 0);
if (s >= 0)
if (s != TR_BAD_SOCKET)
{
static bool buf_logged = false;
if (!buf_logged)
@ -541,10 +543,13 @@ tr_fdSocketCreate (tr_session * session, int domain, int type)
return s;
}
int
tr_fdSocketAccept (tr_session * s, int sockfd, tr_address * addr, tr_port * port)
tr_socket_t
tr_fdSocketAccept (tr_session * s,
tr_socket_t sockfd,
tr_address * addr,
tr_port * port)
{
int fd;
tr_socket_t fd;
socklen_t len;
struct tr_fdInfo * gFd;
struct sockaddr_storage sock;
@ -559,17 +564,17 @@ tr_fdSocketAccept (tr_session * s, int sockfd, tr_address * addr, tr_port * port
len = sizeof (struct sockaddr_storage);
fd = accept (sockfd, (struct sockaddr *) &sock, &len);
if (fd >= 0)
if (fd != TR_BAD_SOCKET)
{
if ((gFd->peerCount < s->peerLimit)
&& tr_address_from_sockaddr_storage (addr, port, &sock))
{
++gFd->peerCount;
}
else
else
{
tr_netCloseSocket (fd);
fd = -1;
fd = TR_BAD_SOCKET;
}
}
@ -577,7 +582,8 @@ tr_fdSocketAccept (tr_session * s, int sockfd, tr_address * addr, tr_port * port
}
void
tr_fdSocketClose (tr_session * session, int fd)
tr_fdSocketClose (tr_session * session,
tr_socket_t fd)
{
assert (tr_isSession (session));
@ -585,7 +591,7 @@ tr_fdSocketClose (tr_session * session, int fd)
{
struct tr_fdInfo * gFd = session->fdInfo;
if (fd >= 0)
if (fd != TR_BAD_SOCKET)
{
tr_netCloseSocket (fd);
--gFd->peerCount;

View File

@ -80,14 +80,17 @@ void tr_fdTorrentClose (tr_session * session, int torrentId);
/***********************************************************************
* Sockets
**********************************************************************/
int tr_fdSocketCreate (tr_session * session, int domain, int type);
tr_socket_t tr_fdSocketCreate (tr_session * session,
int domain,
int type);
int tr_fdSocketAccept (tr_session * session,
int listening_sockfd,
tr_address * addr,
tr_port * port);
tr_socket_t tr_fdSocketAccept (tr_session * session,
tr_socket_t listening_sockfd,
tr_address * addr,
tr_port * port);
void tr_fdSocketClose (tr_session * session, int s);
void tr_fdSocketClose (tr_session * session,
tr_socket_t s);
/***********************************************************************
* tr_fdClose

View File

@ -84,7 +84,7 @@ tr_natpmpInit (void)
nat->state = TR_NATPMP_DISCOVER;
nat->public_port = 0;
nat->private_port = 0;
nat->natpmp.s = -1; /* socket */
nat->natpmp.s = TR_BAD_SOCKET; /* socket */
return nat;
}
@ -93,8 +93,7 @@ tr_natpmpClose (tr_natpmp * nat)
{
if (nat)
{
if (nat->natpmp.s >= 0)
tr_netCloseSocket (nat->natpmp.s);
closenatpmp (&nat->natpmp);
tr_free (nat);
}
}

View File

@ -145,7 +145,8 @@ tr_address_compare (const tr_address * a, const tr_address * b)
**********************************************************************/
int
tr_netSetTOS (int s, int tos)
tr_netSetTOS (tr_socket_t s,
int tos)
{
#ifdef IP_TOS
return setsockopt (s, IPPROTO_IP, IP_TOS, (const void *) &tos, sizeof (tos));
@ -155,7 +156,8 @@ tr_netSetTOS (int s, int tos)
}
int
tr_netSetCongestionControl (int s UNUSED, const char *algorithm UNUSED)
tr_netSetCongestionControl (tr_socket_t s UNUSED,
const char * algorithm UNUSED)
{
#ifdef TCP_CONGESTION
return setsockopt (s, IPPROTO_TCP, TCP_CONGESTION,
@ -222,14 +224,14 @@ setup_sockaddr (const tr_address * addr,
}
}
int
tr_socket_t
tr_netOpenPeerSocket (tr_session * session,
const tr_address * addr,
tr_port port,
bool clientIsSeed)
{
static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
int s;
tr_socket_t s;
struct sockaddr_storage sock;
socklen_t addrlen;
const tr_address * source_addr;
@ -242,19 +244,19 @@ tr_netOpenPeerSocket (tr_session * session,
return -EINVAL;
s = tr_fdSocketCreate (session, domains[addr->type], SOCK_STREAM);
if (s < 0)
return -1;
if (s == TR_BAD_SOCKET)
return TR_BAD_SOCKET;
/* seeds don't need much of a read buffer... */
if (clientIsSeed) {
int n = 8192;
if (setsockopt (s, SOL_SOCKET, SO_RCVBUF, (const void *) &n, sizeof (n)))
tr_logAddInfo ("Unable to set SO_RCVBUF on socket %d: %s", s, tr_strerror (sockerrno));
tr_logAddInfo ("Unable to set SO_RCVBUF on socket %"TR_PRI_SOCK": %s", s, tr_strerror (sockerrno));
}
if (evutil_make_socket_nonblocking (s) < 0) {
tr_netClose (session, s);
return -1;
return TR_BAD_SOCKET;
}
addrlen = setup_sockaddr (addr, port, &sock);
@ -265,10 +267,10 @@ tr_netOpenPeerSocket (tr_session * session,
sourcelen = setup_sockaddr (source_addr, 0, &source_sock);
if (bind (s, (struct sockaddr *) &source_sock, sourcelen))
{
tr_logAddError (_("Couldn't set source address %s on %d: %s"),
tr_logAddError (_("Couldn't set source address %s on %"TR_PRI_SOCK": %s"),
tr_address_to_string (source_addr), s, tr_strerror (errno));
tr_netClose (session, s);
return -errno;
return TR_BAD_SOCKET; /* -errno */
}
if ((connect (s, (struct sockaddr *) &sock,
@ -282,14 +284,14 @@ tr_netOpenPeerSocket (tr_session * session,
tmperrno = sockerrno;
if ((tmperrno != ENETUNREACH && tmperrno != EHOSTUNREACH)
|| addr->type == TR_AF_INET)
tr_logAddError (_("Couldn't connect socket %d to %s, port %d (errno %d - %s)"),
tr_logAddError (_("Couldn't connect socket %"TR_PRI_SOCK" to %s, port %d (errno %d - %s)"),
s, tr_address_to_string (addr), (int)ntohs (port), tmperrno,
tr_strerror (tmperrno));
tr_netClose (session, s);
s = -tmperrno;
s = TR_BAD_SOCKET; /* -tmperrno */
}
tr_logAddDeep (__FILE__, __LINE__, NULL, "New OUTGOING connection %d (%s)",
tr_logAddDeep (__FILE__, __LINE__, NULL, "New OUTGOING connection %"TR_PRI_SOCK" (%s)",
s, tr_peerIoAddrStr (addr, port));
return s;
@ -313,27 +315,30 @@ tr_netOpenPeerUTPSocket (tr_session * session,
return ret;
}
static int
tr_netBindTCPImpl (const tr_address * addr, tr_port port, bool suppressMsgs, int * errOut)
static tr_socket_t
tr_netBindTCPImpl (const tr_address * addr,
tr_port port,
bool suppressMsgs,
int * errOut)
{
static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
struct sockaddr_storage sock;
int fd;
tr_socket_t fd;
int addrlen;
int optval;
assert (tr_address_is_valid (addr));
fd = socket (domains[addr->type], SOCK_STREAM, 0);
if (fd < 0) {
if (fd == TR_BAD_SOCKET) {
*errOut = sockerrno;
return -1;
return TR_BAD_SOCKET;
}
if (evutil_make_socket_nonblocking (fd) < 0) {
*errOut = sockerrno;
tr_netCloseSocket (fd);
return -1;
return TR_BAD_SOCKET;
}
optval = 1;
@ -345,7 +350,7 @@ tr_netBindTCPImpl (const tr_address * addr, tr_port port, bool suppressMsgs, int
if (setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void *) &optval, sizeof (optval)) == -1)
if (sockerrno != ENOPROTOOPT) { /* if the kernel doesn't support it, ignore it */
*errOut = sockerrno;
return -1;
return TR_BAD_SOCKET;
}
#endif
@ -371,23 +376,25 @@ tr_netBindTCPImpl (const tr_address * addr, tr_port port, bool suppressMsgs, int
}
tr_netCloseSocket (fd);
*errOut = err;
return -1;
return TR_BAD_SOCKET;
}
if (!suppressMsgs)
tr_logAddDebug ("Bound socket %d to port %d on %s", fd, port, tr_address_to_string (addr));
tr_logAddDebug ("Bound socket %"TR_PRI_SOCK" to port %d on %s", fd, port, tr_address_to_string (addr));
if (listen (fd, 128) == -1) {
*errOut = sockerrno;
tr_netCloseSocket (fd);
return -1;
return TR_BAD_SOCKET;
}
return fd;
}
int
tr_netBindTCP (const tr_address * addr, tr_port port, bool suppressMsgs)
tr_socket_t
tr_netBindTCP (const tr_address * addr,
tr_port port,
bool suppressMsgs)
{
int unused;
return tr_netBindTCPImpl (addr, port, suppressMsgs, &unused);
@ -402,10 +409,10 @@ tr_net_hasIPv6 (tr_port port)
if (!alreadyDone)
{
int err;
int fd = tr_netBindTCPImpl (&tr_in6addr_any, port, true, &err);
if (fd >= 0 || err != EAFNOSUPPORT) /* we support ipv6 */
tr_socket_t fd = tr_netBindTCPImpl (&tr_in6addr_any, port, true, &err);
if (fd != TR_BAD_SOCKET || err != EAFNOSUPPORT) /* we support ipv6 */
result = true;
if (fd >= 0)
if (fd != TR_BAD_SOCKET)
tr_netCloseSocket (fd);
alreadyDone = true;
}
@ -413,30 +420,31 @@ tr_net_hasIPv6 (tr_port port)
return result;
}
int
tr_socket_t
tr_netAccept (tr_session * session,
int b,
tr_socket_t b,
tr_address * addr,
tr_port * port)
{
int fd = tr_fdSocketAccept (session, b, addr, port);
tr_socket_t fd = tr_fdSocketAccept (session, b, addr, port);
if (fd>=0 && evutil_make_socket_nonblocking (fd)<0) {
if (fd != TR_BAD_SOCKET && evutil_make_socket_nonblocking (fd) < 0) {
tr_netClose (session, fd);
fd = -1;
fd = TR_BAD_SOCKET;
}
return fd;
}
void
tr_netCloseSocket (int fd)
tr_netCloseSocket (tr_socket_t fd)
{
evutil_closesocket (fd);
}
void
tr_netClose (tr_session * session, int s)
tr_netClose (tr_session * session,
tr_socket_t s)
{
tr_fdSocketClose (session, s);
}
@ -457,10 +465,11 @@ get_source_address (const struct sockaddr * dst,
struct sockaddr * src,
socklen_t * src_len)
{
int s, rc, save;
tr_socket_t s;
int rc, save;
s = socket (dst->sa_family, SOCK_DGRAM, 0);
if (s < 0)
if (s == TR_BAD_SOCKET)
goto fail;
/* Since it's a UDP socket, this doesn't actually send any packets. */

View File

@ -33,11 +33,16 @@
#include <inttypes.h>
#include <ws2tcpip.h>
#else
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#ifdef _WIN32
typedef SOCKET tr_socket_t;
#define TR_BAD_SOCKET INVALID_SOCKET
#define TR_PRI_SOCK "Id" /* intentionally signed to print -1 nicely. */
#undef EADDRINUSE
#define EADDRINUSE WSAEADDRINUSE
#undef ECONNREFUSED
@ -56,9 +61,15 @@
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#undef ENETUNREACH
#define ENETUNREACH WSAENETUNREACH
#define sockerrno WSAGetLastError ()
#else
#include <errno.h>
/** @brief Platform-specific socket descriptor type. */
typedef int tr_socket_t;
/** @brief Platform-specific invalid socket descriptor constant. */
#define TR_BAD_SOCKET (-1)
#define TR_PRI_SOCK "d"
#define sockerrno errno
#endif
@ -121,10 +132,10 @@ tr_address_is_valid (const tr_address * a)
struct tr_session;
int tr_netOpenPeerSocket (tr_session * session,
const tr_address * addr,
tr_port port,
bool clientIsSeed);
tr_socket_t tr_netOpenPeerSocket (tr_session * session,
const tr_address * addr,
tr_port port,
bool clientIsSeed);
struct UTPSocket *
tr_netOpenPeerUTPSocket (tr_session * session,
@ -132,23 +143,25 @@ tr_netOpenPeerUTPSocket (tr_session * session,
tr_port port,
bool clientIsSeed);
int tr_netBindTCP (const tr_address * addr,
tr_port port,
bool suppressMsgs);
tr_socket_t tr_netBindTCP (const tr_address * addr,
tr_port port,
bool suppressMsgs);
int tr_netAccept (tr_session * session,
int bound,
tr_address * setme_addr,
tr_port * setme_port);
tr_socket_t tr_netAccept (tr_session * session,
tr_socket_t bound,
tr_address * setme_addr,
tr_port * setme_port);
int tr_netSetTOS (int s,
int tos);
int tr_netSetTOS (tr_socket_t s,
int tos);
int tr_netSetCongestionControl (int s, const char *algorithm);
int tr_netSetCongestionControl (tr_socket_t s,
const char * algorithm);
void tr_netClose (tr_session * session, int s);
void tr_netClose (tr_session * session,
tr_socket_t s);
void tr_netCloseSocket (int fd);
void tr_netCloseSocket (tr_socket_t fd);
void tr_netInit (void);

View File

@ -158,7 +158,7 @@ didWriteWrapper (tr_peerIo * io, unsigned int bytes_transferred)
const unsigned int payload = MIN (next->length, bytes_transferred);
/* For uTP sockets, the overhead is computed in utp_on_overhead. */
const unsigned int overhead =
io->socket ? guessPacketOverhead (payload) : 0;
io->socket != TR_BAD_SOCKET ? guessPacketOverhead (payload) : 0;
const uint64_t now = tr_time_msec ();
tr_bandwidthUsed (&io->bandwidth, TR_UP, payload, next->isPieceData, now);
@ -259,7 +259,7 @@ event_read_cb (evutil_socket_t fd, short event UNUSED, void * vio)
const unsigned int max = 256 * 1024;
assert (tr_isPeerIo (io));
assert (io->socket >= 0);
assert (io->socket != TR_BAD_SOCKET);
io->pendingEvents &= ~EV_READ;
@ -336,7 +336,7 @@ event_write_cb (evutil_socket_t fd, short event UNUSED, void * vio)
char errstr[1024];
assert (tr_isPeerIo (io));
assert (io->socket >= 0);
assert (io->socket != TR_BAD_SOCKET);
io->pendingEvents &= ~EV_WRITE;
@ -393,7 +393,8 @@ event_write_cb (evutil_socket_t fd, short event UNUSED, void * vio)
**/
static void
maybeSetCongestionAlgorithm (int socket, const char * algorithm)
maybeSetCongestionAlgorithm (tr_socket_t socket,
const char * algorithm)
{
if (algorithm && *algorithm)
{
@ -594,7 +595,7 @@ tr_peerIoNew (tr_session * session,
const uint8_t * torrentHash,
bool isIncoming,
bool isSeed,
int socket,
tr_socket_t socket,
struct UTPSocket * utp_socket)
{
tr_peerIo * io;
@ -604,12 +605,12 @@ tr_peerIoNew (tr_session * session,
assert (tr_isBool (isIncoming));
assert (tr_isBool (isSeed));
assert (tr_amInEventThread (session));
assert ((socket < 0) == (utp_socket != NULL));
assert ((socket == TR_BAD_SOCKET) == (utp_socket != NULL));
#ifndef WITH_UTP
assert (socket >= 0);
assert (socket != TR_BAD_SOCKET);
#endif
if (socket >= 0) {
if (socket != TR_BAD_SOCKET) {
tr_netSetTOS (socket, session->peerSocketTOS);
maybeSetCongestionAlgorithm (socket, session->peer_congestion_algorithm);
}
@ -631,9 +632,9 @@ tr_peerIoNew (tr_session * session,
tr_bandwidthConstruct (&io->bandwidth, session, parent);
tr_bandwidthSetPeer (&io->bandwidth, io);
dbgmsg (io, "bandwidth is %p; its parent is %p", (void*)&io->bandwidth, (void*)parent);
dbgmsg (io, "socket is %d, utp_socket is %p", socket, (void*)utp_socket);
dbgmsg (io, "socket is %"TR_PRI_SOCK", utp_socket is %p", socket, (void*)utp_socket);
if (io->socket >= 0) {
if (io->socket != TR_BAD_SOCKET) {
io->event_read = event_new (session->event_base,
io->socket, EV_READ, event_read_cb, io);
io->event_write = event_new (session->event_base,
@ -661,7 +662,7 @@ tr_peerIoNewIncoming (tr_session * session,
tr_bandwidth * parent,
const tr_address * addr,
tr_port port,
int fd,
tr_socket_t fd,
struct UTPSocket * utp_socket)
{
assert (session);
@ -680,7 +681,7 @@ tr_peerIoNewOutgoing (tr_session * session,
bool isSeed,
bool utp)
{
int fd = -1;
tr_socket_t fd = TR_BAD_SOCKET;
struct UTPSocket * utp_socket = NULL;
assert (session);
@ -692,10 +693,10 @@ tr_peerIoNewOutgoing (tr_session * session,
if (!utp_socket) {
fd = tr_netOpenPeerSocket (session, addr, port, isSeed);
dbgmsg (NULL, "tr_netOpenPeerSocket returned fd %d", fd);
dbgmsg (NULL, "tr_netOpenPeerSocket returned fd %"TR_PRI_SOCK, fd);
}
if (fd < 0 && utp_socket == NULL)
if (fd == TR_BAD_SOCKET && utp_socket == NULL)
return NULL;
return tr_peerIoNew (session, parent, addr, port,
@ -713,7 +714,7 @@ event_enable (tr_peerIo * io, short event)
assert (io->session != NULL);
assert (io->session->events != NULL);
if (io->socket >= 0)
if (io->socket != TR_BAD_SOCKET)
{
assert (event_initialized (io->event_read));
assert (event_initialized (io->event_write));
@ -722,7 +723,7 @@ event_enable (tr_peerIo * io, short event)
if ((event & EV_READ) && ! (io->pendingEvents & EV_READ))
{
dbgmsg (io, "enabling ready-to-read polling");
if (io->socket >= 0)
if (io->socket != TR_BAD_SOCKET)
event_add (io->event_read, NULL);
io->pendingEvents |= EV_READ;
}
@ -730,7 +731,7 @@ event_enable (tr_peerIo * io, short event)
if ((event & EV_WRITE) && ! (io->pendingEvents & EV_WRITE))
{
dbgmsg (io, "enabling ready-to-write polling");
if (io->socket >= 0)
if (io->socket != TR_BAD_SOCKET)
event_add (io->event_write, NULL);
io->pendingEvents |= EV_WRITE;
}
@ -743,7 +744,7 @@ event_disable (struct tr_peerIo * io, short event)
assert (io->session != NULL);
assert (io->session->events != NULL);
if (io->socket >= 0)
if (io->socket != TR_BAD_SOCKET)
{
assert (event_initialized (io->event_read));
assert (event_initialized (io->event_write));
@ -752,7 +753,7 @@ event_disable (struct tr_peerIo * io, short event)
if ((event & EV_READ) && (io->pendingEvents & EV_READ))
{
dbgmsg (io, "disabling ready-to-read polling");
if (io->socket >= 0)
if (io->socket != TR_BAD_SOCKET)
event_del (io->event_read);
io->pendingEvents &= ~EV_READ;
}
@ -760,7 +761,7 @@ event_disable (struct tr_peerIo * io, short event)
if ((event & EV_WRITE) && (io->pendingEvents & EV_WRITE))
{
dbgmsg (io, "disabling ready-to-write polling");
if (io->socket >= 0)
if (io->socket != TR_BAD_SOCKET)
event_del (io->event_write);
io->pendingEvents &= ~EV_WRITE;
}
@ -790,9 +791,9 @@ tr_peerIoSetEnabled (tr_peerIo * io,
static void
io_close_socket (tr_peerIo * io)
{
if (io->socket >= 0) {
if (io->socket != TR_BAD_SOCKET) {
tr_netClose (io->session, io->socket);
io->socket = -1;
io->socket = TR_BAD_SOCKET;
}
if (io->event_read != NULL) {
@ -942,7 +943,7 @@ tr_peerIoReconnect (tr_peerIo * io)
io->event_read = event_new (session->event_base, io->socket, EV_READ, event_read_cb, io);
io->event_write = event_new (session->event_base, io->socket, EV_WRITE, event_write_cb, io);
if (io->socket >= 0)
if (io->socket != TR_BAD_SOCKET)
{
event_enable (io, pendingEvents);
tr_netSetTOS (io->socket, session->peerSocketTOS);

View File

@ -86,7 +86,7 @@ typedef struct tr_peerIo
bool isSeed;
tr_port port;
int socket;
tr_socket_t socket;
struct UTPSocket * utp_socket;
int refCount;
@ -132,7 +132,7 @@ tr_peerIo* tr_peerIoNewIncoming (tr_session * session,
struct tr_bandwidth * parent,
const struct tr_address * addr,
tr_port port,
int socket,
tr_socket_t socket,
struct UTPSocket * utp_socket);
void tr_peerIoRefImpl (const char * file,

View File

@ -2055,7 +2055,7 @@ void
tr_peerMgrAddIncoming (tr_peerMgr * manager,
tr_address * addr,
tr_port port,
int socket,
tr_socket_t socket,
struct UTPSocket * utp_socket)
{
tr_session * session;
@ -2068,14 +2068,14 @@ tr_peerMgrAddIncoming (tr_peerMgr * manager,
if (tr_sessionIsAddressBlocked (session, addr))
{
tr_logAddDebug ("Banned IP address \"%s\" tried to connect to us", tr_address_to_string (addr));
if (socket >= 0)
if (socket != TR_BAD_SOCKET)
tr_netClose (session, socket);
else
UTP_Close (utp_socket);
}
else if (getExistingHandshake (&manager->incomingHandshakes, addr))
{
if (socket >= 0)
if (socket != TR_BAD_SOCKET)
tr_netClose (session, socket);
else
UTP_Close (utp_socket);

View File

@ -107,7 +107,7 @@ void tr_peerMgrRebuildRequests (tr_torrent * torrent);
void tr_peerMgrAddIncoming (tr_peerMgr * manager,
tr_address * addr,
tr_port port,
int socket,
tr_socket_t socket,
struct UTPSocket * utp_socket);
tr_pex * tr_peerMgrCompactToPex (const void * compact,

View File

@ -142,7 +142,7 @@ tr_sessionSetEncryption (tr_session * session,
struct tr_bindinfo
{
int socket;
tr_socket_t socket;
tr_address addr;
struct event * ev;
};
@ -151,7 +151,7 @@ struct tr_bindinfo
static void
close_bindinfo (struct tr_bindinfo * b)
{
if ((b != NULL) && (b->socket >=0))
if ((b != NULL) && (b->socket != TR_BAD_SOCKET))
{
event_free (b->ev);
b->ev = NULL;
@ -181,15 +181,15 @@ free_incoming_peer_port (tr_session * session)
static void
accept_incoming_peer (evutil_socket_t fd, short what UNUSED, void * vsession)
{
int clientSocket;
tr_socket_t clientSocket;
tr_port clientPort;
tr_address clientAddr;
tr_session * session = vsession;
clientSocket = tr_netAccept (session, fd, &clientAddr, &clientPort);
if (clientSocket > 0)
if (clientSocket != TR_BAD_SOCKET)
{
tr_logAddDeep (__FILE__, __LINE__, NULL, "new incoming connection %d (%s)",
tr_logAddDeep (__FILE__, __LINE__, NULL, "new incoming connection %"TR_PRI_SOCK" (%s)",
clientSocket, tr_peerIoAddrStr (&clientAddr, clientPort));
tr_peerMgrAddIncoming (session->peerMgr, &clientAddr, clientPort,
clientSocket, NULL);
@ -204,7 +204,7 @@ open_incoming_peer_port (tr_session * session)
/* bind an ipv4 port to listen for incoming peers... */
b = session->public_ipv4;
b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false);
if (b->socket >= 0)
if (b->socket != TR_BAD_SOCKET)
{
b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session);
event_add (b->ev, NULL);
@ -215,7 +215,7 @@ open_incoming_peer_port (tr_session * session)
{
b = session->public_ipv6;
b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false);
if (b->socket >= 0)
if (b->socket != TR_BAD_SOCKET)
{
b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session);
event_add (b->ev, NULL);
@ -594,8 +594,8 @@ tr_sessionInit (const char * tag,
/* initialize the bare skeleton of the session object */
session = tr_new0 (tr_session, 1);
session->udp_socket = -1;
session->udp6_socket = -1;
session->udp_socket = TR_BAD_SOCKET;
session->udp6_socket = TR_BAD_SOCKET;
session->lock = tr_lockNew ();
session->cache = tr_cacheNew (1024*1024*2);
session->tag = tr_strdup (tag);
@ -854,13 +854,13 @@ sessionSetImpl (void * vdata)
tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv4, &str, NULL);
if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET))
b.addr = tr_inaddr_any;
b.socket = -1;
b.socket = TR_BAD_SOCKET;
session->public_ipv4 = tr_memdup (&b, sizeof (struct tr_bindinfo));
tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv6, &str, NULL);
if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET6))
b.addr = tr_in6addr_any;
b.socket = -1;
b.socket = TR_BAD_SOCKET;
session->public_ipv6 = tr_memdup (&b, sizeof (struct tr_bindinfo));
/* incoming peer port */

View File

@ -26,6 +26,7 @@
#include "bandwidth.h"
#include "bitfield.h"
#include "net.h"
#include "utils.h"
#include "variant.h"
@ -150,8 +151,8 @@ struct tr_session
/* The UDP sockets used for the DHT and uTP. */
tr_port udp_port;
int udp_socket;
int udp6_socket;
tr_socket_t udp_socket;
tr_socket_t udp6_socket;
unsigned char * udp6_bound;
struct event *udp_event;
struct event *udp6_event;

View File

@ -285,11 +285,11 @@ tr_dhtInit (tr_session *ss)
have_id = tr_variantDictFindRaw (&benc, TR_KEY_id, &raw, &len);
if (have_id && len==20)
memcpy (myid, raw, len);
if (ss->udp_socket >= 0 &&
if (ss->udp_socket != TR_BAD_SOCKET &&
tr_variantDictFindRaw (&benc, TR_KEY_nodes, &raw, &len) && ! (len%6)) {
nodes = tr_memdup (raw, len);
}
if (ss->udp6_socket > 0 &&
if (ss->udp6_socket != TR_BAD_SOCKET &&
tr_variantDictFindRaw (&benc, TR_KEY_nodes6, &raw, &len6) && ! (len6%18)) {
nodes6 = tr_memdup (raw, len6);
}
@ -435,8 +435,8 @@ tr_dhtStatus (tr_session * session, int af, int * nodes_return)
struct getstatus_closure closure = { af, -1, -1 };
if (!tr_dhtEnabled (session) ||
(af == AF_INET && session->udp_socket < 0) ||
(af == AF_INET6 && session->udp6_socket < 0)) {
(af == AF_INET && session->udp_socket == TR_BAD_SOCKET) ||
(af == AF_INET6 && session->udp6_socket == TR_BAD_SOCKET)) {
if (nodes_return)
*nodes_return = 0;
return TR_DHT_STOPPED;

View File

@ -73,8 +73,8 @@ enum {
};
static struct event * upkeep_timer = NULL;
static int lpd_socket; /**<separate multicast receive socket */
static int lpd_socket2; /**<and multicast send socket */
static tr_socket_t lpd_socket; /**<separate multicast receive socket */
static tr_socket_t lpd_socket2; /**<and multicast send socket */
static struct event * lpd_event = NULL;
static tr_port lpd_port;
@ -285,7 +285,7 @@ int tr_lpdInit (tr_session* ss, tr_address* tr_addr UNUSED)
/* setup datagram socket (receive) */
{
lpd_socket = socket (PF_INET, SOCK_DGRAM, 0);
if (lpd_socket < 0)
if (lpd_socket == TR_BAD_SOCKET)
goto fail;
if (evutil_make_socket_nonblocking (lpd_socket) < 0)
@ -324,7 +324,7 @@ int tr_lpdInit (tr_session* ss, tr_address* tr_addr UNUSED)
const unsigned char scope = lpd_announceScope;
lpd_socket2 = socket (PF_INET, SOCK_DGRAM, 0);
if (lpd_socket2 < 0)
if (lpd_socket2 == TR_BAD_SOCKET)
goto fail;
if (evutil_make_socket_nonblocking (lpd_socket2) < 0)
@ -360,7 +360,7 @@ int tr_lpdInit (tr_session* ss, tr_address* tr_addr UNUSED)
const int save = errno;
evutil_closesocket (lpd_socket);
evutil_closesocket (lpd_socket2);
lpd_socket = lpd_socket2 = -1;
lpd_socket = lpd_socket2 = TR_BAD_SOCKET;
session = NULL;
tr_logAddNamedDbg ("LPD", "LPD initialisation failed (errno = %d)", save);
errno = save;

View File

@ -51,7 +51,8 @@ THE SOFTWARE.
#define SMALL_BUFFER_SIZE (32 * 1024)
static void
set_socket_buffers (int fd, int large)
set_socket_buffers (tr_socket_t fd,
int large)
{
int size, rbuf, sbuf, rc;
socklen_t rbuf_len = sizeof (rbuf), sbuf_len = sizeof (sbuf);
@ -105,9 +106,9 @@ void
tr_udpSetSocketBuffers (tr_session *session)
{
bool utp = tr_sessionIsUTPEnabled (session);
if (session->udp_socket >= 0)
if (session->udp_socket != TR_BAD_SOCKET)
set_socket_buffers (session->udp_socket, utp);
if (session->udp6_socket >= 0)
if (session->udp6_socket != TR_BAD_SOCKET)
set_socket_buffers (session->udp6_socket, utp);
}
@ -124,12 +125,13 @@ rebind_ipv6 (tr_session *ss, bool force)
const struct tr_address * public_addr;
struct sockaddr_in6 sin6;
const unsigned char *ipv6 = tr_globalIPv6 ();
int s = -1, rc;
tr_socket_t s = TR_BAD_SOCKET;
int rc;
int one = 1;
/* We currently have no way to enable or disable IPv6 after initialisation.
No way to fix that without some surgery to the DHT code itself. */
if (ipv6 == NULL || (!force && ss->udp6_socket < 0)) {
if (ipv6 == NULL || (!force && ss->udp6_socket == TR_BAD_SOCKET)) {
if (ss->udp6_bound) {
free (ss->udp6_bound);
ss->udp6_bound = NULL;
@ -141,7 +143,7 @@ rebind_ipv6 (tr_session *ss, bool force)
return;
s = socket (PF_INET6, SOCK_DGRAM, 0);
if (s < 0)
if (s == TR_BAD_SOCKET)
goto fail;
#ifdef IPV6_V6ONLY
@ -163,7 +165,7 @@ rebind_ipv6 (tr_session *ss, bool force)
if (rc < 0)
goto fail;
if (ss->udp6_socket < 0) {
if (ss->udp6_socket == TR_BAD_SOCKET) {
ss->udp6_socket = s;
} else {
/* FIXME: dup2 doesn't work for sockets on Windows */
@ -184,7 +186,7 @@ rebind_ipv6 (tr_session *ss, bool force)
/* Something went wrong. It's difficult to recover, so let's simply
set things up so that we try again next time. */
tr_logAddNamedError ("UDP", "Couldn't rebind IPv6 socket");
if (s >= 0)
if (s != TR_BAD_SOCKET)
tr_netCloseSocket (s);
if (ss->udp6_bound) {
free (ss->udp6_bound);
@ -244,15 +246,15 @@ tr_udpInit (tr_session *ss)
struct sockaddr_in sin;
int rc;
assert (ss->udp_socket < 0);
assert (ss->udp6_socket < 0);
assert (ss->udp_socket == TR_BAD_SOCKET);
assert (ss->udp6_socket == TR_BAD_SOCKET);
ss->udp_port = tr_sessionGetPeerPort (ss);
if (ss->udp_port <= 0)
return;
ss->udp_socket = socket (PF_INET, SOCK_DGRAM, 0);
if (ss->udp_socket < 0) {
if (ss->udp_socket == TR_BAD_SOCKET) {
tr_logAddNamedError ("UDP", "Couldn't create IPv4 socket");
goto ipv6;
}
@ -267,7 +269,7 @@ tr_udpInit (tr_session *ss)
if (rc < 0) {
tr_logAddNamedError ("UDP", "Couldn't bind IPv4 socket");
tr_netCloseSocket (ss->udp_socket);
ss->udp_socket = -1;
ss->udp_socket = TR_BAD_SOCKET;
goto ipv6;
}
ss->udp_event =
@ -279,7 +281,7 @@ tr_udpInit (tr_session *ss)
ipv6:
if (tr_globalIPv6 ())
rebind_ipv6 (ss, true);
if (ss->udp6_socket >= 0) {
if (ss->udp6_socket != TR_BAD_SOCKET) {
ss->udp6_event =
event_new (ss->event_base, ss->udp6_socket, EV_READ | EV_PERSIST,
event_callback, ss);
@ -303,9 +305,9 @@ tr_udpUninit (tr_session *ss)
{
tr_dhtUninit (ss);
if (ss->udp_socket >= 0) {
if (ss->udp_socket != TR_BAD_SOCKET) {
tr_netCloseSocket (ss->udp_socket);
ss->udp_socket = -1;
ss->udp_socket = TR_BAD_SOCKET;
}
if (ss->udp_event) {
@ -313,9 +315,9 @@ tr_udpUninit (tr_session *ss)
ss->udp_event = NULL;
}
if (ss->udp6_socket >= 0) {
if (ss->udp6_socket != TR_BAD_SOCKET) {
tr_netCloseSocket (ss->udp6_socket);
ss->udp6_socket = -1;
ss->udp6_socket = TR_BAD_SOCKET;
}
if (ss->udp6_event) {

View File

@ -121,7 +121,7 @@ incoming (void *closure, struct UTPSocket *s)
return;
}
tr_peerMgrAddIncoming (ss->peerMgr, &addr, port, -1, s);
tr_peerMgrAddIncoming (ss->peerMgr, &addr, port, TR_BAD_SOCKET, s);
}
void
@ -130,9 +130,9 @@ tr_utpSendTo (void *closure, const unsigned char *buf, size_t buflen,
{
tr_session *ss = closure;
if (to->sa_family == AF_INET && ss->udp_socket)
if (to->sa_family == AF_INET && ss->udp_socket != TR_BAD_SOCKET)
sendto (ss->udp_socket, (const void *) buf, buflen, 0, to, tolen);
else if (to->sa_family == AF_INET6 && ss->udp_socket)
else if (to->sa_family == AF_INET6 && ss->udp6_socket != TR_BAD_SOCKET)
sendto (ss->udp6_socket, (const void *) buf, buflen, 0, to, tolen);
}

View File

@ -35,8 +35,10 @@
#ifdef _WIN32
typedef SOCKET tr_pipe_end_t;
static int
pgpipe (int handles[2])
pgpipe (tr_pipe_end_t handles[2])
{
SOCKET s;
struct sockaddr_in serv_addr;
@ -98,7 +100,9 @@ pgpipe (int handles[2])
}
static int
piperead (int s, char *buf, int len)
piperead (tr_pipe_end_t s,
void * buf,
int len)
{
int ret = recv (s, buf, len, 0);
@ -126,6 +130,7 @@ piperead (int s, char *buf, int len)
#define pipewrite(a,b,c) send (a, (char*)b,c,0)
#else
typedef int tr_pipe_end_t;
#define piperead(a,b,c) read (a,b,c)
#define pipewrite(a,b,c) write (a,b,c)
#endif
@ -137,7 +142,7 @@ piperead (int s, char *buf, int len)
typedef struct tr_event_handle
{
uint8_t die;
int fds[2];
tr_pipe_end_t fds[2];
tr_lock * lock;
tr_session * session;
tr_thread * thread;
@ -314,7 +319,7 @@ tr_runInEventThread (tr_session * session,
}
else
{
int fd;
tr_pipe_end_t fd;
char ch;
ssize_t res_1;
ssize_t res_2;