#include "portab.h"
#include "io.h"
-static char UNUSED id[] = "$Id: conn.c,v 1.166 2005/07/28 16:13:09 fw Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.167 2005/07/29 09:29:47 fw Exp $";
#include "imp.h"
#include <assert.h>
LOCAL bool Handle_Write PARAMS(( CONN_ID Idx ));
-LOCAL void New_Connection PARAMS(( int Sock ));
+LOCAL int New_Connection PARAMS(( int Sock ));
LOCAL CONN_ID Socket2Index PARAMS(( int Sock ));
LOCAL void Read_Request PARAMS(( CONN_ID Idx ));
LOCAL bool Handle_Buffer PARAMS(( CONN_ID Idx ));
int deny_severity = LOG_ERR;
#endif
+static void server_login PARAMS((CONN_ID idx));
static void cb_clientserver PARAMS((int sock, short what));
}
Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
+ server_login(idx);
+}
+
+static void
+server_login(CONN_ID idx)
+{
Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", idx,
My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port );
} /* Conn_Exit */
+static unsigned int
+ports_initlisteners(array *a, void (*func)(int,short))
+{
+ unsigned int created = 0, len;
+ int fd;
+ UINT16 *port;
+
+ len = array_length(a, sizeof (UINT16));
+ port = array_start(a);
+ while(len--) {
+ fd = NewListener( *port );
+ if (fd < 0) {
+ port++;
+ continue;
+ }
+ if (!io_event_create( fd, IO_WANTREAD, func )) {
+ Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!",
+ fd, (unsigned int) *port, strerror(errno));
+ close(fd);
+ port++;
+ continue;
+ }
+ created++;
+ port++;
+ }
+
+ return created;
+}
+
+
GLOBAL int
Conn_InitListeners( void )
{
/* Initialize ports on which the server should accept connections */
- int created, fd;
- unsigned int i;
+ unsigned int created;
if (!io_library_init(CONNECTION_POOL)) {
Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno));
return -1;
}
- created = 0;
- for( i = 0; i < Conf_ListenPorts_Count; i++ ) {
- fd = NewListener( Conf_ListenPorts[i] );
- if (fd < 0) {
- Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] );
- continue;
- }
- if (!io_event_create( fd, IO_WANTREAD, cb_listen )) {
- Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!",
- fd, (unsigned int) Conf_ListenPorts[i], strerror(errno));
- close(fd);
- continue;
- }
- created++;
- }
+ created = ports_initlisteners(&Conf_ListenPorts, cb_listen);
+
return created;
} /* Conn_InitListeners */
}
assert( My_Connections[Idx].sock > NONE );
-
wdatalen = array_bytes(&My_Connections[Idx].wbuf );
#ifdef ZLIB
if(( wdatalen == 0 ) && ( ! array_bytes(&My_Connections[Idx].zip.wbuf))) {
} /* Handle_Write */
-LOCAL void
+LOCAL int
New_Connection( int Sock )
{
/* Neue Client-Verbindung von Listen-Socket annehmen und
if( new_sock < 0 )
{
Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
- return;
+ return -1;
}
#ifdef TCPWRAP
Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr ));
Simple_Message( new_sock, "ERROR :Connection refused" );
close( new_sock );
- return;
+ return -1;
}
#endif
Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP address!", inet_ntoa( new_addr.sin_addr ), cnt);
Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" );
close( new_sock );
- return;
+ return -1;
}
/* Freie Connection-Struktur suchen */
Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
Simple_Message( new_sock, "ERROR :Connection limit reached" );
close( new_sock );
- return;
+ return -1;
}
if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
}
Log( LOG_ALERT, "Can't accept connection: limit (%d) reached -- overflow!", Pool_Size );
Simple_Message( new_sock, "ERROR :Connection limit reached" );
close( new_sock );
- return;
+ return -1;
}
ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
Simple_Message( new_sock, "ERROR: Internal error" );
close( new_sock );
- return;
+ return -1;
}
#ifdef DEBUG
Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
Simple_Message( new_sock, "ERROR :Internal error" );
close( new_sock );
- return;
+ return -1;
}
/* Verbindung registrieren */
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;
+ return -1;
}
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 );
/* Penalty-Zeit setzen */
Conn_SetPenalty( idx, 4 );
+ return new_sock;
} /* New_Connection */
/* Read result from pipe */
bytes_read = read( r_fd, readbuf, sizeof readbuf -1 );
- if( bytes_read < 0 )
- {
+ if( bytes_read < 0 ) {
/* Error! */
Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
FreeRes_stat( &My_Connections[i] );
}
len = (unsigned int) bytes_read;
readbuf[len] = '\0';
- if (!array_catb(&s->buffer, readbuf, len)) {
+ 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;
/* Search server ... */
n = Conf_GetServer( i );
assert( n > NONE );
-
+
bufptr = (char*) array_start(&s->buffer);
strlcpy( Conf_Server[n].ip, bufptr, sizeof( Conf_Server[n].ip ));
}