Ignore:
Timestamp:
Sep 20, 2007, 4:32:01 PM (14 years ago)
Author:
livings124
Message:

merge encryption branch to trunk (xcode project is still out of date)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/net.c

    r2614 r3105  
    5858/***********************************************************************
    5959 * DNS resolution
    60  **********************************************************************/
    61 
    62 /***********************************************************************
    63  * tr_netResolve
    64  ***********************************************************************
     60 *
    6561 * Synchronous "resolution": only works with character strings
    6662 * representing numbers expressed in the Internet standard `.' notation.
     
    7167    addr->s_addr = inet_addr( address );
    7268    return ( addr->s_addr == 0xFFFFFFFF );
    73 }
    74 
    75 static tr_thread_t  * resolveThread;
    76 static tr_lock_t    * resolveLock;
    77 static tr_cond_t    * resolveCond;
    78 static volatile int   resolveDie;
    79 static tr_resolve_t * resolveQueue;
    80 
    81 static void resolveRelease ( tr_resolve_t * );
    82 static void resolveFunc    ( void * );
    83 
    84 struct tr_resolve_s
    85 {
    86     tr_tristate_t  status;
    87     char           * address;
    88     struct in_addr addr;
    89 
    90     int            refcount;
    91     tr_resolve_t   * next;
    92 };
    93 
    94 /***********************************************************************
    95  * tr_netResolveThreadInit
    96  ***********************************************************************
    97  * Initializes the static variables used for resolution and launch the
    98  * gethostbyname thread.
    99  **********************************************************************/
    100 void tr_netResolveThreadInit( void )
    101 {
    102     resolveDie   = 0;
    103     resolveQueue = NULL;
    104     resolveLock = tr_lockNew( );
    105     resolveCond = tr_condNew( );
    106     resolveThread = tr_threadNew( resolveFunc, NULL, "resolve" );
    107 }
    108 
    109 /***********************************************************************
    110  * tr_netResolveThreadClose
    111  ***********************************************************************
    112  * Notices the gethostbyname thread that is should terminate. Doesn't
    113  * wait until it does, in case it is stuck in a resolution: we let it
    114  * die and clean itself up.
    115  **********************************************************************/
    116 void tr_netResolveThreadClose( void )
    117 {
    118     tr_lockLock( resolveLock );
    119     resolveDie = 1;
    120     tr_lockUnlock( resolveLock );
    121     tr_condSignal( resolveCond );
    122     tr_wait( 200 );
    123 }
    124 
    125 /***********************************************************************
    126  * tr_netResolveInit
    127  ***********************************************************************
    128  * Adds an address to the resolution queue.
    129  **********************************************************************/
    130 tr_resolve_t * tr_netResolveInit( const char * address )
    131 {
    132     tr_resolve_t * r = tr_new0( tr_resolve_t, 1 );
    133     r->status   = TR_NET_WAIT;
    134     r->address  = strdup( address );
    135     r->refcount = 2;
    136     r->next     = NULL;
    137 
    138     tr_lockLock( resolveLock );
    139     if( !resolveQueue )
    140     {
    141         resolveQueue = r;
    142     }
    143     else
    144     {
    145         tr_resolve_t * iter;
    146         for( iter = resolveQueue; iter->next; iter = iter->next );
    147         iter->next = r;
    148     }
    149     tr_lockUnlock( resolveLock );
    150     tr_condSignal( resolveCond );
    151 
    152     return r;
    153 }
    154 
    155 /***********************************************************************
    156  * tr_netResolvePulse
    157  ***********************************************************************
    158  * Checks the current status of a resolution.
    159  **********************************************************************/
    160 tr_tristate_t tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
    161 {
    162     tr_tristate_t ret;
    163 
    164     tr_lockLock( resolveLock );
    165     ret = r->status;
    166     if( ret == TR_NET_OK )
    167     {
    168         *addr = r->addr;
    169     }
    170     tr_lockUnlock( resolveLock );
    171 
    172     return ret;
    173 }
    174 
    175 /***********************************************************************
    176  * tr_netResolveClose
    177  ***********************************************************************
    178  *
    179  **********************************************************************/
    180 void tr_netResolveClose( tr_resolve_t * r )
    181 {
    182     resolveRelease( r );
    183 }
    184 
    185 /***********************************************************************
    186  * resolveRelease
    187  ***********************************************************************
    188  * The allocated tr_resolve_t structures should be freed when
    189  * tr_netResolveClose was called *and* it was removed from the queue.
    190  * This can happen in any order, so we use a refcount to know we can
    191  * take it out.
    192  **********************************************************************/
    193 static void resolveRelease( tr_resolve_t * r )
    194 {
    195     if( --r->refcount < 1 )
    196     {
    197         free( r->address );
    198         free( r );
    199     }
    200 }
    201 
    202 /***********************************************************************
    203  * resolveFunc
    204  ***********************************************************************
    205  * Keeps waiting for addresses to resolve, and removes them from the
    206  * queue once resolution is done.
    207  **********************************************************************/
    208 static void resolveFunc( void * arg UNUSED )
    209 {
    210     tr_resolve_t * r;
    211     struct hostent * host;
    212 
    213     tr_lockLock( resolveLock );
    214 
    215     while( !resolveDie )
    216     {
    217         if( !( r = resolveQueue ) )
    218         {
    219             tr_condWait( resolveCond, resolveLock );
    220             continue;
    221         }
    222 
    223         /* Blocking resolution */
    224         tr_lockUnlock( resolveLock );
    225         host = gethostbyname( r->address );
    226         tr_lockLock( resolveLock );
    227 
    228         if( host )
    229         {
    230             memcpy( &r->addr, host->h_addr, host->h_length );
    231             r->status = TR_NET_OK;
    232         }
    233         else
    234         {
    235             r->status = TR_NET_ERROR;
    236         }
    237        
    238         resolveQueue = r->next;
    239         resolveRelease( r );
    240     }
    241 
    242     /* Clean up  */
    243     tr_lockUnlock( resolveLock );
    244     tr_lockFree( resolveLock );
    245     resolveLock = NULL;
    246     while( ( r = resolveQueue ) )
    247     {
    248         resolveQueue = r->next;
    249         resolveRelease( r );
    250     }
    25169}
    25270
     
    470288}
    471289
    472 void tr_netClose( int s )
     290void
     291tr_netClose( int s )
    473292{
    474293    tr_fdSocketClose( s );
    475294}
    476295
    477 void tr_netNtop( const struct in_addr * addr, char * buf, int len )
     296void
     297tr_netNtop( const struct in_addr * addr, char * buf, int len )
    478298{
    479299    const uint8_t * cast;
Note: See TracChangeset for help on using the changeset viewer.