]> arthur.barton.de Git - ngircd-alex.git/commitdiff
- Unterstuetzung fuer die Konfigurationsdatei eingebaut.
authorAlexander Barton <alex@barton.de>
Sun, 30 Dec 2001 19:26:11 +0000 (19:26 +0000)
committerAlexander Barton <alex@barton.de>
Sun, 30 Dec 2001 19:26:11 +0000 (19:26 +0000)
src/ngircd/client.c
src/ngircd/conf.c
src/ngircd/conf.h
src/ngircd/conn.c
src/ngircd/irc.c
src/ngircd/ngircd.c

index 913370c806f458503a52302b3f24e21c00942546..5b8e6547ce75f06570f8f606189c994a6e40cd66 100644 (file)
@@ -9,7 +9,7 @@
  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
  * der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
  *
- * $Id: client.c,v 1.12 2001/12/29 20:18:18 alex Exp $
+ * $Id: client.c,v 1.13 2001/12/30 19:26:11 alex Exp $
  *
  * client.c: Management aller Clients
  *
@@ -21,6 +21,9 @@
  * Server gewesen, so existiert eine entsprechende CONNECTION-Struktur.
  *
  * $Log: client.c,v $
+ * Revision 1.13  2001/12/30 19:26:11  alex
+ * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
+ *
  * Revision 1.12  2001/12/29 20:18:18  alex
  * - neue Funktion Client_SetHostname().
  *
@@ -75,6 +78,7 @@
 
 #include <imp.h>
 #include "channel.h"
+#include "conf.h"
 #include "conn.h"
 #include "irc.h"
 #include "log.h"
@@ -98,6 +102,7 @@ GLOBAL VOID Client_Init( VOID )
        if( ! This_Server )
        {
                Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
+               Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
                exit( 1 );
        }
 
@@ -111,7 +116,7 @@ GLOBAL VOID Client_Init( VOID )
        h = gethostbyname( This_Server->host );
        if( h ) strcpy( This_Server->host, h->h_name );
 
-       strcpy( This_Server->nick, This_Server->host );
+       strcpy( This_Server->nick, Conf_ServerName );
 
        My_Clients = This_Server;
 } /* Client_Init */
@@ -264,7 +269,7 @@ GLOBAL CHAR *Client_GetID( CLIENT *Client )
 
        assert( Client != NULL );
        
-       if( Client->type == CLIENT_SERVER ) return Client->host;
+       if( Client->type == CLIENT_SERVER ) return Client->nick;
 
        sprintf( GetID_Buffer, "%s!%s@%s", Client->nick, Client->user, Client->host );
        return GetID_Buffer;
index 8a2085a75f85831f3073e08dd05d0e0e2d43d880..fec8903a1fc7408b52d0dd45478ff59ed9120efd 100644 (file)
@@ -9,11 +9,14 @@
  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
  * der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
  *
- * $Id: conf.c,v 1.4 2001/12/26 22:48:53 alex Exp $
+ * $Id: conf.c,v 1.5 2001/12/30 19:26:11 alex Exp $
  *
  * conf.h: Konfiguration des ngircd
  *
  * $Log: conf.c,v $
+ * Revision 1.5  2001/12/30 19:26:11  alex
+ * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
+ *
  * Revision 1.4  2001/12/26 22:48:53  alex
  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
  *
 
 #include <imp.h>
 #include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "client.h"
+#include "log.h"
+#include "tool.h"
 
 #include <exp.h>
 #include "conf.h"
 
 
+#define MAX_LINE_LEN 246               /* max. Laenge einer Konfigurationszeile */
+
+
 LOCAL VOID Read_Config( VOID );
+LOCAL VOID Validate_Config( VOID );
 
 
 GLOBAL VOID Conf_Init( VOID )
 {
        /* Konfigurationsvariablen initialisieren: zunaechst Default-
         * Werte setzen, dann Konfigurationsdtaei einlesen. */
-       
+
        strcpy( Conf_File, "/usr/local/etc/ngircd.conf" );
+
+       strcpy( Conf_ServerName, "" );
+
+       strcpy( Conf_MotdFile, "/usr/local/etc/ngircd.motd" );
+
+       Conf_ListenPorts_Count = 0;
        
        Conf_PingTimeout = 120;
        Conf_PongTimeout = 10;
 
-       strcpy( Conf_MotdFile, "/usr/local/etc/ngircd.motd" );
-
-       /* Konfigurationsdatei einlesen */
+       /* Konfigurationsdatei einlesen und validieren */
        Read_Config( );
+       Validate_Config( );
 } /* Config_Init */
 
 
@@ -67,9 +86,108 @@ GLOBAL VOID Conf_Exit( VOID )
 LOCAL VOID Read_Config( VOID )
 {
        /* Konfigurationsdatei einlesen. */
+
+       CHAR str[MAX_LINE_LEN], *var, *arg, *ptr;
+       BOOLEAN ok;
+       INT32 port;
+       INT line;
+       FILE *fd;
+
+       fd = fopen( Conf_File, "r" );
+       if( ! fd )
+       {
+               /* Keine Konfigurationsdatei gefunden */
+               Log( LOG_ALERT, "Can't read configuration \"%s\": %s", Conf_File, strerror( errno ));
+               Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
+               exit( 1 );
+       }
+
+       line = 0;
+       while( TRUE )
+       {
+               if( ! fgets( str, MAX_LINE_LEN, fd )) break;
+               ngt_TrimStr( str );
+               line++;
+
+               /* Kommentarzeilen und leere Zeilen ueberspringen */
+               if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
+
+               ok = FALSE;
+
+               ptr = strchr( str, '=' );
+               if( ! ptr )
+               {
+                       Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
+                       continue;
+               }
+               *ptr = '\0';
+
+               var = str; ngt_TrimStr( var );
+               arg = ptr + 1; ngt_TrimStr( arg );
+               
+               if( strcasecmp( str, "ServerName" ) == 0 )
+               {
+                       /* Der Server-Name */
+                       strncpy( Conf_ServerName, arg, CLIENT_ID_LEN );
+                       Conf_ServerName[CLIENT_ID_LEN] = '\0';
+                       ok = TRUE;
+               }
+               else if( strcasecmp( str, "ListenPorts" ) == 0 )
+               {
+                       /* Ports, durch "," getrennt, auf denen der Server
+                        * Verbindungen annehmen soll */
+                       ptr = strtok( arg, "," );
+                       while( ptr )
+                       {
+                               ngt_TrimStr( ptr );
+                               port = atol( ptr );
+                               if( Conf_ListenPorts_Count + 1 > LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
+                               if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = port;
+                               else Log( LOG_ERR, "Illegal port number: %ld. Ignored.", port );
+                               ptr = strtok( NULL, "," );
+                       }
+                       ok = TRUE;
+               }
+               else if( strcasecmp( str, "MotdFile" ) == 0 )
+               {
+                       /* Datei mit der "message of the day" (MOTD) */
+                       strncpy( Conf_MotdFile, arg, FNAME_LEN );
+                       Conf_MotdFile[FNAME_LEN] = '\0';
+                       ok = TRUE;
+               }
+               else if( strcasecmp( str, "PingTimeout" ) == 0 )
+               {
+                       /* PING-Timeout */
+                       Conf_PingTimeout = atoi( arg );
+                       if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
+                       ok = TRUE;
+               }
+               else if( strcasecmp( str, "PongTimeout" ) == 0 )
+               {
+                       /* PONG-Timeout */
+                       Conf_PongTimeout = atoi( arg );
+                       if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
+                       ok = TRUE;
+               }
+               
+               if( ! ok ) Log( LOG_ERR, "%s, line %d: Unknown variable \"%s\"!", Conf_File, line, var );
+       }
        
-       /* ... */
+       fclose( fd );
 } /* Read_Config */
 
 
+LOCAL VOID Validate_Config( VOID )
+{
+       /* Konfiguration ueberpruefen */
+       
+       if( ! Conf_ServerName[0] )
+       {
+               /* Kein Servername konfiguriert */
+               Log( LOG_ALERT, "No server name configured (use \"ServerName\")!", Conf_File, strerror( errno ));
+               Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
+               exit( 1 );
+       }
+} /* Validate_Config */
+
 /* -eof- */
index 07ed1eda5f229889493610ed13dea9bf7e1f0f96..ceea6a52b32e7e53c6cb58118100ea970e4dd00f 100644 (file)
@@ -9,11 +9,14 @@
  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
  * der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
  *
- * $Id: conf.h,v 1.4 2001/12/26 22:48:53 alex Exp $
+ * $Id: conf.h,v 1.5 2001/12/30 19:26:11 alex Exp $
  *
  * conf.h: Konfiguration des ngircd (Header)
  *
  * $Log: conf.h,v $
+ * Revision 1.5  2001/12/30 19:26:11  alex
+ * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
+ *
  * Revision 1.4  2001/12/26 22:48:53  alex
  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
  *
 
 
 #define FNAME_LEN 256
+#define LISTEN_PORTS 16
+
+
+GLOBAL CHAR Conf_File[FNAME_LEN];              /* Konfigurationsdatei */
 
+GLOBAL CHAR Conf_ServerName[CLIENT_ID_LEN];    /* Name ("Nick") des Servers */
 
-GLOBAL CHAR Conf_File[FNAME_LEN];      /* Konfigurationsdatei */
+GLOBAL CHAR Conf_MotdFile[FNAME_LEN];          /* Datei mit MOTD-Text */
 
-GLOBAL INT Conf_PingTimeout;           /* Ping Timeout */
-GLOBAL INT Conf_PongTimeout;           /* Pong Timeout */
+GLOBAL INT Conf_ListenPorts[LISTEN_PORTS];     /* Ports, auf denen der Server Verbindungen */
+GLOBAL INT Conf_ListenPorts_Count;             /* entgegen nimmt sowie deren Anzahl */
 
-GLOBAL CHAR Conf_MotdFile[FNAME_LEN];  /* Datei mit MOTD-Text */
+GLOBAL INT Conf_PingTimeout;                   /* Ping Timeout */
+GLOBAL INT Conf_PongTimeout;                   /* Pong Timeout */
 
 
 GLOBAL VOID Conf_Init( VOID );
index d2479db52a8310eca4961e89a938ae61f5358f94..1a16689830fa3e8ec93e7722a6b40bc58d88e71b 100644 (file)
@@ -9,11 +9,14 @@
  * 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.21 2001/12/29 22:33:36 alex Exp $
+ * $Id: conn.c,v 1.22 2001/12/30 19:26:11 alex Exp $
  *
  * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
  *
  * $Log: conn.c,v $
+ * Revision 1.22  2001/12/30 19:26:11  alex
+ * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
+ *
  * Revision 1.21  2001/12/29 22:33:36  alex
  * - bessere Dokumentation des Modules bzw. der Funktionen.
  *
@@ -288,7 +291,7 @@ GLOBAL BOOLEAN Conn_NewListener( CONST INT Port )
 
        if( sock > My_Max_Fd ) My_Max_Fd = sock;
 
-       Log( LOG_INFO, "Now listening on port %d, socket %d.", Port, sock );
+       Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
 
        return TRUE;
 } /* Conn_NewListener */
index e9061890402eb1dc9fd0fe10789b1d8d2976f64e..c476d34fc25fa2da15830cbe35b914afe6e067b3 100644 (file)
@@ -9,11 +9,14 @@
  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
  * der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
  *
- * $Id: irc.c,v 1.14 2001/12/30 11:42:00 alex Exp $
+ * $Id: irc.c,v 1.15 2001/12/30 19:26:11 alex Exp $
  *
  * irc.c: IRC-Befehle
  *
  * $Log: irc.c,v $
+ * Revision 1.15  2001/12/30 19:26:11  alex
+ * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
+ *
  * Revision 1.14  2001/12/30 11:42:00  alex
  * - der Server meldet nun eine ordentliche "Start-Zeit".
  *
@@ -480,9 +483,9 @@ LOCAL BOOLEAN Hello_User( CLIENT *Client )
        Log( LOG_NOTICE, "User \"%s!%s@%s\" (%s) registered (connection %d).", Client->nick, Client->user, Client->host, Client->name, Client->conn_id );
 
        IRC_WriteStrClient( Client, This_Server, RPL_WELCOME_MSG, Client->nick, Client_GetID( Client ));
-       IRC_WriteStrClient( Client, This_Server, RPL_YOURHOST_MSG, Client->nick, This_Server->host );
+       IRC_WriteStrClient( Client, This_Server, RPL_YOURHOST_MSG, Client->nick, This_Server->nick );
        IRC_WriteStrClient( Client, This_Server, RPL_CREATED_MSG, Client->nick, NGIRCd_StartStr );
-       IRC_WriteStrClient( Client, This_Server, RPL_MYINFO_MSG, Client->nick, This_Server->host );
+       IRC_WriteStrClient( Client, This_Server, RPL_MYINFO_MSG, Client->nick, This_Server->nick );
 
        Client->type = CLIENT_USER;
 
@@ -506,7 +509,7 @@ LOCAL BOOLEAN Show_MOTD( CLIENT *Client )
                return IRC_WriteStrClient( Client, This_Server, ERR_NOMOTD_MSG, Client->nick );
        }
        
-       IRC_WriteStrClient( Client, This_Server, RPL_MOTDSTART_MSG, Client->nick, This_Server->host );
+       IRC_WriteStrClient( Client, This_Server, RPL_MOTDSTART_MSG, Client->nick, This_Server->nick );
        while( TRUE )
        {
                if( ! fgets( line, 126, fd )) break;
index e630ea7078a96269fbb3f9d6a1ad32057932501b..4e18f6ac3fa9c94f3dbcef966802030b140331fc 100644 (file)
@@ -9,11 +9,14 @@
  * 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.13 2001/12/30 11:42:00 alex Exp $
+ * $Id: ngircd.c,v 1.14 2001/12/30 19:26:12 alex Exp $
  *
  * ngircd.c: Hier beginnt alles ;-)
  *
  * $Log: ngircd.c,v $
+ * Revision 1.14  2001/12/30 19:26:12  alex
+ * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
+ *
  * Revision 1.13  2001/12/30 11:42:00  alex
  * - der Server meldet nun eine ordentliche "Start-Zeit".
  *
@@ -88,6 +91,8 @@
 LOCAL VOID Initialize_Signal_Handler( VOID );
 LOCAL VOID Signal_Handler( INT Signal );
 
+LOCAL VOID Initialize_Listen_Ports( VOID );
+
 
 GLOBAL INT main( INT argc, CONST CHAR *argv[] )
 {
@@ -110,9 +115,9 @@ GLOBAL INT main( INT argc, CONST CHAR *argv[] )
 
        /* Signal-Handler initialisieren */
        Initialize_Signal_Handler( );
-       
-       if( ! Conn_NewListener( 6668 )) exit( 1 );
-       if( ! Conn_NewListener( 6669 )) Log( LOG_WARNING, "Can't create second listening socket!" );
+
+       /* Listen-Ports initialisieren */
+       Initialize_Listen_Ports( );
        
        /* Hauptschleife */
        while( ! NGIRCd_Quit )
@@ -177,4 +182,26 @@ LOCAL VOID Signal_Handler( INT Signal )
 } /* Signal_Handler */
 
 
+LOCAL VOID Initialize_Listen_Ports( 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 %d!", Conf_ListenPorts[i] );
+       }
+
+       if( created < 1 )
+       {
+               Log( LOG_ALERT, "Server isn't listening on a single port!" );
+               Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
+               exit( 1 );
+       }
+} /* Initialize_Listen_Ports */
+
 /* -eof- */