]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- Test auf stdint.h (HAVE_STDINT_H) hinzugefuegt.
[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.2 2001/12/11 22:04:21 alex Exp $
13  *
14  * ngircd.c: Hier beginnt alles ;-)
15  *
16  * $Log: ngircd.c,v $
17  * Revision 1.2  2001/12/11 22:04:21  alex
18  * - Test auf stdint.h (HAVE_STDINT_H) hinzugefuegt.
19  *
20  * Revision 1.1.1.1  2001/12/11 21:53:04  alex
21  * Imported sources to CVS.
22  *
23  */
24
25
26 #define PORTAB_CHECK_TYPES
27
28 #ifndef socklen_t
29 #define socklen_t int
30 #endif
31
32 #include <portab.h>
33 #include "global.h"
34
35 #include <imp.h>
36
37 #ifdef HAVE_STDINT_H
38 #include <stdint.h>
39 #endif
40
41 #include <assert.h>
42 #include <errno.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <sys/types.h> 
47 #include <sys/socket.h> 
48 #include <unistd.h>
49 #include <netinet/in.h>
50       
51 #include "log.h"
52
53 #include <exp.h>
54 #include "ngircd.h"
55
56
57 BOOLEAN do_quit_now = FALSE;
58
59
60 LOCAL VOID Signal_Handler( INT Signal );
61
62
63 GLOBAL INT main( INT argc, CONST CHAR *argv[] )
64 {
65         FILE *fd;
66         struct sigaction saction;
67         struct sockaddr_in my_sock, a_sock;
68         int my_sock_hndl;
69         int a_sock_len, a_hndl;
70
71         portab_check_types( );
72
73         Log_Init( );
74         
75         /* Signal-Handler initialisieren */
76         memset( &saction, 0, sizeof( saction ));
77         saction.sa_handler = Signal_Handler;
78
79         /* Signal-Handler einhaengen */
80         sigaction( SIGHUP, &saction, NULL);
81         sigaction( SIGTERM, &saction, NULL);
82         sigaction( SIGUSR1, &saction, NULL);
83         sigaction( SIGUSR2, &saction, NULL);
84         
85         /* Server-"Listen"-Socket initialisieren */
86         memset( &my_sock, 0, sizeof( my_sock ));
87         my_sock.sin_family = AF_INET;
88         my_sock.sin_port = htons( 6668 );
89         my_sock.sin_addr.s_addr = htonl( INADDR_ANY );
90
91         /* Socket erzeugen, ... */
92         my_sock_hndl = socket( AF_INET, SOCK_STREAM, 0);
93         if( socket < 0 )
94         {
95                 Log( LOG_FATAL, "Can't create socket: %s", strerror( errno ));
96                 exit( 1 );
97         }
98         
99         /* ... an Port binden ... */
100         if( bind( my_sock_hndl, (struct sockaddr *)&my_sock, (socklen_t)sizeof( my_sock )) < 0 )
101         {
102                 Log( LOG_FATAL, "Can't bind socket: %s", strerror( errno ));
103                 exit( 1 );
104         }
105
106         /* ... und in "listen mode" gehen :-) */
107         if( listen( my_sock_hndl, 4 ) < 0 )
108         {
109                 Log( LOG_FATAL, "Can't listen on soecket: %s", strerror( errno ));
110                 exit( 1 );
111         }
112         
113         /* Hauptschleife */
114         while( ! do_quit_now )
115         {
116                 /* auf Verbindung warten */
117                 a_sock_len = sizeof( a_sock );
118                 memset( &a_sock, 0, a_sock_len );
119                 a_hndl = accept( my_sock_hndl, (struct sockaddr *)&a_sock, &a_sock_len );
120                 if( a_hndl < 0 )
121                 {
122                         if( errno == EINTR ) continue;
123                         
124                         Log( LOG_FATAL, "Can't accept connection: %s", strerror( errno ));
125                         exit( 1 );
126                 }
127                 Log( LOG_INFO, "Accepted connection from %s:%d (handle %d).", inet_ntoa( a_sock.sin_addr ), ntohs( a_sock.sin_port), a_hndl );
128                 fd = fdopen( a_hndl, "w" );
129
130                 fputs( "hello world!\n", fd ); fflush( fd );
131                 
132                 fclose( fd );
133                 close( a_hndl );
134         }
135         
136         /* Aufraeumen (Sockets etc.!?) */
137
138         Log_Exit( );
139         return 0;
140 } /* main */
141
142
143 LOCAL VOID Signal_Handler( INT Signal )
144 {
145         switch( Signal )
146         {
147                 case SIGTERM:
148                         Log( LOG_WARN, "Got SIGTERM, terminating now ..." );
149                         do_quit_now = TRUE;
150                         break;
151                 default:
152                         Log( LOG_WARN, "Got signal %d! I'll ignore it.", Signal );
153         }
154 } /* Signal_Handler */
155
156
157 /* -eof- */