Blame


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