Ignore:
Timestamp:
Apr 8, 2006, 3:10:52 PM (16 years ago)
Author:
titer
Message:

Fixed a couple of bugs introduced in [164]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/net.c

    r164 r218  
    6767    tr_lock_t      lock;
    6868    tr_thread_t    thread;
     69    int            orphan;
    6970};
     71
     72/* Hem, global variable. Initialized from tr_init(). */
     73tr_lock_t gethostbynameLock;
    7074
    7175static void resolveFunc( void * _r )
     
    7579
    7680    tr_lockLock( &r->lock );
     81
    7782    r->addr.s_addr = inet_addr( r->address );
    7883    if( r->addr.s_addr != 0xFFFFFFFF )
    7984    {
     85        /* This was an IP address, no resolving required */
    8086        r->status = TR_RESOLVE_OK;
     87        goto resolveDone;
     88    }
     89
     90    tr_lockLock( &gethostbynameLock );
     91    tr_lockUnlock( &r->lock );
     92    host = gethostbyname( r->address );
     93    tr_lockLock( &r->lock );
     94    if( host )
     95    {
     96        memcpy( &r->addr, host->h_addr, host->h_length );
     97        r->status = TR_RESOLVE_OK;
     98    }
     99    else
     100    {
     101        r->status = TR_RESOLVE_ERROR;
     102    }
     103    tr_lockUnlock( &gethostbynameLock );
     104
     105resolveDone:
     106    if( r->orphan )
     107    {
     108        /* tr_netResolveClose was closed already. Free memory */
    81109        tr_lockUnlock( &r->lock );
    82         return;
    83     }
    84     tr_lockUnlock( &r->lock );
    85 
    86     if( !( host = gethostbyname( r->address ) ) )
    87     {
    88         tr_lockLock( &r->lock );
    89         r->status = TR_RESOLVE_ERROR;
     110        tr_lockClose( &r->lock );
     111        free( r );
     112    }
     113    else
     114    {
    90115        tr_lockUnlock( &r->lock );
    91         return;
    92     }
    93     tr_lockLock( &r->lock );
    94     memcpy( &r->addr, host->h_addr, host->h_length );
    95     r->status = TR_RESOLVE_OK;
    96     tr_lockUnlock( &r->lock );
     116    }
    97117}
    98118
     
    106126    tr_lockInit( &r->lock );
    107127    tr_threadCreate( &r->thread, resolveFunc, r );
     128    r->orphan = 0;
    108129
    109130    return r;
     
    127148void tr_netResolveClose( tr_resolve_t * r )
    128149{
     150    tr_lockLock( &r->lock );
     151    if( r->status == TR_RESOLVE_WAIT )
     152    {
     153        /* Let the thread die */
     154        r->orphan = 1;
     155        tr_lockUnlock( &r->lock );
     156        return;
     157    }
     158    tr_lockUnlock( &r->lock );
     159
     160    /* Clean up */
    129161    tr_threadJoin( &r->thread );
    130162    tr_lockClose( &r->lock );
Note: See TracChangeset for help on using the changeset viewer.