1
0
mirror of https://github.com/osmarks/ngircd.git synced 2025-12-18 03:48:15 +00:00

use new io event API.

This commit is contained in:
Florian Westphal
2005-07-07 18:49:04 +00:00
parent 60ae9c827e
commit 0d180a913f

View File

@@ -15,8 +15,9 @@
#define CONN_MODULE #define CONN_MODULE
#include "portab.h" #include "portab.h"
#include "io.h"
static char UNUSED id[] = "$Id: conn.c,v 1.156 2005/07/02 14:36:03 alex Exp $"; static char UNUSED id[] = "$Id: conn.c,v 1.157 2005/07/07 18:49:04 fw Exp $";
#include "imp.h" #include "imp.h"
#include <assert.h> #include <assert.h>
@@ -29,7 +30,6 @@ static char UNUSED id[] = "$Id: conn.c,v 1.156 2005/07/02 14:36:03 alex Exp $";
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/time.h> #include <sys/time.h>
@@ -55,6 +55,7 @@ static char UNUSED id[] = "$Id: conn.c,v 1.156 2005/07/02 14:36:03 alex Exp $";
# include <tcpd.h> /* for TCP Wrappers */ # include <tcpd.h> /* for TCP Wrappers */
#endif #endif
#include "array.h"
#include "defines.h" #include "defines.h"
#include "resolve.h" #include "resolve.h"
@@ -81,7 +82,6 @@ static char UNUSED id[] = "$Id: conn.c,v 1.156 2005/07/02 14:36:03 alex Exp $";
#define SERVER_WAIT (NONE - 1) #define SERVER_WAIT (NONE - 1)
LOCAL void Handle_Read PARAMS(( int sock ));
LOCAL bool Handle_Write PARAMS(( CONN_ID Idx )); LOCAL bool Handle_Write PARAMS(( CONN_ID Idx ));
LOCAL void New_Connection PARAMS(( int Sock )); LOCAL void New_Connection PARAMS(( int Sock ));
LOCAL CONN_ID Socket2Index PARAMS(( int Sock )); LOCAL CONN_ID Socket2Index PARAMS(( int Sock ));
@@ -92,18 +92,104 @@ LOCAL void Check_Servers PARAMS(( void ));
LOCAL void Init_Conn_Struct PARAMS(( CONN_ID Idx )); LOCAL void Init_Conn_Struct PARAMS(( CONN_ID Idx ));
LOCAL bool Init_Socket PARAMS(( int Sock )); LOCAL bool Init_Socket PARAMS(( int Sock ));
LOCAL void New_Server PARAMS(( int Server, CONN_ID Idx )); LOCAL void New_Server PARAMS(( int Server, CONN_ID Idx ));
LOCAL void Read_Resolver_Result PARAMS(( int r_fd ));
LOCAL void Simple_Message PARAMS(( int Sock, char *Msg )); LOCAL void Simple_Message PARAMS(( int Sock, char *Msg ));
LOCAL int Count_Connections PARAMS(( struct sockaddr_in addr )); LOCAL int Count_Connections PARAMS(( struct sockaddr_in addr ));
LOCAL fd_set My_Listeners; static array My_Listeners;
LOCAL fd_set My_Sockets;
#ifdef TCPWRAP #ifdef TCPWRAP
int allow_severity = LOG_INFO; int allow_severity = LOG_INFO;
int deny_severity = LOG_ERR; int deny_severity = LOG_ERR;
#endif #endif
static void cb_clientserver PARAMS((int sock, short what));
static void
cb_listen(int sock, short irrelevant)
{
(void) irrelevant;
New_Connection( sock );
}
static void
cb_connserver(int sock, short what)
{
int res, err;
socklen_t sock_len;
CLIENT *c;
CONN_ID idx = Socket2Index( sock );
if (idx <= NONE) {
#ifdef DEBUG
Log(LOG_DEBUG, "cb_connserver wants to write on unknown socket?!");
#endif
io_close(sock);
return;
}
assert( what & IO_WANTWRITE);
/* connect() finished, get result. */
sock_len = sizeof( err );
res = getsockopt( My_Connections[idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
assert( sock_len == sizeof( err ));
/* Fehler aufgetreten? */
if(( res != 0 ) || ( err != 0 )) {
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[Conf_GetServer( idx )].port,
idx, strerror( err ));
/* Clean up socket, connection and client structures */
c = Client_GetFromConn( idx );
if( c ) Client_DestroyNow( c );
io_close( My_Connections[idx].sock );
Init_Conn_Struct( idx );
/* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
Conf_Server[Conf_GetServer( idx )].lasttry = time( NULL );
Conf_UnsetServer( idx );
return;
}
Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", idx,
My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port );
io_event_setcb( My_Connections[idx].sock, cb_clientserver);
io_event_add( My_Connections[idx].sock, IO_WANTREAD|IO_WANTWRITE);
/* Send PASS and SERVER command to peer */
Conn_WriteStr( idx, "PASS %s %s", Conf_Server[Conf_GetServer( idx )].pwd_out, NGIRCd_ProtoID );
Conn_WriteStr( idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
}
static void
cb_clientserver(int sock, short what)
{
CONN_ID idx = Socket2Index( sock );
if (idx <= NONE) {
#ifdef DEBUG
Log(LOG_WARNING, "WTF: cb_clientserver wants to write on unknown socket?!");
#endif
io_close(sock);
return;
}
if (what & IO_WANTREAD)
Read_Request( idx );
if (what & IO_WANTWRITE)
Handle_Write( idx );
}
LOCAL void LOCAL void
FreeRes_stat( CONNECTION *c ) FreeRes_stat( CONNECTION *c )
{ {
@@ -112,10 +198,7 @@ FreeRes_stat( CONNECTION *c )
if (!c->res_stat) return; if (!c->res_stat) return;
FD_CLR( c->res_stat->pipe[0], &Resolver_FDs ); io_close( c->res_stat->pipe[0] );
close( c->res_stat->pipe[0] );
close( c->res_stat->pipe[1] );
free( c->res_stat ); free( c->res_stat );
c->res_stat = NULL; c->res_stat = NULL;
@@ -147,9 +230,7 @@ Conn_Init( void )
Log( LOG_DEBUG, "Allocated connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size ); Log( LOG_DEBUG, "Allocated connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
#endif #endif
/* zu Beginn haben wir keine Verbindungen */ array_free( &My_Listeners );
FD_ZERO( &My_Listeners );
FD_ZERO( &My_Sockets );
/* Groesster File-Descriptor fuer select() */ /* Groesster File-Descriptor fuer select() */
Conn_MaxFD = 0; Conn_MaxFD = 0;
@@ -169,48 +250,26 @@ Conn_Exit( void )
* schliessen und freigeben. */ * schliessen und freigeben. */
CONN_ID idx; CONN_ID idx;
int i;
#ifdef DEBUG #ifdef DEBUG
Log( LOG_DEBUG, "Shutting down all connections ..." ); Log( LOG_DEBUG, "Shutting down all connections ..." );
#endif #endif
#ifdef RENDEZVOUS Conn_ExitListeners();
Rendezvous_UnregisterListeners( );
#endif
/* Sockets schliessen */ /* Sockets schliessen */
for( i = 0; i < Conn_MaxFD + 1; i++ ) for( idx = 0; idx < Pool_Size; idx++ ) {
{ if( My_Connections[idx].sock > NONE ) {
if( FD_ISSET( i, &My_Sockets )) Conn_Close( idx, NULL, NGIRCd_SignalRestart ?
{ "Server going down (restarting)":"Server going down", true );
for( idx = 0; idx < Pool_Size; idx++ ) continue;
{
if( My_Connections[idx].sock == i ) break;
}
if( FD_ISSET( i, &My_Listeners ))
{
close( i );
#ifdef DEBUG
Log( LOG_DEBUG, "Listening socket %d closed.", i );
#endif
}
else if( idx < Pool_Size )
{
if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", true );
else Conn_Close( idx, NULL, "Server going down", true );
}
else
{
Log( LOG_WARNING, "Closing unknown connection %d ...", i );
close( i );
}
} }
} }
free( My_Connections ); free( My_Connections );
My_Connections = NULL; My_Connections = NULL;
Pool_Size = 0; Pool_Size = 0;
io_library_shutdown();
} /* Conn_Exit */ } /* Conn_Exit */
@@ -221,6 +280,11 @@ Conn_InitListeners( void )
int created, i; int created, i;
if (!io_library_init(CONNECTION_POOL)) {
Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno));
return -1;
}
created = 0; created = 0;
for( i = 0; i < Conf_ListenPorts_Count; i++ ) for( i = 0; i < Conf_ListenPorts_Count; i++ )
{ {
@@ -235,24 +299,26 @@ GLOBAL void
Conn_ExitListeners( void ) Conn_ExitListeners( void )
{ {
/* Close down all listening sockets */ /* Close down all listening sockets */
int *fd;
int i; unsigned int arraylen;
#ifdef RENDEZVOUS #ifdef RENDEZVOUS
Rendezvous_UnregisterListeners( ); Rendezvous_UnregisterListeners( );
#endif #endif
Log( LOG_INFO, "Shutting down all listening sockets ..." ); arraylen = array_length(&My_Listeners, sizeof (int));
for( i = 0; i < Conn_MaxFD + 1; i++ ) Log( LOG_INFO, "Shutting down all listening sockets (%d)...", arraylen );
{ while(arraylen--) {
if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners )) fd = (int*) array_get(&My_Listeners, sizeof (int), arraylen);
{ if (fd) {
close( i ); close(*fd);
Log( LOG_DEBUG, "Listening socket %d closed.", *fd );
#ifdef DEBUG #ifdef DEBUG
Log( LOG_DEBUG, "Listening socket %d closed.", i ); } else {
Log( LOG_DEBUG, "array_get pos %d returned NULL", arraylen );
#endif #endif
} }
} }
array_free(&My_Listeners);
} /* Conn_ExitListeners */ } /* Conn_ExitListeners */
@@ -316,10 +382,12 @@ Conn_NewListener( const UINT16 Port )
} }
/* Neuen Listener in Strukturen einfuegen */ /* Neuen Listener in Strukturen einfuegen */
FD_SET( sock, &My_Listeners ); if (!array_catb( &My_Listeners,(char*) &sock, sizeof(int) )) {
FD_SET( sock, &My_Sockets ); Log( LOG_CRIT, "Can't add socket to My_Listeners array: %s!", strerror( errno ));
close( sock );
if( sock > Conn_MaxFD ) Conn_MaxFD = sock; return false;
}
io_event_create( sock, IO_WANTREAD, cb_listen );
if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock ); if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock );
else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock ); else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock );
@@ -351,7 +419,6 @@ Conn_NewListener( const UINT16 Port )
/* Register service */ /* Register service */
Rendezvous_Register( name, RENDEZVOUS_TYPE, Port ); Rendezvous_Register( name, RENDEZVOUS_TYPE, Port );
#endif #endif
return true; return true;
} /* Conn_NewListener */ } /* Conn_NewListener */
@@ -370,11 +437,10 @@ Conn_Handler( void )
* - volle Lesepuffer versuchen zu verarbeiten, * - volle Lesepuffer versuchen zu verarbeiten,
* - Antworten von Resolver Sub-Prozessen annehmen. * - Antworten von Resolver Sub-Prozessen annehmen.
*/ */
int i;
fd_set read_sockets, write_sockets; unsigned int wdatalen;
struct timeval tv; struct timeval tv;
time_t start, t; time_t start, t;
CONN_ID i, idx;
bool timeout; bool timeout;
start = time( NULL ); start = time( NULL );
@@ -398,7 +464,7 @@ Conn_Handler( void )
/* noch volle Lese-Buffer suchen */ /* noch volle Lese-Buffer suchen */
for( i = 0; i < Pool_Size; i++ ) for( i = 0; i < Pool_Size; i++ )
{ {
if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ) && if(( My_Connections[i].sock > NONE ) && ( array_bytes(&My_Connections[i].rbuf) > 0 ) &&
( My_Connections[i].delaytime < t )) ( My_Connections[i].delaytime < t ))
{ {
/* Kann aus dem Buffer noch ein Befehl extrahiert werden? */ /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
@@ -407,61 +473,44 @@ Conn_Handler( void )
} }
/* noch volle Schreib-Puffer suchen */ /* noch volle Schreib-Puffer suchen */
FD_ZERO( &write_sockets ); for( i = 0; i < Pool_Size; i++ ) {
for( i = 0; i < Pool_Size; i++ ) if ( My_Connections[i].sock <= NONE )
{ continue;
wdatalen = array_bytes(&My_Connections[i].wbuf);
#ifdef ZLIB #ifdef ZLIB
if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 ))) if (( wdatalen > 0 ) || ( array_bytes(&My_Connections[i].zip.wbuf)> 0 ))
#else #else
if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 )) if ( wdatalen > 0 )
#endif #endif
{ {
/* Socket der Verbindung in Set aufnehmen */ /* Socket der Verbindung in Set aufnehmen */
FD_SET( My_Connections[i].sock, &write_sockets ); io_event_add( My_Connections[i].sock, IO_WANTWRITE );
}
}
/* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
for( i = 0; i < Pool_Size; i++ )
{
if ( My_Connections[i].sock > NONE ) {
if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING )) {
FD_SET( My_Connections[i].sock, &write_sockets );
}
} }
} }
/* von welchen Sockets koennte gelesen werden? */ /* von welchen Sockets koennte gelesen werden? */
read_sockets = My_Sockets; for (i = 0; i < Pool_Size; i++ ) {
for( i = 0; i < Pool_Size; i++ ) if ( My_Connections[i].sock <= NONE )
{ continue;
if ( My_Connections[i].sock > NONE ) {
if ( My_Connections[i].res_stat ) { if ( My_Connections[i].res_stat ) {
/* wait for completion of Resolver Sub-Process */ /* wait for completion of Resolver Sub-Process */
FD_CLR( My_Connections[i].sock, &read_sockets ); io_event_del( My_Connections[i].sock, IO_WANTREAD );
continue; continue;
} }
if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING )) { if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING ))
/* wait for completion of connect() */ continue; /* wait for completion of connect() */
FD_CLR( My_Connections[i].sock, &read_sockets );
if( My_Connections[i].delaytime > t ) {
/* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
io_event_del( My_Connections[i].sock, IO_WANTREAD );
continue; continue;
} }
} io_event_add( My_Connections[i].sock, IO_WANTREAD );
if( My_Connections[i].delaytime > t )
{
/* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
FD_CLR( My_Connections[i].sock, &read_sockets );
}
}
for( i = 0; i < Conn_MaxFD + 1; i++ )
{
/* Pipes von Resolver Sub-Prozessen aufnehmen */
if( FD_ISSET( i, &Resolver_FDs ))
{
FD_SET( i, &read_sockets );
}
} }
/* Timeout initialisieren */ /* Timeout initialisieren */
@@ -470,46 +519,12 @@ Conn_Handler( void )
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 = io_dispatch( &tv );
if( i == 0 ) if (i == -1 && errno != EINTR ) {
{ Log(LOG_EMERG, "Conn_Handler(): io_dispatch(): %s!", strerror(errno));
/* keine Veraenderung an den Sockets */
continue;
}
if( i == -1 )
{
/* Fehler (z.B. Interrupt) */
if( errno != EINTR )
{
Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
Log(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME); Log(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
exit( 1 ); exit( 1 );
} }
continue;
}
/* Koennen Daten geschrieben werden? */
for( i = 0; i < Conn_MaxFD + 1; i++ )
{
if( ! FD_ISSET( i, &write_sockets )) continue;
/* Es kann geschrieben werden ... */
idx = Socket2Index( i );
if( idx == NONE ) continue;
if( ! Handle_Write( idx ))
{
/* Fehler beim Schreiben! Diesen Socket nun
* auch aus dem Read-Set entfernen: */
FD_CLR( i, &read_sockets );
}
}
/* Daten zum Lesen vorhanden? */
for( i = 0; i < Conn_MaxFD + 1; i++ )
{
if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
}
} }
if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" ); if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
@@ -549,7 +564,6 @@ va_dcl
#else #else
va_start( ap ); va_start( ap );
#endif #endif
if (vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) >= COMMAND_LEN - 2 ) { if (vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) >= COMMAND_LEN - 2 ) {
/* /*
* The string that should be written to the socket is longer * The string that should be written to the socket is longer
@@ -593,7 +607,7 @@ va_dcl
GLOBAL bool GLOBAL bool
Conn_Write( CONN_ID Idx, char *Data, int Len ) Conn_Write( CONN_ID Idx, char *Data, unsigned int Len )
{ {
/* Daten in Socket schreiben. Bei "fatalen" Fehlern wird /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
* der Client disconnectiert und false geliefert. */ * der Client disconnectiert und false geliefert. */
@@ -617,15 +631,13 @@ Conn_Write( CONN_ID Idx, char *Data, int Len )
/* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es, /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
* moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
* Socket zu schreiben (u.a. wg. Komprimierung). */ * Socket zu schreiben (u.a. wg. Komprimierung). */
if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 ) if( array_bytes(&My_Connections[Idx].wbuf) >= WRITEBUFFER_LEN) {
{
/* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
* zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */ * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
if( ! Handle_Write( Idx )) return false; if( ! Handle_Write( Idx )) return false;
/* nun neu pruefen: */ /* check again: if our writebuf is twice als large as the initial limit: Kill connection */
if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 ) if( array_bytes(&My_Connections[Idx].wbuf) >= (WRITEBUFFER_LEN*2)) {
{
Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx ); Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
Conn_Close( Idx, "Write buffer overflow!", NULL, false ); Conn_Close( Idx, "Write buffer overflow!", NULL, false );
return false; return false;
@@ -641,8 +653,9 @@ Conn_Write( CONN_ID Idx, char *Data, int Len )
#endif #endif
{ {
/* Daten in Puffer kopieren */ /* Daten in Puffer kopieren */
memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len ); if (!array_catb( &My_Connections[Idx].wbuf, Data, Len ))
My_Connections[Idx].wdatalen += Len; return false;
My_Connections[Idx].bytes_out += Len; My_Connections[Idx].bytes_out += Len;
} }
@@ -713,14 +726,13 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
(void)Handle_Write( Idx ); (void)Handle_Write( Idx );
/* Shut down socket */ /* Shut down socket */
if( close( My_Connections[Idx].sock ) != 0 ) if( ! io_close( My_Connections[Idx].sock ))
{ {
/* Oops, we can't close the socket!? This is ... ugly! */ /* Oops, we can't close the socket!? This is ... ugly! */
Log( LOG_CRIT, "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno )); Log( LOG_CRIT, "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
} }
/* Mark socket as invalid: */ /* Mark socket as invalid: */
FD_CLR( My_Connections[Idx].sock, &My_Sockets );
My_Connections[Idx].sock = NONE; My_Connections[Idx].sock = NONE;
/* If there is still a client, unregister it now */ /* If there is still a client, unregister it now */
@@ -755,9 +767,13 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) { if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
inflateEnd( &My_Connections[Idx].zip.in ); inflateEnd( &My_Connections[Idx].zip.in );
deflateEnd( &My_Connections[Idx].zip.out ); deflateEnd( &My_Connections[Idx].zip.out );
array_free(&My_Connections[Idx].zip.rbuf);
array_free(&My_Connections[Idx].zip.wbuf);
} }
#endif #endif
array_free(&My_Connections[Idx].rbuf);
array_free(&My_Connections[Idx].wbuf);
/* Clean up connection structure (=free it) */ /* Clean up connection structure (=free it) */
Init_Conn_Struct( Idx ); Init_Conn_Struct( Idx );
@@ -798,102 +814,41 @@ Conn_SyncServerStruct( void )
} /* SyncServerStruct */ } /* SyncServerStruct */
LOCAL void
Handle_Read( int Sock )
{
/* Aktivitaet auf einem Socket verarbeiten:
* - neue Clients annehmen,
* - Daten von Clients verarbeiten,
* - Resolver-Rueckmeldungen annehmen. */
CONN_ID idx;
assert( Sock > NONE );
if( FD_ISSET( Sock, &My_Listeners ))
{
/* es ist einer unserer Listener-Sockets: es soll
* also eine neue Verbindung aufgebaut werden. */
New_Connection( Sock );
}
else if( FD_ISSET( Sock, &Resolver_FDs ))
{
/* Rueckmeldung von einem Resolver Sub-Prozess */
Read_Resolver_Result( Sock );
}
else
{
/* Ein Client Socket: entweder ein User oder Server */
idx = Socket2Index( Sock );
if( idx > NONE ) Read_Request( idx );
}
} /* Handle_Read */
LOCAL bool LOCAL bool
Handle_Write( CONN_ID Idx ) Handle_Write( CONN_ID Idx )
{ {
/* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */ /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
int len, res, err; int len;
socklen_t sock_len; unsigned int wdatalen;
CLIENT *c;
Log(LOG_DEBUG, "Handle_Write");
assert( Idx > NONE ); assert( Idx > NONE );
assert( My_Connections[Idx].sock > NONE ); if ( My_Connections[Idx].sock < 0 ) {
Log(LOG_WARNING, "Handle_Write() on closed socket, idx %d", Idx);
if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ISCONNECTING )) {
/* connect() has finished, check result */
Conn_OPTION_DEL( &My_Connections[Idx], CONN_ISCONNECTING );
/* Ergebnis des connect() ermitteln */
sock_len = sizeof( err );
res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
assert( sock_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[Conf_GetServer( Idx )].port, Idx, strerror( err ));
/* Clean up socket, connection and client structures */
FD_CLR( My_Connections[Idx].sock, &My_Sockets );
c = Client_GetFromConn( Idx );
if( c ) Client_DestroyNow( c );
close( My_Connections[Idx].sock );
Init_Conn_Struct( Idx );
/* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
Conf_Server[Conf_GetServer( Idx )].lasttry = time( NULL );
Conf_UnsetServer( Idx );
return false; return false;
} }
Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", Idx, My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port ); assert( My_Connections[Idx].sock > NONE );
/* Send PASS and SERVER command to peer */
Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[Conf_GetServer( Idx )].pwd_out, NGIRCd_ProtoID ); wdatalen = array_bytes(&My_Connections[Idx].wbuf );
return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo ); #ifdef ZLIB
if(( wdatalen == 0 ) && ( ! array_bytes(&My_Connections[Idx].zip.wbuf))) {
io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
return true;
} }
#ifdef ZLIB
if(( My_Connections[Idx].wdatalen <= 0 ) && ( ! My_Connections[Idx].zip.wdatalen ))
return true;
/* write buffer empty, but not compression buf? -> flush compression buf. */ /* write buffer empty, but not compression buf? -> flush compression buf. */
if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx ); if( wdatalen == 0 ) Zip_Flush( Idx );
#else #else
if( My_Connections[Idx].wdatalen <= 0 ) if( wdatalen == 0 ) {
io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
return true; return true;
}
#endif #endif
len = write( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen ); wdatalen = array_bytes(&My_Connections[Idx].wbuf ); /* Zip_Flush may change wbuf */
len = write( My_Connections[Idx].sock, array_start(&My_Connections[Idx].wbuf), wdatalen );
if( len < 0 ) { if( len < 0 ) {
if( errno == EAGAIN || errno == EINTR) if( errno == EAGAIN || errno == EINTR)
return true; return true;
@@ -904,9 +859,8 @@ Handle_Write( CONN_ID Idx )
return false; return false;
} }
/* Update buffer len and move any data not yet written to beginning of buf */ /* move any data not yet written to beginning */
My_Connections[Idx].wdatalen -= len; array_moveleft(&My_Connections[Idx].wbuf, 1, len);
memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
return true; return true;
} /* Handle_Write */ } /* Handle_Write */
@@ -930,7 +884,6 @@ New_Connection( int Sock )
long new_size, cnt; long new_size, cnt;
assert( Sock > NONE ); assert( Sock > NONE );
/* Connection auf Listen-Socket annehmen */ /* Connection auf Listen-Socket annehmen */
new_sock_len = sizeof( new_addr ); new_sock_len = sizeof( new_addr );
new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len ); new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
@@ -1038,8 +991,7 @@ New_Connection( int Sock )
My_Connections[idx].addr = new_addr; My_Connections[idx].addr = new_addr;
/* Neuen Socket registrieren */ /* Neuen Socket registrieren */
FD_SET( new_sock, &My_Sockets ); io_event_create( new_sock, IO_WANTREAD, cb_clientserver);
if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_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 ); 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 );
@@ -1089,7 +1041,9 @@ Read_Request( CONN_ID Idx )
/* Daten von Socket einlesen und entsprechend behandeln. /* Daten von Socket einlesen und entsprechend behandeln.
* Tritt ein Fehler auf, so wird der Socket geschlossen. */ * Tritt ein Fehler auf, so wird der Socket geschlossen. */
int len, bsize; unsigned int bsize;
int len;
char readbuf[1024];
#ifdef ZLIB #ifdef ZLIB
CLIENT *c; CLIENT *c;
#endif #endif
@@ -1102,51 +1056,57 @@ Read_Request( CONN_ID Idx )
bsize = READBUFFER_LEN; bsize = READBUFFER_LEN;
#ifdef ZLIB #ifdef ZLIB
c = Client_GetFromConn( Idx ); c = Client_GetFromConn( Idx );
if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) && ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN )) bsize = ZREADBUFFER_LEN;
if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) &&
( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN ))
bsize = ZREADBUFFER_LEN;
#endif #endif
#ifdef ZLIB #ifdef ZLIB
if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 )) if (( array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN ) ||
( array_bytes(&My_Connections[Idx].zip.rbuf) >= ZREADBUFFER_LEN ))
#else #else
if( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) if ( array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN )
#endif #endif
{ {
/* Der Lesepuffer ist voll */ /* Der Lesepuffer ist voll */
Log( LOG_ERR, "Receive buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen ); Log( LOG_ERR, "Receive buffer overflow (connection %d): %d bytes!", Idx,
array_bytes(&My_Connections[Idx].rbuf));
Conn_Close( Idx, "Receive buffer overflow!", NULL, false ); Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
return; return;
} }
#ifdef ZLIB len = read( My_Connections[Idx].sock, readbuf, sizeof readbuf );
if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) { if( len == 0 ) {
len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 ); Log( LOG_INFO, "%s:%d (%s) is closing the connection ...",
if( len > 0 ) My_Connections[Idx].zip.rdatalen += len; My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port),
} inet_ntoa( My_Connections[Idx].addr.sin_addr ));
else
#endif
{
len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, bsize - My_Connections[Idx].rdatalen - 1, 0 );
if( len > 0 ) My_Connections[Idx].rdatalen += len;
}
if( len == 0 )
{
/* Socket wurde geschlossen */
Log( LOG_INFO, "%s:%d (%s) is closing the connection ...", My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), inet_ntoa( My_Connections[Idx].addr.sin_addr ));
Conn_Close( Idx, "Socket closed!", "Client closed connection", false ); Conn_Close( Idx, "Socket closed!", "Client closed connection", false );
return; return;
} }
if( len < 0 ) {
if( len < 0 )
{
/* Operation haette Socket "nur" blockiert ... */
if( errno == EAGAIN ) return; if( errno == EAGAIN ) return;
Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx,
/* Fehler beim Lesen */ My_Connections[Idx].sock, strerror( errno ));
Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
Conn_Close( Idx, "Read error!", "Client closed connection", false ); Conn_Close( Idx, "Read error!", "Client closed connection", false );
return; return;
} }
#ifdef ZLIB
if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
if (!array_catb( &My_Connections[Idx].zip.rbuf, readbuf, len)) {
Log( LOG_ERR, "Could not append recieved data to zip input buffer (connn %d): %d bytes!", Idx, len );
Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
return;
}
} else
#endif
{
readbuf[len] = 0;
if (!array_cats( &My_Connections[Idx].rbuf, readbuf )) {
Log( LOG_ERR, "Could not append recieved data to input buffer (connn %d): %d bytes!", Idx, len );
Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
}
}
/* Connection-Statistik aktualisieren */ /* Connection-Statistik aktualisieren */
My_Connections[Idx].bytes_in += len; My_Connections[Idx].bytes_in += len;
@@ -1161,15 +1121,14 @@ Read_Request( CONN_ID Idx )
LOCAL bool LOCAL bool
Handle_Buffer( CONN_ID Idx ) Handle_Buffer( CONN_ID Idx )
{ {
/* Daten im Lese-Puffer einer Verbindung verarbeiten. /* Handle Data in Connections Read-Buffer.
* Wurde ein Request verarbeitet, so wird true geliefert, * Return true if a reuqest was handled, false otherwise (also returned on errors). */
* ansonsten false (auch bei Fehlern). */
#ifndef STRICT_RFC #ifndef STRICT_RFC
char *ptr1, *ptr2; char *ptr1, *ptr2;
#endif #endif
char *ptr; char *ptr;
int len, delta; int len, delta;
unsigned int arraylen;
bool action, result; bool action, result;
#ifdef ZLIB #ifdef ZLIB
bool old_z; bool old_z;
@@ -1180,28 +1139,32 @@ Handle_Buffer( CONN_ID Idx )
{ {
/* Check penalty */ /* Check penalty */
if( My_Connections[Idx].delaytime > time( NULL )) return result; if( My_Connections[Idx].delaytime > time( NULL )) return result;
#ifdef ZLIB #ifdef ZLIB
/* ggf. noch unkomprimiete Daten weiter entpacken */ /* unpack compressed data */
if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP ))
if( ! Unzip_Buffer( Idx )) return false; if( ! Unzip_Buffer( Idx )) return false;
#endif #endif
if( My_Connections[Idx].rdatalen < 1 ) break; arraylen = array_bytes(&My_Connections[Idx].rbuf);
if (arraylen == 0)
break;
/* Eine komplette Anfrage muss mit CR+LF enden, vgl. if (!array_cat0(&My_Connections[Idx].rbuf)) /* make sure buf is NULL terminated */
* RFC 2812. Haben wir eine? */ return false;
My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
if( ptr ) delta = 2; array_truncate(&My_Connections[Idx].rbuf, 1, arraylen); /* do not count trailing NULL */
/* A Complete Request end with CR+LF, see RFC 2812. */
ptr = strstr( array_start(&My_Connections[Idx].rbuf), "\r\n" );
if( ptr ) delta = 2; /* complete request */
#ifndef STRICT_RFC #ifndef STRICT_RFC
else else {
{ /* Check for non-RFC-compliant request (only CR or LF)? Unfortunately,
/* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider * there are quite a few clients that do this (incl. "mIRC" :-( */
* machen soetwas viele Clients, u.a. "mIRC" :-( */ ptr1 = strchr( array_start(&My_Connections[Idx].rbuf), '\r' );
ptr1 = strchr( My_Connections[Idx].rbuf, '\r' ); ptr2 = strchr( array_start(&My_Connections[Idx].rbuf), '\n' );
ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
delta = 1; delta = 1;
if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1; if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
else if( ptr1 ) ptr = ptr1; else if( ptr1 ) ptr = ptr1;
@@ -1210,60 +1173,61 @@ Handle_Buffer( CONN_ID Idx )
#endif #endif
action = false; action = false;
if( ptr ) if( ! ptr )
{ break;
/* Ende der Anfrage wurde gefunden */
/* End of request found */
*ptr = '\0'; *ptr = '\0';
len = ( ptr - My_Connections[Idx].rbuf ) + delta;
if( len > ( COMMAND_LEN - 1 )) len = ( ptr - (char*) array_start(&My_Connections[Idx].rbuf)) + delta;
{
/* Eine Anfrage darf(!) nicht laenger als 512 Zeichen if( len < 0 || len > ( COMMAND_LEN - 1 )) {
* (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas /* Request must not exceed 512 chars (incl. CR+LF!), see
* empfangen wird, wird der Client disconnectiert. */ * RFC 2812. Disconnect Client if this happens. */
Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 ); Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!",
Idx, array_bytes(&My_Connections[Idx].rbuf), COMMAND_LEN - 1 );
Conn_Close( Idx, NULL, "Request too long", true ); Conn_Close( Idx, NULL, "Request too long", true );
return false; return false;
} }
#ifdef ZLIB #ifdef ZLIB
/* merken, ob Stream bereits komprimiert wird */ /* remember if stream is already compressed */
old_z = My_Connections[Idx].options & CONN_ZIP; old_z = My_Connections[Idx].options & CONN_ZIP;
#endif #endif
if( len > delta ) if( len > delta )
{ {
/* Es wurde ein Request gelesen */ /* A Request was read */
My_Connections[Idx].msg_in++; My_Connections[Idx].msg_in++;
if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return false; if( ! Parse_Request( Idx, (char*)array_start(&My_Connections[Idx].rbuf) )) return false;
else action = true; else action = true;
}
/* Puffer anpassen */ array_moveleft(&My_Connections[Idx].rbuf, 1, len);
My_Connections[Idx].rdatalen -= len; #ifdef DEBUG
memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen ); Log(LOG_DEBUG, "%d byte left in rbuf", array_bytes(&My_Connections[Idx].rbuf));
#endif
}
#ifdef ZLIB #ifdef ZLIB
if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 )) if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( array_bytes(&My_Connections[Idx].rbuf) > 0 ))
{ {
/* Mit dem letzten Befehl wurde Socket-Kompression aktiviert. /* The last Command activated Socket-Compression.
* Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer * Data that was read after that needs to be copied to Unzip-buf
* umkopieren, damit diese nun zunaechst entkomprimiert werden */ * for decompression */
if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN ) if( array_bytes(&My_Connections[Idx].rbuf)> ZREADBUFFER_LEN ) {
{ /* No space left */
/* Hupsa! Soviel Platz haben wir aber gar nicht! */ Log( LOG_ALERT, "Can't move receive buffer: No space left in unzip buffer (need %d bytes)!", array_bytes(&My_Connections[Idx].rbuf ));
Log( LOG_ALERT, "Can't move receive buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
return false; return false;
} }
memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen ); if (!array_copy( &My_Connections[Idx].zip.rbuf, &My_Connections[Idx].rbuf ))
My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen; return false;
My_Connections[Idx].rdatalen = 0;
array_trunc(&My_Connections[Idx].rbuf);
#ifdef DEBUG #ifdef DEBUG
Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen ); Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", array_bytes(&My_Connections[Idx].zip.rbuf));
#endif /* DEBUG */ #endif /* DEBUG */
} }
#endif /* ZLIB */ #endif /* ZLIB */
}
if( action ) result = true; if( action ) result = true;
} while( action ); } while( action );
@@ -1483,11 +1447,9 @@ New_Server( int Server, CONN_ID Idx )
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 ));
/* Register new socket */ /* Register new socket */
FD_SET( new_sock, &My_Sockets ); io_event_create( new_sock, IO_WANTWRITE, cb_connserver);
Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING ); Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
#ifdef DEBUG #ifdef DEBUG
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 );
#endif #endif
@@ -1514,14 +1476,11 @@ Init_Socket( int Sock )
int value; int value;
#ifdef O_NONBLOCK /* unknown on A/UX */ if (!io_setnonblock(Sock)) {
if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
{
Log( LOG_CRIT, "Can't enable non-blocking mode for socket: %s!", strerror( errno )); Log( LOG_CRIT, "Can't enable non-blocking mode for socket: %s!", strerror( errno ));
close( Sock ); close( Sock );
return false; return false;
} }
#endif
/* Don't block this port after socket shutdown */ /* Don't block this port after socket shutdown */
value = 1; value = 1;
@@ -1548,8 +1507,8 @@ Init_Socket( int Sock )
} /* Init_Socket */ } /* Init_Socket */
LOCAL void GLOBAL
Read_Resolver_Result( int r_fd ) void Read_Resolver_Result( int r_fd )
{ {
/* Read result of resolver sub-process from pipe and update the /* Read result of resolver sub-process from pipe and update the
* apropriate connection/client structure(s): hostname and/or * apropriate connection/client structure(s): hostname and/or
@@ -1560,6 +1519,7 @@ Read_Resolver_Result( int r_fd )
RES_STAT *s; RES_STAT *s;
char *ptr; char *ptr;
Log( LOG_DEBUG, "Resolver: started, fd %d\n", r_fd );
/* Search associated connection ... */ /* Search associated connection ... */
for( i = 0; i < Pool_Size; i++ ) for( i = 0; i < Pool_Size; i++ )
{ {
@@ -1572,8 +1532,7 @@ Read_Resolver_Result( int r_fd )
{ {
/* Ops, none found? Probably the connection has already /* Ops, none found? Probably the connection has already
* been closed!? We'll ignore that ... */ * been closed!? We'll ignore that ... */
FD_CLR( r_fd, &Resolver_FDs ); io_close( r_fd );
close( r_fd );
#ifdef DEBUG #ifdef DEBUG
Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" ); Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
#endif #endif
@@ -1705,4 +1664,5 @@ Count_Connections( struct sockaddr_in addr_in )
} /* Count_Connections */ } /* Count_Connections */
/* -eof- */ /* -eof- */