Ticket #776: bind.patch

File bind.patch, 5.0 KB (added by charles, 14 years ago)

first cut at implementing this. patch is against svn trunk

  • libtransmission/peer-io.c

     
    2929#include "net.h"
    3030#include "peer-io.h"
    3131#include "ratecontrol.h"
     32#include "shared.h"
    3233#include "trevent.h"
    3334#include "utils.h"
    3435
     
    182183                      const uint8_t         * torrentHash )
    183184{
    184185    int socket;
     186    const int local_port = tr_sharedGetPublicPort( handle->shared ) + 1;
    185187
    186188    assert( handle != NULL );
    187189    assert( in_addr != NULL );
    188190    assert( port >= 0 );
    189191    assert( torrentHash != NULL );
    190192
    191     socket = tr_netOpenTCP( in_addr, port, 0 );
     193    socket = tr_netOpenTCP( in_addr, port, local_port, 0 );
    192194
    193195    return socket < 0
    194196        ? NULL
     
    283285int
    284286tr_peerIoReconnect( tr_peerIo * io )
    285287{
     288    const int local_port = tr_sharedGetPublicPort( io->handle->shared ) + 1;
     289
    286290    assert( !tr_peerIoIsIncoming( io ) );
    287291
    288292    if( io->socket >= 0 )
    289293        tr_netClose( io->socket );
    290294
    291     io->socket = tr_netOpenTCP( &io->in_addr, io->port, 0 );
     295    io->socket = tr_netOpenTCP( &io->in_addr, io->port, local_port, 0 );
    292296 
    293297    if( io->socket >= 0 )
    294298    {
  • libtransmission/net.c

     
    109109}
    110110
    111111static int
    112 tr_netOpen( const struct in_addr * addr, tr_port_t port,
    113             int type, int priority )
     112tr_netBind( int port, int type, int priority )
    114113{
    115114    int s;
    116115    struct sockaddr_in sock;
    117 
    118     if( ( s = createSocket( type, priority ) ) < 0 )
    119     {
    120         return -1;
    121     }
    122 
    123     memset( &sock, 0, sizeof( sock ) );
    124     sock.sin_family      = AF_INET;
    125     sock.sin_addr.s_addr = addr->s_addr;
    126     sock.sin_port        = port;
    127 
    128     if( ( connect( s, (struct sockaddr *) &sock,
    129                    sizeof( struct sockaddr_in ) ) < 0 )
    130 #ifdef WIN32
    131         && ( sockerrno != WSAEWOULDBLOCK )
    132 #endif
    133         && ( sockerrno != EINPROGRESS ) )
    134     {
    135         tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
    136                 s, inet_ntoa(*addr), port,
    137                 sockerrno, tr_strerror(sockerrno) );
    138         tr_netClose( s );
    139         s = -1;
    140     }
    141 
    142     return s;
    143 }
    144  
    145 int
    146 tr_netOpenTCP( const struct in_addr * addr, tr_port_t port, int priority )
    147 {
    148     return tr_netOpen( addr, port, SOCK_STREAM, priority );
    149 }
    150 
    151 static int
    152 tr_netBind( int port, int type )
    153 {
    154     int s;
    155     struct sockaddr_in sock;
    156116#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
    157117    int optval;
    158118#endif
    159119
    160     if( ( s = createSocket( type, 1 ) ) < 0 )
     120    if( ( s = createSocket( type, priority ) ) < 0 )
    161121        return -1;
    162122
    163123#ifdef SO_REUSEADDR
     
    173133    if( bind( s, (struct sockaddr *) &sock,
    174134               sizeof( struct sockaddr_in ) ) )
    175135    {
     136        fprintf( stderr, _( "Couldn't bind port %d: %s\n" ), port, tr_strerror(sockerrno) );
    176137        tr_err( _( "Couldn't bind port %d: %s" ), port, tr_strerror(sockerrno) );
    177138        tr_netClose( s );
    178139        return -1;
     
    185146int
    186147tr_netBindTCP( int port )
    187148{
    188     return tr_netBind( port, SOCK_STREAM );
     149    return tr_netBind( port, SOCK_STREAM, 1 );
    189150}
    190151
    191152int
     153tr_netOpenTCP( const struct in_addr * addr, tr_port_t port,
     154               tr_port_t local_port, int priority )
     155{
     156    const int type = SOCK_STREAM;
     157    int s = tr_netBind( local_port, type, priority );
     158    struct sockaddr_in sock;
     159
     160    memset( &sock, 0, sizeof( sock ) );
     161    sock.sin_family      = AF_INET;
     162    sock.sin_addr.s_addr = addr->s_addr;
     163    sock.sin_port        = port;
     164
     165    if( ( connect( s, (struct sockaddr *) &sock,
     166                   sizeof( struct sockaddr_in ) ) < 0 )
     167#ifdef WIN32
     168        && ( sockerrno != WSAEWOULDBLOCK )
     169#endif
     170        && ( sockerrno != EINPROGRESS ) )
     171    {
     172        tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
     173                s, inet_ntoa(*addr), port,
     174                sockerrno, tr_strerror(sockerrno) );
     175        tr_netClose( s );
     176        s = -1;
     177    }
     178
     179    return s;
     180}
     181
     182int
    192183tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
    193184{
    194185    return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
  • libtransmission/net.h

     
    6767/***********************************************************************
    6868 * TCP and UDP sockets
    6969 **********************************************************************/
    70 int  tr_netOpenTCP  ( const struct in_addr * addr, tr_port_t port, int priority );
     70int  tr_netOpenTCP  ( const struct in_addr * addr, tr_port_t port, tr_port_t local_port, int priority );
    7171int  tr_netOpenUDP  ( const struct in_addr * addr, tr_port_t port, int priority );
    7272int  tr_netBindTCP  ( int port );
    7373int  tr_netBindUDP  ( int port );