* 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.90 2002/11/19 12:50:20 alex Exp $
+ * $Id: conn.c,v 1.95 2002/11/23 17:04:07 alex Exp $
*
* connect.h: Verwaltung aller Netz-Verbindungen ("connections")
*/
LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
-LOCAL VOID Handle_Buffer PARAMS(( CONN_ID Idx ));
+LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
LOCAL VOID Check_Connections PARAMS(( VOID ));
LOCAL VOID Check_Servers PARAMS(( VOID ));
LOCAL VOID Init_Conn_Struct PARAMS(( LONG Idx ));
} /* Conn_Exit */
+GLOBAL INT
+Conn_InitListeners( VOID )
+{
+ /* Ports, auf denen der Server Verbindungen entgegennehmen
+ * soll, initialisieren */
+
+ INT created, i;
+
+ created = 0;
+ for( i = 0; i < Conf_ListenPorts_Count; i++ )
+ {
+ if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
+ else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
+ }
+ return created;
+} /* Conn_InitListeners */
+
+
+GLOBAL VOID
+Conn_ExitListeners( VOID )
+{
+ /* Alle "Listen-Sockets" schliessen */
+
+ INT i;
+
+ Log( LOG_INFO, "Shutting down all listening sockets ..." );
+ for( i = 0; i < Conn_MaxFD + 1; i++ )
+ {
+ if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
+ {
+ close( i );
+ Log( LOG_DEBUG, "Listening socket %d closed.", i );
+ }
+ }
+} /* Conn_ExitListeners */
+
+
GLOBAL BOOLEAN
Conn_NewListener( CONST UINT Port )
{
struct timeval tv;
time_t start, t;
LONG i, idx;
+ BOOLEAN timeout;
start = time( NULL );
while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart ))
{
+ timeout = TRUE;
+
Check_Servers( );
Check_Connections( );
if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
{
/* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
- Handle_Buffer( i );
+ if( Handle_Buffer( i )) timeout = FALSE;
}
}
}
/* Timeout initialisieren */
- tv.tv_sec = 1;
tv.tv_usec = 0;
+ if( timeout ) tv.tv_sec = TIME_RES;
+ else tv.tv_sec = 0;
/* Auf Aktivitaet warten */
i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
if( close( My_Connections[Idx].sock ) != 0 )
{
- Log( LOG_ERR, "Error closing connection %d (socket %d) with %s:%d - %s!", Idx, My_Connections[Idx].sock, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
+ Log( LOG_ERR, "Error closing connection %d (socket %d) with %s:%d - %s!", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
}
else
{
- Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed.", Idx, My_Connections[Idx].sock, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
+ Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed (%.1fK in/%.1fK out).", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), (DOUBLE)My_Connections[Idx].bytes_in / 1024, (DOUBLE)My_Connections[Idx].bytes_out / 1024 );
}
/* Socket als "ungueltig" markieren */
* Socket zu schreiben. */
fd_set write_socket;
+ struct timeval tv;
assert( Idx > NONE );
assert( My_Connections[Idx].sock > NONE );
assert( My_Connections[Idx].wdatalen > 0 );
+ /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
+ tv.tv_sec = 0; tv.tv_usec = 0;
+
FD_ZERO( &write_socket );
FD_SET( My_Connections[Idx].sock, &write_socket );
- if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, 0 ) == -1 )
+ if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
{
/* Fehler! */
if( errno != EINTR )
len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
if( len < 0 )
{
+ /* Operation haette Socket "nur" blockiert ... */
+ if( errno == EAGAIN ) return TRUE;
+
/* Oops, ein Fehler! */
Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
Conn_Close( Idx, "Write error!", NULL, FALSE );
assert( Sock > NONE );
+ /* Connection auf Listen-Socket annehmen */
new_sock_len = sizeof( new_addr );
new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
if( new_sock < 0 )
return;
}
+ /* Socket initialisieren */
+ Init_Socket( new_sock );
+
/* Freie Connection-Struktur suchen */
for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
if( idx >= Pool_Size )
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 */
+ strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
+ Client_SetHostname( c, My_Connections[idx].host );
s = Resolve_Addr( &new_addr );
if( s )
{
Conn_WriteStr( idx, "NOTICE AUTH :%sLooking up your hostname ...", NOTICE_TXTPREFIX );
My_Connections[idx].res_stat = s;
}
- else
- {
- /* kann Namen nicht aufloesen, daher wird die IP-Adresse verwendet */
- strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
- Client_SetHostname( c, My_Connections[idx].host );
- }
/* Penalty-Zeit setzen */
Conn_SetPenalty( idx, 4 );
if( len < 0 )
{
+ /* Operation haette Socket "nur" blockiert ... */
+ if( errno == EAGAIN ) return;
+
/* Fehler beim Lesen */
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 );
} /* Read_Request */
-LOCAL VOID
+LOCAL BOOLEAN
Handle_Buffer( CONN_ID Idx )
{
- /* Daten im Lese-Puffer einer Verbindung verarbeiten. */
+ /* Daten im Lese-Puffer einer Verbindung verarbeiten.
+ * Wurde ein Request verarbeitet, so wird TRUE geliefert,
+ * ansonsten FALSE (auch bei Fehlern). */
#ifndef STRICT_RFC
CHAR *ptr1, *ptr2;
#endif
CHAR *ptr;
INT len, delta;
+ BOOLEAN action;
/* Eine komplette Anfrage muss mit CR+LF enden, vgl.
* RFC 2812. Haben wir eine? */
}
#endif
+ action = FALSE;
if( ptr )
{
/* Ende der Anfrage wurde gefunden */
if( len > ( COMMAND_LEN - 1 ))
{
/* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
- * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
- * empfangen wird, wird der Client disconnectiert. */
+ * (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 (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
Conn_Close( Idx, NULL, "Request too long", TRUE );
- return;
+ return FALSE;
}
if( len > delta )
{
/* Es wurde ein Request gelesen */
- if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
+ if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
+ else action = TRUE;
}
/* Puffer anpassen */
My_Connections[Idx].rdatalen -= len;
memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
}
+
+ return action;
} /* Handle_Buffer */
My_Connections[idx].sock = SERVER_WAIT;
My_Connections[idx].our_server = i;
- /* Hostnamen in IP aufloesen */
+ /* Hostnamen in IP aufloesen (Default bzw. im Fehlerfall: versuchen, den
+ * konfigurierten Text direkt als IP-Adresse zu verwenden ... */
+ strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
+ strcpy( My_Connections[idx].host, Conf_Server[i].host );
s = Resolve_Name( Conf_Server[i].host );
if( s )
{
/* Sub-Prozess wurde asyncron gestartet */
My_Connections[idx].res_stat = s;
}
- else
- {
- /* kann Namen nicht aufloesen: nun versuchen wir einfach,
- * den "Text" direkt als IP-Adresse zu verwenden ... */
- strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
- }
}
} /* Check_Servers */
return;
}
+ Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
+
/* Aufraeumen */
close( My_Connections[i].res_stat->pipe[0] );
close( My_Connections[i].res_stat->pipe[1] );