- mehr Unterfunktionen eingebaut, Modul besser strukturiert & dokumentiert.
- Anpassungen an neue Module.
This commit is contained in:
parent
7da703f186
commit
418add93da
|
@ -9,11 +9,15 @@
|
|||
* Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
|
||||
* der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
|
||||
*
|
||||
* $Id: ngircd.c,v 1.4 2001/12/12 01:58:53 alex Exp $
|
||||
* $Id: ngircd.c,v 1.5 2001/12/12 17:21:21 alex Exp $
|
||||
*
|
||||
* ngircd.c: Hier beginnt alles ;-)
|
||||
*
|
||||
* $Log: ngircd.c,v $
|
||||
* Revision 1.5 2001/12/12 17:21:21 alex
|
||||
* - mehr Unterfunktionen eingebaut, Modul besser strukturiert & dokumentiert.
|
||||
* - Anpassungen an neue Module.
|
||||
*
|
||||
* Revision 1.4 2001/12/12 01:58:53 alex
|
||||
* - Test auf socklen_t verbessert.
|
||||
*
|
||||
|
@ -38,22 +42,12 @@
|
|||
|
||||
#include <imp.h>
|
||||
|
||||
#ifdef HAVE_STDINT_H
|
||||
#include <stdint.h> /* u.a. fuer Mac OS X */
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#include "conf.h"
|
||||
#include "conn.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <exp.h>
|
||||
|
@ -63,22 +57,47 @@
|
|||
BOOLEAN do_quit_now = FALSE; /* TRUE: Hauptschleife beenden */
|
||||
|
||||
|
||||
LOCAL VOID Initialize_Signal_Handler( VOID );
|
||||
LOCAL VOID Signal_Handler( INT Signal );
|
||||
|
||||
|
||||
GLOBAL INT main( INT argc, CONST CHAR *argv[] )
|
||||
{
|
||||
FILE *fd;
|
||||
struct sigaction saction;
|
||||
struct sockaddr_in my_addr, a_addr;
|
||||
int my_sock, a_sock;
|
||||
int a_sock_len;
|
||||
|
||||
/* Datentypen der portab-Library ueberpruefen */
|
||||
portab_check_types( );
|
||||
|
||||
/* Module initialisieren */
|
||||
Log_Init( );
|
||||
Conf_Init( );
|
||||
Conn_Init( );
|
||||
|
||||
/* Signal-Handler initialisieren */
|
||||
Initialize_Signal_Handler( );
|
||||
|
||||
if( ! Conn_New_Listener( 6668 )) exit( 1 );
|
||||
|
||||
|
||||
/* Hauptschleife */
|
||||
while( ! do_quit_now )
|
||||
{
|
||||
Conn_Handler( );
|
||||
}
|
||||
|
||||
/* Alles abmelden */
|
||||
Conn_Exit( );
|
||||
Conf_Exit( );
|
||||
Log_Exit( );
|
||||
|
||||
return 0;
|
||||
} /* main */
|
||||
|
||||
|
||||
LOCAL VOID Initialize_Signal_Handler( VOID )
|
||||
{
|
||||
/* Signal-Handler initialisieren: Strukturen anlegen und einhaengen :-) */
|
||||
|
||||
struct sigaction saction;
|
||||
|
||||
/* Signal-Struktur initialisieren */
|
||||
memset( &saction, 0, sizeof( saction ));
|
||||
saction.sa_handler = Signal_Handler;
|
||||
|
||||
|
@ -90,77 +109,26 @@ GLOBAL INT main( INT argc, CONST CHAR *argv[] )
|
|||
sigaction( SIGTERM, &saction, NULL);
|
||||
sigaction( SIGUSR1, &saction, NULL);
|
||||
sigaction( SIGUSR2, &saction, NULL);
|
||||
|
||||
/* Server-"Listen"-Socket initialisieren */
|
||||
memset( &my_addr, 0, sizeof( my_addr ));
|
||||
my_addr.sin_family = AF_INET;
|
||||
my_addr.sin_port = htons( 6668 );
|
||||
my_addr.sin_addr.s_addr = htonl( INADDR_ANY );
|
||||
|
||||
/* Socket erzeugen, ... */
|
||||
my_sock = socket( AF_INET, SOCK_STREAM, 0);
|
||||
if( socket < 0 )
|
||||
{
|
||||
Log( LOG_FATAL, "Can't create socket: %s", strerror( errno ));
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
/* ... an Port binden ... */
|
||||
if( bind( my_sock, (struct sockaddr *)&my_addr, (socklen_t)sizeof( my_addr )) < 0 )
|
||||
{
|
||||
Log( LOG_FATAL, "Can't bind socket: %s", strerror( errno ));
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
/* ... und in "listen mode" gehen :-) */
|
||||
if( listen( my_sock, 10 ) < 0 )
|
||||
{
|
||||
Log( LOG_FATAL, "Can't listen on soecket: %s", strerror( errno ));
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
/* Hauptschleife */
|
||||
while( ! do_quit_now )
|
||||
{
|
||||
/* auf Verbindung warten */
|
||||
a_sock_len = sizeof( a_addr );
|
||||
memset( &a_addr, 0, a_sock_len );
|
||||
a_sock = accept( my_sock, (struct sockaddr *)&a_addr, &a_sock_len );
|
||||
if( a_sock < 0 )
|
||||
{
|
||||
if( errno == EINTR ) continue;
|
||||
|
||||
Log( LOG_FATAL, "Can't accept connection: %s", strerror( errno ));
|
||||
exit( 1 );
|
||||
}
|
||||
Log( LOG_INFO, "Accepted connection from %s:%d.", inet_ntoa( a_addr.sin_addr ), ntohs( a_addr.sin_port));
|
||||
fd = fdopen( a_sock, "w" );
|
||||
|
||||
fputs( "hello world!\n", fd ); fflush( fd );
|
||||
|
||||
fclose( fd );
|
||||
close( a_sock );
|
||||
}
|
||||
|
||||
/* Aufraeumen (Sockets etc.!?) */
|
||||
close( my_sock );
|
||||
|
||||
Log_Exit( );
|
||||
return 0;
|
||||
} /* main */
|
||||
} /* Initialize_Signal_Handler */
|
||||
|
||||
|
||||
LOCAL VOID Signal_Handler( INT Signal )
|
||||
{
|
||||
/* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
|
||||
* fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
|
||||
* Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
|
||||
|
||||
switch( Signal )
|
||||
{
|
||||
case SIGTERM:
|
||||
case SIGINT:
|
||||
case SIGQUIT:
|
||||
/* wir soll(t)en uns wohl beenden ... */
|
||||
Log( LOG_WARN, "Got signal %d, terminating now ...", Signal );
|
||||
do_quit_now = TRUE;
|
||||
break;
|
||||
default:
|
||||
/* unbekanntes bzw. unbehandeltes Signal */
|
||||
Log( LOG_WARN, "Got signal %d! Ignored.", Signal );
|
||||
}
|
||||
} /* Signal_Handler */
|
||||
|
|
Loading…
Reference in New Issue