2001-12-12 18:18:38 +01:00
|
|
|
/*
|
|
|
|
* ngIRCd -- The Next Generation IRC Daemon
|
|
|
|
* Copyright (c)2001 by Alexander Barton (alex@barton.de)
|
|
|
|
*
|
|
|
|
* Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
|
|
|
|
* der GNU General Public License (GPL), wie von der Free Software Foundation
|
|
|
|
* herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
|
|
|
|
* der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
|
|
|
|
* Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
|
|
|
|
* der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
|
|
|
|
*
|
2001-12-29 04:06:16 +01:00
|
|
|
* $Id: conn.c,v 1.17 2001/12/29 03:06:16 alex Exp $
|
2001-12-12 18:18:38 +01:00
|
|
|
*
|
|
|
|
* connect.h: Verwaltung aller Netz-Verbindungen ("connections")
|
|
|
|
*
|
|
|
|
* $Log: conn.c,v $
|
2001-12-29 04:06:16 +01:00
|
|
|
* Revision 1.17 2001/12/29 03:06:16 alex
|
|
|
|
* - Loglevel (nochmal) angepasst.
|
|
|
|
*
|
2001-12-27 20:32:44 +01:00
|
|
|
* Revision 1.16 2001/12/27 19:32:44 alex
|
|
|
|
* - bei "Null-Requests" wird nichts mehr geloggt. Uberfluessig, da normal.
|
|
|
|
*
|
2001-12-27 17:35:04 +01:00
|
|
|
* Revision 1.15 2001/12/27 16:35:04 alex
|
|
|
|
* - vergessene Variable bei Ping-Timeout-Logmeldung ergaenzt. Opsa.
|
|
|
|
*
|
2001-12-26 15:45:37 +01:00
|
|
|
* Revision 1.14 2001/12/26 14:45:37 alex
|
|
|
|
* - "Code Cleanups".
|
|
|
|
*
|
2001-12-26 04:36:57 +01:00
|
|
|
* Revision 1.13 2001/12/26 03:36:57 alex
|
|
|
|
* - Verbindungen mit Lesefehlern werden nun korrekt terminiert.
|
|
|
|
*
|
2001-12-26 04:20:53 +01:00
|
|
|
* Revision 1.12 2001/12/26 03:20:53 alex
|
|
|
|
* - PING/PONG-Timeout implementiert.
|
|
|
|
*
|
2001-12-26 00:15:16 +01:00
|
|
|
* Revision 1.11 2001/12/25 23:15:16 alex
|
|
|
|
* - buffer werden nun periodisch geprueft, keine haengenden Clients mehr.
|
|
|
|
*
|
2001-12-25 23:03:47 +01:00
|
|
|
* Revision 1.10 2001/12/25 22:03:47 alex
|
|
|
|
* - Conn_Close() eingefuehrt: war die lokale Funktion Close_Connection().
|
|
|
|
*
|
2001-12-24 02:32:33 +01:00
|
|
|
* Revision 1.9 2001/12/24 01:32:33 alex
|
|
|
|
* - in Conn_WriteStr() wurde das CR+LF nicht angehaengt!
|
|
|
|
* - Fehler-Ausgaben vereinheitlicht.
|
|
|
|
*
|
2001-12-23 23:02:54 +01:00
|
|
|
* Revision 1.8 2001/12/23 22:02:54 alex
|
|
|
|
* - Conn_WriteStr() nimmt nun variable Parameter,
|
|
|
|
* - diverse kleinere Aenderungen.
|
|
|
|
*
|
2001-12-21 23:24:25 +01:00
|
|
|
* Revision 1.7 2001/12/21 22:24:25 alex
|
|
|
|
* - kleinere Aenderungen an den Log-Meldungen,
|
|
|
|
* - Parse_Request() wird aufgerufen.
|
|
|
|
*
|
2001-12-15 01:11:55 +01:00
|
|
|
* Revision 1.6 2001/12/15 00:11:55 alex
|
|
|
|
* - Lese- und Schreib-Puffer implementiert.
|
|
|
|
* - einige neue (Unter-)Funktionen eingefuehrt.
|
|
|
|
* - diverse weitere kleinere Aenderungen.
|
|
|
|
*
|
2001-12-14 09:16:47 +01:00
|
|
|
* Revision 1.5 2001/12/14 08:16:47 alex
|
|
|
|
* - Begonnen, Client-spezifische Lesepuffer zu implementieren.
|
|
|
|
* - Umstellung auf Datentyp "CONN_ID".
|
|
|
|
*
|
2001-12-13 03:04:16 +01:00
|
|
|
* Revision 1.4 2001/12/13 02:04:16 alex
|
|
|
|
* - boesen "Speicherschiesser" in Log() gefixt.
|
|
|
|
*
|
2001-12-13 02:33:09 +01:00
|
|
|
* Revision 1.3 2001/12/13 01:33:09 alex
|
|
|
|
* - Conn_Handler() unterstuetzt nun einen Timeout.
|
|
|
|
* - fuer Verbindungen werden keine FILE-Handles mehr benutzt.
|
|
|
|
* - kleinere "Code Cleanups" ;-)
|
|
|
|
*
|
2001-12-13 00:32:02 +01:00
|
|
|
* Revision 1.2 2001/12/12 23:32:02 alex
|
|
|
|
* - diverse Erweiterungen und Verbesserungen (u.a. sind nun mehrere
|
|
|
|
* Verbindungen und Listen-Sockets moeglich).
|
|
|
|
*
|
2001-12-12 18:18:38 +01:00
|
|
|
* Revision 1.1 2001/12/12 17:18:38 alex
|
|
|
|
* - Modul zur Verwaltung aller Netzwerk-Verbindungen begonnen.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <portab.h>
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
#include <imp.h>
|
|
|
|
#include <assert.h>
|
2001-12-23 23:02:54 +01:00
|
|
|
#include <stdarg.h>
|
2001-12-12 18:18:38 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2001-12-26 00:15:16 +01:00
|
|
|
#include <sys/socket.h>
|
2001-12-12 18:18:38 +01:00
|
|
|
#include <sys/time.h>
|
2001-12-26 00:15:16 +01:00
|
|
|
#include <sys/types.h>
|
2001-12-26 04:36:57 +01:00
|
|
|
#include <time.h>
|
2001-12-12 18:18:38 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
#include <stdint.h> /* u.a. fuer Mac OS X */
|
|
|
|
#endif
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
#include "ngircd.h"
|
2001-12-23 23:02:54 +01:00
|
|
|
#include "client.h"
|
2001-12-26 04:20:53 +01:00
|
|
|
#include "conf.h"
|
2001-12-12 18:18:38 +01:00
|
|
|
#include "log.h"
|
2001-12-21 23:24:25 +01:00
|
|
|
#include "parse.h"
|
2001-12-13 00:32:02 +01:00
|
|
|
#include "tool.h"
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
#include <exp.h>
|
|
|
|
#include "conn.h"
|
|
|
|
|
|
|
|
|
2001-12-14 09:16:47 +01:00
|
|
|
#define MAX_CONNECTIONS 100 /* max. Anzahl von Verbindungen an diesem Server */
|
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
#define MAX_CMDLEN 512 /* max. Laenge eines Befehls, vgl. RFC 2812, 3.2 */
|
|
|
|
|
|
|
|
#define READBUFFER_LEN 2 * MAX_CMDLEN /* Laenge des Lesepuffers je Verbindung (Bytes) */
|
|
|
|
#define WRITEBUFFER_LEN 4096 /* Laenge des Schreibpuffers je Verbindung (Bytes) */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct _Connection
|
|
|
|
{
|
|
|
|
INT sock; /* Socket Handle */
|
|
|
|
struct sockaddr_in addr; /* Adresse des Client */
|
2001-12-15 01:11:55 +01:00
|
|
|
CHAR rbuf[READBUFFER_LEN + 1]; /* Lesepuffer */
|
|
|
|
INT rdatalen; /* Laenge der Daten im Lesepuffer */
|
|
|
|
CHAR wbuf[WRITEBUFFER_LEN + 1]; /* Schreibpuffer */
|
|
|
|
INT wdatalen; /* Laenge der Daten im Schreibpuffer */
|
2001-12-26 04:20:53 +01:00
|
|
|
time_t lastdata; /* Letzte Aktivitaet */
|
|
|
|
time_t lastping; /* Letzter PING */
|
2001-12-13 00:32:02 +01:00
|
|
|
} CONNECTION;
|
|
|
|
|
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
LOCAL VOID Handle_Read( INT sock );
|
|
|
|
LOCAL BOOLEAN Handle_Write( CONN_ID Idx );
|
2001-12-13 00:32:02 +01:00
|
|
|
LOCAL VOID New_Connection( INT Sock );
|
2001-12-14 09:16:47 +01:00
|
|
|
LOCAL CONN_ID Socket2Index( INT Sock );
|
|
|
|
LOCAL VOID Read_Request( CONN_ID Idx );
|
2001-12-15 01:11:55 +01:00
|
|
|
LOCAL BOOLEAN Try_Write( CONN_ID Idx );
|
2001-12-26 00:15:16 +01:00
|
|
|
LOCAL VOID Handle_Buffer( CONN_ID Idx );
|
2001-12-26 04:20:53 +01:00
|
|
|
LOCAL VOID Check_Connections( VOID );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
LOCAL fd_set My_Listener;
|
|
|
|
LOCAL fd_set My_Sockets;
|
|
|
|
|
|
|
|
LOCAL INT My_Max_Fd;
|
|
|
|
|
|
|
|
LOCAL CONNECTION My_Connections[MAX_CONNECTIONS];
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
GLOBAL VOID Conn_Init( VOID )
|
|
|
|
{
|
2001-12-14 09:16:47 +01:00
|
|
|
CONN_ID i;
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
/* zu Beginn haben wir keine Verbindungen */
|
|
|
|
FD_ZERO( &My_Listener );
|
|
|
|
FD_ZERO( &My_Sockets );
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
My_Max_Fd = 0;
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
/* Connection-Struktur initialisieren */
|
2001-12-15 01:11:55 +01:00
|
|
|
for( i = 0; i < MAX_CONNECTIONS; i++ ) My_Connections[i].sock = NONE;
|
2001-12-12 18:18:38 +01:00
|
|
|
} /* Conn_Init */
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL VOID Conn_Exit( VOID )
|
|
|
|
{
|
2001-12-14 09:16:47 +01:00
|
|
|
CONN_ID idx;
|
|
|
|
INT i;
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
/* Sockets schliessen */
|
|
|
|
for( i = 0; i < My_Max_Fd + 1; i++ )
|
|
|
|
{
|
|
|
|
if( FD_ISSET( i, &My_Sockets ))
|
|
|
|
{
|
|
|
|
for( idx = 0; idx < MAX_CONNECTIONS; idx++ )
|
|
|
|
{
|
|
|
|
if( My_Connections[idx].sock == i ) break;
|
|
|
|
}
|
2001-12-25 23:03:47 +01:00
|
|
|
if( idx < MAX_CONNECTIONS ) Conn_Close( idx, "Server going down ..." );
|
2001-12-13 00:32:02 +01:00
|
|
|
else if( FD_ISSET( i, &My_Listener ))
|
|
|
|
{
|
|
|
|
close( i );
|
2001-12-21 23:24:25 +01:00
|
|
|
Log( LOG_INFO, "Listening socket %d closed.", i );
|
2001-12-13 00:32:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close( i );
|
|
|
|
Log( LOG_WARNING, "Unknown connection %d closed.", i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-12-12 18:18:38 +01:00
|
|
|
} /* Conn_Exit */
|
|
|
|
|
|
|
|
|
2001-12-26 15:45:37 +01:00
|
|
|
GLOBAL BOOLEAN Conn_NewListener( CONST INT Port )
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
|
|
|
/* Neuen Listen-Socket erzeugen: der Server wartet dann
|
|
|
|
* auf dem angegebenen Port auf Verbindungen. */
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
INT sock, on = 1;
|
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
/* Server-"Listen"-Socket initialisieren */
|
2001-12-13 00:32:02 +01:00
|
|
|
memset( &addr, 0, sizeof( addr ));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons( Port );
|
|
|
|
addr.sin_addr.s_addr = htonl( INADDR_ANY );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
/* Socket erzeugen */
|
2001-12-13 00:32:02 +01:00
|
|
|
sock = socket( PF_INET, SOCK_STREAM, 0);
|
2001-12-13 03:04:16 +01:00
|
|
|
if( sock < 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_ALERT, "Can't create socket: %s!", strerror( errno ));
|
2001-12-12 18:18:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Socket-Optionen setzen */
|
2001-12-13 00:32:02 +01:00
|
|
|
if( fcntl( sock, F_SETFL, O_NONBLOCK ) != 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_ALERT, "Can't enable non-blocking mode: %s!", strerror( errno ));
|
2001-12-13 00:32:02 +01:00
|
|
|
close( sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
if( setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_CRIT, "Can't set socket options: %s!", strerror( errno ));
|
2001-12-12 18:18:38 +01:00
|
|
|
/* dieser Fehler kann ignoriert werden. */
|
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
/* an Port binden */
|
2001-12-13 00:32:02 +01:00
|
|
|
if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_ALERT, "Can't bind socket: %s!", strerror( errno ));
|
2001-12-13 00:32:02 +01:00
|
|
|
close( sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in "listen mode" gehen :-) */
|
2001-12-13 00:32:02 +01:00
|
|
|
if( listen( sock, 10 ) != 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_ALERT, "Can't listen on soecket: %s!", strerror( errno ));
|
2001-12-13 00:32:02 +01:00
|
|
|
close( sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
/* Neuen Listener in Strukturen einfuegen */
|
|
|
|
FD_SET( sock, &My_Listener );
|
|
|
|
FD_SET( sock, &My_Sockets );
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
if( sock > My_Max_Fd ) My_Max_Fd = sock;
|
|
|
|
|
2001-12-14 09:16:47 +01:00
|
|
|
Log( LOG_INFO, "Now listening on port %d, socket %d.", Port, sock );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
return TRUE;
|
2001-12-26 15:45:37 +01:00
|
|
|
} /* Conn_NewListener */
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
GLOBAL VOID Conn_Handler( INT Timeout )
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
2001-12-15 01:11:55 +01:00
|
|
|
fd_set read_sockets, write_sockets;
|
2001-12-13 02:33:09 +01:00
|
|
|
struct timeval tv;
|
2001-12-26 00:15:16 +01:00
|
|
|
time_t start;
|
2001-12-13 00:32:02 +01:00
|
|
|
INT i;
|
2001-12-13 02:33:09 +01:00
|
|
|
|
2001-12-26 00:15:16 +01:00
|
|
|
start = time( NULL );
|
2001-12-26 04:20:53 +01:00
|
|
|
while(( time( NULL ) - start < Timeout ) && ( ! NGIRCd_Quit ))
|
2001-12-15 01:11:55 +01:00
|
|
|
{
|
2001-12-26 04:20:53 +01:00
|
|
|
Check_Connections( );
|
|
|
|
|
|
|
|
/* Timeout initialisieren */
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 50000;
|
|
|
|
|
|
|
|
/* noch volle Lese-Buffer suchen */
|
2001-12-26 00:15:16 +01:00
|
|
|
for( i = 0; i < MAX_CONNECTIONS; i++ )
|
2001-12-15 01:11:55 +01:00
|
|
|
{
|
2001-12-26 04:20:53 +01:00
|
|
|
if(( My_Connections[i].sock >= 0 ) && ( My_Connections[i].rdatalen > 0 ))
|
2001-12-26 00:15:16 +01:00
|
|
|
{
|
2001-12-26 04:20:53 +01:00
|
|
|
/* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
|
|
|
|
Handle_Buffer( i );
|
2001-12-26 00:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-26 04:20:53 +01:00
|
|
|
/* noch volle Schreib-Puffer suchen */
|
|
|
|
FD_ZERO( &write_sockets );
|
2001-12-26 00:15:16 +01:00
|
|
|
for( i = 0; i < MAX_CONNECTIONS; i++ )
|
|
|
|
{
|
2001-12-26 04:20:53 +01:00
|
|
|
if(( My_Connections[i].sock >= 0 ) && ( My_Connections[i].wdatalen > 0 ))
|
2001-12-26 00:15:16 +01:00
|
|
|
{
|
2001-12-26 04:20:53 +01:00
|
|
|
/* Socket der Verbindung in Set aufnehmen */
|
|
|
|
FD_SET( My_Connections[i].sock, &write_sockets );
|
2001-12-26 00:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
2001-12-26 04:20:53 +01:00
|
|
|
|
2001-12-26 00:15:16 +01:00
|
|
|
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 ));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Koennen Daten geschrieben werden? */
|
|
|
|
for( i = 0; i < My_Max_Fd + 1; i++ )
|
|
|
|
{
|
|
|
|
if( FD_ISSET( i, &write_sockets )) Handle_Write( Socket2Index( i ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Daten zum Lesen vorhanden? */
|
|
|
|
for( i = 0; i < My_Max_Fd + 1; i++ )
|
|
|
|
{
|
|
|
|
if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
|
2001-12-15 01:11:55 +01:00
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
}
|
|
|
|
} /* Conn_Handler */
|
|
|
|
|
|
|
|
|
2001-12-23 23:02:54 +01:00
|
|
|
GLOBAL BOOLEAN Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
|
2001-12-15 01:11:55 +01:00
|
|
|
{
|
|
|
|
/* String in Socket schreiben. CR+LF wird von dieser Funktion
|
2001-12-23 23:02:54 +01:00
|
|
|
* automatisch angehaengt. Im Fehlerfall wird dir Verbindung
|
|
|
|
* getrennt und FALSE geliefert. */
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
CHAR buffer[MAX_CMDLEN];
|
2001-12-23 23:02:54 +01:00
|
|
|
BOOLEAN ok;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start( ap, Format );
|
2001-12-24 02:32:33 +01:00
|
|
|
if( vsnprintf( buffer, MAX_CMDLEN - 2, Format, ap ) == MAX_CMDLEN - 2 )
|
2001-12-15 01:11:55 +01:00
|
|
|
{
|
|
|
|
Log( LOG_ALERT, "String too long to send (connection %d)!", Idx );
|
2001-12-25 23:03:47 +01:00
|
|
|
Conn_Close( Idx, "Server error: String too long to send!" );
|
2001-12-15 01:11:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-12-23 23:02:54 +01:00
|
|
|
|
2001-12-25 23:03:47 +01:00
|
|
|
#ifdef SNIFFER
|
2001-12-23 23:02:54 +01:00
|
|
|
Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
|
|
|
|
#endif
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-24 02:32:33 +01:00
|
|
|
strcat( buffer, "\r\n" );
|
|
|
|
ok = Conn_Write( Idx, buffer, strlen( buffer ));
|
|
|
|
|
2001-12-23 23:02:54 +01:00
|
|
|
va_end( ap );
|
|
|
|
return ok;
|
2001-12-15 01:11:55 +01:00
|
|
|
} /* Conn_WriteStr */
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL BOOLEAN Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
|
|
|
|
{
|
|
|
|
/* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
|
|
|
|
* der Client disconnectiert und FALSE geliefert. */
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
assert( Idx >= 0 );
|
|
|
|
assert( My_Connections[Idx].sock >= 0 );
|
|
|
|
assert( Data != NULL );
|
|
|
|
assert( Len > 0 );
|
|
|
|
|
|
|
|
/* pruefen, ob Daten im Schreibpuffer sind. Wenn ja, zunaechst
|
|
|
|
* pruefen, ob diese gesendet werden koennen */
|
|
|
|
if( My_Connections[Idx].wdatalen > 0 )
|
|
|
|
{
|
|
|
|
if( ! Try_Write( Idx )) return FALSE;
|
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
/* pruefen, ob im Schreibpuffer genuegend Platz ist */
|
|
|
|
if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
|
|
|
|
{
|
|
|
|
/* der Puffer ist dummerweise voll ... */
|
|
|
|
Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
|
2001-12-25 23:03:47 +01:00
|
|
|
Conn_Close( Idx, NULL );
|
2001-12-15 01:11:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Daten in Puffer kopieren */
|
|
|
|
memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
|
|
|
|
My_Connections[Idx].wdatalen += Len;
|
|
|
|
|
|
|
|
/* pruefen, on Daten vorhanden sind und geschrieben werden koennen */
|
|
|
|
if( My_Connections[Idx].wdatalen > 0 )
|
|
|
|
{
|
|
|
|
if( ! Try_Write( Idx )) return FALSE;
|
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
return TRUE;
|
|
|
|
} /* Conn_Write */
|
|
|
|
|
|
|
|
|
2001-12-25 23:03:47 +01:00
|
|
|
GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *Msg )
|
|
|
|
{
|
|
|
|
/* Verbindung schliessen */
|
|
|
|
|
|
|
|
assert( Idx >= 0 );
|
|
|
|
assert( My_Connections[Idx].sock >= 0 );
|
|
|
|
|
|
|
|
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 ));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
} /* Conn_Close */
|
|
|
|
|
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
LOCAL BOOLEAN Try_Write( CONN_ID Idx )
|
|
|
|
{
|
|
|
|
/* Versuchen, Daten aus dem Schreib-Puffer in den
|
|
|
|
* Socket zu schreiben. */
|
|
|
|
|
|
|
|
fd_set write_socket;
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
assert( Idx >= 0 );
|
|
|
|
assert( My_Connections[Idx].sock >= 0 );
|
|
|
|
assert( My_Connections[Idx].wdatalen > 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 )
|
|
|
|
{
|
|
|
|
/* Fehler! */
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_ALERT, "select(): %s!", strerror( errno ));
|
2001-12-25 23:03:47 +01:00
|
|
|
Conn_Close( Idx, NULL );
|
2001-12-15 01:11:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
|
|
|
|
else return TRUE;
|
|
|
|
} /* Try_Write */
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID Handle_Read( INT Sock )
|
2001-12-13 00:32:02 +01:00
|
|
|
{
|
|
|
|
/* Aktivitaet auf einem Socket verarbeiten */
|
|
|
|
|
2001-12-14 09:16:47 +01:00
|
|
|
CONN_ID idx;
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
assert( Sock >= 0 );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
if( FD_ISSET( Sock, &My_Listener ))
|
|
|
|
{
|
|
|
|
/* es ist einer unserer Listener-Sockets: es soll
|
|
|
|
* also eine neue Verbindung aufgebaut werden. */
|
|
|
|
|
|
|
|
New_Connection( Sock );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Ein Client Socket: entweder ein User oder Server */
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
idx = Socket2Index( Sock );
|
2001-12-13 02:33:09 +01:00
|
|
|
Read_Request( idx );
|
2001-12-13 00:32:02 +01:00
|
|
|
}
|
2001-12-15 01:11:55 +01:00
|
|
|
} /* Handle_Read */
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL BOOLEAN Handle_Write( CONN_ID Idx )
|
|
|
|
{
|
|
|
|
/* Daten aus Schreibpuffer versenden */
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
INT len;
|
|
|
|
|
|
|
|
assert( Idx >= 0 );
|
|
|
|
assert( My_Connections[Idx].sock >= 0 );
|
|
|
|
assert( My_Connections[Idx].wdatalen > 0 );
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
/* Daten schreiben */
|
|
|
|
len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
|
|
|
|
if( len < 0 )
|
|
|
|
{
|
|
|
|
/* Oops, ein Fehler! */
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_ALERT, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
|
2001-12-25 23:03:47 +01:00
|
|
|
Conn_Close( Idx, NULL );
|
2001-12-15 01:11:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
/* Puffer anpassen */
|
|
|
|
My_Connections[Idx].wdatalen -= len;
|
|
|
|
memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
return TRUE;
|
|
|
|
} /* Handle_Write */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID New_Connection( INT Sock )
|
|
|
|
{
|
2001-12-23 23:02:54 +01:00
|
|
|
/* Neue Client-Verbindung von Listen-Socket annehmen und
|
|
|
|
* CLIENT-Struktur anlegen. */
|
2001-12-15 01:11:55 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
struct sockaddr_in new_addr;
|
2001-12-14 09:16:47 +01:00
|
|
|
INT new_sock, new_sock_len;
|
|
|
|
CONN_ID idx;
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
assert( Sock >= 0 );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
new_sock_len = sizeof( new_addr );
|
2001-12-13 03:04:16 +01:00
|
|
|
new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
|
2001-12-13 00:32:02 +01:00
|
|
|
if( new_sock < 0 )
|
|
|
|
{
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
|
2001-12-13 00:32:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
/* Freie Connection-Struktur suschen */
|
|
|
|
for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock < 0 ) break;
|
|
|
|
if( idx >= MAX_CONNECTIONS )
|
2001-12-12 18:18:38 +01:00
|
|
|
{
|
2001-12-15 01:11:55 +01:00
|
|
|
Log( LOG_ALERT, "Can't accept connection: limit reached (%d)!", MAX_CONNECTIONS );
|
2001-12-13 00:32:02 +01:00
|
|
|
close( new_sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-23 23:02:54 +01:00
|
|
|
/* Client-Struktur initialisieren */
|
2001-12-26 15:45:37 +01:00
|
|
|
if( ! Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr )))
|
2001-12-23 23:02:54 +01:00
|
|
|
{
|
|
|
|
Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
|
|
|
|
close( new_sock );
|
|
|
|
return;
|
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
/* Verbindung registrieren */
|
|
|
|
My_Connections[idx].sock = new_sock;
|
|
|
|
My_Connections[idx].addr = new_addr;
|
2001-12-15 01:11:55 +01:00
|
|
|
My_Connections[idx].rdatalen = 0;
|
|
|
|
My_Connections[idx].wdatalen = 0;
|
2001-12-26 04:20:53 +01:00
|
|
|
My_Connections[idx].lastdata = time( NULL );
|
|
|
|
My_Connections[idx].lastping = 0;
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
/* Neuen Socket registrieren */
|
|
|
|
FD_SET( new_sock, &My_Sockets );
|
|
|
|
|
|
|
|
if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock;
|
2001-12-12 18:18:38 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
Log( LOG_NOTICE, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
|
2001-12-13 00:32:02 +01:00
|
|
|
} /* New_Connection */
|
|
|
|
|
|
|
|
|
2001-12-14 09:16:47 +01:00
|
|
|
LOCAL CONN_ID Socket2Index( INT Sock )
|
2001-12-13 00:32:02 +01:00
|
|
|
{
|
2001-12-15 01:11:55 +01:00
|
|
|
/* zum Socket passende Connection suchen */
|
|
|
|
|
2001-12-14 09:16:47 +01:00
|
|
|
CONN_ID idx;
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
assert( Sock >= 0 );
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == Sock ) break;
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
assert( idx < MAX_CONNECTIONS );
|
2001-12-13 00:32:02 +01:00
|
|
|
return idx;
|
|
|
|
} /* Socket2Index */
|
|
|
|
|
|
|
|
|
2001-12-14 09:16:47 +01:00
|
|
|
LOCAL VOID Read_Request( CONN_ID Idx )
|
2001-12-13 00:32:02 +01:00
|
|
|
{
|
2001-12-13 02:33:09 +01:00
|
|
|
/* Daten von Socket einlesen und entsprechend behandeln.
|
|
|
|
* Tritt ein Fehler auf, so wird der Socket geschlossen. */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
INT len;
|
2001-12-15 01:11:55 +01:00
|
|
|
|
|
|
|
assert( Idx >= 0 );
|
|
|
|
assert( My_Connections[Idx].sock >= 0 );
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, READBUFFER_LEN - My_Connections[Idx].rdatalen, 0 );
|
|
|
|
My_Connections[Idx].rbuf[READBUFFER_LEN] = '\0';
|
2001-12-13 02:33:09 +01:00
|
|
|
|
|
|
|
if( len == 0 )
|
|
|
|
{
|
|
|
|
/* Socket wurde geschlossen */
|
2001-12-21 23:24:25 +01:00
|
|
|
Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
|
2001-12-25 23:03:47 +01:00
|
|
|
Conn_Close( Idx, NULL );
|
2001-12-13 02:33:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( len < 0 )
|
2001-12-13 00:32:02 +01:00
|
|
|
{
|
2001-12-13 02:33:09 +01:00
|
|
|
/* Fehler beim Lesen */
|
2001-12-24 02:32:33 +01:00
|
|
|
Log( LOG_ALERT, "Read error on connection %d: %s!", Idx, strerror( errno ));
|
2001-12-26 04:36:57 +01:00
|
|
|
Conn_Close( Idx, NULL );
|
2001-12-13 00:32:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2001-12-14 09:16:47 +01:00
|
|
|
|
2001-12-26 04:20:53 +01:00
|
|
|
/* Lesebuffer updaten */
|
2001-12-15 01:11:55 +01:00
|
|
|
My_Connections[Idx].rdatalen += len;
|
|
|
|
assert( My_Connections[Idx].rdatalen <= READBUFFER_LEN );
|
|
|
|
My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
|
2001-12-12 18:18:38 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
if( My_Connections[Idx].rdatalen > MAX_CMDLEN )
|
|
|
|
{
|
|
|
|
/* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
|
|
|
|
* (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
|
|
|
|
* empfangen wird, wird der Client disconnectiert. */
|
|
|
|
Log( LOG_ALERT, "Request too long (connection %d)!", Idx );
|
2001-12-25 23:03:47 +01:00
|
|
|
Conn_Close( Idx, "Request too long!" );
|
2001-12-15 01:11:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
|
2001-12-26 04:20:53 +01:00
|
|
|
/* Timestamp aktualisieren */
|
|
|
|
My_Connections[Idx].lastdata = time( NULL );
|
|
|
|
|
2001-12-26 00:15:16 +01:00
|
|
|
Handle_Buffer( Idx );
|
|
|
|
} /* Read_Request */
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID Handle_Buffer( CONN_ID Idx )
|
|
|
|
{
|
|
|
|
CHAR *ptr, *ptr1, *ptr2;
|
|
|
|
INT len, delta;
|
2001-12-13 02:33:09 +01:00
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
/* Eine komplette Anfrage muss mit CR+LF enden, vgl.
|
|
|
|
* RFC 2812. Haben wir eine? */
|
|
|
|
ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
|
2001-12-26 00:15:16 +01:00
|
|
|
|
|
|
|
if( ptr ) delta = 2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
|
|
|
|
* machen soetwas viele Clients, u.a. "mIRC" :-( */
|
|
|
|
ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
|
|
|
|
ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
|
|
|
|
delta = 1;
|
|
|
|
if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
|
|
|
|
else if( ptr1 ) ptr = ptr1;
|
|
|
|
else if( ptr2 ) ptr = ptr2;
|
|
|
|
}
|
|
|
|
|
2001-12-15 01:11:55 +01:00
|
|
|
if( ptr )
|
2001-12-13 02:33:09 +01:00
|
|
|
{
|
2001-12-26 00:15:16 +01:00
|
|
|
/* Ende der Anfrage wurde gefunden */
|
2001-12-15 01:11:55 +01:00
|
|
|
*ptr = '\0';
|
2001-12-26 00:15:16 +01:00
|
|
|
len = ( ptr - My_Connections[Idx].rbuf ) + delta;
|
|
|
|
if( len > delta )
|
2001-12-13 02:33:09 +01:00
|
|
|
{
|
2001-12-21 23:24:25 +01:00
|
|
|
/* Es wurde ein Request gelesen */
|
|
|
|
if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
|
2001-12-13 02:33:09 +01:00
|
|
|
}
|
2001-12-15 01:11:55 +01:00
|
|
|
|
|
|
|
/* Puffer anpassen */
|
|
|
|
My_Connections[Idx].rdatalen -= len;
|
|
|
|
memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
|
2001-12-13 02:33:09 +01:00
|
|
|
}
|
2001-12-26 00:15:16 +01:00
|
|
|
} /* Handle_Buffer */
|
2001-12-13 02:33:09 +01:00
|
|
|
|
|
|
|
|
2001-12-26 04:20:53 +01:00
|
|
|
LOCAL VOID Check_Connections( VOID )
|
|
|
|
{
|
|
|
|
/* Pruefen, ob Verbindungen noch "alive" sind */
|
|
|
|
|
|
|
|
INT i;
|
|
|
|
|
|
|
|
for( i = 0; i < MAX_CONNECTIONS; i++ )
|
|
|
|
{
|
|
|
|
if( My_Connections[i].sock != NONE )
|
|
|
|
{
|
|
|
|
if( My_Connections[i].lastping > My_Connections[i].lastdata )
|
|
|
|
{
|
|
|
|
/* es wurde bereits ein PING gesendet */
|
2001-12-26 15:45:37 +01:00
|
|
|
if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
|
2001-12-26 04:20:53 +01:00
|
|
|
{
|
|
|
|
/* Timeout */
|
2001-12-29 04:06:16 +01:00
|
|
|
Log( LOG_INFO, "Connection %d: Ping timeout.", i );
|
2001-12-26 04:20:53 +01:00
|
|
|
Conn_Close( i, "Ping timeout" );
|
|
|
|
}
|
|
|
|
}
|
2001-12-26 15:45:37 +01:00
|
|
|
else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
|
2001-12-26 04:20:53 +01:00
|
|
|
{
|
|
|
|
/* es muss ein PING gesendet werden */
|
|
|
|
Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
|
|
|
|
My_Connections[i].lastping = time( NULL );
|
|
|
|
Conn_WriteStr( i, "PING :%s", This_Server->nick );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* Conn_Check */
|
|
|
|
|
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
/* -eof- */
|