diff --git a/libtransmission/port-forwarding.c b/libtransmission/port-forwarding.c index ccdb9dc8f..6595670cc 100644 --- a/libtransmission/port-forwarding.c +++ b/libtransmission/port-forwarding.c @@ -108,21 +108,21 @@ onTimer( int fd UNUSED, short what UNUSED, void * vshared ) /* if we're mapped, everything is fine... check back in 20 minutes * to renew the port forwarding if it's expired */ s->doPortCheck = TRUE; - interval.tv_sec = 60*20; + tr_timevalSet( &interval, 60*20, 0 ); break; case TR_PORT_ERROR: /* some kind of an error. wait 60 seconds and retry */ - interval.tv_sec = 60; + tr_timevalSet( &interval, 60, 0 ); break; default: /* in progress. pulse frequently. */ - interval.tv_sec = 0; - interval.tv_usec = 333000; + tr_timevalSet( &interval, 0, 333000 ); break; } + assert( tr_isTimeval( &interval ) ); evtimer_add( s->timer, &interval ); } @@ -143,10 +143,10 @@ tr_sharedInit( tr_session * session, tr_bool isEnabled ) if( isEnabled ) { struct timeval timeval; - timeval.tv_sec = 0; - timeval.tv_usec = 333000; + s->timer = tr_new0( struct event, 1 ); evtimer_set( s->timer, onTimer, s ); + tr_timevalSet( &timeval, 0, 333000 ); evtimer_add( s->timer, &timeval ); } diff --git a/libtransmission/session.c b/libtransmission/session.c index 205aec7d0..623ee847c 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -1068,8 +1068,7 @@ setAltTimer( tr_session * session ) assert( session->altTimer != NULL ); tr_localtime_r( &now, &tm ); - tv.tv_sec = 60 - tm.tm_sec; - tv.tv_usec = 0; + tr_timevalSet( &tv, 60-tm.tm_sec, 0 ); evtimer_add( session->altTimer, &tv ); } diff --git a/libtransmission/tr-dht.c b/libtransmission/tr-dht.c index 651936d2c..1d42764cb 100644 --- a/libtransmission/tr-dht.c +++ b/libtransmission/tr-dht.c @@ -91,9 +91,8 @@ dht_bootstrap(void *closure) if(status == TR_DHT_STOPPED || status >= TR_DHT_FIREWALLED) break; tr_dhtAddNode(cl->session, &addr, port, 1); - tv.tv_sec = 2 + tr_cryptoWeakRandInt( 5 ); - tv.tv_usec = tr_cryptoWeakRandInt( 1000000 ); - select(0, NULL, NULL, NULL, &tv); + tr_timevalSet( &tv, 2 + tr_cryptoWeakRandInt( 5 ), tr_cryptoWeakRandInt( 1000000 ) ); + select( 0, NULL, NULL, NULL, &tv ); } tr_free( cl->nodes ); tr_free( closure ); @@ -169,9 +168,9 @@ tr_dhtInit(tr_session *ss) tr_threadNew( dht_bootstrap, cl ); } - tv.tv_sec = 0; - tv.tv_usec = tr_cryptoWeakRandInt( 1000000 ); + tr_timevalSet( &tv, 0, tr_cryptoWeakRandInt( 1000000 ) ); event_set( &dht_event, dht_socket, EV_READ, event_callback, NULL ); + assert( tr_isTimeval( &tv ) ); event_add( &dht_event, &tv ); return 1; @@ -376,9 +375,8 @@ event_callback(int s, short type, void *ignore UNUSED ) /* Being slightly late is fine, and has the added benefit of adding some jitter. */ - tv.tv_sec = tosleep; - tv.tv_usec = tr_cryptoWeakRandInt( 1000000 ); - event_add(&dht_event, &tv); + tr_timevalSet( &tv, tosleep, tr_cryptoWeakRandInt( 1000000 ) ); + event_add( &dht_event, &tv ); } void diff --git a/libtransmission/trevent.c b/libtransmission/trevent.c index 60909c87a..18fb2169a 100644 --- a/libtransmission/trevent.c +++ b/libtransmission/trevent.c @@ -303,10 +303,12 @@ timerCallback( int fd UNUSED, more = ( *timer->func )( timer->user_data ); timer->inCallback = 0; - if( more ) - evtimer_add( &timer->event, &timer->tv ); - else + if( !more ) tr_timerFree( &timer ); + else { + assert( tr_isTimeval( &timer->tv ) ); + evtimer_add( &timer->event, &timer->tv ); + } } void diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 798c9938c..c1c61c83e 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -457,6 +457,14 @@ tr_strip_positional_args( const char* str ) *** **/ +tr_bool +tr_isTimeval( const struct timeval * tv ) +{ + return tv && ( tv->tv_sec >= 0 ) + && ( tv->tv_usec >= 0 ) + && ( tv->tv_usec < 1000000 ); +} + void tr_timevalMsec( uint64_t milliseconds, struct timeval * setme ) { @@ -464,8 +472,22 @@ tr_timevalMsec( uint64_t milliseconds, struct timeval * setme ) assert( setme != NULL ); setme->tv_sec = microseconds / 1000000; setme->tv_usec = microseconds % 1000000; + assert( tr_isTimeval( setme ) ); } +void +tr_timevalSet( struct timeval * setme, int seconds, int microseconds ) +{ + setme->tv_sec = seconds; + setme->tv_usec = microseconds; + assert( tr_isTimeval( setme ) ); +} + + +/** +*** +**/ + uint8_t * tr_loadFile( const char * path, size_t * size ) diff --git a/libtransmission/utils.h b/libtransmission/utils.h index e67891bfb..0dfa617d4 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -238,7 +238,11 @@ char* tr_buildPath( const char * first_element, ... ) TR_GNUC_NULL_TERMINATED struct timeval; -void tr_timevalMsec( uint64_t milliseconds, struct timeval * setme ); +tr_bool tr_isTimeval( const struct timeval * tv ); + +void tr_timevalMsec( uint64_t milliseconds, struct timeval * setme ); + +void tr_timevalSet( struct timeval * setme, int seconds, int microseconds ); /** @brief return the current date in milliseconds */