2 5fefe1a3 2001-12-12 alex * ngIRCd -- The Next Generation IRC Daemon
3 33f4e676 2004-02-28 alex * Copyright (c)2001-2004 Alexander Barton <alex@barton.de>
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.
11 490f28ff 2002-12-12 alex * Connection management
15 b77dae34 2002-12-30 alex #define CONN_MODULE
17 ca33cbda 2002-03-12 alex #include "portab.h"
19 1cf8ccd8 2005-06-12 fw static char UNUSED id[] = "$Id: conn.c,v 1.154 2005/06/12 17:21:46 fw Exp $";
21 ca33cbda 2002-03-12 alex #include "imp.h"
22 5fefe1a3 2001-12-12 alex #include <assert.h>
23 c1f32e82 2004-10-20 alex #ifdef PROTOTYPES
24 c1f32e82 2004-10-20 alex # include <stdarg.h>
26 c1f32e82 2004-10-20 alex # include <varargs.h>
28 5fefe1a3 2001-12-12 alex #include <stdio.h>
29 b20fa7c6 2002-01-01 alex #include <stdlib.h>
30 5fefe1a3 2001-12-12 alex #include <unistd.h>
31 5fefe1a3 2001-12-12 alex #include <errno.h>
32 5fefe1a3 2001-12-12 alex #include <fcntl.h>
33 5fefe1a3 2001-12-12 alex #include <string.h>
34 9ab186c4 2001-12-25 alex #include <sys/socket.h>
35 5fefe1a3 2001-12-12 alex #include <sys/time.h>
36 9ab186c4 2001-12-25 alex #include <sys/types.h>
37 ba331a2f 2001-12-26 alex #include <time.h>
38 5fefe1a3 2001-12-12 alex #include <netinet/in.h>
40 bb98fd8c 2004-01-25 alex #ifdef HAVE_NETINET_IP_H
41 bb98fd8c 2004-01-25 alex # include <netinet/ip.h>
44 bcc0cdc3 2002-01-05 alex #ifdef HAVE_ARPA_INET_H
45 bb98fd8c 2004-01-25 alex # include <arpa/inet.h>
47 bb98fd8c 2004-01-25 alex # define PF_INET AF_INET
50 5fefe1a3 2001-12-12 alex #ifdef HAVE_STDINT_H
51 bb98fd8c 2004-01-25 alex # include <stdint.h> /* e.g. for Mac OS X */
54 c40592d2 2003-12-26 alex #ifdef TCPWRAP
55 bb98fd8c 2004-01-25 alex # include <tcpd.h> /* for TCP Wrappers */
58 e68cdf30 2002-12-30 alex #include "defines.h"
59 e68cdf30 2002-12-30 alex #include "resolve.h"
61 c2f60abe 2002-05-27 alex #include "exp.h"
62 c2f60abe 2002-05-27 alex #include "conn.h"
64 c2f60abe 2002-05-27 alex #include "imp.h"
65 1c8eb478 2001-12-12 alex #include "ngircd.h"
66 d5c97f81 2001-12-23 alex #include "client.h"
67 65bdfdf2 2001-12-26 alex #include "conf.h"
68 e68cdf30 2002-12-30 alex #include "conn-zip.h"
69 b77dae34 2002-12-30 alex #include "conn-func.h"
70 5fefe1a3 2001-12-12 alex #include "log.h"
71 c4199b04 2001-12-21 alex #include "parse.h"
72 1c8eb478 2001-12-12 alex #include "tool.h"
74 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
75 bb98fd8c 2004-01-25 alex # include "rendezvous.h"
78 ca33cbda 2002-03-12 alex #include "exp.h"
81 34d54344 2002-03-12 alex #define SERVER_WAIT (NONE - 1)
84 8adff592 2005-03-19 fw LOCAL void Handle_Read PARAMS(( int sock ));
85 8adff592 2005-03-19 fw LOCAL bool Handle_Write PARAMS(( CONN_ID Idx ));
86 8adff592 2005-03-19 fw LOCAL void New_Connection PARAMS(( int Sock ));
87 8adff592 2005-03-19 fw LOCAL CONN_ID Socket2Index PARAMS(( int Sock ));
88 8adff592 2005-03-19 fw LOCAL void Read_Request PARAMS(( CONN_ID Idx ));
89 8adff592 2005-03-19 fw LOCAL bool Handle_Buffer PARAMS(( CONN_ID Idx ));
90 8adff592 2005-03-19 fw LOCAL void Check_Connections PARAMS(( void ));
91 8adff592 2005-03-19 fw LOCAL void Check_Servers PARAMS(( void ));
92 8adff592 2005-03-19 fw LOCAL void Init_Conn_Struct PARAMS(( CONN_ID Idx ));
93 8adff592 2005-03-19 fw LOCAL bool Init_Socket PARAMS(( int Sock ));
94 8adff592 2005-03-19 fw LOCAL void New_Server PARAMS(( int Server, CONN_ID Idx ));
95 8adff592 2005-03-19 fw LOCAL void Read_Resolver_Result PARAMS(( int r_fd ));
96 8adff592 2005-03-19 fw LOCAL void Simple_Message PARAMS(( int Sock, char *Msg ));
97 8adff592 2005-03-19 fw LOCAL int Count_Connections PARAMS(( struct sockaddr_in addr ));
99 4a111033 2001-12-29 alex LOCAL fd_set My_Listeners;
100 1c8eb478 2001-12-12 alex LOCAL fd_set My_Sockets;
102 c40592d2 2003-12-26 alex #ifdef TCPWRAP
103 8adff592 2005-03-19 fw int allow_severity = LOG_INFO;
104 8adff592 2005-03-19 fw int deny_severity = LOG_ERR;
108 8ac701d6 2005-06-01 fw FreeRes_stat( CONNECTION *c )
110 8ac701d6 2005-06-01 fw assert( c != NULL );
111 8ac701d6 2005-06-01 fw assert( c->res_stat != NULL );
113 8ac701d6 2005-06-01 fw if (!c->res_stat) return;
115 8ac701d6 2005-06-01 fw FD_CLR( c->res_stat->pipe[0], &Resolver_FDs );
117 8ac701d6 2005-06-01 fw close( c->res_stat->pipe[0] );
118 8ac701d6 2005-06-01 fw close( c->res_stat->pipe[1] );
120 8ac701d6 2005-06-01 fw free( c->res_stat );
121 8ac701d6 2005-06-01 fw c->res_stat = NULL;
126 8adff592 2005-03-19 fw Conn_Init( void )
128 bc140df8 2001-12-29 alex /* Modul initialisieren: statische Strukturen "ausnullen". */
132 b991de0f 2002-11-02 alex /* Speicher fuer Verbindungs-Pool anfordern */
133 b991de0f 2002-11-02 alex Pool_Size = CONNECTION_POOL;
134 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
136 b991de0f 2002-11-02 alex /* konfiguriertes Limit beachten */
137 b991de0f 2002-11-02 alex if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
139 b4363162 2005-04-16 fw My_Connections = (CONNECTION *) calloc( Pool_Size, sizeof( CONNECTION ) );
140 b991de0f 2002-11-02 alex if( ! My_Connections )
142 b991de0f 2002-11-02 alex /* Speicher konnte nicht alloziert werden! */
143 b991de0f 2002-11-02 alex Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
146 7b6e2662 2003-11-05 alex #ifdef DEBUG
147 b9f16c96 2003-08-30 alex Log( LOG_DEBUG, "Allocated connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
150 1c8eb478 2001-12-12 alex /* zu Beginn haben wir keine Verbindungen */
151 4a111033 2001-12-29 alex FD_ZERO( &My_Listeners );
152 1c8eb478 2001-12-12 alex FD_ZERO( &My_Sockets );
154 b991de0f 2002-11-02 alex /* Groesster File-Descriptor fuer select() */
155 c2f60abe 2002-05-27 alex Conn_MaxFD = 0;
157 1c8eb478 2001-12-12 alex /* Connection-Struktur initialisieren */
158 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
160 a02bf31d 2002-12-18 alex /* Global write counter */
161 a02bf31d 2002-12-18 alex WCounter = 0;
162 5fefe1a3 2001-12-12 alex } /* Conn_Init */
166 8adff592 2005-03-19 fw Conn_Exit( void )
168 bc140df8 2001-12-29 alex /* Modul abmelden: alle noch offenen Connections
169 bc140df8 2001-12-29 alex * schliessen und freigeben. */
171 cf050519 2001-12-14 alex CONN_ID idx;
174 7b6e2662 2003-11-05 alex #ifdef DEBUG
175 79809118 2002-01-06 alex Log( LOG_DEBUG, "Shutting down all connections ..." );
178 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
179 29bd35bc 2003-02-23 alex Rendezvous_UnregisterListeners( );
182 29bd35bc 2003-02-23 alex /* Sockets schliessen */
183 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
185 1c8eb478 2001-12-12 alex if( FD_ISSET( i, &My_Sockets ))
187 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ )
189 1c8eb478 2001-12-12 alex if( My_Connections[idx].sock == i ) break;
191 6da91c34 2002-03-02 alex if( FD_ISSET( i, &My_Listeners ))
193 1c8eb478 2001-12-12 alex close( i );
194 7b6e2662 2003-11-05 alex #ifdef DEBUG
195 79809118 2002-01-06 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
198 b991de0f 2002-11-02 alex else if( idx < Pool_Size )
200 8adff592 2005-03-19 fw if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", true );
201 8adff592 2005-03-19 fw else Conn_Close( idx, NULL, "Server going down", true );
205 6da91c34 2002-03-02 alex Log( LOG_WARNING, "Closing unknown connection %d ...", i );
206 1c8eb478 2001-12-12 alex close( i );
211 b991de0f 2002-11-02 alex free( My_Connections );
212 b991de0f 2002-11-02 alex My_Connections = NULL;
213 b991de0f 2002-11-02 alex Pool_Size = 0;
214 5fefe1a3 2001-12-12 alex } /* Conn_Exit */
218 8adff592 2005-03-19 fw Conn_InitListeners( void )
220 e68cdf30 2002-12-30 alex /* Initialize ports on which the server should accept connections */
222 8adff592 2005-03-19 fw int created, i;
224 a0c032b2 2002-11-22 alex created = 0;
225 a0c032b2 2002-11-22 alex for( i = 0; i < Conf_ListenPorts_Count; i++ )
227 a0c032b2 2002-11-22 alex if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
228 326607ee 2005-03-20 fw else Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] );
230 a0c032b2 2002-11-22 alex return created;
231 a0c032b2 2002-11-22 alex } /* Conn_InitListeners */
235 8adff592 2005-03-19 fw Conn_ExitListeners( void )
237 e68cdf30 2002-12-30 alex /* Close down all listening sockets */
241 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
242 29bd35bc 2003-02-23 alex Rendezvous_UnregisterListeners( );
245 a0c032b2 2002-11-22 alex Log( LOG_INFO, "Shutting down all listening sockets ..." );
246 a0c032b2 2002-11-22 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
248 a0c032b2 2002-11-22 alex if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
250 a0c032b2 2002-11-22 alex close( i );
251 7b6e2662 2003-11-05 alex #ifdef DEBUG
252 a0c032b2 2002-11-22 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
256 a0c032b2 2002-11-22 alex } /* Conn_ExitListeners */
260 8adff592 2005-03-19 fw Conn_NewListener( const UINT16 Port )
262 e68cdf30 2002-12-30 alex /* Create new listening socket on specified port */
264 1c8eb478 2001-12-12 alex struct sockaddr_in addr;
265 e33ab903 2003-09-11 alex struct in_addr inaddr;
267 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
268 8adff592 2005-03-19 fw char name[CLIENT_ID_LEN], *info;
271 5fefe1a3 2001-12-12 alex /* Server-"Listen"-Socket initialisieren */
272 1c8eb478 2001-12-12 alex memset( &addr, 0, sizeof( addr ));
273 e33ab903 2003-09-11 alex memset( &inaddr, 0, sizeof( inaddr ));
274 1c8eb478 2001-12-12 alex addr.sin_family = AF_INET;
275 1c8eb478 2001-12-12 alex addr.sin_port = htons( Port );
276 e33ab903 2003-09-11 alex if( Conf_ListenAddress[0] )
278 e33ab903 2003-09-11 alex #ifdef HAVE_INET_ATON
279 e33ab903 2003-09-11 alex if( inet_aton( Conf_ListenAddress, &inaddr ) == 0 )
281 e33ab903 2003-09-11 alex inaddr.s_addr = inet_addr( Conf_ListenAddress );
282 e33ab903 2003-09-11 alex if( inaddr.s_addr == (unsigned)-1 )
285 e33ab903 2003-09-11 alex Log( LOG_CRIT, "Can't listen on %s:%u: can't convert ip address %s!", Conf_ListenAddress, Port, Conf_ListenAddress );
286 8adff592 2005-03-19 fw return false;
289 e33ab903 2003-09-11 alex else inaddr.s_addr = htonl( INADDR_ANY );
290 e33ab903 2003-09-11 alex addr.sin_addr = inaddr;
292 5fefe1a3 2001-12-12 alex /* Socket erzeugen */
293 1c8eb478 2001-12-12 alex sock = socket( PF_INET, SOCK_STREAM, 0);
294 fb55c443 2001-12-13 alex if( sock < 0 )
296 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
297 8adff592 2005-03-19 fw return false;
300 8adff592 2005-03-19 fw if( ! Init_Socket( sock )) return false;
302 5fefe1a3 2001-12-12 alex /* an Port binden */
303 1c8eb478 2001-12-12 alex if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
305 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
306 1c8eb478 2001-12-12 alex close( sock );
307 8adff592 2005-03-19 fw return false;
310 5fefe1a3 2001-12-12 alex /* in "listen mode" gehen :-) */
311 1c8eb478 2001-12-12 alex if( listen( sock, 10 ) != 0 )
313 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
314 1c8eb478 2001-12-12 alex close( sock );
315 8adff592 2005-03-19 fw return false;
318 1c8eb478 2001-12-12 alex /* Neuen Listener in Strukturen einfuegen */
319 4a111033 2001-12-29 alex FD_SET( sock, &My_Listeners );
320 1c8eb478 2001-12-12 alex FD_SET( sock, &My_Sockets );
322 c2f60abe 2002-05-27 alex if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
324 e33ab903 2003-09-11 alex if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock );
325 e33ab903 2003-09-11 alex else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock );
327 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
328 29bd35bc 2003-02-23 alex /* Get best server description text */
329 29bd35bc 2003-02-23 alex if( ! Conf_ServerInfo[0] ) info = Conf_ServerName;
332 29bd35bc 2003-02-23 alex /* Use server info string */
333 29bd35bc 2003-02-23 alex info = NULL;
334 29bd35bc 2003-02-23 alex if( Conf_ServerInfo[0] == '[' )
336 29bd35bc 2003-02-23 alex /* Cut off leading hostname part in "[]" */
337 29bd35bc 2003-02-23 alex info = strchr( Conf_ServerInfo, ']' );
341 29bd35bc 2003-02-23 alex while( *info == ' ' ) info++;
344 29bd35bc 2003-02-23 alex if( ! info ) info = Conf_ServerInfo;
347 29bd35bc 2003-02-23 alex /* Add port number to description if non-standard */
348 29bd35bc 2003-02-23 alex if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port );
349 29bd35bc 2003-02-23 alex else strlcpy( name, info, sizeof( name ));
351 29bd35bc 2003-02-23 alex /* Register service */
352 29bd35bc 2003-02-23 alex Rendezvous_Register( name, RENDEZVOUS_TYPE, Port );
356 08cf5607 2001-12-26 alex } /* Conn_NewListener */
360 8adff592 2005-03-19 fw Conn_Handler( void )
362 6c19b0e4 2002-09-07 alex /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
363 6c19b0e4 2002-09-07 alex * werden dabei durchgefuehrt, bis der Server terminieren oder neu
364 6c19b0e4 2002-09-07 alex * starten soll:
366 bc140df8 2001-12-29 alex * - neue Verbindungen annehmen,
367 03d971d9 2002-01-02 alex * - Server-Verbindungen aufbauen,
368 bc140df8 2001-12-29 alex * - geschlossene Verbindungen loeschen,
369 bc140df8 2001-12-29 alex * - volle Schreibpuffer versuchen zu schreiben,
370 bc140df8 2001-12-29 alex * - volle Lesepuffer versuchen zu verarbeiten,
371 bc140df8 2001-12-29 alex * - Antworten von Resolver Sub-Prozessen annehmen.
374 63758dd7 2001-12-15 alex fd_set read_sockets, write_sockets;
375 747fd2f0 2001-12-13 alex struct timeval tv;
376 7b6cfc17 2002-08-26 alex time_t start, t;
377 e68cdf30 2002-12-30 alex CONN_ID i, idx;
378 8adff592 2005-03-19 fw bool timeout;
380 9ab186c4 2001-12-25 alex start = time( NULL );
381 397c5e2e 2002-12-19 alex while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart ))
383 8adff592 2005-03-19 fw timeout = true;
385 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
386 29bd35bc 2003-02-23 alex Rendezvous_Handler( );
389 397c5e2e 2002-12-19 alex /* Should the configuration be reloaded? */
390 397c5e2e 2002-12-19 alex if( NGIRCd_SignalRehash ) NGIRCd_Rehash( );
392 397c5e2e 2002-12-19 alex /* Check configured servers and established links */
393 397c5e2e 2002-12-19 alex Check_Servers( );
394 65bdfdf2 2001-12-26 alex Check_Connections( );
396 7b6e2662 2003-11-05 alex t = time( NULL );
398 65bdfdf2 2001-12-26 alex /* noch volle Lese-Buffer suchen */
399 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
401 7b6e2662 2003-11-05 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ) &&
402 7b6e2662 2003-11-05 alex ( My_Connections[i].delaytime < t ))
404 65bdfdf2 2001-12-26 alex /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
405 8adff592 2005-03-19 fw if( Handle_Buffer( i )) timeout = false;
409 65bdfdf2 2001-12-26 alex /* noch volle Schreib-Puffer suchen */
410 65bdfdf2 2001-12-26 alex FD_ZERO( &write_sockets );
411 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
413 c40592d2 2003-12-26 alex #ifdef ZLIB
414 65f3adca 2002-11-26 alex if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
416 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
419 65bdfdf2 2001-12-26 alex /* Socket der Verbindung in Set aufnehmen */
420 65bdfdf2 2001-12-26 alex FD_SET( My_Connections[i].sock, &write_sockets );
424 6da91c34 2002-03-02 alex /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
425 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
427 e7c2c86a 2005-04-23 fw if ( My_Connections[i].sock > NONE ) {
428 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING )) {
429 e7c2c86a 2005-04-23 fw FD_SET( My_Connections[i].sock, &write_sockets );
435 4a111033 2001-12-29 alex /* von welchen Sockets koennte gelesen werden? */
436 9ab186c4 2001-12-25 alex read_sockets = My_Sockets;
437 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
439 e7c2c86a 2005-04-23 fw if ( My_Connections[i].sock > NONE ) {
440 8ac701d6 2005-06-01 fw if ( My_Connections[i].res_stat ) {
441 e7c2c86a 2005-04-23 fw /* wait for completion of Resolver Sub-Process */
442 e7c2c86a 2005-04-23 fw FD_CLR( My_Connections[i].sock, &read_sockets );
446 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING )) {
447 e7c2c86a 2005-04-23 fw /* wait for completion of connect() */
448 e7c2c86a 2005-04-23 fw FD_CLR( My_Connections[i].sock, &read_sockets );
452 7b6cfc17 2002-08-26 alex if( My_Connections[i].delaytime > t )
454 7b6cfc17 2002-08-26 alex /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
455 7b6cfc17 2002-08-26 alex FD_CLR( My_Connections[i].sock, &read_sockets );
458 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
460 4a111033 2001-12-29 alex /* Pipes von Resolver Sub-Prozessen aufnehmen */
461 c2f60abe 2002-05-27 alex if( FD_ISSET( i, &Resolver_FDs ))
463 4a111033 2001-12-29 alex FD_SET( i, &read_sockets );
467 6c19b0e4 2002-09-07 alex /* Timeout initialisieren */
468 5ecb00ed 2002-11-23 alex tv.tv_usec = 0;
469 7b6e2662 2003-11-05 alex if( timeout ) tv.tv_sec = 1;
470 5ecb00ed 2002-11-23 alex else tv.tv_sec = 0;
472 4a111033 2001-12-29 alex /* Auf Aktivitaet warten */
473 8a927a1b 2002-09-07 alex i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
474 8a927a1b 2002-09-07 alex if( i == 0 )
476 8a927a1b 2002-09-07 alex /* keine Veraenderung an den Sockets */
479 8a927a1b 2002-09-07 alex if( i == -1 )
481 8a927a1b 2002-09-07 alex /* Fehler (z.B. Interrupt) */
482 db58d347 2002-01-05 alex if( errno != EINTR )
484 b840be98 2002-10-15 alex Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
485 62796722 2003-03-31 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
491 9ab186c4 2001-12-25 alex /* Koennen Daten geschrieben werden? */
492 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
494 3cf845fb 2002-10-14 alex if( ! FD_ISSET( i, &write_sockets )) continue;
496 3cf845fb 2002-10-14 alex /* Es kann geschrieben werden ... */
497 3cf845fb 2002-10-14 alex idx = Socket2Index( i );
498 b840be98 2002-10-15 alex if( idx == NONE ) continue;
500 3cf845fb 2002-10-14 alex if( ! Handle_Write( idx ))
502 3cf845fb 2002-10-14 alex /* Fehler beim Schreiben! Diesen Socket nun
503 3cf845fb 2002-10-14 alex * auch aus dem Read-Set entfernen: */
504 3cf845fb 2002-10-14 alex FD_CLR( i, &read_sockets );
508 9ab186c4 2001-12-25 alex /* Daten zum Lesen vorhanden? */
509 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
511 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
515 397c5e2e 2002-12-19 alex if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
516 397c5e2e 2002-12-19 alex else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
517 1c8eb478 2001-12-12 alex } /* Conn_Handler */
520 f7327524 2002-05-30 alex #ifdef PROTOTYPES
522 8adff592 2005-03-19 fw Conn_WriteStr( CONN_ID Idx, char *Format, ... )
525 f7327524 2002-05-30 alex Conn_WriteStr( Idx, Format, va_alist )
526 f7327524 2002-05-30 alex CONN_ID Idx;
527 8adff592 2005-03-19 fw char *Format;
531 63758dd7 2001-12-15 alex /* String in Socket schreiben. CR+LF wird von dieser Funktion
532 d5c97f81 2001-12-23 alex * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
533 8adff592 2005-03-19 fw * getrennt und false geliefert. */
535 8adff592 2005-03-19 fw char buffer[COMMAND_LEN];
537 d5c97f81 2001-12-23 alex va_list ap;
539 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
540 28c5a21f 2002-03-14 alex assert( Format != NULL );
542 f7327524 2002-05-30 alex #ifdef PROTOTYPES
543 d5c97f81 2001-12-23 alex va_start( ap, Format );
545 f7327524 2002-05-30 alex va_start( ap );
547 a40a026d 2005-01-19 alex if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) >= COMMAND_LEN - 2 )
549 79809118 2002-01-06 alex Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
550 8adff592 2005-03-19 fw Conn_Close( Idx, "Text too long to send!", NULL, false );
551 8adff592 2005-03-19 fw return false;
554 7c91951d 2001-12-25 alex #ifdef SNIFFER
555 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
558 6626395c 2002-12-26 alex strlcat( buffer, "\r\n", sizeof( buffer ));
559 446df061 2001-12-24 alex ok = Conn_Write( Idx, buffer, strlen( buffer ));
560 39219230 2002-12-02 alex My_Connections[Idx].msg_out++;
562 d5c97f81 2001-12-23 alex va_end( ap );
564 63758dd7 2001-12-15 alex } /* Conn_WriteStr */
568 8adff592 2005-03-19 fw Conn_Write( CONN_ID Idx, char *Data, int Len )
570 63758dd7 2001-12-15 alex /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
571 8adff592 2005-03-19 fw * der Client disconnectiert und false geliefert. */
573 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
574 63758dd7 2001-12-15 alex assert( Data != NULL );
575 63758dd7 2001-12-15 alex assert( Len > 0 );
577 65f3adca 2002-11-26 alex /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
578 65f3adca 2002-11-26 alex * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
579 65f3adca 2002-11-26 alex * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
580 65f3adca 2002-11-26 alex * In diesem Fall wird hier einfach ein Fehler geliefert. */
581 67d79b92 2002-09-26 alex if( My_Connections[Idx].sock <= NONE )
583 7b6e2662 2003-11-05 alex #ifdef DEBUG
584 c50ecda7 2002-09-26 alex Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
586 8adff592 2005-03-19 fw return false;
589 65f3adca 2002-11-26 alex /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
590 65f3adca 2002-11-26 alex * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
591 65f3adca 2002-11-26 alex * Socket zu schreiben (u.a. wg. Komprimierung). */
592 63758dd7 2001-12-15 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
594 65f3adca 2002-11-26 alex /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
595 65f3adca 2002-11-26 alex * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
596 ae98008c 2005-06-04 fw if( ! Handle_Write( Idx )) return false;
598 65f3adca 2002-11-26 alex /* nun neu pruefen: */
599 65f3adca 2002-11-26 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
601 65f3adca 2002-11-26 alex Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
602 8adff592 2005-03-19 fw Conn_Close( Idx, "Write buffer overflow!", NULL, false );
603 8adff592 2005-03-19 fw return false;
607 c40592d2 2003-12-26 alex #ifdef ZLIB
608 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
609 65f3adca 2002-11-26 alex /* Daten komprimieren und in Puffer kopieren */
610 8adff592 2005-03-19 fw if( ! Zip_Buffer( Idx, Data, Len )) return false;
615 65f3adca 2002-11-26 alex /* Daten in Puffer kopieren */
616 65f3adca 2002-11-26 alex memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
617 65f3adca 2002-11-26 alex My_Connections[Idx].wdatalen += Len;
618 65f3adca 2002-11-26 alex My_Connections[Idx].bytes_out += Len;
621 a02bf31d 2002-12-18 alex /* Adjust global write counter */
622 a02bf31d 2002-12-18 alex WCounter += Len;
625 63758dd7 2001-12-15 alex } /* Conn_Write */
629 8adff592 2005-03-19 fw Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
631 e21b9d84 2002-12-27 alex /* Close connection. Open pipes of asyncronous resolver
632 e21b9d84 2002-12-27 alex * sub-processes are closed down. */
636 8adff592 2005-03-19 fw double in_k, out_k;
637 c40592d2 2003-12-26 alex #ifdef ZLIB
638 8adff592 2005-03-19 fw double in_z_k, out_z_k;
639 8adff592 2005-03-19 fw int in_p, out_p;
642 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
644 cf4ad8d6 2003-02-21 alex /* Is this link already shutting down? */
645 e7c2c86a 2005-04-23 fw if( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ISCLOSING )) {
646 cf4ad8d6 2003-02-21 alex /* Conn_Close() has been called recursively for this link;
647 ae98008c 2005-06-04 fw * probabe reason: Handle_Write() failed -- see below. */
648 58736b39 2004-12-22 alex #ifdef DEBUG
649 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Recursive request to close connection: %d", Idx );
654 1d8da4b5 2004-04-25 alex assert( My_Connections[Idx].sock > NONE );
656 cf4ad8d6 2003-02-21 alex /* Mark link as "closing" */
657 e7c2c86a 2005-04-23 fw Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCLOSING );
659 33f4e676 2004-02-28 alex if( LogMsg ) txt = LogMsg;
660 33f4e676 2004-02-28 alex else txt = FwdMsg;
661 33f4e676 2004-02-28 alex if( ! txt ) txt = "Reason unknown";
663 33f4e676 2004-02-28 alex Log( LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx, LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ));
665 e21b9d84 2002-12-27 alex /* Search client, if any */
666 f060db5e 2002-10-09 alex c = Client_GetFromConn( Idx );
668 e21b9d84 2002-12-27 alex /* Should the client be informed? */
669 79809118 2002-01-06 alex if( InformClient )
671 1d8cb34b 2002-11-29 alex #ifndef STRICT_RFC
672 e21b9d84 2002-12-27 alex /* Send statistics to client if registered as user: */
673 f060db5e 2002-10-09 alex if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
675 8adff592 2005-03-19 fw 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 );
679 e21b9d84 2002-12-27 alex /* Send ERROR to client (see RFC!) */
680 79809118 2002-01-06 alex if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
681 79809118 2002-01-06 alex else Conn_WriteStr( Idx, "ERROR :Closing connection." );
684 e21b9d84 2002-12-27 alex /* Try to write out the write buffer */
685 ae98008c 2005-06-04 fw (void)Handle_Write( Idx );
687 e21b9d84 2002-12-27 alex /* Shut down socket */
688 7c91951d 2001-12-25 alex if( close( My_Connections[Idx].sock ) != 0 )
690 cd0dc862 2005-05-22 alex /* Oops, we can't close the socket!? This is ... ugly! */
691 cd0dc862 2005-05-22 alex Log( LOG_CRIT, "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
694 e21b9d84 2002-12-27 alex /* Mark socket as invalid: */
695 3cf845fb 2002-10-14 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
696 3cf845fb 2002-10-14 alex My_Connections[Idx].sock = NONE;
698 e21b9d84 2002-12-27 alex /* If there is still a client, unregister it now */
699 8adff592 2005-03-19 fw if( c ) Client_Destroy( c, LogMsg, FwdMsg, true );
701 e21b9d84 2002-12-27 alex /* Calculate statistics and log information */
702 8adff592 2005-03-19 fw in_k = (double)My_Connections[Idx].bytes_in / 1024;
703 8adff592 2005-03-19 fw out_k = (double)My_Connections[Idx].bytes_out / 1024;
704 c40592d2 2003-12-26 alex #ifdef ZLIB
705 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
706 8adff592 2005-03-19 fw in_z_k = (double)My_Connections[Idx].zip.bytes_in / 1024;
707 8adff592 2005-03-19 fw out_z_k = (double)My_Connections[Idx].zip.bytes_out / 1024;
708 8adff592 2005-03-19 fw in_p = (int)(( in_k * 100 ) / in_z_k );
709 8adff592 2005-03-19 fw out_p = (int)(( out_k * 100 ) / out_z_k );
710 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 );
715 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 );
718 e21b9d84 2002-12-27 alex /* Is there a resolver sub-process running? */
719 4a111033 2001-12-29 alex if( My_Connections[Idx].res_stat )
720 8ac701d6 2005-06-01 fw FreeRes_stat( &My_Connections[Idx] );
722 e21b9d84 2002-12-27 alex /* Servers: Modify time of next connect attempt? */
723 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
725 c40592d2 2003-12-26 alex #ifdef ZLIB
726 e21b9d84 2002-12-27 alex /* Clean up zlib, if link was compressed */
727 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
728 65f3adca 2002-11-26 alex inflateEnd( &My_Connections[Idx].zip.in );
729 65f3adca 2002-11-26 alex deflateEnd( &My_Connections[Idx].zip.out );
733 e21b9d84 2002-12-27 alex /* Clean up connection structure (=free it) */
734 3cf845fb 2002-10-14 alex Init_Conn_Struct( Idx );
736 58736b39 2004-12-22 alex #ifdef DEBUG
737 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Shutdown of connection %d completed.", Idx );
739 7c91951d 2001-12-25 alex } /* Conn_Close */
743 8adff592 2005-03-19 fw Conn_SyncServerStruct( void )
745 59a0fb8c 2003-03-27 alex /* Synchronize server structures (connection IDs):
746 59a0fb8c 2003-03-27 alex * connections <-> configuration */
748 59a0fb8c 2003-03-27 alex CLIENT *client;
752 59a0fb8c 2003-03-27 alex for( i = 0; i < Pool_Size; i++ )
754 59a0fb8c 2003-03-27 alex /* Established connection? */
755 59a0fb8c 2003-03-27 alex if( My_Connections[i].sock <= NONE ) continue;
757 59a0fb8c 2003-03-27 alex /* Server connection? */
758 59a0fb8c 2003-03-27 alex client = Client_GetFromConn( i );
759 59a0fb8c 2003-03-27 alex if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue;
761 59a0fb8c 2003-03-27 alex for( c = 0; c < MAX_SERVERS; c++ )
763 59a0fb8c 2003-03-27 alex /* Configured server? */
764 59a0fb8c 2003-03-27 alex if( ! Conf_Server[c].host[0] ) continue;
766 59a0fb8c 2003-03-27 alex /* Duplicate? */
767 59a0fb8c 2003-03-27 alex if( strcmp( Conf_Server[c].name, Client_ID( client )) == 0 ) Conf_Server[c].conn_id = i;
770 59a0fb8c 2003-03-27 alex } /* SyncServerStruct */
774 8adff592 2005-03-19 fw Handle_Read( int Sock )
776 bc140df8 2001-12-29 alex /* Aktivitaet auf einem Socket verarbeiten:
777 bc140df8 2001-12-29 alex * - neue Clients annehmen,
778 bc140df8 2001-12-29 alex * - Daten von Clients verarbeiten,
779 bc140df8 2001-12-29 alex * - Resolver-Rueckmeldungen annehmen. */
781 cf050519 2001-12-14 alex CONN_ID idx;
783 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
785 4a111033 2001-12-29 alex if( FD_ISSET( Sock, &My_Listeners ))
787 1c8eb478 2001-12-12 alex /* es ist einer unserer Listener-Sockets: es soll
788 1c8eb478 2001-12-12 alex * also eine neue Verbindung aufgebaut werden. */
790 1c8eb478 2001-12-12 alex New_Connection( Sock );
792 c2f60abe 2002-05-27 alex else if( FD_ISSET( Sock, &Resolver_FDs ))
794 4a111033 2001-12-29 alex /* Rueckmeldung von einem Resolver Sub-Prozess */
796 4a111033 2001-12-29 alex Read_Resolver_Result( Sock );
800 1c8eb478 2001-12-12 alex /* Ein Client Socket: entweder ein User oder Server */
802 1c8eb478 2001-12-12 alex idx = Socket2Index( Sock );
803 3cf845fb 2002-10-14 alex if( idx > NONE ) Read_Request( idx );
805 63758dd7 2001-12-15 alex } /* Handle_Read */
809 c2f60abe 2002-05-27 alex Handle_Write( CONN_ID Idx )
811 6da91c34 2002-03-02 alex /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
813 8adff592 2005-03-19 fw int len, res, err;
814 c3dfd63b 2003-04-21 alex socklen_t sock_len;
817 b840be98 2002-10-15 alex assert( Idx > NONE );
818 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
820 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ISCONNECTING )) {
821 e7c2c86a 2005-04-23 fw /* connect() has finished, check result */
823 e7c2c86a 2005-04-23 fw Conn_OPTION_DEL( &My_Connections[Idx], CONN_ISCONNECTING );
825 6da91c34 2002-03-02 alex /* Ergebnis des connect() ermitteln */
826 c3dfd63b 2003-04-21 alex sock_len = sizeof( err );
827 c3dfd63b 2003-04-21 alex res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
828 c3dfd63b 2003-04-21 alex assert( sock_len == sizeof( err ));
830 6da91c34 2002-03-02 alex /* Fehler aufgetreten? */
831 6da91c34 2002-03-02 alex if(( res != 0 ) || ( err != 0 ))
833 6da91c34 2002-03-02 alex /* Fehler! */
834 6da91c34 2002-03-02 alex if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
835 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 ));
837 9eee0c88 2003-01-15 alex /* Clean up socket, connection and client structures */
838 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
839 9eee0c88 2003-01-15 alex c = Client_GetFromConn( Idx );
840 9eee0c88 2003-01-15 alex if( c ) Client_DestroyNow( c );
841 6da91c34 2002-03-02 alex close( My_Connections[Idx].sock );
842 6da91c34 2002-03-02 alex Init_Conn_Struct( Idx );
844 cbce54e0 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
845 a2544e49 2002-12-30 alex Conf_Server[Conf_GetServer( Idx )].lasttry = time( NULL );
846 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
848 8adff592 2005-03-19 fw return false;
850 33f4e676 2004-02-28 alex Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", Idx, My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port );
852 33f4e676 2004-02-28 alex /* Send PASS and SERVER command to peer */
853 a2544e49 2002-12-30 alex Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[Conf_GetServer( Idx )].pwd_out, NGIRCd_ProtoID );
854 3cf845fb 2002-10-14 alex return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
857 c40592d2 2003-12-26 alex #ifdef ZLIB
858 ae98008c 2005-06-04 fw if(( My_Connections[Idx].wdatalen <= 0 ) && ( ! My_Connections[Idx].zip.wdatalen ))
861 ae98008c 2005-06-04 fw /* write buffer empty, but not compression buf? -> flush compression buf. */
862 65f3adca 2002-11-26 alex if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
864 ae98008c 2005-06-04 fw if( My_Connections[Idx].wdatalen <= 0 )
868 8ac701d6 2005-06-01 fw len = write( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen );
869 ae98008c 2005-06-04 fw if( len < 0 ) {
870 ae98008c 2005-06-04 fw if( errno == EAGAIN || errno == EINTR)
873 ae98008c 2005-06-04 fw Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx,
874 ae98008c 2005-06-04 fw My_Connections[Idx].sock, strerror( errno ));
875 8adff592 2005-03-19 fw Conn_Close( Idx, "Write error!", NULL, false );
876 8adff592 2005-03-19 fw return false;
879 ae98008c 2005-06-04 fw /* Update buffer len and move any data not yet written to beginning of buf */
880 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen -= len;
881 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
884 63758dd7 2001-12-15 alex } /* Handle_Write */
888 8adff592 2005-03-19 fw New_Connection( int Sock )
890 d5c97f81 2001-12-23 alex /* Neue Client-Verbindung von Listen-Socket annehmen und
891 d5c97f81 2001-12-23 alex * CLIENT-Struktur anlegen. */
893 c40592d2 2003-12-26 alex #ifdef TCPWRAP
894 439c945d 2003-03-07 alex struct request_info req;
896 1c8eb478 2001-12-12 alex struct sockaddr_in new_addr;
897 8adff592 2005-03-19 fw int new_sock, new_sock_len;
898 4a111033 2001-12-29 alex RES_STAT *s;
899 cf050519 2001-12-14 alex CONN_ID idx;
901 b991de0f 2002-11-02 alex POINTER *ptr;
902 8adff592 2005-03-19 fw long new_size, cnt;
904 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
906 c6b9f3a1 2002-11-20 alex /* Connection auf Listen-Socket annehmen */
907 1c8eb478 2001-12-12 alex new_sock_len = sizeof( new_addr );
908 fb55c443 2001-12-13 alex new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
909 1c8eb478 2001-12-12 alex if( new_sock < 0 )
911 446df061 2001-12-24 alex Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
915 c40592d2 2003-12-26 alex #ifdef TCPWRAP
916 439c945d 2003-03-07 alex /* Validate socket using TCP Wrappers */
917 62796722 2003-03-31 alex request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL );
918 b90bedbc 2004-02-03 alex fromhost(&req);
919 439c945d 2003-03-07 alex if( ! hosts_access( &req ))
921 439c945d 2003-03-07 alex /* Access denied! */
922 439c945d 2003-03-07 alex Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr ));
923 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection refused" );
924 439c945d 2003-03-07 alex close( new_sock );
929 c6b9f3a1 2002-11-20 alex /* Socket initialisieren */
930 c6b9f3a1 2002-11-20 alex Init_Socket( new_sock );
932 2981fe9e 2003-11-05 alex /* Check IP-based connection limit */
933 2981fe9e 2003-11-05 alex cnt = Count_Connections( new_addr );
934 2981fe9e 2003-11-05 alex if(( Conf_MaxConnectionsIP > 0 ) && ( cnt >= Conf_MaxConnectionsIP ))
936 c79fa28a 2004-10-04 alex /* Access denied, too many connections from this IP address! */
937 c79fa28a 2004-10-04 alex Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP address!", inet_ntoa( new_addr.sin_addr ), cnt);
938 c79fa28a 2004-10-04 alex Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" );
939 2981fe9e 2003-11-05 alex close( new_sock );
943 3cf845fb 2002-10-14 alex /* Freie Connection-Struktur suchen */
944 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
945 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
947 b991de0f 2002-11-02 alex new_size = Pool_Size + CONNECTION_POOL;
949 b991de0f 2002-11-02 alex /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
950 b991de0f 2002-11-02 alex * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
952 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
954 b991de0f 2002-11-02 alex /* Es ist ein Limit konfiguriert */
955 b991de0f 2002-11-02 alex if( Pool_Size >= Conf_MaxConnections )
957 b991de0f 2002-11-02 alex /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
958 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
959 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection limit reached" );
960 b991de0f 2002-11-02 alex close( new_sock );
963 b991de0f 2002-11-02 alex if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
965 e68cdf30 2002-12-30 alex if( new_size < Pool_Size )
967 8304210c 2005-04-16 fw Log( LOG_ALERT, "Can't accept connection: limit (%d) reached -- overflow!", Pool_Size );
968 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection limit reached" );
969 e68cdf30 2002-12-30 alex close( new_sock );
973 cb76d96e 2004-03-11 alex ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
974 b991de0f 2002-11-02 alex if( ! ptr )
976 429b5f3e 2005-01-17 alex Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
977 429b5f3e 2005-01-17 alex Simple_Message( new_sock, "ERROR: Internal error" );
978 429b5f3e 2005-01-17 alex close( new_sock );
982 7b6e2662 2003-11-05 alex #ifdef DEBUG
983 429b5f3e 2005-01-17 alex Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
986 902ad912 2002-12-17 alex /* Adjust pointer to new block */
987 cb76d96e 2004-03-11 alex My_Connections = (CONNECTION *)ptr;
989 902ad912 2002-12-17 alex /* Initialize new items */
990 902ad912 2002-12-17 alex for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
991 902ad912 2002-12-17 alex idx = Pool_Size;
993 902ad912 2002-12-17 alex /* Adjust new pool size */
994 b991de0f 2002-11-02 alex Pool_Size = new_size;
997 d5c97f81 2001-12-23 alex /* Client-Struktur initialisieren */
998 8adff592 2005-03-19 fw c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false );
1001 d5c97f81 2001-12-23 alex Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1002 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Internal error" );
1003 d5c97f81 2001-12-23 alex close( new_sock );
1007 1c8eb478 2001-12-12 alex /* Verbindung registrieren */
1008 4a111033 2001-12-29 alex Init_Conn_Struct( idx );
1009 1c8eb478 2001-12-12 alex My_Connections[idx].sock = new_sock;
1010 1c8eb478 2001-12-12 alex My_Connections[idx].addr = new_addr;
1012 1c8eb478 2001-12-12 alex /* Neuen Socket registrieren */
1013 1c8eb478 2001-12-12 alex FD_SET( new_sock, &My_Sockets );
1014 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1016 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 );
1018 4a111033 2001-12-29 alex /* Hostnamen ermitteln */
1019 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host ));
1020 ffb90f4c 2002-11-22 alex Client_SetHostname( c, My_Connections[idx].host );
1021 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
1022 b79b315d 2003-12-27 alex s = Resolve_Addr( &new_addr, My_Connections[idx].sock );
1024 c2f60abe 2002-05-27 alex s = Resolve_Addr( &new_addr );
1026 8ac701d6 2005-06-01 fw /* resolver process has been started */
1027 8ac701d6 2005-06-01 fw if( s ) My_Connections[idx].res_stat = s;
1029 7b6cfc17 2002-08-26 alex /* Penalty-Zeit setzen */
1030 736bacde 2002-10-10 alex Conn_SetPenalty( idx, 4 );
1031 1c8eb478 2001-12-12 alex } /* New_Connection */
1034 c2f60abe 2002-05-27 alex LOCAL CONN_ID
1035 8adff592 2005-03-19 fw Socket2Index( int Sock )
1037 63758dd7 2001-12-15 alex /* zum Socket passende Connection suchen */
1039 cf050519 2001-12-14 alex CONN_ID idx;
1041 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
1043 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1045 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1047 b840be98 2002-10-15 alex /* die Connection wurde vermutlich (wegen eines
1048 b840be98 2002-10-15 alex * Fehlers) bereits wieder abgebaut ... */
1049 7b6e2662 2003-11-05 alex #ifdef DEBUG
1050 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1052 b840be98 2002-10-15 alex return NONE;
1054 3cf845fb 2002-10-14 alex else return idx;
1055 1c8eb478 2001-12-12 alex } /* Socket2Index */
1059 c2f60abe 2002-05-27 alex Read_Request( CONN_ID Idx )
1061 747fd2f0 2001-12-13 alex /* Daten von Socket einlesen und entsprechend behandeln.
1062 747fd2f0 2001-12-13 alex * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1064 8adff592 2005-03-19 fw int len, bsize;
1065 c40592d2 2003-12-26 alex #ifdef ZLIB
1066 d3a6537f 2002-11-28 alex CLIENT *c;
1069 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1070 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
1072 d3a6537f 2002-11-28 alex /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
1073 d3a6537f 2002-11-28 alex * ansonsten koennen Daten ggf. nicht umkopiert werden. */
1074 d3a6537f 2002-11-28 alex bsize = READBUFFER_LEN;
1075 c40592d2 2003-12-26 alex #ifdef ZLIB
1076 d3a6537f 2002-11-28 alex c = Client_GetFromConn( Idx );
1077 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;
1080 c40592d2 2003-12-26 alex #ifdef ZLIB
1081 d3a6537f 2002-11-28 alex if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1083 d3a6537f 2002-11-28 alex if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
1086 54e487d4 2002-01-03 alex /* Der Lesepuffer ist voll */
1087 eee27628 2004-10-04 alex Log( LOG_ERR, "Receive buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1088 8adff592 2005-03-19 fw Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
1092 c40592d2 2003-12-26 alex #ifdef ZLIB
1093 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
1094 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 );
1095 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1100 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 );
1101 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].rdatalen += len;
1104 747fd2f0 2001-12-13 alex if( len == 0 )
1106 747fd2f0 2001-12-13 alex /* Socket wurde geschlossen */
1107 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 ));
1108 8adff592 2005-03-19 fw Conn_Close( Idx, "Socket closed!", "Client closed connection", false );
1112 747fd2f0 2001-12-13 alex if( len < 0 )
1114 c6b9f3a1 2002-11-20 alex /* Operation haette Socket "nur" blockiert ... */
1115 c6b9f3a1 2002-11-20 alex if( errno == EAGAIN ) return;
1117 747fd2f0 2001-12-13 alex /* Fehler beim Lesen */
1118 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1119 8adff592 2005-03-19 fw Conn_Close( Idx, "Read error!", "Client closed connection", false );
1123 f060db5e 2002-10-09 alex /* Connection-Statistik aktualisieren */
1124 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in += len;
1126 65bdfdf2 2001-12-26 alex /* Timestamp aktualisieren */
1127 65bdfdf2 2001-12-26 alex My_Connections[Idx].lastdata = time( NULL );
1129 9ab186c4 2001-12-25 alex Handle_Buffer( Idx );
1130 9ab186c4 2001-12-25 alex } /* Read_Request */
1134 c2f60abe 2002-05-27 alex Handle_Buffer( CONN_ID Idx )
1136 5ecb00ed 2002-11-23 alex /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1137 8adff592 2005-03-19 fw * Wurde ein Request verarbeitet, so wird true geliefert,
1138 8adff592 2005-03-19 fw * ansonsten false (auch bei Fehlern). */
1140 12cad28e 2002-10-21 alex #ifndef STRICT_RFC
1141 8adff592 2005-03-19 fw char *ptr1, *ptr2;
1144 8adff592 2005-03-19 fw int len, delta;
1145 8adff592 2005-03-19 fw bool action, result;
1146 c40592d2 2003-12-26 alex #ifdef ZLIB
1150 8adff592 2005-03-19 fw result = false;
1153 7b6e2662 2003-11-05 alex /* Check penalty */
1154 7b6e2662 2003-11-05 alex if( My_Connections[Idx].delaytime > time( NULL )) return result;
1156 c40592d2 2003-12-26 alex #ifdef ZLIB
1157 65f3adca 2002-11-26 alex /* ggf. noch unkomprimiete Daten weiter entpacken */
1158 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP ))
1159 8adff592 2005-03-19 fw if( ! Unzip_Buffer( Idx )) return false;
1162 65f3adca 2002-11-26 alex if( My_Connections[Idx].rdatalen < 1 ) break;
1164 65f3adca 2002-11-26 alex /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1165 65f3adca 2002-11-26 alex * RFC 2812. Haben wir eine? */
1166 65f3adca 2002-11-26 alex My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1167 65f3adca 2002-11-26 alex ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1169 65f3adca 2002-11-26 alex if( ptr ) delta = 2;
1170 65f3adca 2002-11-26 alex #ifndef STRICT_RFC
1173 65f3adca 2002-11-26 alex /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1174 65f3adca 2002-11-26 alex * machen soetwas viele Clients, u.a. "mIRC" :-( */
1175 65f3adca 2002-11-26 alex ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1176 65f3adca 2002-11-26 alex ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1177 65f3adca 2002-11-26 alex delta = 1;
1178 65f3adca 2002-11-26 alex if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1179 65f3adca 2002-11-26 alex else if( ptr1 ) ptr = ptr1;
1180 65f3adca 2002-11-26 alex else if( ptr2 ) ptr = ptr2;
1184 8adff592 2005-03-19 fw action = false;
1187 65f3adca 2002-11-26 alex /* Ende der Anfrage wurde gefunden */
1188 65f3adca 2002-11-26 alex *ptr = '\0';
1189 65f3adca 2002-11-26 alex len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1190 65f3adca 2002-11-26 alex if( len > ( COMMAND_LEN - 1 ))
1192 65f3adca 2002-11-26 alex /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1193 65f3adca 2002-11-26 alex * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1194 65f3adca 2002-11-26 alex * empfangen wird, wird der Client disconnectiert. */
1195 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 );
1196 8adff592 2005-03-19 fw Conn_Close( Idx, NULL, "Request too long", true );
1197 8adff592 2005-03-19 fw return false;
1200 c40592d2 2003-12-26 alex #ifdef ZLIB
1201 d3a6537f 2002-11-28 alex /* merken, ob Stream bereits komprimiert wird */
1202 d3a6537f 2002-11-28 alex old_z = My_Connections[Idx].options & CONN_ZIP;
1205 65f3adca 2002-11-26 alex if( len > delta )
1207 65f3adca 2002-11-26 alex /* Es wurde ein Request gelesen */
1208 39219230 2002-12-02 alex My_Connections[Idx].msg_in++;
1209 8adff592 2005-03-19 fw if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return false;
1210 8adff592 2005-03-19 fw else action = true;
1213 65f3adca 2002-11-26 alex /* Puffer anpassen */
1214 65f3adca 2002-11-26 alex My_Connections[Idx].rdatalen -= len;
1215 65f3adca 2002-11-26 alex memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1217 c40592d2 2003-12-26 alex #ifdef ZLIB
1218 d3a6537f 2002-11-28 alex if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1220 d3a6537f 2002-11-28 alex /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1221 d3a6537f 2002-11-28 alex * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1222 d3a6537f 2002-11-28 alex * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1223 58736b39 2004-12-22 alex if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1225 58736b39 2004-12-22 alex /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1226 58736b39 2004-12-22 alex Log( LOG_ALERT, "Can't move receive buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
1227 8adff592 2005-03-19 fw return false;
1229 58736b39 2004-12-22 alex memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1230 58736b39 2004-12-22 alex My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1231 58736b39 2004-12-22 alex My_Connections[Idx].rdatalen = 0;
1232 58736b39 2004-12-22 alex #ifdef DEBUG
1233 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1234 58736b39 2004-12-22 alex #endif /* DEBUG */
1236 58736b39 2004-12-22 alex #endif /* ZLIB */
1239 8adff592 2005-03-19 fw if( action ) result = true;
1240 65f3adca 2002-11-26 alex } while( action );
1242 65f3adca 2002-11-26 alex return result;
1243 9ab186c4 2001-12-25 alex } /* Handle_Buffer */
1247 8adff592 2005-03-19 fw Check_Connections( void )
1249 bc140df8 2001-12-29 alex /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1250 bc140df8 2001-12-29 alex * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1251 bc140df8 2001-12-29 alex * auch das nicht "hilft", Client disconnectieren. */
1253 03d971d9 2002-01-02 alex CLIENT *c;
1254 e68cdf30 2002-12-30 alex CONN_ID i;
1256 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1258 03d971d9 2002-01-02 alex if( My_Connections[i].sock == NONE ) continue;
1260 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1261 356683ff 2002-01-04 alex if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1263 03d971d9 2002-01-02 alex /* verbundener User, Server oder Service */
1264 65bdfdf2 2001-12-26 alex if( My_Connections[i].lastping > My_Connections[i].lastdata )
1266 65bdfdf2 2001-12-26 alex /* es wurde bereits ein PING gesendet */
1267 08cf5607 2001-12-26 alex if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1269 65bdfdf2 2001-12-26 alex /* Timeout */
1270 7b6e2662 2003-11-05 alex #ifdef DEBUG
1271 e39925af 2002-03-26 alex Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1273 8adff592 2005-03-19 fw Conn_Close( i, NULL, "Ping timeout", true );
1276 08cf5607 2001-12-26 alex else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1278 65bdfdf2 2001-12-26 alex /* es muss ein PING gesendet werden */
1279 7b6e2662 2003-11-05 alex #ifdef DEBUG
1280 65bdfdf2 2001-12-26 alex Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1282 65bdfdf2 2001-12-26 alex My_Connections[i].lastping = time( NULL );
1283 356683ff 2002-01-04 alex Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1288 03d971d9 2002-01-02 alex /* noch nicht vollstaendig aufgebaute Verbindung */
1289 03d971d9 2002-01-02 alex if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1291 03d971d9 2002-01-02 alex /* Timeout */
1292 7b6e2662 2003-11-05 alex #ifdef DEBUG
1293 140d1aa5 2002-02-27 alex Log( LOG_DEBUG, "Connection %d timed out ...", i );
1295 8adff592 2005-03-19 fw Conn_Close( i, NULL, "Timeout", false );
1299 03d971d9 2002-01-02 alex } /* Check_Connections */
1303 8adff592 2005-03-19 fw Check_Servers( void )
1305 a2544e49 2002-12-30 alex /* Check if we can establish further server links */
1307 03d971d9 2002-01-02 alex RES_STAT *s;
1308 a2544e49 2002-12-30 alex CONN_ID idx;
1311 a2544e49 2002-12-30 alex /* Serach all connections, are there results from the resolver? */
1312 a2544e49 2002-12-30 alex for( idx = 0; idx < Pool_Size; idx++ )
1314 a2544e49 2002-12-30 alex if( My_Connections[idx].sock != SERVER_WAIT ) continue;
1316 a2544e49 2002-12-30 alex /* IP resolved? */
1317 a2544e49 2002-12-30 alex if( My_Connections[idx].res_stat == NULL ) New_Server( Conf_GetServer( idx ), idx );
1320 a2544e49 2002-12-30 alex /* Check all configured servers */
1321 a2544e49 2002-12-30 alex for( i = 0; i < MAX_SERVERS; i++ )
1323 8dadb17f 2002-12-31 alex /* Valid outgoing server which isn't already connected or disabled? */
1324 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;
1326 a2544e49 2002-12-30 alex /* Is there already a connection in this group? */
1327 a2544e49 2002-12-30 alex if( Conf_Server[i].group > NONE )
1329 a2544e49 2002-12-30 alex for( n = 0; n < MAX_SERVERS; n++ )
1331 a2544e49 2002-12-30 alex if( n == i ) continue;
1332 a2544e49 2002-12-30 alex if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
1334 a2544e49 2002-12-30 alex if( n < MAX_SERVERS ) continue;
1337 a2544e49 2002-12-30 alex /* Check last connect attempt? */
1338 03d971d9 2002-01-02 alex if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1340 a2544e49 2002-12-30 alex /* Okay, try to connect now */
1341 03d971d9 2002-01-02 alex Conf_Server[i].lasttry = time( NULL );
1343 a2544e49 2002-12-30 alex /* Search free connection structure */
1344 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1345 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1347 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1350 7b6e2662 2003-11-05 alex #ifdef DEBUG
1351 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1354 03d971d9 2002-01-02 alex /* Verbindungs-Struktur initialisieren */
1355 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
1356 03d971d9 2002-01-02 alex My_Connections[idx].sock = SERVER_WAIT;
1357 a2544e49 2002-12-30 alex Conf_Server[i].conn_id = idx;
1359 a2544e49 2002-12-30 alex /* Resolve Hostname. If this fails, try to use it as an IP address */
1360 a2544e49 2002-12-30 alex strlcpy( Conf_Server[i].ip, Conf_Server[i].host, sizeof( Conf_Server[i].ip ));
1361 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host ));
1362 c2f60abe 2002-05-27 alex s = Resolve_Name( Conf_Server[i].host );
1364 8ac701d6 2005-06-01 fw /* resolver process running? */
1365 8ac701d6 2005-06-01 fw if( s ) My_Connections[idx].res_stat = s;
1367 03d971d9 2002-01-02 alex } /* Check_Servers */
1371 8adff592 2005-03-19 fw New_Server( int Server, CONN_ID Idx )
1373 83194a23 2003-04-25 alex /* Establish new server link */
1375 03d971d9 2002-01-02 alex struct sockaddr_in new_addr;
1376 03d971d9 2002-01-02 alex struct in_addr inaddr;
1377 8adff592 2005-03-19 fw int res, new_sock;
1378 54e487d4 2002-01-03 alex CLIENT *c;
1380 a4d5ca63 2002-11-05 alex assert( Server > NONE );
1381 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1383 83194a23 2003-04-25 alex /* Did we get a valid IP address? */
1384 03d971d9 2002-01-02 alex if( ! Conf_Server[Server].ip[0] )
1386 83194a23 2003-04-25 alex /* No. Free connection structure and abort: */
1387 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1388 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1389 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1393 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 );
1395 31a3bfed 2002-05-19 alex #ifdef HAVE_INET_ATON
1396 03d971d9 2002-01-02 alex if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1398 31a3bfed 2002-05-19 alex memset( &inaddr, 0, sizeof( inaddr ));
1399 31a3bfed 2002-05-19 alex inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1400 31a3bfed 2002-05-19 alex if( inaddr.s_addr == (unsigned)-1 )
1403 83194a23 2003-04-25 alex /* Can't convert IP address */
1404 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1405 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1406 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 );
1410 03d971d9 2002-01-02 alex memset( &new_addr, 0, sizeof( new_addr ));
1411 03d971d9 2002-01-02 alex new_addr.sin_family = AF_INET;
1412 03d971d9 2002-01-02 alex new_addr.sin_addr = inaddr;
1413 03d971d9 2002-01-02 alex new_addr.sin_port = htons( Conf_Server[Server].port );
1415 03d971d9 2002-01-02 alex new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1416 03d971d9 2002-01-02 alex if ( new_sock < 0 )
1418 83194a23 2003-04-25 alex /* Can't create socket */
1419 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1420 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1421 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1425 6da91c34 2002-03-02 alex if( ! Init_Socket( new_sock )) return;
1427 6250dcb1 2002-11-11 alex res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1428 6250dcb1 2002-11-11 alex if(( res != 0 ) && ( errno != EINPROGRESS ))
1430 83194a23 2003-04-25 alex /* Can't connect socket */
1431 6250dcb1 2002-11-11 alex Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1432 03d971d9 2002-01-02 alex close( new_sock );
1433 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1434 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1438 03d971d9 2002-01-02 alex /* Client-Struktur initialisieren */
1439 8adff592 2005-03-19 fw c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, false );
1442 83194a23 2003-04-25 alex /* Can't create new client structure */
1443 03d971d9 2002-01-02 alex close( new_sock );
1444 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1445 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1446 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1449 356683ff 2002-01-04 alex Client_SetIntroducer( c, c );
1450 1f975b6e 2002-04-08 alex Client_SetToken( c, TOKEN_OUTBOUND );
1452 83194a23 2003-04-25 alex /* Register connection */
1453 03d971d9 2002-01-02 alex My_Connections[Idx].sock = new_sock;
1454 03d971d9 2002-01-02 alex My_Connections[Idx].addr = new_addr;
1455 695631b2 2002-12-26 alex strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
1457 83194a23 2003-04-25 alex /* Register new socket */
1458 03d971d9 2002-01-02 alex FD_SET( new_sock, &My_Sockets );
1459 e7c2c86a 2005-04-23 fw Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
1461 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1463 7b6e2662 2003-11-05 alex #ifdef DEBUG
1464 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1466 03d971d9 2002-01-02 alex } /* New_Server */
1470 e68cdf30 2002-12-30 alex Init_Conn_Struct( CONN_ID Idx )
1472 bfba1f37 2005-03-20 fw time_t now = time( NULL );
1473 4a111033 2001-12-29 alex /* Connection-Struktur initialisieren */
1475 bfba1f37 2005-03-20 fw memset( &My_Connections[Idx], 0, sizeof ( CONNECTION ));
1476 4a111033 2001-12-29 alex My_Connections[Idx].sock = NONE;
1477 bfba1f37 2005-03-20 fw My_Connections[Idx].lastdata = now;
1478 bfba1f37 2005-03-20 fw My_Connections[Idx].lastprivmsg = now;
1479 4a111033 2001-12-29 alex } /* Init_Conn_Struct */
1483 8adff592 2005-03-19 fw Init_Socket( int Sock )
1485 bb98fd8c 2004-01-25 alex /* Initialize socket (set options) */
1489 bb98fd8c 2004-01-25 alex #ifdef O_NONBLOCK /* unknown on A/UX */
1490 6da91c34 2002-03-02 alex if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1492 bb98fd8c 2004-01-25 alex Log( LOG_CRIT, "Can't enable non-blocking mode for socket: %s!", strerror( errno ));
1493 6da91c34 2002-03-02 alex close( Sock );
1494 8adff592 2005-03-19 fw return false;
1498 bb98fd8c 2004-01-25 alex /* Don't block this port after socket shutdown */
1499 bb98fd8c 2004-01-25 alex value = 1;
1500 bb98fd8c 2004-01-25 alex if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &value, (socklen_t)sizeof( value )) != 0 )
1502 bb98fd8c 2004-01-25 alex Log( LOG_ERR, "Can't set socket option SO_REUSEADDR: %s!", strerror( errno ));
1503 bb98fd8c 2004-01-25 alex /* ignore this error */
1506 bb98fd8c 2004-01-25 alex /* Set type of service (TOS) */
1507 bb98fd8c 2004-01-25 alex #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
1508 bb98fd8c 2004-01-25 alex value = IPTOS_LOWDELAY;
1509 bb98fd8c 2004-01-25 alex #ifdef DEBUG
1510 bb98fd8c 2004-01-25 alex Log( LOG_DEBUG, "Setting option IP_TOS on socket %d to IPTOS_LOWDELAY (%d).", Sock, value );
1512 bb98fd8c 2004-01-25 alex if( setsockopt( Sock, SOL_IP, IP_TOS, &value, (socklen_t)sizeof( value )) != 0 )
1514 bb98fd8c 2004-01-25 alex Log( LOG_ERR, "Can't set socket option IP_TOS: %s!", strerror( errno ));
1515 bb98fd8c 2004-01-25 alex /* ignore this error */
1519 8adff592 2005-03-19 fw return true;
1520 6da91c34 2002-03-02 alex } /* Init_Socket */
1524 8adff592 2005-03-19 fw Read_Resolver_Result( int r_fd )
1526 3012c232 2004-05-11 alex /* Read result of resolver sub-process from pipe and update the
1527 3012c232 2004-05-11 alex * apropriate connection/client structure(s): hostname and/or
1528 3012c232 2004-05-11 alex * IDENT user name.*/
1530 4a111033 2001-12-29 alex CLIENT *c;
1531 8adff592 2005-03-19 fw int len, i, n;
1532 3012c232 2004-05-11 alex RES_STAT *s;
1535 b79b315d 2003-12-27 alex /* Search associated connection ... */
1536 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1538 3012c232 2004-05-11 alex if(( My_Connections[i].sock != NONE )
1539 3012c232 2004-05-11 alex && ( My_Connections[i].res_stat != NULL )
1540 3012c232 2004-05-11 alex && ( My_Connections[i].res_stat->pipe[0] == r_fd ))
1543 b991de0f 2002-11-02 alex if( i >= Pool_Size )
1545 b79b315d 2003-12-27 alex /* Ops, none found? Probably the connection has already
1546 3012c232 2004-05-11 alex * been closed!? We'll ignore that ... */
1547 3012c232 2004-05-11 alex FD_CLR( r_fd, &Resolver_FDs );
1548 4a111033 2001-12-29 alex close( r_fd );
1549 7b6e2662 2003-11-05 alex #ifdef DEBUG
1550 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1555 3012c232 2004-05-11 alex /* Get resolver structure */
1556 3012c232 2004-05-11 alex s = My_Connections[i].res_stat;
1557 3012c232 2004-05-11 alex assert( s != NULL );
1559 3012c232 2004-05-11 alex /* Read result from pipe */
1560 a49a580a 2004-05-30 alex len = read( r_fd, s->buffer + s->bufpos, sizeof( s->buffer ) - s->bufpos - 1 );
1561 3012c232 2004-05-11 alex if( len < 0 )
1563 3012c232 2004-05-11 alex /* Error! */
1564 3012c232 2004-05-11 alex Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1565 8ac701d6 2005-06-01 fw FreeRes_stat( &My_Connections[i] );
1568 3012c232 2004-05-11 alex s->bufpos += len;
1569 3012c232 2004-05-11 alex s->buffer[s->bufpos] = '\0';
1571 3012c232 2004-05-11 alex /* If the result string is incomplete, return to main loop and
1572 3012c232 2004-05-11 alex * wait until we can read in more bytes. */
1573 c23bbe6b 2004-05-11 alex #ifdef IDENTAUTH
1574 3012c232 2004-05-11 alex try_resolve:
1576 3012c232 2004-05-11 alex ptr = strchr( s->buffer, '\n' );
1577 3012c232 2004-05-11 alex if( ! ptr ) return;
1578 3012c232 2004-05-11 alex *ptr = '\0';
1580 7b6e2662 2003-11-05 alex #ifdef DEBUG
1581 3012c232 2004-05-11 alex Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%d bytes), stage %d.", s->buffer, len, s->stage );
1584 3012c232 2004-05-11 alex /* Okay, we got a complete result: this is a host name for outgoing
1585 3012c232 2004-05-11 alex * connections and a host name or IDENT user name (if enabled) for
1586 8ac701d6 2005-06-01 fw * incoming connections.*/
1587 03d971d9 2002-01-02 alex if( My_Connections[i].sock > NONE )
1589 8ac701d6 2005-06-01 fw /* Incoming connection. Search client ... */
1590 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1591 54e487d4 2002-01-03 alex assert( c != NULL );
1593 33f4e676 2004-02-28 alex /* Only update client information of unregistered clients */
1594 3012c232 2004-05-11 alex if( Client_Type( c ) == CLIENT_UNKNOWN )
1596 8ac701d6 2005-06-01 fw switch(s->stage) {
1597 8ac701d6 2005-06-01 fw case 0: /* host name */
1598 3012c232 2004-05-11 alex strlcpy( My_Connections[i].host, s->buffer, sizeof( My_Connections[i].host ));
1599 3012c232 2004-05-11 alex Client_SetHostname( c, s->buffer );
1600 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
1601 3012c232 2004-05-11 alex /* clean up buffer for IDENT result */
1602 3012c232 2004-05-11 alex len = strlen( s->buffer ) + 1;
1603 8ac701d6 2005-06-01 fw assert(len <= sizeof( s->buffer ));
1604 3012c232 2004-05-11 alex memmove( s->buffer, s->buffer + len, sizeof( s->buffer ) - len );
1605 8ac701d6 2005-06-01 fw assert(len <= s->bufpos );
1606 3012c232 2004-05-11 alex s->bufpos -= len;
1608 3012c232 2004-05-11 alex /* Don't close pipe and clean up, but
1609 3012c232 2004-05-11 alex * instead wait for IDENT result */
1610 3012c232 2004-05-11 alex s->stage = 1;
1611 3012c232 2004-05-11 alex goto try_resolve;
1613 8ac701d6 2005-06-01 fw case 1: /* IDENT user name */
1614 3012c232 2004-05-11 alex if( s->buffer[0] )
1616 3012c232 2004-05-11 alex Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, s->buffer );
1617 8adff592 2005-03-19 fw Client_SetUser( c, s->buffer, true );
1619 3012c232 2004-05-11 alex else Log( LOG_INFO, "IDENT lookup for connection %ld: no result.", i );
1623 8ac701d6 2005-06-01 fw Log( LOG_ERR, "Resolver: got result for unknown stage %d!?", s->stage );
1626 3012c232 2004-05-11 alex #ifdef DEBUG
1627 3012c232 2004-05-11 alex else Log( LOG_DEBUG, "Resolver: discarding result for already registered connection %d.", i );
1632 3012c232 2004-05-11 alex /* Outgoing connection (server link): set the IP address
1633 3012c232 2004-05-11 alex * so that we can connect to it in the main loop. */
1635 3012c232 2004-05-11 alex /* Search server ... */
1636 a2544e49 2002-12-30 alex n = Conf_GetServer( i );
1637 33f4e676 2004-02-28 alex assert( n > NONE );
1639 3012c232 2004-05-11 alex strlcpy( Conf_Server[n].ip, s->buffer, sizeof( Conf_Server[n].ip ));
1642 3012c232 2004-05-11 alex /* Clean up ... */
1643 8ac701d6 2005-06-01 fw FreeRes_stat( &My_Connections[i] );
1645 b79b315d 2003-12-27 alex /* Reset penalty time */
1646 736bacde 2002-10-10 alex Conn_ResetPenalty( i );
1647 4a111033 2001-12-29 alex } /* Read_Resolver_Result */
1651 8adff592 2005-03-19 fw Simple_Message( int Sock, char *Msg )
1653 1cf8ccd8 2005-06-12 fw char buf[COMMAND_LEN];
1654 e744936d 2003-03-07 alex /* Write "simple" message to socket, without using compression
1655 e744936d 2003-03-07 alex * or even the connection write buffers. Used e.g. for error
1656 e744936d 2003-03-07 alex * messages by New_Connection(). */
1657 e744936d 2003-03-07 alex assert( Sock > NONE );
1658 e744936d 2003-03-07 alex assert( Msg != NULL );
1660 1cf8ccd8 2005-06-12 fw strlcpy( buf, Msg, sizeof buf - 2);
1661 1cf8ccd8 2005-06-12 fw strlcat( buf, "\r\n", sizeof buf);
1662 1cf8ccd8 2005-06-12 fw (void)write( Sock, buf, strlen( buf ) );
1663 e744936d 2003-03-07 alex } /* Simple_Error */
1667 2981fe9e 2003-11-05 alex Count_Connections( struct sockaddr_in addr_in )
1672 2981fe9e 2003-11-05 alex for( i = 0; i < Pool_Size; i++ )
1674 2981fe9e 2003-11-05 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].addr.sin_addr.s_addr == addr_in.sin_addr.s_addr )) cnt++;
1676 2981fe9e 2003-11-05 alex return cnt;
1677 2981fe9e 2003-11-05 alex } /* Count_Connections */
1680 5fefe1a3 2001-12-12 alex /* -eof- */