Blame


1 5fefe1a3 2001-12-12 alex /*
2 5fefe1a3 2001-12-12 alex * ngIRCd -- The Next Generation IRC Daemon
3 03d971d9 2002-01-02 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 5fefe1a3 2001-12-12 alex *
5 5fefe1a3 2001-12-12 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 5fefe1a3 2001-12-12 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 5fefe1a3 2001-12-12 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 5fefe1a3 2001-12-12 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 5fefe1a3 2001-12-12 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 804b1ec4 2001-12-31 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11 5fefe1a3 2001-12-12 alex *
12 e39925af 2002-03-26 alex * $Id: conn.c,v 1.57 2002/03/26 23:47:45 alex Exp $
13 5fefe1a3 2001-12-12 alex *
14 5fefe1a3 2001-12-12 alex * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
15 5fefe1a3 2001-12-12 alex */
16 5fefe1a3 2001-12-12 alex
17 5fefe1a3 2001-12-12 alex
18 ca33cbda 2002-03-12 alex #include "portab.h"
19 5fefe1a3 2001-12-12 alex
20 ca33cbda 2002-03-12 alex #include "imp.h"
21 5fefe1a3 2001-12-12 alex #include <assert.h>
22 d5c97f81 2001-12-23 alex #include <stdarg.h>
23 5fefe1a3 2001-12-12 alex #include <stdio.h>
24 b20fa7c6 2002-01-01 alex #include <stdlib.h>
25 5fefe1a3 2001-12-12 alex #include <unistd.h>
26 5fefe1a3 2001-12-12 alex #include <errno.h>
27 5fefe1a3 2001-12-12 alex #include <fcntl.h>
28 5fefe1a3 2001-12-12 alex #include <string.h>
29 9ab186c4 2001-12-25 alex #include <sys/socket.h>
30 5fefe1a3 2001-12-12 alex #include <sys/time.h>
31 9ab186c4 2001-12-25 alex #include <sys/types.h>
32 ba331a2f 2001-12-26 alex #include <time.h>
33 5fefe1a3 2001-12-12 alex #include <netinet/in.h>
34 4a111033 2001-12-29 alex #include <netdb.h>
35 5fefe1a3 2001-12-12 alex
36 bcc0cdc3 2002-01-05 alex #ifdef HAVE_ARPA_INET_H
37 bcc0cdc3 2002-01-05 alex #include <arpa/inet.h>
38 bcc0cdc3 2002-01-05 alex #else
39 bcc0cdc3 2002-01-05 alex #define PF_INET AF_INET
40 bcc0cdc3 2002-01-05 alex #endif
41 bcc0cdc3 2002-01-05 alex
42 5fefe1a3 2001-12-12 alex #ifdef HAVE_STDINT_H
43 5fefe1a3 2001-12-12 alex #include <stdint.h> /* u.a. fuer Mac OS X */
44 5fefe1a3 2001-12-12 alex #endif
45 5fefe1a3 2001-12-12 alex
46 1c8eb478 2001-12-12 alex #include "ngircd.h"
47 d5c97f81 2001-12-23 alex #include "client.h"
48 65bdfdf2 2001-12-26 alex #include "conf.h"
49 5fefe1a3 2001-12-12 alex #include "log.h"
50 c4199b04 2001-12-21 alex #include "parse.h"
51 1c8eb478 2001-12-12 alex #include "tool.h"
52 5fefe1a3 2001-12-12 alex
53 ca33cbda 2002-03-12 alex #include "exp.h"
54 5fefe1a3 2001-12-12 alex #include "conn.h"
55 cf050519 2001-12-14 alex
56 63758dd7 2001-12-15 alex
57 34d54344 2002-03-12 alex #define SERVER_WAIT (NONE - 1)
58 4a111033 2001-12-29 alex
59 4a111033 2001-12-29 alex
60 1c8eb478 2001-12-12 alex typedef struct _Connection
61 1c8eb478 2001-12-12 alex {
62 1c8eb478 2001-12-12 alex INT sock; /* Socket Handle */
63 1c8eb478 2001-12-12 alex struct sockaddr_in addr; /* Adresse des Client */
64 4a111033 2001-12-29 alex RES_STAT *res_stat; /* "Resolver-Status", s.o. */
65 4a111033 2001-12-29 alex CHAR host[HOST_LEN]; /* Hostname */
66 804b1ec4 2001-12-31 alex CHAR rbuf[READBUFFER_LEN]; /* Lesepuffer */
67 63758dd7 2001-12-15 alex INT rdatalen; /* Laenge der Daten im Lesepuffer */
68 804b1ec4 2001-12-31 alex CHAR wbuf[WRITEBUFFER_LEN]; /* Schreibpuffer */
69 63758dd7 2001-12-15 alex INT wdatalen; /* Laenge der Daten im Schreibpuffer */
70 03d971d9 2002-01-02 alex INT our_server; /* wenn von uns zu connectender Server: ID */
71 65bdfdf2 2001-12-26 alex time_t lastdata; /* Letzte Aktivitaet */
72 65bdfdf2 2001-12-26 alex time_t lastping; /* Letzter PING */
73 804b1ec4 2001-12-31 alex time_t lastprivmsg; /* Letzte PRIVMSG */
74 1c8eb478 2001-12-12 alex } CONNECTION;
75 1c8eb478 2001-12-12 alex
76 1c8eb478 2001-12-12 alex
77 63758dd7 2001-12-15 alex LOCAL VOID Handle_Read( INT sock );
78 63758dd7 2001-12-15 alex LOCAL BOOLEAN Handle_Write( CONN_ID Idx );
79 1c8eb478 2001-12-12 alex LOCAL VOID New_Connection( INT Sock );
80 cf050519 2001-12-14 alex LOCAL CONN_ID Socket2Index( INT Sock );
81 cf050519 2001-12-14 alex LOCAL VOID Read_Request( CONN_ID Idx );
82 63758dd7 2001-12-15 alex LOCAL BOOLEAN Try_Write( CONN_ID Idx );
83 9ab186c4 2001-12-25 alex LOCAL VOID Handle_Buffer( CONN_ID Idx );
84 65bdfdf2 2001-12-26 alex LOCAL VOID Check_Connections( VOID );
85 03d971d9 2002-01-02 alex LOCAL VOID Check_Servers( VOID );
86 4a111033 2001-12-29 alex LOCAL VOID Init_Conn_Struct( INT Idx );
87 6da91c34 2002-03-02 alex LOCAL BOOLEAN Init_Socket( INT Sock );
88 03d971d9 2002-01-02 alex LOCAL VOID New_Server( INT Server, CONN_ID Idx );
89 1c8eb478 2001-12-12 alex
90 03d971d9 2002-01-02 alex LOCAL RES_STAT *ResolveAddr( struct sockaddr_in *Addr );
91 03d971d9 2002-01-02 alex LOCAL RES_STAT *ResolveName( CHAR *Host );
92 03d971d9 2002-01-02 alex LOCAL VOID Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd );
93 03d971d9 2002-01-02 alex LOCAL VOID Do_ResolveName( CHAR *Host, INT w_fd );
94 4a111033 2001-12-29 alex LOCAL VOID Read_Resolver_Result( INT r_fd );
95 03d971d9 2002-01-02 alex LOCAL CHAR *Resolv_Error( INT H_Error );
96 1c8eb478 2001-12-12 alex
97 4a111033 2001-12-29 alex
98 4a111033 2001-12-29 alex LOCAL fd_set My_Listeners;
99 1c8eb478 2001-12-12 alex LOCAL fd_set My_Sockets;
100 4a111033 2001-12-29 alex LOCAL fd_set My_Resolvers;
101 6da91c34 2002-03-02 alex LOCAL fd_set My_Connects;
102 1c8eb478 2001-12-12 alex
103 1c8eb478 2001-12-12 alex LOCAL INT My_Max_Fd;
104 1c8eb478 2001-12-12 alex
105 1c8eb478 2001-12-12 alex LOCAL CONNECTION My_Connections[MAX_CONNECTIONS];
106 1c8eb478 2001-12-12 alex
107 1c8eb478 2001-12-12 alex
108 5fefe1a3 2001-12-12 alex GLOBAL VOID Conn_Init( VOID )
109 5fefe1a3 2001-12-12 alex {
110 bc140df8 2001-12-29 alex /* Modul initialisieren: statische Strukturen "ausnullen". */
111 bc140df8 2001-12-29 alex
112 cf050519 2001-12-14 alex CONN_ID i;
113 1c8eb478 2001-12-12 alex
114 1c8eb478 2001-12-12 alex /* zu Beginn haben wir keine Verbindungen */
115 4a111033 2001-12-29 alex FD_ZERO( &My_Listeners );
116 1c8eb478 2001-12-12 alex FD_ZERO( &My_Sockets );
117 4a111033 2001-12-29 alex FD_ZERO( &My_Resolvers );
118 6da91c34 2002-03-02 alex FD_ZERO( &My_Connects );
119 9ab186c4 2001-12-25 alex
120 1c8eb478 2001-12-12 alex My_Max_Fd = 0;
121 9ab186c4 2001-12-25 alex
122 1c8eb478 2001-12-12 alex /* Connection-Struktur initialisieren */
123 4a111033 2001-12-29 alex for( i = 0; i < MAX_CONNECTIONS; i++ ) Init_Conn_Struct( i );
124 5fefe1a3 2001-12-12 alex } /* Conn_Init */
125 5fefe1a3 2001-12-12 alex
126 5fefe1a3 2001-12-12 alex
127 5fefe1a3 2001-12-12 alex GLOBAL VOID Conn_Exit( VOID )
128 5fefe1a3 2001-12-12 alex {
129 bc140df8 2001-12-29 alex /* Modul abmelden: alle noch offenen Connections
130 bc140df8 2001-12-29 alex * schliessen und freigeben. */
131 bc140df8 2001-12-29 alex
132 cf050519 2001-12-14 alex CONN_ID idx;
133 cf050519 2001-12-14 alex INT i;
134 9ab186c4 2001-12-25 alex
135 1c8eb478 2001-12-12 alex /* Sockets schliessen */
136 79809118 2002-01-06 alex Log( LOG_DEBUG, "Shutting down all connections ..." );
137 1c8eb478 2001-12-12 alex for( i = 0; i < My_Max_Fd + 1; i++ )
138 1c8eb478 2001-12-12 alex {
139 1c8eb478 2001-12-12 alex if( FD_ISSET( i, &My_Sockets ))
140 1c8eb478 2001-12-12 alex {
141 1c8eb478 2001-12-12 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ )
142 1c8eb478 2001-12-12 alex {
143 1c8eb478 2001-12-12 alex if( My_Connections[idx].sock == i ) break;
144 1c8eb478 2001-12-12 alex }
145 6da91c34 2002-03-02 alex if( FD_ISSET( i, &My_Listeners ))
146 1c8eb478 2001-12-12 alex {
147 1c8eb478 2001-12-12 alex close( i );
148 79809118 2002-01-06 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
149 1c8eb478 2001-12-12 alex }
150 6da91c34 2002-03-02 alex else if( FD_ISSET( i, &My_Connects ))
151 6da91c34 2002-03-02 alex {
152 6da91c34 2002-03-02 alex close( i );
153 6da91c34 2002-03-02 alex Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
154 6da91c34 2002-03-02 alex }
155 6da91c34 2002-03-02 alex else if( idx < MAX_CONNECTIONS ) Conn_Close( idx, NULL, "Server going down", TRUE );
156 1c8eb478 2001-12-12 alex else
157 1c8eb478 2001-12-12 alex {
158 6da91c34 2002-03-02 alex Log( LOG_WARNING, "Closing unknown connection %d ...", i );
159 1c8eb478 2001-12-12 alex close( i );
160 1c8eb478 2001-12-12 alex }
161 1c8eb478 2001-12-12 alex }
162 1c8eb478 2001-12-12 alex }
163 5fefe1a3 2001-12-12 alex } /* Conn_Exit */
164 5fefe1a3 2001-12-12 alex
165 5fefe1a3 2001-12-12 alex
166 08cf5607 2001-12-26 alex GLOBAL BOOLEAN Conn_NewListener( CONST INT Port )
167 5fefe1a3 2001-12-12 alex {
168 bc140df8 2001-12-29 alex /* Neuen Listen-Socket erzeugen: der Server wartet dann auf
169 bc140df8 2001-12-29 alex * dem angegebenen Port auf Verbindungen. Kann der Listen-
170 bc140df8 2001-12-29 alex * Socket nicht erteugt werden, so wird NULL geliefert.*/
171 5fefe1a3 2001-12-12 alex
172 1c8eb478 2001-12-12 alex struct sockaddr_in addr;
173 6da91c34 2002-03-02 alex INT sock;
174 1c8eb478 2001-12-12 alex
175 5fefe1a3 2001-12-12 alex /* Server-"Listen"-Socket initialisieren */
176 1c8eb478 2001-12-12 alex memset( &addr, 0, sizeof( addr ));
177 1c8eb478 2001-12-12 alex addr.sin_family = AF_INET;
178 1c8eb478 2001-12-12 alex addr.sin_port = htons( Port );
179 1c8eb478 2001-12-12 alex addr.sin_addr.s_addr = htonl( INADDR_ANY );
180 5fefe1a3 2001-12-12 alex
181 5fefe1a3 2001-12-12 alex /* Socket erzeugen */
182 1c8eb478 2001-12-12 alex sock = socket( PF_INET, SOCK_STREAM, 0);
183 fb55c443 2001-12-13 alex if( sock < 0 )
184 5fefe1a3 2001-12-12 alex {
185 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
186 5fefe1a3 2001-12-12 alex return FALSE;
187 5fefe1a3 2001-12-12 alex }
188 5fefe1a3 2001-12-12 alex
189 6da91c34 2002-03-02 alex if( ! Init_Socket( sock )) return FALSE;
190 1c8eb478 2001-12-12 alex
191 5fefe1a3 2001-12-12 alex /* an Port binden */
192 1c8eb478 2001-12-12 alex if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
193 5fefe1a3 2001-12-12 alex {
194 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
195 1c8eb478 2001-12-12 alex close( sock );
196 5fefe1a3 2001-12-12 alex return FALSE;
197 5fefe1a3 2001-12-12 alex }
198 5fefe1a3 2001-12-12 alex
199 5fefe1a3 2001-12-12 alex /* in "listen mode" gehen :-) */
200 1c8eb478 2001-12-12 alex if( listen( sock, 10 ) != 0 )
201 5fefe1a3 2001-12-12 alex {
202 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
203 1c8eb478 2001-12-12 alex close( sock );
204 5fefe1a3 2001-12-12 alex return FALSE;
205 5fefe1a3 2001-12-12 alex }
206 1c8eb478 2001-12-12 alex
207 1c8eb478 2001-12-12 alex /* Neuen Listener in Strukturen einfuegen */
208 4a111033 2001-12-29 alex FD_SET( sock, &My_Listeners );
209 1c8eb478 2001-12-12 alex FD_SET( sock, &My_Sockets );
210 9ab186c4 2001-12-25 alex
211 1c8eb478 2001-12-12 alex if( sock > My_Max_Fd ) My_Max_Fd = sock;
212 1c8eb478 2001-12-12 alex
213 9856253d 2001-12-30 alex Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
214 1c8eb478 2001-12-12 alex
215 5fefe1a3 2001-12-12 alex return TRUE;
216 08cf5607 2001-12-26 alex } /* Conn_NewListener */
217 5fefe1a3 2001-12-12 alex
218 5fefe1a3 2001-12-12 alex
219 747fd2f0 2001-12-13 alex GLOBAL VOID Conn_Handler( INT Timeout )
220 5fefe1a3 2001-12-12 alex {
221 bc140df8 2001-12-29 alex /* Aktive Verbindungen ueberwachen. Mindestens alle "Timeout"
222 bc140df8 2001-12-29 alex * Sekunden wird die Funktion verlassen. Folgende Aktionen
223 bc140df8 2001-12-29 alex * werden durchgefuehrt:
224 bc140df8 2001-12-29 alex * - neue Verbindungen annehmen,
225 03d971d9 2002-01-02 alex * - Server-Verbindungen aufbauen,
226 bc140df8 2001-12-29 alex * - geschlossene Verbindungen loeschen,
227 bc140df8 2001-12-29 alex * - volle Schreibpuffer versuchen zu schreiben,
228 bc140df8 2001-12-29 alex * - volle Lesepuffer versuchen zu verarbeiten,
229 bc140df8 2001-12-29 alex * - Antworten von Resolver Sub-Prozessen annehmen.
230 bc140df8 2001-12-29 alex */
231 bc140df8 2001-12-29 alex
232 63758dd7 2001-12-15 alex fd_set read_sockets, write_sockets;
233 747fd2f0 2001-12-13 alex struct timeval tv;
234 9ab186c4 2001-12-25 alex time_t start;
235 1c8eb478 2001-12-12 alex INT i;
236 747fd2f0 2001-12-13 alex
237 9ab186c4 2001-12-25 alex start = time( NULL );
238 65bdfdf2 2001-12-26 alex while(( time( NULL ) - start < Timeout ) && ( ! NGIRCd_Quit ))
239 63758dd7 2001-12-15 alex {
240 03d971d9 2002-01-02 alex Check_Servers( );
241 03d971d9 2002-01-02 alex
242 65bdfdf2 2001-12-26 alex Check_Connections( );
243 65bdfdf2 2001-12-26 alex
244 65bdfdf2 2001-12-26 alex /* Timeout initialisieren */
245 7b8b5423 2002-03-02 alex tv.tv_sec = 0;
246 7b8b5423 2002-03-02 alex tv.tv_usec = 50000;
247 65bdfdf2 2001-12-26 alex
248 65bdfdf2 2001-12-26 alex /* noch volle Lese-Buffer suchen */
249 9ab186c4 2001-12-25 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
250 63758dd7 2001-12-15 alex {
251 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
252 9ab186c4 2001-12-25 alex {
253 65bdfdf2 2001-12-26 alex /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
254 65bdfdf2 2001-12-26 alex Handle_Buffer( i );
255 9ab186c4 2001-12-25 alex }
256 63758dd7 2001-12-15 alex }
257 590f2a3f 2002-03-11 alex
258 65bdfdf2 2001-12-26 alex /* noch volle Schreib-Puffer suchen */
259 65bdfdf2 2001-12-26 alex FD_ZERO( &write_sockets );
260 9ab186c4 2001-12-25 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
261 9ab186c4 2001-12-25 alex {
262 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
263 9ab186c4 2001-12-25 alex {
264 65bdfdf2 2001-12-26 alex /* Socket der Verbindung in Set aufnehmen */
265 65bdfdf2 2001-12-26 alex FD_SET( My_Connections[i].sock, &write_sockets );
266 9ab186c4 2001-12-25 alex }
267 6da91c34 2002-03-02 alex }
268 6da91c34 2002-03-02 alex /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
269 6da91c34 2002-03-02 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
270 6da91c34 2002-03-02 alex {
271 6da91c34 2002-03-02 alex if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects ))) FD_SET( My_Connections[i].sock, &write_sockets );
272 9ab186c4 2001-12-25 alex }
273 4a111033 2001-12-29 alex
274 4a111033 2001-12-29 alex /* von welchen Sockets koennte gelesen werden? */
275 9ab186c4 2001-12-25 alex read_sockets = My_Sockets;
276 4a111033 2001-12-29 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
277 4a111033 2001-12-29 alex {
278 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
279 4a111033 2001-12-29 alex {
280 4a111033 2001-12-29 alex /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
281 6da91c34 2002-03-02 alex FD_CLR( My_Connections[i].sock, &read_sockets );
282 6da91c34 2002-03-02 alex }
283 6da91c34 2002-03-02 alex if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
284 6da91c34 2002-03-02 alex {
285 6da91c34 2002-03-02 alex /* Hier laeuft noch ein asyncrones connect() */
286 4a111033 2001-12-29 alex FD_CLR( My_Connections[i].sock, &read_sockets );
287 4a111033 2001-12-29 alex }
288 4a111033 2001-12-29 alex }
289 4a111033 2001-12-29 alex for( i = 0; i < My_Max_Fd + 1; i++ )
290 4a111033 2001-12-29 alex {
291 4a111033 2001-12-29 alex /* Pipes von Resolver Sub-Prozessen aufnehmen */
292 4a111033 2001-12-29 alex if( FD_ISSET( i, &My_Resolvers ))
293 4a111033 2001-12-29 alex {
294 4a111033 2001-12-29 alex FD_SET( i, &read_sockets );
295 4a111033 2001-12-29 alex }
296 4a111033 2001-12-29 alex }
297 4a111033 2001-12-29 alex
298 4a111033 2001-12-29 alex /* Auf Aktivitaet warten */
299 9ab186c4 2001-12-25 alex if( select( My_Max_Fd + 1, &read_sockets, &write_sockets, NULL, &tv ) == -1 )
300 9ab186c4 2001-12-25 alex {
301 db58d347 2002-01-05 alex if( errno != EINTR )
302 db58d347 2002-01-05 alex {
303 79809118 2002-01-06 alex Log( LOG_EMERG, "select(): %s!", strerror( errno ));
304 db58d347 2002-01-05 alex Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
305 db58d347 2002-01-05 alex exit( 1 );
306 db58d347 2002-01-05 alex }
307 db58d347 2002-01-05 alex continue;
308 9ab186c4 2001-12-25 alex }
309 9ab186c4 2001-12-25 alex
310 9ab186c4 2001-12-25 alex /* Koennen Daten geschrieben werden? */
311 9ab186c4 2001-12-25 alex for( i = 0; i < My_Max_Fd + 1; i++ )
312 9ab186c4 2001-12-25 alex {
313 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &write_sockets )) Handle_Write( Socket2Index( i ));
314 9ab186c4 2001-12-25 alex }
315 9ab186c4 2001-12-25 alex
316 9ab186c4 2001-12-25 alex /* Daten zum Lesen vorhanden? */
317 9ab186c4 2001-12-25 alex for( i = 0; i < My_Max_Fd + 1; i++ )
318 9ab186c4 2001-12-25 alex {
319 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
320 9ab186c4 2001-12-25 alex }
321 63758dd7 2001-12-15 alex }
322 1c8eb478 2001-12-12 alex } /* Conn_Handler */
323 1c8eb478 2001-12-12 alex
324 1c8eb478 2001-12-12 alex
325 d5c97f81 2001-12-23 alex GLOBAL BOOLEAN Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
326 63758dd7 2001-12-15 alex {
327 63758dd7 2001-12-15 alex /* String in Socket schreiben. CR+LF wird von dieser Funktion
328 d5c97f81 2001-12-23 alex * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
329 d5c97f81 2001-12-23 alex * getrennt und FALSE geliefert. */
330 9ab186c4 2001-12-25 alex
331 804b1ec4 2001-12-31 alex CHAR buffer[COMMAND_LEN];
332 d5c97f81 2001-12-23 alex BOOLEAN ok;
333 d5c97f81 2001-12-23 alex va_list ap;
334 d5c97f81 2001-12-23 alex
335 28c5a21f 2002-03-14 alex assert( Idx >= 0 );
336 28c5a21f 2002-03-14 alex assert( My_Connections[Idx].sock > NONE );
337 28c5a21f 2002-03-14 alex assert( Format != NULL );
338 28c5a21f 2002-03-14 alex
339 d5c97f81 2001-12-23 alex va_start( ap, Format );
340 804b1ec4 2001-12-31 alex if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
341 63758dd7 2001-12-15 alex {
342 79809118 2002-01-06 alex Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
343 79809118 2002-01-06 alex Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
344 63758dd7 2001-12-15 alex return FALSE;
345 63758dd7 2001-12-15 alex }
346 d5c97f81 2001-12-23 alex
347 7c91951d 2001-12-25 alex #ifdef SNIFFER
348 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
349 d5c97f81 2001-12-23 alex #endif
350 9ab186c4 2001-12-25 alex
351 446df061 2001-12-24 alex strcat( buffer, "\r\n" );
352 446df061 2001-12-24 alex ok = Conn_Write( Idx, buffer, strlen( buffer ));
353 446df061 2001-12-24 alex
354 d5c97f81 2001-12-23 alex va_end( ap );
355 d5c97f81 2001-12-23 alex return ok;
356 63758dd7 2001-12-15 alex } /* Conn_WriteStr */
357 63758dd7 2001-12-15 alex
358 63758dd7 2001-12-15 alex
359 63758dd7 2001-12-15 alex GLOBAL BOOLEAN Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
360 63758dd7 2001-12-15 alex {
361 63758dd7 2001-12-15 alex /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
362 63758dd7 2001-12-15 alex * der Client disconnectiert und FALSE geliefert. */
363 9ab186c4 2001-12-25 alex
364 63758dd7 2001-12-15 alex assert( Idx >= 0 );
365 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
366 63758dd7 2001-12-15 alex assert( Data != NULL );
367 63758dd7 2001-12-15 alex assert( Len > 0 );
368 63758dd7 2001-12-15 alex
369 63758dd7 2001-12-15 alex /* pruefen, ob Daten im Schreibpuffer sind. Wenn ja, zunaechst
370 63758dd7 2001-12-15 alex * pruefen, ob diese gesendet werden koennen */
371 63758dd7 2001-12-15 alex if( My_Connections[Idx].wdatalen > 0 )
372 63758dd7 2001-12-15 alex {
373 63758dd7 2001-12-15 alex if( ! Try_Write( Idx )) return FALSE;
374 63758dd7 2001-12-15 alex }
375 9ab186c4 2001-12-25 alex
376 63758dd7 2001-12-15 alex /* pruefen, ob im Schreibpuffer genuegend Platz ist */
377 63758dd7 2001-12-15 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
378 63758dd7 2001-12-15 alex {
379 63758dd7 2001-12-15 alex /* der Puffer ist dummerweise voll ... */
380 63758dd7 2001-12-15 alex Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
381 79809118 2002-01-06 alex Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
382 63758dd7 2001-12-15 alex return FALSE;
383 63758dd7 2001-12-15 alex }
384 63758dd7 2001-12-15 alex
385 63758dd7 2001-12-15 alex /* Daten in Puffer kopieren */
386 63758dd7 2001-12-15 alex memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
387 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen += Len;
388 63758dd7 2001-12-15 alex
389 63758dd7 2001-12-15 alex /* pruefen, on Daten vorhanden sind und geschrieben werden koennen */
390 63758dd7 2001-12-15 alex if( My_Connections[Idx].wdatalen > 0 )
391 63758dd7 2001-12-15 alex {
392 63758dd7 2001-12-15 alex if( ! Try_Write( Idx )) return FALSE;
393 63758dd7 2001-12-15 alex }
394 9ab186c4 2001-12-25 alex
395 63758dd7 2001-12-15 alex return TRUE;
396 63758dd7 2001-12-15 alex } /* Conn_Write */
397 7c91951d 2001-12-25 alex
398 7c91951d 2001-12-25 alex
399 79809118 2002-01-06 alex GLOBAL VOID Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
400 7c91951d 2001-12-25 alex {
401 bc140df8 2001-12-29 alex /* Verbindung schliessen. Evtl. noch von Resolver
402 bc140df8 2001-12-29 alex * Sub-Prozessen offene Pipes werden geschlossen. */
403 7c91951d 2001-12-25 alex
404 03d971d9 2002-01-02 alex CLIENT *c;
405 590f2a3f 2002-03-11 alex
406 7c91951d 2001-12-25 alex assert( Idx >= 0 );
407 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
408 7c91951d 2001-12-25 alex
409 79809118 2002-01-06 alex if( InformClient )
410 03d971d9 2002-01-02 alex {
411 79809118 2002-01-06 alex if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
412 79809118 2002-01-06 alex else Conn_WriteStr( Idx, "ERROR :Closing connection." );
413 03d971d9 2002-01-02 alex if( My_Connections[Idx].sock == NONE ) return;
414 03d971d9 2002-01-02 alex }
415 7c91951d 2001-12-25 alex
416 7c91951d 2001-12-25 alex if( close( My_Connections[Idx].sock ) != 0 )
417 7c91951d 2001-12-25 alex {
418 7c91951d 2001-12-25 alex Log( LOG_ERR, "Error closing connection %d with %s:%d - %s!", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
419 7c91951d 2001-12-25 alex }
420 7c91951d 2001-12-25 alex else
421 7c91951d 2001-12-25 alex {
422 4d4f2d4f 2002-01-04 alex Log( LOG_INFO, "Connection %d with %s:%d closed.", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
423 7c91951d 2001-12-25 alex }
424 7c91951d 2001-12-25 alex
425 03d971d9 2002-01-02 alex c = Client_GetFromConn( Idx );
426 50ec7a56 2002-03-11 alex if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
427 7c91951d 2001-12-25 alex
428 4a111033 2001-12-29 alex if( My_Connections[Idx].res_stat )
429 4a111033 2001-12-29 alex {
430 4a111033 2001-12-29 alex /* Resolver-Strukturen freigeben, wenn noch nicht geschehen */
431 b9728ba2 2001-12-29 alex FD_CLR( My_Connections[Idx].res_stat->pipe[0], &My_Resolvers );
432 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[0] );
433 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[1] );
434 4a111033 2001-12-29 alex free( My_Connections[Idx].res_stat );
435 4a111033 2001-12-29 alex }
436 03d971d9 2002-01-02 alex
437 a3ee1a9a 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt so setzen, dass
438 a3ee1a9a 2002-03-02 alex * der naechste Verbindungsversuch in RECONNECT_DELAY Sekunden
439 590f2a3f 2002-03-11 alex * gestartet wird. */
440 590f2a3f 2002-03-11 alex if(( My_Connections[Idx].our_server >= 0 ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry < time( NULL )))
441 590f2a3f 2002-03-11 alex {
442 590f2a3f 2002-03-11 alex /* Okay, die Verbindung stand schon "genuegend lange" */
443 590f2a3f 2002-03-11 alex Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
444 590f2a3f 2002-03-11 alex }
445 03d971d9 2002-01-02 alex
446 7c91951d 2001-12-25 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
447 5457e078 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
448 7c91951d 2001-12-25 alex My_Connections[Idx].sock = NONE;
449 7c91951d 2001-12-25 alex } /* Conn_Close */
450 804b1ec4 2001-12-31 alex
451 804b1ec4 2001-12-31 alex
452 804b1ec4 2001-12-31 alex GLOBAL VOID Conn_UpdateIdle( CONN_ID Idx )
453 804b1ec4 2001-12-31 alex {
454 804b1ec4 2001-12-31 alex /* Idle-Timer zuruecksetzen */
455 804b1ec4 2001-12-31 alex
456 804b1ec4 2001-12-31 alex assert( Idx >= 0 );
457 804b1ec4 2001-12-31 alex My_Connections[Idx].lastprivmsg = time( NULL );
458 804b1ec4 2001-12-31 alex }
459 804b1ec4 2001-12-31 alex
460 804b1ec4 2001-12-31 alex
461 8465653c 2002-02-23 alex GLOBAL time_t Conn_GetIdle( CONN_ID Idx )
462 804b1ec4 2001-12-31 alex {
463 804b1ec4 2001-12-31 alex /* Idle-Time einer Verbindung liefern (in Sekunden) */
464 63758dd7 2001-12-15 alex
465 804b1ec4 2001-12-31 alex assert( Idx >= 0 );
466 804b1ec4 2001-12-31 alex return time( NULL ) - My_Connections[Idx].lastprivmsg;
467 804b1ec4 2001-12-31 alex } /* Conn_GetIdle */
468 b9f005af 2002-02-11 alex
469 b9f005af 2002-02-11 alex
470 8465653c 2002-02-23 alex GLOBAL time_t Conn_LastPing( CONN_ID Idx )
471 b9f005af 2002-02-11 alex {
472 b9f005af 2002-02-11 alex /* Zeitpunkt des letzten PING liefern */
473 b9f005af 2002-02-11 alex
474 b9f005af 2002-02-11 alex assert( Idx >= 0 );
475 b9f005af 2002-02-11 alex return My_Connections[Idx].lastping;
476 b9f005af 2002-02-11 alex } /* Conn_LastPing */
477 63758dd7 2001-12-15 alex
478 804b1ec4 2001-12-31 alex
479 63758dd7 2001-12-15 alex LOCAL BOOLEAN Try_Write( CONN_ID Idx )
480 63758dd7 2001-12-15 alex {
481 63758dd7 2001-12-15 alex /* Versuchen, Daten aus dem Schreib-Puffer in den
482 63758dd7 2001-12-15 alex * Socket zu schreiben. */
483 63758dd7 2001-12-15 alex
484 63758dd7 2001-12-15 alex fd_set write_socket;
485 9ab186c4 2001-12-25 alex
486 63758dd7 2001-12-15 alex assert( Idx >= 0 );
487 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
488 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
489 63758dd7 2001-12-15 alex
490 63758dd7 2001-12-15 alex FD_ZERO( &write_socket );
491 63758dd7 2001-12-15 alex FD_SET( My_Connections[Idx].sock, &write_socket );
492 63758dd7 2001-12-15 alex if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, 0 ) == -1 )
493 63758dd7 2001-12-15 alex {
494 63758dd7 2001-12-15 alex /* Fehler! */
495 63758dd7 2001-12-15 alex if( errno != EINTR )
496 63758dd7 2001-12-15 alex {
497 79809118 2002-01-06 alex Log( LOG_ALERT, "select() failed: %s!", strerror( errno ));
498 79809118 2002-01-06 alex Conn_Close( Idx, "Server error!", NULL, FALSE );
499 63758dd7 2001-12-15 alex return FALSE;
500 63758dd7 2001-12-15 alex }
501 63758dd7 2001-12-15 alex }
502 63758dd7 2001-12-15 alex
503 63758dd7 2001-12-15 alex if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
504 63758dd7 2001-12-15 alex else return TRUE;
505 63758dd7 2001-12-15 alex } /* Try_Write */
506 63758dd7 2001-12-15 alex
507 63758dd7 2001-12-15 alex
508 63758dd7 2001-12-15 alex LOCAL VOID Handle_Read( INT Sock )
509 1c8eb478 2001-12-12 alex {
510 bc140df8 2001-12-29 alex /* Aktivitaet auf einem Socket verarbeiten:
511 bc140df8 2001-12-29 alex * - neue Clients annehmen,
512 bc140df8 2001-12-29 alex * - Daten von Clients verarbeiten,
513 bc140df8 2001-12-29 alex * - Resolver-Rueckmeldungen annehmen. */
514 1c8eb478 2001-12-12 alex
515 cf050519 2001-12-14 alex CONN_ID idx;
516 9ab186c4 2001-12-25 alex
517 63758dd7 2001-12-15 alex assert( Sock >= 0 );
518 590f2a3f 2002-03-11 alex
519 4a111033 2001-12-29 alex if( FD_ISSET( Sock, &My_Listeners ))
520 1c8eb478 2001-12-12 alex {
521 1c8eb478 2001-12-12 alex /* es ist einer unserer Listener-Sockets: es soll
522 1c8eb478 2001-12-12 alex * also eine neue Verbindung aufgebaut werden. */
523 1c8eb478 2001-12-12 alex
524 1c8eb478 2001-12-12 alex New_Connection( Sock );
525 1c8eb478 2001-12-12 alex }
526 4a111033 2001-12-29 alex else if( FD_ISSET( Sock, &My_Resolvers ))
527 4a111033 2001-12-29 alex {
528 4a111033 2001-12-29 alex /* Rueckmeldung von einem Resolver Sub-Prozess */
529 4a111033 2001-12-29 alex
530 4a111033 2001-12-29 alex Read_Resolver_Result( Sock );
531 4a111033 2001-12-29 alex }
532 1c8eb478 2001-12-12 alex else
533 1c8eb478 2001-12-12 alex {
534 1c8eb478 2001-12-12 alex /* Ein Client Socket: entweder ein User oder Server */
535 9ab186c4 2001-12-25 alex
536 1c8eb478 2001-12-12 alex idx = Socket2Index( Sock );
537 747fd2f0 2001-12-13 alex Read_Request( idx );
538 1c8eb478 2001-12-12 alex }
539 63758dd7 2001-12-15 alex } /* Handle_Read */
540 1c8eb478 2001-12-12 alex
541 1c8eb478 2001-12-12 alex
542 63758dd7 2001-12-15 alex LOCAL BOOLEAN Handle_Write( CONN_ID Idx )
543 63758dd7 2001-12-15 alex {
544 6da91c34 2002-03-02 alex /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
545 9ab186c4 2001-12-25 alex
546 6da91c34 2002-03-02 alex INT len, res, err;
547 63758dd7 2001-12-15 alex
548 63758dd7 2001-12-15 alex assert( Idx >= 0 );
549 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
550 6da91c34 2002-03-02 alex
551 6da91c34 2002-03-02 alex if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
552 6da91c34 2002-03-02 alex {
553 6da91c34 2002-03-02 alex /* es soll nichts geschrieben werden, sondern ein
554 6da91c34 2002-03-02 alex * connect() hat ein Ergebnis geliefert */
555 6da91c34 2002-03-02 alex
556 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
557 6da91c34 2002-03-02 alex
558 6da91c34 2002-03-02 alex /* Ergebnis des connect() ermitteln */
559 6da91c34 2002-03-02 alex len = sizeof( err );
560 6da91c34 2002-03-02 alex res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
561 6da91c34 2002-03-02 alex assert( len == sizeof( err ));
562 6da91c34 2002-03-02 alex
563 6da91c34 2002-03-02 alex /* Fehler aufgetreten? */
564 6da91c34 2002-03-02 alex if(( res != 0 ) || ( err != 0 ))
565 6da91c34 2002-03-02 alex {
566 6da91c34 2002-03-02 alex /* Fehler! */
567 6da91c34 2002-03-02 alex if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
568 6da91c34 2002-03-02 alex else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port, Idx, strerror( err ));
569 6da91c34 2002-03-02 alex
570 6da91c34 2002-03-02 alex /* Socket etc. pp. aufraeumen */
571 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
572 6da91c34 2002-03-02 alex close( My_Connections[Idx].sock );
573 6da91c34 2002-03-02 alex Init_Conn_Struct( Idx );
574 cbce54e0 2002-03-02 alex
575 cbce54e0 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
576 cbce54e0 2002-03-02 alex Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
577 cbce54e0 2002-03-02 alex
578 6da91c34 2002-03-02 alex return FALSE;
579 6da91c34 2002-03-02 alex }
580 6da91c34 2002-03-02 alex Log( LOG_DEBUG, "Connection %d with \"%s:%d\" established, now sendig PASS and SERVER ...", Idx, My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port );
581 6da91c34 2002-03-02 alex
582 6da91c34 2002-03-02 alex /* PASS und SERVER verschicken */
583 6da91c34 2002-03-02 alex Conn_WriteStr( Idx, "PASS %s "PASSSERVERADD, Conf_Server[My_Connections[Idx].our_server].pwd );
584 6da91c34 2002-03-02 alex Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
585 6da91c34 2002-03-02 alex
586 6da91c34 2002-03-02 alex return TRUE;
587 6da91c34 2002-03-02 alex }
588 6da91c34 2002-03-02 alex
589 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
590 9ab186c4 2001-12-25 alex
591 63758dd7 2001-12-15 alex /* Daten schreiben */
592 63758dd7 2001-12-15 alex len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
593 63758dd7 2001-12-15 alex if( len < 0 )
594 63758dd7 2001-12-15 alex {
595 63758dd7 2001-12-15 alex /* Oops, ein Fehler! */
596 79809118 2002-01-06 alex Log( LOG_ERR, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
597 79809118 2002-01-06 alex Conn_Close( Idx, "Write error (buffer)!", NULL, FALSE );
598 63758dd7 2001-12-15 alex return FALSE;
599 63758dd7 2001-12-15 alex }
600 9ab186c4 2001-12-25 alex
601 63758dd7 2001-12-15 alex /* Puffer anpassen */
602 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen -= len;
603 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
604 9ab186c4 2001-12-25 alex
605 63758dd7 2001-12-15 alex return TRUE;
606 63758dd7 2001-12-15 alex } /* Handle_Write */
607 63758dd7 2001-12-15 alex
608 63758dd7 2001-12-15 alex
609 1c8eb478 2001-12-12 alex LOCAL VOID New_Connection( INT Sock )
610 1c8eb478 2001-12-12 alex {
611 d5c97f81 2001-12-23 alex /* Neue Client-Verbindung von Listen-Socket annehmen und
612 d5c97f81 2001-12-23 alex * CLIENT-Struktur anlegen. */
613 63758dd7 2001-12-15 alex
614 1c8eb478 2001-12-12 alex struct sockaddr_in new_addr;
615 cf050519 2001-12-14 alex INT new_sock, new_sock_len;
616 4a111033 2001-12-29 alex RES_STAT *s;
617 cf050519 2001-12-14 alex CONN_ID idx;
618 590f2a3f 2002-03-11 alex
619 63758dd7 2001-12-15 alex assert( Sock >= 0 );
620 5fefe1a3 2001-12-12 alex
621 1c8eb478 2001-12-12 alex new_sock_len = sizeof( new_addr );
622 fb55c443 2001-12-13 alex new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
623 1c8eb478 2001-12-12 alex if( new_sock < 0 )
624 5fefe1a3 2001-12-12 alex {
625 446df061 2001-12-24 alex Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
626 5fefe1a3 2001-12-12 alex return;
627 5fefe1a3 2001-12-12 alex }
628 9ab186c4 2001-12-25 alex
629 1c8eb478 2001-12-12 alex /* Freie Connection-Struktur suschen */
630 03d971d9 2002-01-02 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == NONE ) break;
631 1c8eb478 2001-12-12 alex if( idx >= MAX_CONNECTIONS )
632 1c8eb478 2001-12-12 alex {
633 63758dd7 2001-12-15 alex Log( LOG_ALERT, "Can't accept connection: limit reached (%d)!", MAX_CONNECTIONS );
634 1c8eb478 2001-12-12 alex close( new_sock );
635 1c8eb478 2001-12-12 alex return;
636 1c8eb478 2001-12-12 alex }
637 9ab186c4 2001-12-25 alex
638 d5c97f81 2001-12-23 alex /* Client-Struktur initialisieren */
639 2e4d085d 2002-01-05 alex if( ! Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE ))
640 d5c97f81 2001-12-23 alex {
641 d5c97f81 2001-12-23 alex Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
642 d5c97f81 2001-12-23 alex close( new_sock );
643 d5c97f81 2001-12-23 alex return;
644 d5c97f81 2001-12-23 alex }
645 9ab186c4 2001-12-25 alex
646 1c8eb478 2001-12-12 alex /* Verbindung registrieren */
647 4a111033 2001-12-29 alex Init_Conn_Struct( idx );
648 1c8eb478 2001-12-12 alex My_Connections[idx].sock = new_sock;
649 1c8eb478 2001-12-12 alex My_Connections[idx].addr = new_addr;
650 1c8eb478 2001-12-12 alex
651 1c8eb478 2001-12-12 alex /* Neuen Socket registrieren */
652 1c8eb478 2001-12-12 alex FD_SET( new_sock, &My_Sockets );
653 1c8eb478 2001-12-12 alex if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock;
654 1c8eb478 2001-12-12 alex
655 4d4f2d4f 2002-01-04 alex Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
656 4a111033 2001-12-29 alex
657 4a111033 2001-12-29 alex /* Hostnamen ermitteln */
658 03d971d9 2002-01-02 alex s = ResolveAddr( &new_addr );
659 4a111033 2001-12-29 alex if( s )
660 4a111033 2001-12-29 alex {
661 4a111033 2001-12-29 alex /* Sub-Prozess wurde asyncron gestartet */
662 4a111033 2001-12-29 alex My_Connections[idx].res_stat = s;
663 4a111033 2001-12-29 alex }
664 4a111033 2001-12-29 alex else
665 4a111033 2001-12-29 alex {
666 4a111033 2001-12-29 alex /* kann Namen nicht aufloesen */
667 4a111033 2001-12-29 alex strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
668 4a111033 2001-12-29 alex }
669 1c8eb478 2001-12-12 alex } /* New_Connection */
670 5fefe1a3 2001-12-12 alex
671 5fefe1a3 2001-12-12 alex
672 cf050519 2001-12-14 alex LOCAL CONN_ID Socket2Index( INT Sock )
673 1c8eb478 2001-12-12 alex {
674 63758dd7 2001-12-15 alex /* zum Socket passende Connection suchen */
675 63758dd7 2001-12-15 alex
676 cf050519 2001-12-14 alex CONN_ID idx;
677 9ab186c4 2001-12-25 alex
678 63758dd7 2001-12-15 alex assert( Sock >= 0 );
679 9ab186c4 2001-12-25 alex
680 1c8eb478 2001-12-12 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == Sock ) break;
681 9ab186c4 2001-12-25 alex
682 63758dd7 2001-12-15 alex assert( idx < MAX_CONNECTIONS );
683 1c8eb478 2001-12-12 alex return idx;
684 1c8eb478 2001-12-12 alex } /* Socket2Index */
685 1c8eb478 2001-12-12 alex
686 1c8eb478 2001-12-12 alex
687 cf050519 2001-12-14 alex LOCAL VOID Read_Request( CONN_ID Idx )
688 1c8eb478 2001-12-12 alex {
689 747fd2f0 2001-12-13 alex /* Daten von Socket einlesen und entsprechend behandeln.
690 747fd2f0 2001-12-13 alex * Tritt ein Fehler auf, so wird der Socket geschlossen. */
691 1c8eb478 2001-12-12 alex
692 747fd2f0 2001-12-13 alex INT len;
693 63758dd7 2001-12-15 alex
694 63758dd7 2001-12-15 alex assert( Idx >= 0 );
695 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
696 9ab186c4 2001-12-25 alex
697 54e487d4 2002-01-03 alex if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 2 < 0 )
698 54e487d4 2002-01-03 alex {
699 54e487d4 2002-01-03 alex /* Der Lesepuffer ist voll */
700 79809118 2002-01-06 alex Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
701 79809118 2002-01-06 alex Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
702 54e487d4 2002-01-03 alex return;
703 54e487d4 2002-01-03 alex }
704 54e487d4 2002-01-03 alex
705 54e487d4 2002-01-03 alex len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, READBUFFER_LEN - My_Connections[Idx].rdatalen - 2, 0 );
706 747fd2f0 2001-12-13 alex
707 747fd2f0 2001-12-13 alex if( len == 0 )
708 1c8eb478 2001-12-12 alex {
709 747fd2f0 2001-12-13 alex /* Socket wurde geschlossen */
710 c4199b04 2001-12-21 alex Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
711 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
712 1c8eb478 2001-12-12 alex return;
713 1c8eb478 2001-12-12 alex }
714 747fd2f0 2001-12-13 alex
715 747fd2f0 2001-12-13 alex if( len < 0 )
716 747fd2f0 2001-12-13 alex {
717 747fd2f0 2001-12-13 alex /* Fehler beim Lesen */
718 79809118 2002-01-06 alex Log( LOG_ERR, "Read error on connection %d: %s!", Idx, strerror( errno ));
719 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
720 747fd2f0 2001-12-13 alex return;
721 747fd2f0 2001-12-13 alex }
722 cf050519 2001-12-14 alex
723 65bdfdf2 2001-12-26 alex /* Lesebuffer updaten */
724 63758dd7 2001-12-15 alex My_Connections[Idx].rdatalen += len;
725 54e487d4 2002-01-03 alex assert( My_Connections[Idx].rdatalen < READBUFFER_LEN );
726 63758dd7 2001-12-15 alex My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
727 9ab186c4 2001-12-25 alex
728 65bdfdf2 2001-12-26 alex /* Timestamp aktualisieren */
729 65bdfdf2 2001-12-26 alex My_Connections[Idx].lastdata = time( NULL );
730 65bdfdf2 2001-12-26 alex
731 9ab186c4 2001-12-25 alex Handle_Buffer( Idx );
732 9ab186c4 2001-12-25 alex } /* Read_Request */
733 9ab186c4 2001-12-25 alex
734 9ab186c4 2001-12-25 alex
735 9ab186c4 2001-12-25 alex LOCAL VOID Handle_Buffer( CONN_ID Idx )
736 9ab186c4 2001-12-25 alex {
737 bc140df8 2001-12-29 alex /* Daten im Lese-Puffer einer Verbindung verarbeiten. */
738 bc140df8 2001-12-29 alex
739 9ab186c4 2001-12-25 alex CHAR *ptr, *ptr1, *ptr2;
740 9ab186c4 2001-12-25 alex INT len, delta;
741 590f2a3f 2002-03-11 alex
742 63758dd7 2001-12-15 alex /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
743 63758dd7 2001-12-15 alex * RFC 2812. Haben wir eine? */
744 63758dd7 2001-12-15 alex ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
745 9ab186c4 2001-12-25 alex
746 9ab186c4 2001-12-25 alex if( ptr ) delta = 2;
747 54e487d4 2002-01-03 alex #ifndef STRICT_RFC
748 9ab186c4 2001-12-25 alex else
749 9ab186c4 2001-12-25 alex {
750 9ab186c4 2001-12-25 alex /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
751 9ab186c4 2001-12-25 alex * machen soetwas viele Clients, u.a. "mIRC" :-( */
752 9ab186c4 2001-12-25 alex ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
753 9ab186c4 2001-12-25 alex ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
754 9ab186c4 2001-12-25 alex delta = 1;
755 9ab186c4 2001-12-25 alex if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
756 9ab186c4 2001-12-25 alex else if( ptr1 ) ptr = ptr1;
757 9ab186c4 2001-12-25 alex else if( ptr2 ) ptr = ptr2;
758 9ab186c4 2001-12-25 alex }
759 54e487d4 2002-01-03 alex #endif
760 590f2a3f 2002-03-11 alex
761 63758dd7 2001-12-15 alex if( ptr )
762 747fd2f0 2001-12-13 alex {
763 9ab186c4 2001-12-25 alex /* Ende der Anfrage wurde gefunden */
764 63758dd7 2001-12-15 alex *ptr = '\0';
765 9ab186c4 2001-12-25 alex len = ( ptr - My_Connections[Idx].rbuf ) + delta;
766 54e487d4 2002-01-03 alex if( len > COMMAND_LEN )
767 54e487d4 2002-01-03 alex {
768 54e487d4 2002-01-03 alex /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
769 54e487d4 2002-01-03 alex * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
770 54e487d4 2002-01-03 alex * empfangen wird, wird der Client disconnectiert. */
771 79809118 2002-01-06 alex Log( LOG_ERR, "Request too long (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
772 79809118 2002-01-06 alex Conn_Close( Idx, NULL, "Request too long", TRUE );
773 54e487d4 2002-01-03 alex return;
774 54e487d4 2002-01-03 alex }
775 590f2a3f 2002-03-11 alex
776 9ab186c4 2001-12-25 alex if( len > delta )
777 747fd2f0 2001-12-13 alex {
778 c4199b04 2001-12-21 alex /* Es wurde ein Request gelesen */
779 c4199b04 2001-12-21 alex if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
780 747fd2f0 2001-12-13 alex }
781 63758dd7 2001-12-15 alex
782 63758dd7 2001-12-15 alex /* Puffer anpassen */
783 63758dd7 2001-12-15 alex My_Connections[Idx].rdatalen -= len;
784 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
785 747fd2f0 2001-12-13 alex }
786 9ab186c4 2001-12-25 alex } /* Handle_Buffer */
787 747fd2f0 2001-12-13 alex
788 747fd2f0 2001-12-13 alex
789 65bdfdf2 2001-12-26 alex LOCAL VOID Check_Connections( VOID )
790 65bdfdf2 2001-12-26 alex {
791 bc140df8 2001-12-29 alex /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
792 bc140df8 2001-12-29 alex * nicht der Fall, zunaechst PING-PONG spielen und, wenn
793 bc140df8 2001-12-29 alex * auch das nicht "hilft", Client disconnectieren. */
794 65bdfdf2 2001-12-26 alex
795 03d971d9 2002-01-02 alex CLIENT *c;
796 65bdfdf2 2001-12-26 alex INT i;
797 65bdfdf2 2001-12-26 alex
798 65bdfdf2 2001-12-26 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
799 65bdfdf2 2001-12-26 alex {
800 03d971d9 2002-01-02 alex if( My_Connections[i].sock == NONE ) continue;
801 03d971d9 2002-01-02 alex
802 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
803 356683ff 2002-01-04 alex if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
804 65bdfdf2 2001-12-26 alex {
805 03d971d9 2002-01-02 alex /* verbundener User, Server oder Service */
806 65bdfdf2 2001-12-26 alex if( My_Connections[i].lastping > My_Connections[i].lastdata )
807 65bdfdf2 2001-12-26 alex {
808 65bdfdf2 2001-12-26 alex /* es wurde bereits ein PING gesendet */
809 08cf5607 2001-12-26 alex if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
810 65bdfdf2 2001-12-26 alex {
811 65bdfdf2 2001-12-26 alex /* Timeout */
812 e39925af 2002-03-26 alex Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
813 79809118 2002-01-06 alex Conn_Close( i, NULL, "Ping timeout", TRUE );
814 65bdfdf2 2001-12-26 alex }
815 65bdfdf2 2001-12-26 alex }
816 08cf5607 2001-12-26 alex else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
817 65bdfdf2 2001-12-26 alex {
818 65bdfdf2 2001-12-26 alex /* es muss ein PING gesendet werden */
819 65bdfdf2 2001-12-26 alex Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
820 65bdfdf2 2001-12-26 alex My_Connections[i].lastping = time( NULL );
821 356683ff 2002-01-04 alex Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
822 65bdfdf2 2001-12-26 alex }
823 65bdfdf2 2001-12-26 alex }
824 03d971d9 2002-01-02 alex else
825 03d971d9 2002-01-02 alex {
826 03d971d9 2002-01-02 alex /* noch nicht vollstaendig aufgebaute Verbindung */
827 03d971d9 2002-01-02 alex if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
828 03d971d9 2002-01-02 alex {
829 03d971d9 2002-01-02 alex /* Timeout */
830 140d1aa5 2002-02-27 alex Log( LOG_DEBUG, "Connection %d timed out ...", i );
831 5457e078 2002-03-02 alex Conn_Close( i, NULL, "Timeout", FALSE );
832 03d971d9 2002-01-02 alex }
833 03d971d9 2002-01-02 alex }
834 65bdfdf2 2001-12-26 alex }
835 03d971d9 2002-01-02 alex } /* Check_Connections */
836 03d971d9 2002-01-02 alex
837 03d971d9 2002-01-02 alex
838 03d971d9 2002-01-02 alex LOCAL VOID Check_Servers( VOID )
839 03d971d9 2002-01-02 alex {
840 03d971d9 2002-01-02 alex /* Pruefen, ob Server-Verbindungen aufgebaut werden
841 03d971d9 2002-01-02 alex * muessen bzw. koennen */
842 03d971d9 2002-01-02 alex
843 03d971d9 2002-01-02 alex INT idx, i, n;
844 03d971d9 2002-01-02 alex RES_STAT *s;
845 26ffbc78 2002-02-19 alex
846 26ffbc78 2002-02-19 alex /* Wenn "Passive-Mode" aktiv: nicht verbinden */
847 26ffbc78 2002-02-19 alex if( NGIRCd_Passive ) return;
848 590f2a3f 2002-03-11 alex
849 03d971d9 2002-01-02 alex for( i = 0; i < Conf_Server_Count; i++ )
850 03d971d9 2002-01-02 alex {
851 03d971d9 2002-01-02 alex /* Ist ein Hostname und Port definiert? */
852 03d971d9 2002-01-02 alex if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue;
853 590f2a3f 2002-03-11 alex
854 03d971d9 2002-01-02 alex /* Haben wir schon eine Verbindung? */
855 03d971d9 2002-01-02 alex for( n = 0; n < MAX_CONNECTIONS; n++ )
856 03d971d9 2002-01-02 alex {
857 9146fa25 2002-03-12 alex if( My_Connections[n].sock == NONE ) continue;
858 9146fa25 2002-03-12 alex
859 d67d94ea 2002-03-10 alex /* Verbindung zu diesem Server? */
860 9146fa25 2002-03-12 alex if( My_Connections[n].our_server == i )
861 03d971d9 2002-01-02 alex {
862 03d971d9 2002-01-02 alex /* Komplett aufgebaute Verbindung? */
863 03d971d9 2002-01-02 alex if( My_Connections[n].sock > NONE ) break;
864 03d971d9 2002-01-02 alex
865 03d971d9 2002-01-02 alex /* IP schon aufgeloest? */
866 03d971d9 2002-01-02 alex if( My_Connections[n].res_stat == NULL ) New_Server( i, n );
867 d67d94ea 2002-03-10 alex }
868 d67d94ea 2002-03-10 alex
869 d67d94ea 2002-03-10 alex /* Verbindung in dieser Server-Gruppe? */
870 9146fa25 2002-03-12 alex if(( My_Connections[n].our_server != NONE ) && ( Conf_Server[i].group != NONE ))
871 d67d94ea 2002-03-10 alex {
872 9146fa25 2002-03-12 alex if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break;
873 03d971d9 2002-01-02 alex }
874 03d971d9 2002-01-02 alex }
875 03d971d9 2002-01-02 alex if( n < MAX_CONNECTIONS ) continue;
876 590f2a3f 2002-03-11 alex
877 03d971d9 2002-01-02 alex /* Wann war der letzte Connect-Versuch? */
878 03d971d9 2002-01-02 alex if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
879 65bdfdf2 2001-12-26 alex
880 03d971d9 2002-01-02 alex /* Okay, Verbindungsaufbau versuchen */
881 03d971d9 2002-01-02 alex Conf_Server[i].lasttry = time( NULL );
882 65bdfdf2 2001-12-26 alex
883 03d971d9 2002-01-02 alex /* Freie Connection-Struktur suschen */
884 03d971d9 2002-01-02 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == NONE ) break;
885 03d971d9 2002-01-02 alex if( idx >= MAX_CONNECTIONS )
886 03d971d9 2002-01-02 alex {
887 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", MAX_CONNECTIONS );
888 03d971d9 2002-01-02 alex return;
889 03d971d9 2002-01-02 alex }
890 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
891 03d971d9 2002-01-02 alex
892 03d971d9 2002-01-02 alex /* Verbindungs-Struktur initialisieren */
893 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
894 03d971d9 2002-01-02 alex My_Connections[idx].sock = SERVER_WAIT;
895 03d971d9 2002-01-02 alex My_Connections[idx].our_server = i;
896 590f2a3f 2002-03-11 alex
897 03d971d9 2002-01-02 alex /* Hostnamen in IP aufloesen */
898 03d971d9 2002-01-02 alex s = ResolveName( Conf_Server[i].host );
899 03d971d9 2002-01-02 alex if( s )
900 03d971d9 2002-01-02 alex {
901 03d971d9 2002-01-02 alex /* Sub-Prozess wurde asyncron gestartet */
902 03d971d9 2002-01-02 alex My_Connections[idx].res_stat = s;
903 03d971d9 2002-01-02 alex }
904 03d971d9 2002-01-02 alex else
905 03d971d9 2002-01-02 alex {
906 03d971d9 2002-01-02 alex /* kann Namen nicht aufloesen: Connection-Struktur freigeben */
907 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
908 03d971d9 2002-01-02 alex }
909 03d971d9 2002-01-02 alex }
910 03d971d9 2002-01-02 alex } /* Check_Servers */
911 03d971d9 2002-01-02 alex
912 03d971d9 2002-01-02 alex
913 03d971d9 2002-01-02 alex LOCAL VOID New_Server( INT Server, CONN_ID Idx )
914 03d971d9 2002-01-02 alex {
915 03d971d9 2002-01-02 alex /* Neue Server-Verbindung aufbauen */
916 03d971d9 2002-01-02 alex
917 03d971d9 2002-01-02 alex struct sockaddr_in new_addr;
918 03d971d9 2002-01-02 alex struct in_addr inaddr;
919 03d971d9 2002-01-02 alex INT new_sock;
920 54e487d4 2002-01-03 alex CLIENT *c;
921 03d971d9 2002-01-02 alex
922 03d971d9 2002-01-02 alex assert( Server >= 0 );
923 03d971d9 2002-01-02 alex assert( Idx >= 0 );
924 03d971d9 2002-01-02 alex
925 03d971d9 2002-01-02 alex /* Wurde eine gueltige IP-Adresse gefunden? */
926 03d971d9 2002-01-02 alex if( ! Conf_Server[Server].ip[0] )
927 03d971d9 2002-01-02 alex {
928 03d971d9 2002-01-02 alex /* Nein. Verbindung wieder freigeben: */
929 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
930 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
931 03d971d9 2002-01-02 alex return;
932 03d971d9 2002-01-02 alex }
933 590f2a3f 2002-03-11 alex
934 94435271 2002-02-19 alex Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Conf_Server[Server].port, Idx );
935 03d971d9 2002-01-02 alex
936 03d971d9 2002-01-02 alex if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
937 03d971d9 2002-01-02 alex {
938 03d971d9 2002-01-02 alex /* Konnte Adresse nicht konvertieren */
939 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
940 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): can't convert ip address %s!", Conf_Server[Server].host, Idx, Conf_Server[Server].ip );
941 03d971d9 2002-01-02 alex return;
942 03d971d9 2002-01-02 alex }
943 03d971d9 2002-01-02 alex
944 03d971d9 2002-01-02 alex memset( &new_addr, 0, sizeof( new_addr ));
945 03d971d9 2002-01-02 alex new_addr.sin_family = AF_INET;
946 03d971d9 2002-01-02 alex new_addr.sin_addr = inaddr;
947 03d971d9 2002-01-02 alex new_addr.sin_port = htons( Conf_Server[Server].port );
948 03d971d9 2002-01-02 alex
949 03d971d9 2002-01-02 alex new_sock = socket( PF_INET, SOCK_STREAM, 0 );
950 03d971d9 2002-01-02 alex if ( new_sock < 0 )
951 03d971d9 2002-01-02 alex {
952 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
953 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
954 03d971d9 2002-01-02 alex return;
955 03d971d9 2002-01-02 alex }
956 6da91c34 2002-03-02 alex
957 6da91c34 2002-03-02 alex if( ! Init_Socket( new_sock )) return;
958 6da91c34 2002-03-02 alex
959 6da91c34 2002-03-02 alex connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
960 6da91c34 2002-03-02 alex if( errno != EINPROGRESS )
961 03d971d9 2002-01-02 alex {
962 590f2a3f 2002-03-11 alex
963 03d971d9 2002-01-02 alex close( new_sock );
964 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
965 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
966 03d971d9 2002-01-02 alex return;
967 03d971d9 2002-01-02 alex }
968 03d971d9 2002-01-02 alex
969 03d971d9 2002-01-02 alex /* Client-Struktur initialisieren */
970 2e4d085d 2002-01-05 alex c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
971 54e487d4 2002-01-03 alex if( ! c )
972 03d971d9 2002-01-02 alex {
973 03d971d9 2002-01-02 alex close( new_sock );
974 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
975 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
976 03d971d9 2002-01-02 alex return;
977 03d971d9 2002-01-02 alex }
978 356683ff 2002-01-04 alex Client_SetIntroducer( c, c );
979 6da91c34 2002-03-02 alex
980 03d971d9 2002-01-02 alex /* Verbindung registrieren */
981 03d971d9 2002-01-02 alex My_Connections[Idx].sock = new_sock;
982 03d971d9 2002-01-02 alex My_Connections[Idx].addr = new_addr;
983 03d971d9 2002-01-02 alex strcpy( My_Connections[Idx].host, Conf_Server[Server].host );
984 03d971d9 2002-01-02 alex
985 03d971d9 2002-01-02 alex /* Neuen Socket registrieren */
986 03d971d9 2002-01-02 alex FD_SET( new_sock, &My_Sockets );
987 6da91c34 2002-03-02 alex FD_SET( new_sock, &My_Connects );
988 03d971d9 2002-01-02 alex if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock;
989 03d971d9 2002-01-02 alex } /* New_Server */
990 03d971d9 2002-01-02 alex
991 03d971d9 2002-01-02 alex
992 4a111033 2001-12-29 alex LOCAL VOID Init_Conn_Struct( INT Idx )
993 4a111033 2001-12-29 alex {
994 4a111033 2001-12-29 alex /* Connection-Struktur initialisieren */
995 4a111033 2001-12-29 alex
996 4a111033 2001-12-29 alex My_Connections[Idx].sock = NONE;
997 4a111033 2001-12-29 alex My_Connections[Idx].res_stat = NULL;
998 4a111033 2001-12-29 alex My_Connections[Idx].host[0] = '\0';
999 4a111033 2001-12-29 alex My_Connections[Idx].rbuf[0] = '\0';
1000 4a111033 2001-12-29 alex My_Connections[Idx].rdatalen = 0;
1001 4a111033 2001-12-29 alex My_Connections[Idx].wbuf[0] = '\0';
1002 4a111033 2001-12-29 alex My_Connections[Idx].wdatalen = 0;
1003 d67d94ea 2002-03-10 alex My_Connections[Idx].our_server = NONE;
1004 4a111033 2001-12-29 alex My_Connections[Idx].lastdata = time( NULL );
1005 4a111033 2001-12-29 alex My_Connections[Idx].lastping = 0;
1006 804b1ec4 2001-12-31 alex My_Connections[Idx].lastprivmsg = time( NULL );
1007 4a111033 2001-12-29 alex } /* Init_Conn_Struct */
1008 4a111033 2001-12-29 alex
1009 4a111033 2001-12-29 alex
1010 6da91c34 2002-03-02 alex LOCAL BOOLEAN Init_Socket( INT Sock )
1011 6da91c34 2002-03-02 alex {
1012 6da91c34 2002-03-02 alex /* Socket-Optionen setzen */
1013 6da91c34 2002-03-02 alex
1014 6da91c34 2002-03-02 alex INT on = 1;
1015 6da91c34 2002-03-02 alex
1016 239727b4 2002-03-13 alex #ifdef O_NONBLOCK /* A/UX kennt das nicht? */
1017 6da91c34 2002-03-02 alex if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1018 6da91c34 2002-03-02 alex {
1019 6da91c34 2002-03-02 alex Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1020 6da91c34 2002-03-02 alex close( Sock );
1021 6da91c34 2002-03-02 alex return FALSE;
1022 6da91c34 2002-03-02 alex }
1023 239727b4 2002-03-13 alex #endif
1024 6da91c34 2002-03-02 alex if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1025 6da91c34 2002-03-02 alex {
1026 6da91c34 2002-03-02 alex Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1027 6da91c34 2002-03-02 alex /* dieser Fehler kann ignoriert werden. */
1028 6da91c34 2002-03-02 alex }
1029 6da91c34 2002-03-02 alex
1030 6da91c34 2002-03-02 alex return TRUE;
1031 6da91c34 2002-03-02 alex } /* Init_Socket */
1032 6da91c34 2002-03-02 alex
1033 6da91c34 2002-03-02 alex
1034 03d971d9 2002-01-02 alex LOCAL RES_STAT *ResolveAddr( struct sockaddr_in *Addr )
1035 4a111033 2001-12-29 alex {
1036 03d971d9 2002-01-02 alex /* IP (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
1037 4a111033 2001-12-29 alex * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
1038 4a111033 2001-12-29 alex * Der Host kann dann nicht aufgeloest werden. */
1039 4a111033 2001-12-29 alex
1040 4a111033 2001-12-29 alex RES_STAT *s;
1041 4a111033 2001-12-29 alex INT pid;
1042 4a111033 2001-12-29 alex
1043 bc140df8 2001-12-29 alex /* Speicher anfordern */
1044 4a111033 2001-12-29 alex s = malloc( sizeof( RES_STAT ));
1045 4a111033 2001-12-29 alex if( ! s )
1046 4a111033 2001-12-29 alex {
1047 79809118 2002-01-06 alex Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
1048 4a111033 2001-12-29 alex return NULL;
1049 4a111033 2001-12-29 alex }
1050 4a111033 2001-12-29 alex
1051 bc140df8 2001-12-29 alex /* Pipe fuer Antwort initialisieren */
1052 b9728ba2 2001-12-29 alex if( pipe( s->pipe ) != 0 )
1053 4a111033 2001-12-29 alex {
1054 4a111033 2001-12-29 alex free( s );
1055 4a111033 2001-12-29 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
1056 4a111033 2001-12-29 alex return NULL;
1057 4a111033 2001-12-29 alex }
1058 4a111033 2001-12-29 alex
1059 bc140df8 2001-12-29 alex /* Sub-Prozess erzeugen */
1060 4a111033 2001-12-29 alex pid = fork( );
1061 4a111033 2001-12-29 alex if( pid > 0 )
1062 4a111033 2001-12-29 alex {
1063 4a111033 2001-12-29 alex /* Haupt-Prozess */
1064 bc140df8 2001-12-29 alex Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
1065 b9728ba2 2001-12-29 alex FD_SET( s->pipe[0], &My_Resolvers );
1066 b9728ba2 2001-12-29 alex if( s->pipe[0] > My_Max_Fd ) My_Max_Fd = s->pipe[0];
1067 4a111033 2001-12-29 alex s->pid = pid;
1068 4a111033 2001-12-29 alex return s;
1069 4a111033 2001-12-29 alex }
1070 4a111033 2001-12-29 alex else if( pid == 0 )
1071 4a111033 2001-12-29 alex {
1072 4a111033 2001-12-29 alex /* Sub-Prozess */
1073 4a111033 2001-12-29 alex Log_Init_Resolver( );
1074 03d971d9 2002-01-02 alex Do_ResolveAddr( Addr, s->pipe[1] );
1075 4a111033 2001-12-29 alex Log_Exit_Resolver( );
1076 4a111033 2001-12-29 alex exit( 0 );
1077 4a111033 2001-12-29 alex }
1078 4a111033 2001-12-29 alex else
1079 4a111033 2001-12-29 alex {
1080 4a111033 2001-12-29 alex /* Fehler */
1081 4a111033 2001-12-29 alex free( s );
1082 79809118 2002-01-06 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
1083 4a111033 2001-12-29 alex return NULL;
1084 4a111033 2001-12-29 alex }
1085 03d971d9 2002-01-02 alex } /* ResolveAddr */
1086 4a111033 2001-12-29 alex
1087 4a111033 2001-12-29 alex
1088 03d971d9 2002-01-02 alex LOCAL RES_STAT *ResolveName( CHAR *Host )
1089 03d971d9 2002-01-02 alex {
1090 03d971d9 2002-01-02 alex /* Hostnamen (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
1091 03d971d9 2002-01-02 alex * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
1092 03d971d9 2002-01-02 alex * Der Host kann dann nicht aufgeloest werden. */
1093 03d971d9 2002-01-02 alex
1094 03d971d9 2002-01-02 alex RES_STAT *s;
1095 03d971d9 2002-01-02 alex INT pid;
1096 03d971d9 2002-01-02 alex
1097 03d971d9 2002-01-02 alex /* Speicher anfordern */
1098 03d971d9 2002-01-02 alex s = malloc( sizeof( RES_STAT ));
1099 03d971d9 2002-01-02 alex if( ! s )
1100 03d971d9 2002-01-02 alex {
1101 79809118 2002-01-06 alex Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
1102 03d971d9 2002-01-02 alex return NULL;
1103 03d971d9 2002-01-02 alex }
1104 03d971d9 2002-01-02 alex
1105 03d971d9 2002-01-02 alex /* Pipe fuer Antwort initialisieren */
1106 03d971d9 2002-01-02 alex if( pipe( s->pipe ) != 0 )
1107 03d971d9 2002-01-02 alex {
1108 03d971d9 2002-01-02 alex free( s );
1109 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
1110 03d971d9 2002-01-02 alex return NULL;
1111 03d971d9 2002-01-02 alex }
1112 03d971d9 2002-01-02 alex
1113 03d971d9 2002-01-02 alex /* Sub-Prozess erzeugen */
1114 03d971d9 2002-01-02 alex pid = fork( );
1115 03d971d9 2002-01-02 alex if( pid > 0 )
1116 03d971d9 2002-01-02 alex {
1117 03d971d9 2002-01-02 alex /* Haupt-Prozess */
1118 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
1119 03d971d9 2002-01-02 alex FD_SET( s->pipe[0], &My_Resolvers );
1120 03d971d9 2002-01-02 alex if( s->pipe[0] > My_Max_Fd ) My_Max_Fd = s->pipe[0];
1121 03d971d9 2002-01-02 alex s->pid = pid;
1122 03d971d9 2002-01-02 alex return s;
1123 03d971d9 2002-01-02 alex }
1124 03d971d9 2002-01-02 alex else if( pid == 0 )
1125 03d971d9 2002-01-02 alex {
1126 03d971d9 2002-01-02 alex /* Sub-Prozess */
1127 03d971d9 2002-01-02 alex Log_Init_Resolver( );
1128 03d971d9 2002-01-02 alex Do_ResolveName( Host, s->pipe[1] );
1129 03d971d9 2002-01-02 alex Log_Exit_Resolver( );
1130 03d971d9 2002-01-02 alex exit( 0 );
1131 03d971d9 2002-01-02 alex }
1132 03d971d9 2002-01-02 alex else
1133 03d971d9 2002-01-02 alex {
1134 03d971d9 2002-01-02 alex /* Fehler */
1135 03d971d9 2002-01-02 alex free( s );
1136 79809118 2002-01-06 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
1137 03d971d9 2002-01-02 alex return NULL;
1138 03d971d9 2002-01-02 alex }
1139 03d971d9 2002-01-02 alex } /* ResolveName */
1140 03d971d9 2002-01-02 alex
1141 03d971d9 2002-01-02 alex
1142 03d971d9 2002-01-02 alex LOCAL VOID Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
1143 03d971d9 2002-01-02 alex {
1144 03d971d9 2002-01-02 alex /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
1145 03d971d9 2002-01-02 alex
1146 03d971d9 2002-01-02 alex CHAR hostname[HOST_LEN];
1147 03d971d9 2002-01-02 alex struct hostent *h;
1148 03d971d9 2002-01-02 alex
1149 03d971d9 2002-01-02 alex Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
1150 03d971d9 2002-01-02 alex
1151 03d971d9 2002-01-02 alex /* Namen aufloesen */
1152 03d971d9 2002-01-02 alex h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
1153 03d971d9 2002-01-02 alex if( h ) strcpy( hostname, h->h_name );
1154 03d971d9 2002-01-02 alex else
1155 03d971d9 2002-01-02 alex {
1156 03d971d9 2002-01-02 alex Log_Resolver( LOG_WARNING, "Can't resolve address %s: code %s!", inet_ntoa( Addr->sin_addr ), Resolv_Error( h_errno ));
1157 03d971d9 2002-01-02 alex strcpy( hostname, inet_ntoa( Addr->sin_addr ));
1158 03d971d9 2002-01-02 alex }
1159 03d971d9 2002-01-02 alex
1160 03d971d9 2002-01-02 alex /* Antwort an Parent schreiben */
1161 03d971d9 2002-01-02 alex if( write( w_fd, hostname, strlen( hostname ) + 1 ) != ( strlen( hostname ) + 1 ))
1162 03d971d9 2002-01-02 alex {
1163 79809118 2002-01-06 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
1164 03d971d9 2002-01-02 alex close( w_fd );
1165 03d971d9 2002-01-02 alex return;
1166 03d971d9 2002-01-02 alex }
1167 03d971d9 2002-01-02 alex
1168 03d971d9 2002-01-02 alex Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
1169 03d971d9 2002-01-02 alex } /* Do_ResolveAddr */
1170 03d971d9 2002-01-02 alex
1171 03d971d9 2002-01-02 alex
1172 03d971d9 2002-01-02 alex LOCAL VOID Do_ResolveName( CHAR *Host, INT w_fd )
1173 03d971d9 2002-01-02 alex {
1174 03d971d9 2002-01-02 alex /* Resolver Sub-Prozess: Name aufloesen und Ergebnis in Pipe schreiben. */
1175 03d971d9 2002-01-02 alex
1176 03d971d9 2002-01-02 alex CHAR ip[16];
1177 03d971d9 2002-01-02 alex struct hostent *h;
1178 03d971d9 2002-01-02 alex struct in_addr *addr;
1179 03d971d9 2002-01-02 alex
1180 03d971d9 2002-01-02 alex Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
1181 03d971d9 2002-01-02 alex
1182 03d971d9 2002-01-02 alex /* Namen aufloesen */
1183 03d971d9 2002-01-02 alex h = gethostbyname( Host );
1184 03d971d9 2002-01-02 alex if( h )
1185 03d971d9 2002-01-02 alex {
1186 03d971d9 2002-01-02 alex addr = (struct in_addr *)h->h_addr;
1187 03d971d9 2002-01-02 alex strcpy( ip, inet_ntoa( *addr ));
1188 03d971d9 2002-01-02 alex }
1189 03d971d9 2002-01-02 alex else
1190 03d971d9 2002-01-02 alex {
1191 03d971d9 2002-01-02 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Resolv_Error( h_errno ));
1192 03d971d9 2002-01-02 alex strcpy( ip, "" );
1193 03d971d9 2002-01-02 alex }
1194 03d971d9 2002-01-02 alex
1195 03d971d9 2002-01-02 alex /* Antwort an Parent schreiben */
1196 03d971d9 2002-01-02 alex if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
1197 03d971d9 2002-01-02 alex {
1198 79809118 2002-01-06 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
1199 03d971d9 2002-01-02 alex close( w_fd );
1200 03d971d9 2002-01-02 alex return;
1201 03d971d9 2002-01-02 alex }
1202 03d971d9 2002-01-02 alex
1203 03d971d9 2002-01-02 alex if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
1204 03d971d9 2002-01-02 alex } /* Do_ResolveName */
1205 03d971d9 2002-01-02 alex
1206 03d971d9 2002-01-02 alex
1207 4a111033 2001-12-29 alex LOCAL VOID Read_Resolver_Result( INT r_fd )
1208 4a111033 2001-12-29 alex {
1209 4a111033 2001-12-29 alex /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1210 03d971d9 2002-01-02 alex * und entsprechende Connection aktualisieren */
1211 4a111033 2001-12-29 alex
1212 03d971d9 2002-01-02 alex CHAR result[HOST_LEN];
1213 4a111033 2001-12-29 alex CLIENT *c;
1214 03d971d9 2002-01-02 alex INT len, i;
1215 4a111033 2001-12-29 alex
1216 4a111033 2001-12-29 alex FD_CLR( r_fd, &My_Resolvers );
1217 4a111033 2001-12-29 alex
1218 4a111033 2001-12-29 alex /* Anfrage vom Parent lesen */
1219 03d971d9 2002-01-02 alex len = read( r_fd, result, HOST_LEN);
1220 03d971d9 2002-01-02 alex if( len < 0 )
1221 4a111033 2001-12-29 alex {
1222 4a111033 2001-12-29 alex /* Fehler beim Lesen aus der Pipe */
1223 4a111033 2001-12-29 alex close( r_fd );
1224 79809118 2002-01-06 alex Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1225 4a111033 2001-12-29 alex return;
1226 4a111033 2001-12-29 alex }
1227 03d971d9 2002-01-02 alex result[len] = '\0';
1228 4a111033 2001-12-29 alex
1229 bc140df8 2001-12-29 alex /* zugehoerige Connection suchen */
1230 4a111033 2001-12-29 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
1231 4a111033 2001-12-29 alex {
1232 03d971d9 2002-01-02 alex if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break;
1233 4a111033 2001-12-29 alex }
1234 4a111033 2001-12-29 alex if( i >= MAX_CONNECTIONS )
1235 4a111033 2001-12-29 alex {
1236 03d971d9 2002-01-02 alex /* Opsa! Keine passende Connection gefunden!? Vermutlich
1237 03d971d9 2002-01-02 alex * wurde sie schon wieder geschlossen. */
1238 4a111033 2001-12-29 alex close( r_fd );
1239 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1240 4a111033 2001-12-29 alex return;
1241 4a111033 2001-12-29 alex }
1242 4a111033 2001-12-29 alex
1243 4a111033 2001-12-29 alex /* Aufraeumen */
1244 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[0] );
1245 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[1] );
1246 4a111033 2001-12-29 alex free( My_Connections[i].res_stat );
1247 4a111033 2001-12-29 alex My_Connections[i].res_stat = NULL;
1248 03d971d9 2002-01-02 alex
1249 03d971d9 2002-01-02 alex if( My_Connections[i].sock > NONE )
1250 03d971d9 2002-01-02 alex {
1251 03d971d9 2002-01-02 alex /* Eingehende Verbindung: Hostnamen setzen */
1252 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1253 54e487d4 2002-01-03 alex assert( c != NULL );
1254 54e487d4 2002-01-03 alex strcpy( My_Connections[i].host, result );
1255 54e487d4 2002-01-03 alex Client_SetHostname( c, result );
1256 03d971d9 2002-01-02 alex }
1257 03d971d9 2002-01-02 alex else
1258 03d971d9 2002-01-02 alex {
1259 03d971d9 2002-01-02 alex /* Ausgehende Verbindung (=Server): IP setzen */
1260 03d971d9 2002-01-02 alex assert( My_Connections[i].our_server >= 0 );
1261 03d971d9 2002-01-02 alex strcpy( Conf_Server[My_Connections[i].our_server].ip, result );
1262 03d971d9 2002-01-02 alex }
1263 4a111033 2001-12-29 alex } /* Read_Resolver_Result */
1264 4a111033 2001-12-29 alex
1265 4a111033 2001-12-29 alex
1266 03d971d9 2002-01-02 alex LOCAL CHAR *Resolv_Error( INT H_Error )
1267 4a111033 2001-12-29 alex {
1268 03d971d9 2002-01-02 alex /* Fehlerbeschreibung fuer H_Error liefern */
1269 4a111033 2001-12-29 alex
1270 03d971d9 2002-01-02 alex switch( H_Error )
1271 4a111033 2001-12-29 alex {
1272 03d971d9 2002-01-02 alex case HOST_NOT_FOUND:
1273 03d971d9 2002-01-02 alex return "host not found";
1274 bcc0cdc3 2002-01-05 alex case NO_DATA:
1275 03d971d9 2002-01-02 alex return "name valid but no IP address defined";
1276 03d971d9 2002-01-02 alex case NO_RECOVERY:
1277 03d971d9 2002-01-02 alex return "name server error";
1278 03d971d9 2002-01-02 alex case TRY_AGAIN:
1279 03d971d9 2002-01-02 alex return "name server temporary not available";
1280 03d971d9 2002-01-02 alex default:
1281 03d971d9 2002-01-02 alex return "unknown error";
1282 4a111033 2001-12-29 alex }
1283 03d971d9 2002-01-02 alex } /* Resolv_Error */
1284 4a111033 2001-12-29 alex
1285 4a111033 2001-12-29 alex
1286 5fefe1a3 2001-12-12 alex /* -eof- */