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 b2615bcc 2002-11-19 alex * $Id: conn.c,v 1.90 2002/11/19 12:50:20 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 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
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 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
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 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
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 a4d5ca63 2002-11-05 alex if(( My_Connections[Idx].our_server > NONE ) && ( 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 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
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 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
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 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
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 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
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 a4d5ca63 2002-11-05 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 a4d5ca63 2002-11-05 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 a4d5ca63 2002-11-05 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 a4d5ca63 2002-11-05 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 a4d5ca63 2002-11-05 alex GLOBAL VOID
663 a4d5ca63 2002-11-05 alex Conn_SetServer( CONN_ID Idx, INT ConfServer )
664 a4d5ca63 2002-11-05 alex {
665 a4d5ca63 2002-11-05 alex /* Connection als Server markieren: Index des konfigurierten
666 a4d5ca63 2002-11-05 alex * Servers speichern. Verbindung muss bereits bestehen! */
667 a4d5ca63 2002-11-05 alex
668 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
669 a4d5ca63 2002-11-05 alex assert( My_Connections[Idx].sock > NONE );
670 a4d5ca63 2002-11-05 alex
671 a4d5ca63 2002-11-05 alex My_Connections[Idx].our_server = ConfServer;
672 a4d5ca63 2002-11-05 alex } /* Conn_SetServer */
673 a4d5ca63 2002-11-05 alex
674 a4d5ca63 2002-11-05 alex
675 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
676 c2f60abe 2002-05-27 alex Try_Write( CONN_ID Idx )
677 63758dd7 2001-12-15 alex {
678 63758dd7 2001-12-15 alex /* Versuchen, Daten aus dem Schreib-Puffer in den
679 63758dd7 2001-12-15 alex * Socket zu schreiben. */
680 63758dd7 2001-12-15 alex
681 63758dd7 2001-12-15 alex fd_set write_socket;
682 9ab186c4 2001-12-25 alex
683 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
684 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
685 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
686 63758dd7 2001-12-15 alex
687 63758dd7 2001-12-15 alex FD_ZERO( &write_socket );
688 63758dd7 2001-12-15 alex FD_SET( My_Connections[Idx].sock, &write_socket );
689 63758dd7 2001-12-15 alex if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, 0 ) == -1 )
690 63758dd7 2001-12-15 alex {
691 63758dd7 2001-12-15 alex /* Fehler! */
692 63758dd7 2001-12-15 alex if( errno != EINTR )
693 63758dd7 2001-12-15 alex {
694 b840be98 2002-10-15 alex Log( LOG_ALERT, "Try_Write(): select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
695 79809118 2002-01-06 alex Conn_Close( Idx, "Server error!", NULL, FALSE );
696 63758dd7 2001-12-15 alex return FALSE;
697 63758dd7 2001-12-15 alex }
698 63758dd7 2001-12-15 alex }
699 63758dd7 2001-12-15 alex
700 63758dd7 2001-12-15 alex if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
701 63758dd7 2001-12-15 alex else return TRUE;
702 63758dd7 2001-12-15 alex } /* Try_Write */
703 63758dd7 2001-12-15 alex
704 63758dd7 2001-12-15 alex
705 c2f60abe 2002-05-27 alex LOCAL VOID
706 c2f60abe 2002-05-27 alex Handle_Read( INT Sock )
707 1c8eb478 2001-12-12 alex {
708 bc140df8 2001-12-29 alex /* Aktivitaet auf einem Socket verarbeiten:
709 bc140df8 2001-12-29 alex * - neue Clients annehmen,
710 bc140df8 2001-12-29 alex * - Daten von Clients verarbeiten,
711 bc140df8 2001-12-29 alex * - Resolver-Rueckmeldungen annehmen. */
712 1c8eb478 2001-12-12 alex
713 cf050519 2001-12-14 alex CONN_ID idx;
714 9ab186c4 2001-12-25 alex
715 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
716 590f2a3f 2002-03-11 alex
717 4a111033 2001-12-29 alex if( FD_ISSET( Sock, &My_Listeners ))
718 1c8eb478 2001-12-12 alex {
719 1c8eb478 2001-12-12 alex /* es ist einer unserer Listener-Sockets: es soll
720 1c8eb478 2001-12-12 alex * also eine neue Verbindung aufgebaut werden. */
721 1c8eb478 2001-12-12 alex
722 1c8eb478 2001-12-12 alex New_Connection( Sock );
723 1c8eb478 2001-12-12 alex }
724 c2f60abe 2002-05-27 alex else if( FD_ISSET( Sock, &Resolver_FDs ))
725 4a111033 2001-12-29 alex {
726 4a111033 2001-12-29 alex /* Rueckmeldung von einem Resolver Sub-Prozess */
727 4a111033 2001-12-29 alex
728 4a111033 2001-12-29 alex Read_Resolver_Result( Sock );
729 4a111033 2001-12-29 alex }
730 1c8eb478 2001-12-12 alex else
731 1c8eb478 2001-12-12 alex {
732 1c8eb478 2001-12-12 alex /* Ein Client Socket: entweder ein User oder Server */
733 9ab186c4 2001-12-25 alex
734 1c8eb478 2001-12-12 alex idx = Socket2Index( Sock );
735 3cf845fb 2002-10-14 alex if( idx > NONE ) Read_Request( idx );
736 1c8eb478 2001-12-12 alex }
737 63758dd7 2001-12-15 alex } /* Handle_Read */
738 1c8eb478 2001-12-12 alex
739 1c8eb478 2001-12-12 alex
740 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
741 c2f60abe 2002-05-27 alex Handle_Write( CONN_ID Idx )
742 63758dd7 2001-12-15 alex {
743 6da91c34 2002-03-02 alex /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
744 9ab186c4 2001-12-25 alex
745 6da91c34 2002-03-02 alex INT len, res, err;
746 63758dd7 2001-12-15 alex
747 b840be98 2002-10-15 alex assert( Idx > NONE );
748 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
749 6da91c34 2002-03-02 alex
750 6da91c34 2002-03-02 alex if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
751 6da91c34 2002-03-02 alex {
752 6da91c34 2002-03-02 alex /* es soll nichts geschrieben werden, sondern ein
753 6da91c34 2002-03-02 alex * connect() hat ein Ergebnis geliefert */
754 6da91c34 2002-03-02 alex
755 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Connects );
756 6da91c34 2002-03-02 alex
757 6da91c34 2002-03-02 alex /* Ergebnis des connect() ermitteln */
758 6da91c34 2002-03-02 alex len = sizeof( err );
759 6da91c34 2002-03-02 alex res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
760 6da91c34 2002-03-02 alex assert( len == sizeof( err ));
761 6da91c34 2002-03-02 alex
762 6da91c34 2002-03-02 alex /* Fehler aufgetreten? */
763 6da91c34 2002-03-02 alex if(( res != 0 ) || ( err != 0 ))
764 6da91c34 2002-03-02 alex {
765 6da91c34 2002-03-02 alex /* Fehler! */
766 6da91c34 2002-03-02 alex if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
767 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 ));
768 6da91c34 2002-03-02 alex
769 6da91c34 2002-03-02 alex /* Socket etc. pp. aufraeumen */
770 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
771 6da91c34 2002-03-02 alex close( My_Connections[Idx].sock );
772 6da91c34 2002-03-02 alex Init_Conn_Struct( Idx );
773 cbce54e0 2002-03-02 alex
774 cbce54e0 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
775 cbce54e0 2002-03-02 alex Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
776 cbce54e0 2002-03-02 alex
777 6da91c34 2002-03-02 alex return FALSE;
778 6da91c34 2002-03-02 alex }
779 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 );
780 6da91c34 2002-03-02 alex
781 6da91c34 2002-03-02 alex /* PASS und SERVER verschicken */
782 b2615bcc 2002-11-19 alex Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd_out, NGIRCd_ProtoID );
783 3cf845fb 2002-10-14 alex return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
784 6da91c34 2002-03-02 alex }
785 6da91c34 2002-03-02 alex
786 63758dd7 2001-12-15 alex assert( My_Connections[Idx].wdatalen > 0 );
787 9ab186c4 2001-12-25 alex
788 63758dd7 2001-12-15 alex /* Daten schreiben */
789 63758dd7 2001-12-15 alex len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
790 63758dd7 2001-12-15 alex if( len < 0 )
791 63758dd7 2001-12-15 alex {
792 63758dd7 2001-12-15 alex /* Oops, ein Fehler! */
793 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
794 3cf845fb 2002-10-14 alex Conn_Close( Idx, "Write error!", NULL, FALSE );
795 63758dd7 2001-12-15 alex return FALSE;
796 63758dd7 2001-12-15 alex }
797 f060db5e 2002-10-09 alex
798 f060db5e 2002-10-09 alex /* Connection-Statistik aktualisieren */
799 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_out += len;
800 9ab186c4 2001-12-25 alex
801 63758dd7 2001-12-15 alex /* Puffer anpassen */
802 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen -= len;
803 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
804 9ab186c4 2001-12-25 alex
805 63758dd7 2001-12-15 alex return TRUE;
806 63758dd7 2001-12-15 alex } /* Handle_Write */
807 63758dd7 2001-12-15 alex
808 63758dd7 2001-12-15 alex
809 c2f60abe 2002-05-27 alex LOCAL VOID
810 c2f60abe 2002-05-27 alex New_Connection( INT Sock )
811 1c8eb478 2001-12-12 alex {
812 d5c97f81 2001-12-23 alex /* Neue Client-Verbindung von Listen-Socket annehmen und
813 d5c97f81 2001-12-23 alex * CLIENT-Struktur anlegen. */
814 63758dd7 2001-12-15 alex
815 1c8eb478 2001-12-12 alex struct sockaddr_in new_addr;
816 cf050519 2001-12-14 alex INT new_sock, new_sock_len;
817 4a111033 2001-12-29 alex RES_STAT *s;
818 cf050519 2001-12-14 alex CONN_ID idx;
819 b2d472fc 2002-05-19 alex CLIENT *c;
820 b991de0f 2002-11-02 alex POINTER *ptr;
821 b991de0f 2002-11-02 alex LONG new_size;
822 590f2a3f 2002-03-11 alex
823 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
824 5fefe1a3 2001-12-12 alex
825 1c8eb478 2001-12-12 alex new_sock_len = sizeof( new_addr );
826 fb55c443 2001-12-13 alex new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
827 1c8eb478 2001-12-12 alex if( new_sock < 0 )
828 5fefe1a3 2001-12-12 alex {
829 446df061 2001-12-24 alex Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
830 5fefe1a3 2001-12-12 alex return;
831 5fefe1a3 2001-12-12 alex }
832 9ab186c4 2001-12-25 alex
833 3cf845fb 2002-10-14 alex /* Freie Connection-Struktur suchen */
834 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
835 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
836 1c8eb478 2001-12-12 alex {
837 b991de0f 2002-11-02 alex new_size = Pool_Size + CONNECTION_POOL;
838 b991de0f 2002-11-02 alex
839 b991de0f 2002-11-02 alex /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
840 b991de0f 2002-11-02 alex * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
841 b991de0f 2002-11-02 alex
842 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
843 b991de0f 2002-11-02 alex {
844 b991de0f 2002-11-02 alex /* Es ist ein Limit konfiguriert */
845 b991de0f 2002-11-02 alex if( Pool_Size >= Conf_MaxConnections )
846 b991de0f 2002-11-02 alex {
847 b991de0f 2002-11-02 alex /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
848 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
849 b991de0f 2002-11-02 alex close( new_sock );
850 b991de0f 2002-11-02 alex return;
851 b991de0f 2002-11-02 alex }
852 b991de0f 2002-11-02 alex if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
853 b991de0f 2002-11-02 alex }
854 b991de0f 2002-11-02 alex
855 b991de0f 2002-11-02 alex /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
856 b991de0f 2002-11-02 alex * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
857 b991de0f 2002-11-02 alex * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
858 b991de0f 2002-11-02 alex ptr = realloc( My_Connections, sizeof( CONNECTION ) * new_size );
859 b991de0f 2002-11-02 alex if( ! ptr )
860 b991de0f 2002-11-02 alex {
861 b991de0f 2002-11-02 alex /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
862 b991de0f 2002-11-02 alex ptr = malloc( sizeof( CONNECTION ) * new_size );
863 b991de0f 2002-11-02 alex if( ! ptr )
864 b991de0f 2002-11-02 alex {
865 b991de0f 2002-11-02 alex /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
866 b991de0f 2002-11-02 alex Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
867 b991de0f 2002-11-02 alex close( new_sock );
868 b991de0f 2002-11-02 alex return;
869 b991de0f 2002-11-02 alex }
870 b991de0f 2002-11-02 alex
871 b991de0f 2002-11-02 alex /* Struktur umkopieren ... */
872 b991de0f 2002-11-02 alex memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
873 b991de0f 2002-11-02 alex
874 b991de0f 2002-11-02 alex Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [malloc()/memcpy()]", new_size );
875 b991de0f 2002-11-02 alex }
876 b991de0f 2002-11-02 alex else Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [realloc()]", new_size );
877 b991de0f 2002-11-02 alex
878 b991de0f 2002-11-02 alex My_Connections = ptr;
879 b991de0f 2002-11-02 alex Pool_Size = new_size;
880 1c8eb478 2001-12-12 alex }
881 9ab186c4 2001-12-25 alex
882 d5c97f81 2001-12-23 alex /* Client-Struktur initialisieren */
883 b2d472fc 2002-05-19 alex c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
884 b2d472fc 2002-05-19 alex if( ! c )
885 d5c97f81 2001-12-23 alex {
886 d5c97f81 2001-12-23 alex Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
887 d5c97f81 2001-12-23 alex close( new_sock );
888 d5c97f81 2001-12-23 alex return;
889 d5c97f81 2001-12-23 alex }
890 9ab186c4 2001-12-25 alex
891 1c8eb478 2001-12-12 alex /* Verbindung registrieren */
892 4a111033 2001-12-29 alex Init_Conn_Struct( idx );
893 1c8eb478 2001-12-12 alex My_Connections[idx].sock = new_sock;
894 1c8eb478 2001-12-12 alex My_Connections[idx].addr = new_addr;
895 1c8eb478 2001-12-12 alex
896 1c8eb478 2001-12-12 alex /* Neuen Socket registrieren */
897 1c8eb478 2001-12-12 alex FD_SET( new_sock, &My_Sockets );
898 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
899 1c8eb478 2001-12-12 alex
900 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 );
901 4a111033 2001-12-29 alex
902 4a111033 2001-12-29 alex /* Hostnamen ermitteln */
903 c2f60abe 2002-05-27 alex s = Resolve_Addr( &new_addr );
904 4a111033 2001-12-29 alex if( s )
905 4a111033 2001-12-29 alex {
906 4a111033 2001-12-29 alex /* Sub-Prozess wurde asyncron gestartet */
907 b6c049cb 2002-10-09 alex Conn_WriteStr( idx, "NOTICE AUTH :%sLooking up your hostname ...", NOTICE_TXTPREFIX );
908 4a111033 2001-12-29 alex My_Connections[idx].res_stat = s;
909 4a111033 2001-12-29 alex }
910 4a111033 2001-12-29 alex else
911 4a111033 2001-12-29 alex {
912 b2d472fc 2002-05-19 alex /* kann Namen nicht aufloesen, daher wird die IP-Adresse verwendet */
913 4a111033 2001-12-29 alex strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
914 b2d472fc 2002-05-19 alex Client_SetHostname( c, My_Connections[idx].host );
915 4a111033 2001-12-29 alex }
916 7b6cfc17 2002-08-26 alex
917 7b6cfc17 2002-08-26 alex /* Penalty-Zeit setzen */
918 736bacde 2002-10-10 alex Conn_SetPenalty( idx, 4 );
919 1c8eb478 2001-12-12 alex } /* New_Connection */
920 5fefe1a3 2001-12-12 alex
921 5fefe1a3 2001-12-12 alex
922 c2f60abe 2002-05-27 alex LOCAL CONN_ID
923 c2f60abe 2002-05-27 alex Socket2Index( INT Sock )
924 1c8eb478 2001-12-12 alex {
925 63758dd7 2001-12-15 alex /* zum Socket passende Connection suchen */
926 63758dd7 2001-12-15 alex
927 cf050519 2001-12-14 alex CONN_ID idx;
928 9ab186c4 2001-12-25 alex
929 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
930 9ab186c4 2001-12-25 alex
931 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
932 9ab186c4 2001-12-25 alex
933 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
934 b840be98 2002-10-15 alex {
935 b840be98 2002-10-15 alex /* die Connection wurde vermutlich (wegen eines
936 b840be98 2002-10-15 alex * Fehlers) bereits wieder abgebaut ... */
937 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
938 b840be98 2002-10-15 alex return NONE;
939 b840be98 2002-10-15 alex }
940 3cf845fb 2002-10-14 alex else return idx;
941 1c8eb478 2001-12-12 alex } /* Socket2Index */
942 1c8eb478 2001-12-12 alex
943 1c8eb478 2001-12-12 alex
944 c2f60abe 2002-05-27 alex LOCAL VOID
945 c2f60abe 2002-05-27 alex Read_Request( CONN_ID Idx )
946 1c8eb478 2001-12-12 alex {
947 747fd2f0 2001-12-13 alex /* Daten von Socket einlesen und entsprechend behandeln.
948 747fd2f0 2001-12-13 alex * Tritt ein Fehler auf, so wird der Socket geschlossen. */
949 1c8eb478 2001-12-12 alex
950 747fd2f0 2001-12-13 alex INT len;
951 63758dd7 2001-12-15 alex
952 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
953 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
954 9ab186c4 2001-12-25 alex
955 54e487d4 2002-01-03 alex if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 2 < 0 )
956 54e487d4 2002-01-03 alex {
957 54e487d4 2002-01-03 alex /* Der Lesepuffer ist voll */
958 79809118 2002-01-06 alex Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
959 79809118 2002-01-06 alex Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
960 54e487d4 2002-01-03 alex return;
961 54e487d4 2002-01-03 alex }
962 54e487d4 2002-01-03 alex
963 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 );
964 747fd2f0 2001-12-13 alex
965 747fd2f0 2001-12-13 alex if( len == 0 )
966 1c8eb478 2001-12-12 alex {
967 747fd2f0 2001-12-13 alex /* Socket wurde geschlossen */
968 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));
969 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
970 1c8eb478 2001-12-12 alex return;
971 1c8eb478 2001-12-12 alex }
972 747fd2f0 2001-12-13 alex
973 747fd2f0 2001-12-13 alex if( len < 0 )
974 747fd2f0 2001-12-13 alex {
975 747fd2f0 2001-12-13 alex /* Fehler beim Lesen */
976 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
977 ae6ab2c3 2002-03-04 alex Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
978 747fd2f0 2001-12-13 alex return;
979 747fd2f0 2001-12-13 alex }
980 cf050519 2001-12-14 alex
981 f060db5e 2002-10-09 alex /* Connection-Statistik aktualisieren */
982 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in += len;
983 f060db5e 2002-10-09 alex
984 65bdfdf2 2001-12-26 alex /* Lesebuffer updaten */
985 63758dd7 2001-12-15 alex My_Connections[Idx].rdatalen += len;
986 54e487d4 2002-01-03 alex assert( My_Connections[Idx].rdatalen < READBUFFER_LEN );
987 63758dd7 2001-12-15 alex My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
988 9ab186c4 2001-12-25 alex
989 65bdfdf2 2001-12-26 alex /* Timestamp aktualisieren */
990 65bdfdf2 2001-12-26 alex My_Connections[Idx].lastdata = time( NULL );
991 65bdfdf2 2001-12-26 alex
992 9ab186c4 2001-12-25 alex Handle_Buffer( Idx );
993 9ab186c4 2001-12-25 alex } /* Read_Request */
994 9ab186c4 2001-12-25 alex
995 9ab186c4 2001-12-25 alex
996 c2f60abe 2002-05-27 alex LOCAL VOID
997 c2f60abe 2002-05-27 alex Handle_Buffer( CONN_ID Idx )
998 9ab186c4 2001-12-25 alex {
999 bc140df8 2001-12-29 alex /* Daten im Lese-Puffer einer Verbindung verarbeiten. */
1000 bc140df8 2001-12-29 alex
1001 12cad28e 2002-10-21 alex #ifndef STRICT_RFC
1002 12cad28e 2002-10-21 alex CHAR *ptr1, *ptr2;
1003 12cad28e 2002-10-21 alex #endif
1004 12cad28e 2002-10-21 alex CHAR *ptr;
1005 9ab186c4 2001-12-25 alex INT len, delta;
1006 590f2a3f 2002-03-11 alex
1007 63758dd7 2001-12-15 alex /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1008 63758dd7 2001-12-15 alex * RFC 2812. Haben wir eine? */
1009 63758dd7 2001-12-15 alex ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1010 9ab186c4 2001-12-25 alex
1011 9ab186c4 2001-12-25 alex if( ptr ) delta = 2;
1012 54e487d4 2002-01-03 alex #ifndef STRICT_RFC
1013 9ab186c4 2001-12-25 alex else
1014 9ab186c4 2001-12-25 alex {
1015 9ab186c4 2001-12-25 alex /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1016 9ab186c4 2001-12-25 alex * machen soetwas viele Clients, u.a. "mIRC" :-( */
1017 9ab186c4 2001-12-25 alex ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1018 9ab186c4 2001-12-25 alex ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1019 9ab186c4 2001-12-25 alex delta = 1;
1020 9ab186c4 2001-12-25 alex if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1021 9ab186c4 2001-12-25 alex else if( ptr1 ) ptr = ptr1;
1022 9ab186c4 2001-12-25 alex else if( ptr2 ) ptr = ptr2;
1023 9ab186c4 2001-12-25 alex }
1024 54e487d4 2002-01-03 alex #endif
1025 590f2a3f 2002-03-11 alex
1026 63758dd7 2001-12-15 alex if( ptr )
1027 747fd2f0 2001-12-13 alex {
1028 9ab186c4 2001-12-25 alex /* Ende der Anfrage wurde gefunden */
1029 63758dd7 2001-12-15 alex *ptr = '\0';
1030 9ab186c4 2001-12-25 alex len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1031 c2f60abe 2002-05-27 alex if( len > ( COMMAND_LEN - 1 ))
1032 54e487d4 2002-01-03 alex {
1033 54e487d4 2002-01-03 alex /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1034 54e487d4 2002-01-03 alex * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1035 54e487d4 2002-01-03 alex * empfangen wird, wird der Client disconnectiert. */
1036 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 );
1037 79809118 2002-01-06 alex Conn_Close( Idx, NULL, "Request too long", TRUE );
1038 54e487d4 2002-01-03 alex return;
1039 54e487d4 2002-01-03 alex }
1040 590f2a3f 2002-03-11 alex
1041 9ab186c4 2001-12-25 alex if( len > delta )
1042 747fd2f0 2001-12-13 alex {
1043 c4199b04 2001-12-21 alex /* Es wurde ein Request gelesen */
1044 c4199b04 2001-12-21 alex if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
1045 747fd2f0 2001-12-13 alex }
1046 63758dd7 2001-12-15 alex
1047 63758dd7 2001-12-15 alex /* Puffer anpassen */
1048 63758dd7 2001-12-15 alex My_Connections[Idx].rdatalen -= len;
1049 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1050 747fd2f0 2001-12-13 alex }
1051 9ab186c4 2001-12-25 alex } /* Handle_Buffer */
1052 747fd2f0 2001-12-13 alex
1053 747fd2f0 2001-12-13 alex
1054 c2f60abe 2002-05-27 alex LOCAL VOID
1055 c2f60abe 2002-05-27 alex Check_Connections( VOID )
1056 65bdfdf2 2001-12-26 alex {
1057 bc140df8 2001-12-29 alex /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1058 bc140df8 2001-12-29 alex * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1059 bc140df8 2001-12-29 alex * auch das nicht "hilft", Client disconnectieren. */
1060 65bdfdf2 2001-12-26 alex
1061 03d971d9 2002-01-02 alex CLIENT *c;
1062 b991de0f 2002-11-02 alex LONG i;
1063 65bdfdf2 2001-12-26 alex
1064 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1065 65bdfdf2 2001-12-26 alex {
1066 03d971d9 2002-01-02 alex if( My_Connections[i].sock == NONE ) continue;
1067 03d971d9 2002-01-02 alex
1068 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1069 356683ff 2002-01-04 alex if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1070 65bdfdf2 2001-12-26 alex {
1071 03d971d9 2002-01-02 alex /* verbundener User, Server oder Service */
1072 65bdfdf2 2001-12-26 alex if( My_Connections[i].lastping > My_Connections[i].lastdata )
1073 65bdfdf2 2001-12-26 alex {
1074 65bdfdf2 2001-12-26 alex /* es wurde bereits ein PING gesendet */
1075 08cf5607 2001-12-26 alex if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1076 65bdfdf2 2001-12-26 alex {
1077 65bdfdf2 2001-12-26 alex /* Timeout */
1078 e39925af 2002-03-26 alex Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1079 79809118 2002-01-06 alex Conn_Close( i, NULL, "Ping timeout", TRUE );
1080 65bdfdf2 2001-12-26 alex }
1081 65bdfdf2 2001-12-26 alex }
1082 08cf5607 2001-12-26 alex else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1083 65bdfdf2 2001-12-26 alex {
1084 65bdfdf2 2001-12-26 alex /* es muss ein PING gesendet werden */
1085 65bdfdf2 2001-12-26 alex Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1086 65bdfdf2 2001-12-26 alex My_Connections[i].lastping = time( NULL );
1087 356683ff 2002-01-04 alex Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1088 65bdfdf2 2001-12-26 alex }
1089 65bdfdf2 2001-12-26 alex }
1090 03d971d9 2002-01-02 alex else
1091 03d971d9 2002-01-02 alex {
1092 03d971d9 2002-01-02 alex /* noch nicht vollstaendig aufgebaute Verbindung */
1093 03d971d9 2002-01-02 alex if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1094 03d971d9 2002-01-02 alex {
1095 03d971d9 2002-01-02 alex /* Timeout */
1096 140d1aa5 2002-02-27 alex Log( LOG_DEBUG, "Connection %d timed out ...", i );
1097 5457e078 2002-03-02 alex Conn_Close( i, NULL, "Timeout", FALSE );
1098 03d971d9 2002-01-02 alex }
1099 03d971d9 2002-01-02 alex }
1100 65bdfdf2 2001-12-26 alex }
1101 03d971d9 2002-01-02 alex } /* Check_Connections */
1102 03d971d9 2002-01-02 alex
1103 03d971d9 2002-01-02 alex
1104 c2f60abe 2002-05-27 alex LOCAL VOID
1105 c2f60abe 2002-05-27 alex Check_Servers( VOID )
1106 03d971d9 2002-01-02 alex {
1107 03d971d9 2002-01-02 alex /* Pruefen, ob Server-Verbindungen aufgebaut werden
1108 03d971d9 2002-01-02 alex * muessen bzw. koennen */
1109 03d971d9 2002-01-02 alex
1110 03d971d9 2002-01-02 alex RES_STAT *s;
1111 b991de0f 2002-11-02 alex LONG idx, n;
1112 b991de0f 2002-11-02 alex INT i;
1113 26ffbc78 2002-02-19 alex
1114 26ffbc78 2002-02-19 alex /* Wenn "Passive-Mode" aktiv: nicht verbinden */
1115 26ffbc78 2002-02-19 alex if( NGIRCd_Passive ) return;
1116 590f2a3f 2002-03-11 alex
1117 03d971d9 2002-01-02 alex for( i = 0; i < Conf_Server_Count; i++ )
1118 03d971d9 2002-01-02 alex {
1119 03d971d9 2002-01-02 alex /* Ist ein Hostname und Port definiert? */
1120 03d971d9 2002-01-02 alex if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue;
1121 590f2a3f 2002-03-11 alex
1122 03d971d9 2002-01-02 alex /* Haben wir schon eine Verbindung? */
1123 b991de0f 2002-11-02 alex for( n = 0; n < Pool_Size; n++ )
1124 03d971d9 2002-01-02 alex {
1125 9146fa25 2002-03-12 alex if( My_Connections[n].sock == NONE ) continue;
1126 9146fa25 2002-03-12 alex
1127 d67d94ea 2002-03-10 alex /* Verbindung zu diesem Server? */
1128 9146fa25 2002-03-12 alex if( My_Connections[n].our_server == i )
1129 03d971d9 2002-01-02 alex {
1130 03d971d9 2002-01-02 alex /* Komplett aufgebaute Verbindung? */
1131 03d971d9 2002-01-02 alex if( My_Connections[n].sock > NONE ) break;
1132 03d971d9 2002-01-02 alex
1133 03d971d9 2002-01-02 alex /* IP schon aufgeloest? */
1134 03d971d9 2002-01-02 alex if( My_Connections[n].res_stat == NULL ) New_Server( i, n );
1135 d67d94ea 2002-03-10 alex }
1136 d67d94ea 2002-03-10 alex
1137 d67d94ea 2002-03-10 alex /* Verbindung in dieser Server-Gruppe? */
1138 9146fa25 2002-03-12 alex if(( My_Connections[n].our_server != NONE ) && ( Conf_Server[i].group != NONE ))
1139 d67d94ea 2002-03-10 alex {
1140 9146fa25 2002-03-12 alex if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break;
1141 03d971d9 2002-01-02 alex }
1142 03d971d9 2002-01-02 alex }
1143 b991de0f 2002-11-02 alex if( n < Pool_Size ) continue;
1144 590f2a3f 2002-03-11 alex
1145 03d971d9 2002-01-02 alex /* Wann war der letzte Connect-Versuch? */
1146 03d971d9 2002-01-02 alex if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1147 65bdfdf2 2001-12-26 alex
1148 03d971d9 2002-01-02 alex /* Okay, Verbindungsaufbau versuchen */
1149 03d971d9 2002-01-02 alex Conf_Server[i].lasttry = time( NULL );
1150 65bdfdf2 2001-12-26 alex
1151 03d971d9 2002-01-02 alex /* Freie Connection-Struktur suschen */
1152 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1153 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1154 03d971d9 2002-01-02 alex {
1155 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1156 03d971d9 2002-01-02 alex return;
1157 03d971d9 2002-01-02 alex }
1158 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1159 03d971d9 2002-01-02 alex
1160 03d971d9 2002-01-02 alex /* Verbindungs-Struktur initialisieren */
1161 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
1162 03d971d9 2002-01-02 alex My_Connections[idx].sock = SERVER_WAIT;
1163 03d971d9 2002-01-02 alex My_Connections[idx].our_server = i;
1164 590f2a3f 2002-03-11 alex
1165 03d971d9 2002-01-02 alex /* Hostnamen in IP aufloesen */
1166 c2f60abe 2002-05-27 alex s = Resolve_Name( Conf_Server[i].host );
1167 03d971d9 2002-01-02 alex if( s )
1168 03d971d9 2002-01-02 alex {
1169 03d971d9 2002-01-02 alex /* Sub-Prozess wurde asyncron gestartet */
1170 03d971d9 2002-01-02 alex My_Connections[idx].res_stat = s;
1171 03d971d9 2002-01-02 alex }
1172 03d971d9 2002-01-02 alex else
1173 03d971d9 2002-01-02 alex {
1174 b2d472fc 2002-05-19 alex /* kann Namen nicht aufloesen: nun versuchen wir einfach,
1175 b2d472fc 2002-05-19 alex * den "Text" direkt als IP-Adresse zu verwenden ... */
1176 b2d472fc 2002-05-19 alex strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
1177 03d971d9 2002-01-02 alex }
1178 03d971d9 2002-01-02 alex }
1179 03d971d9 2002-01-02 alex } /* Check_Servers */
1180 03d971d9 2002-01-02 alex
1181 03d971d9 2002-01-02 alex
1182 c2f60abe 2002-05-27 alex LOCAL VOID
1183 c2f60abe 2002-05-27 alex New_Server( INT Server, CONN_ID Idx )
1184 03d971d9 2002-01-02 alex {
1185 03d971d9 2002-01-02 alex /* Neue Server-Verbindung aufbauen */
1186 03d971d9 2002-01-02 alex
1187 03d971d9 2002-01-02 alex struct sockaddr_in new_addr;
1188 03d971d9 2002-01-02 alex struct in_addr inaddr;
1189 6250dcb1 2002-11-11 alex INT res, new_sock;
1190 54e487d4 2002-01-03 alex CLIENT *c;
1191 03d971d9 2002-01-02 alex
1192 a4d5ca63 2002-11-05 alex assert( Server > NONE );
1193 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1194 03d971d9 2002-01-02 alex
1195 03d971d9 2002-01-02 alex /* Wurde eine gueltige IP-Adresse gefunden? */
1196 03d971d9 2002-01-02 alex if( ! Conf_Server[Server].ip[0] )
1197 03d971d9 2002-01-02 alex {
1198 03d971d9 2002-01-02 alex /* Nein. Verbindung wieder freigeben: */
1199 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1200 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1201 03d971d9 2002-01-02 alex return;
1202 03d971d9 2002-01-02 alex }
1203 590f2a3f 2002-03-11 alex
1204 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 );
1205 03d971d9 2002-01-02 alex
1206 31a3bfed 2002-05-19 alex #ifdef HAVE_INET_ATON
1207 03d971d9 2002-01-02 alex if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1208 31a3bfed 2002-05-19 alex #else
1209 31a3bfed 2002-05-19 alex memset( &inaddr, 0, sizeof( inaddr ));
1210 31a3bfed 2002-05-19 alex inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1211 31a3bfed 2002-05-19 alex if( inaddr.s_addr == (unsigned)-1 )
1212 31a3bfed 2002-05-19 alex #endif
1213 03d971d9 2002-01-02 alex {
1214 03d971d9 2002-01-02 alex /* Konnte Adresse nicht konvertieren */
1215 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1216 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 );
1217 03d971d9 2002-01-02 alex return;
1218 03d971d9 2002-01-02 alex }
1219 03d971d9 2002-01-02 alex
1220 03d971d9 2002-01-02 alex memset( &new_addr, 0, sizeof( new_addr ));
1221 03d971d9 2002-01-02 alex new_addr.sin_family = AF_INET;
1222 03d971d9 2002-01-02 alex new_addr.sin_addr = inaddr;
1223 03d971d9 2002-01-02 alex new_addr.sin_port = htons( Conf_Server[Server].port );
1224 03d971d9 2002-01-02 alex
1225 03d971d9 2002-01-02 alex new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1226 03d971d9 2002-01-02 alex if ( new_sock < 0 )
1227 03d971d9 2002-01-02 alex {
1228 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1229 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1230 03d971d9 2002-01-02 alex return;
1231 03d971d9 2002-01-02 alex }
1232 6da91c34 2002-03-02 alex
1233 6da91c34 2002-03-02 alex if( ! Init_Socket( new_sock )) return;
1234 6da91c34 2002-03-02 alex
1235 6250dcb1 2002-11-11 alex res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1236 6250dcb1 2002-11-11 alex if(( res != 0 ) && ( errno != EINPROGRESS ))
1237 03d971d9 2002-01-02 alex {
1238 6250dcb1 2002-11-11 alex Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1239 03d971d9 2002-01-02 alex close( new_sock );
1240 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1241 03d971d9 2002-01-02 alex return;
1242 03d971d9 2002-01-02 alex }
1243 03d971d9 2002-01-02 alex
1244 03d971d9 2002-01-02 alex /* Client-Struktur initialisieren */
1245 2e4d085d 2002-01-05 alex c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1246 54e487d4 2002-01-03 alex if( ! c )
1247 03d971d9 2002-01-02 alex {
1248 03d971d9 2002-01-02 alex close( new_sock );
1249 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1250 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1251 03d971d9 2002-01-02 alex return;
1252 03d971d9 2002-01-02 alex }
1253 356683ff 2002-01-04 alex Client_SetIntroducer( c, c );
1254 1f975b6e 2002-04-08 alex Client_SetToken( c, TOKEN_OUTBOUND );
1255 6da91c34 2002-03-02 alex
1256 03d971d9 2002-01-02 alex /* Verbindung registrieren */
1257 03d971d9 2002-01-02 alex My_Connections[Idx].sock = new_sock;
1258 03d971d9 2002-01-02 alex My_Connections[Idx].addr = new_addr;
1259 03d971d9 2002-01-02 alex strcpy( My_Connections[Idx].host, Conf_Server[Server].host );
1260 03d971d9 2002-01-02 alex
1261 03d971d9 2002-01-02 alex /* Neuen Socket registrieren */
1262 03d971d9 2002-01-02 alex FD_SET( new_sock, &My_Sockets );
1263 6da91c34 2002-03-02 alex FD_SET( new_sock, &My_Connects );
1264 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1265 b840be98 2002-10-15 alex
1266 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1267 03d971d9 2002-01-02 alex } /* New_Server */
1268 03d971d9 2002-01-02 alex
1269 03d971d9 2002-01-02 alex
1270 c2f60abe 2002-05-27 alex LOCAL VOID
1271 b991de0f 2002-11-02 alex Init_Conn_Struct( LONG Idx )
1272 4a111033 2001-12-29 alex {
1273 4a111033 2001-12-29 alex /* Connection-Struktur initialisieren */
1274 4a111033 2001-12-29 alex
1275 4a111033 2001-12-29 alex My_Connections[Idx].sock = NONE;
1276 4a111033 2001-12-29 alex My_Connections[Idx].res_stat = NULL;
1277 4a111033 2001-12-29 alex My_Connections[Idx].host[0] = '\0';
1278 4a111033 2001-12-29 alex My_Connections[Idx].rbuf[0] = '\0';
1279 4a111033 2001-12-29 alex My_Connections[Idx].rdatalen = 0;
1280 4a111033 2001-12-29 alex My_Connections[Idx].wbuf[0] = '\0';
1281 4a111033 2001-12-29 alex My_Connections[Idx].wdatalen = 0;
1282 d67d94ea 2002-03-10 alex My_Connections[Idx].our_server = NONE;
1283 4a111033 2001-12-29 alex My_Connections[Idx].lastdata = time( NULL );
1284 4a111033 2001-12-29 alex My_Connections[Idx].lastping = 0;
1285 804b1ec4 2001-12-31 alex My_Connections[Idx].lastprivmsg = time( NULL );
1286 7b6cfc17 2002-08-26 alex My_Connections[Idx].delaytime = 0;
1287 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in = 0;
1288 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_out = 0;
1289 a29e37a4 2002-11-04 alex My_Connections[Idx].flag = 0;
1290 4a111033 2001-12-29 alex } /* Init_Conn_Struct */
1291 4a111033 2001-12-29 alex
1292 4a111033 2001-12-29 alex
1293 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
1294 c2f60abe 2002-05-27 alex Init_Socket( INT Sock )
1295 6da91c34 2002-03-02 alex {
1296 6da91c34 2002-03-02 alex /* Socket-Optionen setzen */
1297 6da91c34 2002-03-02 alex
1298 6da91c34 2002-03-02 alex INT on = 1;
1299 6da91c34 2002-03-02 alex
1300 239727b4 2002-03-13 alex #ifdef O_NONBLOCK /* A/UX kennt das nicht? */
1301 6da91c34 2002-03-02 alex if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1302 6da91c34 2002-03-02 alex {
1303 6da91c34 2002-03-02 alex Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1304 6da91c34 2002-03-02 alex close( Sock );
1305 6da91c34 2002-03-02 alex return FALSE;
1306 6da91c34 2002-03-02 alex }
1307 239727b4 2002-03-13 alex #endif
1308 6da91c34 2002-03-02 alex if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1309 6da91c34 2002-03-02 alex {
1310 6da91c34 2002-03-02 alex Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1311 6da91c34 2002-03-02 alex /* dieser Fehler kann ignoriert werden. */
1312 6da91c34 2002-03-02 alex }
1313 6da91c34 2002-03-02 alex
1314 6da91c34 2002-03-02 alex return TRUE;
1315 6da91c34 2002-03-02 alex } /* Init_Socket */
1316 6da91c34 2002-03-02 alex
1317 6da91c34 2002-03-02 alex
1318 c2f60abe 2002-05-27 alex LOCAL VOID
1319 c2f60abe 2002-05-27 alex Read_Resolver_Result( INT r_fd )
1320 03d971d9 2002-01-02 alex {
1321 4a111033 2001-12-29 alex /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1322 c2f60abe 2002-05-27 alex * und entsprechende Connection aktualisieren */
1323 4a111033 2001-12-29 alex
1324 03d971d9 2002-01-02 alex CHAR result[HOST_LEN];
1325 4a111033 2001-12-29 alex CLIENT *c;
1326 03d971d9 2002-01-02 alex INT len, i;
1327 4a111033 2001-12-29 alex
1328 c2f60abe 2002-05-27 alex FD_CLR( r_fd, &Resolver_FDs );
1329 4a111033 2001-12-29 alex
1330 4a111033 2001-12-29 alex /* Anfrage vom Parent lesen */
1331 df2bd0e6 2002-10-03 alex len = read( r_fd, result, HOST_LEN - 1 );
1332 03d971d9 2002-01-02 alex if( len < 0 )
1333 4a111033 2001-12-29 alex {
1334 4a111033 2001-12-29 alex /* Fehler beim Lesen aus der Pipe */
1335 4a111033 2001-12-29 alex close( r_fd );
1336 79809118 2002-01-06 alex Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1337 4a111033 2001-12-29 alex return;
1338 4a111033 2001-12-29 alex }
1339 03d971d9 2002-01-02 alex result[len] = '\0';
1340 4a111033 2001-12-29 alex
1341 bc140df8 2001-12-29 alex /* zugehoerige Connection suchen */
1342 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1343 4a111033 2001-12-29 alex {
1344 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;
1345 4a111033 2001-12-29 alex }
1346 b991de0f 2002-11-02 alex if( i >= Pool_Size )
1347 4a111033 2001-12-29 alex {
1348 03d971d9 2002-01-02 alex /* Opsa! Keine passende Connection gefunden!? Vermutlich
1349 b840be98 2002-10-15 alex * wurde sie schon wieder geschlossen. */
1350 4a111033 2001-12-29 alex close( r_fd );
1351 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1352 4a111033 2001-12-29 alex return;
1353 4a111033 2001-12-29 alex }
1354 4a111033 2001-12-29 alex
1355 4a111033 2001-12-29 alex /* Aufraeumen */
1356 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[0] );
1357 b9728ba2 2001-12-29 alex close( My_Connections[i].res_stat->pipe[1] );
1358 4a111033 2001-12-29 alex free( My_Connections[i].res_stat );
1359 4a111033 2001-12-29 alex My_Connections[i].res_stat = NULL;
1360 03d971d9 2002-01-02 alex
1361 03d971d9 2002-01-02 alex if( My_Connections[i].sock > NONE )
1362 03d971d9 2002-01-02 alex {
1363 03d971d9 2002-01-02 alex /* Eingehende Verbindung: Hostnamen setzen */
1364 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1365 54e487d4 2002-01-03 alex assert( c != NULL );
1366 54e487d4 2002-01-03 alex strcpy( My_Connections[i].host, result );
1367 54e487d4 2002-01-03 alex Client_SetHostname( c, result );
1368 b6c049cb 2002-10-09 alex
1369 b6c049cb 2002-10-09 alex Conn_WriteStr( i, "NOTICE AUTH :%sGot your hostname.", NOTICE_TXTPREFIX );
1370 03d971d9 2002-01-02 alex }
1371 03d971d9 2002-01-02 alex else
1372 03d971d9 2002-01-02 alex {
1373 03d971d9 2002-01-02 alex /* Ausgehende Verbindung (=Server): IP setzen */
1374 a4d5ca63 2002-11-05 alex assert( My_Connections[i].our_server > NONE );
1375 03d971d9 2002-01-02 alex strcpy( Conf_Server[My_Connections[i].our_server].ip, result );
1376 03d971d9 2002-01-02 alex }
1377 736bacde 2002-10-10 alex
1378 736bacde 2002-10-10 alex /* Penalty-Zeit zurueck setzen */
1379 736bacde 2002-10-10 alex Conn_ResetPenalty( i );
1380 4a111033 2001-12-29 alex } /* Read_Resolver_Result */
1381 4a111033 2001-12-29 alex
1382 4a111033 2001-12-29 alex
1383 5fefe1a3 2001-12-12 alex /* -eof- */