* Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
* der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
*
- * $Id: conn.c,v 1.6 2001/12/15 00:11:55 alex Exp $
+ * $Id: conn.c,v 1.9 2001/12/24 01:32:33 alex Exp $
*
* connect.h: Verwaltung aller Netz-Verbindungen ("connections")
*
* $Log: conn.c,v $
+ * 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.
#include <imp.h>
#include <assert.h>
+#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#endif
#include "ngircd.h"
+#include "client.h"
#include "log.h"
+#include "parse.h"
#include "tool.h"
#include <exp.h>
else if( FD_ISSET( i, &My_Listener ))
{
close( i );
- Log( LOG_INFO, "Closed listening socket %d.", i );
+ Log( LOG_INFO, "Listening socket %d closed.", i );
}
else
{
sock = socket( PF_INET, SOCK_STREAM, 0);
if( sock < 0 )
{
- Log( LOG_ALERT, "Can't create socket: %s", strerror( errno ));
+ Log( LOG_ALERT, "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 ));
+ 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 ));
+ Log( LOG_CRIT, "Can't set socket options: %s!", strerror( errno ));
/* dieser Fehler kann ignoriert werden. */
}
/* 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_ALERT, "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_ALERT, "Can't listen on soecket: %s!", strerror( errno ));
close( sock );
return FALSE;
}
read_sockets = My_Sockets;
if( select( My_Max_Fd + 1, &read_sockets, &write_sockets, NULL, &tv ) == -1 )
{
- if( errno != EINTR ) Log( LOG_ALERT, "select(): %s", strerror( errno ));
+ if( errno != EINTR ) Log( LOG_ALERT, "select(): %s!", strerror( errno ));
return;
}
} /* Conn_Handler */
-GLOBAL BOOLEAN Conn_WriteStr( CONN_ID Idx, CHAR *Data )
+GLOBAL BOOLEAN Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
{
/* String in Socket schreiben. CR+LF wird von dieser Funktion
- * automatisch angehaengt. */
+ * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
+ * getrennt und FALSE geliefert. */
CHAR buffer[MAX_CMDLEN];
-
- if( strlen( Data ) > MAX_CMDLEN - 2 )
+ BOOLEAN ok;
+ va_list ap;
+
+ va_start( ap, Format );
+ if( vsnprintf( buffer, MAX_CMDLEN - 2, Format, ap ) == MAX_CMDLEN - 2 )
{
Log( LOG_ALERT, "String too long to send (connection %d)!", Idx );
Close_Connection( Idx, "Server error: String too long to send!" );
return FALSE;
}
-
- sprintf( buffer, "%s\r\n", Data );
- return Conn_Write( Idx, buffer, strlen( buffer ));
+
+#ifdef DEBUG
+ Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
+#endif
+
+ strcat( buffer, "\r\n" );
+ ok = Conn_Write( Idx, buffer, strlen( buffer ));
+
+ va_end( ap );
+ return ok;
} /* Conn_WriteStr */
/* Fehler! */
if( errno != EINTR )
{
- Log( LOG_ALERT, "select(): %s", strerror( errno ));
+ Log( LOG_ALERT, "select(): %s!", strerror( errno ));
Close_Connection( Idx, NULL );
return FALSE;
}
if( len < 0 )
{
/* Oops, ein Fehler! */
- Log( LOG_ALERT, "Write error (buffer) on connection %d: %s", Idx, strerror( errno ));
+ Log( LOG_ALERT, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
Close_Connection( Idx, NULL );
return FALSE;
}
LOCAL VOID New_Connection( INT Sock )
{
- /* Neue Client-Verbindung von Listen-Socket annehmen */
+ /* Neue Client-Verbindung von Listen-Socket annehmen und
+ * CLIENT-Struktur anlegen. */
struct sockaddr_in new_addr;
INT new_sock, new_sock_len;
new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
if( new_sock < 0 )
{
- Log( LOG_CRIT, "Can't accept connection: %s", strerror( errno ));
+ Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
return;
}
close( new_sock );
return;
}
-
+
+ /* Client-Struktur initialisieren */
+ if( ! Client_New_Local( idx, inet_ntoa( new_addr.sin_addr )))
+ {
+ Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
+ close( new_sock );
+ return;
+ }
+
/* Verbindung registrieren */
My_Connections[idx].sock = new_sock;
My_Connections[idx].addr = new_addr;
{
/* Verbindung schlie§en */
- CHAR bye_str[MAX_CMDLEN];
-
assert( Idx >= 0 );
assert( My_Connections[Idx].sock >= 0 );
- if( Msg )
- {
- sprintf( bye_str, "ERROR :%s", Msg );
- Conn_WriteStr( Idx, bye_str );
- }
-
+ if( Msg ) Conn_WriteStr( Idx, "ERROR :%s", Msg );
+
if( close( My_Connections[Idx].sock ) != 0 )
{
- Log( LOG_ERR, "Error closing connection %d with %s:%d - %s", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
+ Log( LOG_ERR, "Error closing connection %d with %s:%d - %s!", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
+ return;
}
else
{
- Log( LOG_NOTICE, "Closed connection %d with %s:%d.", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
+ 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 ));
}
+ Client_Destroy( Client_GetFromConn( Idx ));
+
FD_CLR( My_Connections[Idx].sock, &My_Sockets );
My_Connections[Idx].sock = NONE;
} /* Close_Connection */
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));
Close_Connection( Idx, NULL );
return;
}
if( len < 0 )
{
/* Fehler beim Lesen */
- Log( LOG_ALERT, "Read error on connection %d!", Idx );
+ Log( LOG_ALERT, "Read error on connection %d: %s!", Idx, strerror( errno ));
Close_Connection( Idx, "Read error!" );
return;
}
len = ( ptr - My_Connections[Idx].rbuf ) + 2;
if( len > 2 )
{
- printf( " in: '%s'\n", My_Connections[Idx].rbuf );
+ /* Es wurde ein Request gelesen */
+ if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
}
else Log( LOG_DEBUG, "Got null-request (connection %d).", Idx );