Blame


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