The server tried to connect to other servers only once when DNS or socket

failures occured.
This commit is contained in:
Alexander Barton 2003-04-25 16:47:52 +00:00
parent a1ff081b8a
commit 83194a23a3
1 changed files with 38 additions and 30 deletions

View File

@ -16,7 +16,7 @@
#include "portab.h" #include "portab.h"
static char UNUSED id[] = "$Id: conn.c,v 1.122 2003/04/21 10:52:26 alex Exp $"; static char UNUSED id[] = "$Id: conn.c,v 1.123 2003/04/25 16:47:52 alex Exp $";
#include "imp.h" #include "imp.h"
#include <assert.h> #include <assert.h>
@ -151,7 +151,7 @@ Conn_Exit( VOID )
#ifdef RENDEZVOUS #ifdef RENDEZVOUS
Rendezvous_UnregisterListeners( ); Rendezvous_UnregisterListeners( );
#endif #endif
/* Sockets schliessen */ /* Sockets schliessen */
for( i = 0; i < Conn_MaxFD + 1; i++ ) for( i = 0; i < Conn_MaxFD + 1; i++ )
{ {
@ -183,7 +183,7 @@ Conn_Exit( VOID )
} }
} }
} }
free( My_Connections ); free( My_Connections );
My_Connections = NULL; My_Connections = NULL;
Pool_Size = 0; Pool_Size = 0;
@ -217,7 +217,7 @@ Conn_ExitListeners( VOID )
#ifdef RENDEZVOUS #ifdef RENDEZVOUS
Rendezvous_UnregisterListeners( ); Rendezvous_UnregisterListeners( );
#endif #endif
Log( LOG_INFO, "Shutting down all listening sockets ..." ); Log( LOG_INFO, "Shutting down all listening sockets ..." );
for( i = 0; i < Conn_MaxFD + 1; i++ ) for( i = 0; i < Conn_MaxFD + 1; i++ )
{ {
@ -240,7 +240,7 @@ Conn_NewListener( CONST UINT Port )
#ifdef RENDEZVOUS #ifdef RENDEZVOUS
CHAR name[CLIENT_ID_LEN], *info; CHAR name[CLIENT_ID_LEN], *info;
#endif #endif
/* Server-"Listen"-Socket initialisieren */ /* Server-"Listen"-Socket initialisieren */
memset( &addr, 0, sizeof( addr )); memset( &addr, 0, sizeof( addr ));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@ -415,7 +415,7 @@ Conn_Handler( VOID )
tv.tv_usec = 0; tv.tv_usec = 0;
if( timeout ) tv.tv_sec = TIME_RES; if( timeout ) tv.tv_sec = TIME_RES;
else tv.tv_sec = 0; else tv.tv_sec = 0;
/* Auf Aktivitaet warten */ /* Auf Aktivitaet warten */
i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv ); i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
if( i == 0 ) if( i == 0 )
@ -443,7 +443,7 @@ Conn_Handler( VOID )
/* Es kann geschrieben werden ... */ /* Es kann geschrieben werden ... */
idx = Socket2Index( i ); idx = Socket2Index( i );
if( idx == NONE ) continue; if( idx == NONE ) continue;
if( ! Handle_Write( idx )) if( ! Handle_Write( idx ))
{ {
/* Fehler beim Schreiben! Diesen Socket nun /* Fehler beim Schreiben! Diesen Socket nun
@ -899,7 +899,7 @@ New_Connection( INT Sock )
Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno )); Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
return; return;
} }
#ifdef USE_TCPWRAP #ifdef USE_TCPWRAP
/* Validate socket using TCP Wrappers */ /* Validate socket using TCP Wrappers */
request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL ); request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL );
@ -921,10 +921,10 @@ New_Connection( INT Sock )
if( idx >= Pool_Size ) if( idx >= Pool_Size )
{ {
new_size = Pool_Size + CONNECTION_POOL; new_size = Pool_Size + CONNECTION_POOL;
/* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden. /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
* Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */ * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
if( Conf_MaxConnections > 0 ) if( Conf_MaxConnections > 0 )
{ {
/* Es ist ein Limit konfiguriert */ /* Es ist ein Limit konfiguriert */
@ -945,7 +945,7 @@ New_Connection( INT Sock )
close( new_sock ); close( new_sock );
return; return;
} }
/* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
* und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
* dynamische Verwaltung waere wohl _deutlich_ besser ...) */ * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
@ -962,21 +962,21 @@ New_Connection( INT Sock )
close( new_sock ); close( new_sock );
return; return;
} }
/* Struktur umkopieren ... */ /* Struktur umkopieren ... */
memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size ); memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [malloc()/memcpy()]", new_size, sizeof( CONNECTION ) * new_size ); Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [malloc()/memcpy()]", new_size, sizeof( CONNECTION ) * new_size );
} }
else Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size ); else Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
/* Adjust pointer to new block */ /* Adjust pointer to new block */
My_Connections = ptr; My_Connections = ptr;
/* Initialize new items */ /* Initialize new items */
for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx ); for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
idx = Pool_Size; idx = Pool_Size;
/* Adjust new pool size */ /* Adjust new pool size */
Pool_Size = new_size; Pool_Size = new_size;
} }
@ -1011,7 +1011,7 @@ New_Connection( INT Sock )
/* Sub-Prozess wurde asyncron gestartet */ /* Sub-Prozess wurde asyncron gestartet */
My_Connections[idx].res_stat = s; My_Connections[idx].res_stat = s;
} }
/* Penalty-Zeit setzen */ /* Penalty-Zeit setzen */
Conn_SetPenalty( idx, 4 ); Conn_SetPenalty( idx, 4 );
} /* New_Connection */ } /* New_Connection */
@ -1142,14 +1142,14 @@ Handle_Buffer( CONN_ID Idx )
if( ! Unzip_Buffer( Idx )) return FALSE; if( ! Unzip_Buffer( Idx )) return FALSE;
} }
#endif #endif
if( My_Connections[Idx].rdatalen < 1 ) break; if( My_Connections[Idx].rdatalen < 1 ) break;
/* Eine komplette Anfrage muss mit CR+LF enden, vgl. /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
* RFC 2812. Haben wir eine? */ * RFC 2812. Haben wir eine? */
My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0'; My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
ptr = strstr( My_Connections[Idx].rbuf, "\r\n" ); ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
if( ptr ) delta = 2; if( ptr ) delta = 2;
#ifndef STRICT_RFC #ifndef STRICT_RFC
else else
@ -1164,7 +1164,7 @@ Handle_Buffer( CONN_ID Idx )
else if( ptr2 ) ptr = ptr2; else if( ptr2 ) ptr = ptr2;
} }
#endif #endif
action = FALSE; action = FALSE;
if( ptr ) if( ptr )
{ {
@ -1219,10 +1219,10 @@ Handle_Buffer( CONN_ID Idx )
} }
#endif #endif
} }
if( action ) result = TRUE; if( action ) result = TRUE;
} while( action ); } while( action );
return result; return result;
} /* Handle_Buffer */ } /* Handle_Buffer */
@ -1348,7 +1348,7 @@ Check_Servers( VOID )
LOCAL VOID LOCAL VOID
New_Server( INT Server, CONN_ID Idx ) New_Server( INT Server, CONN_ID Idx )
{ {
/* Neue Server-Verbindung aufbauen */ /* Establish new server link */
struct sockaddr_in new_addr; struct sockaddr_in new_addr;
struct in_addr inaddr; struct in_addr inaddr;
@ -1358,11 +1358,12 @@ New_Server( INT Server, CONN_ID Idx )
assert( Server > NONE ); assert( Server > NONE );
assert( Idx > NONE ); assert( Idx > NONE );
/* Wurde eine gueltige IP-Adresse gefunden? */ /* Did we get a valid IP address? */
if( ! Conf_Server[Server].ip[0] ) if( ! Conf_Server[Server].ip[0] )
{ {
/* Nein. Verbindung wieder freigeben: */ /* No. Free connection structure and abort: */
Init_Conn_Struct( Idx ); Init_Conn_Struct( Idx );
Conf_Server[Server].conn_id = NONE;
Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx ); Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
return; return;
} }
@ -1377,8 +1378,9 @@ New_Server( INT Server, CONN_ID Idx )
if( inaddr.s_addr == (unsigned)-1 ) if( inaddr.s_addr == (unsigned)-1 )
#endif #endif
{ {
/* Konnte Adresse nicht konvertieren */ /* Can't convert IP address */
Init_Conn_Struct( Idx ); Init_Conn_Struct( Idx );
Conf_Server[Server].conn_id = NONE;
Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): can't convert ip address %s!", Conf_Server[Server].host, Idx, Conf_Server[Server].ip ); Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): can't convert ip address %s!", Conf_Server[Server].host, Idx, Conf_Server[Server].ip );
return; return;
} }
@ -1391,7 +1393,9 @@ New_Server( INT Server, CONN_ID Idx )
new_sock = socket( PF_INET, SOCK_STREAM, 0 ); new_sock = socket( PF_INET, SOCK_STREAM, 0 );
if ( new_sock < 0 ) if ( new_sock < 0 )
{ {
/* Can't create socket */
Init_Conn_Struct( Idx ); Init_Conn_Struct( Idx );
Conf_Server[Server].conn_id = NONE;
Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno )); Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
return; return;
} }
@ -1401,9 +1405,11 @@ New_Server( INT Server, CONN_ID Idx )
res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )); res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
if(( res != 0 ) && ( errno != EINPROGRESS )) if(( res != 0 ) && ( errno != EINPROGRESS ))
{ {
/* Can't connect socket */
Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno )); Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
close( new_sock ); close( new_sock );
Init_Conn_Struct( Idx ); Init_Conn_Struct( Idx );
Conf_Server[Server].conn_id = NONE;
return; return;
} }
@ -1411,24 +1417,26 @@ New_Server( INT Server, CONN_ID Idx )
c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE ); c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
if( ! c ) if( ! c )
{ {
/* Can't create new client structure */
close( new_sock ); close( new_sock );
Init_Conn_Struct( Idx ); Init_Conn_Struct( Idx );
Conf_Server[Server].conn_id = NONE;
Log( LOG_ALERT, "Can't establish connection: can't create client structure!" ); Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
return; return;
} }
Client_SetIntroducer( c, c ); Client_SetIntroducer( c, c );
Client_SetToken( c, TOKEN_OUTBOUND ); Client_SetToken( c, TOKEN_OUTBOUND );
/* Verbindung registrieren */ /* Register connection */
My_Connections[Idx].sock = new_sock; My_Connections[Idx].sock = new_sock;
My_Connections[Idx].addr = new_addr; My_Connections[Idx].addr = new_addr;
strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host )); strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
/* Neuen Socket registrieren */ /* Register new socket */
FD_SET( new_sock, &My_Sockets ); FD_SET( new_sock, &My_Sockets );
FD_SET( new_sock, &My_Connects ); FD_SET( new_sock, &My_Connects );
if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock; if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock ); Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
} /* New_Server */ } /* New_Server */
@ -1531,7 +1539,7 @@ Read_Resolver_Result( INT r_fd )
} }
Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result ); Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
/* Aufraeumen */ /* Aufraeumen */
close( My_Connections[i].res_stat->pipe[0] ); close( My_Connections[i].res_stat->pipe[0] );
close( My_Connections[i].res_stat->pipe[1] ); close( My_Connections[i].res_stat->pipe[1] );