Ignore:
Timestamp:
Mar 25, 2011, 5:34:26 AM (11 years ago)
Author:
jordan
Message:

(trunk libT) API cleanup of the tr_address functions to make them more consistent.

This is loosely related to #2910, but only in the sense of laying the groundwork for #2910's fix...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/net.c

    r12225 r12229  
    8282
    8383const char *
    84 tr_ntop( const tr_address * src, char * dst, int size )
    85 {
    86     assert( tr_isAddress( src ) );
    87 
    88     if( src->type == TR_AF_INET )
    89         return evutil_inet_ntop( AF_INET, &src->addr, dst, size );
     84tr_address_to_string_with_buf( const tr_address * addr, char * buf, size_t buflen )
     85{
     86    assert( tr_address_is_valid( addr ) );
     87
     88    if( addr->type == TR_AF_INET )
     89        return evutil_inet_ntop( AF_INET, &addr->addr, buf, buflen );
    9090    else
    91         return evutil_inet_ntop( AF_INET6, &src->addr, dst, size );
     91        return evutil_inet_ntop( AF_INET6, &addr->addr, buf, buflen );
    9292}
    9393
    9494/*
    95  * Non-threadsafe version of tr_ntop, which uses a static memory area for a buffer.
     95 * Non-threadsafe version of tr_address_to_string_with_buf()
     96 * and uses a static memory area for a buffer.
    9697 * This function is suitable to be called from libTransmission's networking code,
    9798 * which is single-threaded.
    9899 */
    99100const char *
    100 tr_ntop_non_ts( const tr_address * src )
     101tr_address_to_string( const tr_address * addr )
    101102{
    102103    static char buf[INET6_ADDRSTRLEN];
    103     return tr_ntop( src, buf, sizeof( buf ) );
    104 }
    105 
    106 tr_address *
    107 tr_pton( const char * src, tr_address * dst )
    108 {
    109     int retval = evutil_inet_pton( AF_INET, src, &dst->addr );
    110     assert( dst );
    111     if( retval < 0 )
    112         return NULL;
    113     else if( retval == 0 )
    114         retval = evutil_inet_pton( AF_INET6, src, &dst->addr );
    115     else
    116     {
     104    return tr_address_to_string_with_buf( addr, buf, sizeof( buf ) );
     105}
     106
     107bool
     108tr_address_from_string( tr_address * dst, const char * src )
     109{
     110    bool ok;
     111
     112    if(( ok = evutil_inet_pton( AF_INET, src, &dst->addr ) == 1 ))
    117113        dst->type = TR_AF_INET;
    118         return dst;
    119     }
    120 
    121     if( retval < 1 )
    122         return NULL;
    123     dst->type = TR_AF_INET6;
    124     return dst;
     114
     115    if( !ok ) /* try IPv6 */
     116        if(( ok = evutil_inet_pton( AF_INET6, src, &dst->addr ) == 1 ))
     117            dst->type = TR_AF_INET6;
     118
     119    return ok;
    125120}
    126121
     
    133128 */
    134129int
    135 tr_compareAddresses( const tr_address * a, const tr_address * b)
     130tr_address_compare( const tr_address * a, const tr_address * b)
    136131{
    137132    static const int sizes[2] = { sizeof(struct in_addr), sizeof(struct in6_addr) };
     
    171166
    172167bool
    173 tr_ssToAddr( tr_address * setme_addr,
    174              tr_port    * setme_port,
    175              const struct sockaddr_storage * from )
     168tr_address_from_sockaddr_storage( tr_address                    * setme_addr,
     169                                  tr_port                        * setme_port,
     170                                  const struct sockaddr_storage * from )
    176171{
    177172    if( from->ss_family == AF_INET )
     
    201196                struct sockaddr_storage * sockaddr)
    202197{
    203     assert( tr_isAddress( addr ) );
     198    assert( tr_address_is_valid( addr ) );
    204199
    205200    if( addr->type == TR_AF_INET )
     
    240235    struct sockaddr_storage source_sock;
    241236
    242     assert( tr_isAddress( addr ) );
    243 
    244     if( !tr_isValidPeerAddress( addr, port ) )
     237    assert( tr_address_is_valid( addr ) );
     238
     239    if( !tr_address_is_valid_for_peers( addr, port ) )
    245240        return -EINVAL;
    246241
     
    270265    {
    271266        tr_err( _( "Couldn't set source address %s on %d: %s" ),
    272                 tr_ntop_non_ts( source_addr ), s, tr_strerror( errno ) );
     267                tr_address_to_string( source_addr ), s, tr_strerror( errno ) );
    273268        return -errno;
    274269    }
     
    286281                || addr->type == TR_AF_INET )
    287282            tr_err( _( "Couldn't connect socket %d to %s, port %d (errno %d - %s)" ),
    288                     s, tr_ntop_non_ts( addr ), (int)ntohs( port ), tmperrno,
     283                    s, tr_address_to_string( addr ), (int)ntohs( port ), tmperrno,
    289284                    tr_strerror( tmperrno ) );
    290285        tr_netClose( session, s );
     
    321316    int optval;
    322317
    323     assert( tr_isAddress( addr ) );
     318    assert( tr_address_is_valid( addr ) );
    324319
    325320    fd = socket( domains[addr->type], SOCK_STREAM, 0 );
     
    366361                fmt = _( "Couldn't bind port %d on %s: %s (%s)" );
    367362
    368             tr_err( fmt, port, tr_ntop_non_ts( addr ), tr_strerror( err ), hint );
     363            tr_err( fmt, port, tr_address_to_string( addr ), tr_strerror( err ), hint );
    369364        }
    370365        tr_netCloseSocket( fd );
     
    374369
    375370    if( !suppressMsgs )
    376         tr_dbg( "Bound socket %d to port %d on %s", fd, port, tr_ntop_non_ts( addr ) );
     371        tr_dbg( "Bound socket %d to port %d on %s", fd, port, tr_address_to_string( addr ) );
    377372
    378373    if( listen( fd, 128 ) == -1 ) {
     
    613608        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    614609
    615     assert( tr_isAddress( a ) );
     610    assert( tr_address_is_valid( a ) );
    616611
    617612    switch( a->type )
     
    645640
    646641bool
    647 tr_isValidPeerAddress( const tr_address * addr, tr_port port )
     642tr_address_is_valid_for_peers( const tr_address * addr, tr_port port )
    648643{
    649644    return ( port != 0 )
    650         && ( tr_isAddress( addr ) )
     645        && ( tr_address_is_valid( addr ) )
    651646        && ( !isIPv6LinkLocalAddress( addr ) )
    652647        && ( !isIPv4MappedAddress( addr ) )
Note: See TracChangeset for help on using the changeset viewer.