2 5fefe1a3 2001-12-12 alex * ngIRCd -- The Next Generation IRC Daemon
3 03d971d9 2002-01-02 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 5fefe1a3 2001-12-12 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 5fefe1a3 2001-12-12 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 5fefe1a3 2001-12-12 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 5fefe1a3 2001-12-12 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 5fefe1a3 2001-12-12 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 804b1ec4 2001-12-31 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 c50ecda7 2002-09-26 alex * $Id: conn.c,v 1.73 2002/09/26 15:59:02 alex Exp $
14 5fefe1a3 2001-12-12 alex * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
18 ca33cbda 2002-03-12 alex #include "portab.h"
20 ca33cbda 2002-03-12 alex #include "imp.h"
21 5fefe1a3 2001-12-12 alex #include <assert.h>
22 d5c97f81 2001-12-23 alex #include <stdarg.h>
23 5fefe1a3 2001-12-12 alex #include <stdio.h>
24 b20fa7c6 2002-01-01 alex #include <stdlib.h>
25 5fefe1a3 2001-12-12 alex #include <unistd.h>
26 5fefe1a3 2001-12-12 alex #include <errno.h>
27 5fefe1a3 2001-12-12 alex #include <fcntl.h>
28 5fefe1a3 2001-12-12 alex #include <string.h>
29 9ab186c4 2001-12-25 alex #include <sys/socket.h>
30 5fefe1a3 2001-12-12 alex #include <sys/time.h>
31 9ab186c4 2001-12-25 alex #include <sys/types.h>
32 ba331a2f 2001-12-26 alex #include <time.h>
33 5fefe1a3 2001-12-12 alex #include <netinet/in.h>
35 bcc0cdc3 2002-01-05 alex #ifdef HAVE_ARPA_INET_H
36 bcc0cdc3 2002-01-05 alex #include <arpa/inet.h>
38 bcc0cdc3 2002-01-05 alex #define PF_INET AF_INET
41 5fefe1a3 2001-12-12 alex #ifdef HAVE_STDINT_H
42 5fefe1a3 2001-12-12 alex #include <stdint.h> /* u.a. fuer Mac OS X */
45 c2f60abe 2002-05-27 alex #include "exp.h"
46 c2f60abe 2002-05-27 alex #include "conn.h"
48 c2f60abe 2002-05-27 alex #include "imp.h"
49 1c8eb478 2001-12-12 alex #include "ngircd.h"
50 d5c97f81 2001-12-23 alex #include "client.h"
51 c2f60abe 2002-05-27 alex #include "resolve.h"
52 65bdfdf2 2001-12-26 alex #include "conf.h"
53 5fefe1a3 2001-12-12 alex #include "log.h"
54 c4199b04 2001-12-21 alex #include "parse.h"
55 1c8eb478 2001-12-12 alex #include "tool.h"
57 ca33cbda 2002-03-12 alex #include "exp.h"
60 34d54344 2002-03-12 alex #define SERVER_WAIT (NONE - 1)
63 1c8eb478 2001-12-12 alex typedef struct _Connection
65 1c8eb478 2001-12-12 alex INT sock; /* Socket Handle */
66 1c8eb478 2001-12-12 alex struct sockaddr_in addr; /* Adresse des Client */
67 4a111033 2001-12-29 alex RES_STAT *res_stat; /* "Resolver-Status", s.o. */
68 4a111033 2001-12-29 alex CHAR host[HOST_LEN]; /* Hostname */
69 804b1ec4 2001-12-31 alex CHAR rbuf[READBUFFER_LEN]; /* Lesepuffer */
70 63758dd7 2001-12-15 alex INT rdatalen; /* Laenge der Daten im Lesepuffer */
71 804b1ec4 2001-12-31 alex CHAR wbuf[WRITEBUFFER_LEN]; /* Schreibpuffer */
72 63758dd7 2001-12-15 alex INT wdatalen; /* Laenge der Daten im Schreibpuffer */
73 03d971d9 2002-01-02 alex INT our_server; /* wenn von uns zu connectender Server: ID */
74 65bdfdf2 2001-12-26 alex time_t lastdata; /* Letzte Aktivitaet */
75 65bdfdf2 2001-12-26 alex time_t lastping; /* Letzter PING */
76 804b1ec4 2001-12-31 alex time_t lastprivmsg; /* Letzte PRIVMSG */
77 7b6cfc17 2002-08-26 alex time_t delaytime; /* Nicht beachten bis ("penalty") */
78 1c8eb478 2001-12-12 alex } CONNECTION;
81 c2f60abe 2002-05-27 alex LOCAL VOID Handle_Read PARAMS(( INT sock ));
82 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Handle_Write PARAMS(( CONN_ID Idx ));
83 c2f60abe 2002-05-27 alex LOCAL VOID New_Connection PARAMS(( INT Sock ));
84 c2f60abe 2002-05-27 alex LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
85 c2f60abe 2002-05-27 alex LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
86 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
87 c2f60abe 2002-05-27 alex LOCAL VOID Handle_Buffer PARAMS(( CONN_ID Idx ));
88 c2f60abe 2002-05-27 alex LOCAL VOID Check_Connections PARAMS(( VOID ));
89 c2f60abe 2002-05-27 alex LOCAL VOID Check_Servers PARAMS(( VOID ));
90 c2f60abe 2002-05-27 alex LOCAL VOID Init_Conn_Struct PARAMS(( INT Idx ));
91 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock ));
92 c2f60abe 2002-05-27 alex LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx ));
93 c2f60abe 2002-05-27 alex LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd ));
96 4a111033 2001-12-29 alex LOCAL fd_set My_Listeners;
97 1c8eb478 2001-12-12 alex LOCAL fd_set My_Sockets;
98 6da91c34 2002-03-02 alex LOCAL fd_set My_Connects;
100 1c8eb478 2001-12-12 alex LOCAL CONNECTION My_Connections[MAX_CONNECTIONS];
103 c2f60abe 2002-05-27 alex GLOBAL VOID
104 c2f60abe 2002-05-27 alex Conn_Init( VOID )
106 bc140df8 2001-12-29 alex /* Modul initialisieren: statische Strukturen "ausnullen". */
110 1c8eb478 2001-12-12 alex /* zu Beginn haben wir keine Verbindungen */
111 4a111033 2001-12-29 alex FD_ZERO( &My_Listeners );
112 1c8eb478 2001-12-12 alex FD_ZERO( &My_Sockets );
113 6da91c34 2002-03-02 alex FD_ZERO( &My_Connects );
115 c2f60abe 2002-05-27 alex Conn_MaxFD = 0;
117 1c8eb478 2001-12-12 alex /* Connection-Struktur initialisieren */
118 4a111033 2001-12-29 alex for( i = 0; i < MAX_CONNECTIONS; i++ ) Init_Conn_Struct( i );
119 5fefe1a3 2001-12-12 alex } /* Conn_Init */
122 c2f60abe 2002-05-27 alex GLOBAL VOID
123 c2f60abe 2002-05-27 alex Conn_Exit( VOID )
125 bc140df8 2001-12-29 alex /* Modul abmelden: alle noch offenen Connections
126 bc140df8 2001-12-29 alex * schliessen und freigeben. */
128 cf050519 2001-12-14 alex CONN_ID idx;
131 1c8eb478 2001-12-12 alex /* Sockets schliessen */
132 79809118 2002-01-06 alex Log( LOG_DEBUG, "Shutting down all connections ..." );
133 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
135 1c8eb478 2001-12-12 alex if( FD_ISSET( i, &My_Sockets ))
137 1c8eb478 2001-12-12 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ )
139 1c8eb478 2001-12-12 alex if( My_Connections[idx].sock == i ) break;
141 6da91c34 2002-03-02 alex if( FD_ISSET( i, &My_Listeners ))
143 1c8eb478 2001-12-12 alex close( i );
144 79809118 2002-01-06 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
146 6da91c34 2002-03-02 alex else if( FD_ISSET( i, &My_Connects ))
148 6da91c34 2002-03-02 alex close( i );
149 6da91c34 2002-03-02 alex Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
151 ae958aa1 2002-06-02 alex else if( idx < MAX_CONNECTIONS )
153 ae958aa1 2002-06-02 alex if( NGIRCd_Restart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
154 ae958aa1 2002-06-02 alex else Conn_Close( idx, NULL, "Server going down", TRUE );
158 6da91c34 2002-03-02 alex Log( LOG_WARNING, "Closing unknown connection %d ...", i );
159 1c8eb478 2001-12-12 alex close( i );
163 5fefe1a3 2001-12-12 alex } /* Conn_Exit */
166 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
167 c2f60abe 2002-05-27 alex Conn_NewListener( CONST UINT Port )
169 bc140df8 2001-12-29 alex /* Neuen Listen-Socket erzeugen: der Server wartet dann auf
170 bc140df8 2001-12-29 alex * dem angegebenen Port auf Verbindungen. Kann der Listen-
171 bc140df8 2001-12-29 alex * Socket nicht erteugt werden, so wird NULL geliefert.*/
173 1c8eb478 2001-12-12 alex struct sockaddr_in addr;
176 5fefe1a3 2001-12-12 alex /* Server-"Listen"-Socket initialisieren */
177 1c8eb478 2001-12-12 alex memset( &addr, 0, sizeof( addr ));
178 1c8eb478 2001-12-12 alex addr.sin_family = AF_INET;
179 1c8eb478 2001-12-12 alex addr.sin_port = htons( Port );
180 1c8eb478 2001-12-12 alex addr.sin_addr.s_addr = htonl( INADDR_ANY );
182 5fefe1a3 2001-12-12 alex /* Socket erzeugen */
183 1c8eb478 2001-12-12 alex sock = socket( PF_INET, SOCK_STREAM, 0);
184 fb55c443 2001-12-13 alex if( sock < 0 )
186 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
187 5fefe1a3 2001-12-12 alex return FALSE;
190 6da91c34 2002-03-02 alex if( ! Init_Socket( sock )) return FALSE;
192 5fefe1a3 2001-12-12 alex /* an Port binden */
193 1c8eb478 2001-12-12 alex if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
195 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
196 1c8eb478 2001-12-12 alex close( sock );
197 5fefe1a3 2001-12-12 alex return FALSE;
200 5fefe1a3 2001-12-12 alex /* in "listen mode" gehen :-) */
201 1c8eb478 2001-12-12 alex if( listen( sock, 10 ) != 0 )
203 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
204 1c8eb478 2001-12-12 alex close( sock );
205 5fefe1a3 2001-12-12 alex return FALSE;
208 1c8eb478 2001-12-12 alex /* Neuen Listener in Strukturen einfuegen */
209 4a111033 2001-12-29 alex FD_SET( sock, &My_Listeners );
210 1c8eb478 2001-12-12 alex FD_SET( sock, &My_Sockets );
212 c2f60abe 2002-05-27 alex if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
214 9856253d 2001-12-30 alex Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
216 5fefe1a3 2001-12-12 alex return TRUE;
217 08cf5607 2001-12-26 alex } /* Conn_NewListener */
220 c2f60abe 2002-05-27 alex GLOBAL VOID
221 ae958aa1 2002-06-02 alex Conn_Handler( VOID )
223 6c19b0e4 2002-09-07 alex /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
224 6c19b0e4 2002-09-07 alex * werden dabei durchgefuehrt, bis der Server terminieren oder neu
225 6c19b0e4 2002-09-07 alex * starten soll:
227 bc140df8 2001-12-29 alex * - neue Verbindungen annehmen,
228 03d971d9 2002-01-02 alex * - Server-Verbindungen aufbauen,
229 bc140df8 2001-12-29 alex * - geschlossene Verbindungen loeschen,
230 bc140df8 2001-12-29 alex * - volle Schreibpuffer versuchen zu schreiben,
231 bc140df8 2001-12-29 alex * - volle Lesepuffer versuchen zu verarbeiten,
232 bc140df8 2001-12-29 alex * - Antworten von Resolver Sub-Prozessen annehmen.
235 63758dd7 2001-12-15 alex fd_set read_sockets, write_sockets;
236 747fd2f0 2001-12-13 alex struct timeval tv;
237 7b6cfc17 2002-08-26 alex time_t start, t;
240 9ab186c4 2001-12-25 alex start = time( NULL );
241 ae958aa1 2002-06-02 alex while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart ))
243 03d971d9 2002-01-02 alex Check_Servers( );
245 65bdfdf2 2001-12-26 alex Check_Connections( );
247 65bdfdf2 2001-12-26 alex /* noch volle Lese-Buffer suchen */
248 9ab186c4 2001-12-25 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
250 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
252 65bdfdf2 2001-12-26 alex /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
253 65bdfdf2 2001-12-26 alex Handle_Buffer( i );
257 65bdfdf2 2001-12-26 alex /* noch volle Schreib-Puffer suchen */
258 65bdfdf2 2001-12-26 alex FD_ZERO( &write_sockets );
259 9ab186c4 2001-12-25 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
261 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
263 65bdfdf2 2001-12-26 alex /* Socket der Verbindung in Set aufnehmen */
264 65bdfdf2 2001-12-26 alex FD_SET( My_Connections[i].sock, &write_sockets );
267 6da91c34 2002-03-02 alex /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
268 6da91c34 2002-03-02 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
270 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 );
273 4a111033 2001-12-29 alex /* von welchen Sockets koennte gelesen werden? */
274 7b6cfc17 2002-08-26 alex t = time( NULL );
275 9ab186c4 2001-12-25 alex read_sockets = My_Sockets;
276 4a111033 2001-12-29 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
278 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
280 4a111033 2001-12-29 alex /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
281 6da91c34 2002-03-02 alex FD_CLR( My_Connections[i].sock, &read_sockets );
283 6da91c34 2002-03-02 alex if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
285 6da91c34 2002-03-02 alex /* Hier laeuft noch ein asyncrones connect() */
286 4a111033 2001-12-29 alex FD_CLR( My_Connections[i].sock, &read_sockets );
288 7b6cfc17 2002-08-26 alex if( My_Connections[i].delaytime > t )
290 7b6cfc17 2002-08-26 alex /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
291 7b6cfc17 2002-08-26 alex FD_CLR( My_Connections[i].sock, &read_sockets );
292 8a927a1b 2002-09-07 alex FD_CLR( My_Connections[i].sock, &write_sockets );
295 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
297 4a111033 2001-12-29 alex /* Pipes von Resolver Sub-Prozessen aufnehmen */
298 c2f60abe 2002-05-27 alex if( FD_ISSET( i, &Resolver_FDs ))
300 4a111033 2001-12-29 alex FD_SET( i, &read_sockets );
304 6c19b0e4 2002-09-07 alex /* Timeout initialisieren */
305 eab10c91 2002-09-07 alex tv.tv_sec = 1;
306 6c19b0e4 2002-09-07 alex tv.tv_usec = 0;
308 4a111033 2001-12-29 alex /* Auf Aktivitaet warten */
309 8a927a1b 2002-09-07 alex i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
310 8a927a1b 2002-09-07 alex if( i == 0 )
312 8a927a1b 2002-09-07 alex /* keine Veraenderung an den Sockets */
315 8a927a1b 2002-09-07 alex if( i == -1 )
317 8a927a1b 2002-09-07 alex /* Fehler (z.B. Interrupt) */
318 db58d347 2002-01-05 alex if( errno != EINTR )
320 79809118 2002-01-06 alex Log( LOG_EMERG, "select(): %s!", strerror( errno ));
321 f7327524 2002-05-30 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
327 9ab186c4 2001-12-25 alex /* Koennen Daten geschrieben werden? */
328 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
330 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &write_sockets )) Handle_Write( Socket2Index( i ));
333 9ab186c4 2001-12-25 alex /* Daten zum Lesen vorhanden? */
334 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
336 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
339 1c8eb478 2001-12-12 alex } /* Conn_Handler */
342 f7327524 2002-05-30 alex #ifdef PROTOTYPES
343 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
344 c2f60abe 2002-05-27 alex Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
346 f7327524 2002-05-30 alex GLOBAL BOOLEAN
347 f7327524 2002-05-30 alex Conn_WriteStr( Idx, Format, va_alist )
348 f7327524 2002-05-30 alex CONN_ID Idx;
349 f7327524 2002-05-30 alex CHAR *Format;
353 63758dd7 2001-12-15 alex /* String in Socket schreiben. CR+LF wird von dieser Funktion
354 d5c97f81 2001-12-23 alex * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
355 d5c97f81 2001-12-23 alex * getrennt und FALSE geliefert. */
357 804b1ec4 2001-12-31 alex CHAR buffer[COMMAND_LEN];
358 d5c97f81 2001-12-23 alex BOOLEAN ok;
359 d5c97f81 2001-12-23 alex va_list ap;
361 28c5a21f 2002-03-14 alex assert( Idx >= 0 );
362 28c5a21f 2002-03-14 alex assert( Format != NULL );
364 f7327524 2002-05-30 alex #ifdef PROTOTYPES
365 d5c97f81 2001-12-23 alex va_start( ap, Format );
367 f7327524 2002-05-30 alex va_start( ap );
369 804b1ec4 2001-12-31 alex if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
371 79809118 2002-01-06 alex Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
372 79809118 2002-01-06 alex Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
373 63758dd7 2001-12-15 alex return FALSE;
376 7c91951d 2001-12-25 alex #ifdef SNIFFER
377 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
380 446df061 2001-12-24 alex strcat( buffer, "\r\n" );
381 446df061 2001-12-24 alex ok = Conn_Write( Idx, buffer, strlen( buffer ));
383 d5c97f81 2001-12-23 alex va_end( ap );
385 63758dd7 2001-12-15 alex } /* Conn_WriteStr */
388 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
389 c2f60abe 2002-05-27 alex Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
391 63758dd7 2001-12-15 alex /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
392 63758dd7 2001-12-15 alex * der Client disconnectiert und FALSE geliefert. */
394 63758dd7 2001-12-15 alex assert( Idx >= 0 );
395 63758dd7 2001-12-15 alex assert( Data != NULL );
396 63758dd7 2001-12-15 alex assert( Len > 0 );
398 c50ecda7 2002-09-26 alex /* Ist der entsprechende Socket ueberhaupt noch offen?
399 c50ecda7 2002-09-26 alex * In einem "Handler-Durchlauf" kann es passieren, dass
400 c50ecda7 2002-09-26 alex * dem nicht mehr so ist, wenn einer von mehreren
401 c50ecda7 2002-09-26 alex * Conn_Write()'s fehlgeschlagen ist. In diesem Fall
402 c50ecda7 2002-09-26 alex * wird hier einfach ein Fehler geliefert. */
403 c50ecda7 2002-09-26 alex if( ! My_Connections[Idx].sock > NONE )
405 c50ecda7 2002-09-26 alex Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
406 c50ecda7 2002-09-26 alex return FALSE;
409 63758dd7 2001-12-15 alex /* pruefen, ob Daten im Schreibpuffer sind. Wenn ja, zunaechst
410 63758dd7 2001-12-15 alex * pruefen, ob diese gesendet werden koennen */
411 63758dd7 2001-12-15 alex if( My_Connections[Idx].wdatalen > 0 )
413 63758dd7 2001-12-15 alex if( ! Try_Write( Idx )) return FALSE;
416 63758dd7 2001-12-15 alex /* pruefen, ob im Schreibpuffer genuegend Platz ist */
417 63758dd7 2001-12-15 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
419 63758dd7 2001-12-15 alex /* der Puffer ist dummerweise voll ... */
420 63758dd7 2001-12-15 alex Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
421 79809118 2002-01-06 alex Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
422 63758dd7 2001-12-15 alex return FALSE;
425 63758dd7 2001-12-15 alex /* Daten in Puffer kopieren */
426 63758dd7 2001-12-15 alex memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
427 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen += Len;
429 63758dd7 2001-12-15 alex /* pruefen, on Daten vorhanden sind und geschrieben werden koennen */
430 63758dd7 2001-12-15 alex if( My_Connections[Idx].wdatalen > 0 )
432 63758dd7 2001-12-15 alex if( ! Try_Write( Idx )) return FALSE;
435 63758dd7 2001-12-15 alex return TRUE;
436 63758dd7 2001-12-15 alex } /* Conn_Write */
439 c2f60abe 2002-05-27 alex GLOBAL VOID
440 c2f60abe 2002-05-27 alex Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
442 bc140df8 2001-12-29 alex /* Verbindung schliessen. Evtl. noch von Resolver
443 bc140df8 2001-12-29 alex * Sub-Prozessen offene Pipes werden geschlossen. */
447 7c91951d 2001-12-25 alex assert( Idx >= 0 );
448 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
450 79809118 2002-01-06 alex if( InformClient )
452 79809118 2002-01-06 alex if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
453 79809118 2002-01-06 alex else Conn_WriteStr( Idx, "ERROR :Closing connection." );
454 03d971d9 2002-01-02 alex if( My_Connections[Idx].sock == NONE ) return;
457 7c91951d 2001-12-25 alex if( close( My_Connections[Idx].sock ) != 0 )
459 7c91951d 2001-12-25 alex Log( LOG_ERR, "Error closing connection %d with %s:%d - %s!", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
463 4d4f2d4f 2002-01-04 alex Log( LOG_INFO, "Connection %d with %s:%d closed.", Idx, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
466 03d971d9 2002-01-02 alex c = Client_GetFromConn( Idx );
467 50ec7a56 2002-03-11 alex if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
469 4a111033 2001-12-29 alex if( My_Connections[Idx].res_stat )
471 4a111033 2001-12-29 alex /* Resolver-Strukturen freigeben, wenn noch nicht geschehen */
472 c2f60abe 2002-05-27 alex FD_CLR( My_Connections[Idx].res_stat->pipe[0], &Resolver_FDs );
473 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[0] );
474 b9728ba2 2001-12-29 alex close( My_Connections[Idx].res_stat->pipe[1] );
475 4a111033 2001-12-29 alex free( My_Connections[Idx].res_stat );
478 a3ee1a9a 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt so setzen, dass
479 a3ee1a9a 2002-03-02 alex * der naechste Verbindungsversuch in RECONNECT_DELAY Sekunden
480 590f2a3f 2002-03-11 alex * gestartet wird. */
481 590f2a3f 2002-03-11 alex if(( My_Connections[Idx].our_server >= 0 ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry < time( NULL )))
483 590f2a3f 2002-03-11 alex /* Okay, die Verbindung stand schon "genuegend lange" */
484 590f2a3f 2002-03-11 alex Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
487 7c91951d 2001-12-25 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
488 5457e078 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
489 7c91951d 2001-12-25 alex My_Connections[Idx].sock = NONE;
490 7c91951d 2001-12-25 alex } /* Conn_Close */
493 c2f60abe 2002-05-27 alex GLOBAL VOID
494 c2f60abe 2002-05-27 alex Conn_UpdateIdle( CONN_ID Idx )
496 804b1ec4 2001-12-31 alex /* Idle-Timer zuruecksetzen */
498 804b1ec4 2001-12-31 alex assert( Idx >= 0 );
499 804b1ec4 2001-12-31 alex My_Connections[Idx].lastprivmsg = time( NULL );
503 c2f60abe 2002-05-27 alex GLOBAL time_t
504 c2f60abe 2002-05-27 alex Conn_GetIdle( CONN_ID Idx )
506 804b1ec4 2001-12-31 alex /* Idle-Time einer Verbindung liefern (in Sekunden) */
508 804b1ec4 2001-12-31 alex assert( Idx >= 0 );
509 804b1ec4 2001-12-31 alex return time( NULL ) - My_Connections[Idx].lastprivmsg;
510 804b1ec4 2001-12-31 alex } /* Conn_GetIdle */
513 c2f60abe 2002-05-27 alex GLOBAL time_t
514 c2f60abe 2002-05-27 alex Conn_LastPing( CONN_ID Idx )
516 b9f005af 2002-02-11 alex /* Zeitpunkt des letzten PING liefern */
518 b9f005af 2002-02-11 alex assert( Idx >= 0 );
519 b9f005af 2002-02-11 alex return My_Connections[Idx].lastping;
520 b9f005af 2002-02-11 alex } /* Conn_LastPing */
523 7b6cfc17 2002-08-26 alex GLOBAL VOID
524 7b6cfc17 2002-08-26 alex Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
526 7b6cfc17 2002-08-26 alex /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
527 7b6cfc17 2002-08-26 alex * waehrend dieser Zeit wird der entsprechende Socket vom Server
528 7b6cfc17 2002-08-26 alex * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
529 7b6cfc17 2002-08-26 alex * dieser Funktion nur erhoeht, nicht aber verringert werden. */
533 7b6cfc17 2002-08-26 alex assert( Idx >= 0 );
534 7b6cfc17 2002-08-26 alex assert( Seconds >= 0 );
536 7b6cfc17 2002-08-26 alex t = time( NULL ) + Seconds;
537 7b6cfc17 2002-08-26 alex if( t > My_Connections[Idx].delaytime ) My_Connections[Idx].delaytime = t;
538 7b6cfc17 2002-08-26 alex } /* Conn_SetPenalty */
541 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
542 c2f60abe 2002-05-27 alex Try_Write( CONN_ID Idx )
544 63758dd7 2001-12-15 alex /* Versuchen, Daten aus dem Schreib-Puffer in den
545 63758dd7 2001-12-15 alex * Socket zu schreiben. */
547 63758dd7 2001-12-15 alex fd_set write_socket;
549 63758dd7 2001-12-15 alex assert( Idx >= 0 );
550 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
551 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
553 63758dd7 2001-12-15 alex FD_ZERO( &write_socket );
554 63758dd7 2001-12-15 alex FD_SET( My_Connections[Idx].sock, &write_socket );
555 63758dd7 2001-12-15 alex if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, 0 ) == -1 )
557 63758dd7 2001-12-15 alex /* Fehler! */
558 63758dd7 2001-12-15 alex if( errno != EINTR )
560 79809118 2002-01-06 alex Log( LOG_ALERT, "select() failed: %s!", strerror( errno ));
561 79809118 2002-01-06 alex Conn_Close( Idx, "Server error!", NULL, FALSE );
562 63758dd7 2001-12-15 alex return FALSE;
566 63758dd7 2001-12-15 alex if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
567 63758dd7 2001-12-15 alex else return TRUE;
568 63758dd7 2001-12-15 alex } /* Try_Write */
572 c2f60abe 2002-05-27 alex Handle_Read( INT Sock )
574 bc140df8 2001-12-29 alex /* Aktivitaet auf einem Socket verarbeiten:
575 bc140df8 2001-12-29 alex * - neue Clients annehmen,
576 bc140df8 2001-12-29 alex * - Daten von Clients verarbeiten,
577 bc140df8 2001-12-29 alex * - Resolver-Rueckmeldungen annehmen. */
579 cf050519 2001-12-14 alex CONN_ID idx;
581 63758dd7 2001-12-15 alex assert( Sock >= 0 );
583 4a111033 2001-12-29 alex if( FD_ISSET( Sock, &My_Listeners ))
585 1c8eb478 2001-12-12 alex /* es ist einer unserer Listener-Sockets: es soll
586 1c8eb478 2001-12-12 alex * also eine neue Verbindung aufgebaut werden. */
588 1c8eb478 2001-12-12 alex New_Connection( Sock );
590 c2f60abe 2002-05-27 alex else if( FD_ISSET( Sock, &Resolver_FDs ))
592 4a111033 2001-12-29 alex /* Rueckmeldung von einem Resolver Sub-Prozess */
594 4a111033 2001-12-29 alex Read_Resolver_Result( Sock );
598 1c8eb478 2001-12-12 alex /* Ein Client Socket: entweder ein User oder Server */
600 1c8eb478 2001-12-12 alex idx = Socket2Index( Sock );
601 747fd2f0 2001-12-13 alex Read_Request( idx );
603 63758dd7 2001-12-15 alex } /* Handle_Read */
606 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
607 c2f60abe 2002-05-27 alex Handle_Write( CONN_ID Idx )
609 6da91c34 2002-03-02 alex /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
611 6da91c34 2002-03-02 alex INT len, res, err;
613 63758dd7 2001-12-15 alex assert( Idx >= 0 );
614 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
616 6da91c34 2002-03-02 alex if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
618 6da91c34 2002-03-02 alex /* es soll nichts geschrieben werden, sondern ein
619 6da91c34 2002-03-02 alex * connect() hat ein Ergebnis geliefert */
621 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
623 6da91c34 2002-03-02 alex /* Ergebnis des connect() ermitteln */
624 6da91c34 2002-03-02 alex len = sizeof( err );
625 6da91c34 2002-03-02 alex res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
626 6da91c34 2002-03-02 alex assert( len == sizeof( err ));
628 6da91c34 2002-03-02 alex /* Fehler aufgetreten? */
629 6da91c34 2002-03-02 alex if(( res != 0 ) || ( err != 0 ))
631 6da91c34 2002-03-02 alex /* Fehler! */
632 6da91c34 2002-03-02 alex if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
633 6da91c34 2002-03-02 alex else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port, Idx, strerror( err ));
635 6da91c34 2002-03-02 alex /* Socket etc. pp. aufraeumen */
636 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
637 6da91c34 2002-03-02 alex close( My_Connections[Idx].sock );
638 6da91c34 2002-03-02 alex Init_Conn_Struct( Idx );
640 cbce54e0 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
641 cbce54e0 2002-03-02 alex Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
643 6da91c34 2002-03-02 alex return FALSE;
645 6da91c34 2002-03-02 alex Log( LOG_DEBUG, "Connection %d with \"%s:%d\" established, now sendig PASS and SERVER ...", Idx, My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port );
647 6da91c34 2002-03-02 alex /* PASS und SERVER verschicken */
648 d58431a0 2002-09-02 alex Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd, NGIRCd_ProtoID );
649 6da91c34 2002-03-02 alex Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
651 6da91c34 2002-03-02 alex return TRUE;
654 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
656 63758dd7 2001-12-15 alex /* Daten schreiben */
657 63758dd7 2001-12-15 alex len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
658 63758dd7 2001-12-15 alex if( len < 0 )
660 63758dd7 2001-12-15 alex /* Oops, ein Fehler! */
661 79809118 2002-01-06 alex Log( LOG_ERR, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
662 79809118 2002-01-06 alex Conn_Close( Idx, "Write error (buffer)!", NULL, FALSE );
663 63758dd7 2001-12-15 alex return FALSE;
666 63758dd7 2001-12-15 alex /* Puffer anpassen */
667 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen -= len;
668 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
670 63758dd7 2001-12-15 alex return TRUE;
671 63758dd7 2001-12-15 alex } /* Handle_Write */
675 c2f60abe 2002-05-27 alex New_Connection( INT Sock )
677 d5c97f81 2001-12-23 alex /* Neue Client-Verbindung von Listen-Socket annehmen und
678 d5c97f81 2001-12-23 alex * CLIENT-Struktur anlegen. */
680 1c8eb478 2001-12-12 alex struct sockaddr_in new_addr;
681 cf050519 2001-12-14 alex INT new_sock, new_sock_len;
682 4a111033 2001-12-29 alex RES_STAT *s;
683 cf050519 2001-12-14 alex CONN_ID idx;
686 63758dd7 2001-12-15 alex assert( Sock >= 0 );
688 1c8eb478 2001-12-12 alex new_sock_len = sizeof( new_addr );
689 fb55c443 2001-12-13 alex new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
690 1c8eb478 2001-12-12 alex if( new_sock < 0 )
692 446df061 2001-12-24 alex Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
696 1c8eb478 2001-12-12 alex /* Freie Connection-Struktur suschen */
697 03d971d9 2002-01-02 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == NONE ) break;
698 1c8eb478 2001-12-12 alex if( idx >= MAX_CONNECTIONS )
700 63758dd7 2001-12-15 alex Log( LOG_ALERT, "Can't accept connection: limit reached (%d)!", MAX_CONNECTIONS );
701 1c8eb478 2001-12-12 alex close( new_sock );
705 d5c97f81 2001-12-23 alex /* Client-Struktur initialisieren */
706 b2d472fc 2002-05-19 alex c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
709 d5c97f81 2001-12-23 alex Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
710 d5c97f81 2001-12-23 alex close( new_sock );
714 1c8eb478 2001-12-12 alex /* Verbindung registrieren */
715 4a111033 2001-12-29 alex Init_Conn_Struct( idx );
716 1c8eb478 2001-12-12 alex My_Connections[idx].sock = new_sock;
717 1c8eb478 2001-12-12 alex My_Connections[idx].addr = new_addr;
719 1c8eb478 2001-12-12 alex /* Neuen Socket registrieren */
720 1c8eb478 2001-12-12 alex FD_SET( new_sock, &My_Sockets );
721 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
723 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 );
725 4a111033 2001-12-29 alex /* Hostnamen ermitteln */
726 c2f60abe 2002-05-27 alex s = Resolve_Addr( &new_addr );
729 4a111033 2001-12-29 alex /* Sub-Prozess wurde asyncron gestartet */
730 4a111033 2001-12-29 alex My_Connections[idx].res_stat = s;
734 b2d472fc 2002-05-19 alex /* kann Namen nicht aufloesen, daher wird die IP-Adresse verwendet */
735 4a111033 2001-12-29 alex strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
736 b2d472fc 2002-05-19 alex Client_SetHostname( c, My_Connections[idx].host );
739 7b6cfc17 2002-08-26 alex /* Penalty-Zeit setzen */
740 7b6cfc17 2002-08-26 alex Conn_SetPenalty( idx, 1 );
741 1c8eb478 2001-12-12 alex } /* New_Connection */
744 c2f60abe 2002-05-27 alex LOCAL CONN_ID
745 c2f60abe 2002-05-27 alex Socket2Index( INT Sock )
747 63758dd7 2001-12-15 alex /* zum Socket passende Connection suchen */
749 cf050519 2001-12-14 alex CONN_ID idx;
751 63758dd7 2001-12-15 alex assert( Sock >= 0 );
753 1c8eb478 2001-12-12 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == Sock ) break;
755 63758dd7 2001-12-15 alex assert( idx < MAX_CONNECTIONS );
756 1c8eb478 2001-12-12 alex return idx;
757 1c8eb478 2001-12-12 alex } /* Socket2Index */
761 c2f60abe 2002-05-27 alex Read_Request( CONN_ID Idx )
763 747fd2f0 2001-12-13 alex /* Daten von Socket einlesen und entsprechend behandeln.
764 747fd2f0 2001-12-13 alex * Tritt ein Fehler auf, so wird der Socket geschlossen. */
768 63758dd7 2001-12-15 alex assert( Idx >= 0 );
769 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
771 54e487d4 2002-01-03 alex if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 2 < 0 )
773 54e487d4 2002-01-03 alex /* Der Lesepuffer ist voll */
774 79809118 2002-01-06 alex Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
775 79809118 2002-01-06 alex Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
779 54e487d4 2002-01-03 alex len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, READBUFFER_LEN - My_Connections[Idx].rdatalen - 2, 0 );
781 747fd2f0 2001-12-13 alex if( len == 0 )
783 747fd2f0 2001-12-13 alex /* Socket wurde geschlossen */
784 c4199b04 2001-12-21 alex Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
785 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
789 747fd2f0 2001-12-13 alex if( len < 0 )
791 747fd2f0 2001-12-13 alex /* Fehler beim Lesen */
792 79809118 2002-01-06 alex Log( LOG_ERR, "Read error on connection %d: %s!", Idx, strerror( errno ));
793 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
797 65bdfdf2 2001-12-26 alex /* Lesebuffer updaten */
798 63758dd7 2001-12-15 alex My_Connections[Idx].rdatalen += len;
799 54e487d4 2002-01-03 alex assert( My_Connections[Idx].rdatalen < READBUFFER_LEN );
800 63758dd7 2001-12-15 alex My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
802 65bdfdf2 2001-12-26 alex /* Timestamp aktualisieren */
803 65bdfdf2 2001-12-26 alex My_Connections[Idx].lastdata = time( NULL );
805 9ab186c4 2001-12-25 alex Handle_Buffer( Idx );
806 9ab186c4 2001-12-25 alex } /* Read_Request */
810 c2f60abe 2002-05-27 alex Handle_Buffer( CONN_ID Idx )
812 bc140df8 2001-12-29 alex /* Daten im Lese-Puffer einer Verbindung verarbeiten. */
814 9ab186c4 2001-12-25 alex CHAR *ptr, *ptr1, *ptr2;
815 9ab186c4 2001-12-25 alex INT len, delta;
817 63758dd7 2001-12-15 alex /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
818 63758dd7 2001-12-15 alex * RFC 2812. Haben wir eine? */
819 63758dd7 2001-12-15 alex ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
821 9ab186c4 2001-12-25 alex if( ptr ) delta = 2;
822 54e487d4 2002-01-03 alex #ifndef STRICT_RFC
825 9ab186c4 2001-12-25 alex /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
826 9ab186c4 2001-12-25 alex * machen soetwas viele Clients, u.a. "mIRC" :-( */
827 9ab186c4 2001-12-25 alex ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
828 9ab186c4 2001-12-25 alex ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
830 9ab186c4 2001-12-25 alex if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
831 9ab186c4 2001-12-25 alex else if( ptr1 ) ptr = ptr1;
832 9ab186c4 2001-12-25 alex else if( ptr2 ) ptr = ptr2;
838 9ab186c4 2001-12-25 alex /* Ende der Anfrage wurde gefunden */
839 63758dd7 2001-12-15 alex *ptr = '\0';
840 9ab186c4 2001-12-25 alex len = ( ptr - My_Connections[Idx].rbuf ) + delta;
841 c2f60abe 2002-05-27 alex if( len > ( COMMAND_LEN - 1 ))
843 54e487d4 2002-01-03 alex /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
844 54e487d4 2002-01-03 alex * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
845 54e487d4 2002-01-03 alex * empfangen wird, wird der Client disconnectiert. */
846 7b6cfc17 2002-08-26 alex Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
847 79809118 2002-01-06 alex Conn_Close( Idx, NULL, "Request too long", TRUE );
851 9ab186c4 2001-12-25 alex if( len > delta )
853 c4199b04 2001-12-21 alex /* Es wurde ein Request gelesen */
854 c4199b04 2001-12-21 alex if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
857 63758dd7 2001-12-15 alex /* Puffer anpassen */
858 63758dd7 2001-12-15 alex My_Connections[Idx].rdatalen -= len;
859 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
861 9ab186c4 2001-12-25 alex } /* Handle_Buffer */
865 c2f60abe 2002-05-27 alex Check_Connections( VOID )
867 bc140df8 2001-12-29 alex /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
868 bc140df8 2001-12-29 alex * nicht der Fall, zunaechst PING-PONG spielen und, wenn
869 bc140df8 2001-12-29 alex * auch das nicht "hilft", Client disconnectieren. */
874 65bdfdf2 2001-12-26 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
876 03d971d9 2002-01-02 alex if( My_Connections[i].sock == NONE ) continue;
878 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
879 356683ff 2002-01-04 alex if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
881 03d971d9 2002-01-02 alex /* verbundener User, Server oder Service */
882 65bdfdf2 2001-12-26 alex if( My_Connections[i].lastping > My_Connections[i].lastdata )
884 65bdfdf2 2001-12-26 alex /* es wurde bereits ein PING gesendet */
885 08cf5607 2001-12-26 alex if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
887 65bdfdf2 2001-12-26 alex /* Timeout */
888 e39925af 2002-03-26 alex Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
889 79809118 2002-01-06 alex Conn_Close( i, NULL, "Ping timeout", TRUE );
892 08cf5607 2001-12-26 alex else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
894 65bdfdf2 2001-12-26 alex /* es muss ein PING gesendet werden */
895 65bdfdf2 2001-12-26 alex Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
896 65bdfdf2 2001-12-26 alex My_Connections[i].lastping = time( NULL );
897 356683ff 2002-01-04 alex Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
902 03d971d9 2002-01-02 alex /* noch nicht vollstaendig aufgebaute Verbindung */
903 03d971d9 2002-01-02 alex if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
905 03d971d9 2002-01-02 alex /* Timeout */
906 140d1aa5 2002-02-27 alex Log( LOG_DEBUG, "Connection %d timed out ...", i );
907 5457e078 2002-03-02 alex Conn_Close( i, NULL, "Timeout", FALSE );
911 03d971d9 2002-01-02 alex } /* Check_Connections */
915 c2f60abe 2002-05-27 alex Check_Servers( VOID )
917 03d971d9 2002-01-02 alex /* Pruefen, ob Server-Verbindungen aufgebaut werden
918 03d971d9 2002-01-02 alex * muessen bzw. koennen */
920 03d971d9 2002-01-02 alex INT idx, i, n;
921 03d971d9 2002-01-02 alex RES_STAT *s;
923 26ffbc78 2002-02-19 alex /* Wenn "Passive-Mode" aktiv: nicht verbinden */
924 26ffbc78 2002-02-19 alex if( NGIRCd_Passive ) return;
926 03d971d9 2002-01-02 alex for( i = 0; i < Conf_Server_Count; i++ )
928 03d971d9 2002-01-02 alex /* Ist ein Hostname und Port definiert? */
929 03d971d9 2002-01-02 alex if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue;
931 03d971d9 2002-01-02 alex /* Haben wir schon eine Verbindung? */
932 03d971d9 2002-01-02 alex for( n = 0; n < MAX_CONNECTIONS; n++ )
934 9146fa25 2002-03-12 alex if( My_Connections[n].sock == NONE ) continue;
936 d67d94ea 2002-03-10 alex /* Verbindung zu diesem Server? */
937 9146fa25 2002-03-12 alex if( My_Connections[n].our_server == i )
939 03d971d9 2002-01-02 alex /* Komplett aufgebaute Verbindung? */
940 03d971d9 2002-01-02 alex if( My_Connections[n].sock > NONE ) break;
942 03d971d9 2002-01-02 alex /* IP schon aufgeloest? */
943 03d971d9 2002-01-02 alex if( My_Connections[n].res_stat == NULL ) New_Server( i, n );
946 d67d94ea 2002-03-10 alex /* Verbindung in dieser Server-Gruppe? */
947 9146fa25 2002-03-12 alex if(( My_Connections[n].our_server != NONE ) && ( Conf_Server[i].group != NONE ))
949 9146fa25 2002-03-12 alex if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break;
952 03d971d9 2002-01-02 alex if( n < MAX_CONNECTIONS ) continue;
954 03d971d9 2002-01-02 alex /* Wann war der letzte Connect-Versuch? */
955 03d971d9 2002-01-02 alex if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
957 03d971d9 2002-01-02 alex /* Okay, Verbindungsaufbau versuchen */
958 03d971d9 2002-01-02 alex Conf_Server[i].lasttry = time( NULL );
960 03d971d9 2002-01-02 alex /* Freie Connection-Struktur suschen */
961 03d971d9 2002-01-02 alex for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == NONE ) break;
962 03d971d9 2002-01-02 alex if( idx >= MAX_CONNECTIONS )
964 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", MAX_CONNECTIONS );
967 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
969 03d971d9 2002-01-02 alex /* Verbindungs-Struktur initialisieren */
970 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
971 03d971d9 2002-01-02 alex My_Connections[idx].sock = SERVER_WAIT;
972 03d971d9 2002-01-02 alex My_Connections[idx].our_server = i;
974 03d971d9 2002-01-02 alex /* Hostnamen in IP aufloesen */
975 c2f60abe 2002-05-27 alex s = Resolve_Name( Conf_Server[i].host );
978 03d971d9 2002-01-02 alex /* Sub-Prozess wurde asyncron gestartet */
979 03d971d9 2002-01-02 alex My_Connections[idx].res_stat = s;
983 b2d472fc 2002-05-19 alex /* kann Namen nicht aufloesen: nun versuchen wir einfach,
984 b2d472fc 2002-05-19 alex * den "Text" direkt als IP-Adresse zu verwenden ... */
985 b2d472fc 2002-05-19 alex strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
988 03d971d9 2002-01-02 alex } /* Check_Servers */
992 c2f60abe 2002-05-27 alex New_Server( INT Server, CONN_ID Idx )
994 03d971d9 2002-01-02 alex /* Neue Server-Verbindung aufbauen */
996 03d971d9 2002-01-02 alex struct sockaddr_in new_addr;
997 03d971d9 2002-01-02 alex struct in_addr inaddr;
998 03d971d9 2002-01-02 alex INT new_sock;
1001 03d971d9 2002-01-02 alex assert( Server >= 0 );
1002 03d971d9 2002-01-02 alex assert( Idx >= 0 );
1004 03d971d9 2002-01-02 alex /* Wurde eine gueltige IP-Adresse gefunden? */
1005 03d971d9 2002-01-02 alex if( ! Conf_Server[Server].ip[0] )
1007 03d971d9 2002-01-02 alex /* Nein. Verbindung wieder freigeben: */
1008 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1009 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1013 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 );
1015 31a3bfed 2002-05-19 alex #ifdef HAVE_INET_ATON
1016 03d971d9 2002-01-02 alex if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1018 31a3bfed 2002-05-19 alex memset( &inaddr, 0, sizeof( inaddr ));
1019 31a3bfed 2002-05-19 alex inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1020 31a3bfed 2002-05-19 alex if( inaddr.s_addr == (unsigned)-1 )
1023 03d971d9 2002-01-02 alex /* Konnte Adresse nicht konvertieren */
1024 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1025 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 );
1029 03d971d9 2002-01-02 alex memset( &new_addr, 0, sizeof( new_addr ));
1030 03d971d9 2002-01-02 alex new_addr.sin_family = AF_INET;
1031 03d971d9 2002-01-02 alex new_addr.sin_addr = inaddr;
1032 03d971d9 2002-01-02 alex new_addr.sin_port = htons( Conf_Server[Server].port );
1034 03d971d9 2002-01-02 alex new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1035 03d971d9 2002-01-02 alex if ( new_sock < 0 )
1037 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1038 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1042 6da91c34 2002-03-02 alex if( ! Init_Socket( new_sock )) return;
1044 6da91c34 2002-03-02 alex connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1045 6da91c34 2002-03-02 alex if( errno != EINPROGRESS )
1048 03d971d9 2002-01-02 alex close( new_sock );
1049 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1050 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1054 03d971d9 2002-01-02 alex /* Client-Struktur initialisieren */
1055 2e4d085d 2002-01-05 alex c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1058 03d971d9 2002-01-02 alex close( new_sock );
1059 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1060 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1063 356683ff 2002-01-04 alex Client_SetIntroducer( c, c );
1064 1f975b6e 2002-04-08 alex Client_SetToken( c, TOKEN_OUTBOUND );
1066 03d971d9 2002-01-02 alex /* Verbindung registrieren */
1067 03d971d9 2002-01-02 alex My_Connections[Idx].sock = new_sock;
1068 03d971d9 2002-01-02 alex My_Connections[Idx].addr = new_addr;
1069 03d971d9 2002-01-02 alex strcpy( My_Connections[Idx].host, Conf_Server[Server].host );
1071 03d971d9 2002-01-02 alex /* Neuen Socket registrieren */
1072 03d971d9 2002-01-02 alex FD_SET( new_sock, &My_Sockets );
1073 6da91c34 2002-03-02 alex FD_SET( new_sock, &My_Connects );
1074 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1075 03d971d9 2002-01-02 alex } /* New_Server */
1078 c2f60abe 2002-05-27 alex LOCAL VOID
1079 c2f60abe 2002-05-27 alex Init_Conn_Struct( INT Idx )
1081 4a111033 2001-12-29 alex /* Connection-Struktur initialisieren */
1083 4a111033 2001-12-29 alex My_Connections[Idx].sock = NONE;
1084 4a111033 2001-12-29 alex My_Connections[Idx].res_stat = NULL;
1085 4a111033 2001-12-29 alex My_Connections[Idx].host[0] = '\0';
1086 4a111033 2001-12-29 alex My_Connections[Idx].rbuf[0] = '\0';
1087 4a111033 2001-12-29 alex My_Connections[Idx].rdatalen = 0;
1088 4a111033 2001-12-29 alex My_Connections[Idx].wbuf[0] = '\0';
1089 4a111033 2001-12-29 alex My_Connections[Idx].wdatalen = 0;
1090 d67d94ea 2002-03-10 alex My_Connections[Idx].our_server = NONE;
1091 4a111033 2001-12-29 alex My_Connections[Idx].lastdata = time( NULL );
1092 4a111033 2001-12-29 alex My_Connections[Idx].lastping = 0;
1093 804b1ec4 2001-12-31 alex My_Connections[Idx].lastprivmsg = time( NULL );
1094 7b6cfc17 2002-08-26 alex My_Connections[Idx].delaytime = 0;
1095 4a111033 2001-12-29 alex } /* Init_Conn_Struct */
1098 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
1099 c2f60abe 2002-05-27 alex Init_Socket( INT Sock )
1101 6da91c34 2002-03-02 alex /* Socket-Optionen setzen */
1103 6da91c34 2002-03-02 alex INT on = 1;
1105 239727b4 2002-03-13 alex #ifdef O_NONBLOCK /* A/UX kennt das nicht? */
1106 6da91c34 2002-03-02 alex if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1108 6da91c34 2002-03-02 alex Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1109 6da91c34 2002-03-02 alex close( Sock );
1110 6da91c34 2002-03-02 alex return FALSE;
1113 6da91c34 2002-03-02 alex if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1115 6da91c34 2002-03-02 alex Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1116 6da91c34 2002-03-02 alex /* dieser Fehler kann ignoriert werden. */
1119 6da91c34 2002-03-02 alex return TRUE;
1120 6da91c34 2002-03-02 alex } /* Init_Socket */
1123 c2f60abe 2002-05-27 alex LOCAL VOID
1124 c2f60abe 2002-05-27 alex Read_Resolver_Result( INT r_fd )
1126 4a111033 2001-12-29 alex /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1127 c2f60abe 2002-05-27 alex * und entsprechende Connection aktualisieren */
1129 03d971d9 2002-01-02 alex CHAR result[HOST_LEN];
1130 4a111033 2001-12-29 alex CLIENT *c;
1131 03d971d9 2002-01-02 alex INT len, i;
1133 c2f60abe 2002-05-27 alex FD_CLR( r_fd, &Resolver_FDs );
1135 4a111033 2001-12-29 alex /* Anfrage vom Parent lesen */
1136 03d971d9 2002-01-02 alex len = read( r_fd, result, HOST_LEN);
1137 03d971d9 2002-01-02 alex if( len < 0 )
1139 4a111033 2001-12-29 alex /* Fehler beim Lesen aus der Pipe */
1140 4a111033 2001-12-29 alex close( r_fd );
1141 79809118 2002-01-06 alex Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1144 03d971d9 2002-01-02 alex result[len] = '\0';
1146 bc140df8 2001-12-29 alex /* zugehoerige Connection suchen */
1147 4a111033 2001-12-29 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
1149 03d971d9 2002-01-02 alex if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break;
1151 4a111033 2001-12-29 alex if( i >= MAX_CONNECTIONS )
1153 03d971d9 2002-01-02 alex /* Opsa! Keine passende Connection gefunden!? Vermutlich
1154 c2f60abe 2002-05-27 alex * wurde sie schon wieder geschlossen. */
1155 4a111033 2001-12-29 alex close( r_fd );
1156 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1160 4a111033 2001-12-29 alex /* Aufraeumen */
1161 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[0] );
1162 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[1] );
1163 4a111033 2001-12-29 alex free( My_Connections[i].res_stat );
1164 4a111033 2001-12-29 alex My_Connections[i].res_stat = NULL;
1166 03d971d9 2002-01-02 alex if( My_Connections[i].sock > NONE )
1168 03d971d9 2002-01-02 alex /* Eingehende Verbindung: Hostnamen setzen */
1169 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1170 54e487d4 2002-01-03 alex assert( c != NULL );
1171 54e487d4 2002-01-03 alex strcpy( My_Connections[i].host, result );
1172 54e487d4 2002-01-03 alex Client_SetHostname( c, result );
1176 03d971d9 2002-01-02 alex /* Ausgehende Verbindung (=Server): IP setzen */
1177 03d971d9 2002-01-02 alex assert( My_Connections[i].our_server >= 0 );
1178 03d971d9 2002-01-02 alex strcpy( Conf_Server[My_Connections[i].our_server].ip, result );
1180 4a111033 2001-12-29 alex } /* Read_Resolver_Result */
1183 5fefe1a3 2001-12-12 alex /* -eof- */