Blame


1 5fefe1a3 2001-12-12 alex /*
2 5fefe1a3 2001-12-12 alex * ngIRCd -- The Next Generation IRC Daemon
3 33f4e676 2004-02-28 alex * Copyright (c)2001-2004 Alexander Barton <alex@barton.de>
4 5fefe1a3 2001-12-12 alex *
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 5fefe1a3 2001-12-12 alex *
11 490f28ff 2002-12-12 alex * Connection management
12 5fefe1a3 2001-12-12 alex */
13 5fefe1a3 2001-12-12 alex
14 5fefe1a3 2001-12-12 alex
15 b77dae34 2002-12-30 alex #define CONN_MODULE
16 e68cdf30 2002-12-30 alex
17 ca33cbda 2002-03-12 alex #include "portab.h"
18 490f28ff 2002-12-12 alex
19 1cf8ccd8 2005-06-12 fw static char UNUSED id[] = "$Id: conn.c,v 1.154 2005/06/12 17:21:46 fw Exp $";
20 5fefe1a3 2001-12-12 alex
21 ca33cbda 2002-03-12 alex #include "imp.h"
22 5fefe1a3 2001-12-12 alex #include <assert.h>
23 c1f32e82 2004-10-20 alex #ifdef PROTOTYPES
24 c1f32e82 2004-10-20 alex # include <stdarg.h>
25 c1f32e82 2004-10-20 alex #else
26 c1f32e82 2004-10-20 alex # include <varargs.h>
27 c1f32e82 2004-10-20 alex #endif
28 5fefe1a3 2001-12-12 alex #include <stdio.h>
29 b20fa7c6 2002-01-01 alex #include <stdlib.h>
30 5fefe1a3 2001-12-12 alex #include <unistd.h>
31 5fefe1a3 2001-12-12 alex #include <errno.h>
32 5fefe1a3 2001-12-12 alex #include <fcntl.h>
33 5fefe1a3 2001-12-12 alex #include <string.h>
34 9ab186c4 2001-12-25 alex #include <sys/socket.h>
35 5fefe1a3 2001-12-12 alex #include <sys/time.h>
36 9ab186c4 2001-12-25 alex #include <sys/types.h>
37 ba331a2f 2001-12-26 alex #include <time.h>
38 5fefe1a3 2001-12-12 alex #include <netinet/in.h>
39 5fefe1a3 2001-12-12 alex
40 bb98fd8c 2004-01-25 alex #ifdef HAVE_NETINET_IP_H
41 bb98fd8c 2004-01-25 alex # include <netinet/ip.h>
42 bb98fd8c 2004-01-25 alex #endif
43 bb98fd8c 2004-01-25 alex
44 bcc0cdc3 2002-01-05 alex #ifdef HAVE_ARPA_INET_H
45 bb98fd8c 2004-01-25 alex # include <arpa/inet.h>
46 bcc0cdc3 2002-01-05 alex #else
47 bb98fd8c 2004-01-25 alex # define PF_INET AF_INET
48 bcc0cdc3 2002-01-05 alex #endif
49 bcc0cdc3 2002-01-05 alex
50 5fefe1a3 2001-12-12 alex #ifdef HAVE_STDINT_H
51 bb98fd8c 2004-01-25 alex # include <stdint.h> /* e.g. for Mac OS X */
52 439c945d 2003-03-07 alex #endif
53 439c945d 2003-03-07 alex
54 c40592d2 2003-12-26 alex #ifdef TCPWRAP
55 bb98fd8c 2004-01-25 alex # include <tcpd.h> /* for TCP Wrappers */
56 5fefe1a3 2001-12-12 alex #endif
57 5fefe1a3 2001-12-12 alex
58 e68cdf30 2002-12-30 alex #include "defines.h"
59 e68cdf30 2002-12-30 alex #include "resolve.h"
60 65f3adca 2002-11-26 alex
61 c2f60abe 2002-05-27 alex #include "exp.h"
62 c2f60abe 2002-05-27 alex #include "conn.h"
63 c2f60abe 2002-05-27 alex
64 c2f60abe 2002-05-27 alex #include "imp.h"
65 1c8eb478 2001-12-12 alex #include "ngircd.h"
66 d5c97f81 2001-12-23 alex #include "client.h"
67 65bdfdf2 2001-12-26 alex #include "conf.h"
68 e68cdf30 2002-12-30 alex #include "conn-zip.h"
69 b77dae34 2002-12-30 alex #include "conn-func.h"
70 5fefe1a3 2001-12-12 alex #include "log.h"
71 c4199b04 2001-12-21 alex #include "parse.h"
72 1c8eb478 2001-12-12 alex #include "tool.h"
73 5fefe1a3 2001-12-12 alex
74 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
75 bb98fd8c 2004-01-25 alex # include "rendezvous.h"
76 29bd35bc 2003-02-23 alex #endif
77 29bd35bc 2003-02-23 alex
78 ca33cbda 2002-03-12 alex #include "exp.h"
79 cf050519 2001-12-14 alex
80 63758dd7 2001-12-15 alex
81 34d54344 2002-03-12 alex #define SERVER_WAIT (NONE - 1)
82 4a111033 2001-12-29 alex
83 4a111033 2001-12-29 alex
84 8adff592 2005-03-19 fw LOCAL void Handle_Read PARAMS(( int sock ));
85 8adff592 2005-03-19 fw LOCAL bool Handle_Write PARAMS(( CONN_ID Idx ));
86 8adff592 2005-03-19 fw LOCAL void New_Connection PARAMS(( int Sock ));
87 8adff592 2005-03-19 fw LOCAL CONN_ID Socket2Index PARAMS(( int Sock ));
88 8adff592 2005-03-19 fw LOCAL void Read_Request PARAMS(( CONN_ID Idx ));
89 8adff592 2005-03-19 fw LOCAL bool Handle_Buffer PARAMS(( CONN_ID Idx ));
90 8adff592 2005-03-19 fw LOCAL void Check_Connections PARAMS(( void ));
91 8adff592 2005-03-19 fw LOCAL void Check_Servers PARAMS(( void ));
92 8adff592 2005-03-19 fw LOCAL void Init_Conn_Struct PARAMS(( CONN_ID Idx ));
93 8adff592 2005-03-19 fw LOCAL bool Init_Socket PARAMS(( int Sock ));
94 8adff592 2005-03-19 fw LOCAL void New_Server PARAMS(( int Server, CONN_ID Idx ));
95 8adff592 2005-03-19 fw LOCAL void Read_Resolver_Result PARAMS(( int r_fd ));
96 8adff592 2005-03-19 fw LOCAL void Simple_Message PARAMS(( int Sock, char *Msg ));
97 8adff592 2005-03-19 fw LOCAL int Count_Connections PARAMS(( struct sockaddr_in addr ));
98 1c8eb478 2001-12-12 alex
99 4a111033 2001-12-29 alex LOCAL fd_set My_Listeners;
100 1c8eb478 2001-12-12 alex LOCAL fd_set My_Sockets;
101 439c945d 2003-03-07 alex
102 c40592d2 2003-12-26 alex #ifdef TCPWRAP
103 8adff592 2005-03-19 fw int allow_severity = LOG_INFO;
104 8adff592 2005-03-19 fw int deny_severity = LOG_ERR;
105 439c945d 2003-03-07 alex #endif
106 1c8eb478 2001-12-12 alex
107 8ac701d6 2005-06-01 fw LOCAL void
108 8ac701d6 2005-06-01 fw FreeRes_stat( CONNECTION *c )
109 8ac701d6 2005-06-01 fw {
110 8ac701d6 2005-06-01 fw assert( c != NULL );
111 8ac701d6 2005-06-01 fw assert( c->res_stat != NULL );
112 1c8eb478 2001-12-12 alex
113 8ac701d6 2005-06-01 fw if (!c->res_stat) return;
114 8ac701d6 2005-06-01 fw
115 8ac701d6 2005-06-01 fw FD_CLR( c->res_stat->pipe[0], &Resolver_FDs );
116 8ac701d6 2005-06-01 fw
117 8ac701d6 2005-06-01 fw close( c->res_stat->pipe[0] );
118 8ac701d6 2005-06-01 fw close( c->res_stat->pipe[1] );
119 8ac701d6 2005-06-01 fw
120 8ac701d6 2005-06-01 fw free( c->res_stat );
121 8ac701d6 2005-06-01 fw c->res_stat = NULL;
122 8ac701d6 2005-06-01 fw }
123 8ac701d6 2005-06-01 fw
124 8ac701d6 2005-06-01 fw
125 8adff592 2005-03-19 fw GLOBAL void
126 8adff592 2005-03-19 fw Conn_Init( void )
127 5fefe1a3 2001-12-12 alex {
128 bc140df8 2001-12-29 alex /* Modul initialisieren: statische Strukturen "ausnullen". */
129 bc140df8 2001-12-29 alex
130 cf050519 2001-12-14 alex CONN_ID i;
131 1c8eb478 2001-12-12 alex
132 b991de0f 2002-11-02 alex /* Speicher fuer Verbindungs-Pool anfordern */
133 b991de0f 2002-11-02 alex Pool_Size = CONNECTION_POOL;
134 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
135 b991de0f 2002-11-02 alex {
136 b991de0f 2002-11-02 alex /* konfiguriertes Limit beachten */
137 b991de0f 2002-11-02 alex if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
138 b991de0f 2002-11-02 alex }
139 b4363162 2005-04-16 fw My_Connections = (CONNECTION *) calloc( Pool_Size, sizeof( CONNECTION ) );
140 b991de0f 2002-11-02 alex if( ! My_Connections )
141 b991de0f 2002-11-02 alex {
142 b991de0f 2002-11-02 alex /* Speicher konnte nicht alloziert werden! */
143 b991de0f 2002-11-02 alex Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
144 b991de0f 2002-11-02 alex exit( 1 );
145 b991de0f 2002-11-02 alex }
146 7b6e2662 2003-11-05 alex #ifdef DEBUG
147 b9f16c96 2003-08-30 alex Log( LOG_DEBUG, "Allocated connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
148 7b6e2662 2003-11-05 alex #endif
149 b991de0f 2002-11-02 alex
150 1c8eb478 2001-12-12 alex /* zu Beginn haben wir keine Verbindungen */
151 4a111033 2001-12-29 alex FD_ZERO( &My_Listeners );
152 1c8eb478 2001-12-12 alex FD_ZERO( &My_Sockets );
153 9ab186c4 2001-12-25 alex
154 b991de0f 2002-11-02 alex /* Groesster File-Descriptor fuer select() */
155 c2f60abe 2002-05-27 alex Conn_MaxFD = 0;
156 9ab186c4 2001-12-25 alex
157 1c8eb478 2001-12-12 alex /* Connection-Struktur initialisieren */
158 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
159 a02bf31d 2002-12-18 alex
160 a02bf31d 2002-12-18 alex /* Global write counter */
161 a02bf31d 2002-12-18 alex WCounter = 0;
162 5fefe1a3 2001-12-12 alex } /* Conn_Init */
163 5fefe1a3 2001-12-12 alex
164 5fefe1a3 2001-12-12 alex
165 8adff592 2005-03-19 fw GLOBAL void
166 8adff592 2005-03-19 fw Conn_Exit( void )
167 5fefe1a3 2001-12-12 alex {
168 bc140df8 2001-12-29 alex /* Modul abmelden: alle noch offenen Connections
169 bc140df8 2001-12-29 alex * schliessen und freigeben. */
170 bc140df8 2001-12-29 alex
171 cf050519 2001-12-14 alex CONN_ID idx;
172 8adff592 2005-03-19 fw int i;
173 9ab186c4 2001-12-25 alex
174 7b6e2662 2003-11-05 alex #ifdef DEBUG
175 79809118 2002-01-06 alex Log( LOG_DEBUG, "Shutting down all connections ..." );
176 7b6e2662 2003-11-05 alex #endif
177 29bd35bc 2003-02-23 alex
178 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
179 29bd35bc 2003-02-23 alex Rendezvous_UnregisterListeners( );
180 29bd35bc 2003-02-23 alex #endif
181 83194a23 2003-04-25 alex
182 29bd35bc 2003-02-23 alex /* Sockets schliessen */
183 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
184 1c8eb478 2001-12-12 alex {
185 1c8eb478 2001-12-12 alex if( FD_ISSET( i, &My_Sockets ))
186 1c8eb478 2001-12-12 alex {
187 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ )
188 1c8eb478 2001-12-12 alex {
189 1c8eb478 2001-12-12 alex if( My_Connections[idx].sock == i ) break;
190 1c8eb478 2001-12-12 alex }
191 6da91c34 2002-03-02 alex if( FD_ISSET( i, &My_Listeners ))
192 1c8eb478 2001-12-12 alex {
193 1c8eb478 2001-12-12 alex close( i );
194 7b6e2662 2003-11-05 alex #ifdef DEBUG
195 79809118 2002-01-06 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
196 7b6e2662 2003-11-05 alex #endif
197 6da91c34 2002-03-02 alex }
198 b991de0f 2002-11-02 alex else if( idx < Pool_Size )
199 ae958aa1 2002-06-02 alex {
200 8adff592 2005-03-19 fw if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", true );
201 8adff592 2005-03-19 fw else Conn_Close( idx, NULL, "Server going down", true );
202 ae958aa1 2002-06-02 alex }
203 1c8eb478 2001-12-12 alex else
204 1c8eb478 2001-12-12 alex {
205 6da91c34 2002-03-02 alex Log( LOG_WARNING, "Closing unknown connection %d ...", i );
206 1c8eb478 2001-12-12 alex close( i );
207 1c8eb478 2001-12-12 alex }
208 1c8eb478 2001-12-12 alex }
209 1c8eb478 2001-12-12 alex }
210 83194a23 2003-04-25 alex
211 b991de0f 2002-11-02 alex free( My_Connections );
212 b991de0f 2002-11-02 alex My_Connections = NULL;
213 b991de0f 2002-11-02 alex Pool_Size = 0;
214 5fefe1a3 2001-12-12 alex } /* Conn_Exit */
215 5fefe1a3 2001-12-12 alex
216 5fefe1a3 2001-12-12 alex
217 8adff592 2005-03-19 fw GLOBAL int
218 8adff592 2005-03-19 fw Conn_InitListeners( void )
219 a0c032b2 2002-11-22 alex {
220 e68cdf30 2002-12-30 alex /* Initialize ports on which the server should accept connections */
221 a0c032b2 2002-11-22 alex
222 8adff592 2005-03-19 fw int created, i;
223 a0c032b2 2002-11-22 alex
224 a0c032b2 2002-11-22 alex created = 0;
225 a0c032b2 2002-11-22 alex for( i = 0; i < Conf_ListenPorts_Count; i++ )
226 a0c032b2 2002-11-22 alex {
227 a0c032b2 2002-11-22 alex if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
228 326607ee 2005-03-20 fw else Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] );
229 a0c032b2 2002-11-22 alex }
230 a0c032b2 2002-11-22 alex return created;
231 a0c032b2 2002-11-22 alex } /* Conn_InitListeners */
232 a0c032b2 2002-11-22 alex
233 a0c032b2 2002-11-22 alex
234 8adff592 2005-03-19 fw GLOBAL void
235 8adff592 2005-03-19 fw Conn_ExitListeners( void )
236 a0c032b2 2002-11-22 alex {
237 e68cdf30 2002-12-30 alex /* Close down all listening sockets */
238 a0c032b2 2002-11-22 alex
239 8adff592 2005-03-19 fw int i;
240 a0c032b2 2002-11-22 alex
241 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
242 29bd35bc 2003-02-23 alex Rendezvous_UnregisterListeners( );
243 29bd35bc 2003-02-23 alex #endif
244 83194a23 2003-04-25 alex
245 a0c032b2 2002-11-22 alex Log( LOG_INFO, "Shutting down all listening sockets ..." );
246 a0c032b2 2002-11-22 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
247 a0c032b2 2002-11-22 alex {
248 a0c032b2 2002-11-22 alex if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
249 a0c032b2 2002-11-22 alex {
250 a0c032b2 2002-11-22 alex close( i );
251 7b6e2662 2003-11-05 alex #ifdef DEBUG
252 a0c032b2 2002-11-22 alex Log( LOG_DEBUG, "Listening socket %d closed.", i );
253 7b6e2662 2003-11-05 alex #endif
254 a0c032b2 2002-11-22 alex }
255 a0c032b2 2002-11-22 alex }
256 a0c032b2 2002-11-22 alex } /* Conn_ExitListeners */
257 a0c032b2 2002-11-22 alex
258 a0c032b2 2002-11-22 alex
259 8adff592 2005-03-19 fw GLOBAL bool
260 8adff592 2005-03-19 fw Conn_NewListener( const UINT16 Port )
261 5fefe1a3 2001-12-12 alex {
262 e68cdf30 2002-12-30 alex /* Create new listening socket on specified port */
263 5fefe1a3 2001-12-12 alex
264 1c8eb478 2001-12-12 alex struct sockaddr_in addr;
265 e33ab903 2003-09-11 alex struct in_addr inaddr;
266 8adff592 2005-03-19 fw int sock;
267 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
268 8adff592 2005-03-19 fw char name[CLIENT_ID_LEN], *info;
269 29bd35bc 2003-02-23 alex #endif
270 83194a23 2003-04-25 alex
271 5fefe1a3 2001-12-12 alex /* Server-"Listen"-Socket initialisieren */
272 1c8eb478 2001-12-12 alex memset( &addr, 0, sizeof( addr ));
273 e33ab903 2003-09-11 alex memset( &inaddr, 0, sizeof( inaddr ));
274 1c8eb478 2001-12-12 alex addr.sin_family = AF_INET;
275 1c8eb478 2001-12-12 alex addr.sin_port = htons( Port );
276 e33ab903 2003-09-11 alex if( Conf_ListenAddress[0] )
277 e33ab903 2003-09-11 alex {
278 e33ab903 2003-09-11 alex #ifdef HAVE_INET_ATON
279 e33ab903 2003-09-11 alex if( inet_aton( Conf_ListenAddress, &inaddr ) == 0 )
280 e33ab903 2003-09-11 alex #else
281 e33ab903 2003-09-11 alex inaddr.s_addr = inet_addr( Conf_ListenAddress );
282 e33ab903 2003-09-11 alex if( inaddr.s_addr == (unsigned)-1 )
283 e33ab903 2003-09-11 alex #endif
284 e33ab903 2003-09-11 alex {
285 e33ab903 2003-09-11 alex Log( LOG_CRIT, "Can't listen on %s:%u: can't convert ip address %s!", Conf_ListenAddress, Port, Conf_ListenAddress );
286 8adff592 2005-03-19 fw return false;
287 e33ab903 2003-09-11 alex }
288 e33ab903 2003-09-11 alex }
289 e33ab903 2003-09-11 alex else inaddr.s_addr = htonl( INADDR_ANY );
290 e33ab903 2003-09-11 alex addr.sin_addr = inaddr;
291 5fefe1a3 2001-12-12 alex
292 5fefe1a3 2001-12-12 alex /* Socket erzeugen */
293 1c8eb478 2001-12-12 alex sock = socket( PF_INET, SOCK_STREAM, 0);
294 fb55c443 2001-12-13 alex if( sock < 0 )
295 5fefe1a3 2001-12-12 alex {
296 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
297 8adff592 2005-03-19 fw return false;
298 5fefe1a3 2001-12-12 alex }
299 5fefe1a3 2001-12-12 alex
300 8adff592 2005-03-19 fw if( ! Init_Socket( sock )) return false;
301 1c8eb478 2001-12-12 alex
302 5fefe1a3 2001-12-12 alex /* an Port binden */
303 1c8eb478 2001-12-12 alex if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
304 5fefe1a3 2001-12-12 alex {
305 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
306 1c8eb478 2001-12-12 alex close( sock );
307 8adff592 2005-03-19 fw return false;
308 5fefe1a3 2001-12-12 alex }
309 5fefe1a3 2001-12-12 alex
310 5fefe1a3 2001-12-12 alex /* in "listen mode" gehen :-) */
311 1c8eb478 2001-12-12 alex if( listen( sock, 10 ) != 0 )
312 5fefe1a3 2001-12-12 alex {
313 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
314 1c8eb478 2001-12-12 alex close( sock );
315 8adff592 2005-03-19 fw return false;
316 5fefe1a3 2001-12-12 alex }
317 1c8eb478 2001-12-12 alex
318 1c8eb478 2001-12-12 alex /* Neuen Listener in Strukturen einfuegen */
319 4a111033 2001-12-29 alex FD_SET( sock, &My_Listeners );
320 1c8eb478 2001-12-12 alex FD_SET( sock, &My_Sockets );
321 9ab186c4 2001-12-25 alex
322 c2f60abe 2002-05-27 alex if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
323 1c8eb478 2001-12-12 alex
324 e33ab903 2003-09-11 alex if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock );
325 e33ab903 2003-09-11 alex else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock );
326 29bd35bc 2003-02-23 alex
327 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
328 29bd35bc 2003-02-23 alex /* Get best server description text */
329 29bd35bc 2003-02-23 alex if( ! Conf_ServerInfo[0] ) info = Conf_ServerName;
330 29bd35bc 2003-02-23 alex else
331 29bd35bc 2003-02-23 alex {
332 29bd35bc 2003-02-23 alex /* Use server info string */
333 29bd35bc 2003-02-23 alex info = NULL;
334 29bd35bc 2003-02-23 alex if( Conf_ServerInfo[0] == '[' )
335 29bd35bc 2003-02-23 alex {
336 29bd35bc 2003-02-23 alex /* Cut off leading hostname part in "[]" */
337 29bd35bc 2003-02-23 alex info = strchr( Conf_ServerInfo, ']' );
338 29bd35bc 2003-02-23 alex if( info )
339 29bd35bc 2003-02-23 alex {
340 29bd35bc 2003-02-23 alex info++;
341 29bd35bc 2003-02-23 alex while( *info == ' ' ) info++;
342 29bd35bc 2003-02-23 alex }
343 29bd35bc 2003-02-23 alex }
344 29bd35bc 2003-02-23 alex if( ! info ) info = Conf_ServerInfo;
345 29bd35bc 2003-02-23 alex }
346 1c8eb478 2001-12-12 alex
347 29bd35bc 2003-02-23 alex /* Add port number to description if non-standard */
348 29bd35bc 2003-02-23 alex if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port );
349 29bd35bc 2003-02-23 alex else strlcpy( name, info, sizeof( name ));
350 29bd35bc 2003-02-23 alex
351 29bd35bc 2003-02-23 alex /* Register service */
352 29bd35bc 2003-02-23 alex Rendezvous_Register( name, RENDEZVOUS_TYPE, Port );
353 29bd35bc 2003-02-23 alex #endif
354 29bd35bc 2003-02-23 alex
355 8adff592 2005-03-19 fw return true;
356 08cf5607 2001-12-26 alex } /* Conn_NewListener */
357 5fefe1a3 2001-12-12 alex
358 5fefe1a3 2001-12-12 alex
359 8adff592 2005-03-19 fw GLOBAL void
360 8adff592 2005-03-19 fw Conn_Handler( void )
361 5fefe1a3 2001-12-12 alex {
362 6c19b0e4 2002-09-07 alex /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
363 6c19b0e4 2002-09-07 alex * werden dabei durchgefuehrt, bis der Server terminieren oder neu
364 6c19b0e4 2002-09-07 alex * starten soll:
365 6c19b0e4 2002-09-07 alex *
366 bc140df8 2001-12-29 alex * - neue Verbindungen annehmen,
367 03d971d9 2002-01-02 alex * - Server-Verbindungen aufbauen,
368 bc140df8 2001-12-29 alex * - geschlossene Verbindungen loeschen,
369 bc140df8 2001-12-29 alex * - volle Schreibpuffer versuchen zu schreiben,
370 bc140df8 2001-12-29 alex * - volle Lesepuffer versuchen zu verarbeiten,
371 bc140df8 2001-12-29 alex * - Antworten von Resolver Sub-Prozessen annehmen.
372 bc140df8 2001-12-29 alex */
373 bc140df8 2001-12-29 alex
374 63758dd7 2001-12-15 alex fd_set read_sockets, write_sockets;
375 747fd2f0 2001-12-13 alex struct timeval tv;
376 7b6cfc17 2002-08-26 alex time_t start, t;
377 e68cdf30 2002-12-30 alex CONN_ID i, idx;
378 8adff592 2005-03-19 fw bool timeout;
379 747fd2f0 2001-12-13 alex
380 9ab186c4 2001-12-25 alex start = time( NULL );
381 397c5e2e 2002-12-19 alex while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart ))
382 63758dd7 2001-12-15 alex {
383 8adff592 2005-03-19 fw timeout = true;
384 29bd35bc 2003-02-23 alex
385 29bd35bc 2003-02-23 alex #ifdef RENDEZVOUS
386 29bd35bc 2003-02-23 alex Rendezvous_Handler( );
387 29bd35bc 2003-02-23 alex #endif
388 397c5e2e 2002-12-19 alex
389 397c5e2e 2002-12-19 alex /* Should the configuration be reloaded? */
390 397c5e2e 2002-12-19 alex if( NGIRCd_SignalRehash ) NGIRCd_Rehash( );
391 03d971d9 2002-01-02 alex
392 397c5e2e 2002-12-19 alex /* Check configured servers and established links */
393 397c5e2e 2002-12-19 alex Check_Servers( );
394 65bdfdf2 2001-12-26 alex Check_Connections( );
395 65bdfdf2 2001-12-26 alex
396 7b6e2662 2003-11-05 alex t = time( NULL );
397 7b6e2662 2003-11-05 alex
398 65bdfdf2 2001-12-26 alex /* noch volle Lese-Buffer suchen */
399 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
400 63758dd7 2001-12-15 alex {
401 7b6e2662 2003-11-05 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ) &&
402 7b6e2662 2003-11-05 alex ( My_Connections[i].delaytime < t ))
403 9ab186c4 2001-12-25 alex {
404 65bdfdf2 2001-12-26 alex /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
405 8adff592 2005-03-19 fw if( Handle_Buffer( i )) timeout = false;
406 9ab186c4 2001-12-25 alex }
407 63758dd7 2001-12-15 alex }
408 590f2a3f 2002-03-11 alex
409 65bdfdf2 2001-12-26 alex /* noch volle Schreib-Puffer suchen */
410 65bdfdf2 2001-12-26 alex FD_ZERO( &write_sockets );
411 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
412 9ab186c4 2001-12-25 alex {
413 c40592d2 2003-12-26 alex #ifdef ZLIB
414 65f3adca 2002-11-26 alex if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
415 65f3adca 2002-11-26 alex #else
416 03d971d9 2002-01-02 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
417 65f3adca 2002-11-26 alex #endif
418 9ab186c4 2001-12-25 alex {
419 65bdfdf2 2001-12-26 alex /* Socket der Verbindung in Set aufnehmen */
420 65bdfdf2 2001-12-26 alex FD_SET( My_Connections[i].sock, &write_sockets );
421 9ab186c4 2001-12-25 alex }
422 6da91c34 2002-03-02 alex }
423 e68cdf30 2002-12-30 alex
424 6da91c34 2002-03-02 alex /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
425 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
426 6da91c34 2002-03-02 alex {
427 e7c2c86a 2005-04-23 fw if ( My_Connections[i].sock > NONE ) {
428 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING )) {
429 e7c2c86a 2005-04-23 fw FD_SET( My_Connections[i].sock, &write_sockets );
430 e7c2c86a 2005-04-23 fw }
431 e7c2c86a 2005-04-23 fw }
432 e7c2c86a 2005-04-23 fw
433 9ab186c4 2001-12-25 alex }
434 4a111033 2001-12-29 alex
435 4a111033 2001-12-29 alex /* von welchen Sockets koennte gelesen werden? */
436 9ab186c4 2001-12-25 alex read_sockets = My_Sockets;
437 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
438 4a111033 2001-12-29 alex {
439 e7c2c86a 2005-04-23 fw if ( My_Connections[i].sock > NONE ) {
440 8ac701d6 2005-06-01 fw if ( My_Connections[i].res_stat ) {
441 e7c2c86a 2005-04-23 fw /* wait for completion of Resolver Sub-Process */
442 e7c2c86a 2005-04-23 fw FD_CLR( My_Connections[i].sock, &read_sockets );
443 e7c2c86a 2005-04-23 fw continue;
444 e7c2c86a 2005-04-23 fw }
445 e7c2c86a 2005-04-23 fw
446 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING )) {
447 e7c2c86a 2005-04-23 fw /* wait for completion of connect() */
448 e7c2c86a 2005-04-23 fw FD_CLR( My_Connections[i].sock, &read_sockets );
449 e7c2c86a 2005-04-23 fw continue;
450 e7c2c86a 2005-04-23 fw }
451 4a111033 2001-12-29 alex }
452 7b6cfc17 2002-08-26 alex if( My_Connections[i].delaytime > t )
453 7b6cfc17 2002-08-26 alex {
454 7b6cfc17 2002-08-26 alex /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
455 7b6cfc17 2002-08-26 alex FD_CLR( My_Connections[i].sock, &read_sockets );
456 7b6cfc17 2002-08-26 alex }
457 4a111033 2001-12-29 alex }
458 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
459 4a111033 2001-12-29 alex {
460 4a111033 2001-12-29 alex /* Pipes von Resolver Sub-Prozessen aufnehmen */
461 c2f60abe 2002-05-27 alex if( FD_ISSET( i, &Resolver_FDs ))
462 4a111033 2001-12-29 alex {
463 4a111033 2001-12-29 alex FD_SET( i, &read_sockets );
464 4a111033 2001-12-29 alex }
465 4a111033 2001-12-29 alex }
466 4a111033 2001-12-29 alex
467 6c19b0e4 2002-09-07 alex /* Timeout initialisieren */
468 5ecb00ed 2002-11-23 alex tv.tv_usec = 0;
469 7b6e2662 2003-11-05 alex if( timeout ) tv.tv_sec = 1;
470 5ecb00ed 2002-11-23 alex else tv.tv_sec = 0;
471 83194a23 2003-04-25 alex
472 4a111033 2001-12-29 alex /* Auf Aktivitaet warten */
473 8a927a1b 2002-09-07 alex i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
474 8a927a1b 2002-09-07 alex if( i == 0 )
475 8a927a1b 2002-09-07 alex {
476 8a927a1b 2002-09-07 alex /* keine Veraenderung an den Sockets */
477 8a927a1b 2002-09-07 alex continue;
478 8a927a1b 2002-09-07 alex }
479 8a927a1b 2002-09-07 alex if( i == -1 )
480 9ab186c4 2001-12-25 alex {
481 8a927a1b 2002-09-07 alex /* Fehler (z.B. Interrupt) */
482 db58d347 2002-01-05 alex if( errno != EINTR )
483 db58d347 2002-01-05 alex {
484 b840be98 2002-10-15 alex Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
485 62796722 2003-03-31 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
486 db58d347 2002-01-05 alex exit( 1 );
487 db58d347 2002-01-05 alex }
488 8a927a1b 2002-09-07 alex continue;
489 9ab186c4 2001-12-25 alex }
490 9ab186c4 2001-12-25 alex
491 9ab186c4 2001-12-25 alex /* Koennen Daten geschrieben werden? */
492 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
493 9ab186c4 2001-12-25 alex {
494 3cf845fb 2002-10-14 alex if( ! FD_ISSET( i, &write_sockets )) continue;
495 b840be98 2002-10-15 alex
496 3cf845fb 2002-10-14 alex /* Es kann geschrieben werden ... */
497 3cf845fb 2002-10-14 alex idx = Socket2Index( i );
498 b840be98 2002-10-15 alex if( idx == NONE ) continue;
499 83194a23 2003-04-25 alex
500 3cf845fb 2002-10-14 alex if( ! Handle_Write( idx ))
501 3cf845fb 2002-10-14 alex {
502 3cf845fb 2002-10-14 alex /* Fehler beim Schreiben! Diesen Socket nun
503 3cf845fb 2002-10-14 alex * auch aus dem Read-Set entfernen: */
504 3cf845fb 2002-10-14 alex FD_CLR( i, &read_sockets );
505 3cf845fb 2002-10-14 alex }
506 9ab186c4 2001-12-25 alex }
507 9ab186c4 2001-12-25 alex
508 9ab186c4 2001-12-25 alex /* Daten zum Lesen vorhanden? */
509 c2f60abe 2002-05-27 alex for( i = 0; i < Conn_MaxFD + 1; i++ )
510 9ab186c4 2001-12-25 alex {
511 9ab186c4 2001-12-25 alex if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
512 9ab186c4 2001-12-25 alex }
513 63758dd7 2001-12-15 alex }
514 397c5e2e 2002-12-19 alex
515 397c5e2e 2002-12-19 alex if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
516 397c5e2e 2002-12-19 alex else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
517 1c8eb478 2001-12-12 alex } /* Conn_Handler */
518 1c8eb478 2001-12-12 alex
519 1c8eb478 2001-12-12 alex
520 f7327524 2002-05-30 alex #ifdef PROTOTYPES
521 8adff592 2005-03-19 fw GLOBAL bool
522 8adff592 2005-03-19 fw Conn_WriteStr( CONN_ID Idx, char *Format, ... )
523 f7327524 2002-05-30 alex #else
524 8adff592 2005-03-19 fw GLOBAL bool
525 f7327524 2002-05-30 alex Conn_WriteStr( Idx, Format, va_alist )
526 f7327524 2002-05-30 alex CONN_ID Idx;
527 8adff592 2005-03-19 fw char *Format;
528 f7327524 2002-05-30 alex va_dcl
529 f7327524 2002-05-30 alex #endif
530 63758dd7 2001-12-15 alex {
531 63758dd7 2001-12-15 alex /* String in Socket schreiben. CR+LF wird von dieser Funktion
532 d5c97f81 2001-12-23 alex * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
533 8adff592 2005-03-19 fw * getrennt und false geliefert. */
534 9ab186c4 2001-12-25 alex
535 8adff592 2005-03-19 fw char buffer[COMMAND_LEN];
536 8adff592 2005-03-19 fw bool ok;
537 d5c97f81 2001-12-23 alex va_list ap;
538 d5c97f81 2001-12-23 alex
539 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
540 28c5a21f 2002-03-14 alex assert( Format != NULL );
541 f7327524 2002-05-30 alex
542 f7327524 2002-05-30 alex #ifdef PROTOTYPES
543 d5c97f81 2001-12-23 alex va_start( ap, Format );
544 f7327524 2002-05-30 alex #else
545 f7327524 2002-05-30 alex va_start( ap );
546 f7327524 2002-05-30 alex #endif
547 a40a026d 2005-01-19 alex if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) >= COMMAND_LEN - 2 )
548 63758dd7 2001-12-15 alex {
549 79809118 2002-01-06 alex Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
550 8adff592 2005-03-19 fw Conn_Close( Idx, "Text too long to send!", NULL, false );
551 8adff592 2005-03-19 fw return false;
552 63758dd7 2001-12-15 alex }
553 d5c97f81 2001-12-23 alex
554 7c91951d 2001-12-25 alex #ifdef SNIFFER
555 d79a7d28 2002-01-18 alex if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
556 d5c97f81 2001-12-23 alex #endif
557 9ab186c4 2001-12-25 alex
558 6626395c 2002-12-26 alex strlcat( buffer, "\r\n", sizeof( buffer ));
559 446df061 2001-12-24 alex ok = Conn_Write( Idx, buffer, strlen( buffer ));
560 39219230 2002-12-02 alex My_Connections[Idx].msg_out++;
561 446df061 2001-12-24 alex
562 d5c97f81 2001-12-23 alex va_end( ap );
563 d5c97f81 2001-12-23 alex return ok;
564 63758dd7 2001-12-15 alex } /* Conn_WriteStr */
565 63758dd7 2001-12-15 alex
566 63758dd7 2001-12-15 alex
567 8adff592 2005-03-19 fw GLOBAL bool
568 8adff592 2005-03-19 fw Conn_Write( CONN_ID Idx, char *Data, int Len )
569 63758dd7 2001-12-15 alex {
570 63758dd7 2001-12-15 alex /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
571 8adff592 2005-03-19 fw * der Client disconnectiert und false geliefert. */
572 9ab186c4 2001-12-25 alex
573 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
574 63758dd7 2001-12-15 alex assert( Data != NULL );
575 63758dd7 2001-12-15 alex assert( Len > 0 );
576 63758dd7 2001-12-15 alex
577 65f3adca 2002-11-26 alex /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
578 65f3adca 2002-11-26 alex * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
579 65f3adca 2002-11-26 alex * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
580 65f3adca 2002-11-26 alex * In diesem Fall wird hier einfach ein Fehler geliefert. */
581 67d79b92 2002-09-26 alex if( My_Connections[Idx].sock <= NONE )
582 c50ecda7 2002-09-26 alex {
583 7b6e2662 2003-11-05 alex #ifdef DEBUG
584 c50ecda7 2002-09-26 alex Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
585 7b6e2662 2003-11-05 alex #endif
586 8adff592 2005-03-19 fw return false;
587 63758dd7 2001-12-15 alex }
588 9ab186c4 2001-12-25 alex
589 65f3adca 2002-11-26 alex /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
590 65f3adca 2002-11-26 alex * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
591 65f3adca 2002-11-26 alex * Socket zu schreiben (u.a. wg. Komprimierung). */
592 63758dd7 2001-12-15 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
593 63758dd7 2001-12-15 alex {
594 65f3adca 2002-11-26 alex /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
595 65f3adca 2002-11-26 alex * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
596 ae98008c 2005-06-04 fw if( ! Handle_Write( Idx )) return false;
597 65f3adca 2002-11-26 alex
598 65f3adca 2002-11-26 alex /* nun neu pruefen: */
599 65f3adca 2002-11-26 alex if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
600 65f3adca 2002-11-26 alex {
601 65f3adca 2002-11-26 alex Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
602 8adff592 2005-03-19 fw Conn_Close( Idx, "Write buffer overflow!", NULL, false );
603 8adff592 2005-03-19 fw return false;
604 65f3adca 2002-11-26 alex }
605 63758dd7 2001-12-15 alex }
606 63758dd7 2001-12-15 alex
607 c40592d2 2003-12-26 alex #ifdef ZLIB
608 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
609 65f3adca 2002-11-26 alex /* Daten komprimieren und in Puffer kopieren */
610 8adff592 2005-03-19 fw if( ! Zip_Buffer( Idx, Data, Len )) return false;
611 63758dd7 2001-12-15 alex }
612 65f3adca 2002-11-26 alex else
613 65f3adca 2002-11-26 alex #endif
614 65f3adca 2002-11-26 alex {
615 65f3adca 2002-11-26 alex /* Daten in Puffer kopieren */
616 65f3adca 2002-11-26 alex memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
617 65f3adca 2002-11-26 alex My_Connections[Idx].wdatalen += Len;
618 65f3adca 2002-11-26 alex My_Connections[Idx].bytes_out += Len;
619 65f3adca 2002-11-26 alex }
620 9ab186c4 2001-12-25 alex
621 a02bf31d 2002-12-18 alex /* Adjust global write counter */
622 a02bf31d 2002-12-18 alex WCounter += Len;
623 a02bf31d 2002-12-18 alex
624 8adff592 2005-03-19 fw return true;
625 63758dd7 2001-12-15 alex } /* Conn_Write */
626 7c91951d 2001-12-25 alex
627 7c91951d 2001-12-25 alex
628 8adff592 2005-03-19 fw GLOBAL void
629 8adff592 2005-03-19 fw Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
630 7c91951d 2001-12-25 alex {
631 e21b9d84 2002-12-27 alex /* Close connection. Open pipes of asyncronous resolver
632 e21b9d84 2002-12-27 alex * sub-processes are closed down. */
633 7c91951d 2001-12-25 alex
634 03d971d9 2002-01-02 alex CLIENT *c;
635 8adff592 2005-03-19 fw char *txt;
636 8adff592 2005-03-19 fw double in_k, out_k;
637 c40592d2 2003-12-26 alex #ifdef ZLIB
638 8adff592 2005-03-19 fw double in_z_k, out_z_k;
639 8adff592 2005-03-19 fw int in_p, out_p;
640 65f3adca 2002-11-26 alex #endif
641 590f2a3f 2002-03-11 alex
642 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
643 f060db5e 2002-10-09 alex
644 cf4ad8d6 2003-02-21 alex /* Is this link already shutting down? */
645 e7c2c86a 2005-04-23 fw if( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ISCLOSING )) {
646 cf4ad8d6 2003-02-21 alex /* Conn_Close() has been called recursively for this link;
647 ae98008c 2005-06-04 fw * probabe reason: Handle_Write() failed -- see below. */
648 58736b39 2004-12-22 alex #ifdef DEBUG
649 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Recursive request to close connection: %d", Idx );
650 58736b39 2004-12-22 alex #endif
651 cf4ad8d6 2003-02-21 alex return;
652 cf4ad8d6 2003-02-21 alex }
653 cf4ad8d6 2003-02-21 alex
654 1d8da4b5 2004-04-25 alex assert( My_Connections[Idx].sock > NONE );
655 1d8da4b5 2004-04-25 alex
656 cf4ad8d6 2003-02-21 alex /* Mark link as "closing" */
657 e7c2c86a 2005-04-23 fw Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCLOSING );
658 33f4e676 2004-02-28 alex
659 33f4e676 2004-02-28 alex if( LogMsg ) txt = LogMsg;
660 33f4e676 2004-02-28 alex else txt = FwdMsg;
661 33f4e676 2004-02-28 alex if( ! txt ) txt = "Reason unknown";
662 cf4ad8d6 2003-02-21 alex
663 33f4e676 2004-02-28 alex Log( LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx, LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ));
664 33f4e676 2004-02-28 alex
665 e21b9d84 2002-12-27 alex /* Search client, if any */
666 f060db5e 2002-10-09 alex c = Client_GetFromConn( Idx );
667 7c91951d 2001-12-25 alex
668 e21b9d84 2002-12-27 alex /* Should the client be informed? */
669 79809118 2002-01-06 alex if( InformClient )
670 03d971d9 2002-01-02 alex {
671 1d8cb34b 2002-11-29 alex #ifndef STRICT_RFC
672 e21b9d84 2002-12-27 alex /* Send statistics to client if registered as user: */
673 f060db5e 2002-10-09 alex if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
674 f060db5e 2002-10-09 alex {
675 8adff592 2005-03-19 fw Conn_WriteStr( Idx, "NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.", Client_ThisServer( ), NOTICE_TXTPREFIX, (double)My_Connections[Idx].bytes_in / 1024, (double)My_Connections[Idx].bytes_out / 1024 );
676 f060db5e 2002-10-09 alex }
677 1d8cb34b 2002-11-29 alex #endif
678 f060db5e 2002-10-09 alex
679 e21b9d84 2002-12-27 alex /* Send ERROR to client (see RFC!) */
680 79809118 2002-01-06 alex if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
681 79809118 2002-01-06 alex else Conn_WriteStr( Idx, "ERROR :Closing connection." );
682 03d971d9 2002-01-02 alex }
683 93a39e87 2002-11-28 alex
684 e21b9d84 2002-12-27 alex /* Try to write out the write buffer */
685 ae98008c 2005-06-04 fw (void)Handle_Write( Idx );
686 7c91951d 2001-12-25 alex
687 e21b9d84 2002-12-27 alex /* Shut down socket */
688 7c91951d 2001-12-25 alex if( close( My_Connections[Idx].sock ) != 0 )
689 7c91951d 2001-12-25 alex {
690 cd0dc862 2005-05-22 alex /* Oops, we can't close the socket!? This is ... ugly! */
691 cd0dc862 2005-05-22 alex Log( LOG_CRIT, "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
692 7c91951d 2001-12-25 alex }
693 e21b9d84 2002-12-27 alex
694 e21b9d84 2002-12-27 alex /* Mark socket as invalid: */
695 3cf845fb 2002-10-14 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
696 3cf845fb 2002-10-14 alex My_Connections[Idx].sock = NONE;
697 7c91951d 2001-12-25 alex
698 e21b9d84 2002-12-27 alex /* If there is still a client, unregister it now */
699 8adff592 2005-03-19 fw if( c ) Client_Destroy( c, LogMsg, FwdMsg, true );
700 7c91951d 2001-12-25 alex
701 e21b9d84 2002-12-27 alex /* Calculate statistics and log information */
702 8adff592 2005-03-19 fw in_k = (double)My_Connections[Idx].bytes_in / 1024;
703 8adff592 2005-03-19 fw out_k = (double)My_Connections[Idx].bytes_out / 1024;
704 c40592d2 2003-12-26 alex #ifdef ZLIB
705 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
706 8adff592 2005-03-19 fw in_z_k = (double)My_Connections[Idx].zip.bytes_in / 1024;
707 8adff592 2005-03-19 fw out_z_k = (double)My_Connections[Idx].zip.bytes_out / 1024;
708 8adff592 2005-03-19 fw in_p = (int)(( in_k * 100 ) / in_z_k );
709 8adff592 2005-03-19 fw out_p = (int)(( out_k * 100 ) / out_z_k );
710 e21b9d84 2002-12-27 alex Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, in_z_k, in_p, out_k, out_z_k, out_p );
711 e21b9d84 2002-12-27 alex }
712 e21b9d84 2002-12-27 alex else
713 e21b9d84 2002-12-27 alex #endif
714 e21b9d84 2002-12-27 alex {
715 e21b9d84 2002-12-27 alex Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, out_k );
716 e21b9d84 2002-12-27 alex }
717 e21b9d84 2002-12-27 alex
718 e21b9d84 2002-12-27 alex /* Is there a resolver sub-process running? */
719 4a111033 2001-12-29 alex if( My_Connections[Idx].res_stat )
720 8ac701d6 2005-06-01 fw FreeRes_stat( &My_Connections[Idx] );
721 03d971d9 2002-01-02 alex
722 e21b9d84 2002-12-27 alex /* Servers: Modify time of next connect attempt? */
723 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
724 03d971d9 2002-01-02 alex
725 c40592d2 2003-12-26 alex #ifdef ZLIB
726 e21b9d84 2002-12-27 alex /* Clean up zlib, if link was compressed */
727 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
728 65f3adca 2002-11-26 alex inflateEnd( &My_Connections[Idx].zip.in );
729 65f3adca 2002-11-26 alex deflateEnd( &My_Connections[Idx].zip.out );
730 65f3adca 2002-11-26 alex }
731 65f3adca 2002-11-26 alex #endif
732 65f3adca 2002-11-26 alex
733 e21b9d84 2002-12-27 alex /* Clean up connection structure (=free it) */
734 3cf845fb 2002-10-14 alex Init_Conn_Struct( Idx );
735 58736b39 2004-12-22 alex
736 58736b39 2004-12-22 alex #ifdef DEBUG
737 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Shutdown of connection %d completed.", Idx );
738 58736b39 2004-12-22 alex #endif
739 7c91951d 2001-12-25 alex } /* Conn_Close */
740 59a0fb8c 2003-03-27 alex
741 59a0fb8c 2003-03-27 alex
742 8adff592 2005-03-19 fw GLOBAL void
743 8adff592 2005-03-19 fw Conn_SyncServerStruct( void )
744 59a0fb8c 2003-03-27 alex {
745 59a0fb8c 2003-03-27 alex /* Synchronize server structures (connection IDs):
746 59a0fb8c 2003-03-27 alex * connections <-> configuration */
747 b991de0f 2002-11-02 alex
748 59a0fb8c 2003-03-27 alex CLIENT *client;
749 59a0fb8c 2003-03-27 alex CONN_ID i;
750 8adff592 2005-03-19 fw int c;
751 b991de0f 2002-11-02 alex
752 59a0fb8c 2003-03-27 alex for( i = 0; i < Pool_Size; i++ )
753 59a0fb8c 2003-03-27 alex {
754 59a0fb8c 2003-03-27 alex /* Established connection? */
755 59a0fb8c 2003-03-27 alex if( My_Connections[i].sock <= NONE ) continue;
756 59a0fb8c 2003-03-27 alex
757 59a0fb8c 2003-03-27 alex /* Server connection? */
758 59a0fb8c 2003-03-27 alex client = Client_GetFromConn( i );
759 59a0fb8c 2003-03-27 alex if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue;
760 59a0fb8c 2003-03-27 alex
761 59a0fb8c 2003-03-27 alex for( c = 0; c < MAX_SERVERS; c++ )
762 59a0fb8c 2003-03-27 alex {
763 59a0fb8c 2003-03-27 alex /* Configured server? */
764 59a0fb8c 2003-03-27 alex if( ! Conf_Server[c].host[0] ) continue;
765 59a0fb8c 2003-03-27 alex
766 59a0fb8c 2003-03-27 alex /* Duplicate? */
767 59a0fb8c 2003-03-27 alex if( strcmp( Conf_Server[c].name, Client_ID( client )) == 0 ) Conf_Server[c].conn_id = i;
768 59a0fb8c 2003-03-27 alex }
769 59a0fb8c 2003-03-27 alex }
770 59a0fb8c 2003-03-27 alex } /* SyncServerStruct */
771 59a0fb8c 2003-03-27 alex
772 63758dd7 2001-12-15 alex
773 8adff592 2005-03-19 fw LOCAL void
774 8adff592 2005-03-19 fw Handle_Read( int Sock )
775 1c8eb478 2001-12-12 alex {
776 bc140df8 2001-12-29 alex /* Aktivitaet auf einem Socket verarbeiten:
777 bc140df8 2001-12-29 alex * - neue Clients annehmen,
778 bc140df8 2001-12-29 alex * - Daten von Clients verarbeiten,
779 bc140df8 2001-12-29 alex * - Resolver-Rueckmeldungen annehmen. */
780 1c8eb478 2001-12-12 alex
781 cf050519 2001-12-14 alex CONN_ID idx;
782 9ab186c4 2001-12-25 alex
783 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
784 590f2a3f 2002-03-11 alex
785 4a111033 2001-12-29 alex if( FD_ISSET( Sock, &My_Listeners ))
786 1c8eb478 2001-12-12 alex {
787 1c8eb478 2001-12-12 alex /* es ist einer unserer Listener-Sockets: es soll
788 1c8eb478 2001-12-12 alex * also eine neue Verbindung aufgebaut werden. */
789 1c8eb478 2001-12-12 alex
790 1c8eb478 2001-12-12 alex New_Connection( Sock );
791 1c8eb478 2001-12-12 alex }
792 c2f60abe 2002-05-27 alex else if( FD_ISSET( Sock, &Resolver_FDs ))
793 4a111033 2001-12-29 alex {
794 4a111033 2001-12-29 alex /* Rueckmeldung von einem Resolver Sub-Prozess */
795 4a111033 2001-12-29 alex
796 4a111033 2001-12-29 alex Read_Resolver_Result( Sock );
797 4a111033 2001-12-29 alex }
798 1c8eb478 2001-12-12 alex else
799 1c8eb478 2001-12-12 alex {
800 1c8eb478 2001-12-12 alex /* Ein Client Socket: entweder ein User oder Server */
801 9ab186c4 2001-12-25 alex
802 1c8eb478 2001-12-12 alex idx = Socket2Index( Sock );
803 3cf845fb 2002-10-14 alex if( idx > NONE ) Read_Request( idx );
804 1c8eb478 2001-12-12 alex }
805 63758dd7 2001-12-15 alex } /* Handle_Read */
806 1c8eb478 2001-12-12 alex
807 1c8eb478 2001-12-12 alex
808 8adff592 2005-03-19 fw LOCAL bool
809 c2f60abe 2002-05-27 alex Handle_Write( CONN_ID Idx )
810 63758dd7 2001-12-15 alex {
811 6da91c34 2002-03-02 alex /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
812 9ab186c4 2001-12-25 alex
813 8adff592 2005-03-19 fw int len, res, err;
814 c3dfd63b 2003-04-21 alex socklen_t sock_len;
815 9eee0c88 2003-01-15 alex CLIENT *c;
816 63758dd7 2001-12-15 alex
817 b840be98 2002-10-15 alex assert( Idx > NONE );
818 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
819 6da91c34 2002-03-02 alex
820 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ISCONNECTING )) {
821 e7c2c86a 2005-04-23 fw /* connect() has finished, check result */
822 6da91c34 2002-03-02 alex
823 e7c2c86a 2005-04-23 fw Conn_OPTION_DEL( &My_Connections[Idx], CONN_ISCONNECTING );
824 6da91c34 2002-03-02 alex
825 6da91c34 2002-03-02 alex /* Ergebnis des connect() ermitteln */
826 c3dfd63b 2003-04-21 alex sock_len = sizeof( err );
827 c3dfd63b 2003-04-21 alex res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
828 c3dfd63b 2003-04-21 alex assert( sock_len == sizeof( err ));
829 6da91c34 2002-03-02 alex
830 6da91c34 2002-03-02 alex /* Fehler aufgetreten? */
831 6da91c34 2002-03-02 alex if(( res != 0 ) || ( err != 0 ))
832 6da91c34 2002-03-02 alex {
833 6da91c34 2002-03-02 alex /* Fehler! */
834 6da91c34 2002-03-02 alex if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
835 a2544e49 2002-12-30 alex else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port, Idx, strerror( err ));
836 6da91c34 2002-03-02 alex
837 9eee0c88 2003-01-15 alex /* Clean up socket, connection and client structures */
838 6da91c34 2002-03-02 alex FD_CLR( My_Connections[Idx].sock, &My_Sockets );
839 9eee0c88 2003-01-15 alex c = Client_GetFromConn( Idx );
840 9eee0c88 2003-01-15 alex if( c ) Client_DestroyNow( c );
841 6da91c34 2002-03-02 alex close( My_Connections[Idx].sock );
842 6da91c34 2002-03-02 alex Init_Conn_Struct( Idx );
843 cbce54e0 2002-03-02 alex
844 cbce54e0 2002-03-02 alex /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
845 a2544e49 2002-12-30 alex Conf_Server[Conf_GetServer( Idx )].lasttry = time( NULL );
846 a2544e49 2002-12-30 alex Conf_UnsetServer( Idx );
847 cbce54e0 2002-03-02 alex
848 8adff592 2005-03-19 fw return false;
849 6da91c34 2002-03-02 alex }
850 33f4e676 2004-02-28 alex Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", Idx, My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port );
851 6da91c34 2002-03-02 alex
852 33f4e676 2004-02-28 alex /* Send PASS and SERVER command to peer */
853 a2544e49 2002-12-30 alex Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[Conf_GetServer( Idx )].pwd_out, NGIRCd_ProtoID );
854 3cf845fb 2002-10-14 alex return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
855 6da91c34 2002-03-02 alex }
856 6da91c34 2002-03-02 alex
857 c40592d2 2003-12-26 alex #ifdef ZLIB
858 ae98008c 2005-06-04 fw if(( My_Connections[Idx].wdatalen <= 0 ) && ( ! My_Connections[Idx].zip.wdatalen ))
859 ae98008c 2005-06-04 fw return true;
860 ae98008c 2005-06-04 fw
861 ae98008c 2005-06-04 fw /* write buffer empty, but not compression buf? -> flush compression buf. */
862 65f3adca 2002-11-26 alex if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
863 ae98008c 2005-06-04 fw #else
864 ae98008c 2005-06-04 fw if( My_Connections[Idx].wdatalen <= 0 )
865 ae98008c 2005-06-04 fw return true;
866 65f3adca 2002-11-26 alex #endif
867 65f3adca 2002-11-26 alex
868 8ac701d6 2005-06-01 fw len = write( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen );
869 ae98008c 2005-06-04 fw if( len < 0 ) {
870 ae98008c 2005-06-04 fw if( errno == EAGAIN || errno == EINTR)
871 ae98008c 2005-06-04 fw return true;
872 c6b9f3a1 2002-11-20 alex
873 ae98008c 2005-06-04 fw Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx,
874 ae98008c 2005-06-04 fw My_Connections[Idx].sock, strerror( errno ));
875 8adff592 2005-03-19 fw Conn_Close( Idx, "Write error!", NULL, false );
876 8adff592 2005-03-19 fw return false;
877 63758dd7 2001-12-15 alex }
878 f060db5e 2002-10-09 alex
879 ae98008c 2005-06-04 fw /* Update buffer len and move any data not yet written to beginning of buf */
880 63758dd7 2001-12-15 alex My_Connections[Idx].wdatalen -= len;
881 63758dd7 2001-12-15 alex memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
882 9ab186c4 2001-12-25 alex
883 8adff592 2005-03-19 fw return true;
884 63758dd7 2001-12-15 alex } /* Handle_Write */
885 63758dd7 2001-12-15 alex
886 63758dd7 2001-12-15 alex
887 8adff592 2005-03-19 fw LOCAL void
888 8adff592 2005-03-19 fw New_Connection( int Sock )
889 1c8eb478 2001-12-12 alex {
890 d5c97f81 2001-12-23 alex /* Neue Client-Verbindung von Listen-Socket annehmen und
891 d5c97f81 2001-12-23 alex * CLIENT-Struktur anlegen. */
892 63758dd7 2001-12-15 alex
893 c40592d2 2003-12-26 alex #ifdef TCPWRAP
894 439c945d 2003-03-07 alex struct request_info req;
895 439c945d 2003-03-07 alex #endif
896 1c8eb478 2001-12-12 alex struct sockaddr_in new_addr;
897 8adff592 2005-03-19 fw int new_sock, new_sock_len;
898 4a111033 2001-12-29 alex RES_STAT *s;
899 cf050519 2001-12-14 alex CONN_ID idx;
900 b2d472fc 2002-05-19 alex CLIENT *c;
901 b991de0f 2002-11-02 alex POINTER *ptr;
902 8adff592 2005-03-19 fw long new_size, cnt;
903 590f2a3f 2002-03-11 alex
904 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
905 5fefe1a3 2001-12-12 alex
906 c6b9f3a1 2002-11-20 alex /* Connection auf Listen-Socket annehmen */
907 1c8eb478 2001-12-12 alex new_sock_len = sizeof( new_addr );
908 fb55c443 2001-12-13 alex new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
909 1c8eb478 2001-12-12 alex if( new_sock < 0 )
910 5fefe1a3 2001-12-12 alex {
911 446df061 2001-12-24 alex Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
912 5fefe1a3 2001-12-12 alex return;
913 5fefe1a3 2001-12-12 alex }
914 83194a23 2003-04-25 alex
915 c40592d2 2003-12-26 alex #ifdef TCPWRAP
916 439c945d 2003-03-07 alex /* Validate socket using TCP Wrappers */
917 62796722 2003-03-31 alex request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL );
918 b90bedbc 2004-02-03 alex fromhost(&req);
919 439c945d 2003-03-07 alex if( ! hosts_access( &req ))
920 439c945d 2003-03-07 alex {
921 439c945d 2003-03-07 alex /* Access denied! */
922 439c945d 2003-03-07 alex Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr ));
923 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection refused" );
924 439c945d 2003-03-07 alex close( new_sock );
925 439c945d 2003-03-07 alex return;
926 439c945d 2003-03-07 alex }
927 439c945d 2003-03-07 alex #endif
928 c6b9f3a1 2002-11-20 alex
929 c6b9f3a1 2002-11-20 alex /* Socket initialisieren */
930 c6b9f3a1 2002-11-20 alex Init_Socket( new_sock );
931 2981fe9e 2003-11-05 alex
932 2981fe9e 2003-11-05 alex /* Check IP-based connection limit */
933 2981fe9e 2003-11-05 alex cnt = Count_Connections( new_addr );
934 2981fe9e 2003-11-05 alex if(( Conf_MaxConnectionsIP > 0 ) && ( cnt >= Conf_MaxConnectionsIP ))
935 2981fe9e 2003-11-05 alex {
936 c79fa28a 2004-10-04 alex /* Access denied, too many connections from this IP address! */
937 c79fa28a 2004-10-04 alex Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP address!", inet_ntoa( new_addr.sin_addr ), cnt);
938 c79fa28a 2004-10-04 alex Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" );
939 2981fe9e 2003-11-05 alex close( new_sock );
940 2981fe9e 2003-11-05 alex return;
941 2981fe9e 2003-11-05 alex }
942 9ab186c4 2001-12-25 alex
943 3cf845fb 2002-10-14 alex /* Freie Connection-Struktur suchen */
944 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
945 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
946 1c8eb478 2001-12-12 alex {
947 b991de0f 2002-11-02 alex new_size = Pool_Size + CONNECTION_POOL;
948 83194a23 2003-04-25 alex
949 b991de0f 2002-11-02 alex /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
950 b991de0f 2002-11-02 alex * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
951 83194a23 2003-04-25 alex
952 b991de0f 2002-11-02 alex if( Conf_MaxConnections > 0 )
953 b991de0f 2002-11-02 alex {
954 b991de0f 2002-11-02 alex /* Es ist ein Limit konfiguriert */
955 b991de0f 2002-11-02 alex if( Pool_Size >= Conf_MaxConnections )
956 b991de0f 2002-11-02 alex {
957 b991de0f 2002-11-02 alex /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
958 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
959 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection limit reached" );
960 b991de0f 2002-11-02 alex close( new_sock );
961 b991de0f 2002-11-02 alex return;
962 b991de0f 2002-11-02 alex }
963 b991de0f 2002-11-02 alex if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
964 b991de0f 2002-11-02 alex }
965 e68cdf30 2002-12-30 alex if( new_size < Pool_Size )
966 e68cdf30 2002-12-30 alex {
967 8304210c 2005-04-16 fw Log( LOG_ALERT, "Can't accept connection: limit (%d) reached -- overflow!", Pool_Size );
968 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Connection limit reached" );
969 e68cdf30 2002-12-30 alex close( new_sock );
970 e68cdf30 2002-12-30 alex return;
971 e68cdf30 2002-12-30 alex }
972 83194a23 2003-04-25 alex
973 cb76d96e 2004-03-11 alex ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
974 b991de0f 2002-11-02 alex if( ! ptr )
975 b991de0f 2002-11-02 alex {
976 429b5f3e 2005-01-17 alex Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
977 429b5f3e 2005-01-17 alex Simple_Message( new_sock, "ERROR: Internal error" );
978 429b5f3e 2005-01-17 alex close( new_sock );
979 429b5f3e 2005-01-17 alex return;
980 b991de0f 2002-11-02 alex }
981 429b5f3e 2005-01-17 alex
982 7b6e2662 2003-11-05 alex #ifdef DEBUG
983 429b5f3e 2005-01-17 alex Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
984 7b6e2662 2003-11-05 alex #endif
985 83194a23 2003-04-25 alex
986 902ad912 2002-12-17 alex /* Adjust pointer to new block */
987 cb76d96e 2004-03-11 alex My_Connections = (CONNECTION *)ptr;
988 83194a23 2003-04-25 alex
989 902ad912 2002-12-17 alex /* Initialize new items */
990 902ad912 2002-12-17 alex for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
991 902ad912 2002-12-17 alex idx = Pool_Size;
992 83194a23 2003-04-25 alex
993 902ad912 2002-12-17 alex /* Adjust new pool size */
994 b991de0f 2002-11-02 alex Pool_Size = new_size;
995 1c8eb478 2001-12-12 alex }
996 9ab186c4 2001-12-25 alex
997 d5c97f81 2001-12-23 alex /* Client-Struktur initialisieren */
998 8adff592 2005-03-19 fw c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false );
999 b2d472fc 2002-05-19 alex if( ! c )
1000 d5c97f81 2001-12-23 alex {
1001 d5c97f81 2001-12-23 alex Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1002 e744936d 2003-03-07 alex Simple_Message( new_sock, "ERROR :Internal error" );
1003 d5c97f81 2001-12-23 alex close( new_sock );
1004 d5c97f81 2001-12-23 alex return;
1005 d5c97f81 2001-12-23 alex }
1006 9ab186c4 2001-12-25 alex
1007 1c8eb478 2001-12-12 alex /* Verbindung registrieren */
1008 4a111033 2001-12-29 alex Init_Conn_Struct( idx );
1009 1c8eb478 2001-12-12 alex My_Connections[idx].sock = new_sock;
1010 1c8eb478 2001-12-12 alex My_Connections[idx].addr = new_addr;
1011 1c8eb478 2001-12-12 alex
1012 1c8eb478 2001-12-12 alex /* Neuen Socket registrieren */
1013 1c8eb478 2001-12-12 alex FD_SET( new_sock, &My_Sockets );
1014 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1015 1c8eb478 2001-12-12 alex
1016 4d4f2d4f 2002-01-04 alex Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
1017 4a111033 2001-12-29 alex
1018 4a111033 2001-12-29 alex /* Hostnamen ermitteln */
1019 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host ));
1020 ffb90f4c 2002-11-22 alex Client_SetHostname( c, My_Connections[idx].host );
1021 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
1022 b79b315d 2003-12-27 alex s = Resolve_Addr( &new_addr, My_Connections[idx].sock );
1023 b79b315d 2003-12-27 alex #else
1024 c2f60abe 2002-05-27 alex s = Resolve_Addr( &new_addr );
1025 b79b315d 2003-12-27 alex #endif
1026 8ac701d6 2005-06-01 fw /* resolver process has been started */
1027 8ac701d6 2005-06-01 fw if( s ) My_Connections[idx].res_stat = s;
1028 83194a23 2003-04-25 alex
1029 7b6cfc17 2002-08-26 alex /* Penalty-Zeit setzen */
1030 736bacde 2002-10-10 alex Conn_SetPenalty( idx, 4 );
1031 1c8eb478 2001-12-12 alex } /* New_Connection */
1032 5fefe1a3 2001-12-12 alex
1033 5fefe1a3 2001-12-12 alex
1034 c2f60abe 2002-05-27 alex LOCAL CONN_ID
1035 8adff592 2005-03-19 fw Socket2Index( int Sock )
1036 1c8eb478 2001-12-12 alex {
1037 63758dd7 2001-12-15 alex /* zum Socket passende Connection suchen */
1038 63758dd7 2001-12-15 alex
1039 cf050519 2001-12-14 alex CONN_ID idx;
1040 9ab186c4 2001-12-25 alex
1041 a4d5ca63 2002-11-05 alex assert( Sock > NONE );
1042 9ab186c4 2001-12-25 alex
1043 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1044 9ab186c4 2001-12-25 alex
1045 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1046 b840be98 2002-10-15 alex {
1047 b840be98 2002-10-15 alex /* die Connection wurde vermutlich (wegen eines
1048 b840be98 2002-10-15 alex * Fehlers) bereits wieder abgebaut ... */
1049 7b6e2662 2003-11-05 alex #ifdef DEBUG
1050 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1051 7b6e2662 2003-11-05 alex #endif
1052 b840be98 2002-10-15 alex return NONE;
1053 b840be98 2002-10-15 alex }
1054 3cf845fb 2002-10-14 alex else return idx;
1055 1c8eb478 2001-12-12 alex } /* Socket2Index */
1056 1c8eb478 2001-12-12 alex
1057 1c8eb478 2001-12-12 alex
1058 8adff592 2005-03-19 fw LOCAL void
1059 c2f60abe 2002-05-27 alex Read_Request( CONN_ID Idx )
1060 1c8eb478 2001-12-12 alex {
1061 747fd2f0 2001-12-13 alex /* Daten von Socket einlesen und entsprechend behandeln.
1062 747fd2f0 2001-12-13 alex * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1063 1c8eb478 2001-12-12 alex
1064 8adff592 2005-03-19 fw int len, bsize;
1065 c40592d2 2003-12-26 alex #ifdef ZLIB
1066 d3a6537f 2002-11-28 alex CLIENT *c;
1067 1d8cb34b 2002-11-29 alex #endif
1068 63758dd7 2001-12-15 alex
1069 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1070 03d971d9 2002-01-02 alex assert( My_Connections[Idx].sock > NONE );
1071 9ab186c4 2001-12-25 alex
1072 d3a6537f 2002-11-28 alex /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
1073 d3a6537f 2002-11-28 alex * ansonsten koennen Daten ggf. nicht umkopiert werden. */
1074 d3a6537f 2002-11-28 alex bsize = READBUFFER_LEN;
1075 c40592d2 2003-12-26 alex #ifdef ZLIB
1076 d3a6537f 2002-11-28 alex c = Client_GetFromConn( Idx );
1077 d3a6537f 2002-11-28 alex if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) && ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN )) bsize = ZREADBUFFER_LEN;
1078 d3a6537f 2002-11-28 alex #endif
1079 d3a6537f 2002-11-28 alex
1080 c40592d2 2003-12-26 alex #ifdef ZLIB
1081 d3a6537f 2002-11-28 alex if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1082 65f3adca 2002-11-26 alex #else
1083 d3a6537f 2002-11-28 alex if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
1084 65f3adca 2002-11-26 alex #endif
1085 54e487d4 2002-01-03 alex {
1086 54e487d4 2002-01-03 alex /* Der Lesepuffer ist voll */
1087 eee27628 2004-10-04 alex Log( LOG_ERR, "Receive buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1088 8adff592 2005-03-19 fw Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
1089 54e487d4 2002-01-03 alex return;
1090 54e487d4 2002-01-03 alex }
1091 54e487d4 2002-01-03 alex
1092 c40592d2 2003-12-26 alex #ifdef ZLIB
1093 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
1094 65f3adca 2002-11-26 alex len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 );
1095 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1096 65f3adca 2002-11-26 alex }
1097 65f3adca 2002-11-26 alex else
1098 65f3adca 2002-11-26 alex #endif
1099 65f3adca 2002-11-26 alex {
1100 d3a6537f 2002-11-28 alex len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, bsize - My_Connections[Idx].rdatalen - 1, 0 );
1101 65f3adca 2002-11-26 alex if( len > 0 ) My_Connections[Idx].rdatalen += len;
1102 65f3adca 2002-11-26 alex }
1103 747fd2f0 2001-12-13 alex
1104 747fd2f0 2001-12-13 alex if( len == 0 )
1105 1c8eb478 2001-12-12 alex {
1106 747fd2f0 2001-12-13 alex /* Socket wurde geschlossen */
1107 6591f17f 2002-12-03 alex Log( LOG_INFO, "%s:%d (%s) is closing the connection ...", My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), inet_ntoa( My_Connections[Idx].addr.sin_addr ));
1108 8adff592 2005-03-19 fw Conn_Close( Idx, "Socket closed!", "Client closed connection", false );
1109 1c8eb478 2001-12-12 alex return;
1110 1c8eb478 2001-12-12 alex }
1111 747fd2f0 2001-12-13 alex
1112 747fd2f0 2001-12-13 alex if( len < 0 )
1113 747fd2f0 2001-12-13 alex {
1114 c6b9f3a1 2002-11-20 alex /* Operation haette Socket "nur" blockiert ... */
1115 c6b9f3a1 2002-11-20 alex if( errno == EAGAIN ) return;
1116 c6b9f3a1 2002-11-20 alex
1117 747fd2f0 2001-12-13 alex /* Fehler beim Lesen */
1118 3cf845fb 2002-10-14 alex Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1119 8adff592 2005-03-19 fw Conn_Close( Idx, "Read error!", "Client closed connection", false );
1120 747fd2f0 2001-12-13 alex return;
1121 747fd2f0 2001-12-13 alex }
1122 cf050519 2001-12-14 alex
1123 f060db5e 2002-10-09 alex /* Connection-Statistik aktualisieren */
1124 f060db5e 2002-10-09 alex My_Connections[Idx].bytes_in += len;
1125 f060db5e 2002-10-09 alex
1126 65bdfdf2 2001-12-26 alex /* Timestamp aktualisieren */
1127 65bdfdf2 2001-12-26 alex My_Connections[Idx].lastdata = time( NULL );
1128 65bdfdf2 2001-12-26 alex
1129 9ab186c4 2001-12-25 alex Handle_Buffer( Idx );
1130 9ab186c4 2001-12-25 alex } /* Read_Request */
1131 9ab186c4 2001-12-25 alex
1132 9ab186c4 2001-12-25 alex
1133 8adff592 2005-03-19 fw LOCAL bool
1134 c2f60abe 2002-05-27 alex Handle_Buffer( CONN_ID Idx )
1135 9ab186c4 2001-12-25 alex {
1136 5ecb00ed 2002-11-23 alex /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1137 8adff592 2005-03-19 fw * Wurde ein Request verarbeitet, so wird true geliefert,
1138 8adff592 2005-03-19 fw * ansonsten false (auch bei Fehlern). */
1139 bc140df8 2001-12-29 alex
1140 12cad28e 2002-10-21 alex #ifndef STRICT_RFC
1141 8adff592 2005-03-19 fw char *ptr1, *ptr2;
1142 12cad28e 2002-10-21 alex #endif
1143 8adff592 2005-03-19 fw char *ptr;
1144 8adff592 2005-03-19 fw int len, delta;
1145 8adff592 2005-03-19 fw bool action, result;
1146 c40592d2 2003-12-26 alex #ifdef ZLIB
1147 8adff592 2005-03-19 fw bool old_z;
1148 d3a6537f 2002-11-28 alex #endif
1149 590f2a3f 2002-03-11 alex
1150 8adff592 2005-03-19 fw result = false;
1151 65f3adca 2002-11-26 alex do
1152 9ab186c4 2001-12-25 alex {
1153 7b6e2662 2003-11-05 alex /* Check penalty */
1154 7b6e2662 2003-11-05 alex if( My_Connections[Idx].delaytime > time( NULL )) return result;
1155 7b6e2662 2003-11-05 alex
1156 c40592d2 2003-12-26 alex #ifdef ZLIB
1157 65f3adca 2002-11-26 alex /* ggf. noch unkomprimiete Daten weiter entpacken */
1158 e7c2c86a 2005-04-23 fw if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP ))
1159 8adff592 2005-03-19 fw if( ! Unzip_Buffer( Idx )) return false;
1160 65f3adca 2002-11-26 alex #endif
1161 83194a23 2003-04-25 alex
1162 65f3adca 2002-11-26 alex if( My_Connections[Idx].rdatalen < 1 ) break;
1163 590f2a3f 2002-03-11 alex
1164 65f3adca 2002-11-26 alex /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1165 65f3adca 2002-11-26 alex * RFC 2812. Haben wir eine? */
1166 65f3adca 2002-11-26 alex My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1167 65f3adca 2002-11-26 alex ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1168 83194a23 2003-04-25 alex
1169 65f3adca 2002-11-26 alex if( ptr ) delta = 2;
1170 65f3adca 2002-11-26 alex #ifndef STRICT_RFC
1171 65f3adca 2002-11-26 alex else
1172 747fd2f0 2001-12-13 alex {
1173 65f3adca 2002-11-26 alex /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1174 65f3adca 2002-11-26 alex * machen soetwas viele Clients, u.a. "mIRC" :-( */
1175 65f3adca 2002-11-26 alex ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1176 65f3adca 2002-11-26 alex ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1177 65f3adca 2002-11-26 alex delta = 1;
1178 65f3adca 2002-11-26 alex if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1179 65f3adca 2002-11-26 alex else if( ptr1 ) ptr = ptr1;
1180 65f3adca 2002-11-26 alex else if( ptr2 ) ptr = ptr2;
1181 747fd2f0 2001-12-13 alex }
1182 65f3adca 2002-11-26 alex #endif
1183 83194a23 2003-04-25 alex
1184 8adff592 2005-03-19 fw action = false;
1185 65f3adca 2002-11-26 alex if( ptr )
1186 65f3adca 2002-11-26 alex {
1187 65f3adca 2002-11-26 alex /* Ende der Anfrage wurde gefunden */
1188 65f3adca 2002-11-26 alex *ptr = '\0';
1189 65f3adca 2002-11-26 alex len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1190 65f3adca 2002-11-26 alex if( len > ( COMMAND_LEN - 1 ))
1191 65f3adca 2002-11-26 alex {
1192 65f3adca 2002-11-26 alex /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1193 65f3adca 2002-11-26 alex * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1194 65f3adca 2002-11-26 alex * empfangen wird, wird der Client disconnectiert. */
1195 65f3adca 2002-11-26 alex Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
1196 8adff592 2005-03-19 fw Conn_Close( Idx, NULL, "Request too long", true );
1197 8adff592 2005-03-19 fw return false;
1198 65f3adca 2002-11-26 alex }
1199 d3a6537f 2002-11-28 alex
1200 c40592d2 2003-12-26 alex #ifdef ZLIB
1201 d3a6537f 2002-11-28 alex /* merken, ob Stream bereits komprimiert wird */
1202 d3a6537f 2002-11-28 alex old_z = My_Connections[Idx].options & CONN_ZIP;
1203 d3a6537f 2002-11-28 alex #endif
1204 d3a6537f 2002-11-28 alex
1205 65f3adca 2002-11-26 alex if( len > delta )
1206 65f3adca 2002-11-26 alex {
1207 65f3adca 2002-11-26 alex /* Es wurde ein Request gelesen */
1208 39219230 2002-12-02 alex My_Connections[Idx].msg_in++;
1209 8adff592 2005-03-19 fw if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return false;
1210 8adff592 2005-03-19 fw else action = true;
1211 65f3adca 2002-11-26 alex }
1212 d3a6537f 2002-11-28 alex
1213 65f3adca 2002-11-26 alex /* Puffer anpassen */
1214 65f3adca 2002-11-26 alex My_Connections[Idx].rdatalen -= len;
1215 65f3adca 2002-11-26 alex memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1216 d3a6537f 2002-11-28 alex
1217 c40592d2 2003-12-26 alex #ifdef ZLIB
1218 d3a6537f 2002-11-28 alex if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1219 d3a6537f 2002-11-28 alex {
1220 d3a6537f 2002-11-28 alex /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1221 d3a6537f 2002-11-28 alex * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1222 d3a6537f 2002-11-28 alex * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1223 58736b39 2004-12-22 alex if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1224 58736b39 2004-12-22 alex {
1225 58736b39 2004-12-22 alex /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1226 58736b39 2004-12-22 alex Log( LOG_ALERT, "Can't move receive buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
1227 8adff592 2005-03-19 fw return false;
1228 d3a6537f 2002-11-28 alex }
1229 58736b39 2004-12-22 alex memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1230 58736b39 2004-12-22 alex My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1231 58736b39 2004-12-22 alex My_Connections[Idx].rdatalen = 0;
1232 58736b39 2004-12-22 alex #ifdef DEBUG
1233 58736b39 2004-12-22 alex Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1234 58736b39 2004-12-22 alex #endif /* DEBUG */
1235 d3a6537f 2002-11-28 alex }
1236 58736b39 2004-12-22 alex #endif /* ZLIB */
1237 65f3adca 2002-11-26 alex }
1238 83194a23 2003-04-25 alex
1239 8adff592 2005-03-19 fw if( action ) result = true;
1240 65f3adca 2002-11-26 alex } while( action );
1241 83194a23 2003-04-25 alex
1242 65f3adca 2002-11-26 alex return result;
1243 9ab186c4 2001-12-25 alex } /* Handle_Buffer */
1244 747fd2f0 2001-12-13 alex
1245 747fd2f0 2001-12-13 alex
1246 8adff592 2005-03-19 fw LOCAL void
1247 8adff592 2005-03-19 fw Check_Connections( void )
1248 65bdfdf2 2001-12-26 alex {
1249 bc140df8 2001-12-29 alex /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1250 bc140df8 2001-12-29 alex * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1251 bc140df8 2001-12-29 alex * auch das nicht "hilft", Client disconnectieren. */
1252 65bdfdf2 2001-12-26 alex
1253 03d971d9 2002-01-02 alex CLIENT *c;
1254 e68cdf30 2002-12-30 alex CONN_ID i;
1255 65bdfdf2 2001-12-26 alex
1256 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1257 65bdfdf2 2001-12-26 alex {
1258 03d971d9 2002-01-02 alex if( My_Connections[i].sock == NONE ) continue;
1259 03d971d9 2002-01-02 alex
1260 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1261 356683ff 2002-01-04 alex if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1262 65bdfdf2 2001-12-26 alex {
1263 03d971d9 2002-01-02 alex /* verbundener User, Server oder Service */
1264 65bdfdf2 2001-12-26 alex if( My_Connections[i].lastping > My_Connections[i].lastdata )
1265 65bdfdf2 2001-12-26 alex {
1266 65bdfdf2 2001-12-26 alex /* es wurde bereits ein PING gesendet */
1267 08cf5607 2001-12-26 alex if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1268 65bdfdf2 2001-12-26 alex {
1269 65bdfdf2 2001-12-26 alex /* Timeout */
1270 7b6e2662 2003-11-05 alex #ifdef DEBUG
1271 e39925af 2002-03-26 alex Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1272 7b6e2662 2003-11-05 alex #endif
1273 8adff592 2005-03-19 fw Conn_Close( i, NULL, "Ping timeout", true );
1274 65bdfdf2 2001-12-26 alex }
1275 65bdfdf2 2001-12-26 alex }
1276 08cf5607 2001-12-26 alex else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1277 65bdfdf2 2001-12-26 alex {
1278 65bdfdf2 2001-12-26 alex /* es muss ein PING gesendet werden */
1279 7b6e2662 2003-11-05 alex #ifdef DEBUG
1280 65bdfdf2 2001-12-26 alex Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1281 7b6e2662 2003-11-05 alex #endif
1282 65bdfdf2 2001-12-26 alex My_Connections[i].lastping = time( NULL );
1283 356683ff 2002-01-04 alex Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1284 65bdfdf2 2001-12-26 alex }
1285 65bdfdf2 2001-12-26 alex }
1286 03d971d9 2002-01-02 alex else
1287 03d971d9 2002-01-02 alex {
1288 03d971d9 2002-01-02 alex /* noch nicht vollstaendig aufgebaute Verbindung */
1289 03d971d9 2002-01-02 alex if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1290 03d971d9 2002-01-02 alex {
1291 03d971d9 2002-01-02 alex /* Timeout */
1292 7b6e2662 2003-11-05 alex #ifdef DEBUG
1293 140d1aa5 2002-02-27 alex Log( LOG_DEBUG, "Connection %d timed out ...", i );
1294 7b6e2662 2003-11-05 alex #endif
1295 8adff592 2005-03-19 fw Conn_Close( i, NULL, "Timeout", false );
1296 03d971d9 2002-01-02 alex }
1297 03d971d9 2002-01-02 alex }
1298 65bdfdf2 2001-12-26 alex }
1299 03d971d9 2002-01-02 alex } /* Check_Connections */
1300 03d971d9 2002-01-02 alex
1301 03d971d9 2002-01-02 alex
1302 8adff592 2005-03-19 fw LOCAL void
1303 8adff592 2005-03-19 fw Check_Servers( void )
1304 03d971d9 2002-01-02 alex {
1305 a2544e49 2002-12-30 alex /* Check if we can establish further server links */
1306 03d971d9 2002-01-02 alex
1307 03d971d9 2002-01-02 alex RES_STAT *s;
1308 a2544e49 2002-12-30 alex CONN_ID idx;
1309 8adff592 2005-03-19 fw int i, n;
1310 26ffbc78 2002-02-19 alex
1311 a2544e49 2002-12-30 alex /* Serach all connections, are there results from the resolver? */
1312 a2544e49 2002-12-30 alex for( idx = 0; idx < Pool_Size; idx++ )
1313 03d971d9 2002-01-02 alex {
1314 a2544e49 2002-12-30 alex if( My_Connections[idx].sock != SERVER_WAIT ) continue;
1315 590f2a3f 2002-03-11 alex
1316 a2544e49 2002-12-30 alex /* IP resolved? */
1317 a2544e49 2002-12-30 alex if( My_Connections[idx].res_stat == NULL ) New_Server( Conf_GetServer( idx ), idx );
1318 a2544e49 2002-12-30 alex }
1319 03d971d9 2002-01-02 alex
1320 a2544e49 2002-12-30 alex /* Check all configured servers */
1321 a2544e49 2002-12-30 alex for( i = 0; i < MAX_SERVERS; i++ )
1322 a2544e49 2002-12-30 alex {
1323 8dadb17f 2002-12-31 alex /* Valid outgoing server which isn't already connected or disabled? */
1324 8dadb17f 2002-12-31 alex if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) || ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED )) continue;
1325 d67d94ea 2002-03-10 alex
1326 a2544e49 2002-12-30 alex /* Is there already a connection in this group? */
1327 a2544e49 2002-12-30 alex if( Conf_Server[i].group > NONE )
1328 a2544e49 2002-12-30 alex {
1329 a2544e49 2002-12-30 alex for( n = 0; n < MAX_SERVERS; n++ )
1330 d67d94ea 2002-03-10 alex {
1331 a2544e49 2002-12-30 alex if( n == i ) continue;
1332 a2544e49 2002-12-30 alex if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
1333 03d971d9 2002-01-02 alex }
1334 a2544e49 2002-12-30 alex if( n < MAX_SERVERS ) continue;
1335 03d971d9 2002-01-02 alex }
1336 590f2a3f 2002-03-11 alex
1337 a2544e49 2002-12-30 alex /* Check last connect attempt? */
1338 03d971d9 2002-01-02 alex if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1339 65bdfdf2 2001-12-26 alex
1340 a2544e49 2002-12-30 alex /* Okay, try to connect now */
1341 03d971d9 2002-01-02 alex Conf_Server[i].lasttry = time( NULL );
1342 65bdfdf2 2001-12-26 alex
1343 a2544e49 2002-12-30 alex /* Search free connection structure */
1344 b991de0f 2002-11-02 alex for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1345 b991de0f 2002-11-02 alex if( idx >= Pool_Size )
1346 03d971d9 2002-01-02 alex {
1347 b991de0f 2002-11-02 alex Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1348 03d971d9 2002-01-02 alex return;
1349 03d971d9 2002-01-02 alex }
1350 7b6e2662 2003-11-05 alex #ifdef DEBUG
1351 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1352 7b6e2662 2003-11-05 alex #endif
1353 03d971d9 2002-01-02 alex
1354 03d971d9 2002-01-02 alex /* Verbindungs-Struktur initialisieren */
1355 03d971d9 2002-01-02 alex Init_Conn_Struct( idx );
1356 03d971d9 2002-01-02 alex My_Connections[idx].sock = SERVER_WAIT;
1357 a2544e49 2002-12-30 alex Conf_Server[i].conn_id = idx;
1358 590f2a3f 2002-03-11 alex
1359 a2544e49 2002-12-30 alex /* Resolve Hostname. If this fails, try to use it as an IP address */
1360 a2544e49 2002-12-30 alex strlcpy( Conf_Server[i].ip, Conf_Server[i].host, sizeof( Conf_Server[i].ip ));
1361 695631b2 2002-12-26 alex strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host ));
1362 c2f60abe 2002-05-27 alex s = Resolve_Name( Conf_Server[i].host );
1363 8ac701d6 2005-06-01 fw
1364 8ac701d6 2005-06-01 fw /* resolver process running? */
1365 8ac701d6 2005-06-01 fw if( s ) My_Connections[idx].res_stat = s;
1366 03d971d9 2002-01-02 alex }
1367 03d971d9 2002-01-02 alex } /* Check_Servers */
1368 03d971d9 2002-01-02 alex
1369 03d971d9 2002-01-02 alex
1370 8adff592 2005-03-19 fw LOCAL void
1371 8adff592 2005-03-19 fw New_Server( int Server, CONN_ID Idx )
1372 03d971d9 2002-01-02 alex {
1373 83194a23 2003-04-25 alex /* Establish new server link */
1374 03d971d9 2002-01-02 alex
1375 03d971d9 2002-01-02 alex struct sockaddr_in new_addr;
1376 03d971d9 2002-01-02 alex struct in_addr inaddr;
1377 8adff592 2005-03-19 fw int res, new_sock;
1378 54e487d4 2002-01-03 alex CLIENT *c;
1379 03d971d9 2002-01-02 alex
1380 a4d5ca63 2002-11-05 alex assert( Server > NONE );
1381 a4d5ca63 2002-11-05 alex assert( Idx > NONE );
1382 03d971d9 2002-01-02 alex
1383 83194a23 2003-04-25 alex /* Did we get a valid IP address? */
1384 03d971d9 2002-01-02 alex if( ! Conf_Server[Server].ip[0] )
1385 03d971d9 2002-01-02 alex {
1386 83194a23 2003-04-25 alex /* No. Free connection structure and abort: */
1387 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1388 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1389 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1390 03d971d9 2002-01-02 alex return;
1391 03d971d9 2002-01-02 alex }
1392 590f2a3f 2002-03-11 alex
1393 94435271 2002-02-19 alex Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Conf_Server[Server].port, Idx );
1394 03d971d9 2002-01-02 alex
1395 31a3bfed 2002-05-19 alex #ifdef HAVE_INET_ATON
1396 03d971d9 2002-01-02 alex if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1397 31a3bfed 2002-05-19 alex #else
1398 31a3bfed 2002-05-19 alex memset( &inaddr, 0, sizeof( inaddr ));
1399 31a3bfed 2002-05-19 alex inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1400 31a3bfed 2002-05-19 alex if( inaddr.s_addr == (unsigned)-1 )
1401 31a3bfed 2002-05-19 alex #endif
1402 03d971d9 2002-01-02 alex {
1403 83194a23 2003-04-25 alex /* Can't convert IP address */
1404 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1405 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1406 03d971d9 2002-01-02 alex Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): can't convert ip address %s!", Conf_Server[Server].host, Idx, Conf_Server[Server].ip );
1407 03d971d9 2002-01-02 alex return;
1408 03d971d9 2002-01-02 alex }
1409 03d971d9 2002-01-02 alex
1410 03d971d9 2002-01-02 alex memset( &new_addr, 0, sizeof( new_addr ));
1411 03d971d9 2002-01-02 alex new_addr.sin_family = AF_INET;
1412 03d971d9 2002-01-02 alex new_addr.sin_addr = inaddr;
1413 03d971d9 2002-01-02 alex new_addr.sin_port = htons( Conf_Server[Server].port );
1414 03d971d9 2002-01-02 alex
1415 03d971d9 2002-01-02 alex new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1416 03d971d9 2002-01-02 alex if ( new_sock < 0 )
1417 03d971d9 2002-01-02 alex {
1418 83194a23 2003-04-25 alex /* Can't create socket */
1419 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1420 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1421 79809118 2002-01-06 alex Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1422 03d971d9 2002-01-02 alex return;
1423 03d971d9 2002-01-02 alex }
1424 6da91c34 2002-03-02 alex
1425 6da91c34 2002-03-02 alex if( ! Init_Socket( new_sock )) return;
1426 6da91c34 2002-03-02 alex
1427 6250dcb1 2002-11-11 alex res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1428 6250dcb1 2002-11-11 alex if(( res != 0 ) && ( errno != EINPROGRESS ))
1429 03d971d9 2002-01-02 alex {
1430 83194a23 2003-04-25 alex /* Can't connect socket */
1431 6250dcb1 2002-11-11 alex Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1432 03d971d9 2002-01-02 alex close( new_sock );
1433 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1434 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1435 03d971d9 2002-01-02 alex return;
1436 03d971d9 2002-01-02 alex }
1437 03d971d9 2002-01-02 alex
1438 03d971d9 2002-01-02 alex /* Client-Struktur initialisieren */
1439 8adff592 2005-03-19 fw c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, false );
1440 54e487d4 2002-01-03 alex if( ! c )
1441 03d971d9 2002-01-02 alex {
1442 83194a23 2003-04-25 alex /* Can't create new client structure */
1443 03d971d9 2002-01-02 alex close( new_sock );
1444 03d971d9 2002-01-02 alex Init_Conn_Struct( Idx );
1445 83194a23 2003-04-25 alex Conf_Server[Server].conn_id = NONE;
1446 03d971d9 2002-01-02 alex Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1447 03d971d9 2002-01-02 alex return;
1448 03d971d9 2002-01-02 alex }
1449 356683ff 2002-01-04 alex Client_SetIntroducer( c, c );
1450 1f975b6e 2002-04-08 alex Client_SetToken( c, TOKEN_OUTBOUND );
1451 6da91c34 2002-03-02 alex
1452 83194a23 2003-04-25 alex /* Register connection */
1453 03d971d9 2002-01-02 alex My_Connections[Idx].sock = new_sock;
1454 03d971d9 2002-01-02 alex My_Connections[Idx].addr = new_addr;
1455 695631b2 2002-12-26 alex strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
1456 03d971d9 2002-01-02 alex
1457 83194a23 2003-04-25 alex /* Register new socket */
1458 03d971d9 2002-01-02 alex FD_SET( new_sock, &My_Sockets );
1459 e7c2c86a 2005-04-23 fw Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
1460 e7c2c86a 2005-04-23 fw
1461 c2f60abe 2002-05-27 alex if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1462 83194a23 2003-04-25 alex
1463 7b6e2662 2003-11-05 alex #ifdef DEBUG
1464 b840be98 2002-10-15 alex Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1465 7b6e2662 2003-11-05 alex #endif
1466 03d971d9 2002-01-02 alex } /* New_Server */
1467 03d971d9 2002-01-02 alex
1468 03d971d9 2002-01-02 alex
1469 8adff592 2005-03-19 fw LOCAL void
1470 e68cdf30 2002-12-30 alex Init_Conn_Struct( CONN_ID Idx )
1471 4a111033 2001-12-29 alex {
1472 bfba1f37 2005-03-20 fw time_t now = time( NULL );
1473 4a111033 2001-12-29 alex /* Connection-Struktur initialisieren */
1474 4a111033 2001-12-29 alex
1475 bfba1f37 2005-03-20 fw memset( &My_Connections[Idx], 0, sizeof ( CONNECTION ));
1476 4a111033 2001-12-29 alex My_Connections[Idx].sock = NONE;
1477 bfba1f37 2005-03-20 fw My_Connections[Idx].lastdata = now;
1478 bfba1f37 2005-03-20 fw My_Connections[Idx].lastprivmsg = now;
1479 4a111033 2001-12-29 alex } /* Init_Conn_Struct */
1480 4a111033 2001-12-29 alex
1481 4a111033 2001-12-29 alex
1482 8adff592 2005-03-19 fw LOCAL bool
1483 8adff592 2005-03-19 fw Init_Socket( int Sock )
1484 6da91c34 2002-03-02 alex {
1485 bb98fd8c 2004-01-25 alex /* Initialize socket (set options) */
1486 6da91c34 2002-03-02 alex
1487 8adff592 2005-03-19 fw int value;
1488 6da91c34 2002-03-02 alex
1489 bb98fd8c 2004-01-25 alex #ifdef O_NONBLOCK /* unknown on A/UX */
1490 6da91c34 2002-03-02 alex if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1491 6da91c34 2002-03-02 alex {
1492 bb98fd8c 2004-01-25 alex Log( LOG_CRIT, "Can't enable non-blocking mode for socket: %s!", strerror( errno ));
1493 6da91c34 2002-03-02 alex close( Sock );
1494 8adff592 2005-03-19 fw return false;
1495 6da91c34 2002-03-02 alex }
1496 239727b4 2002-03-13 alex #endif
1497 bb98fd8c 2004-01-25 alex
1498 bb98fd8c 2004-01-25 alex /* Don't block this port after socket shutdown */
1499 bb98fd8c 2004-01-25 alex value = 1;
1500 bb98fd8c 2004-01-25 alex if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &value, (socklen_t)sizeof( value )) != 0 )
1501 6da91c34 2002-03-02 alex {
1502 bb98fd8c 2004-01-25 alex Log( LOG_ERR, "Can't set socket option SO_REUSEADDR: %s!", strerror( errno ));
1503 bb98fd8c 2004-01-25 alex /* ignore this error */
1504 6da91c34 2002-03-02 alex }
1505 6da91c34 2002-03-02 alex
1506 bb98fd8c 2004-01-25 alex /* Set type of service (TOS) */
1507 bb98fd8c 2004-01-25 alex #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
1508 bb98fd8c 2004-01-25 alex value = IPTOS_LOWDELAY;
1509 bb98fd8c 2004-01-25 alex #ifdef DEBUG
1510 bb98fd8c 2004-01-25 alex Log( LOG_DEBUG, "Setting option IP_TOS on socket %d to IPTOS_LOWDELAY (%d).", Sock, value );
1511 bb98fd8c 2004-01-25 alex #endif
1512 bb98fd8c 2004-01-25 alex if( setsockopt( Sock, SOL_IP, IP_TOS, &value, (socklen_t)sizeof( value )) != 0 )
1513 bb98fd8c 2004-01-25 alex {
1514 bb98fd8c 2004-01-25 alex Log( LOG_ERR, "Can't set socket option IP_TOS: %s!", strerror( errno ));
1515 bb98fd8c 2004-01-25 alex /* ignore this error */
1516 bb98fd8c 2004-01-25 alex }
1517 bb98fd8c 2004-01-25 alex #endif
1518 bb98fd8c 2004-01-25 alex
1519 8adff592 2005-03-19 fw return true;
1520 6da91c34 2002-03-02 alex } /* Init_Socket */
1521 6da91c34 2002-03-02 alex
1522 6da91c34 2002-03-02 alex
1523 8adff592 2005-03-19 fw LOCAL void
1524 8adff592 2005-03-19 fw Read_Resolver_Result( int r_fd )
1525 03d971d9 2002-01-02 alex {
1526 3012c232 2004-05-11 alex /* Read result of resolver sub-process from pipe and update the
1527 3012c232 2004-05-11 alex * apropriate connection/client structure(s): hostname and/or
1528 3012c232 2004-05-11 alex * IDENT user name.*/
1529 4a111033 2001-12-29 alex
1530 4a111033 2001-12-29 alex CLIENT *c;
1531 8adff592 2005-03-19 fw int len, i, n;
1532 3012c232 2004-05-11 alex RES_STAT *s;
1533 8adff592 2005-03-19 fw char *ptr;
1534 4a111033 2001-12-29 alex
1535 b79b315d 2003-12-27 alex /* Search associated connection ... */
1536 b991de0f 2002-11-02 alex for( i = 0; i < Pool_Size; i++ )
1537 4a111033 2001-12-29 alex {
1538 3012c232 2004-05-11 alex if(( My_Connections[i].sock != NONE )
1539 3012c232 2004-05-11 alex && ( My_Connections[i].res_stat != NULL )
1540 3012c232 2004-05-11 alex && ( My_Connections[i].res_stat->pipe[0] == r_fd ))
1541 3012c232 2004-05-11 alex break;
1542 4a111033 2001-12-29 alex }
1543 b991de0f 2002-11-02 alex if( i >= Pool_Size )
1544 4a111033 2001-12-29 alex {
1545 b79b315d 2003-12-27 alex /* Ops, none found? Probably the connection has already
1546 3012c232 2004-05-11 alex * been closed!? We'll ignore that ... */
1547 3012c232 2004-05-11 alex FD_CLR( r_fd, &Resolver_FDs );
1548 4a111033 2001-12-29 alex close( r_fd );
1549 7b6e2662 2003-11-05 alex #ifdef DEBUG
1550 03d971d9 2002-01-02 alex Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1551 7b6e2662 2003-11-05 alex #endif
1552 4a111033 2001-12-29 alex return;
1553 4a111033 2001-12-29 alex }
1554 4a111033 2001-12-29 alex
1555 3012c232 2004-05-11 alex /* Get resolver structure */
1556 3012c232 2004-05-11 alex s = My_Connections[i].res_stat;
1557 3012c232 2004-05-11 alex assert( s != NULL );
1558 3012c232 2004-05-11 alex
1559 3012c232 2004-05-11 alex /* Read result from pipe */
1560 a49a580a 2004-05-30 alex len = read( r_fd, s->buffer + s->bufpos, sizeof( s->buffer ) - s->bufpos - 1 );
1561 3012c232 2004-05-11 alex if( len < 0 )
1562 3012c232 2004-05-11 alex {
1563 3012c232 2004-05-11 alex /* Error! */
1564 3012c232 2004-05-11 alex Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1565 8ac701d6 2005-06-01 fw FreeRes_stat( &My_Connections[i] );
1566 3012c232 2004-05-11 alex return;
1567 3012c232 2004-05-11 alex }
1568 3012c232 2004-05-11 alex s->bufpos += len;
1569 3012c232 2004-05-11 alex s->buffer[s->bufpos] = '\0';
1570 3012c232 2004-05-11 alex
1571 3012c232 2004-05-11 alex /* If the result string is incomplete, return to main loop and
1572 3012c232 2004-05-11 alex * wait until we can read in more bytes. */
1573 c23bbe6b 2004-05-11 alex #ifdef IDENTAUTH
1574 3012c232 2004-05-11 alex try_resolve:
1575 c23bbe6b 2004-05-11 alex #endif
1576 3012c232 2004-05-11 alex ptr = strchr( s->buffer, '\n' );
1577 3012c232 2004-05-11 alex if( ! ptr ) return;
1578 3012c232 2004-05-11 alex *ptr = '\0';
1579 3012c232 2004-05-11 alex
1580 7b6e2662 2003-11-05 alex #ifdef DEBUG
1581 3012c232 2004-05-11 alex Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%d bytes), stage %d.", s->buffer, len, s->stage );
1582 7b6e2662 2003-11-05 alex #endif
1583 83194a23 2003-04-25 alex
1584 3012c232 2004-05-11 alex /* Okay, we got a complete result: this is a host name for outgoing
1585 3012c232 2004-05-11 alex * connections and a host name or IDENT user name (if enabled) for
1586 8ac701d6 2005-06-01 fw * incoming connections.*/
1587 03d971d9 2002-01-02 alex if( My_Connections[i].sock > NONE )
1588 03d971d9 2002-01-02 alex {
1589 8ac701d6 2005-06-01 fw /* Incoming connection. Search client ... */
1590 03d971d9 2002-01-02 alex c = Client_GetFromConn( i );
1591 54e487d4 2002-01-03 alex assert( c != NULL );
1592 33f4e676 2004-02-28 alex
1593 33f4e676 2004-02-28 alex /* Only update client information of unregistered clients */
1594 3012c232 2004-05-11 alex if( Client_Type( c ) == CLIENT_UNKNOWN )
1595 33f4e676 2004-02-28 alex {
1596 8ac701d6 2005-06-01 fw switch(s->stage) {
1597 8ac701d6 2005-06-01 fw case 0: /* host name */
1598 3012c232 2004-05-11 alex strlcpy( My_Connections[i].host, s->buffer, sizeof( My_Connections[i].host ));
1599 3012c232 2004-05-11 alex Client_SetHostname( c, s->buffer );
1600 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
1601 3012c232 2004-05-11 alex /* clean up buffer for IDENT result */
1602 3012c232 2004-05-11 alex len = strlen( s->buffer ) + 1;
1603 8ac701d6 2005-06-01 fw assert(len <= sizeof( s->buffer ));
1604 3012c232 2004-05-11 alex memmove( s->buffer, s->buffer + len, sizeof( s->buffer ) - len );
1605 8ac701d6 2005-06-01 fw assert(len <= s->bufpos );
1606 3012c232 2004-05-11 alex s->bufpos -= len;
1607 b79b315d 2003-12-27 alex
1608 3012c232 2004-05-11 alex /* Don't close pipe and clean up, but
1609 3012c232 2004-05-11 alex * instead wait for IDENT result */
1610 3012c232 2004-05-11 alex s->stage = 1;
1611 3012c232 2004-05-11 alex goto try_resolve;
1612 8ac701d6 2005-06-01 fw
1613 8ac701d6 2005-06-01 fw case 1: /* IDENT user name */
1614 3012c232 2004-05-11 alex if( s->buffer[0] )
1615 3012c232 2004-05-11 alex {
1616 3012c232 2004-05-11 alex Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, s->buffer );
1617 8adff592 2005-03-19 fw Client_SetUser( c, s->buffer, true );
1618 3012c232 2004-05-11 alex }
1619 3012c232 2004-05-11 alex else Log( LOG_INFO, "IDENT lookup for connection %ld: no result.", i );
1620 3012c232 2004-05-11 alex #endif
1621 8ac701d6 2005-06-01 fw break;
1622 8ac701d6 2005-06-01 fw default:
1623 8ac701d6 2005-06-01 fw Log( LOG_ERR, "Resolver: got result for unknown stage %d!?", s->stage );
1624 3012c232 2004-05-11 alex }
1625 b79b315d 2003-12-27 alex }
1626 3012c232 2004-05-11 alex #ifdef DEBUG
1627 3012c232 2004-05-11 alex else Log( LOG_DEBUG, "Resolver: discarding result for already registered connection %d.", i );
1628 b79b315d 2003-12-27 alex #endif
1629 03d971d9 2002-01-02 alex }
1630 03d971d9 2002-01-02 alex else
1631 03d971d9 2002-01-02 alex {
1632 3012c232 2004-05-11 alex /* Outgoing connection (server link): set the IP address
1633 3012c232 2004-05-11 alex * so that we can connect to it in the main loop. */
1634 3012c232 2004-05-11 alex
1635 3012c232 2004-05-11 alex /* Search server ... */
1636 a2544e49 2002-12-30 alex n = Conf_GetServer( i );
1637 33f4e676 2004-02-28 alex assert( n > NONE );
1638 3012c232 2004-05-11 alex
1639 3012c232 2004-05-11 alex strlcpy( Conf_Server[n].ip, s->buffer, sizeof( Conf_Server[n].ip ));
1640 03d971d9 2002-01-02 alex }
1641 736bacde 2002-10-10 alex
1642 3012c232 2004-05-11 alex /* Clean up ... */
1643 8ac701d6 2005-06-01 fw FreeRes_stat( &My_Connections[i] );
1644 3012c232 2004-05-11 alex
1645 b79b315d 2003-12-27 alex /* Reset penalty time */
1646 736bacde 2002-10-10 alex Conn_ResetPenalty( i );
1647 4a111033 2001-12-29 alex } /* Read_Resolver_Result */
1648 65f3adca 2002-11-26 alex
1649 65f3adca 2002-11-26 alex
1650 8adff592 2005-03-19 fw LOCAL void
1651 8adff592 2005-03-19 fw Simple_Message( int Sock, char *Msg )
1652 e744936d 2003-03-07 alex {
1653 1cf8ccd8 2005-06-12 fw char buf[COMMAND_LEN];
1654 e744936d 2003-03-07 alex /* Write "simple" message to socket, without using compression
1655 e744936d 2003-03-07 alex * or even the connection write buffers. Used e.g. for error
1656 e744936d 2003-03-07 alex * messages by New_Connection(). */
1657 e744936d 2003-03-07 alex assert( Sock > NONE );
1658 e744936d 2003-03-07 alex assert( Msg != NULL );
1659 e744936d 2003-03-07 alex
1660 1cf8ccd8 2005-06-12 fw strlcpy( buf, Msg, sizeof buf - 2);
1661 1cf8ccd8 2005-06-12 fw strlcat( buf, "\r\n", sizeof buf);
1662 1cf8ccd8 2005-06-12 fw (void)write( Sock, buf, strlen( buf ) );
1663 e744936d 2003-03-07 alex } /* Simple_Error */
1664 2981fe9e 2003-11-05 alex
1665 2981fe9e 2003-11-05 alex
1666 8adff592 2005-03-19 fw LOCAL int
1667 2981fe9e 2003-11-05 alex Count_Connections( struct sockaddr_in addr_in )
1668 2981fe9e 2003-11-05 alex {
1669 8adff592 2005-03-19 fw int i, cnt;
1670 2981fe9e 2003-11-05 alex
1671 2981fe9e 2003-11-05 alex cnt = 0;
1672 2981fe9e 2003-11-05 alex for( i = 0; i < Pool_Size; i++ )
1673 2981fe9e 2003-11-05 alex {
1674 2981fe9e 2003-11-05 alex if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].addr.sin_addr.s_addr == addr_in.sin_addr.s_addr )) cnt++;
1675 2981fe9e 2003-11-05 alex }
1676 2981fe9e 2003-11-05 alex return cnt;
1677 2981fe9e 2003-11-05 alex } /* Count_Connections */
1678 e744936d 2003-03-07 alex
1679 e744936d 2003-03-07 alex
1680 5fefe1a3 2001-12-12 alex /* -eof- */