]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- ein paar mehr Kommentare; Variablennamen verstaendlicher gemacht.
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001 by Alexander Barton (alex@barton.de)
4  *
5  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: ngircd.c,v 1.3 2001/12/12 01:40:39 alex Exp $
13  *
14  * ngircd.c: Hier beginnt alles ;-)
15  *
16  * $Log: ngircd.c,v $
17  * Revision 1.3  2001/12/12 01:40:39  alex
18  * - ein paar mehr Kommentare; Variablennamen verstaendlicher gemacht.
19  * - fehlenden Header <arpa/inet.h> ergaenz.
20  * - SIGINT und SIGQUIT werden nun ebenfalls behandelt.
21  *
22  * Revision 1.2  2001/12/11 22:04:21  alex
23  * - Test auf stdint.h (HAVE_STDINT_H) hinzugefuegt.
24  *
25  * Revision 1.1.1.1  2001/12/11 21:53:04  alex
26  * Imported sources to CVS.
27  *
28  */
29
30
31 #define PORTAB_CHECK_TYPES              /* Prueffunktion einbinden, s.u. */
32
33 #ifndef socklen_t
34 #define socklen_t int                   /* u.a. fuer Mac OS X */
35 #endif
36
37 #include <portab.h>
38 #include "global.h"
39
40 #include <imp.h>
41
42 #ifdef HAVE_STDINT_H
43 #include <stdint.h>                     /* u.a. fuer Mac OS X */
44 #endif
45
46 #include <assert.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <sys/types.h> 
53 #include <sys/socket.h> 
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56
57       
58 #include "log.h"
59
60 #include <exp.h>
61 #include "ngircd.h"
62
63
64 BOOLEAN do_quit_now = FALSE;            /* TRUE: Hauptschleife beenden */
65
66
67 LOCAL VOID Signal_Handler( INT Signal );
68
69
70 GLOBAL INT main( INT argc, CONST CHAR *argv[] )
71 {
72         FILE *fd;
73         struct sigaction saction;
74         struct sockaddr_in my_addr, a_addr;
75         int my_sock, a_sock;
76         int a_sock_len;
77
78         portab_check_types( );
79
80         Log_Init( );
81         
82         /* Signal-Handler initialisieren */
83         memset( &saction, 0, sizeof( saction ));
84         saction.sa_handler = Signal_Handler;
85
86         /* Signal-Handler einhaengen */
87         sigaction( SIGALRM, &saction, NULL );
88         sigaction( SIGHUP, &saction, NULL);
89         sigaction( SIGINT, &saction, NULL );
90         sigaction( SIGQUIT, &saction, NULL );
91         sigaction( SIGTERM, &saction, NULL);
92         sigaction( SIGUSR1, &saction, NULL);
93         sigaction( SIGUSR2, &saction, NULL);
94         
95         /* Server-"Listen"-Socket initialisieren */
96         memset( &my_addr, 0, sizeof( my_addr ));
97         my_addr.sin_family = AF_INET;
98         my_addr.sin_port = htons( 6668 );
99         my_addr.sin_addr.s_addr = htonl( INADDR_ANY );
100
101         /* Socket erzeugen, ... */
102         my_sock = socket( AF_INET, SOCK_STREAM, 0);
103         if( socket < 0 )
104         {
105                 Log( LOG_FATAL, "Can't create socket: %s", strerror( errno ));
106                 exit( 1 );
107         }
108         
109         /* ... an Port binden ... */
110         if( bind( my_sock, (struct sockaddr *)&my_addr, (socklen_t)sizeof( my_addr )) < 0 )
111         {
112                 Log( LOG_FATAL, "Can't bind socket: %s", strerror( errno ));
113                 exit( 1 );
114         }
115
116         /* ... und in "listen mode" gehen :-) */
117         if( listen( my_sock, 10 ) < 0 )
118         {
119                 Log( LOG_FATAL, "Can't listen on soecket: %s", strerror( errno ));
120                 exit( 1 );
121         }
122         
123         /* Hauptschleife */
124         while( ! do_quit_now )
125         {
126                 /* auf Verbindung warten */
127                 a_sock_len = sizeof( a_addr );
128                 memset( &a_addr, 0, a_sock_len );
129                 a_sock = accept( my_sock, (struct sockaddr *)&a_addr, &a_sock_len );
130                 if( a_sock < 0 )
131                 {
132                         if( errno == EINTR ) continue;
133                         
134                         Log( LOG_FATAL, "Can't accept connection: %s", strerror( errno ));
135                         exit( 1 );
136                 }
137                 Log( LOG_INFO, "Accepted connection from %s:%d.", inet_ntoa( a_addr.sin_addr ), ntohs( a_addr.sin_port));
138                 fd = fdopen( a_sock, "w" );
139
140                 fputs( "hello world!\n", fd ); fflush( fd );
141                 
142                 fclose( fd );
143                 close( a_sock );
144         }
145         
146         /* Aufraeumen (Sockets etc.!?) */
147         close( my_sock );
148
149         Log_Exit( );
150         return 0;
151 } /* main */
152
153
154 LOCAL VOID Signal_Handler( INT Signal )
155 {
156         switch( Signal )
157         {
158                 case SIGTERM:
159                 case SIGINT:
160                 case SIGQUIT:
161                         Log( LOG_WARN, "Got signal %d, terminating now ...", Signal );
162                         do_quit_now = TRUE;
163                         break;
164                 default:
165                         Log( LOG_WARN, "Got signal %d! Ignored.", Signal );
166         }
167 } /* Signal_Handler */
168
169
170 /* -eof- */