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 429b5f3e 2005-01-17 alex static char UNUSED id[] = "$Id: conn.c,v 1.142 2005/01/17 11:57:39 alex 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 c2f60abe 2002-05-27 alex LOCAL VOID Handle_Read PARAMS(( INT sock ));
85 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Handle_Write PARAMS(( CONN_ID Idx ));
86 c2f60abe 2002-05-27 alex LOCAL VOID New_Connection PARAMS(( INT Sock ));
87 c2f60abe 2002-05-27 alex LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
88 c2f60abe 2002-05-27 alex LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
89 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
90 5ecb00ed 2002-11-23 alex LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
91 c2f60abe 2002-05-27 alex LOCAL VOID Check_Connections PARAMS(( VOID ));
92 c2f60abe 2002-05-27 alex LOCAL VOID Check_Servers PARAMS(( VOID ));
93 e68cdf30 2002-12-30 alex LOCAL VOID Init_Conn_Struct PARAMS(( CONN_ID Idx ));
94 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock ));
95 c2f60abe 2002-05-27 alex LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx ));
96 c2f60abe 2002-05-27 alex LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd ));
97 e744936d 2003-03-07 alex LOCAL VOID Simple_Message PARAMS(( INT Sock, CHAR *Msg ));
98 2981fe9e 2003-11-05 alex LOCAL INT Count_Connections PARAMS(( struct sockaddr_in addr ));
100 4a111033 2001-12-29 alex LOCAL fd_set My_Listeners;
101 1c8eb478 2001-12-12 alex LOCAL fd_set My_Sockets;
102 6da91c34 2002-03-02 alex LOCAL fd_set My_Connects;
104 c40592d2 2003-12-26 alex #ifdef TCPWRAP
105 439c945d 2003-03-07 alex INT allow_severity = LOG_INFO;
106 439c945d 2003-03-07 alex INT deny_severity = LOG_ERR;
110 c2f60abe 2002-05-27 alex GLOBAL VOID
111 c2f60abe 2002-05-27 alex Conn_Init( VOID )
113 bc140df8 2001-12-29 alex /* Modul initialisieren: statische Strukturen "ausnullen". */
117 b991de0f 2002-11-02 alex /* Speicher fuer Verbindungs-Pool anfordern */
118 b991de0f 2002-11-02 alex Pool_Size = CONNECTION_POOL;
119 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
121 b991de0f 2002-11-02 alex /* konfiguriertes Limit beachten */
122 b991de0f 2002-11-02 alex if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
124 cb76d96e 2004-03-11 alex My_Connections = (CONNECTION *)malloc( sizeof( CONNECTION ) * Pool_Size );
125 b991de0f 2002-11-02 alex if( ! My_Connections )
127 b991de0f 2002-11-02 alex /* Speicher konnte nicht alloziert werden! */
128 b991de0f 2002-11-02 alex Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
131 7b6e2662 2003-11-05 alex #ifdef DEBUG
132 b9f16c96 2003-08-30 alex Log( LOG_DEBUG, "Allocated connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
135 1c8eb478 2001-12-12 alex /* zu Beginn haben wir keine Verbindungen */
136 4a111033 2001-12-29 alex FD_ZERO( &My_Listeners );
137 1c8eb478 2001-12-12 alex FD_ZERO( &My_Sockets );
138 6da91c34 2002-03-02 alex FD_ZERO( &My_Connects );
140 b991de0f 2002-11-02 alex /* Groesster File-Descriptor fuer select() */
141 c2f60abe 2002-05-27 alex Conn_MaxFD = 0;
143 1c8eb478 2001-12-12 alex /* Connection-Struktur initialisieren */
144 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
146 a02bf31d 2002-12-18 alex /* Global write counter */
147 a02bf31d 2002-12-18 alex WCounter = 0;
148 5fefe1a3 2001-12-12 alex } /* Conn_Init */
151 c2f60abe 2002-05-27 alex GLOBAL VOID
152 c2f60abe 2002-05-27 alex Conn_Exit( VOID )
154 bc140df8 2001-12-29 alex /* Modul abmelden: alle noch offenen Connections
155 bc140df8 2001-12-29 alex * schliessen und freigeben. */
157 cf050519 2001-12-14 alex CONN_ID idx;
160 7b6e2662 2003-11-05 alex #ifdef DEBUG
161 79809118 2002-01-06 alex Log( LOG_DEBUG, "Shutting down all connections ..." );
164 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
165 29bd35bc 2003-02-23 alex Rendezvous_UnregisterListeners( );
168 29bd35bc 2003-02-23 alex /* Sockets schliessen */
169 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
171 1c8eb478 2001-12-12 alex if( FD_ISSET( i, &My_Sockets ))
173 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ )
175 1c8eb478 2001-12-12 alex if( My_Connections[idx].sock == i ) break;
177 6da91c34 2002-03-02 alex if( FD_ISSET( i, &My_Listeners ))
179 1c8eb478 2001-12-12 alex close( i );
180 7b6e2662 2003-11-05 alex #ifdef DEBUG
181 79809118 2002-01-06 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
184 6da91c34 2002-03-02 alex else if( FD_ISSET( i, &My_Connects ))
186 6da91c34 2002-03-02 alex close( i );
187 7b6e2662 2003-11-05 alex #ifdef DEBUG
188 6da91c34 2002-03-02 alex Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
191 b991de0f 2002-11-02 alex else if( idx < Pool_Size )
193 397c5e2e 2002-12-19 alex if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
194 ae958aa1 2002-06-02 alex else Conn_Close( idx, NULL, "Server going down", TRUE );
198 6da91c34 2002-03-02 alex Log( LOG_WARNING, "Closing unknown connection %d ...", i );
199 1c8eb478 2001-12-12 alex close( i );
204 b991de0f 2002-11-02 alex free( My_Connections );
205 b991de0f 2002-11-02 alex My_Connections = NULL;
206 b991de0f 2002-11-02 alex Pool_Size = 0;
207 5fefe1a3 2001-12-12 alex } /* Conn_Exit */
211 a0c032b2 2002-11-22 alex Conn_InitListeners( VOID )
213 e68cdf30 2002-12-30 alex /* Initialize ports on which the server should accept connections */
215 a0c032b2 2002-11-22 alex INT created, i;
217 a0c032b2 2002-11-22 alex created = 0;
218 a0c032b2 2002-11-22 alex for( i = 0; i < Conf_ListenPorts_Count; i++ )
220 a0c032b2 2002-11-22 alex if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
221 a0c032b2 2002-11-22 alex else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
223 a0c032b2 2002-11-22 alex return created;
224 a0c032b2 2002-11-22 alex } /* Conn_InitListeners */
227 a0c032b2 2002-11-22 alex GLOBAL VOID
228 a0c032b2 2002-11-22 alex Conn_ExitListeners( VOID )
230 e68cdf30 2002-12-30 alex /* Close down all listening sockets */
234 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
235 29bd35bc 2003-02-23 alex Rendezvous_UnregisterListeners( );
238 a0c032b2 2002-11-22 alex Log( LOG_INFO, "Shutting down all listening sockets ..." );
239 a0c032b2 2002-11-22 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
241 a0c032b2 2002-11-22 alex if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
243 a0c032b2 2002-11-22 alex close( i );
244 7b6e2662 2003-11-05 alex #ifdef DEBUG
245 a0c032b2 2002-11-22 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
249 a0c032b2 2002-11-22 alex } /* Conn_ExitListeners */
252 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
253 c2f60abe 2002-05-27 alex Conn_NewListener( CONST UINT Port )
255 e68cdf30 2002-12-30 alex /* Create new listening socket on specified port */
257 1c8eb478 2001-12-12 alex struct sockaddr_in addr;
258 e33ab903 2003-09-11 alex struct in_addr inaddr;
260 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
261 29bd35bc 2003-02-23 alex CHAR name[CLIENT_ID_LEN], *info;
264 5fefe1a3 2001-12-12 alex /* Server-"Listen"-Socket initialisieren */
265 1c8eb478 2001-12-12 alex memset( &addr, 0, sizeof( addr ));
266 e33ab903 2003-09-11 alex memset( &inaddr, 0, sizeof( inaddr ));
267 1c8eb478 2001-12-12 alex addr.sin_family = AF_INET;
268 1c8eb478 2001-12-12 alex addr.sin_port = htons( Port );
269 e33ab903 2003-09-11 alex if( Conf_ListenAddress[0] )
271 e33ab903 2003-09-11 alex #ifdef HAVE_INET_ATON
272 e33ab903 2003-09-11 alex if( inet_aton( Conf_ListenAddress, &inaddr ) == 0 )
274 e33ab903 2003-09-11 alex inaddr.s_addr = inet_addr( Conf_ListenAddress );
275 e33ab903 2003-09-11 alex if( inaddr.s_addr == (unsigned)-1 )
278 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 );
279 e33ab903 2003-09-11 alex return FALSE;
282 e33ab903 2003-09-11 alex else inaddr.s_addr = htonl( INADDR_ANY );
283 e33ab903 2003-09-11 alex addr.sin_addr = inaddr;
285 5fefe1a3 2001-12-12 alex /* Socket erzeugen */
286 1c8eb478 2001-12-12 alex sock = socket( PF_INET, SOCK_STREAM, 0);
287 fb55c443 2001-12-13 alex if( sock < 0 )
289 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
290 5fefe1a3 2001-12-12 alex return FALSE;
293 6da91c34 2002-03-02 alex if( ! Init_Socket( sock )) return FALSE;
295 5fefe1a3 2001-12-12 alex /* an Port binden */
296 1c8eb478 2001-12-12 alex if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
298 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
299 1c8eb478 2001-12-12 alex close( sock );
300 5fefe1a3 2001-12-12 alex return FALSE;
303 5fefe1a3 2001-12-12 alex /* in "listen mode" gehen :-) */
304 1c8eb478 2001-12-12 alex if( listen( sock, 10 ) != 0 )
306 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
307 1c8eb478 2001-12-12 alex close( sock );
308 5fefe1a3 2001-12-12 alex return FALSE;
311 1c8eb478 2001-12-12 alex /* Neuen Listener in Strukturen einfuegen */
312 4a111033 2001-12-29 alex FD_SET( sock, &My_Listeners );
313 1c8eb478 2001-12-12 alex FD_SET( sock, &My_Sockets );
315 c2f60abe 2002-05-27 alex if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
317 e33ab903 2003-09-11 alex if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock );
318 e33ab903 2003-09-11 alex else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock );
320 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
321 29bd35bc 2003-02-23 alex /* Get best server description text */
322 29bd35bc 2003-02-23 alex if( ! Conf_ServerInfo[0] ) info = Conf_ServerName;
325 29bd35bc 2003-02-23 alex /* Use server info string */
326 29bd35bc 2003-02-23 alex info = NULL;
327 29bd35bc 2003-02-23 alex if( Conf_ServerInfo[0] == '[' )
329 29bd35bc 2003-02-23 alex /* Cut off leading hostname part in "[]" */
330 29bd35bc 2003-02-23 alex info = strchr( Conf_ServerInfo, ']' );
334 29bd35bc 2003-02-23 alex while( *info == ' ' ) info++;
337 29bd35bc 2003-02-23 alex if( ! info ) info = Conf_ServerInfo;
340 29bd35bc 2003-02-23 alex /* Add port number to description if non-standard */
341 29bd35bc 2003-02-23 alex if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port );
342 29bd35bc 2003-02-23 alex else strlcpy( name, info, sizeof( name ));
344 29bd35bc 2003-02-23 alex /* Register service */
345 29bd35bc 2003-02-23 alex Rendezvous_Register( name, RENDEZVOUS_TYPE, Port );
348 5fefe1a3 2001-12-12 alex return TRUE;
349 08cf5607 2001-12-26 alex } /* Conn_NewListener */
352 c2f60abe 2002-05-27 alex GLOBAL VOID
353 ae958aa1 2002-06-02 alex Conn_Handler( VOID )
355 6c19b0e4 2002-09-07 alex /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
356 6c19b0e4 2002-09-07 alex * werden dabei durchgefuehrt, bis der Server terminieren oder neu
357 6c19b0e4 2002-09-07 alex * starten soll:
359 bc140df8 2001-12-29 alex * - neue Verbindungen annehmen,
360 03d971d9 2002-01-02 alex * - Server-Verbindungen aufbauen,
361 bc140df8 2001-12-29 alex * - geschlossene Verbindungen loeschen,
362 bc140df8 2001-12-29 alex * - volle Schreibpuffer versuchen zu schreiben,
363 bc140df8 2001-12-29 alex * - volle Lesepuffer versuchen zu verarbeiten,
364 bc140df8 2001-12-29 alex * - Antworten von Resolver Sub-Prozessen annehmen.
367 63758dd7 2001-12-15 alex fd_set read_sockets, write_sockets;
368 747fd2f0 2001-12-13 alex struct timeval tv;
369 7b6cfc17 2002-08-26 alex time_t start, t;
370 e68cdf30 2002-12-30 alex CONN_ID i, idx;
371 5ecb00ed 2002-11-23 alex BOOLEAN timeout;
373 9ab186c4 2001-12-25 alex start = time( NULL );
374 397c5e2e 2002-12-19 alex while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart ))
376 5ecb00ed 2002-11-23 alex timeout = TRUE;
378 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
379 29bd35bc 2003-02-23 alex Rendezvous_Handler( );
382 397c5e2e 2002-12-19 alex /* Should the configuration be reloaded? */
383 397c5e2e 2002-12-19 alex if( NGIRCd_SignalRehash ) NGIRCd_Rehash( );
385 397c5e2e 2002-12-19 alex /* Check configured servers and established links */
386 397c5e2e 2002-12-19 alex Check_Servers( );
387 65bdfdf2 2001-12-26 alex Check_Connections( );
389 7b6e2662 2003-11-05 alex t = time( NULL );
391 65bdfdf2 2001-12-26 alex /* noch volle Lese-Buffer suchen */
392 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
394 7b6e2662 2003-11-05 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ) &&
395 7b6e2662 2003-11-05 alex ( My_Connections[i].delaytime < t ))
397 65bdfdf2 2001-12-26 alex /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
398 5ecb00ed 2002-11-23 alex if( Handle_Buffer( i )) timeout = FALSE;
402 65bdfdf2 2001-12-26 alex /* noch volle Schreib-Puffer suchen */
403 65bdfdf2 2001-12-26 alex FD_ZERO( &write_sockets );
404 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
406 c40592d2 2003-12-26 alex #ifdef ZLIB
407 65f3adca 2002-11-26 alex if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
409 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
412 65bdfdf2 2001-12-26 alex /* Socket der Verbindung in Set aufnehmen */
413 65bdfdf2 2001-12-26 alex FD_SET( My_Connections[i].sock, &write_sockets );
417 6da91c34 2002-03-02 alex /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
418 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
420 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 );
423 4a111033 2001-12-29 alex /* von welchen Sockets koennte gelesen werden? */
424 9ab186c4 2001-12-25 alex read_sockets = My_Sockets;
425 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
427 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
429 4a111033 2001-12-29 alex /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
430 6da91c34 2002-03-02 alex FD_CLR( My_Connections[i].sock, &read_sockets );
432 6da91c34 2002-03-02 alex if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
434 6da91c34 2002-03-02 alex /* Hier laeuft noch ein asyncrones connect() */
435 4a111033 2001-12-29 alex FD_CLR( My_Connections[i].sock, &read_sockets );
437 7b6cfc17 2002-08-26 alex if( My_Connections[i].delaytime > t )
439 7b6cfc17 2002-08-26 alex /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
440 7b6cfc17 2002-08-26 alex FD_CLR( My_Connections[i].sock, &read_sockets );
443 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
445 4a111033 2001-12-29 alex /* Pipes von Resolver Sub-Prozessen aufnehmen */
446 c2f60abe 2002-05-27 alex if( FD_ISSET( i, &Resolver_FDs ))
448 4a111033 2001-12-29 alex FD_SET( i, &read_sockets );
452 6c19b0e4 2002-09-07 alex /* Timeout initialisieren */
453 5ecb00ed 2002-11-23 alex tv.tv_usec = 0;
454 7b6e2662 2003-11-05 alex if( timeout ) tv.tv_sec = 1;
455 5ecb00ed 2002-11-23 alex else tv.tv_sec = 0;
457 4a111033 2001-12-29 alex /* Auf Aktivitaet warten */
458 8a927a1b 2002-09-07 alex i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
459 8a927a1b 2002-09-07 alex if( i == 0 )
461 8a927a1b 2002-09-07 alex /* keine Veraenderung an den Sockets */
464 8a927a1b 2002-09-07 alex if( i == -1 )
466 8a927a1b 2002-09-07 alex /* Fehler (z.B. Interrupt) */
467 db58d347 2002-01-05 alex if( errno != EINTR )
469 b840be98 2002-10-15 alex Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
470 62796722 2003-03-31 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
476 9ab186c4 2001-12-25 alex /* Koennen Daten geschrieben werden? */
477 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
479 3cf845fb 2002-10-14 alex if( ! FD_ISSET( i, &write_sockets )) continue;
481 3cf845fb 2002-10-14 alex /* Es kann geschrieben werden ... */
482 3cf845fb 2002-10-14 alex idx = Socket2Index( i );
483 b840be98 2002-10-15 alex if( idx == NONE ) continue;
485 3cf845fb 2002-10-14 alex if( ! Handle_Write( idx ))
487 3cf845fb 2002-10-14 alex /* Fehler beim Schreiben! Diesen Socket nun
488 3cf845fb 2002-10-14 alex * auch aus dem Read-Set entfernen: */
489 3cf845fb 2002-10-14 alex FD_CLR( i, &read_sockets );
493 9ab186c4 2001-12-25 alex /* Daten zum Lesen vorhanden? */
494 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
496 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
500 397c5e2e 2002-12-19 alex if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
501 397c5e2e 2002-12-19 alex else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
502 1c8eb478 2001-12-12 alex } /* Conn_Handler */
505 f7327524 2002-05-30 alex #ifdef PROTOTYPES
506 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
507 c2f60abe 2002-05-27 alex Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
509 f7327524 2002-05-30 alex GLOBAL BOOLEAN
510 f7327524 2002-05-30 alex Conn_WriteStr( Idx, Format, va_alist )
511 f7327524 2002-05-30 alex CONN_ID Idx;
512 f7327524 2002-05-30 alex CHAR *Format;
516 63758dd7 2001-12-15 alex /* String in Socket schreiben. CR+LF wird von dieser Funktion
517 d5c97f81 2001-12-23 alex * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
518 d5c97f81 2001-12-23 alex * getrennt und FALSE geliefert. */
520 804b1ec4 2001-12-31 alex CHAR buffer[COMMAND_LEN];
521 d5c97f81 2001-12-23 alex BOOLEAN ok;
522 d5c97f81 2001-12-23 alex va_list ap;
524 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
525 28c5a21f 2002-03-14 alex assert( Format != NULL );
527 f7327524 2002-05-30 alex #ifdef PROTOTYPES
528 d5c97f81 2001-12-23 alex va_start( ap, Format );
530 f7327524 2002-05-30 alex va_start( ap );
532 804b1ec4 2001-12-31 alex if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
534 79809118 2002-01-06 alex Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
535 79809118 2002-01-06 alex Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
536 63758dd7 2001-12-15 alex return FALSE;
539 7c91951d 2001-12-25 alex #ifdef SNIFFER
540 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
543 6626395c 2002-12-26 alex strlcat( buffer, "\r\n", sizeof( buffer ));
544 446df061 2001-12-24 alex ok = Conn_Write( Idx, buffer, strlen( buffer ));
545 39219230 2002-12-02 alex My_Connections[Idx].msg_out++;
547 d5c97f81 2001-12-23 alex va_end( ap );
549 63758dd7 2001-12-15 alex } /* Conn_WriteStr */
552 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
553 c2f60abe 2002-05-27 alex Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
555 63758dd7 2001-12-15 alex /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
556 63758dd7 2001-12-15 alex * der Client disconnectiert und FALSE geliefert. */
558 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
559 63758dd7 2001-12-15 alex assert( Data != NULL );
560 63758dd7 2001-12-15 alex assert( Len > 0 );
562 65f3adca 2002-11-26 alex /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
563 65f3adca 2002-11-26 alex * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
564 65f3adca 2002-11-26 alex * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
565 65f3adca 2002-11-26 alex * In diesem Fall wird hier einfach ein Fehler geliefert. */
566 67d79b92 2002-09-26 alex if( My_Connections[Idx].sock <= NONE )
568 7b6e2662 2003-11-05 alex #ifdef DEBUG
569 c50ecda7 2002-09-26 alex Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
571 c50ecda7 2002-09-26 alex return FALSE;
574 65f3adca 2002-11-26 alex /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
575 65f3adca 2002-11-26 alex * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
576 65f3adca 2002-11-26 alex * Socket zu schreiben (u.a. wg. Komprimierung). */
577 63758dd7 2001-12-15 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
579 65f3adca 2002-11-26 alex /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
580 65f3adca 2002-11-26 alex * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
581 65f3adca 2002-11-26 alex if( ! Try_Write( Idx )) return FALSE;
583 65f3adca 2002-11-26 alex /* nun neu pruefen: */
584 65f3adca 2002-11-26 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
586 65f3adca 2002-11-26 alex Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
587 65f3adca 2002-11-26 alex Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
588 65f3adca 2002-11-26 alex return FALSE;
592 c40592d2 2003-12-26 alex #ifdef ZLIB
593 65f3adca 2002-11-26 alex if( My_Connections[Idx].options & CONN_ZIP )
595 65f3adca 2002-11-26 alex /* Daten komprimieren und in Puffer kopieren */
596 65f3adca 2002-11-26 alex if( ! Zip_Buffer( Idx, Data, Len )) return FALSE;
601 65f3adca 2002-11-26 alex /* Daten in Puffer kopieren */
602 65f3adca 2002-11-26 alex memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
603 65f3adca 2002-11-26 alex My_Connections[Idx].wdatalen += Len;
604 65f3adca 2002-11-26 alex My_Connections[Idx].bytes_out += Len;
607 a02bf31d 2002-12-18 alex /* Adjust global write counter */
608 a02bf31d 2002-12-18 alex WCounter += Len;
610 63758dd7 2001-12-15 alex return TRUE;
611 63758dd7 2001-12-15 alex } /* Conn_Write */
614 c2f60abe 2002-05-27 alex GLOBAL VOID
615 c2f60abe 2002-05-27 alex Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
617 e21b9d84 2002-12-27 alex /* Close connection. Open pipes of asyncronous resolver
618 e21b9d84 2002-12-27 alex * sub-processes are closed down. */
622 65f3adca 2002-11-26 alex DOUBLE in_k, out_k;
623 c40592d2 2003-12-26 alex #ifdef ZLIB
624 65f3adca 2002-11-26 alex DOUBLE in_z_k, out_z_k;
625 65f3adca 2002-11-26 alex INT in_p, out_p;
628 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
630 cf4ad8d6 2003-02-21 alex /* Is this link already shutting down? */
631 cf4ad8d6 2003-02-21 alex if( My_Connections[Idx].options & CONN_ISCLOSING )
633 cf4ad8d6 2003-02-21 alex /* Conn_Close() has been called recursively for this link;
634 cf4ad8d6 2003-02-21 alex * probabe reason: Try_Write() failed -- see below. */
635 58736b39 2004-12-22 alex #ifdef DEBUG
636 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Recursive request to close connection: %d", Idx );
641 1d8da4b5 2004-04-25 alex assert( My_Connections[Idx].sock > NONE );
643 cf4ad8d6 2003-02-21 alex /* Mark link as "closing" */
644 cf4ad8d6 2003-02-21 alex My_Connections[Idx].options |= CONN_ISCLOSING;
646 33f4e676 2004-02-28 alex if( LogMsg ) txt = LogMsg;
647 33f4e676 2004-02-28 alex else txt = FwdMsg;
648 33f4e676 2004-02-28 alex if( ! txt ) txt = "Reason unknown";
650 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 ));
652 e21b9d84 2002-12-27 alex /* Search client, if any */
653 f060db5e 2002-10-09 alex c = Client_GetFromConn( Idx );
655 e21b9d84 2002-12-27 alex /* Should the client be informed? */
656 79809118 2002-01-06 alex if( InformClient )
658 1d8cb34b 2002-11-29 alex #ifndef STRICT_RFC
659 e21b9d84 2002-12-27 alex /* Send statistics to client if registered as user: */
660 f060db5e 2002-10-09 alex if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
662 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 );
666 e21b9d84 2002-12-27 alex /* Send ERROR to client (see RFC!) */
667 79809118 2002-01-06 alex if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
668 79809118 2002-01-06 alex else Conn_WriteStr( Idx, "ERROR :Closing connection." );
671 e21b9d84 2002-12-27 alex /* Try to write out the write buffer */
672 cf4ad8d6 2003-02-21 alex (VOID)Try_Write( Idx );
674 e21b9d84 2002-12-27 alex /* Shut down socket */
675 7c91951d 2001-12-25 alex if( close( My_Connections[Idx].sock ) != 0 )
677 e21b9d84 2002-12-27 alex /* Oops, we can't close the socket!? This is fatal! */
678 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 ));
679 62796722 2003-03-31 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
683 e21b9d84 2002-12-27 alex /* Mark socket as invalid: */
684 3cf845fb 2002-10-14 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
685 3cf845fb 2002-10-14 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
686 3cf845fb 2002-10-14 alex My_Connections[Idx].sock = NONE;
688 e21b9d84 2002-12-27 alex /* If there is still a client, unregister it now */
689 50ec7a56 2002-03-11 alex if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
691 e21b9d84 2002-12-27 alex /* Calculate statistics and log information */
692 e21b9d84 2002-12-27 alex in_k = (DOUBLE)My_Connections[Idx].bytes_in / 1024;
693 e21b9d84 2002-12-27 alex out_k = (DOUBLE)My_Connections[Idx].bytes_out / 1024;
694 c40592d2 2003-12-26 alex #ifdef ZLIB
695 e21b9d84 2002-12-27 alex if( My_Connections[Idx].options & CONN_ZIP )
697 e21b9d84 2002-12-27 alex in_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_in / 1024;
698 e21b9d84 2002-12-27 alex out_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_out / 1024;
699 e21b9d84 2002-12-27 alex in_p = (INT)(( in_k * 100 ) / in_z_k );
700 e21b9d84 2002-12-27 alex out_p = (INT)(( out_k * 100 ) / out_z_k );
701 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 );
706 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 );
709 e21b9d84 2002-12-27 alex /* Is there a resolver sub-process running? */
710 4a111033 2001-12-29 alex if( My_Connections[Idx].res_stat )
712 e21b9d84 2002-12-27 alex /* Free resolver structures */
713 c2f60abe 2002-05-27 alex FD_CLR( My_Connections[Idx].res_stat->pipe[0], &Resolver_FDs );
714 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[0] );
715 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[1] );
716 4a111033 2001-12-29 alex free( My_Connections[Idx].res_stat );
719 e21b9d84 2002-12-27 alex /* Servers: Modify time of next connect attempt? */
720 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
722 c40592d2 2003-12-26 alex #ifdef ZLIB
723 e21b9d84 2002-12-27 alex /* Clean up zlib, if link was compressed */
724 65f3adca 2002-11-26 alex if( Conn_Options( Idx ) & CONN_ZIP )
726 65f3adca 2002-11-26 alex inflateEnd( &My_Connections[Idx].zip.in );
727 65f3adca 2002-11-26 alex deflateEnd( &My_Connections[Idx].zip.out );
731 e21b9d84 2002-12-27 alex /* Clean up connection structure (=free it) */
732 3cf845fb 2002-10-14 alex Init_Conn_Struct( Idx );
734 58736b39 2004-12-22 alex #ifdef DEBUG
735 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Shutdown of connection %d completed.", Idx );
737 7c91951d 2001-12-25 alex } /* Conn_Close */
740 59a0fb8c 2003-03-27 alex GLOBAL VOID
741 59a0fb8c 2003-03-27 alex Conn_SyncServerStruct( VOID )
743 59a0fb8c 2003-03-27 alex /* Synchronize server structures (connection IDs):
744 59a0fb8c 2003-03-27 alex * connections <-> configuration */
746 59a0fb8c 2003-03-27 alex CLIENT *client;
750 59a0fb8c 2003-03-27 alex for( i = 0; i < Pool_Size; i++ )
752 59a0fb8c 2003-03-27 alex /* Established connection? */
753 59a0fb8c 2003-03-27 alex if( My_Connections[i].sock <= NONE ) continue;
755 59a0fb8c 2003-03-27 alex /* Server connection? */
756 59a0fb8c 2003-03-27 alex client = Client_GetFromConn( i );
757 59a0fb8c 2003-03-27 alex if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue;
759 59a0fb8c 2003-03-27 alex for( c = 0; c < MAX_SERVERS; c++ )
761 59a0fb8c 2003-03-27 alex /* Configured server? */
762 59a0fb8c 2003-03-27 alex if( ! Conf_Server[c].host[0] ) continue;
764 59a0fb8c 2003-03-27 alex /* Duplicate? */
765 59a0fb8c 2003-03-27 alex if( strcmp( Conf_Server[c].name, Client_ID( client )) == 0 ) Conf_Server[c].conn_id = i;
768 59a0fb8c 2003-03-27 alex } /* SyncServerStruct */
771 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
772 c2f60abe 2002-05-27 alex Try_Write( CONN_ID Idx )
774 93a39e87 2002-11-28 alex /* Versuchen, Daten aus dem Schreib-Puffer in den Socket zu
775 93a39e87 2002-11-28 alex * schreiben. TRUE wird geliefert, wenn entweder keine Daten
776 93a39e87 2002-11-28 alex * zum Versenden vorhanden sind oder erfolgreich bearbeitet
777 93a39e87 2002-11-28 alex * werden konnten. Im Fehlerfall wird FALSE geliefert und
778 93a39e87 2002-11-28 alex * die Verbindung geschlossen. */
780 63758dd7 2001-12-15 alex fd_set write_socket;
781 22b7b079 2002-11-23 alex struct timeval tv;
783 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
784 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
786 93a39e87 2002-11-28 alex /* sind ueberhaupt Daten vorhanden? */
787 c40592d2 2003-12-26 alex #ifdef ZLIB
788 93a39e87 2002-11-28 alex if(( ! My_Connections[Idx].wdatalen > 0 ) && ( ! My_Connections[Idx].zip.wdatalen )) return TRUE;
790 93a39e87 2002-11-28 alex if( ! My_Connections[Idx].wdatalen > 0 ) return TRUE;
793 22b7b079 2002-11-23 alex /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
794 22b7b079 2002-11-23 alex tv.tv_sec = 0; tv.tv_usec = 0;
796 63758dd7 2001-12-15 alex FD_ZERO( &write_socket );
797 63758dd7 2001-12-15 alex FD_SET( My_Connections[Idx].sock, &write_socket );
798 22b7b079 2002-11-23 alex if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
800 63758dd7 2001-12-15 alex /* Fehler! */
801 63758dd7 2001-12-15 alex if( errno != EINTR )
803 b840be98 2002-10-15 alex Log( LOG_ALERT, "Try_Write(): select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
804 79809118 2002-01-06 alex Conn_Close( Idx, "Server error!", NULL, FALSE );
805 63758dd7 2001-12-15 alex return FALSE;
809 63758dd7 2001-12-15 alex if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
810 63758dd7 2001-12-15 alex else return TRUE;
811 63758dd7 2001-12-15 alex } /* Try_Write */
815 c2f60abe 2002-05-27 alex Handle_Read( INT Sock )
817 bc140df8 2001-12-29 alex /* Aktivitaet auf einem Socket verarbeiten:
818 bc140df8 2001-12-29 alex * - neue Clients annehmen,
819 bc140df8 2001-12-29 alex * - Daten von Clients verarbeiten,
820 bc140df8 2001-12-29 alex * - Resolver-Rueckmeldungen annehmen. */
822 cf050519 2001-12-14 alex CONN_ID idx;
824 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
826 4a111033 2001-12-29 alex if( FD_ISSET( Sock, &My_Listeners ))
828 1c8eb478 2001-12-12 alex /* es ist einer unserer Listener-Sockets: es soll
829 1c8eb478 2001-12-12 alex * also eine neue Verbindung aufgebaut werden. */
831 1c8eb478 2001-12-12 alex New_Connection( Sock );
833 c2f60abe 2002-05-27 alex else if( FD_ISSET( Sock, &Resolver_FDs ))
835 4a111033 2001-12-29 alex /* Rueckmeldung von einem Resolver Sub-Prozess */
837 4a111033 2001-12-29 alex Read_Resolver_Result( Sock );
841 1c8eb478 2001-12-12 alex /* Ein Client Socket: entweder ein User oder Server */
843 1c8eb478 2001-12-12 alex idx = Socket2Index( Sock );
844 3cf845fb 2002-10-14 alex if( idx > NONE ) Read_Request( idx );
846 63758dd7 2001-12-15 alex } /* Handle_Read */
849 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
850 c2f60abe 2002-05-27 alex Handle_Write( CONN_ID Idx )
852 6da91c34 2002-03-02 alex /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
854 6da91c34 2002-03-02 alex INT len, res, err;
855 c3dfd63b 2003-04-21 alex socklen_t sock_len;
858 b840be98 2002-10-15 alex assert( Idx > NONE );
859 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
861 6da91c34 2002-03-02 alex if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
863 6da91c34 2002-03-02 alex /* es soll nichts geschrieben werden, sondern ein
864 6da91c34 2002-03-02 alex * connect() hat ein Ergebnis geliefert */
866 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
868 6da91c34 2002-03-02 alex /* Ergebnis des connect() ermitteln */
869 c3dfd63b 2003-04-21 alex sock_len = sizeof( err );
870 c3dfd63b 2003-04-21 alex res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
871 c3dfd63b 2003-04-21 alex assert( sock_len == sizeof( err ));
873 6da91c34 2002-03-02 alex /* Fehler aufgetreten? */
874 6da91c34 2002-03-02 alex if(( res != 0 ) || ( err != 0 ))
876 6da91c34 2002-03-02 alex /* Fehler! */
877 6da91c34 2002-03-02 alex if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
878 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 ));
880 9eee0c88 2003-01-15 alex /* Clean up socket, connection and client structures */
881 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
882 9eee0c88 2003-01-15 alex c = Client_GetFromConn( Idx );
883 9eee0c88 2003-01-15 alex if( c ) Client_DestroyNow( c );
884 6da91c34 2002-03-02 alex close( My_Connections[Idx].sock );
885 6da91c34 2002-03-02 alex Init_Conn_Struct( Idx );
887 cbce54e0 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
888 a2544e49 2002-12-30 alex Conf_Server[Conf_GetServer( Idx )].lasttry = time( NULL );
889 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
891 6da91c34 2002-03-02 alex return FALSE;
893 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 );
895 33f4e676 2004-02-28 alex /* Send PASS and SERVER command to peer */
896 a2544e49 2002-12-30 alex Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[Conf_GetServer( Idx )].pwd_out, NGIRCd_ProtoID );
897 3cf845fb 2002-10-14 alex return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
900 c40592d2 2003-12-26 alex #ifdef ZLIB
901 65f3adca 2002-11-26 alex /* Schreibpuffer leer, aber noch Daten im Kompressionsbuffer?
902 65f3adca 2002-11-26 alex * Dann muss dieser nun geflushed werden! */
903 65f3adca 2002-11-26 alex if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
906 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
908 63758dd7 2001-12-15 alex /* Daten schreiben */
909 63758dd7 2001-12-15 alex len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
910 63758dd7 2001-12-15 alex if( len < 0 )
912 c6b9f3a1 2002-11-20 alex /* Operation haette Socket "nur" blockiert ... */
913 c6b9f3a1 2002-11-20 alex if( errno == EAGAIN ) return TRUE;
915 63758dd7 2001-12-15 alex /* Oops, ein Fehler! */
916 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
917 3cf845fb 2002-10-14 alex Conn_Close( Idx, "Write error!", NULL, FALSE );
918 63758dd7 2001-12-15 alex return FALSE;
921 63758dd7 2001-12-15 alex /* Puffer anpassen */
922 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen -= len;
923 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
925 63758dd7 2001-12-15 alex return TRUE;
926 63758dd7 2001-12-15 alex } /* Handle_Write */
930 c2f60abe 2002-05-27 alex New_Connection( INT Sock )
932 d5c97f81 2001-12-23 alex /* Neue Client-Verbindung von Listen-Socket annehmen und
933 d5c97f81 2001-12-23 alex * CLIENT-Struktur anlegen. */
935 c40592d2 2003-12-26 alex #ifdef TCPWRAP
936 439c945d 2003-03-07 alex struct request_info req;
938 1c8eb478 2001-12-12 alex struct sockaddr_in new_addr;
939 cf050519 2001-12-14 alex INT new_sock, new_sock_len;
940 4a111033 2001-12-29 alex RES_STAT *s;
941 cf050519 2001-12-14 alex CONN_ID idx;
943 b991de0f 2002-11-02 alex POINTER *ptr;
944 2981fe9e 2003-11-05 alex LONG new_size, cnt;
946 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
948 c6b9f3a1 2002-11-20 alex /* Connection auf Listen-Socket annehmen */
949 1c8eb478 2001-12-12 alex new_sock_len = sizeof( new_addr );
950 fb55c443 2001-12-13 alex new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
951 1c8eb478 2001-12-12 alex if( new_sock < 0 )
953 446df061 2001-12-24 alex Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
957 c40592d2 2003-12-26 alex #ifdef TCPWRAP
958 439c945d 2003-03-07 alex /* Validate socket using TCP Wrappers */
959 62796722 2003-03-31 alex request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL );
960 b90bedbc 2004-02-03 alex fromhost(&req);
961 439c945d 2003-03-07 alex if( ! hosts_access( &req ))
963 439c945d 2003-03-07 alex /* Access denied! */
964 439c945d 2003-03-07 alex Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr ));
965 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection refused" );
966 439c945d 2003-03-07 alex close( new_sock );
971 c6b9f3a1 2002-11-20 alex /* Socket initialisieren */
972 c6b9f3a1 2002-11-20 alex Init_Socket( new_sock );
974 2981fe9e 2003-11-05 alex /* Check IP-based connection limit */
975 2981fe9e 2003-11-05 alex cnt = Count_Connections( new_addr );
976 2981fe9e 2003-11-05 alex if(( Conf_MaxConnectionsIP > 0 ) && ( cnt >= Conf_MaxConnectionsIP ))
978 c79fa28a 2004-10-04 alex /* Access denied, too many connections from this IP address! */
979 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);
980 c79fa28a 2004-10-04 alex Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" );
981 2981fe9e 2003-11-05 alex close( new_sock );
985 3cf845fb 2002-10-14 alex /* Freie Connection-Struktur suchen */
986 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
987 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
989 b991de0f 2002-11-02 alex new_size = Pool_Size + CONNECTION_POOL;
991 b991de0f 2002-11-02 alex /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
992 b991de0f 2002-11-02 alex * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
994 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
996 b991de0f 2002-11-02 alex /* Es ist ein Limit konfiguriert */
997 b991de0f 2002-11-02 alex if( Pool_Size >= Conf_MaxConnections )
999 b991de0f 2002-11-02 alex /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
1000 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
1001 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection limit reached" );
1002 b991de0f 2002-11-02 alex close( new_sock );
1005 b991de0f 2002-11-02 alex if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
1007 e68cdf30 2002-12-30 alex if( new_size < Pool_Size )
1009 e68cdf30 2002-12-30 alex Log( LOG_ALERT, "Can't accespt connection: limit (%d) reached -- overflow!", Pool_Size );
1010 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection limit reached" );
1011 e68cdf30 2002-12-30 alex close( new_sock );
1015 cb76d96e 2004-03-11 alex ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
1016 b991de0f 2002-11-02 alex if( ! ptr )
1018 429b5f3e 2005-01-17 alex Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
1019 429b5f3e 2005-01-17 alex Simple_Message( new_sock, "ERROR: Internal error" );
1020 429b5f3e 2005-01-17 alex close( new_sock );
1024 7b6e2662 2003-11-05 alex #ifdef DEBUG
1025 429b5f3e 2005-01-17 alex Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
1028 902ad912 2002-12-17 alex /* Adjust pointer to new block */
1029 cb76d96e 2004-03-11 alex My_Connections = (CONNECTION *)ptr;
1031 902ad912 2002-12-17 alex /* Initialize new items */
1032 902ad912 2002-12-17 alex for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
1033 902ad912 2002-12-17 alex idx = Pool_Size;
1035 902ad912 2002-12-17 alex /* Adjust new pool size */
1036 b991de0f 2002-11-02 alex Pool_Size = new_size;
1039 d5c97f81 2001-12-23 alex /* Client-Struktur initialisieren */
1040 b2d472fc 2002-05-19 alex c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
1043 d5c97f81 2001-12-23 alex Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1044 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Internal error" );
1045 d5c97f81 2001-12-23 alex close( new_sock );
1049 1c8eb478 2001-12-12 alex /* Verbindung registrieren */
1050 4a111033 2001-12-29 alex Init_Conn_Struct( idx );
1051 1c8eb478 2001-12-12 alex My_Connections[idx].sock = new_sock;
1052 1c8eb478 2001-12-12 alex My_Connections[idx].addr = new_addr;
1054 1c8eb478 2001-12-12 alex /* Neuen Socket registrieren */
1055 1c8eb478 2001-12-12 alex FD_SET( new_sock, &My_Sockets );
1056 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1058 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 );
1060 4a111033 2001-12-29 alex /* Hostnamen ermitteln */
1061 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host ));
1062 ffb90f4c 2002-11-22 alex Client_SetHostname( c, My_Connections[idx].host );
1063 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
1064 b79b315d 2003-12-27 alex s = Resolve_Addr( &new_addr, My_Connections[idx].sock );
1066 c2f60abe 2002-05-27 alex s = Resolve_Addr( &new_addr );
1070 4a111033 2001-12-29 alex /* Sub-Prozess wurde asyncron gestartet */
1071 4a111033 2001-12-29 alex My_Connections[idx].res_stat = s;
1074 7b6cfc17 2002-08-26 alex /* Penalty-Zeit setzen */
1075 736bacde 2002-10-10 alex Conn_SetPenalty( idx, 4 );
1076 1c8eb478 2001-12-12 alex } /* New_Connection */
1079 c2f60abe 2002-05-27 alex LOCAL CONN_ID
1080 c2f60abe 2002-05-27 alex Socket2Index( INT Sock )
1082 63758dd7 2001-12-15 alex /* zum Socket passende Connection suchen */
1084 cf050519 2001-12-14 alex CONN_ID idx;
1086 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
1088 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1090 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1092 b840be98 2002-10-15 alex /* die Connection wurde vermutlich (wegen eines
1093 b840be98 2002-10-15 alex * Fehlers) bereits wieder abgebaut ... */
1094 7b6e2662 2003-11-05 alex #ifdef DEBUG
1095 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1097 b840be98 2002-10-15 alex return NONE;
1099 3cf845fb 2002-10-14 alex else return idx;
1100 1c8eb478 2001-12-12 alex } /* Socket2Index */
1103 c2f60abe 2002-05-27 alex LOCAL VOID
1104 c2f60abe 2002-05-27 alex Read_Request( CONN_ID Idx )
1106 747fd2f0 2001-12-13 alex /* Daten von Socket einlesen und entsprechend behandeln.
1107 747fd2f0 2001-12-13 alex * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1109 d3a6537f 2002-11-28 alex INT len, bsize;
1110 c40592d2 2003-12-26 alex #ifdef ZLIB
1111 d3a6537f 2002-11-28 alex CLIENT *c;
1114 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1115 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
1117 d3a6537f 2002-11-28 alex /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
1118 d3a6537f 2002-11-28 alex * ansonsten koennen Daten ggf. nicht umkopiert werden. */
1119 d3a6537f 2002-11-28 alex bsize = READBUFFER_LEN;
1120 c40592d2 2003-12-26 alex #ifdef ZLIB
1121 d3a6537f 2002-11-28 alex c = Client_GetFromConn( Idx );
1122 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;
1125 c40592d2 2003-12-26 alex #ifdef ZLIB
1126 d3a6537f 2002-11-28 alex if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1128 d3a6537f 2002-11-28 alex if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
1131 54e487d4 2002-01-03 alex /* Der Lesepuffer ist voll */
1132 eee27628 2004-10-04 alex Log( LOG_ERR, "Receive buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1133 eee27628 2004-10-04 alex Conn_Close( Idx, "Receive buffer overflow!", NULL, FALSE );
1137 c40592d2 2003-12-26 alex #ifdef ZLIB
1138 65f3adca 2002-11-26 alex if( My_Connections[Idx].options & CONN_ZIP )
1140 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 );
1141 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1146 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 );
1147 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].rdatalen += len;
1150 747fd2f0 2001-12-13 alex if( len == 0 )
1152 747fd2f0 2001-12-13 alex /* Socket wurde geschlossen */
1153 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 ));
1154 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
1158 747fd2f0 2001-12-13 alex if( len < 0 )
1160 c6b9f3a1 2002-11-20 alex /* Operation haette Socket "nur" blockiert ... */
1161 c6b9f3a1 2002-11-20 alex if( errno == EAGAIN ) return;
1163 747fd2f0 2001-12-13 alex /* Fehler beim Lesen */
1164 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1165 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
1169 f060db5e 2002-10-09 alex /* Connection-Statistik aktualisieren */
1170 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in += len;
1172 65bdfdf2 2001-12-26 alex /* Timestamp aktualisieren */
1173 65bdfdf2 2001-12-26 alex My_Connections[Idx].lastdata = time( NULL );
1175 9ab186c4 2001-12-25 alex Handle_Buffer( Idx );
1176 9ab186c4 2001-12-25 alex } /* Read_Request */
1179 5ecb00ed 2002-11-23 alex LOCAL BOOLEAN
1180 c2f60abe 2002-05-27 alex Handle_Buffer( CONN_ID Idx )
1182 5ecb00ed 2002-11-23 alex /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1183 5ecb00ed 2002-11-23 alex * Wurde ein Request verarbeitet, so wird TRUE geliefert,
1184 5ecb00ed 2002-11-23 alex * ansonsten FALSE (auch bei Fehlern). */
1186 12cad28e 2002-10-21 alex #ifndef STRICT_RFC
1187 12cad28e 2002-10-21 alex CHAR *ptr1, *ptr2;
1189 12cad28e 2002-10-21 alex CHAR *ptr;
1190 9ab186c4 2001-12-25 alex INT len, delta;
1191 65f3adca 2002-11-26 alex BOOLEAN action, result;
1192 c40592d2 2003-12-26 alex #ifdef ZLIB
1193 d3a6537f 2002-11-28 alex BOOLEAN old_z;
1196 65f3adca 2002-11-26 alex result = FALSE;
1199 7b6e2662 2003-11-05 alex /* Check penalty */
1200 7b6e2662 2003-11-05 alex if( My_Connections[Idx].delaytime > time( NULL )) return result;
1202 c40592d2 2003-12-26 alex #ifdef ZLIB
1203 65f3adca 2002-11-26 alex /* ggf. noch unkomprimiete Daten weiter entpacken */
1204 65f3adca 2002-11-26 alex if( My_Connections[Idx].options & CONN_ZIP )
1206 65f3adca 2002-11-26 alex if( ! Unzip_Buffer( Idx )) return FALSE;
1210 65f3adca 2002-11-26 alex if( My_Connections[Idx].rdatalen < 1 ) break;
1212 65f3adca 2002-11-26 alex /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1213 65f3adca 2002-11-26 alex * RFC 2812. Haben wir eine? */
1214 65f3adca 2002-11-26 alex My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1215 65f3adca 2002-11-26 alex ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1217 65f3adca 2002-11-26 alex if( ptr ) delta = 2;
1218 65f3adca 2002-11-26 alex #ifndef STRICT_RFC
1221 65f3adca 2002-11-26 alex /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1222 65f3adca 2002-11-26 alex * machen soetwas viele Clients, u.a. "mIRC" :-( */
1223 65f3adca 2002-11-26 alex ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1224 65f3adca 2002-11-26 alex ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1225 65f3adca 2002-11-26 alex delta = 1;
1226 65f3adca 2002-11-26 alex if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1227 65f3adca 2002-11-26 alex else if( ptr1 ) ptr = ptr1;
1228 65f3adca 2002-11-26 alex else if( ptr2 ) ptr = ptr2;
1232 65f3adca 2002-11-26 alex action = FALSE;
1235 65f3adca 2002-11-26 alex /* Ende der Anfrage wurde gefunden */
1236 65f3adca 2002-11-26 alex *ptr = '\0';
1237 65f3adca 2002-11-26 alex len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1238 65f3adca 2002-11-26 alex if( len > ( COMMAND_LEN - 1 ))
1240 65f3adca 2002-11-26 alex /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1241 65f3adca 2002-11-26 alex * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1242 65f3adca 2002-11-26 alex * empfangen wird, wird der Client disconnectiert. */
1243 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 );
1244 65f3adca 2002-11-26 alex Conn_Close( Idx, NULL, "Request too long", TRUE );
1245 65f3adca 2002-11-26 alex return FALSE;
1248 c40592d2 2003-12-26 alex #ifdef ZLIB
1249 d3a6537f 2002-11-28 alex /* merken, ob Stream bereits komprimiert wird */
1250 d3a6537f 2002-11-28 alex old_z = My_Connections[Idx].options & CONN_ZIP;
1253 65f3adca 2002-11-26 alex if( len > delta )
1255 65f3adca 2002-11-26 alex /* Es wurde ein Request gelesen */
1256 39219230 2002-12-02 alex My_Connections[Idx].msg_in++;
1257 65f3adca 2002-11-26 alex if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
1258 65f3adca 2002-11-26 alex else action = TRUE;
1261 65f3adca 2002-11-26 alex /* Puffer anpassen */
1262 65f3adca 2002-11-26 alex My_Connections[Idx].rdatalen -= len;
1263 65f3adca 2002-11-26 alex memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1265 c40592d2 2003-12-26 alex #ifdef ZLIB
1266 d3a6537f 2002-11-28 alex if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1268 d3a6537f 2002-11-28 alex /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1269 d3a6537f 2002-11-28 alex * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1270 d3a6537f 2002-11-28 alex * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1271 58736b39 2004-12-22 alex if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1273 58736b39 2004-12-22 alex /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1274 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 );
1275 58736b39 2004-12-22 alex return FALSE;
1277 58736b39 2004-12-22 alex memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1278 58736b39 2004-12-22 alex My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1279 58736b39 2004-12-22 alex My_Connections[Idx].rdatalen = 0;
1280 58736b39 2004-12-22 alex #ifdef DEBUG
1281 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1282 58736b39 2004-12-22 alex #endif /* DEBUG */
1284 58736b39 2004-12-22 alex #endif /* ZLIB */
1287 65f3adca 2002-11-26 alex if( action ) result = TRUE;
1288 65f3adca 2002-11-26 alex } while( action );
1290 65f3adca 2002-11-26 alex return result;
1291 9ab186c4 2001-12-25 alex } /* Handle_Buffer */
1294 c2f60abe 2002-05-27 alex LOCAL VOID
1295 c2f60abe 2002-05-27 alex Check_Connections( VOID )
1297 bc140df8 2001-12-29 alex /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1298 bc140df8 2001-12-29 alex * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1299 bc140df8 2001-12-29 alex * auch das nicht "hilft", Client disconnectieren. */
1301 03d971d9 2002-01-02 alex CLIENT *c;
1302 e68cdf30 2002-12-30 alex CONN_ID i;
1304 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1306 03d971d9 2002-01-02 alex if( My_Connections[i].sock == NONE ) continue;
1308 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1309 356683ff 2002-01-04 alex if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1311 03d971d9 2002-01-02 alex /* verbundener User, Server oder Service */
1312 65bdfdf2 2001-12-26 alex if( My_Connections[i].lastping > My_Connections[i].lastdata )
1314 65bdfdf2 2001-12-26 alex /* es wurde bereits ein PING gesendet */
1315 08cf5607 2001-12-26 alex if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1317 65bdfdf2 2001-12-26 alex /* Timeout */
1318 7b6e2662 2003-11-05 alex #ifdef DEBUG
1319 e39925af 2002-03-26 alex Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1321 79809118 2002-01-06 alex Conn_Close( i, NULL, "Ping timeout", TRUE );
1324 08cf5607 2001-12-26 alex else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1326 65bdfdf2 2001-12-26 alex /* es muss ein PING gesendet werden */
1327 7b6e2662 2003-11-05 alex #ifdef DEBUG
1328 65bdfdf2 2001-12-26 alex Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1330 65bdfdf2 2001-12-26 alex My_Connections[i].lastping = time( NULL );
1331 356683ff 2002-01-04 alex Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1336 03d971d9 2002-01-02 alex /* noch nicht vollstaendig aufgebaute Verbindung */
1337 03d971d9 2002-01-02 alex if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1339 03d971d9 2002-01-02 alex /* Timeout */
1340 7b6e2662 2003-11-05 alex #ifdef DEBUG
1341 140d1aa5 2002-02-27 alex Log( LOG_DEBUG, "Connection %d timed out ...", i );
1343 5457e078 2002-03-02 alex Conn_Close( i, NULL, "Timeout", FALSE );
1347 03d971d9 2002-01-02 alex } /* Check_Connections */
1350 c2f60abe 2002-05-27 alex LOCAL VOID
1351 c2f60abe 2002-05-27 alex Check_Servers( VOID )
1353 a2544e49 2002-12-30 alex /* Check if we can establish further server links */
1355 03d971d9 2002-01-02 alex RES_STAT *s;
1356 a2544e49 2002-12-30 alex CONN_ID idx;
1359 a2544e49 2002-12-30 alex /* Serach all connections, are there results from the resolver? */
1360 a2544e49 2002-12-30 alex for( idx = 0; idx < Pool_Size; idx++ )
1362 a2544e49 2002-12-30 alex if( My_Connections[idx].sock != SERVER_WAIT ) continue;
1364 a2544e49 2002-12-30 alex /* IP resolved? */
1365 a2544e49 2002-12-30 alex if( My_Connections[idx].res_stat == NULL ) New_Server( Conf_GetServer( idx ), idx );
1368 a2544e49 2002-12-30 alex /* Check all configured servers */
1369 a2544e49 2002-12-30 alex for( i = 0; i < MAX_SERVERS; i++ )
1371 8dadb17f 2002-12-31 alex /* Valid outgoing server which isn't already connected or disabled? */
1372 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;
1374 a2544e49 2002-12-30 alex /* Is there already a connection in this group? */
1375 a2544e49 2002-12-30 alex if( Conf_Server[i].group > NONE )
1377 a2544e49 2002-12-30 alex for( n = 0; n < MAX_SERVERS; n++ )
1379 a2544e49 2002-12-30 alex if( n == i ) continue;
1380 a2544e49 2002-12-30 alex if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
1382 a2544e49 2002-12-30 alex if( n < MAX_SERVERS ) continue;
1385 a2544e49 2002-12-30 alex /* Check last connect attempt? */
1386 03d971d9 2002-01-02 alex if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1388 a2544e49 2002-12-30 alex /* Okay, try to connect now */
1389 03d971d9 2002-01-02 alex Conf_Server[i].lasttry = time( NULL );
1391 a2544e49 2002-12-30 alex /* Search free connection structure */
1392 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1393 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1395 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1398 7b6e2662 2003-11-05 alex #ifdef DEBUG
1399 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1402 03d971d9 2002-01-02 alex /* Verbindungs-Struktur initialisieren */
1403 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
1404 03d971d9 2002-01-02 alex My_Connections[idx].sock = SERVER_WAIT;
1405 a2544e49 2002-12-30 alex Conf_Server[i].conn_id = idx;
1407 a2544e49 2002-12-30 alex /* Resolve Hostname. If this fails, try to use it as an IP address */
1408 a2544e49 2002-12-30 alex strlcpy( Conf_Server[i].ip, Conf_Server[i].host, sizeof( Conf_Server[i].ip ));
1409 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host ));
1410 c2f60abe 2002-05-27 alex s = Resolve_Name( Conf_Server[i].host );
1413 03d971d9 2002-01-02 alex /* Sub-Prozess wurde asyncron gestartet */
1414 03d971d9 2002-01-02 alex My_Connections[idx].res_stat = s;
1417 03d971d9 2002-01-02 alex } /* Check_Servers */
1420 c2f60abe 2002-05-27 alex LOCAL VOID
1421 c2f60abe 2002-05-27 alex New_Server( INT Server, CONN_ID Idx )
1423 83194a23 2003-04-25 alex /* Establish new server link */
1425 03d971d9 2002-01-02 alex struct sockaddr_in new_addr;
1426 03d971d9 2002-01-02 alex struct in_addr inaddr;
1427 6250dcb1 2002-11-11 alex INT res, new_sock;
1428 54e487d4 2002-01-03 alex CLIENT *c;
1430 a4d5ca63 2002-11-05 alex assert( Server > NONE );
1431 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1433 83194a23 2003-04-25 alex /* Did we get a valid IP address? */
1434 03d971d9 2002-01-02 alex if( ! Conf_Server[Server].ip[0] )
1436 83194a23 2003-04-25 alex /* No. Free connection structure and abort: */
1437 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1438 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1439 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1443 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 );
1445 31a3bfed 2002-05-19 alex #ifdef HAVE_INET_ATON
1446 03d971d9 2002-01-02 alex if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1448 31a3bfed 2002-05-19 alex memset( &inaddr, 0, sizeof( inaddr ));
1449 31a3bfed 2002-05-19 alex inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1450 31a3bfed 2002-05-19 alex if( inaddr.s_addr == (unsigned)-1 )
1453 83194a23 2003-04-25 alex /* Can't convert IP address */
1454 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1455 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1456 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 );
1460 03d971d9 2002-01-02 alex memset( &new_addr, 0, sizeof( new_addr ));
1461 03d971d9 2002-01-02 alex new_addr.sin_family = AF_INET;
1462 03d971d9 2002-01-02 alex new_addr.sin_addr = inaddr;
1463 03d971d9 2002-01-02 alex new_addr.sin_port = htons( Conf_Server[Server].port );
1465 03d971d9 2002-01-02 alex new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1466 03d971d9 2002-01-02 alex if ( new_sock < 0 )
1468 83194a23 2003-04-25 alex /* Can't create socket */
1469 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1470 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1471 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1475 6da91c34 2002-03-02 alex if( ! Init_Socket( new_sock )) return;
1477 6250dcb1 2002-11-11 alex res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1478 6250dcb1 2002-11-11 alex if(( res != 0 ) && ( errno != EINPROGRESS ))
1480 83194a23 2003-04-25 alex /* Can't connect socket */
1481 6250dcb1 2002-11-11 alex Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1482 03d971d9 2002-01-02 alex close( new_sock );
1483 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1484 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1488 03d971d9 2002-01-02 alex /* Client-Struktur initialisieren */
1489 2e4d085d 2002-01-05 alex c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1492 83194a23 2003-04-25 alex /* Can't create new client structure */
1493 03d971d9 2002-01-02 alex close( new_sock );
1494 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1495 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1496 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1499 356683ff 2002-01-04 alex Client_SetIntroducer( c, c );
1500 1f975b6e 2002-04-08 alex Client_SetToken( c, TOKEN_OUTBOUND );
1502 83194a23 2003-04-25 alex /* Register connection */
1503 03d971d9 2002-01-02 alex My_Connections[Idx].sock = new_sock;
1504 03d971d9 2002-01-02 alex My_Connections[Idx].addr = new_addr;
1505 695631b2 2002-12-26 alex strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
1507 83194a23 2003-04-25 alex /* Register new socket */
1508 03d971d9 2002-01-02 alex FD_SET( new_sock, &My_Sockets );
1509 6da91c34 2002-03-02 alex FD_SET( new_sock, &My_Connects );
1510 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1512 7b6e2662 2003-11-05 alex #ifdef DEBUG
1513 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1515 03d971d9 2002-01-02 alex } /* New_Server */
1518 c2f60abe 2002-05-27 alex LOCAL VOID
1519 e68cdf30 2002-12-30 alex Init_Conn_Struct( CONN_ID Idx )
1521 4a111033 2001-12-29 alex /* Connection-Struktur initialisieren */
1523 4a111033 2001-12-29 alex My_Connections[Idx].sock = NONE;
1524 4a111033 2001-12-29 alex My_Connections[Idx].res_stat = NULL;
1525 4a111033 2001-12-29 alex My_Connections[Idx].host[0] = '\0';
1526 4a111033 2001-12-29 alex My_Connections[Idx].rbuf[0] = '\0';
1527 4a111033 2001-12-29 alex My_Connections[Idx].rdatalen = 0;
1528 4a111033 2001-12-29 alex My_Connections[Idx].wbuf[0] = '\0';
1529 4a111033 2001-12-29 alex My_Connections[Idx].wdatalen = 0;
1530 39219230 2002-12-02 alex My_Connections[Idx].starttime = time( NULL );
1531 4a111033 2001-12-29 alex My_Connections[Idx].lastdata = time( NULL );
1532 4a111033 2001-12-29 alex My_Connections[Idx].lastping = 0;
1533 804b1ec4 2001-12-31 alex My_Connections[Idx].lastprivmsg = time( NULL );
1534 7b6cfc17 2002-08-26 alex My_Connections[Idx].delaytime = 0;
1535 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in = 0;
1536 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_out = 0;
1537 39219230 2002-12-02 alex My_Connections[Idx].msg_in = 0;
1538 39219230 2002-12-02 alex My_Connections[Idx].msg_out = 0;
1539 a29e37a4 2002-11-04 alex My_Connections[Idx].flag = 0;
1540 65f3adca 2002-11-26 alex My_Connections[Idx].options = 0;
1542 c40592d2 2003-12-26 alex #ifdef ZLIB
1543 65f3adca 2002-11-26 alex My_Connections[Idx].zip.rbuf[0] = '\0';
1544 65f3adca 2002-11-26 alex My_Connections[Idx].zip.rdatalen = 0;
1545 65f3adca 2002-11-26 alex My_Connections[Idx].zip.wbuf[0] = '\0';
1546 65f3adca 2002-11-26 alex My_Connections[Idx].zip.wdatalen = 0;
1547 65f3adca 2002-11-26 alex My_Connections[Idx].zip.bytes_in = 0;
1548 65f3adca 2002-11-26 alex My_Connections[Idx].zip.bytes_out = 0;
1550 4a111033 2001-12-29 alex } /* Init_Conn_Struct */
1553 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
1554 c2f60abe 2002-05-27 alex Init_Socket( INT Sock )
1556 bb98fd8c 2004-01-25 alex /* Initialize socket (set options) */
1558 bb98fd8c 2004-01-25 alex INT value;
1560 bb98fd8c 2004-01-25 alex #ifdef O_NONBLOCK /* unknown on A/UX */
1561 6da91c34 2002-03-02 alex if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1563 bb98fd8c 2004-01-25 alex Log( LOG_CRIT, "Can't enable non-blocking mode for socket: %s!", strerror( errno ));
1564 6da91c34 2002-03-02 alex close( Sock );
1565 6da91c34 2002-03-02 alex return FALSE;
1569 bb98fd8c 2004-01-25 alex /* Don't block this port after socket shutdown */
1570 bb98fd8c 2004-01-25 alex value = 1;
1571 bb98fd8c 2004-01-25 alex if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &value, (socklen_t)sizeof( value )) != 0 )
1573 bb98fd8c 2004-01-25 alex Log( LOG_ERR, "Can't set socket option SO_REUSEADDR: %s!", strerror( errno ));
1574 bb98fd8c 2004-01-25 alex /* ignore this error */
1577 bb98fd8c 2004-01-25 alex /* Set type of service (TOS) */
1578 bb98fd8c 2004-01-25 alex #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
1579 bb98fd8c 2004-01-25 alex value = IPTOS_LOWDELAY;
1580 bb98fd8c 2004-01-25 alex #ifdef DEBUG
1581 bb98fd8c 2004-01-25 alex Log( LOG_DEBUG, "Setting option IP_TOS on socket %d to IPTOS_LOWDELAY (%d).", Sock, value );
1583 bb98fd8c 2004-01-25 alex if( setsockopt( Sock, SOL_IP, IP_TOS, &value, (socklen_t)sizeof( value )) != 0 )
1585 bb98fd8c 2004-01-25 alex Log( LOG_ERR, "Can't set socket option IP_TOS: %s!", strerror( errno ));
1586 bb98fd8c 2004-01-25 alex /* ignore this error */
1590 6da91c34 2002-03-02 alex return TRUE;
1591 6da91c34 2002-03-02 alex } /* Init_Socket */
1594 c2f60abe 2002-05-27 alex LOCAL VOID
1595 c2f60abe 2002-05-27 alex Read_Resolver_Result( INT r_fd )
1597 3012c232 2004-05-11 alex /* Read result of resolver sub-process from pipe and update the
1598 3012c232 2004-05-11 alex * apropriate connection/client structure(s): hostname and/or
1599 3012c232 2004-05-11 alex * IDENT user name.*/
1601 4a111033 2001-12-29 alex CLIENT *c;
1602 a2544e49 2002-12-30 alex INT len, i, n;
1603 3012c232 2004-05-11 alex RES_STAT *s;
1604 3012c232 2004-05-11 alex CHAR *ptr;
1606 b79b315d 2003-12-27 alex /* Search associated connection ... */
1607 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1609 3012c232 2004-05-11 alex if(( My_Connections[i].sock != NONE )
1610 3012c232 2004-05-11 alex && ( My_Connections[i].res_stat != NULL )
1611 3012c232 2004-05-11 alex && ( My_Connections[i].res_stat->pipe[0] == r_fd ))
1614 b991de0f 2002-11-02 alex if( i >= Pool_Size )
1616 b79b315d 2003-12-27 alex /* Ops, none found? Probably the connection has already
1617 3012c232 2004-05-11 alex * been closed!? We'll ignore that ... */
1618 3012c232 2004-05-11 alex FD_CLR( r_fd, &Resolver_FDs );
1619 4a111033 2001-12-29 alex close( r_fd );
1620 7b6e2662 2003-11-05 alex #ifdef DEBUG
1621 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1626 3012c232 2004-05-11 alex /* Get resolver structure */
1627 3012c232 2004-05-11 alex s = My_Connections[i].res_stat;
1628 3012c232 2004-05-11 alex assert( s != NULL );
1630 3012c232 2004-05-11 alex /* Read result from pipe */
1631 a49a580a 2004-05-30 alex len = read( r_fd, s->buffer + s->bufpos, sizeof( s->buffer ) - s->bufpos - 1 );
1632 3012c232 2004-05-11 alex if( len < 0 )
1634 3012c232 2004-05-11 alex /* Error! */
1635 3012c232 2004-05-11 alex close( r_fd );
1636 3012c232 2004-05-11 alex Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1639 3012c232 2004-05-11 alex s->bufpos += len;
1640 3012c232 2004-05-11 alex s->buffer[s->bufpos] = '\0';
1642 3012c232 2004-05-11 alex /* If the result string is incomplete, return to main loop and
1643 3012c232 2004-05-11 alex * wait until we can read in more bytes. */
1644 c23bbe6b 2004-05-11 alex #ifdef IDENTAUTH
1645 3012c232 2004-05-11 alex try_resolve:
1647 3012c232 2004-05-11 alex ptr = strchr( s->buffer, '\n' );
1648 3012c232 2004-05-11 alex if( ! ptr ) return;
1649 3012c232 2004-05-11 alex *ptr = '\0';
1651 7b6e2662 2003-11-05 alex #ifdef DEBUG
1652 3012c232 2004-05-11 alex Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%d bytes), stage %d.", s->buffer, len, s->stage );
1655 3012c232 2004-05-11 alex /* Okay, we got a complete result: this is a host name for outgoing
1656 3012c232 2004-05-11 alex * connections and a host name or IDENT user name (if enabled) for
1657 3012c232 2004-05-11 alex * incoming conneciions.*/
1658 03d971d9 2002-01-02 alex if( My_Connections[i].sock > NONE )
1660 33f4e676 2004-02-28 alex /* Incoming connection */
1662 33f4e676 2004-02-28 alex /* Search client ... */
1663 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1664 54e487d4 2002-01-03 alex assert( c != NULL );
1666 33f4e676 2004-02-28 alex /* Only update client information of unregistered clients */
1667 3012c232 2004-05-11 alex if( Client_Type( c ) == CLIENT_UNKNOWN )
1669 3012c232 2004-05-11 alex if( s->stage == 0 )
1671 3012c232 2004-05-11 alex /* host name */
1672 3012c232 2004-05-11 alex strlcpy( My_Connections[i].host, s->buffer, sizeof( My_Connections[i].host ));
1673 3012c232 2004-05-11 alex Client_SetHostname( c, s->buffer );
1675 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
1676 3012c232 2004-05-11 alex /* clean up buffer for IDENT result */
1677 3012c232 2004-05-11 alex len = strlen( s->buffer ) + 1;
1678 3012c232 2004-05-11 alex memmove( s->buffer, s->buffer + len, sizeof( s->buffer ) - len );
1679 3012c232 2004-05-11 alex s->bufpos -= len;
1681 3012c232 2004-05-11 alex /* Don't close pipe and clean up, but
1682 3012c232 2004-05-11 alex * instead wait for IDENT result */
1683 3012c232 2004-05-11 alex s->stage = 1;
1684 3012c232 2004-05-11 alex goto try_resolve;
1686 3012c232 2004-05-11 alex else if( s->stage == 1 )
1688 3012c232 2004-05-11 alex /* IDENT user name */
1689 3012c232 2004-05-11 alex if( s->buffer[0] )
1691 3012c232 2004-05-11 alex Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, s->buffer );
1692 3012c232 2004-05-11 alex Client_SetUser( c, s->buffer, TRUE );
1694 3012c232 2004-05-11 alex else Log( LOG_INFO, "IDENT lookup for connection %ld: no result.", i );
1697 3012c232 2004-05-11 alex else Log( LOG_ERR, "Resolver: got result for unknown stage %d!?", s->stage );
1699 3012c232 2004-05-11 alex #ifdef DEBUG
1700 3012c232 2004-05-11 alex else Log( LOG_DEBUG, "Resolver: discarding result for already registered connection %d.", i );
1705 3012c232 2004-05-11 alex /* Outgoing connection (server link): set the IP address
1706 3012c232 2004-05-11 alex * so that we can connect to it in the main loop. */
1708 3012c232 2004-05-11 alex /* Search server ... */
1709 a2544e49 2002-12-30 alex n = Conf_GetServer( i );
1710 33f4e676 2004-02-28 alex assert( n > NONE );
1712 3012c232 2004-05-11 alex strlcpy( Conf_Server[n].ip, s->buffer, sizeof( Conf_Server[n].ip ));
1715 3012c232 2004-05-11 alex /* Clean up ... */
1716 3012c232 2004-05-11 alex FD_CLR( r_fd, &Resolver_FDs );
1717 3012c232 2004-05-11 alex close( My_Connections[i].res_stat->pipe[0] );
1718 3012c232 2004-05-11 alex close( My_Connections[i].res_stat->pipe[1] );
1719 3012c232 2004-05-11 alex free( My_Connections[i].res_stat );
1720 3012c232 2004-05-11 alex My_Connections[i].res_stat = NULL;
1722 b79b315d 2003-12-27 alex /* Reset penalty time */
1723 736bacde 2002-10-10 alex Conn_ResetPenalty( i );
1724 4a111033 2001-12-29 alex } /* Read_Resolver_Result */
1727 e744936d 2003-03-07 alex LOCAL VOID
1728 e744936d 2003-03-07 alex Simple_Message( INT Sock, CHAR *Msg )
1730 e744936d 2003-03-07 alex /* Write "simple" message to socket, without using compression
1731 e744936d 2003-03-07 alex * or even the connection write buffers. Used e.g. for error
1732 e744936d 2003-03-07 alex * messages by New_Connection(). */
1734 e744936d 2003-03-07 alex assert( Sock > NONE );
1735 e744936d 2003-03-07 alex assert( Msg != NULL );
1737 e744936d 2003-03-07 alex (VOID)send( Sock, Msg, strlen( Msg ), 0 );
1738 e744936d 2003-03-07 alex (VOID)send( Sock, "\r\n", 2, 0 );
1739 e744936d 2003-03-07 alex } /* Simple_Error */
1743 2981fe9e 2003-11-05 alex Count_Connections( struct sockaddr_in addr_in )
1745 2981fe9e 2003-11-05 alex INT i, cnt;
1748 2981fe9e 2003-11-05 alex for( i = 0; i < Pool_Size; i++ )
1750 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++;
1752 2981fe9e 2003-11-05 alex return cnt;
1753 2981fe9e 2003-11-05 alex } /* Count_Connections */
1756 5fefe1a3 2001-12-12 alex /* -eof- */