* Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
* der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
*
- * $Id: conn.c,v 1.26 2002/01/02 02:50:47 alex Exp $
+ * $Id: conn.c,v 1.57 2002/03/26 23:47:45 alex Exp $
*
* connect.h: Verwaltung aller Netz-Verbindungen ("connections")
- *
- * $Log: conn.c,v $
- * Revision 1.26 2002/01/02 02:50:47 alex
- * - Asyncroner Resolver Hostname->IP.
- * - Server-Links begonnen zu implementieren. Die Verbindung wird aufgebaut,
- * jedoch noch keine SERVER-Befehle verschickt.
- * - Diverse Bug-Fixes und kleinere Erweiterungen.
- *
- * Revision 1.24 2002/01/01 18:25:44 alex
- * - #include's fuer stdlib.h ergaenzt.
- *
- * Revision 1.23 2001/12/31 02:18:51 alex
- * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
- * - neuen Header "defines.h" mit (fast) allen Konstanten.
- * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
- *
- * Revision 1.22 2001/12/30 19:26:11 alex
- * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
- *
- * Revision 1.21 2001/12/29 22:33:36 alex
- * - bessere Dokumentation des Modules bzw. der Funktionen.
- *
- * Revision 1.20 2001/12/29 22:09:43 alex
- * - kleinere Aenderungen ("clean-ups") bei Logging (Resolver).
- *
- * Revision 1.19 2001/12/29 21:53:57 alex
- * - Da hatte ich mich wohl ein wenig verrannt; jetzt sollte der Resolver
- * aber tatsaechlich funktionieren.
- *
- * Revision 1.18 2001/12/29 20:17:25 alex
- * - asyncronen Resolver (IP->Name) implementiert, dadurch div. Aenderungen.
- *
- * Revision 1.17 2001/12/29 03:06:16 alex
- * - Loglevel (nochmal) angepasst.
- *
- * Revision 1.16 2001/12/27 19:32:44 alex
- * - bei "Null-Requests" wird nichts mehr geloggt. Uberfluessig, da normal.
- *
- * Revision 1.15 2001/12/27 16:35:04 alex
- * - vergessene Variable bei Ping-Timeout-Logmeldung ergaenzt. Opsa.
- *
- * Revision 1.14 2001/12/26 14:45:37 alex
- * - "Code Cleanups".
- *
- * Revision 1.13 2001/12/26 03:36:57 alex
- * - Verbindungen mit Lesefehlern werden nun korrekt terminiert.
- *
- * Revision 1.12 2001/12/26 03:20:53 alex
- * - PING/PONG-Timeout implementiert.
- *
- * Revision 1.11 2001/12/25 23:15:16 alex
- * - buffer werden nun periodisch geprueft, keine haengenden Clients mehr.
- *
- * Revision 1.10 2001/12/25 22:03:47 alex
- * - Conn_Close() eingefuehrt: war die lokale Funktion Close_Connection().
- *
- * Revision 1.9 2001/12/24 01:32:33 alex
- * - in Conn_WriteStr() wurde das CR+LF nicht angehaengt!
- * - Fehler-Ausgaben vereinheitlicht.
- *
- * Revision 1.8 2001/12/23 22:02:54 alex
- * - Conn_WriteStr() nimmt nun variable Parameter,
- * - diverse kleinere Aenderungen.
- *
- * Revision 1.7 2001/12/21 22:24:25 alex
- * - kleinere Aenderungen an den Log-Meldungen,
- * - Parse_Request() wird aufgerufen.
- *
- * Revision 1.6 2001/12/15 00:11:55 alex
- * - Lese- und Schreib-Puffer implementiert.
- * - einige neue (Unter-)Funktionen eingefuehrt.
- * - diverse weitere kleinere Aenderungen.
- *
- * Revision 1.5 2001/12/14 08:16:47 alex
- * - Begonnen, Client-spezifische Lesepuffer zu implementieren.
- * - Umstellung auf Datentyp "CONN_ID".
- *
- * Revision 1.4 2001/12/13 02:04:16 alex
- * - boesen "Speicherschiesser" in Log() gefixt.
- *
- * Revision 1.3 2001/12/13 01:33:09 alex
- * - Conn_Handler() unterstuetzt nun einen Timeout.
- * - fuer Verbindungen werden keine FILE-Handles mehr benutzt.
- * - kleinere "Code Cleanups" ;-)
- *
- * Revision 1.2 2001/12/12 23:32:02 alex
- * - diverse Erweiterungen und Verbesserungen (u.a. sind nun mehrere
- * Verbindungen und Listen-Sockets moeglich).
- *
- * Revision 1.1 2001/12/12 17:18:38 alex
- * - Modul zur Verwaltung aller Netzwerk-Verbindungen begonnen.
*/
-#include <portab.h>
-#include "global.h"
+#include "portab.h"
-#include <imp.h>
+#include "imp.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
#include <netdb.h>
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#else
+#define PF_INET AF_INET
+#endif
+
#ifdef HAVE_STDINT_H
#include <stdint.h> /* u.a. fuer Mac OS X */
#endif
#include "parse.h"
#include "tool.h"
-#include <exp.h>
+#include "exp.h"
#include "conn.h"
-#define SERVER_WAIT NONE - 1
+#define SERVER_WAIT (NONE - 1)
typedef struct _Connection
LOCAL VOID Check_Connections( VOID );
LOCAL VOID Check_Servers( VOID );
LOCAL VOID Init_Conn_Struct( INT Idx );
+LOCAL BOOLEAN Init_Socket( INT Sock );
LOCAL VOID New_Server( INT Server, CONN_ID Idx );
LOCAL RES_STAT *ResolveAddr( struct sockaddr_in *Addr );
LOCAL fd_set My_Listeners;
LOCAL fd_set My_Sockets;
LOCAL fd_set My_Resolvers;
+LOCAL fd_set My_Connects;
LOCAL INT My_Max_Fd;
FD_ZERO( &My_Listeners );
FD_ZERO( &My_Sockets );
FD_ZERO( &My_Resolvers );
+ FD_ZERO( &My_Connects );
My_Max_Fd = 0;
INT i;
/* Sockets schliessen */
+ Log( LOG_DEBUG, "Shutting down all connections ..." );
for( i = 0; i < My_Max_Fd + 1; i++ )
{
if( FD_ISSET( i, &My_Sockets ))
{
if( My_Connections[idx].sock == i ) break;
}
- if( idx < MAX_CONNECTIONS ) Conn_Close( idx, "Server going down ..." );
- else if( FD_ISSET( i, &My_Listeners ))
+ if( FD_ISSET( i, &My_Listeners ))
{
close( i );
- Log( LOG_INFO, "Listening socket %d closed.", i );
+ Log( LOG_DEBUG, "Listening socket %d closed.", i );
}
+ else if( FD_ISSET( i, &My_Connects ))
+ {
+ close( i );
+ Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
+ }
+ else if( idx < MAX_CONNECTIONS ) Conn_Close( idx, NULL, "Server going down", TRUE );
else
{
+ Log( LOG_WARNING, "Closing unknown connection %d ...", i );
close( i );
- Log( LOG_WARNING, "Unknown connection %d closed.", i );
}
}
}
* Socket nicht erteugt werden, so wird NULL geliefert.*/
struct sockaddr_in addr;
- INT sock, on = 1;
+ INT sock;
/* Server-"Listen"-Socket initialisieren */
memset( &addr, 0, sizeof( addr ));
sock = socket( PF_INET, SOCK_STREAM, 0);
if( sock < 0 )
{
- Log( LOG_ALERT, "Can't create socket: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
return FALSE;
}
- /* Socket-Optionen setzen */
- if( fcntl( sock, F_SETFL, O_NONBLOCK ) != 0 )
- {
- Log( LOG_ALERT, "Can't enable non-blocking mode: %s!", strerror( errno ));
- close( sock );
- return FALSE;
- }
- if( setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
- {
- Log( LOG_CRIT, "Can't set socket options: %s!", strerror( errno ));
- /* dieser Fehler kann ignoriert werden. */
- }
+ if( ! Init_Socket( sock )) return FALSE;
/* an Port binden */
if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
{
- Log( LOG_ALERT, "Can't bind socket: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
close( sock );
return FALSE;
}
/* in "listen mode" gehen :-) */
if( listen( sock, 10 ) != 0 )
{
- Log( LOG_ALERT, "Can't listen on soecket: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
close( sock );
return FALSE;
}
Handle_Buffer( i );
}
}
-
+
/* noch volle Schreib-Puffer suchen */
FD_ZERO( &write_sockets );
for( i = 0; i < MAX_CONNECTIONS; i++ )
FD_SET( My_Connections[i].sock, &write_sockets );
}
}
+ /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
+ for( i = 0; i < MAX_CONNECTIONS; i++ )
+ {
+ if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects ))) FD_SET( My_Connections[i].sock, &write_sockets );
+ }
/* von welchen Sockets koennte gelesen werden? */
read_sockets = My_Sockets;
/* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
FD_CLR( My_Connections[i].sock, &read_sockets );
}
+ if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
+ {
+ /* Hier laeuft noch ein asyncrones connect() */
+ FD_CLR( My_Connections[i].sock, &read_sockets );
+ }
}
for( i = 0; i < My_Max_Fd + 1; i++ )
{
/* Auf Aktivitaet warten */
if( select( My_Max_Fd + 1, &read_sockets, &write_sockets, NULL, &tv ) == -1 )
{
- if( errno != EINTR ) Log( LOG_ALERT, "select(): %s!", strerror( errno ));
- return;
+ if( errno != EINTR )
+ {
+ Log( LOG_EMERG, "select(): %s!", strerror( errno ));
+ Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
+ exit( 1 );
+ }
+ continue;
}
/* Koennen Daten geschrieben werden? */
BOOLEAN ok;
va_list ap;
+ assert( Idx >= 0 );
+ assert( My_Connections[Idx].sock > NONE );
+ assert( Format != NULL );
+
va_start( ap, Format );
if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
{
- Log( LOG_ALERT, "String too long to send (connection %d)!", Idx );
- Conn_Close( Idx, "Server error: String too long to send!" );
+ Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
+ Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
return FALSE;
}
#ifdef SNIFFER
- Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
+ if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
#endif
strcat( buffer, "\r\n" );
{
/* der Puffer ist dummerweise voll ... */
Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
- Conn_Close( Idx, NULL );
+ Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
return FALSE;
}
} /* Conn_Write */
-GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *Msg )
+GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
{
/* Verbindung schliessen. Evtl. noch von Resolver
* Sub-Prozessen offene Pipes werden geschlossen. */
CLIENT *c;
-
+
assert( Idx >= 0 );
assert( My_Connections[Idx].sock > NONE );
- if( Msg )
+ if( InformClient )
{
- Conn_WriteStr( Idx, "ERROR :%s", Msg );
+ if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
+ else Conn_WriteStr( Idx, "ERROR :Closing connection." );
if( My_Connections[Idx].sock == NONE ) return;
}
}
else
{
- Log( LOG_NOTICE, "Connection %d with %s:%d closed.", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
+ Log( LOG_INFO, "Connection %d with %s:%d closed.", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
}
c = Client_GetFromConn( Idx );
- if( c ) Client_Destroy( c );
+ if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
if( My_Connections[Idx].res_stat )
{
free( My_Connections[Idx].res_stat );
}
- /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
- if( My_Connections[Idx].our_server >= 0 ) Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
+ /* Bei Server-Verbindungen lasttry-Zeitpunkt so setzen, dass
+ * der naechste Verbindungsversuch in RECONNECT_DELAY Sekunden
+ * gestartet wird. */
+ if(( My_Connections[Idx].our_server >= 0 ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry < time( NULL )))
+ {
+ /* Okay, die Verbindung stand schon "genuegend lange" */
+ Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
+ }
FD_CLR( My_Connections[Idx].sock, &My_Sockets );
+ FD_CLR( My_Connections[Idx].sock, &My_Connects );
My_Connections[Idx].sock = NONE;
} /* Conn_Close */
}
-GLOBAL INT32 Conn_GetIdle( CONN_ID Idx )
+GLOBAL time_t Conn_GetIdle( CONN_ID Idx )
{
/* Idle-Time einer Verbindung liefern (in Sekunden) */
} /* Conn_GetIdle */
+GLOBAL time_t Conn_LastPing( CONN_ID Idx )
+{
+ /* Zeitpunkt des letzten PING liefern */
+
+ assert( Idx >= 0 );
+ return My_Connections[Idx].lastping;
+} /* Conn_LastPing */
+
+
LOCAL BOOLEAN Try_Write( CONN_ID Idx )
{
/* Versuchen, Daten aus dem Schreib-Puffer in den
/* Fehler! */
if( errno != EINTR )
{
- Log( LOG_ALERT, "select(): %s!", strerror( errno ));
- Conn_Close( Idx, NULL );
+ Log( LOG_ALERT, "select() failed: %s!", strerror( errno ));
+ Conn_Close( Idx, "Server error!", NULL, FALSE );
return FALSE;
}
}
CONN_ID idx;
assert( Sock >= 0 );
-
+
if( FD_ISSET( Sock, &My_Listeners ))
{
/* es ist einer unserer Listener-Sockets: es soll
LOCAL BOOLEAN Handle_Write( CONN_ID Idx )
{
- /* Daten aus Schreibpuffer versenden */
+ /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
- INT len;
+ INT len, res, err;
assert( Idx >= 0 );
assert( My_Connections[Idx].sock > NONE );
+
+ if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
+ {
+ /* es soll nichts geschrieben werden, sondern ein
+ * connect() hat ein Ergebnis geliefert */
+
+ FD_CLR( My_Connections[Idx].sock, &My_Connects );
+
+ /* Ergebnis des connect() ermitteln */
+ len = sizeof( err );
+ res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
+ assert( len == sizeof( err ));
+
+ /* Fehler aufgetreten? */
+ if(( res != 0 ) || ( err != 0 ))
+ {
+ /* Fehler! */
+ if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
+ else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port, Idx, strerror( err ));
+
+ /* Socket etc. pp. aufraeumen */
+ FD_CLR( My_Connections[Idx].sock, &My_Sockets );
+ close( My_Connections[Idx].sock );
+ Init_Conn_Struct( Idx );
+
+ /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
+ Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
+
+ return FALSE;
+ }
+ Log( LOG_DEBUG, "Connection %d with \"%s:%d\" established, now sendig PASS and SERVER ...", Idx, My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port );
+
+ /* PASS und SERVER verschicken */
+ Conn_WriteStr( Idx, "PASS %s "PASSSERVERADD, Conf_Server[My_Connections[Idx].our_server].pwd );
+ Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
+
+ return TRUE;
+ }
+
assert( My_Connections[Idx].wdatalen > 0 );
/* Daten schreiben */
if( len < 0 )
{
/* Oops, ein Fehler! */
- Log( LOG_ALERT, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
- Conn_Close( Idx, NULL );
+ Log( LOG_ERR, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
+ Conn_Close( Idx, "Write error (buffer)!", NULL, FALSE );
return FALSE;
}
}
/* Client-Struktur initialisieren */
- if( ! Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr )))
+ if( ! Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE ))
{
Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
close( new_sock );
FD_SET( new_sock, &My_Sockets );
if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock;
- Log( LOG_NOTICE, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
+ Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
/* Hostnamen ermitteln */
s = ResolveAddr( &new_addr );
assert( Idx >= 0 );
assert( My_Connections[Idx].sock > NONE );
- len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, READBUFFER_LEN - My_Connections[Idx].rdatalen - 1, 0 );
- My_Connections[Idx].rbuf[READBUFFER_LEN - 1] = '\0';
+ if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 2 < 0 )
+ {
+ /* Der Lesepuffer ist voll */
+ Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
+ Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
+ return;
+ }
+
+ len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, READBUFFER_LEN - My_Connections[Idx].rdatalen - 2, 0 );
if( len == 0 )
{
/* Socket wurde geschlossen */
Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
- Conn_Close( Idx, NULL );
+ Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
return;
}
if( len < 0 )
{
/* Fehler beim Lesen */
- Log( LOG_ALERT, "Read error on connection %d: %s!", Idx, strerror( errno ));
- Conn_Close( Idx, NULL );
+ Log( LOG_ERR, "Read error on connection %d: %s!", Idx, strerror( errno ));
+ Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
return;
}
/* Lesebuffer updaten */
My_Connections[Idx].rdatalen += len;
- assert( My_Connections[Idx].rdatalen <= READBUFFER_LEN );
+ assert( My_Connections[Idx].rdatalen < READBUFFER_LEN );
My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
- if( My_Connections[Idx].rdatalen > COMMAND_LEN )
- {
- /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
- * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
- * empfangen wird, wird der Client disconnectiert. */
- Log( LOG_ALERT, "Request too long (connection %d)!", Idx );
- Conn_Close( Idx, "Request too long!" );
- return;
- }
-
/* Timestamp aktualisieren */
My_Connections[Idx].lastdata = time( NULL );
CHAR *ptr, *ptr1, *ptr2;
INT len, delta;
-
+
/* Eine komplette Anfrage muss mit CR+LF enden, vgl.
* RFC 2812. Haben wir eine? */
ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
if( ptr ) delta = 2;
+#ifndef STRICT_RFC
else
{
/* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
else if( ptr1 ) ptr = ptr1;
else if( ptr2 ) ptr = ptr2;
}
+#endif
if( ptr )
{
/* Ende der Anfrage wurde gefunden */
*ptr = '\0';
len = ( ptr - My_Connections[Idx].rbuf ) + delta;
+ if( len > COMMAND_LEN )
+ {
+ /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
+ * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
+ * empfangen wird, wird der Client disconnectiert. */
+ Log( LOG_ERR, "Request too long (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
+ Conn_Close( Idx, NULL, "Request too long", TRUE );
+ return;
+ }
+
if( len > delta )
{
/* Es wurde ein Request gelesen */
if( My_Connections[i].sock == NONE ) continue;
c = Client_GetFromConn( i );
- if( c && (( c->type == CLIENT_USER ) || ( c->type == CLIENT_SERVER ) || ( c->type == CLIENT_SERVICE )))
+ if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
{
/* verbundener User, Server oder Service */
if( My_Connections[i].lastping > My_Connections[i].lastdata )
if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
{
/* Timeout */
- Log( LOG_INFO, "Connection %d: PING timeout.", i );
- Conn_Close( i, "PING timeout" );
+ Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
+ Conn_Close( i, NULL, "Ping timeout", TRUE );
}
}
else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
/* es muss ein PING gesendet werden */
Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
My_Connections[i].lastping = time( NULL );
- Conn_WriteStr( i, "PING :%s", This_Server->nick );
+ Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
}
}
else
if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
{
/* Timeout */
- Log( LOG_INFO, "Connection %d: Timeout.", i );
- Conn_Close( i, "Timeout" );
+ Log( LOG_DEBUG, "Connection %d timed out ...", i );
+ Conn_Close( i, NULL, "Timeout", FALSE );
}
}
}
INT idx, i, n;
RES_STAT *s;
-
+
+ /* Wenn "Passive-Mode" aktiv: nicht verbinden */
+ if( NGIRCd_Passive ) return;
+
for( i = 0; i < Conf_Server_Count; i++ )
{
/* Ist ein Hostname und Port definiert? */
if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue;
-
+
/* Haben wir schon eine Verbindung? */
for( n = 0; n < MAX_CONNECTIONS; n++ )
{
- if(( My_Connections[n].sock != NONE ) && ( My_Connections[n].our_server == i ))
+ if( My_Connections[n].sock == NONE ) continue;
+
+ /* Verbindung zu diesem Server? */
+ if( My_Connections[n].our_server == i )
{
/* Komplett aufgebaute Verbindung? */
if( My_Connections[n].sock > NONE ) break;
/* IP schon aufgeloest? */
if( My_Connections[n].res_stat == NULL ) New_Server( i, n );
}
+
+ /* Verbindung in dieser Server-Gruppe? */
+ if(( My_Connections[n].our_server != NONE ) && ( Conf_Server[i].group != NONE ))
+ {
+ if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break;
+ }
}
if( n < MAX_CONNECTIONS ) continue;
-
+
/* Wann war der letzte Connect-Versuch? */
if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
Init_Conn_Struct( idx );
My_Connections[idx].sock = SERVER_WAIT;
My_Connections[idx].our_server = i;
-
+
/* Hostnamen in IP aufloesen */
s = ResolveName( Conf_Server[i].host );
if( s )
struct sockaddr_in new_addr;
struct in_addr inaddr;
INT new_sock;
+ CLIENT *c;
assert( Server >= 0 );
assert( Idx >= 0 );
Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
return;
}
-
- Log( LOG_INFO, "Establishing connection to \"%s\", %s (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Idx );
+
+ Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Conf_Server[Server].port, Idx );
if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
{
if ( new_sock < 0 )
{
Init_Conn_Struct( Idx );
- Log( LOG_ALERT, "Can't create socket: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
return;
}
- if( connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )) < 0)
+
+ if( ! Init_Socket( new_sock )) return;
+
+ connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
+ if( errno != EINPROGRESS )
{
+
close( new_sock );
Init_Conn_Struct( Idx );
- Log( LOG_ALERT, "Can't connect socket: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
return;
}
/* Client-Struktur initialisieren */
- if( ! Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr )))
+ c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
+ if( ! c )
{
close( new_sock );
Init_Conn_Struct( Idx );
Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
return;
}
-
+ Client_SetIntroducer( c, c );
+
/* Verbindung registrieren */
My_Connections[Idx].sock = new_sock;
My_Connections[Idx].addr = new_addr;
/* Neuen Socket registrieren */
FD_SET( new_sock, &My_Sockets );
+ FD_SET( new_sock, &My_Connects );
if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock;
} /* New_Server */
My_Connections[Idx].rdatalen = 0;
My_Connections[Idx].wbuf[0] = '\0';
My_Connections[Idx].wdatalen = 0;
- My_Connections[Idx].our_server = -1;
+ My_Connections[Idx].our_server = NONE;
My_Connections[Idx].lastdata = time( NULL );
My_Connections[Idx].lastping = 0;
My_Connections[Idx].lastprivmsg = time( NULL );
} /* Init_Conn_Struct */
+LOCAL BOOLEAN Init_Socket( INT Sock )
+{
+ /* Socket-Optionen setzen */
+
+ INT on = 1;
+
+#ifdef O_NONBLOCK /* A/UX kennt das nicht? */
+ if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
+ {
+ Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
+ close( Sock );
+ return FALSE;
+ }
+#endif
+ if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
+ {
+ Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
+ /* dieser Fehler kann ignoriert werden. */
+ }
+
+ return TRUE;
+} /* Init_Socket */
+
+
LOCAL RES_STAT *ResolveAddr( struct sockaddr_in *Addr )
{
/* IP (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
s = malloc( sizeof( RES_STAT ));
if( ! s )
{
- Log( LOG_ALERT, "Resolver: Can't alloc memory!" );
+ Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
return NULL;
}
{
/* Fehler */
free( s );
- Log( LOG_ALERT, "Resolver: Can't fork: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
return NULL;
}
} /* ResolveAddr */
s = malloc( sizeof( RES_STAT ));
if( ! s )
{
- Log( LOG_ALERT, "Resolver: Can't alloc memory!" );
+ Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
return NULL;
}
{
/* Fehler */
free( s );
- Log( LOG_ALERT, "Resolver: Can't fork: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
return NULL;
}
} /* ResolveName */
/* Antwort an Parent schreiben */
if( write( w_fd, hostname, strlen( hostname ) + 1 ) != ( strlen( hostname ) + 1 ))
{
- Log_Resolver( LOG_ALERT, "Resolver: Can't write to parent: %s!", strerror( errno ));
+ Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
close( w_fd );
return;
}
/* Antwort an Parent schreiben */
if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
{
- Log_Resolver( LOG_ALERT, "Resolver: Can't write to parent: %s!", strerror( errno ));
+ Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
close( w_fd );
return;
}
{
/* Fehler beim Lesen aus der Pipe */
close( r_fd );
- Log( LOG_ALERT, "Resolver: Can't read result: %s!", strerror( errno ));
+ Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
return;
}
result[len] = '\0';
if( My_Connections[i].sock > NONE )
{
/* Eingehende Verbindung: Hostnamen setzen */
- strcpy( My_Connections[i].host, result );
c = Client_GetFromConn( i );
- if( c ) Client_SetHostname( c, result );
+ assert( c != NULL );
+ strcpy( My_Connections[i].host, result );
+ Client_SetHostname( c, result );
}
else
{
{
case HOST_NOT_FOUND:
return "host not found";
- case NO_ADDRESS:
+ case NO_DATA:
return "name valid but no IP address defined";
case NO_RECOVERY:
return "name server error";