#include "portab.h"
#include "io.h"
-static char UNUSED id[] = "$Id: conn.c,v 1.162 2005/07/11 20:58:05 fw Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.166 2005/07/28 16:13:09 fw Exp $";
#include "imp.h"
#include <assert.h>
static void
-cb_connserver(int sock, short what)
+cb_connserver(int sock, UNUSED short what)
{
int res, err;
socklen_t sock_len;
io_close( c->res_stat->pipe[0] );
+ array_free(&c->res_stat->buffer);
free( c->res_stat );
c->res_stat = NULL;
}
} /* Conn_ExitListeners */
+/* return new listening port file descriptor or -1 on failure */
LOCAL int
NewListener( const UINT16 Port )
{
GLOBAL void
Conn_Handler( void )
{
- /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
- * werden dabei durchgefuehrt, bis der Server terminieren oder neu
- * starten soll:
- *
- * - neue Verbindungen annehmen,
- * - Server-Verbindungen aufbauen,
- * - geschlossene Verbindungen loeschen,
- * - volle Schreibpuffer versuchen zu schreiben,
- * - volle Lesepuffer versuchen zu verarbeiten,
- * - Antworten von Resolver Sub-Prozessen annehmen.
+ /* "Main Loop.": Loop until a signal (for shutdown or restart) arrives.
+ * Call io_dispatch() to check for read/writeable sockets every second
+ * Wait for status change on pending connections (e.g: when the hostname has been resolved)
+ * check for penalty/timeouts
+ * handle input buffers
*/
int i;
unsigned int wdatalen;
for( i = 0; i < Pool_Size; i++ ) {
if ( My_Connections[i].sock <= NONE )
continue;
-
+
wdatalen = array_bytes(&My_Connections[i].wbuf);
#ifdef ZLIB
/* Try to write out the write buffer */
(void)Handle_Write( Idx );
-
+
/* Shut down socket */
if( ! io_close( My_Connections[Idx].sock ))
{
}
ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
- if( ! ptr )
- {
+ if( ! ptr ) {
Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
Simple_Message( new_sock, "ERROR: Internal error" );
close( new_sock );
/* Client-Struktur initialisieren */
c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false );
- if( ! c )
- {
+ if( ! c ) {
Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
Simple_Message( new_sock, "ERROR :Internal error" );
close( new_sock );
My_Connections[idx].addr = new_addr;
/* Neuen Socket registrieren */
- io_event_create( new_sock, IO_WANTREAD, cb_clientserver);
+ if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) {
+ Simple_Message( new_sock, "ERROR :Internal error" );
+ Conn_Close( idx, "io_event_create() failed", NULL, false );
+ return;
+ }
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 );
/* Daten von Socket einlesen und entsprechend behandeln.
* Tritt ein Fehler auf, so wird der Socket geschlossen. */
- unsigned int bsize;
int len;
char readbuf[1024];
-#ifdef ZLIB
- CLIENT *c;
-#endif
assert( Idx > NONE );
assert( My_Connections[Idx].sock > NONE );
- /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
- * ansonsten koennen Daten ggf. nicht umkopiert werden. */
- bsize = READBUFFER_LEN;
#ifdef ZLIB
- 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 (( array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN ) ||
( array_bytes(&My_Connections[Idx].zip.rbuf) >= ZREADBUFFER_LEN ))
#else
len = read( My_Connections[Idx].sock, readbuf, sizeof readbuf -1 );
if( len == 0 ) {
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 ));
+ 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 );
return;
}
+
if( len < 0 ) {
if( errno == EAGAIN ) return;
Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx,
#endif
char *ptr;
int len, delta;
- unsigned int arraylen;
bool action, result;
#ifdef ZLIB
bool old_z;
if( ! Unzip_Buffer( Idx )) return false;
#endif
- arraylen = array_bytes(&My_Connections[Idx].rbuf);
- if (arraylen == 0)
+ if (0 == array_bytes(&My_Connections[Idx].rbuf))
break;
- if (!array_cat0(&My_Connections[Idx].rbuf)) /* make sure buf is NULL terminated */
+ if (!array_cat0_temporary(&My_Connections[Idx].rbuf)) /* make sure buf is NULL terminated */
return false;
- 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" );
My_Connections[Idx].msg_in++;
if( ! Parse_Request( Idx, (char*)array_start(&My_Connections[Idx].rbuf) )) return false;
else action = true;
-
+
array_moveleft(&My_Connections[Idx].rbuf, 1, len);
#ifdef DEBUG
Log(LOG_DEBUG, "%d byte left in rbuf", array_bytes(&My_Connections[Idx].rbuf));
assert( Idx > NONE );
/* Did we get a valid IP address? */
- if( ! Conf_Server[Server].ip[0] )
- {
+ if( ! Conf_Server[Server].ip[0] ) {
/* No. Free connection structure and abort: */
- 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 );
- return;
+ goto out;
}
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 );
#endif
{
/* Can't convert IP address */
- 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 );
- return;
+ goto out;
}
memset( &new_addr, 0, sizeof( new_addr ));
new_sock = socket( PF_INET, SOCK_STREAM, 0 );
if ( new_sock < 0 ) {
/* Can't create socket */
- Init_Conn_Struct( Idx );
- Conf_Server[Server].conn_id = NONE;
Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
- return;
+ goto out;
}
if( ! Init_Socket( new_sock )) return;
/* Can't connect socket */
Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
close( new_sock );
- Init_Conn_Struct( Idx );
- Conf_Server[Server].conn_id = NONE;
- return;
+ goto out;
}
/* Client-Struktur initialisieren */
c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, false );
if( ! c ) {
/* Can't create new client structure */
- close( new_sock );
- Init_Conn_Struct( Idx );
- Conf_Server[Server].conn_id = NONE;
Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
- return;
+ close( new_sock );
+ goto out;
}
+
Client_SetIntroducer( c, c );
Client_SetToken( c, TOKEN_OUTBOUND );
strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
/* Register new socket */
- io_event_create( new_sock, IO_WANTWRITE, cb_connserver);
- Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
+ if (!io_event_create( new_sock, IO_WANTWRITE, cb_connserver)) {
+ Log( LOG_ALERT, "io_event_create(): could not add fd %d", strerror(errno));
+ Conn_Close( Idx, "io_event_create() failed", NULL, false );
+ goto out;
+ }
#ifdef DEBUG
Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
#endif
+ Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
+ return;
+out:
+ Init_Conn_Struct( Idx );
+ Conf_Server[Server].conn_id = NONE;
} /* New_Server */
* IDENT user name.*/
CLIENT *c;
- int len, i, n;
+ int bytes_read, i, n;
+ unsigned int len;
RES_STAT *s;
char *ptr;
+ char *bufptr;
+ char readbuf[HOST_LEN];
Log( LOG_DEBUG, "Resolver: started, fd %d\n", r_fd );
/* Search associated connection ... */
assert( s != NULL );
/* Read result from pipe */
- len = read( r_fd, s->buffer + s->bufpos, sizeof( s->buffer ) - s->bufpos - 1 );
- if( len < 0 )
+ bytes_read = read( r_fd, readbuf, sizeof readbuf -1 );
+ if( bytes_read < 0 )
{
/* Error! */
Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
FreeRes_stat( &My_Connections[i] );
return;
}
- s->bufpos += len;
- s->buffer[s->bufpos] = '\0';
+ len = (unsigned int) bytes_read;
+ readbuf[len] = '\0';
+ if (!array_catb(&s->buffer, readbuf, len)) {
+ Log( LOG_CRIT, "Resolver: Can't append result %s to buffer: %s", readbuf, strerror( errno ));
+ FreeRes_stat(&My_Connections[i]);
+ return;
+ }
+
+ if (!array_cat0_temporary(&s->buffer)) {
+ Log( LOG_CRIT, "Resolver: Can't append result %s to buffer: %s", readbuf, strerror( errno ));
+ FreeRes_stat(&My_Connections[i]);
+ return;
+ }
/* If the result string is incomplete, return to main loop and
* wait until we can read in more bytes. */
#ifdef IDENTAUTH
try_resolve:
#endif
- ptr = strchr( s->buffer, '\n' );
+ bufptr = (char*) array_start(&s->buffer);
+ assert(bufptr != NULL);
+ ptr = strchr( bufptr, '\n' );
if( ! ptr ) return;
*ptr = '\0';
#ifdef DEBUG
- Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%d bytes), stage %d.", s->buffer, len, s->stage );
+ Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%u bytes read), stage %d.", bufptr, len, s->stage);
#endif
/* Okay, we got a complete result: this is a host name for outgoing
{
switch(s->stage) {
case 0: /* host name */
- strlcpy( My_Connections[i].host, s->buffer, sizeof( My_Connections[i].host ));
- Client_SetHostname( c, s->buffer );
+ strlcpy( My_Connections[i].host, bufptr, sizeof( My_Connections[i].host));
+
+ Client_SetHostname( c, bufptr);
#ifdef IDENTAUTH
/* clean up buffer for IDENT result */
- len = strlen( s->buffer ) + 1;
- assert((size_t) len <= sizeof( s->buffer ));
- memmove( s->buffer, s->buffer + len, sizeof( s->buffer ) - len );
- assert(len <= s->bufpos );
- s->bufpos -= len;
+ len = strlen(bufptr) + 1;
+ assert(len <= array_bytes(&s->buffer));
+ array_moveleft(&s->buffer, 1, len);
/* Don't close pipe and clean up, but
* instead wait for IDENT result */
goto try_resolve;
case 1: /* IDENT user name */
- if( s->buffer[0] )
- {
- Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, s->buffer );
- Client_SetUser( c, s->buffer, true );
+ if (array_bytes(&s->buffer)) {
+ bufptr = (char*) array_start(&s->buffer);
+ Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, bufptr);
+ Client_SetUser( c, bufptr, true );
}
else Log( LOG_INFO, "IDENT lookup for connection %ld: no result.", i );
#endif
/* Search server ... */
n = Conf_GetServer( i );
assert( n > NONE );
-
- strlcpy( Conf_Server[n].ip, s->buffer, sizeof( Conf_Server[n].ip ));
+
+ bufptr = (char*) array_start(&s->buffer);
+ strlcpy( Conf_Server[n].ip, bufptr, sizeof( Conf_Server[n].ip ));
}
/* Clean up ... */