Changeset 12094


Ignore:
Timestamp:
Mar 4, 2011, 9:00:52 PM (11 years ago)
Author:
jordan
Message:

(trunk libT) new function: tr_ssToAddr()

This handles converting the data in a sockaddr_storage to a tr_address + port, and removes redundant code from fdlimit.c and tr-udp.c that previously did this work.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r12076 r12094  
    679679    if( fd >= 0 )
    680680    {
    681         /* "The ss_family field of the sockaddr_storage structure will always
    682          * align with the family field of any protocol-specific structure." */
    683         if( sock.ss_family == AF_INET )
    684         {
    685             struct sockaddr_in *si;
    686             union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
    687             s.dummy = sock;
    688             si = &s.si;
    689             addr->type = TR_AF_INET;
    690             addr->addr.addr4.s_addr = si->sin_addr.s_addr;
    691             *port = si->sin_port;
    692         }
    693         else
    694         {
    695             struct sockaddr_in6 *si;
    696             union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
    697             s.dummy = sock;
    698             si = &s.si;
    699             addr->type = TR_AF_INET6;
    700             addr->addr.addr6 = si->sin6_addr;
    701             *port = si->sin6_port;
    702         }
     681        tr_ssToAddr( addr, port, &sock );
    703682        ++gFd->socket_count;
    704683    }
  • trunk/libtransmission/net.c

    r11971 r12094  
    250250}
    251251
     252tr_bool
     253tr_ssToAddr( tr_address * setme_addr,     
     254             tr_port    * setme_port,     
     255             const struct sockaddr_storage * from )
     256{
     257    if( from->ss_family == AF_INET )
     258    {
     259        struct sockaddr_in * sin = (struct sockaddr_in *)from;
     260        setme_addr->type = TR_AF_INET;
     261        setme_addr->addr.addr4.s_addr = sin->sin_addr.s_addr;
     262        *setme_port = sin->sin_port;
     263        return TRUE;
     264    }
     265
     266    if( from->ss_family == AF_INET6 )
     267    {
     268        struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) from;
     269        setme_addr->type = TR_AF_INET6;
     270        setme_addr->addr.addr6 = sin6->sin6_addr;
     271        *setme_port = sin6->sin6_port;
     272        return TRUE;
     273    }
     274
     275    return FALSE;
     276}
     277
    252278static socklen_t
    253279setup_sockaddr( const tr_address        * addr,
  • trunk/libtransmission/net.h

    r11971 r12094  
    8080extern const tr_address tr_in6addr_any;
    8181
     82tr_bool tr_ssToAddr( tr_address * setme_addr,     
     83                     tr_port    * setme_port,     
     84                     const struct sockaddr_storage * from );
     85
    8286const char *tr_ntop( const tr_address * src,
    8387                     char * dst,
     
    129133void tr_netInit( void );
    130134
     135
    131136/**
    132137 * @brief get a human-representable string representing the network error.
  • trunk/libtransmission/tr-utp.c

    r12077 r12094  
    115115   
    116116    UTP_GetPeerName(s, from, &fromlen);
    117     if(from->sa_family == AF_INET) {
    118         struct sockaddr_in *sin = (struct sockaddr_in*)from;
    119         addr.type = TR_AF_INET;
    120         addr.addr.addr4.s_addr = sin->sin_addr.s_addr;
    121         port = sin->sin_port;
    122     } else if(from->sa_family == AF_INET6) {
    123         struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)from;
    124         addr.type = TR_AF_INET6;
    125         addr.addr.addr6 = sin6->sin6_addr;
    126         port = sin6->sin6_port;
    127     } else {
     117    if( !tr_ssToAddr( &addr, &port, &from_storage ) )
     118    {
    128119        tr_nerr("UTP", "Unknown socket family");
    129120        UTP_Close(s);
Note: See TracChangeset for help on using the changeset viewer.