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