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 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 5fefe1a3 2001-12-12 alex *
11 490f28ff 2002-12-12 alex * Connection management
12 5fefe1a3 2001-12-12 alex */
13 5fefe1a3 2001-12-12 alex
14 5fefe1a3 2001-12-12 alex
15 b77dae34 2002-12-30 alex #define CONN_MODULE
16 e68cdf30 2002-12-30 alex
17 ca33cbda 2002-03-12 alex #include "portab.h"
18 490f28ff 2002-12-12 alex
19 8dadb17f 2002-12-31 alex static char UNUSED id[] = "$Id: conn.c,v 1.114 2002/12/31 16:13:29 alex Exp $";
20 5fefe1a3 2001-12-12 alex
21 ca33cbda 2002-03-12 alex #include "imp.h"
22 5fefe1a3 2001-12-12 alex #include <assert.h>
23 d5c97f81 2001-12-23 alex #include <stdarg.h>
24 5fefe1a3 2001-12-12 alex #include <stdio.h>
25 b20fa7c6 2002-01-01 alex #include <stdlib.h>
26 5fefe1a3 2001-12-12 alex #include <unistd.h>
27 5fefe1a3 2001-12-12 alex #include <errno.h>
28 5fefe1a3 2001-12-12 alex #include <fcntl.h>
29 5fefe1a3 2001-12-12 alex #include <string.h>
30 9ab186c4 2001-12-25 alex #include <sys/socket.h>
31 5fefe1a3 2001-12-12 alex #include <sys/time.h>
32 9ab186c4 2001-12-25 alex #include <sys/types.h>
33 ba331a2f 2001-12-26 alex #include <time.h>
34 5fefe1a3 2001-12-12 alex #include <netinet/in.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 e68cdf30 2002-12-30 alex #include "defines.h"
47 e68cdf30 2002-12-30 alex #include "resolve.h"
48 65f3adca 2002-11-26 alex
49 c2f60abe 2002-05-27 alex #include "exp.h"
50 c2f60abe 2002-05-27 alex #include "conn.h"
51 c2f60abe 2002-05-27 alex
52 c2f60abe 2002-05-27 alex #include "imp.h"
53 1c8eb478 2001-12-12 alex #include "ngircd.h"
54 d5c97f81 2001-12-23 alex #include "client.h"
55 65bdfdf2 2001-12-26 alex #include "conf.h"
56 e68cdf30 2002-12-30 alex #include "conn-zip.h"
57 b77dae34 2002-12-30 alex #include "conn-func.h"
58 5fefe1a3 2001-12-12 alex #include "log.h"
59 c4199b04 2001-12-21 alex #include "parse.h"
60 1c8eb478 2001-12-12 alex #include "tool.h"
61 5fefe1a3 2001-12-12 alex
62 ca33cbda 2002-03-12 alex #include "exp.h"
63 cf050519 2001-12-14 alex
64 63758dd7 2001-12-15 alex
65 34d54344 2002-03-12 alex #define SERVER_WAIT (NONE - 1)
66 4a111033 2001-12-29 alex
67 4a111033 2001-12-29 alex
68 c2f60abe 2002-05-27 alex LOCAL VOID Handle_Read PARAMS(( INT sock ));
69 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Handle_Write PARAMS(( CONN_ID Idx ));
70 c2f60abe 2002-05-27 alex LOCAL VOID New_Connection PARAMS(( INT Sock ));
71 c2f60abe 2002-05-27 alex LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
72 c2f60abe 2002-05-27 alex LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
73 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
74 5ecb00ed 2002-11-23 alex LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
75 c2f60abe 2002-05-27 alex LOCAL VOID Check_Connections PARAMS(( VOID ));
76 c2f60abe 2002-05-27 alex LOCAL VOID Check_Servers PARAMS(( VOID ));
77 e68cdf30 2002-12-30 alex LOCAL VOID Init_Conn_Struct PARAMS(( CONN_ID Idx ));
78 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock ));
79 c2f60abe 2002-05-27 alex LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx ));
80 c2f60abe 2002-05-27 alex LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd ));
81 1c8eb478 2001-12-12 alex
82 4a111033 2001-12-29 alex LOCAL fd_set My_Listeners;
83 1c8eb478 2001-12-12 alex LOCAL fd_set My_Sockets;
84 6da91c34 2002-03-02 alex LOCAL fd_set My_Connects;
85 1c8eb478 2001-12-12 alex
86 1c8eb478 2001-12-12 alex
87 c2f60abe 2002-05-27 alex GLOBAL VOID
88 c2f60abe 2002-05-27 alex Conn_Init( VOID )
89 5fefe1a3 2001-12-12 alex {
90 bc140df8 2001-12-29 alex /* Modul initialisieren: statische Strukturen "ausnullen". */
91 bc140df8 2001-12-29 alex
92 cf050519 2001-12-14 alex CONN_ID i;
93 1c8eb478 2001-12-12 alex
94 b991de0f 2002-11-02 alex /* Speicher fuer Verbindungs-Pool anfordern */
95 b991de0f 2002-11-02 alex Pool_Size = CONNECTION_POOL;
96 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
97 b991de0f 2002-11-02 alex {
98 b991de0f 2002-11-02 alex /* konfiguriertes Limit beachten */
99 b991de0f 2002-11-02 alex if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
100 b991de0f 2002-11-02 alex }
101 b991de0f 2002-11-02 alex My_Connections = malloc( sizeof( CONNECTION ) * Pool_Size );
102 b991de0f 2002-11-02 alex if( ! My_Connections )
103 b991de0f 2002-11-02 alex {
104 b991de0f 2002-11-02 alex /* Speicher konnte nicht alloziert werden! */
105 b991de0f 2002-11-02 alex Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
106 b991de0f 2002-11-02 alex exit( 1 );
107 b991de0f 2002-11-02 alex }
108 e68cdf30 2002-12-30 alex Log( LOG_DEBUG, "Allocted connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
109 b991de0f 2002-11-02 alex
110 1c8eb478 2001-12-12 alex /* zu Beginn haben wir keine Verbindungen */
111 4a111033 2001-12-29 alex FD_ZERO( &My_Listeners );
112 1c8eb478 2001-12-12 alex FD_ZERO( &My_Sockets );
113 6da91c34 2002-03-02 alex FD_ZERO( &My_Connects );
114 9ab186c4 2001-12-25 alex
115 b991de0f 2002-11-02 alex /* Groesster File-Descriptor fuer select() */
116 c2f60abe 2002-05-27 alex Conn_MaxFD = 0;
117 9ab186c4 2001-12-25 alex
118 1c8eb478 2001-12-12 alex /* Connection-Struktur initialisieren */
119 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
120 a02bf31d 2002-12-18 alex
121 a02bf31d 2002-12-18 alex /* Global write counter */
122 a02bf31d 2002-12-18 alex WCounter = 0;
123 5fefe1a3 2001-12-12 alex } /* Conn_Init */
124 5fefe1a3 2001-12-12 alex
125 5fefe1a3 2001-12-12 alex
126 c2f60abe 2002-05-27 alex GLOBAL VOID
127 c2f60abe 2002-05-27 alex 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 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 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 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; 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 b991de0f 2002-11-02 alex else if( idx < Pool_Size )
156 ae958aa1 2002-06-02 alex {
157 397c5e2e 2002-12-19 alex if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
158 ae958aa1 2002-06-02 alex else Conn_Close( idx, NULL, "Server going down", TRUE );
159 ae958aa1 2002-06-02 alex }
160 1c8eb478 2001-12-12 alex else
161 1c8eb478 2001-12-12 alex {
162 6da91c34 2002-03-02 alex Log( LOG_WARNING, "Closing unknown connection %d ...", i );
163 1c8eb478 2001-12-12 alex close( i );
164 1c8eb478 2001-12-12 alex }
165 1c8eb478 2001-12-12 alex }
166 1c8eb478 2001-12-12 alex }
167 b991de0f 2002-11-02 alex
168 b991de0f 2002-11-02 alex free( My_Connections );
169 b991de0f 2002-11-02 alex My_Connections = NULL;
170 b991de0f 2002-11-02 alex Pool_Size = 0;
171 5fefe1a3 2001-12-12 alex } /* Conn_Exit */
172 5fefe1a3 2001-12-12 alex
173 5fefe1a3 2001-12-12 alex
174 a0c032b2 2002-11-22 alex GLOBAL INT
175 a0c032b2 2002-11-22 alex Conn_InitListeners( VOID )
176 a0c032b2 2002-11-22 alex {
177 e68cdf30 2002-12-30 alex /* Initialize ports on which the server should accept connections */
178 a0c032b2 2002-11-22 alex
179 a0c032b2 2002-11-22 alex INT created, i;
180 a0c032b2 2002-11-22 alex
181 a0c032b2 2002-11-22 alex created = 0;
182 a0c032b2 2002-11-22 alex for( i = 0; i < Conf_ListenPorts_Count; i++ )
183 a0c032b2 2002-11-22 alex {
184 a0c032b2 2002-11-22 alex if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
185 a0c032b2 2002-11-22 alex else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
186 a0c032b2 2002-11-22 alex }
187 a0c032b2 2002-11-22 alex return created;
188 a0c032b2 2002-11-22 alex } /* Conn_InitListeners */
189 a0c032b2 2002-11-22 alex
190 a0c032b2 2002-11-22 alex
191 a0c032b2 2002-11-22 alex GLOBAL VOID
192 a0c032b2 2002-11-22 alex Conn_ExitListeners( VOID )
193 a0c032b2 2002-11-22 alex {
194 e68cdf30 2002-12-30 alex /* Close down all listening sockets */
195 a0c032b2 2002-11-22 alex
196 a0c032b2 2002-11-22 alex INT i;
197 a0c032b2 2002-11-22 alex
198 a0c032b2 2002-11-22 alex Log( LOG_INFO, "Shutting down all listening sockets ..." );
199 a0c032b2 2002-11-22 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
200 a0c032b2 2002-11-22 alex {
201 a0c032b2 2002-11-22 alex if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
202 a0c032b2 2002-11-22 alex {
203 a0c032b2 2002-11-22 alex close( i );
204 a0c032b2 2002-11-22 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
205 a0c032b2 2002-11-22 alex }
206 a0c032b2 2002-11-22 alex }
207 a0c032b2 2002-11-22 alex } /* Conn_ExitListeners */
208 a0c032b2 2002-11-22 alex
209 a0c032b2 2002-11-22 alex
210 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
211 c2f60abe 2002-05-27 alex Conn_NewListener( CONST UINT Port )
212 5fefe1a3 2001-12-12 alex {
213 e68cdf30 2002-12-30 alex /* Create new listening socket on specified port */
214 5fefe1a3 2001-12-12 alex
215 1c8eb478 2001-12-12 alex struct sockaddr_in addr;
216 6da91c34 2002-03-02 alex INT sock;
217 1c8eb478 2001-12-12 alex
218 5fefe1a3 2001-12-12 alex /* Server-"Listen"-Socket initialisieren */
219 1c8eb478 2001-12-12 alex memset( &addr, 0, sizeof( addr ));
220 1c8eb478 2001-12-12 alex addr.sin_family = AF_INET;
221 1c8eb478 2001-12-12 alex addr.sin_port = htons( Port );
222 1c8eb478 2001-12-12 alex addr.sin_addr.s_addr = htonl( INADDR_ANY );
223 5fefe1a3 2001-12-12 alex
224 5fefe1a3 2001-12-12 alex /* Socket erzeugen */
225 1c8eb478 2001-12-12 alex sock = socket( PF_INET, SOCK_STREAM, 0);
226 fb55c443 2001-12-13 alex if( sock < 0 )
227 5fefe1a3 2001-12-12 alex {
228 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
229 5fefe1a3 2001-12-12 alex return FALSE;
230 5fefe1a3 2001-12-12 alex }
231 5fefe1a3 2001-12-12 alex
232 6da91c34 2002-03-02 alex if( ! Init_Socket( sock )) return FALSE;
233 1c8eb478 2001-12-12 alex
234 5fefe1a3 2001-12-12 alex /* an Port binden */
235 1c8eb478 2001-12-12 alex if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
236 5fefe1a3 2001-12-12 alex {
237 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
238 1c8eb478 2001-12-12 alex close( sock );
239 5fefe1a3 2001-12-12 alex return FALSE;
240 5fefe1a3 2001-12-12 alex }
241 5fefe1a3 2001-12-12 alex
242 5fefe1a3 2001-12-12 alex /* in "listen mode" gehen :-) */
243 1c8eb478 2001-12-12 alex if( listen( sock, 10 ) != 0 )
244 5fefe1a3 2001-12-12 alex {
245 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
246 1c8eb478 2001-12-12 alex close( sock );
247 5fefe1a3 2001-12-12 alex return FALSE;
248 5fefe1a3 2001-12-12 alex }
249 1c8eb478 2001-12-12 alex
250 1c8eb478 2001-12-12 alex /* Neuen Listener in Strukturen einfuegen */
251 4a111033 2001-12-29 alex FD_SET( sock, &My_Listeners );
252 1c8eb478 2001-12-12 alex FD_SET( sock, &My_Sockets );
253 9ab186c4 2001-12-25 alex
254 c2f60abe 2002-05-27 alex if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
255 1c8eb478 2001-12-12 alex
256 9856253d 2001-12-30 alex Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
257 1c8eb478 2001-12-12 alex
258 5fefe1a3 2001-12-12 alex return TRUE;
259 08cf5607 2001-12-26 alex } /* Conn_NewListener */
260 5fefe1a3 2001-12-12 alex
261 5fefe1a3 2001-12-12 alex
262 c2f60abe 2002-05-27 alex GLOBAL VOID
263 ae958aa1 2002-06-02 alex Conn_Handler( VOID )
264 5fefe1a3 2001-12-12 alex {
265 6c19b0e4 2002-09-07 alex /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
266 6c19b0e4 2002-09-07 alex * werden dabei durchgefuehrt, bis der Server terminieren oder neu
267 6c19b0e4 2002-09-07 alex * starten soll:
268 6c19b0e4 2002-09-07 alex *
269 bc140df8 2001-12-29 alex * - neue Verbindungen annehmen,
270 03d971d9 2002-01-02 alex * - Server-Verbindungen aufbauen,
271 bc140df8 2001-12-29 alex * - geschlossene Verbindungen loeschen,
272 bc140df8 2001-12-29 alex * - volle Schreibpuffer versuchen zu schreiben,
273 bc140df8 2001-12-29 alex * - volle Lesepuffer versuchen zu verarbeiten,
274 bc140df8 2001-12-29 alex * - Antworten von Resolver Sub-Prozessen annehmen.
275 bc140df8 2001-12-29 alex */
276 bc140df8 2001-12-29 alex
277 63758dd7 2001-12-15 alex fd_set read_sockets, write_sockets;
278 747fd2f0 2001-12-13 alex struct timeval tv;
279 7b6cfc17 2002-08-26 alex time_t start, t;
280 e68cdf30 2002-12-30 alex CONN_ID i, idx;
281 5ecb00ed 2002-11-23 alex BOOLEAN timeout;
282 747fd2f0 2001-12-13 alex
283 9ab186c4 2001-12-25 alex start = time( NULL );
284 397c5e2e 2002-12-19 alex while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart ))
285 63758dd7 2001-12-15 alex {
286 5ecb00ed 2002-11-23 alex timeout = TRUE;
287 397c5e2e 2002-12-19 alex
288 397c5e2e 2002-12-19 alex /* Should the configuration be reloaded? */
289 397c5e2e 2002-12-19 alex if( NGIRCd_SignalRehash ) NGIRCd_Rehash( );
290 03d971d9 2002-01-02 alex
291 397c5e2e 2002-12-19 alex /* Check configured servers and established links */
292 397c5e2e 2002-12-19 alex Check_Servers( );
293 65bdfdf2 2001-12-26 alex Check_Connections( );
294 65bdfdf2 2001-12-26 alex
295 65bdfdf2 2001-12-26 alex /* noch volle Lese-Buffer suchen */
296 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
297 63758dd7 2001-12-15 alex {
298 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
299 9ab186c4 2001-12-25 alex {
300 65bdfdf2 2001-12-26 alex /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
301 5ecb00ed 2002-11-23 alex if( Handle_Buffer( i )) timeout = FALSE;
302 9ab186c4 2001-12-25 alex }
303 63758dd7 2001-12-15 alex }
304 590f2a3f 2002-03-11 alex
305 65bdfdf2 2001-12-26 alex /* noch volle Schreib-Puffer suchen */
306 65bdfdf2 2001-12-26 alex FD_ZERO( &write_sockets );
307 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
308 9ab186c4 2001-12-25 alex {
309 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
310 65f3adca 2002-11-26 alex if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
311 65f3adca 2002-11-26 alex #else
312 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
313 65f3adca 2002-11-26 alex #endif
314 9ab186c4 2001-12-25 alex {
315 65bdfdf2 2001-12-26 alex /* Socket der Verbindung in Set aufnehmen */
316 65bdfdf2 2001-12-26 alex FD_SET( My_Connections[i].sock, &write_sockets );
317 9ab186c4 2001-12-25 alex }
318 6da91c34 2002-03-02 alex }
319 e68cdf30 2002-12-30 alex
320 6da91c34 2002-03-02 alex /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
321 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
322 6da91c34 2002-03-02 alex {
323 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 );
324 9ab186c4 2001-12-25 alex }
325 4a111033 2001-12-29 alex
326 4a111033 2001-12-29 alex /* von welchen Sockets koennte gelesen werden? */
327 7b6cfc17 2002-08-26 alex t = time( NULL );
328 9ab186c4 2001-12-25 alex read_sockets = My_Sockets;
329 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
330 4a111033 2001-12-29 alex {
331 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
332 4a111033 2001-12-29 alex {
333 4a111033 2001-12-29 alex /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
334 6da91c34 2002-03-02 alex FD_CLR( My_Connections[i].sock, &read_sockets );
335 6da91c34 2002-03-02 alex }
336 6da91c34 2002-03-02 alex if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
337 6da91c34 2002-03-02 alex {
338 6da91c34 2002-03-02 alex /* Hier laeuft noch ein asyncrones connect() */
339 4a111033 2001-12-29 alex FD_CLR( My_Connections[i].sock, &read_sockets );
340 4a111033 2001-12-29 alex }
341 7b6cfc17 2002-08-26 alex if( My_Connections[i].delaytime > t )
342 7b6cfc17 2002-08-26 alex {
343 7b6cfc17 2002-08-26 alex /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
344 7b6cfc17 2002-08-26 alex FD_CLR( My_Connections[i].sock, &read_sockets );
345 7b6cfc17 2002-08-26 alex }
346 4a111033 2001-12-29 alex }
347 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
348 4a111033 2001-12-29 alex {
349 4a111033 2001-12-29 alex /* Pipes von Resolver Sub-Prozessen aufnehmen */
350 c2f60abe 2002-05-27 alex if( FD_ISSET( i, &Resolver_FDs ))
351 4a111033 2001-12-29 alex {
352 4a111033 2001-12-29 alex FD_SET( i, &read_sockets );
353 4a111033 2001-12-29 alex }
354 4a111033 2001-12-29 alex }
355 4a111033 2001-12-29 alex
356 6c19b0e4 2002-09-07 alex /* Timeout initialisieren */
357 5ecb00ed 2002-11-23 alex tv.tv_usec = 0;
358 5ecb00ed 2002-11-23 alex if( timeout ) tv.tv_sec = TIME_RES;
359 5ecb00ed 2002-11-23 alex else tv.tv_sec = 0;
360 6c19b0e4 2002-09-07 alex
361 4a111033 2001-12-29 alex /* Auf Aktivitaet warten */
362 8a927a1b 2002-09-07 alex i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
363 8a927a1b 2002-09-07 alex if( i == 0 )
364 8a927a1b 2002-09-07 alex {
365 8a927a1b 2002-09-07 alex /* keine Veraenderung an den Sockets */
366 8a927a1b 2002-09-07 alex continue;
367 8a927a1b 2002-09-07 alex }
368 8a927a1b 2002-09-07 alex if( i == -1 )
369 9ab186c4 2001-12-25 alex {
370 8a927a1b 2002-09-07 alex /* Fehler (z.B. Interrupt) */
371 db58d347 2002-01-05 alex if( errno != EINTR )
372 db58d347 2002-01-05 alex {
373 b840be98 2002-10-15 alex Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
374 f7327524 2002-05-30 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
375 db58d347 2002-01-05 alex exit( 1 );
376 db58d347 2002-01-05 alex }
377 8a927a1b 2002-09-07 alex continue;
378 9ab186c4 2001-12-25 alex }
379 9ab186c4 2001-12-25 alex
380 9ab186c4 2001-12-25 alex /* Koennen Daten geschrieben werden? */
381 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
382 9ab186c4 2001-12-25 alex {
383 3cf845fb 2002-10-14 alex if( ! FD_ISSET( i, &write_sockets )) continue;
384 b840be98 2002-10-15 alex
385 3cf845fb 2002-10-14 alex /* Es kann geschrieben werden ... */
386 3cf845fb 2002-10-14 alex idx = Socket2Index( i );
387 b840be98 2002-10-15 alex if( idx == NONE ) continue;
388 3cf845fb 2002-10-14 alex
389 3cf845fb 2002-10-14 alex if( ! Handle_Write( idx ))
390 3cf845fb 2002-10-14 alex {
391 3cf845fb 2002-10-14 alex /* Fehler beim Schreiben! Diesen Socket nun
392 3cf845fb 2002-10-14 alex * auch aus dem Read-Set entfernen: */
393 3cf845fb 2002-10-14 alex FD_CLR( i, &read_sockets );
394 3cf845fb 2002-10-14 alex }
395 9ab186c4 2001-12-25 alex }
396 9ab186c4 2001-12-25 alex
397 9ab186c4 2001-12-25 alex /* Daten zum Lesen vorhanden? */
398 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
399 9ab186c4 2001-12-25 alex {
400 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
401 9ab186c4 2001-12-25 alex }
402 63758dd7 2001-12-15 alex }
403 397c5e2e 2002-12-19 alex
404 397c5e2e 2002-12-19 alex if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
405 397c5e2e 2002-12-19 alex else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
406 1c8eb478 2001-12-12 alex } /* Conn_Handler */
407 1c8eb478 2001-12-12 alex
408 1c8eb478 2001-12-12 alex
409 f7327524 2002-05-30 alex #ifdef PROTOTYPES
410 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
411 c2f60abe 2002-05-27 alex Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
412 f7327524 2002-05-30 alex #else
413 f7327524 2002-05-30 alex GLOBAL BOOLEAN
414 f7327524 2002-05-30 alex Conn_WriteStr( Idx, Format, va_alist )
415 f7327524 2002-05-30 alex CONN_ID Idx;
416 f7327524 2002-05-30 alex CHAR *Format;
417 f7327524 2002-05-30 alex va_dcl
418 f7327524 2002-05-30 alex #endif
419 63758dd7 2001-12-15 alex {
420 63758dd7 2001-12-15 alex /* String in Socket schreiben. CR+LF wird von dieser Funktion
421 d5c97f81 2001-12-23 alex * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
422 d5c97f81 2001-12-23 alex * getrennt und FALSE geliefert. */
423 9ab186c4 2001-12-25 alex
424 804b1ec4 2001-12-31 alex CHAR buffer[COMMAND_LEN];
425 d5c97f81 2001-12-23 alex BOOLEAN ok;
426 d5c97f81 2001-12-23 alex va_list ap;
427 d5c97f81 2001-12-23 alex
428 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
429 28c5a21f 2002-03-14 alex assert( Format != NULL );
430 f7327524 2002-05-30 alex
431 f7327524 2002-05-30 alex #ifdef PROTOTYPES
432 d5c97f81 2001-12-23 alex va_start( ap, Format );
433 f7327524 2002-05-30 alex #else
434 f7327524 2002-05-30 alex va_start( ap );
435 f7327524 2002-05-30 alex #endif
436 804b1ec4 2001-12-31 alex if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
437 63758dd7 2001-12-15 alex {
438 79809118 2002-01-06 alex Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
439 79809118 2002-01-06 alex Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
440 63758dd7 2001-12-15 alex return FALSE;
441 63758dd7 2001-12-15 alex }
442 d5c97f81 2001-12-23 alex
443 7c91951d 2001-12-25 alex #ifdef SNIFFER
444 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
445 d5c97f81 2001-12-23 alex #endif
446 9ab186c4 2001-12-25 alex
447 6626395c 2002-12-26 alex strlcat( buffer, "\r\n", sizeof( buffer ));
448 446df061 2001-12-24 alex ok = Conn_Write( Idx, buffer, strlen( buffer ));
449 39219230 2002-12-02 alex My_Connections[Idx].msg_out++;
450 446df061 2001-12-24 alex
451 d5c97f81 2001-12-23 alex va_end( ap );
452 d5c97f81 2001-12-23 alex return ok;
453 63758dd7 2001-12-15 alex } /* Conn_WriteStr */
454 63758dd7 2001-12-15 alex
455 63758dd7 2001-12-15 alex
456 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
457 c2f60abe 2002-05-27 alex Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
458 63758dd7 2001-12-15 alex {
459 63758dd7 2001-12-15 alex /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
460 63758dd7 2001-12-15 alex * der Client disconnectiert und FALSE geliefert. */
461 9ab186c4 2001-12-25 alex
462 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
463 63758dd7 2001-12-15 alex assert( Data != NULL );
464 63758dd7 2001-12-15 alex assert( Len > 0 );
465 63758dd7 2001-12-15 alex
466 65f3adca 2002-11-26 alex /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
467 65f3adca 2002-11-26 alex * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
468 65f3adca 2002-11-26 alex * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
469 65f3adca 2002-11-26 alex * In diesem Fall wird hier einfach ein Fehler geliefert. */
470 67d79b92 2002-09-26 alex if( My_Connections[Idx].sock <= NONE )
471 c50ecda7 2002-09-26 alex {
472 c50ecda7 2002-09-26 alex Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
473 c50ecda7 2002-09-26 alex return FALSE;
474 63758dd7 2001-12-15 alex }
475 9ab186c4 2001-12-25 alex
476 65f3adca 2002-11-26 alex /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
477 65f3adca 2002-11-26 alex * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
478 65f3adca 2002-11-26 alex * Socket zu schreiben (u.a. wg. Komprimierung). */
479 63758dd7 2001-12-15 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
480 63758dd7 2001-12-15 alex {
481 65f3adca 2002-11-26 alex /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
482 65f3adca 2002-11-26 alex * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
483 65f3adca 2002-11-26 alex if( ! Try_Write( Idx )) return FALSE;
484 65f3adca 2002-11-26 alex
485 65f3adca 2002-11-26 alex /* nun neu pruefen: */
486 65f3adca 2002-11-26 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
487 65f3adca 2002-11-26 alex {
488 65f3adca 2002-11-26 alex Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
489 65f3adca 2002-11-26 alex Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
490 65f3adca 2002-11-26 alex return FALSE;
491 65f3adca 2002-11-26 alex }
492 63758dd7 2001-12-15 alex }
493 63758dd7 2001-12-15 alex
494 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
495 65f3adca 2002-11-26 alex if( My_Connections[Idx].options & CONN_ZIP )
496 63758dd7 2001-12-15 alex {
497 65f3adca 2002-11-26 alex /* Daten komprimieren und in Puffer kopieren */
498 65f3adca 2002-11-26 alex if( ! Zip_Buffer( Idx, Data, Len )) return FALSE;
499 63758dd7 2001-12-15 alex }
500 65f3adca 2002-11-26 alex else
501 65f3adca 2002-11-26 alex #endif
502 65f3adca 2002-11-26 alex {
503 65f3adca 2002-11-26 alex /* Daten in Puffer kopieren */
504 65f3adca 2002-11-26 alex memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
505 65f3adca 2002-11-26 alex My_Connections[Idx].wdatalen += Len;
506 65f3adca 2002-11-26 alex My_Connections[Idx].bytes_out += Len;
507 65f3adca 2002-11-26 alex }
508 9ab186c4 2001-12-25 alex
509 a02bf31d 2002-12-18 alex /* Adjust global write counter */
510 a02bf31d 2002-12-18 alex WCounter += Len;
511 a02bf31d 2002-12-18 alex
512 63758dd7 2001-12-15 alex return TRUE;
513 63758dd7 2001-12-15 alex } /* Conn_Write */
514 7c91951d 2001-12-25 alex
515 7c91951d 2001-12-25 alex
516 c2f60abe 2002-05-27 alex GLOBAL VOID
517 c2f60abe 2002-05-27 alex Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
518 7c91951d 2001-12-25 alex {
519 e21b9d84 2002-12-27 alex /* Close connection. Open pipes of asyncronous resolver
520 e21b9d84 2002-12-27 alex * sub-processes are closed down. */
521 7c91951d 2001-12-25 alex
522 03d971d9 2002-01-02 alex CLIENT *c;
523 65f3adca 2002-11-26 alex DOUBLE in_k, out_k;
524 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
525 65f3adca 2002-11-26 alex DOUBLE in_z_k, out_z_k;
526 65f3adca 2002-11-26 alex INT in_p, out_p;
527 65f3adca 2002-11-26 alex #endif
528 590f2a3f 2002-03-11 alex
529 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
530 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
531 f060db5e 2002-10-09 alex
532 e21b9d84 2002-12-27 alex /* Search client, if any */
533 f060db5e 2002-10-09 alex c = Client_GetFromConn( Idx );
534 7c91951d 2001-12-25 alex
535 e21b9d84 2002-12-27 alex /* Should the client be informed? */
536 79809118 2002-01-06 alex if( InformClient )
537 03d971d9 2002-01-02 alex {
538 1d8cb34b 2002-11-29 alex #ifndef STRICT_RFC
539 e21b9d84 2002-12-27 alex /* Send statistics to client if registered as user: */
540 f060db5e 2002-10-09 alex if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
541 f060db5e 2002-10-09 alex {
542 5b25c8cc 2002-10-22 alex Conn_WriteStr( Idx, "NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.", Client_ThisServer( ), NOTICE_TXTPREFIX, (DOUBLE)My_Connections[Idx].bytes_in / 1024, (DOUBLE)My_Connections[Idx].bytes_out / 1024 );
543 f060db5e 2002-10-09 alex }
544 1d8cb34b 2002-11-29 alex #endif
545 f060db5e 2002-10-09 alex
546 e21b9d84 2002-12-27 alex /* Send ERROR to client (see RFC!) */
547 79809118 2002-01-06 alex if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
548 79809118 2002-01-06 alex else Conn_WriteStr( Idx, "ERROR :Closing connection." );
549 03d971d9 2002-01-02 alex if( My_Connections[Idx].sock == NONE ) return;
550 03d971d9 2002-01-02 alex }
551 93a39e87 2002-11-28 alex
552 e21b9d84 2002-12-27 alex /* Try to write out the write buffer */
553 93a39e87 2002-11-28 alex Try_Write( Idx );
554 7c91951d 2001-12-25 alex
555 e21b9d84 2002-12-27 alex /* Shut down socket */
556 7c91951d 2001-12-25 alex if( close( My_Connections[Idx].sock ) != 0 )
557 7c91951d 2001-12-25 alex {
558 e21b9d84 2002-12-27 alex /* Oops, we can't close the socket!? This is fatal! */
559 e21b9d84 2002-12-27 alex Log( LOG_EMERG, "Error closing connection %d (socket %d) with %s:%d - %s!", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
560 e21b9d84 2002-12-27 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
561 e21b9d84 2002-12-27 alex exit( 1 );
562 7c91951d 2001-12-25 alex }
563 e21b9d84 2002-12-27 alex
564 e21b9d84 2002-12-27 alex /* Mark socket as invalid: */
565 3cf845fb 2002-10-14 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
566 3cf845fb 2002-10-14 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
567 3cf845fb 2002-10-14 alex My_Connections[Idx].sock = NONE;
568 7c91951d 2001-12-25 alex
569 e21b9d84 2002-12-27 alex /* If there is still a client, unregister it now */
570 50ec7a56 2002-03-11 alex if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
571 7c91951d 2001-12-25 alex
572 e21b9d84 2002-12-27 alex /* Calculate statistics and log information */
573 e21b9d84 2002-12-27 alex in_k = (DOUBLE)My_Connections[Idx].bytes_in / 1024;
574 e21b9d84 2002-12-27 alex out_k = (DOUBLE)My_Connections[Idx].bytes_out / 1024;
575 e21b9d84 2002-12-27 alex #ifdef USE_ZLIB
576 e21b9d84 2002-12-27 alex if( My_Connections[Idx].options & CONN_ZIP )
577 e21b9d84 2002-12-27 alex {
578 e21b9d84 2002-12-27 alex in_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_in / 1024;
579 e21b9d84 2002-12-27 alex out_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_out / 1024;
580 e21b9d84 2002-12-27 alex in_p = (INT)(( in_k * 100 ) / in_z_k );
581 e21b9d84 2002-12-27 alex out_p = (INT)(( out_k * 100 ) / out_z_k );
582 e21b9d84 2002-12-27 alex Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, in_z_k, in_p, out_k, out_z_k, out_p );
583 e21b9d84 2002-12-27 alex }
584 e21b9d84 2002-12-27 alex else
585 e21b9d84 2002-12-27 alex #endif
586 e21b9d84 2002-12-27 alex {
587 e21b9d84 2002-12-27 alex Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, out_k );
588 e21b9d84 2002-12-27 alex }
589 e21b9d84 2002-12-27 alex
590 e21b9d84 2002-12-27 alex /* Is there a resolver sub-process running? */
591 4a111033 2001-12-29 alex if( My_Connections[Idx].res_stat )
592 4a111033 2001-12-29 alex {
593 e21b9d84 2002-12-27 alex /* Free resolver structures */
594 c2f60abe 2002-05-27 alex FD_CLR( My_Connections[Idx].res_stat->pipe[0], &Resolver_FDs );
595 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[0] );
596 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[1] );
597 4a111033 2001-12-29 alex free( My_Connections[Idx].res_stat );
598 4a111033 2001-12-29 alex }
599 03d971d9 2002-01-02 alex
600 e21b9d84 2002-12-27 alex /* Servers: Modify time of next connect attempt? */
601 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
602 03d971d9 2002-01-02 alex
603 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
604 e21b9d84 2002-12-27 alex /* Clean up zlib, if link was compressed */
605 65f3adca 2002-11-26 alex if( Conn_Options( Idx ) & CONN_ZIP )
606 65f3adca 2002-11-26 alex {
607 65f3adca 2002-11-26 alex inflateEnd( &My_Connections[Idx].zip.in );
608 65f3adca 2002-11-26 alex deflateEnd( &My_Connections[Idx].zip.out );
609 65f3adca 2002-11-26 alex }
610 65f3adca 2002-11-26 alex #endif
611 65f3adca 2002-11-26 alex
612 e21b9d84 2002-12-27 alex /* Clean up connection structure (=free it) */
613 3cf845fb 2002-10-14 alex Init_Conn_Struct( Idx );
614 7c91951d 2001-12-25 alex } /* Conn_Close */
615 b991de0f 2002-11-02 alex
616 b991de0f 2002-11-02 alex
617 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
618 c2f60abe 2002-05-27 alex Try_Write( CONN_ID Idx )
619 63758dd7 2001-12-15 alex {
620 93a39e87 2002-11-28 alex /* Versuchen, Daten aus dem Schreib-Puffer in den Socket zu
621 93a39e87 2002-11-28 alex * schreiben. TRUE wird geliefert, wenn entweder keine Daten
622 93a39e87 2002-11-28 alex * zum Versenden vorhanden sind oder erfolgreich bearbeitet
623 93a39e87 2002-11-28 alex * werden konnten. Im Fehlerfall wird FALSE geliefert und
624 93a39e87 2002-11-28 alex * die Verbindung geschlossen. */
625 63758dd7 2001-12-15 alex
626 63758dd7 2001-12-15 alex fd_set write_socket;
627 22b7b079 2002-11-23 alex struct timeval tv;
628 9ab186c4 2001-12-25 alex
629 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
630 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
631 93a39e87 2002-11-28 alex
632 93a39e87 2002-11-28 alex /* sind ueberhaupt Daten vorhanden? */
633 93a39e87 2002-11-28 alex #ifdef USE_ZLIB
634 93a39e87 2002-11-28 alex if(( ! My_Connections[Idx].wdatalen > 0 ) && ( ! My_Connections[Idx].zip.wdatalen )) return TRUE;
635 93a39e87 2002-11-28 alex #else
636 93a39e87 2002-11-28 alex if( ! My_Connections[Idx].wdatalen > 0 ) return TRUE;
637 93a39e87 2002-11-28 alex #endif
638 63758dd7 2001-12-15 alex
639 22b7b079 2002-11-23 alex /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
640 22b7b079 2002-11-23 alex tv.tv_sec = 0; tv.tv_usec = 0;
641 22b7b079 2002-11-23 alex
642 63758dd7 2001-12-15 alex FD_ZERO( &write_socket );
643 63758dd7 2001-12-15 alex FD_SET( My_Connections[Idx].sock, &write_socket );
644 22b7b079 2002-11-23 alex if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
645 63758dd7 2001-12-15 alex {
646 63758dd7 2001-12-15 alex /* Fehler! */
647 63758dd7 2001-12-15 alex if( errno != EINTR )
648 63758dd7 2001-12-15 alex {
649 b840be98 2002-10-15 alex Log( LOG_ALERT, "Try_Write(): select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
650 79809118 2002-01-06 alex Conn_Close( Idx, "Server error!", NULL, FALSE );
651 63758dd7 2001-12-15 alex return FALSE;
652 63758dd7 2001-12-15 alex }
653 63758dd7 2001-12-15 alex }
654 63758dd7 2001-12-15 alex
655 63758dd7 2001-12-15 alex if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
656 63758dd7 2001-12-15 alex else return TRUE;
657 63758dd7 2001-12-15 alex } /* Try_Write */
658 63758dd7 2001-12-15 alex
659 63758dd7 2001-12-15 alex
660 c2f60abe 2002-05-27 alex LOCAL VOID
661 c2f60abe 2002-05-27 alex Handle_Read( INT Sock )
662 1c8eb478 2001-12-12 alex {
663 bc140df8 2001-12-29 alex /* Aktivitaet auf einem Socket verarbeiten:
664 bc140df8 2001-12-29 alex * - neue Clients annehmen,
665 bc140df8 2001-12-29 alex * - Daten von Clients verarbeiten,
666 bc140df8 2001-12-29 alex * - Resolver-Rueckmeldungen annehmen. */
667 1c8eb478 2001-12-12 alex
668 cf050519 2001-12-14 alex CONN_ID idx;
669 9ab186c4 2001-12-25 alex
670 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
671 590f2a3f 2002-03-11 alex
672 4a111033 2001-12-29 alex if( FD_ISSET( Sock, &My_Listeners ))
673 1c8eb478 2001-12-12 alex {
674 1c8eb478 2001-12-12 alex /* es ist einer unserer Listener-Sockets: es soll
675 1c8eb478 2001-12-12 alex * also eine neue Verbindung aufgebaut werden. */
676 1c8eb478 2001-12-12 alex
677 1c8eb478 2001-12-12 alex New_Connection( Sock );
678 1c8eb478 2001-12-12 alex }
679 c2f60abe 2002-05-27 alex else if( FD_ISSET( Sock, &Resolver_FDs ))
680 4a111033 2001-12-29 alex {
681 4a111033 2001-12-29 alex /* Rueckmeldung von einem Resolver Sub-Prozess */
682 4a111033 2001-12-29 alex
683 4a111033 2001-12-29 alex Read_Resolver_Result( Sock );
684 4a111033 2001-12-29 alex }
685 1c8eb478 2001-12-12 alex else
686 1c8eb478 2001-12-12 alex {
687 1c8eb478 2001-12-12 alex /* Ein Client Socket: entweder ein User oder Server */
688 9ab186c4 2001-12-25 alex
689 1c8eb478 2001-12-12 alex idx = Socket2Index( Sock );
690 3cf845fb 2002-10-14 alex if( idx > NONE ) Read_Request( idx );
691 1c8eb478 2001-12-12 alex }
692 63758dd7 2001-12-15 alex } /* Handle_Read */
693 1c8eb478 2001-12-12 alex
694 1c8eb478 2001-12-12 alex
695 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
696 c2f60abe 2002-05-27 alex Handle_Write( CONN_ID Idx )
697 63758dd7 2001-12-15 alex {
698 6da91c34 2002-03-02 alex /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
699 9ab186c4 2001-12-25 alex
700 6da91c34 2002-03-02 alex INT len, res, err;
701 63758dd7 2001-12-15 alex
702 b840be98 2002-10-15 alex assert( Idx > NONE );
703 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
704 6da91c34 2002-03-02 alex
705 6da91c34 2002-03-02 alex if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
706 6da91c34 2002-03-02 alex {
707 6da91c34 2002-03-02 alex /* es soll nichts geschrieben werden, sondern ein
708 6da91c34 2002-03-02 alex * connect() hat ein Ergebnis geliefert */
709 6da91c34 2002-03-02 alex
710 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
711 6da91c34 2002-03-02 alex
712 6da91c34 2002-03-02 alex /* Ergebnis des connect() ermitteln */
713 6da91c34 2002-03-02 alex len = sizeof( err );
714 6da91c34 2002-03-02 alex res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
715 6da91c34 2002-03-02 alex assert( len == sizeof( err ));
716 6da91c34 2002-03-02 alex
717 6da91c34 2002-03-02 alex /* Fehler aufgetreten? */
718 6da91c34 2002-03-02 alex if(( res != 0 ) || ( err != 0 ))
719 6da91c34 2002-03-02 alex {
720 6da91c34 2002-03-02 alex /* Fehler! */
721 6da91c34 2002-03-02 alex if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
722 a2544e49 2002-12-30 alex else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port, Idx, strerror( err ));
723 6da91c34 2002-03-02 alex
724 6da91c34 2002-03-02 alex /* Socket etc. pp. aufraeumen */
725 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
726 6da91c34 2002-03-02 alex close( My_Connections[Idx].sock );
727 6da91c34 2002-03-02 alex Init_Conn_Struct( Idx );
728 cbce54e0 2002-03-02 alex
729 cbce54e0 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
730 a2544e49 2002-12-30 alex Conf_Server[Conf_GetServer( Idx )].lasttry = time( NULL );
731 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
732 cbce54e0 2002-03-02 alex
733 6da91c34 2002-03-02 alex return FALSE;
734 6da91c34 2002-03-02 alex }
735 a2544e49 2002-12-30 alex Log( LOG_DEBUG, "Connection %d with \"%s:%d\" established, now sendig PASS and SERVER ...", Idx, My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port );
736 6da91c34 2002-03-02 alex
737 6da91c34 2002-03-02 alex /* PASS und SERVER verschicken */
738 a2544e49 2002-12-30 alex Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[Conf_GetServer( Idx )].pwd_out, NGIRCd_ProtoID );
739 3cf845fb 2002-10-14 alex return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
740 6da91c34 2002-03-02 alex }
741 6da91c34 2002-03-02 alex
742 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
743 65f3adca 2002-11-26 alex /* Schreibpuffer leer, aber noch Daten im Kompressionsbuffer?
744 65f3adca 2002-11-26 alex * Dann muss dieser nun geflushed werden! */
745 65f3adca 2002-11-26 alex if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
746 65f3adca 2002-11-26 alex #endif
747 65f3adca 2002-11-26 alex
748 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
749 9ab186c4 2001-12-25 alex
750 63758dd7 2001-12-15 alex /* Daten schreiben */
751 63758dd7 2001-12-15 alex len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
752 63758dd7 2001-12-15 alex if( len < 0 )
753 63758dd7 2001-12-15 alex {
754 c6b9f3a1 2002-11-20 alex /* Operation haette Socket "nur" blockiert ... */
755 c6b9f3a1 2002-11-20 alex if( errno == EAGAIN ) return TRUE;
756 c6b9f3a1 2002-11-20 alex
757 63758dd7 2001-12-15 alex /* Oops, ein Fehler! */
758 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
759 3cf845fb 2002-10-14 alex Conn_Close( Idx, "Write error!", NULL, FALSE );
760 63758dd7 2001-12-15 alex return FALSE;
761 63758dd7 2001-12-15 alex }
762 f060db5e 2002-10-09 alex
763 63758dd7 2001-12-15 alex /* Puffer anpassen */
764 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen -= len;
765 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
766 9ab186c4 2001-12-25 alex
767 63758dd7 2001-12-15 alex return TRUE;
768 63758dd7 2001-12-15 alex } /* Handle_Write */
769 63758dd7 2001-12-15 alex
770 63758dd7 2001-12-15 alex
771 c2f60abe 2002-05-27 alex LOCAL VOID
772 c2f60abe 2002-05-27 alex New_Connection( INT Sock )
773 1c8eb478 2001-12-12 alex {
774 d5c97f81 2001-12-23 alex /* Neue Client-Verbindung von Listen-Socket annehmen und
775 d5c97f81 2001-12-23 alex * CLIENT-Struktur anlegen. */
776 63758dd7 2001-12-15 alex
777 1c8eb478 2001-12-12 alex struct sockaddr_in new_addr;
778 cf050519 2001-12-14 alex INT new_sock, new_sock_len;
779 4a111033 2001-12-29 alex RES_STAT *s;
780 cf050519 2001-12-14 alex CONN_ID idx;
781 b2d472fc 2002-05-19 alex CLIENT *c;
782 b991de0f 2002-11-02 alex POINTER *ptr;
783 b991de0f 2002-11-02 alex LONG new_size;
784 590f2a3f 2002-03-11 alex
785 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
786 5fefe1a3 2001-12-12 alex
787 c6b9f3a1 2002-11-20 alex /* Connection auf Listen-Socket annehmen */
788 1c8eb478 2001-12-12 alex new_sock_len = sizeof( new_addr );
789 fb55c443 2001-12-13 alex new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
790 1c8eb478 2001-12-12 alex if( new_sock < 0 )
791 5fefe1a3 2001-12-12 alex {
792 446df061 2001-12-24 alex Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
793 5fefe1a3 2001-12-12 alex return;
794 5fefe1a3 2001-12-12 alex }
795 c6b9f3a1 2002-11-20 alex
796 c6b9f3a1 2002-11-20 alex /* Socket initialisieren */
797 c6b9f3a1 2002-11-20 alex Init_Socket( new_sock );
798 9ab186c4 2001-12-25 alex
799 3cf845fb 2002-10-14 alex /* Freie Connection-Struktur suchen */
800 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
801 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
802 1c8eb478 2001-12-12 alex {
803 b991de0f 2002-11-02 alex new_size = Pool_Size + CONNECTION_POOL;
804 b991de0f 2002-11-02 alex
805 b991de0f 2002-11-02 alex /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
806 b991de0f 2002-11-02 alex * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
807 b991de0f 2002-11-02 alex
808 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
809 b991de0f 2002-11-02 alex {
810 b991de0f 2002-11-02 alex /* Es ist ein Limit konfiguriert */
811 b991de0f 2002-11-02 alex if( Pool_Size >= Conf_MaxConnections )
812 b991de0f 2002-11-02 alex {
813 b991de0f 2002-11-02 alex /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
814 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
815 b991de0f 2002-11-02 alex close( new_sock );
816 b991de0f 2002-11-02 alex return;
817 b991de0f 2002-11-02 alex }
818 b991de0f 2002-11-02 alex if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
819 b991de0f 2002-11-02 alex }
820 e68cdf30 2002-12-30 alex if( new_size < Pool_Size )
821 e68cdf30 2002-12-30 alex {
822 e68cdf30 2002-12-30 alex Log( LOG_ALERT, "Can't accespt connection: limit (%d) reached -- overflow!", Pool_Size );
823 e68cdf30 2002-12-30 alex close( new_sock );
824 e68cdf30 2002-12-30 alex return;
825 e68cdf30 2002-12-30 alex }
826 b991de0f 2002-11-02 alex
827 b991de0f 2002-11-02 alex /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
828 b991de0f 2002-11-02 alex * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
829 b991de0f 2002-11-02 alex * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
830 b991de0f 2002-11-02 alex ptr = realloc( My_Connections, sizeof( CONNECTION ) * new_size );
831 b991de0f 2002-11-02 alex if( ! ptr )
832 b991de0f 2002-11-02 alex {
833 b991de0f 2002-11-02 alex /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
834 b991de0f 2002-11-02 alex ptr = malloc( sizeof( CONNECTION ) * new_size );
835 b991de0f 2002-11-02 alex if( ! ptr )
836 b991de0f 2002-11-02 alex {
837 b991de0f 2002-11-02 alex /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
838 b991de0f 2002-11-02 alex Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
839 b991de0f 2002-11-02 alex close( new_sock );
840 b991de0f 2002-11-02 alex return;
841 b991de0f 2002-11-02 alex }
842 b991de0f 2002-11-02 alex
843 b991de0f 2002-11-02 alex /* Struktur umkopieren ... */
844 b991de0f 2002-11-02 alex memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
845 b991de0f 2002-11-02 alex
846 902ad912 2002-12-17 alex Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [malloc()/memcpy()]", new_size, sizeof( CONNECTION ) * new_size );
847 b991de0f 2002-11-02 alex }
848 902ad912 2002-12-17 alex else Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
849 b991de0f 2002-11-02 alex
850 902ad912 2002-12-17 alex /* Adjust pointer to new block */
851 b991de0f 2002-11-02 alex My_Connections = ptr;
852 902ad912 2002-12-17 alex
853 902ad912 2002-12-17 alex /* Initialize new items */
854 902ad912 2002-12-17 alex for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
855 902ad912 2002-12-17 alex idx = Pool_Size;
856 902ad912 2002-12-17 alex
857 902ad912 2002-12-17 alex /* Adjust new pool size */
858 b991de0f 2002-11-02 alex Pool_Size = new_size;
859 1c8eb478 2001-12-12 alex }
860 9ab186c4 2001-12-25 alex
861 d5c97f81 2001-12-23 alex /* Client-Struktur initialisieren */
862 b2d472fc 2002-05-19 alex c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
863 b2d472fc 2002-05-19 alex if( ! c )
864 d5c97f81 2001-12-23 alex {
865 d5c97f81 2001-12-23 alex Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
866 d5c97f81 2001-12-23 alex close( new_sock );
867 d5c97f81 2001-12-23 alex return;
868 d5c97f81 2001-12-23 alex }
869 9ab186c4 2001-12-25 alex
870 1c8eb478 2001-12-12 alex /* Verbindung registrieren */
871 4a111033 2001-12-29 alex Init_Conn_Struct( idx );
872 1c8eb478 2001-12-12 alex My_Connections[idx].sock = new_sock;
873 1c8eb478 2001-12-12 alex My_Connections[idx].addr = new_addr;
874 1c8eb478 2001-12-12 alex
875 1c8eb478 2001-12-12 alex /* Neuen Socket registrieren */
876 1c8eb478 2001-12-12 alex FD_SET( new_sock, &My_Sockets );
877 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
878 1c8eb478 2001-12-12 alex
879 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 );
880 4a111033 2001-12-29 alex
881 4a111033 2001-12-29 alex /* Hostnamen ermitteln */
882 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host ));
883 ffb90f4c 2002-11-22 alex Client_SetHostname( c, My_Connections[idx].host );
884 c2f60abe 2002-05-27 alex s = Resolve_Addr( &new_addr );
885 4a111033 2001-12-29 alex if( s )
886 4a111033 2001-12-29 alex {
887 4a111033 2001-12-29 alex /* Sub-Prozess wurde asyncron gestartet */
888 4a111033 2001-12-29 alex My_Connections[idx].res_stat = s;
889 4a111033 2001-12-29 alex }
890 7b6cfc17 2002-08-26 alex
891 7b6cfc17 2002-08-26 alex /* Penalty-Zeit setzen */
892 736bacde 2002-10-10 alex Conn_SetPenalty( idx, 4 );
893 1c8eb478 2001-12-12 alex } /* New_Connection */
894 5fefe1a3 2001-12-12 alex
895 5fefe1a3 2001-12-12 alex
896 c2f60abe 2002-05-27 alex LOCAL CONN_ID
897 c2f60abe 2002-05-27 alex Socket2Index( INT Sock )
898 1c8eb478 2001-12-12 alex {
899 63758dd7 2001-12-15 alex /* zum Socket passende Connection suchen */
900 63758dd7 2001-12-15 alex
901 cf050519 2001-12-14 alex CONN_ID idx;
902 9ab186c4 2001-12-25 alex
903 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
904 9ab186c4 2001-12-25 alex
905 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
906 9ab186c4 2001-12-25 alex
907 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
908 b840be98 2002-10-15 alex {
909 b840be98 2002-10-15 alex /* die Connection wurde vermutlich (wegen eines
910 b840be98 2002-10-15 alex * Fehlers) bereits wieder abgebaut ... */
911 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
912 b840be98 2002-10-15 alex return NONE;
913 b840be98 2002-10-15 alex }
914 3cf845fb 2002-10-14 alex else return idx;
915 1c8eb478 2001-12-12 alex } /* Socket2Index */
916 1c8eb478 2001-12-12 alex
917 1c8eb478 2001-12-12 alex
918 c2f60abe 2002-05-27 alex LOCAL VOID
919 c2f60abe 2002-05-27 alex Read_Request( CONN_ID Idx )
920 1c8eb478 2001-12-12 alex {
921 747fd2f0 2001-12-13 alex /* Daten von Socket einlesen und entsprechend behandeln.
922 747fd2f0 2001-12-13 alex * Tritt ein Fehler auf, so wird der Socket geschlossen. */
923 1c8eb478 2001-12-12 alex
924 d3a6537f 2002-11-28 alex INT len, bsize;
925 1d8cb34b 2002-11-29 alex #ifdef USE_ZLIB
926 d3a6537f 2002-11-28 alex CLIENT *c;
927 1d8cb34b 2002-11-29 alex #endif
928 63758dd7 2001-12-15 alex
929 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
930 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
931 9ab186c4 2001-12-25 alex
932 d3a6537f 2002-11-28 alex /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
933 d3a6537f 2002-11-28 alex * ansonsten koennen Daten ggf. nicht umkopiert werden. */
934 d3a6537f 2002-11-28 alex bsize = READBUFFER_LEN;
935 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
936 d3a6537f 2002-11-28 alex c = Client_GetFromConn( Idx );
937 d3a6537f 2002-11-28 alex if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) && ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN )) bsize = ZREADBUFFER_LEN;
938 d3a6537f 2002-11-28 alex #endif
939 d3a6537f 2002-11-28 alex
940 d3a6537f 2002-11-28 alex #ifdef USE_ZLIB
941 d3a6537f 2002-11-28 alex if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
942 65f3adca 2002-11-26 alex #else
943 d3a6537f 2002-11-28 alex if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
944 65f3adca 2002-11-26 alex #endif
945 54e487d4 2002-01-03 alex {
946 54e487d4 2002-01-03 alex /* Der Lesepuffer ist voll */
947 79809118 2002-01-06 alex Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
948 79809118 2002-01-06 alex Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
949 54e487d4 2002-01-03 alex return;
950 54e487d4 2002-01-03 alex }
951 54e487d4 2002-01-03 alex
952 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
953 65f3adca 2002-11-26 alex if( My_Connections[Idx].options & CONN_ZIP )
954 65f3adca 2002-11-26 alex {
955 65f3adca 2002-11-26 alex len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 );
956 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
957 65f3adca 2002-11-26 alex }
958 65f3adca 2002-11-26 alex else
959 65f3adca 2002-11-26 alex #endif
960 65f3adca 2002-11-26 alex {
961 d3a6537f 2002-11-28 alex len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, bsize - My_Connections[Idx].rdatalen - 1, 0 );
962 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].rdatalen += len;
963 65f3adca 2002-11-26 alex }
964 747fd2f0 2001-12-13 alex
965 747fd2f0 2001-12-13 alex if( len == 0 )
966 1c8eb478 2001-12-12 alex {
967 747fd2f0 2001-12-13 alex /* Socket wurde geschlossen */
968 6591f17f 2002-12-03 alex Log( LOG_INFO, "%s:%d (%s) is closing the connection ...", My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), inet_ntoa( My_Connections[Idx].addr.sin_addr ));
969 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
970 1c8eb478 2001-12-12 alex return;
971 1c8eb478 2001-12-12 alex }
972 747fd2f0 2001-12-13 alex
973 747fd2f0 2001-12-13 alex if( len < 0 )
974 747fd2f0 2001-12-13 alex {
975 c6b9f3a1 2002-11-20 alex /* Operation haette Socket "nur" blockiert ... */
976 c6b9f3a1 2002-11-20 alex if( errno == EAGAIN ) return;
977 c6b9f3a1 2002-11-20 alex
978 747fd2f0 2001-12-13 alex /* Fehler beim Lesen */
979 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
980 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
981 747fd2f0 2001-12-13 alex return;
982 747fd2f0 2001-12-13 alex }
983 cf050519 2001-12-14 alex
984 f060db5e 2002-10-09 alex /* Connection-Statistik aktualisieren */
985 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in += len;
986 f060db5e 2002-10-09 alex
987 65bdfdf2 2001-12-26 alex /* Timestamp aktualisieren */
988 65bdfdf2 2001-12-26 alex My_Connections[Idx].lastdata = time( NULL );
989 65bdfdf2 2001-12-26 alex
990 9ab186c4 2001-12-25 alex Handle_Buffer( Idx );
991 9ab186c4 2001-12-25 alex } /* Read_Request */
992 9ab186c4 2001-12-25 alex
993 9ab186c4 2001-12-25 alex
994 5ecb00ed 2002-11-23 alex LOCAL BOOLEAN
995 c2f60abe 2002-05-27 alex Handle_Buffer( CONN_ID Idx )
996 9ab186c4 2001-12-25 alex {
997 5ecb00ed 2002-11-23 alex /* Daten im Lese-Puffer einer Verbindung verarbeiten.
998 5ecb00ed 2002-11-23 alex * Wurde ein Request verarbeitet, so wird TRUE geliefert,
999 5ecb00ed 2002-11-23 alex * ansonsten FALSE (auch bei Fehlern). */
1000 bc140df8 2001-12-29 alex
1001 12cad28e 2002-10-21 alex #ifndef STRICT_RFC
1002 12cad28e 2002-10-21 alex CHAR *ptr1, *ptr2;
1003 12cad28e 2002-10-21 alex #endif
1004 12cad28e 2002-10-21 alex CHAR *ptr;
1005 9ab186c4 2001-12-25 alex INT len, delta;
1006 65f3adca 2002-11-26 alex BOOLEAN action, result;
1007 d3a6537f 2002-11-28 alex #ifdef USE_ZLIB
1008 d3a6537f 2002-11-28 alex BOOLEAN old_z;
1009 d3a6537f 2002-11-28 alex #endif
1010 590f2a3f 2002-03-11 alex
1011 65f3adca 2002-11-26 alex result = FALSE;
1012 65f3adca 2002-11-26 alex do
1013 9ab186c4 2001-12-25 alex {
1014 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
1015 65f3adca 2002-11-26 alex /* ggf. noch unkomprimiete Daten weiter entpacken */
1016 65f3adca 2002-11-26 alex if( My_Connections[Idx].options & CONN_ZIP )
1017 54e487d4 2002-01-03 alex {
1018 65f3adca 2002-11-26 alex if( ! Unzip_Buffer( Idx )) return FALSE;
1019 54e487d4 2002-01-03 alex }
1020 65f3adca 2002-11-26 alex #endif
1021 65f3adca 2002-11-26 alex
1022 65f3adca 2002-11-26 alex if( My_Connections[Idx].rdatalen < 1 ) break;
1023 590f2a3f 2002-03-11 alex
1024 65f3adca 2002-11-26 alex /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1025 65f3adca 2002-11-26 alex * RFC 2812. Haben wir eine? */
1026 65f3adca 2002-11-26 alex My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1027 65f3adca 2002-11-26 alex ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1028 65f3adca 2002-11-26 alex
1029 65f3adca 2002-11-26 alex if( ptr ) delta = 2;
1030 65f3adca 2002-11-26 alex #ifndef STRICT_RFC
1031 65f3adca 2002-11-26 alex else
1032 747fd2f0 2001-12-13 alex {
1033 65f3adca 2002-11-26 alex /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1034 65f3adca 2002-11-26 alex * machen soetwas viele Clients, u.a. "mIRC" :-( */
1035 65f3adca 2002-11-26 alex ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1036 65f3adca 2002-11-26 alex ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1037 65f3adca 2002-11-26 alex delta = 1;
1038 65f3adca 2002-11-26 alex if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1039 65f3adca 2002-11-26 alex else if( ptr1 ) ptr = ptr1;
1040 65f3adca 2002-11-26 alex else if( ptr2 ) ptr = ptr2;
1041 747fd2f0 2001-12-13 alex }
1042 65f3adca 2002-11-26 alex #endif
1043 5ecb00ed 2002-11-23 alex
1044 65f3adca 2002-11-26 alex action = FALSE;
1045 65f3adca 2002-11-26 alex if( ptr )
1046 65f3adca 2002-11-26 alex {
1047 65f3adca 2002-11-26 alex /* Ende der Anfrage wurde gefunden */
1048 65f3adca 2002-11-26 alex *ptr = '\0';
1049 65f3adca 2002-11-26 alex len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1050 65f3adca 2002-11-26 alex if( len > ( COMMAND_LEN - 1 ))
1051 65f3adca 2002-11-26 alex {
1052 65f3adca 2002-11-26 alex /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1053 65f3adca 2002-11-26 alex * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1054 65f3adca 2002-11-26 alex * empfangen wird, wird der Client disconnectiert. */
1055 65f3adca 2002-11-26 alex Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
1056 65f3adca 2002-11-26 alex Conn_Close( Idx, NULL, "Request too long", TRUE );
1057 65f3adca 2002-11-26 alex return FALSE;
1058 65f3adca 2002-11-26 alex }
1059 d3a6537f 2002-11-28 alex
1060 d3a6537f 2002-11-28 alex #ifdef USE_ZLIB
1061 d3a6537f 2002-11-28 alex /* merken, ob Stream bereits komprimiert wird */
1062 d3a6537f 2002-11-28 alex old_z = My_Connections[Idx].options & CONN_ZIP;
1063 d3a6537f 2002-11-28 alex #endif
1064 d3a6537f 2002-11-28 alex
1065 65f3adca 2002-11-26 alex if( len > delta )
1066 65f3adca 2002-11-26 alex {
1067 65f3adca 2002-11-26 alex /* Es wurde ein Request gelesen */
1068 39219230 2002-12-02 alex My_Connections[Idx].msg_in++;
1069 65f3adca 2002-11-26 alex if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
1070 65f3adca 2002-11-26 alex else action = TRUE;
1071 65f3adca 2002-11-26 alex }
1072 d3a6537f 2002-11-28 alex
1073 65f3adca 2002-11-26 alex /* Puffer anpassen */
1074 65f3adca 2002-11-26 alex My_Connections[Idx].rdatalen -= len;
1075 65f3adca 2002-11-26 alex memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1076 d3a6537f 2002-11-28 alex
1077 d3a6537f 2002-11-28 alex #ifdef USE_ZLIB
1078 d3a6537f 2002-11-28 alex if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1079 d3a6537f 2002-11-28 alex {
1080 d3a6537f 2002-11-28 alex /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1081 d3a6537f 2002-11-28 alex * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1082 d3a6537f 2002-11-28 alex * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1083 d3a6537f 2002-11-28 alex {
1084 d3a6537f 2002-11-28 alex if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1085 d3a6537f 2002-11-28 alex {
1086 d3a6537f 2002-11-28 alex /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1087 d3a6537f 2002-11-28 alex Log( LOG_ALERT, "Can't move read buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
1088 d3a6537f 2002-11-28 alex return FALSE;
1089 d3a6537f 2002-11-28 alex }
1090 d3a6537f 2002-11-28 alex memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1091 d3a6537f 2002-11-28 alex My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1092 d3a6537f 2002-11-28 alex My_Connections[Idx].rdatalen = 0;
1093 d3a6537f 2002-11-28 alex Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1094 d3a6537f 2002-11-28 alex }
1095 d3a6537f 2002-11-28 alex }
1096 d3a6537f 2002-11-28 alex #endif
1097 65f3adca 2002-11-26 alex }
1098 65f3adca 2002-11-26 alex
1099 65f3adca 2002-11-26 alex if( action ) result = TRUE;
1100 65f3adca 2002-11-26 alex } while( action );
1101 65f3adca 2002-11-26 alex
1102 65f3adca 2002-11-26 alex return result;
1103 9ab186c4 2001-12-25 alex } /* Handle_Buffer */
1104 747fd2f0 2001-12-13 alex
1105 747fd2f0 2001-12-13 alex
1106 c2f60abe 2002-05-27 alex LOCAL VOID
1107 c2f60abe 2002-05-27 alex Check_Connections( VOID )
1108 65bdfdf2 2001-12-26 alex {
1109 bc140df8 2001-12-29 alex /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1110 bc140df8 2001-12-29 alex * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1111 bc140df8 2001-12-29 alex * auch das nicht "hilft", Client disconnectieren. */
1112 65bdfdf2 2001-12-26 alex
1113 03d971d9 2002-01-02 alex CLIENT *c;
1114 e68cdf30 2002-12-30 alex CONN_ID i;
1115 65bdfdf2 2001-12-26 alex
1116 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1117 65bdfdf2 2001-12-26 alex {
1118 03d971d9 2002-01-02 alex if( My_Connections[i].sock == NONE ) continue;
1119 03d971d9 2002-01-02 alex
1120 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1121 356683ff 2002-01-04 alex if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1122 65bdfdf2 2001-12-26 alex {
1123 03d971d9 2002-01-02 alex /* verbundener User, Server oder Service */
1124 65bdfdf2 2001-12-26 alex if( My_Connections[i].lastping > My_Connections[i].lastdata )
1125 65bdfdf2 2001-12-26 alex {
1126 65bdfdf2 2001-12-26 alex /* es wurde bereits ein PING gesendet */
1127 08cf5607 2001-12-26 alex if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1128 65bdfdf2 2001-12-26 alex {
1129 65bdfdf2 2001-12-26 alex /* Timeout */
1130 e39925af 2002-03-26 alex Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1131 79809118 2002-01-06 alex Conn_Close( i, NULL, "Ping timeout", TRUE );
1132 65bdfdf2 2001-12-26 alex }
1133 65bdfdf2 2001-12-26 alex }
1134 08cf5607 2001-12-26 alex else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1135 65bdfdf2 2001-12-26 alex {
1136 65bdfdf2 2001-12-26 alex /* es muss ein PING gesendet werden */
1137 65bdfdf2 2001-12-26 alex Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1138 65bdfdf2 2001-12-26 alex My_Connections[i].lastping = time( NULL );
1139 356683ff 2002-01-04 alex Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1140 65bdfdf2 2001-12-26 alex }
1141 65bdfdf2 2001-12-26 alex }
1142 03d971d9 2002-01-02 alex else
1143 03d971d9 2002-01-02 alex {
1144 03d971d9 2002-01-02 alex /* noch nicht vollstaendig aufgebaute Verbindung */
1145 03d971d9 2002-01-02 alex if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1146 03d971d9 2002-01-02 alex {
1147 03d971d9 2002-01-02 alex /* Timeout */
1148 140d1aa5 2002-02-27 alex Log( LOG_DEBUG, "Connection %d timed out ...", i );
1149 5457e078 2002-03-02 alex Conn_Close( i, NULL, "Timeout", FALSE );
1150 03d971d9 2002-01-02 alex }
1151 03d971d9 2002-01-02 alex }
1152 65bdfdf2 2001-12-26 alex }
1153 03d971d9 2002-01-02 alex } /* Check_Connections */
1154 03d971d9 2002-01-02 alex
1155 03d971d9 2002-01-02 alex
1156 c2f60abe 2002-05-27 alex LOCAL VOID
1157 c2f60abe 2002-05-27 alex Check_Servers( VOID )
1158 03d971d9 2002-01-02 alex {
1159 a2544e49 2002-12-30 alex /* Check if we can establish further server links */
1160 03d971d9 2002-01-02 alex
1161 03d971d9 2002-01-02 alex RES_STAT *s;
1162 a2544e49 2002-12-30 alex CONN_ID idx;
1163 a2544e49 2002-12-30 alex INT i, n;
1164 26ffbc78 2002-02-19 alex
1165 a2544e49 2002-12-30 alex /* Serach all connections, are there results from the resolver? */
1166 a2544e49 2002-12-30 alex for( idx = 0; idx < Pool_Size; idx++ )
1167 03d971d9 2002-01-02 alex {
1168 a2544e49 2002-12-30 alex if( My_Connections[idx].sock != SERVER_WAIT ) continue;
1169 590f2a3f 2002-03-11 alex
1170 a2544e49 2002-12-30 alex /* IP resolved? */
1171 a2544e49 2002-12-30 alex if( My_Connections[idx].res_stat == NULL ) New_Server( Conf_GetServer( idx ), idx );
1172 a2544e49 2002-12-30 alex }
1173 03d971d9 2002-01-02 alex
1174 a2544e49 2002-12-30 alex /* Check all configured servers */
1175 a2544e49 2002-12-30 alex for( i = 0; i < MAX_SERVERS; i++ )
1176 a2544e49 2002-12-30 alex {
1177 8dadb17f 2002-12-31 alex /* Valid outgoing server which isn't already connected or disabled? */
1178 8dadb17f 2002-12-31 alex if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) || ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED )) continue;
1179 d67d94ea 2002-03-10 alex
1180 a2544e49 2002-12-30 alex /* Is there already a connection in this group? */
1181 a2544e49 2002-12-30 alex if( Conf_Server[i].group > NONE )
1182 a2544e49 2002-12-30 alex {
1183 a2544e49 2002-12-30 alex for( n = 0; n < MAX_SERVERS; n++ )
1184 d67d94ea 2002-03-10 alex {
1185 a2544e49 2002-12-30 alex if( n == i ) continue;
1186 a2544e49 2002-12-30 alex if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
1187 03d971d9 2002-01-02 alex }
1188 a2544e49 2002-12-30 alex if( n < MAX_SERVERS ) continue;
1189 03d971d9 2002-01-02 alex }
1190 590f2a3f 2002-03-11 alex
1191 a2544e49 2002-12-30 alex /* Check last connect attempt? */
1192 03d971d9 2002-01-02 alex if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1193 65bdfdf2 2001-12-26 alex
1194 a2544e49 2002-12-30 alex /* Okay, try to connect now */
1195 03d971d9 2002-01-02 alex Conf_Server[i].lasttry = time( NULL );
1196 65bdfdf2 2001-12-26 alex
1197 a2544e49 2002-12-30 alex /* Search free connection structure */
1198 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1199 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1200 03d971d9 2002-01-02 alex {
1201 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1202 03d971d9 2002-01-02 alex return;
1203 03d971d9 2002-01-02 alex }
1204 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1205 03d971d9 2002-01-02 alex
1206 03d971d9 2002-01-02 alex /* Verbindungs-Struktur initialisieren */
1207 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
1208 03d971d9 2002-01-02 alex My_Connections[idx].sock = SERVER_WAIT;
1209 a2544e49 2002-12-30 alex Conf_Server[i].conn_id = idx;
1210 590f2a3f 2002-03-11 alex
1211 a2544e49 2002-12-30 alex /* Resolve Hostname. If this fails, try to use it as an IP address */
1212 a2544e49 2002-12-30 alex strlcpy( Conf_Server[i].ip, Conf_Server[i].host, sizeof( Conf_Server[i].ip ));
1213 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host ));
1214 c2f60abe 2002-05-27 alex s = Resolve_Name( Conf_Server[i].host );
1215 03d971d9 2002-01-02 alex if( s )
1216 03d971d9 2002-01-02 alex {
1217 03d971d9 2002-01-02 alex /* Sub-Prozess wurde asyncron gestartet */
1218 03d971d9 2002-01-02 alex My_Connections[idx].res_stat = s;
1219 03d971d9 2002-01-02 alex }
1220 03d971d9 2002-01-02 alex }
1221 03d971d9 2002-01-02 alex } /* Check_Servers */
1222 03d971d9 2002-01-02 alex
1223 03d971d9 2002-01-02 alex
1224 c2f60abe 2002-05-27 alex LOCAL VOID
1225 c2f60abe 2002-05-27 alex New_Server( INT Server, CONN_ID Idx )
1226 03d971d9 2002-01-02 alex {
1227 03d971d9 2002-01-02 alex /* Neue Server-Verbindung aufbauen */
1228 03d971d9 2002-01-02 alex
1229 03d971d9 2002-01-02 alex struct sockaddr_in new_addr;
1230 03d971d9 2002-01-02 alex struct in_addr inaddr;
1231 6250dcb1 2002-11-11 alex INT res, new_sock;
1232 54e487d4 2002-01-03 alex CLIENT *c;
1233 03d971d9 2002-01-02 alex
1234 a4d5ca63 2002-11-05 alex assert( Server > NONE );
1235 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1236 03d971d9 2002-01-02 alex
1237 03d971d9 2002-01-02 alex /* Wurde eine gueltige IP-Adresse gefunden? */
1238 03d971d9 2002-01-02 alex if( ! Conf_Server[Server].ip[0] )
1239 03d971d9 2002-01-02 alex {
1240 03d971d9 2002-01-02 alex /* Nein. Verbindung wieder freigeben: */
1241 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1242 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1243 03d971d9 2002-01-02 alex return;
1244 03d971d9 2002-01-02 alex }
1245 590f2a3f 2002-03-11 alex
1246 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 );
1247 03d971d9 2002-01-02 alex
1248 31a3bfed 2002-05-19 alex #ifdef HAVE_INET_ATON
1249 03d971d9 2002-01-02 alex if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1250 31a3bfed 2002-05-19 alex #else
1251 31a3bfed 2002-05-19 alex memset( &inaddr, 0, sizeof( inaddr ));
1252 31a3bfed 2002-05-19 alex inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1253 31a3bfed 2002-05-19 alex if( inaddr.s_addr == (unsigned)-1 )
1254 31a3bfed 2002-05-19 alex #endif
1255 03d971d9 2002-01-02 alex {
1256 03d971d9 2002-01-02 alex /* Konnte Adresse nicht konvertieren */
1257 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1258 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 );
1259 03d971d9 2002-01-02 alex return;
1260 03d971d9 2002-01-02 alex }
1261 03d971d9 2002-01-02 alex
1262 03d971d9 2002-01-02 alex memset( &new_addr, 0, sizeof( new_addr ));
1263 03d971d9 2002-01-02 alex new_addr.sin_family = AF_INET;
1264 03d971d9 2002-01-02 alex new_addr.sin_addr = inaddr;
1265 03d971d9 2002-01-02 alex new_addr.sin_port = htons( Conf_Server[Server].port );
1266 03d971d9 2002-01-02 alex
1267 03d971d9 2002-01-02 alex new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1268 03d971d9 2002-01-02 alex if ( new_sock < 0 )
1269 03d971d9 2002-01-02 alex {
1270 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1271 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1272 03d971d9 2002-01-02 alex return;
1273 03d971d9 2002-01-02 alex }
1274 6da91c34 2002-03-02 alex
1275 6da91c34 2002-03-02 alex if( ! Init_Socket( new_sock )) return;
1276 6da91c34 2002-03-02 alex
1277 6250dcb1 2002-11-11 alex res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1278 6250dcb1 2002-11-11 alex if(( res != 0 ) && ( errno != EINPROGRESS ))
1279 03d971d9 2002-01-02 alex {
1280 6250dcb1 2002-11-11 alex Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1281 03d971d9 2002-01-02 alex close( new_sock );
1282 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1283 03d971d9 2002-01-02 alex return;
1284 03d971d9 2002-01-02 alex }
1285 03d971d9 2002-01-02 alex
1286 03d971d9 2002-01-02 alex /* Client-Struktur initialisieren */
1287 2e4d085d 2002-01-05 alex c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1288 54e487d4 2002-01-03 alex if( ! c )
1289 03d971d9 2002-01-02 alex {
1290 03d971d9 2002-01-02 alex close( new_sock );
1291 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1292 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1293 03d971d9 2002-01-02 alex return;
1294 03d971d9 2002-01-02 alex }
1295 356683ff 2002-01-04 alex Client_SetIntroducer( c, c );
1296 1f975b6e 2002-04-08 alex Client_SetToken( c, TOKEN_OUTBOUND );
1297 6da91c34 2002-03-02 alex
1298 03d971d9 2002-01-02 alex /* Verbindung registrieren */
1299 03d971d9 2002-01-02 alex My_Connections[Idx].sock = new_sock;
1300 03d971d9 2002-01-02 alex My_Connections[Idx].addr = new_addr;
1301 695631b2 2002-12-26 alex strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
1302 03d971d9 2002-01-02 alex
1303 03d971d9 2002-01-02 alex /* Neuen Socket registrieren */
1304 03d971d9 2002-01-02 alex FD_SET( new_sock, &My_Sockets );
1305 6da91c34 2002-03-02 alex FD_SET( new_sock, &My_Connects );
1306 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1307 b840be98 2002-10-15 alex
1308 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1309 03d971d9 2002-01-02 alex } /* New_Server */
1310 03d971d9 2002-01-02 alex
1311 03d971d9 2002-01-02 alex
1312 c2f60abe 2002-05-27 alex LOCAL VOID
1313 e68cdf30 2002-12-30 alex Init_Conn_Struct( CONN_ID Idx )
1314 4a111033 2001-12-29 alex {
1315 4a111033 2001-12-29 alex /* Connection-Struktur initialisieren */
1316 4a111033 2001-12-29 alex
1317 4a111033 2001-12-29 alex My_Connections[Idx].sock = NONE;
1318 4a111033 2001-12-29 alex My_Connections[Idx].res_stat = NULL;
1319 4a111033 2001-12-29 alex My_Connections[Idx].host[0] = '\0';
1320 4a111033 2001-12-29 alex My_Connections[Idx].rbuf[0] = '\0';
1321 4a111033 2001-12-29 alex My_Connections[Idx].rdatalen = 0;
1322 4a111033 2001-12-29 alex My_Connections[Idx].wbuf[0] = '\0';
1323 4a111033 2001-12-29 alex My_Connections[Idx].wdatalen = 0;
1324 39219230 2002-12-02 alex My_Connections[Idx].starttime = time( NULL );
1325 4a111033 2001-12-29 alex My_Connections[Idx].lastdata = time( NULL );
1326 4a111033 2001-12-29 alex My_Connections[Idx].lastping = 0;
1327 804b1ec4 2001-12-31 alex My_Connections[Idx].lastprivmsg = time( NULL );
1328 7b6cfc17 2002-08-26 alex My_Connections[Idx].delaytime = 0;
1329 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in = 0;
1330 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_out = 0;
1331 39219230 2002-12-02 alex My_Connections[Idx].msg_in = 0;
1332 39219230 2002-12-02 alex My_Connections[Idx].msg_out = 0;
1333 a29e37a4 2002-11-04 alex My_Connections[Idx].flag = 0;
1334 65f3adca 2002-11-26 alex My_Connections[Idx].options = 0;
1335 65f3adca 2002-11-26 alex
1336 65f3adca 2002-11-26 alex #ifdef USE_ZLIB
1337 65f3adca 2002-11-26 alex My_Connections[Idx].zip.rbuf[0] = '\0';
1338 65f3adca 2002-11-26 alex My_Connections[Idx].zip.rdatalen = 0;
1339 65f3adca 2002-11-26 alex My_Connections[Idx].zip.wbuf[0] = '\0';
1340 65f3adca 2002-11-26 alex My_Connections[Idx].zip.wdatalen = 0;
1341 65f3adca 2002-11-26 alex My_Connections[Idx].zip.bytes_in = 0;
1342 65f3adca 2002-11-26 alex My_Connections[Idx].zip.bytes_out = 0;
1343 65f3adca 2002-11-26 alex #endif
1344 4a111033 2001-12-29 alex } /* Init_Conn_Struct */
1345 4a111033 2001-12-29 alex
1346 4a111033 2001-12-29 alex
1347 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
1348 c2f60abe 2002-05-27 alex Init_Socket( INT Sock )
1349 6da91c34 2002-03-02 alex {
1350 6da91c34 2002-03-02 alex /* Socket-Optionen setzen */
1351 6da91c34 2002-03-02 alex
1352 6da91c34 2002-03-02 alex INT on = 1;
1353 6da91c34 2002-03-02 alex
1354 239727b4 2002-03-13 alex #ifdef O_NONBLOCK /* A/UX kennt das nicht? */
1355 6da91c34 2002-03-02 alex if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1356 6da91c34 2002-03-02 alex {
1357 6da91c34 2002-03-02 alex Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1358 6da91c34 2002-03-02 alex close( Sock );
1359 6da91c34 2002-03-02 alex return FALSE;
1360 6da91c34 2002-03-02 alex }
1361 239727b4 2002-03-13 alex #endif
1362 6da91c34 2002-03-02 alex if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1363 6da91c34 2002-03-02 alex {
1364 6da91c34 2002-03-02 alex Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1365 6da91c34 2002-03-02 alex /* dieser Fehler kann ignoriert werden. */
1366 6da91c34 2002-03-02 alex }
1367 6da91c34 2002-03-02 alex
1368 6da91c34 2002-03-02 alex return TRUE;
1369 6da91c34 2002-03-02 alex } /* Init_Socket */
1370 6da91c34 2002-03-02 alex
1371 6da91c34 2002-03-02 alex
1372 c2f60abe 2002-05-27 alex LOCAL VOID
1373 c2f60abe 2002-05-27 alex Read_Resolver_Result( INT r_fd )
1374 03d971d9 2002-01-02 alex {
1375 4a111033 2001-12-29 alex /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1376 c2f60abe 2002-05-27 alex * und entsprechende Connection aktualisieren */
1377 4a111033 2001-12-29 alex
1378 03d971d9 2002-01-02 alex CHAR result[HOST_LEN];
1379 4a111033 2001-12-29 alex CLIENT *c;
1380 a2544e49 2002-12-30 alex INT len, i, n;
1381 4a111033 2001-12-29 alex
1382 c2f60abe 2002-05-27 alex FD_CLR( r_fd, &Resolver_FDs );
1383 4a111033 2001-12-29 alex
1384 4a111033 2001-12-29 alex /* Anfrage vom Parent lesen */
1385 df2bd0e6 2002-10-03 alex len = read( r_fd, result, HOST_LEN - 1 );
1386 03d971d9 2002-01-02 alex if( len < 0 )
1387 4a111033 2001-12-29 alex {
1388 4a111033 2001-12-29 alex /* Fehler beim Lesen aus der Pipe */
1389 4a111033 2001-12-29 alex close( r_fd );
1390 79809118 2002-01-06 alex Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1391 4a111033 2001-12-29 alex return;
1392 4a111033 2001-12-29 alex }
1393 03d971d9 2002-01-02 alex result[len] = '\0';
1394 4a111033 2001-12-29 alex
1395 bc140df8 2001-12-29 alex /* zugehoerige Connection suchen */
1396 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1397 4a111033 2001-12-29 alex {
1398 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;
1399 4a111033 2001-12-29 alex }
1400 b991de0f 2002-11-02 alex if( i >= Pool_Size )
1401 4a111033 2001-12-29 alex {
1402 03d971d9 2002-01-02 alex /* Opsa! Keine passende Connection gefunden!? Vermutlich
1403 b840be98 2002-10-15 alex * wurde sie schon wieder geschlossen. */
1404 4a111033 2001-12-29 alex close( r_fd );
1405 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1406 4a111033 2001-12-29 alex return;
1407 4a111033 2001-12-29 alex }
1408 4a111033 2001-12-29 alex
1409 ffb90f4c 2002-11-22 alex Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
1410 ffb90f4c 2002-11-22 alex
1411 4a111033 2001-12-29 alex /* Aufraeumen */
1412 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[0] );
1413 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[1] );
1414 4a111033 2001-12-29 alex free( My_Connections[i].res_stat );
1415 4a111033 2001-12-29 alex My_Connections[i].res_stat = NULL;
1416 03d971d9 2002-01-02 alex
1417 03d971d9 2002-01-02 alex if( My_Connections[i].sock > NONE )
1418 03d971d9 2002-01-02 alex {
1419 03d971d9 2002-01-02 alex /* Eingehende Verbindung: Hostnamen setzen */
1420 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1421 54e487d4 2002-01-03 alex assert( c != NULL );
1422 695631b2 2002-12-26 alex strlcpy( My_Connections[i].host, result, sizeof( My_Connections[i].host ));
1423 54e487d4 2002-01-03 alex Client_SetHostname( c, result );
1424 03d971d9 2002-01-02 alex }
1425 03d971d9 2002-01-02 alex else
1426 03d971d9 2002-01-02 alex {
1427 03d971d9 2002-01-02 alex /* Ausgehende Verbindung (=Server): IP setzen */
1428 a2544e49 2002-12-30 alex n = Conf_GetServer( i );
1429 a2544e49 2002-12-30 alex if( n > NONE ) strlcpy( Conf_Server[n].ip, result, sizeof( Conf_Server[n].ip ));
1430 a2544e49 2002-12-30 alex else Log( LOG_ERR, "Got resolver result for non-configured server!?" );
1431 03d971d9 2002-01-02 alex }
1432 736bacde 2002-10-10 alex
1433 736bacde 2002-10-10 alex /* Penalty-Zeit zurueck setzen */
1434 736bacde 2002-10-10 alex Conn_ResetPenalty( i );
1435 4a111033 2001-12-29 alex } /* Read_Resolver_Result */
1436 65f3adca 2002-11-26 alex
1437 65f3adca 2002-11-26 alex
1438 5fefe1a3 2001-12-12 alex /* -eof- */