Ignore:
Timestamp:
Jul 9, 2009, 6:14:33 PM (12 years ago)
Author:
titer
Message:

Don't assume that errnos are positive (they are not on Haiku), and
pass them on through an additional parameter if needed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/net.c

    r8599 r8791  
    196196}
    197197
    198 tr_bool
    199 tr_net_hasIPv6( tr_port port )
    200 {
    201     static tr_bool result = FALSE;
    202     static tr_bool alreadyDone = FALSE;
    203 
    204     if( !alreadyDone )
    205     {
    206         int fd = tr_netBindTCP( &tr_in6addr_any, port, TRUE );
    207         if( fd >= 0 || -fd != EAFNOSUPPORT ) /* we support ipv6 */
    208             result = TRUE;
    209         if( fd >= 0 )
    210             EVUTIL_CLOSESOCKET( fd );
    211         alreadyDone = TRUE;
    212     }
    213 
    214     return result;
    215 }
    216 
    217198/***********************************************************************
    218199 * TCP sockets
     
    369350}
    370351
    371 int
    372 tr_netBindTCP( const tr_address * addr, tr_port port, tr_bool suppressMsgs )
     352static int
     353tr_netBindTCPImpl( const tr_address * addr, tr_port port, tr_bool suppressMsgs, int * errOut )
    373354{
    374355    static const int domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
     
    381362
    382363    fd = socket( domains[addr->type], SOCK_STREAM, 0 );
    383     if( fd < 0 )
    384         return -1;
     364    if( fd < 0 ) {
     365        *errOut = sockerrno;
     366        return -1;
     367    }
    385368
    386369    if( evutil_make_socket_nonblocking( fd ) < 0 ) {
     370        *errOut = sockerrno;
    387371        EVUTIL_CLOSESOCKET( fd );
    388372        return -1;
     
    396380    if( addr->type == TR_AF_INET6 )
    397381        if( setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof( optval ) ) == -1 )
    398             if( sockerrno != ENOPROTOOPT ) /* if the kernel doesn't support it, ignore it */
    399                 return -sockerrno;
     382            if( sockerrno != ENOPROTOOPT ) { /* if the kernel doesn't support it, ignore it */
     383                *errOut = sockerrno;
     384                return -1;
     385            }
    400386#endif
    401387
     
    407393                    port, tr_ntop_non_ts( addr ), tr_strerror( err ) );
    408394        EVUTIL_CLOSESOCKET( fd );
    409         return -err;
     395        *errOut = err;
     396        return -1;
    410397    }
    411398
     
    414401
    415402    if( listen( fd, 128 ) == -1 ) {
     403        *errOut = sockerrno;
    416404        EVUTIL_CLOSESOCKET( fd );
    417         return -sockerrno;
     405        return -1;
    418406    }
    419407
    420408    return fd;
     409}
     410
     411int
     412tr_netBindTCP( const tr_address * addr, tr_port port, tr_bool suppressMsgs )
     413{
     414    int unused;
     415    return tr_netBindTCPImpl( addr, port, suppressMsgs, &unused );
     416}
     417
     418tr_bool
     419tr_net_hasIPv6( tr_port port )
     420{
     421    static tr_bool result = FALSE;
     422    static tr_bool alreadyDone = FALSE;
     423
     424    if( !alreadyDone )
     425    {
     426        int err;
     427        int fd = tr_netBindTCPImpl( &tr_in6addr_any, port, TRUE, &err );
     428        if( fd >= 0 || err != EAFNOSUPPORT ) /* we support ipv6 */
     429            result = TRUE;
     430        if( fd >= 0 )
     431            EVUTIL_CLOSESOCKET( fd );
     432        alreadyDone = TRUE;
     433    }
     434
     435    return result;
    421436}
    422437
Note: See TracChangeset for help on using the changeset viewer.