(trunk libT) more runtime safety checks

This commit is contained in:
Charles Kerr 2008-12-16 21:06:47 +00:00
parent 56663b860b
commit de5228c415
2 changed files with 41 additions and 8 deletions

View File

@ -68,10 +68,17 @@ tr_netInit( void )
} }
} }
tr_bool
tr_isAddress( const tr_address * a )
{
return a && ( a->type==AF_INET || a->type==AF_INET6 );
}
const char * const char *
tr_ntop( const tr_address * src, char * dst, int size ) tr_ntop( const tr_address * src, char * dst, int size )
{ {
assert( tr_isAddress( src ) );
if( src->type == TR_AF_INET ) if( src->type == TR_AF_INET )
return inet_ntop( AF_INET, &src->addr, dst, size ); return inet_ntop( AF_INET, &src->addr, dst, size );
else else
@ -113,6 +120,8 @@ tr_pton( const char * src, tr_address * dst )
void void
tr_normalizeV4Mapped( tr_address * const addr ) tr_normalizeV4Mapped( tr_address * const addr )
{ {
assert( tr_isAddress( addr ) );
if( addr->type == TR_AF_INET6 && IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 ) ) if( addr->type == TR_AF_INET6 && IN6_IS_ADDR_V4MAPPED( &addr->addr.addr6 ) )
{ {
addr->type = TR_AF_INET; addr->type = TR_AF_INET;
@ -132,10 +141,8 @@ tr_compareAddresses( const tr_address * a, const tr_address * b)
{ {
int addrlen; int addrlen;
assert( a ); assert( tr_isAddress( a ) );
assert( b ); assert( tr_isAddress( b ) );
assert( a->type == TR_AF_INET || a->type == TR_AF_INET6 );
assert( b->type == TR_AF_INET || b->type == TR_AF_INET6 );
/* IPv6 addresses are always "greater than" IPv4 */ /* IPv6 addresses are always "greater than" IPv4 */
if( a->type != b->type ) if( a->type != b->type )
@ -163,7 +170,10 @@ tr_socketListAppend( tr_socketList * const head,
const tr_address * const addr ) const tr_address * const addr )
{ {
tr_socketList * tmp; tr_socketList * tmp;
assert( head ); assert( head );
assert( tr_isAddress( addr ) );
for( tmp = head; tmp->next; tmp = tmp->next ); for( tmp = head; tmp->next; tmp = tmp->next );
tmp->next = tr_socketListNew( addr ); tmp->next = tr_socketListNew( addr );
return tmp->next; return tmp->next;
@ -173,6 +183,9 @@ tr_socketList *
tr_socketListNew( const tr_address * const addr ) tr_socketListNew( const tr_address * const addr )
{ {
tr_socketList * tmp; tr_socketList * tmp;
assert( tr_isAddress( addr ) );
tmp = tr_new( tr_socketList, 1 ); tmp = tr_new( tr_socketList, 1 );
tmp->socket = -1; tmp->socket = -1;
tmp->addr = *addr; tmp->addr = *addr;
@ -184,6 +197,7 @@ void
tr_socketListFree( tr_socketList * const head ) tr_socketListFree( tr_socketList * const head )
{ {
assert( head ); assert( head );
if( head->next ) if( head->next )
tr_socketListFree( head->next ); tr_socketListFree( head->next );
tr_free( head ); tr_free( head );
@ -194,8 +208,10 @@ tr_socketListRemove( tr_socketList * const head,
tr_socketList * const el) tr_socketList * const el)
{ {
tr_socketList * tmp; tr_socketList * tmp;
assert( head ); assert( head );
assert( el ); assert( el );
for( tmp = head; tmp->next && tmp->next != el; tmp = tmp->next ); for( tmp = head; tmp->next && tmp->next != el; tmp = tmp->next );
tmp->next = el->next; tmp->next = el->next;
el->next = NULL; el->next = NULL;
@ -207,17 +223,21 @@ tr_socketListTruncate( tr_socketList * const head,
tr_socketList * const start ) tr_socketList * const start )
{ {
tr_socketList * tmp; tr_socketList * tmp;
assert( head ); assert( head );
assert( start ); assert( start );
for( tmp = head; tmp->next && tmp->next != start; tmp = tmp->next ); for( tmp = head; tmp->next && tmp->next != start; tmp = tmp->next );
tr_socketListFree( start ); tr_socketListFree( start );
tmp->next = NULL; tmp->next = NULL;
} }
#if 0
int int
tr_socketListGetSocket( const tr_socketList * const el ) tr_socketListGetSocket( const tr_socketList * const el )
{ {
assert( el ); assert( el );
return el->socket; return el->socket;
} }
@ -227,6 +247,7 @@ tr_socketListGetAddress( const tr_socketList * const el )
assert( el ); assert( el );
return &el->addr; return &el->addr;
} }
#endif
void void
tr_socketListForEach( tr_socketList * const head, tr_socketListForEach( tr_socketList * const head,
@ -245,8 +266,7 @@ tr_socketListForEach( tr_socketList * const head,
**********************************************************************/ **********************************************************************/
int int
tr_netSetTOS( int s, tr_netSetTOS( int s, int tos )
int tos )
{ {
#ifdef IP_TOS #ifdef IP_TOS
return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) ); return setsockopt( s, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof( tos ) );
@ -301,6 +321,9 @@ setup_sockaddr( const tr_address * addr,
{ {
struct sockaddr_in sock4; struct sockaddr_in sock4;
struct sockaddr_in6 sock6; struct sockaddr_in6 sock6;
assert( tr_isAddress( addr ) );
if( addr->type == TR_AF_INET ) if( addr->type == TR_AF_INET )
{ {
memset( &sock4, 0, sizeof( sock4 ) ); memset( &sock4, 0, sizeof( sock4 ) );
@ -332,6 +355,8 @@ tr_netOpenTCP( tr_session * session,
const int type = SOCK_STREAM; const int type = SOCK_STREAM;
socklen_t addrlen; socklen_t addrlen;
assert( tr_isAddress( addr ) );
if( ( s = createSocket( ( addr->type == TR_AF_INET ? AF_INET : AF_INET6 ), if( ( s = createSocket( ( addr->type == TR_AF_INET ? AF_INET : AF_INET6 ),
type ) ) < 0 ) type ) ) < 0 )
return s; return s;
@ -369,6 +394,8 @@ tr_netBindTCP( const tr_address * addr, tr_port port, tr_bool suppressMsgs )
const int type = SOCK_STREAM; const int type = SOCK_STREAM;
int addrlen; int addrlen;
assert( tr_isAddress( addr ) );
#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT ) #if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
int optval; int optval;
#endif #endif
@ -407,7 +434,11 @@ tr_netAccept( tr_session * session,
tr_address * addr, tr_address * addr,
tr_port * port ) tr_port * port )
{ {
int fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) ); int fd;
assert( tr_isAddress( addr ) );
fd = makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
setSndBuf( session, fd ); setSndBuf( session, fd );
return fd; return fd;
} }

View File

@ -81,6 +81,8 @@ int tr_compareAddresses( const tr_address * a,
const tr_address * b); const tr_address * b);
void tr_normalizeV4Mapped( tr_address * const addr ); void tr_normalizeV4Mapped( tr_address * const addr );
tr_bool tr_isAddress( const tr_address * a );
/*********************************************************************** /***********************************************************************
* Socket list housekeeping * Socket list housekeeping
**********************************************************************/ **********************************************************************/