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