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