]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn.c
- der Server verschickt keine "NOTICE AUTH"'s mehr.
[ngircd-alex.git] / src / ngircd / conn.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4  *
5  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: conn.c,v 1.100 2002/11/29 17:36:50 alex Exp $
13  *
14  * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <time.h>
33 #include <netinet/in.h>
34
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
37 #else
38 #define PF_INET AF_INET
39 #endif
40
41 #ifdef HAVE_STDINT_H
42 #include <stdint.h>                     /* u.a. fuer Mac OS X */
43 #endif
44
45 #ifdef USE_ZLIB
46 #include <zlib.h>
47 #endif
48
49 #include "exp.h"
50 #include "conn.h"
51
52 #include "imp.h"
53 #include "ngircd.h"
54 #include "client.h"
55 #include "resolve.h"
56 #include "conf.h"
57 #include "log.h"
58 #include "parse.h"
59 #include "tool.h"
60
61 #include "exp.h"
62
63
64 #define SERVER_WAIT (NONE - 1)
65
66
67 #ifdef USE_ZLIB
68 typedef struct _ZipData
69 {
70         z_stream in;                    /* "Handle" fuer Input-Stream */
71         z_stream out;                   /* "Handle" fuer Output-Stream */
72         CHAR rbuf[READBUFFER_LEN];      /* Lesepuffer */
73         INT rdatalen;                   /* Laenge der Daten im Lesepuffer (komprimiert) */
74         CHAR wbuf[WRITEBUFFER_LEN];     /* Schreibpuffer */
75         INT wdatalen;                   /* Laenge der Daten im Schreibpuffer (unkomprimiert) */
76         LONG bytes_in, bytes_out;       /* Counter fuer Statistik (unkomprimiert!) */
77 } ZIPDATA;
78 #endif
79
80
81 typedef struct _Connection
82 {
83         INT sock;                       /* Socket Handle */
84         struct sockaddr_in addr;        /* Adresse des Client */
85         RES_STAT *res_stat;             /* "Resolver-Status", s.o. */
86         CHAR host[HOST_LEN];            /* Hostname */
87         CHAR rbuf[READBUFFER_LEN];      /* Lesepuffer */
88         INT rdatalen;                   /* Laenge der Daten im Lesepuffer */
89         CHAR wbuf[WRITEBUFFER_LEN];     /* Schreibpuffer */
90         INT wdatalen;                   /* Laenge der Daten im Schreibpuffer */
91         INT our_server;                 /* wenn von uns zu connectender Server: ID */
92         time_t lastdata;                /* Letzte Aktivitaet */
93         time_t lastping;                /* Letzter PING */
94         time_t lastprivmsg;             /* Letzte PRIVMSG */
95         time_t delaytime;               /* Nicht beachten bis ("penalty") */
96         LONG bytes_in, bytes_out;       /* Counter fuer Statistik */
97         INT flag;                       /* "Markierungs-Flag" (vgl. "irc-write"-Modul) */
98         INT options;                    /* Link-Optionen */
99 #ifdef USE_ZLIB
100         ZIPDATA zip;                    /* Kompressionsinformationen */
101 #endif
102 } CONNECTION;
103
104
105 LOCAL VOID Handle_Read PARAMS(( INT sock ));
106 LOCAL BOOLEAN Handle_Write PARAMS(( CONN_ID Idx ));
107 LOCAL VOID New_Connection PARAMS(( INT Sock ));
108 LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
109 LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
110 LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
111 LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
112 LOCAL VOID Check_Connections PARAMS(( VOID ));
113 LOCAL VOID Check_Servers PARAMS(( VOID ));
114 LOCAL VOID Init_Conn_Struct PARAMS(( LONG Idx ));
115 LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock ));
116 LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx ));
117 LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd ));
118
119 #ifdef USE_ZLIB
120 LOCAL BOOLEAN Zip_Buffer PARAMS(( CONN_ID Idx, CHAR *Data, INT Len ));
121 LOCAL BOOLEAN Zip_Flush PARAMS(( CONN_ID Idx ));
122 LOCAL BOOLEAN Unzip_Buffer PARAMS(( CONN_ID Idx ));
123 #endif
124
125
126 LOCAL fd_set My_Listeners;
127 LOCAL fd_set My_Sockets;
128 LOCAL fd_set My_Connects;
129
130 LOCAL CONNECTION *My_Connections;
131 LOCAL LONG Pool_Size;
132
133
134 GLOBAL VOID
135 Conn_Init( VOID )
136 {
137         /* Modul initialisieren: statische Strukturen "ausnullen". */
138
139         CONN_ID i;
140
141         /* Speicher fuer Verbindungs-Pool anfordern */
142         Pool_Size = CONNECTION_POOL;
143         if( Conf_MaxConnections > 0 )
144         {
145                 /* konfiguriertes Limit beachten */
146                 if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
147         }
148         My_Connections = malloc( sizeof( CONNECTION ) * Pool_Size );
149         if( ! My_Connections )
150         {
151                 /* Speicher konnte nicht alloziert werden! */
152                 Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
153                 exit( 1 );
154         }
155         Log( LOG_DEBUG, "Allocted connection pool for %ld items.", Pool_Size );
156
157         /* zu Beginn haben wir keine Verbindungen */
158         FD_ZERO( &My_Listeners );
159         FD_ZERO( &My_Sockets );
160         FD_ZERO( &My_Connects );
161
162         /* Groesster File-Descriptor fuer select() */
163         Conn_MaxFD = 0;
164
165         /* Connection-Struktur initialisieren */
166         for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
167 } /* Conn_Init */
168
169
170 GLOBAL VOID
171 Conn_Exit( VOID )
172 {
173         /* Modul abmelden: alle noch offenen Connections
174          * schliessen und freigeben. */
175
176         CONN_ID idx;
177         INT i;
178
179         /* Sockets schliessen */
180         Log( LOG_DEBUG, "Shutting down all connections ..." );
181         for( i = 0; i < Conn_MaxFD + 1; i++ )
182         {
183                 if( FD_ISSET( i, &My_Sockets ))
184                 {
185                         for( idx = 0; idx < Pool_Size; idx++ )
186                         {
187                                 if( My_Connections[idx].sock == i ) break;
188                         }
189                         if( FD_ISSET( i, &My_Listeners ))
190                         {
191                                 close( i );
192                                 Log( LOG_DEBUG, "Listening socket %d closed.", i );
193                         }
194                         else if( FD_ISSET( i, &My_Connects ))
195                         {
196                                 close( i );
197                                 Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
198                         }
199                         else if( idx < Pool_Size )
200                         {
201                                 if( NGIRCd_Restart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
202                                 else Conn_Close( idx, NULL, "Server going down", TRUE );
203                         }
204                         else
205                         {
206                                 Log( LOG_WARNING, "Closing unknown connection %d ...", i );
207                                 close( i );
208                         }
209                 }
210         }
211         
212         free( My_Connections );
213         My_Connections = NULL;
214         Pool_Size = 0;
215 } /* Conn_Exit */
216
217
218 GLOBAL INT
219 Conn_InitListeners( VOID )
220 {
221         /* Ports, auf denen der Server Verbindungen entgegennehmen
222         * soll, initialisieren */
223
224         INT created, i;
225
226         created = 0;
227         for( i = 0; i < Conf_ListenPorts_Count; i++ )
228         {
229                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
230                 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
231         }
232         return created;
233 } /* Conn_InitListeners */
234
235
236 GLOBAL VOID
237 Conn_ExitListeners( VOID )
238 {
239         /* Alle "Listen-Sockets" schliessen */
240
241         INT i;
242
243         Log( LOG_INFO, "Shutting down all listening sockets ..." );
244         for( i = 0; i < Conn_MaxFD + 1; i++ )
245         {
246                 if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
247                 {
248                         close( i );
249                         Log( LOG_DEBUG, "Listening socket %d closed.", i );
250                 }
251         }
252 } /* Conn_ExitListeners */
253
254
255 GLOBAL BOOLEAN
256 Conn_NewListener( CONST UINT Port )
257 {
258         /* Neuen Listen-Socket erzeugen: der Server wartet dann auf
259          * dem angegebenen Port auf Verbindungen. Kann der Listen-
260          * Socket nicht erteugt werden, so wird NULL geliefert.*/
261
262         struct sockaddr_in addr;
263         INT sock;
264
265         /* Server-"Listen"-Socket initialisieren */
266         memset( &addr, 0, sizeof( addr ));
267         addr.sin_family = AF_INET;
268         addr.sin_port = htons( Port );
269         addr.sin_addr.s_addr = htonl( INADDR_ANY );
270
271         /* Socket erzeugen */
272         sock = socket( PF_INET, SOCK_STREAM, 0);
273         if( sock < 0 )
274         {
275                 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
276                 return FALSE;
277         }
278
279         if( ! Init_Socket( sock )) return FALSE;
280
281         /* an Port binden */
282         if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
283         {
284                 Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
285                 close( sock );
286                 return FALSE;
287         }
288
289         /* in "listen mode" gehen :-) */
290         if( listen( sock, 10 ) != 0 )
291         {
292                 Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
293                 close( sock );
294                 return FALSE;
295         }
296
297         /* Neuen Listener in Strukturen einfuegen */
298         FD_SET( sock, &My_Listeners );
299         FD_SET( sock, &My_Sockets );
300
301         if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
302
303         Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
304
305         return TRUE;
306 } /* Conn_NewListener */
307
308
309 GLOBAL VOID
310 Conn_Handler( VOID )
311 {
312         /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
313          * werden dabei durchgefuehrt, bis der Server terminieren oder neu
314          * starten soll:
315          *
316          *  - neue Verbindungen annehmen,
317          *  - Server-Verbindungen aufbauen,
318          *  - geschlossene Verbindungen loeschen,
319          *  - volle Schreibpuffer versuchen zu schreiben,
320          *  - volle Lesepuffer versuchen zu verarbeiten,
321          *  - Antworten von Resolver Sub-Prozessen annehmen.
322          */
323
324         fd_set read_sockets, write_sockets;
325         struct timeval tv;
326         time_t start, t;
327         LONG i, idx;
328         BOOLEAN timeout;
329
330         start = time( NULL );
331         while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart ))
332         {
333                 timeout = TRUE;
334         
335                 Check_Servers( );
336
337                 Check_Connections( );
338
339                 /* noch volle Lese-Buffer suchen */
340                 for( i = 0; i < Pool_Size; i++ )
341                 {
342                         if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
343                         {
344                                 /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
345                                 if( Handle_Buffer( i )) timeout = FALSE;
346                         }
347                 }
348
349                 /* noch volle Schreib-Puffer suchen */
350                 FD_ZERO( &write_sockets );
351                 for( i = 0; i < Pool_Size; i++ )
352                 {
353 #ifdef USE_ZLIB
354                         if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
355 #else
356                         if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
357 #endif
358                         {
359                                 /* Socket der Verbindung in Set aufnehmen */
360                                 FD_SET( My_Connections[i].sock, &write_sockets );
361                         }
362                 }
363                 /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
364                 for( i = 0; i < Pool_Size; i++ )
365                 {
366                         if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects ))) FD_SET( My_Connections[i].sock, &write_sockets );
367                 }
368
369                 /* von welchen Sockets koennte gelesen werden? */
370                 t = time( NULL );
371                 read_sockets = My_Sockets;
372                 for( i = 0; i < Pool_Size; i++ )
373                 {
374                         if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
375                         {
376                                 /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
377                                 FD_CLR( My_Connections[i].sock, &read_sockets );
378                         }
379                         if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
380                         {
381                                 /* Hier laeuft noch ein asyncrones connect() */
382                                 FD_CLR( My_Connections[i].sock, &read_sockets );
383                         }
384                         if( My_Connections[i].delaytime > t )
385                         {
386                                 /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
387                                 FD_CLR( My_Connections[i].sock, &read_sockets );
388                                 FD_CLR( My_Connections[i].sock, &write_sockets );
389                         }
390                 }
391                 for( i = 0; i < Conn_MaxFD + 1; i++ )
392                 {
393                         /* Pipes von Resolver Sub-Prozessen aufnehmen */
394                         if( FD_ISSET( i, &Resolver_FDs ))
395                         {
396                                 FD_SET( i, &read_sockets );
397                         }
398                 }
399
400                 /* Timeout initialisieren */
401                 tv.tv_usec = 0;
402                 if( timeout ) tv.tv_sec = TIME_RES;
403                 else tv.tv_sec = 0;
404                 
405                 /* Auf Aktivitaet warten */
406                 i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
407                 if( i == 0 )
408                 {
409                         /* keine Veraenderung an den Sockets */
410                         continue;
411                 }
412                 if( i == -1 )
413                 {
414                         /* Fehler (z.B. Interrupt) */
415                         if( errno != EINTR )
416                         {
417                                 Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
418                                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
419                                 exit( 1 );
420                         }
421                         continue;
422                 }
423
424                 /* Koennen Daten geschrieben werden? */
425                 for( i = 0; i < Conn_MaxFD + 1; i++ )
426                 {
427                         if( ! FD_ISSET( i, &write_sockets )) continue;
428
429                         /* Es kann geschrieben werden ... */
430                         idx = Socket2Index( i );
431                         if( idx == NONE ) continue;
432                         
433                         if( ! Handle_Write( idx ))
434                         {
435                                 /* Fehler beim Schreiben! Diesen Socket nun
436                                  * auch aus dem Read-Set entfernen: */
437                                 FD_CLR( i, &read_sockets );
438                         }
439                 }
440
441                 /* Daten zum Lesen vorhanden? */
442                 for( i = 0; i < Conn_MaxFD + 1; i++ )
443                 {
444                         if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
445                 }
446         }
447 } /* Conn_Handler */
448
449
450 #ifdef PROTOTYPES
451 GLOBAL BOOLEAN
452 Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
453 #else
454 GLOBAL BOOLEAN
455 Conn_WriteStr( Idx, Format, va_alist )
456 CONN_ID Idx;
457 CHAR *Format;
458 va_dcl
459 #endif
460 {
461         /* String in Socket schreiben. CR+LF wird von dieser Funktion
462          * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
463          * getrennt und FALSE geliefert. */
464
465         CHAR buffer[COMMAND_LEN];
466         BOOLEAN ok;
467         va_list ap;
468
469         assert( Idx > NONE );
470         assert( Format != NULL );
471
472 #ifdef PROTOTYPES
473         va_start( ap, Format );
474 #else
475         va_start( ap );
476 #endif
477         if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
478         {
479                 Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
480                 Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
481                 return FALSE;
482         }
483
484 #ifdef SNIFFER
485         if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
486 #endif
487
488         strcat( buffer, "\r\n" );
489         ok = Conn_Write( Idx, buffer, strlen( buffer ));
490
491         va_end( ap );
492         return ok;
493 } /* Conn_WriteStr */
494
495
496 GLOBAL BOOLEAN
497 Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
498 {
499         /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
500          * der Client disconnectiert und FALSE geliefert. */
501
502         assert( Idx > NONE );
503         assert( Data != NULL );
504         assert( Len > 0 );
505
506         /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
507          * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
508          * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
509          * In diesem Fall wird hier einfach ein Fehler geliefert. */
510         if( My_Connections[Idx].sock <= NONE )
511         {
512                 Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
513                 return FALSE;
514         }
515
516         /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
517          * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
518          * Socket zu schreiben (u.a. wg. Komprimierung). */
519         if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
520         {
521                 /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
522                  * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
523                 if( ! Try_Write( Idx )) return FALSE;
524
525                 /* nun neu pruefen: */
526                 if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
527                 {
528                         Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
529                         Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
530                         return FALSE;
531                 }
532         }
533
534 #ifdef USE_ZLIB
535         if( My_Connections[Idx].options & CONN_ZIP )
536         {
537                 /* Daten komprimieren und in Puffer kopieren */
538                 if( ! Zip_Buffer( Idx, Data, Len )) return FALSE;
539         }
540         else
541 #endif
542         {
543                 /* Daten in Puffer kopieren */
544                 memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
545                 My_Connections[Idx].wdatalen += Len;
546                 My_Connections[Idx].bytes_out += Len;
547         }
548
549         return TRUE;
550 } /* Conn_Write */
551
552
553 GLOBAL VOID
554 Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
555 {
556         /* Verbindung schliessen. Evtl. noch von Resolver
557          * Sub-Prozessen offene Pipes werden geschlossen. */
558
559         CLIENT *c;
560         DOUBLE in_k, out_k;
561 #ifdef USE_ZLIB
562         DOUBLE in_z_k, out_z_k;
563         INT in_p, out_p;
564 #endif
565
566         assert( Idx > NONE );
567         assert( My_Connections[Idx].sock > NONE );
568
569         c = Client_GetFromConn( Idx );
570
571         if( InformClient )
572         {
573 #ifndef STRICT_RFC
574                 /* Statistik an Client melden, wenn User */
575                 if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
576                 {
577                         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 );
578                 }
579 #endif
580
581                 /* ERROR an Client schicken (von RFC so vorgesehen!) */
582                 if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
583                 else Conn_WriteStr( Idx, "ERROR :Closing connection." );
584                 if( My_Connections[Idx].sock == NONE ) return;
585         }
586
587         /* zunaechst versuchen, noch im Schreibpuffer vorhandene
588          * Daten auf den Socket zu schreiben ... */
589         Try_Write( Idx );
590
591         if( close( My_Connections[Idx].sock ) != 0 )
592         {
593                 Log( LOG_ERR, "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 ));
594         }
595         else
596         {
597                 in_k = (DOUBLE)My_Connections[Idx].bytes_in / 1024;
598                 out_k = (DOUBLE)My_Connections[Idx].bytes_out / 1024;
599 #ifdef USE_ZLIB
600                 if( My_Connections[Idx].options & CONN_ZIP )
601                 {
602                         in_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_in / 1024;
603                         out_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_out / 1024;
604                         in_p = (INT)(( in_k * 100 ) / in_z_k );
605                         out_p = (INT)(( out_k * 100 ) / out_z_k );
606                         Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).", Idx, My_Connections[Idx].sock, 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 );
607                 }
608                 else
609 #endif
610                 {
611                         Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, out_k );
612                 }
613         }
614         
615         /* Socket als "ungueltig" markieren */
616         FD_CLR( My_Connections[Idx].sock, &My_Sockets );
617         FD_CLR( My_Connections[Idx].sock, &My_Connects );
618         My_Connections[Idx].sock = NONE;
619
620         if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
621
622         if( My_Connections[Idx].res_stat )
623         {
624                 /* Resolver-Strukturen freigeben, wenn noch nicht geschehen */
625                 FD_CLR( My_Connections[Idx].res_stat->pipe[0], &Resolver_FDs );
626                 close( My_Connections[Idx].res_stat->pipe[0] );
627                 close( My_Connections[Idx].res_stat->pipe[1] );
628                 free( My_Connections[Idx].res_stat );
629         }
630
631         /* Startzeit des naechsten Connect-Versuchs modifizieren? */
632         if(( My_Connections[Idx].our_server > NONE ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry <  time( NULL ) - Conf_ConnectRetry ))
633         {
634                 /* Okay, die Verbindung stand schon "genuegend lange":
635                  * lasttry-Zeitpunkt so setzen, dass der naechste
636                  * Verbindungsversuch in RECONNECT_DELAY Sekunden
637                  * gestartet wird. */
638                 Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
639         }
640
641 #ifdef USE_ZLIB
642         /* Ggf. zlib abmelden */
643         if( Conn_Options( Idx ) & CONN_ZIP )
644         {
645                 inflateEnd( &My_Connections[Idx].zip.in );
646                 deflateEnd( &My_Connections[Idx].zip.out );
647         }
648 #endif
649
650         /* Connection-Struktur loeschen (=freigeben) */
651         Init_Conn_Struct( Idx );
652 } /* Conn_Close */
653
654
655 GLOBAL VOID
656 Conn_UpdateIdle( CONN_ID Idx )
657 {
658         /* Idle-Timer zuruecksetzen */
659
660         assert( Idx > NONE );
661         My_Connections[Idx].lastprivmsg = time( NULL );
662 }
663
664
665 GLOBAL time_t
666 Conn_GetIdle( CONN_ID Idx )
667 {
668         /* Idle-Time einer Verbindung liefern (in Sekunden) */
669
670         assert( Idx > NONE );
671         return time( NULL ) - My_Connections[Idx].lastprivmsg;
672 } /* Conn_GetIdle */
673
674
675 GLOBAL time_t
676 Conn_LastPing( CONN_ID Idx )
677 {
678         /* Zeitpunkt des letzten PING liefern */
679
680         assert( Idx > NONE );
681         return My_Connections[Idx].lastping;
682 } /* Conn_LastPing */
683
684
685 GLOBAL VOID
686 Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
687 {
688         /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
689          * waehrend dieser Zeit wird der entsprechende Socket vom Server
690          * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
691          * dieser Funktion nur erhoeht, nicht aber verringert werden. */
692         
693         time_t t;
694         
695         assert( Idx > NONE );
696         assert( Seconds >= 0 );
697         
698         t = time( NULL ) + Seconds;
699         if( t > My_Connections[Idx].delaytime ) My_Connections[Idx].delaytime = t;
700 } /* Conn_SetPenalty */
701
702
703 GLOBAL VOID
704 Conn_ResetPenalty( CONN_ID Idx )
705 {
706         assert( Idx > NONE );
707         My_Connections[Idx].delaytime = 0;
708 } /* Conn_ResetPenalty */
709
710
711 GLOBAL VOID
712 Conn_ClearFlags( VOID )
713 {
714         /* Alle Connection auf "nicht-markiert" setzen */
715
716         LONG i;
717
718         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
719 } /* Conn_ClearFlags */
720
721
722 GLOBAL INT
723 Conn_Flag( CONN_ID Idx )
724 {
725         /* Ist eine Connection markiert (TRUE) oder nicht? */
726
727         assert( Idx > NONE );
728         return My_Connections[Idx].flag;
729 } /* Conn_Flag */
730
731
732 GLOBAL VOID
733 Conn_SetFlag( CONN_ID Idx, INT Flag )
734 {
735         /* Connection markieren */
736
737         assert( Idx > NONE );
738         My_Connections[Idx].flag = Flag;
739 } /* Conn_SetFlag */
740
741
742 GLOBAL CONN_ID
743 Conn_First( VOID )
744 {
745         /* Connection-Struktur der ersten Verbindung liefern;
746          * Ist keine Verbindung vorhanden, wird NONE geliefert. */
747
748         LONG i;
749         
750         for( i = 0; i < Pool_Size; i++ )
751         {
752                 if( My_Connections[i].sock != NONE ) return i;
753         }
754         return NONE;
755 } /* Conn_First */
756
757
758 GLOBAL CONN_ID
759 Conn_Next( CONN_ID Idx )
760 {
761         /* Naechste Verbindungs-Struktur liefern; existiert keine
762          * weitere, so wird NONE geliefert. */
763
764         LONG i = NONE;
765
766         assert( Idx > NONE );
767         
768         for( i = Idx + 1; i < Pool_Size; i++ )
769         {
770                 if( My_Connections[i].sock != NONE ) return i;
771         }
772         return NONE;
773 } /* Conn_Next */
774
775
776 GLOBAL VOID
777 Conn_SetServer( CONN_ID Idx, INT ConfServer )
778 {
779         /* Connection als Server markieren: Index des konfigurierten
780          * Servers speichern. Verbindung muss bereits bestehen! */
781         
782         assert( Idx > NONE );
783         assert( My_Connections[Idx].sock > NONE );
784         
785         My_Connections[Idx].our_server = ConfServer;
786 } /* Conn_SetServer */
787
788
789 GLOBAL VOID
790 Conn_SetOption( CONN_ID Idx, INT Option )
791 {
792         /* Option fuer Verbindung setzen.
793          * Initial sind alle Optionen _nicht_ gesetzt. */
794
795         assert( Idx > NONE );
796         assert( Option != 0 );
797
798         My_Connections[Idx].options |= Option;
799 } /* Conn_SetOption */
800
801
802 GLOBAL VOID
803 Conn_UnsetOption( CONN_ID Idx, INT Option )
804 {
805         /* Option fuer Verbindung loeschen */
806
807         assert( Idx > NONE );
808         assert( Option != 0 );
809
810         My_Connections[Idx].options &= ~Option;
811 } /* Conn_UnsetOption */
812
813
814 GLOBAL INT
815 Conn_Options( CONN_ID Idx )
816 {
817         assert( Idx > NONE );
818         return My_Connections[Idx].options;
819 } /* Conn_Options */
820
821
822 #ifdef USE_ZLIB
823
824 GLOBAL BOOLEAN
825 Conn_InitZip( CONN_ID Idx )
826 {
827         /* Kompression fuer Link initialisieren */
828
829         assert( Idx > NONE );
830
831         My_Connections[Idx].zip.in.avail_in = 0;
832         My_Connections[Idx].zip.in.total_in = 0;
833         My_Connections[Idx].zip.in.total_out = 0;
834         My_Connections[Idx].zip.in.zalloc = NULL;
835         My_Connections[Idx].zip.in.zfree = NULL;
836         My_Connections[Idx].zip.in.data_type = Z_ASCII;
837
838         if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
839         {
840                 /* Fehler! */
841                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
842                 return FALSE;
843         }
844         
845         My_Connections[Idx].zip.out.total_in = 0;
846         My_Connections[Idx].zip.out.total_in = 0;
847         My_Connections[Idx].zip.out.zalloc = NULL;
848         My_Connections[Idx].zip.out.zfree = NULL;
849         My_Connections[Idx].zip.out.data_type = Z_ASCII;
850
851         if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
852         {
853                 /* Fehler! */
854                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
855                 return FALSE;
856         }
857
858         My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
859         My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
860
861         Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
862         Conn_SetOption( Idx, CONN_ZIP );
863
864         return TRUE;
865 } /* Conn_InitZip */
866
867 #endif
868
869
870 LOCAL BOOLEAN
871 Try_Write( CONN_ID Idx )
872 {
873         /* Versuchen, Daten aus dem Schreib-Puffer in den Socket zu
874          * schreiben. TRUE wird geliefert, wenn entweder keine Daten
875          * zum Versenden vorhanden sind oder erfolgreich bearbeitet
876          * werden konnten. Im Fehlerfall wird FALSE geliefert und
877          * die Verbindung geschlossen. */
878
879         fd_set write_socket;
880         struct timeval tv;
881
882         assert( Idx > NONE );
883         assert( My_Connections[Idx].sock > NONE );
884
885         /* sind ueberhaupt Daten vorhanden? */
886 #ifdef USE_ZLIB
887         if(( ! My_Connections[Idx].wdatalen > 0 ) && ( ! My_Connections[Idx].zip.wdatalen )) return TRUE;
888 #else
889         if( ! My_Connections[Idx].wdatalen > 0 ) return TRUE;
890 #endif
891
892         /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
893         tv.tv_sec = 0; tv.tv_usec = 0;
894
895         FD_ZERO( &write_socket );
896         FD_SET( My_Connections[Idx].sock, &write_socket );
897         if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
898         {
899                 /* Fehler! */
900                 if( errno != EINTR )
901                 {
902                         Log( LOG_ALERT, "Try_Write(): select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
903                         Conn_Close( Idx, "Server error!", NULL, FALSE );
904                         return FALSE;
905                 }
906         }
907
908         if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
909         else return TRUE;
910 } /* Try_Write */
911
912
913 LOCAL VOID
914 Handle_Read( INT Sock )
915 {
916         /* Aktivitaet auf einem Socket verarbeiten:
917          *  - neue Clients annehmen,
918          *  - Daten von Clients verarbeiten,
919          *  - Resolver-Rueckmeldungen annehmen. */
920
921         CONN_ID idx;
922
923         assert( Sock > NONE );
924
925         if( FD_ISSET( Sock, &My_Listeners ))
926         {
927                 /* es ist einer unserer Listener-Sockets: es soll
928                  * also eine neue Verbindung aufgebaut werden. */
929
930                 New_Connection( Sock );
931         }
932         else if( FD_ISSET( Sock, &Resolver_FDs ))
933         {
934                 /* Rueckmeldung von einem Resolver Sub-Prozess */
935
936                 Read_Resolver_Result( Sock );
937         }
938         else
939         {
940                 /* Ein Client Socket: entweder ein User oder Server */
941
942                 idx = Socket2Index( Sock );
943                 if( idx > NONE ) Read_Request( idx );
944         }
945 } /* Handle_Read */
946
947
948 LOCAL BOOLEAN
949 Handle_Write( CONN_ID Idx )
950 {
951         /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
952
953         INT len, res, err;
954
955         assert( Idx > NONE );
956         assert( My_Connections[Idx].sock > NONE );
957
958         if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
959         {
960                 /* es soll nichts geschrieben werden, sondern ein
961                  * connect() hat ein Ergebnis geliefert */
962
963                 FD_CLR( My_Connections[Idx].sock, &My_Connects );
964
965                 /* Ergebnis des connect() ermitteln */
966                 len = sizeof( err );
967                 res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
968                 assert( len == sizeof( err ));
969
970                 /* Fehler aufgetreten? */
971                 if(( res != 0 ) || ( err != 0 ))
972                 {
973                         /* Fehler! */
974                         if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
975                         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 ));
976
977                         /* Socket etc. pp. aufraeumen */
978                         FD_CLR( My_Connections[Idx].sock, &My_Sockets );
979                         close( My_Connections[Idx].sock );
980                         Init_Conn_Struct( Idx );
981
982                         /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
983                         Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
984
985                         return FALSE;
986                 }
987                 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 );
988
989                 /* PASS und SERVER verschicken */
990                 Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd_out, NGIRCd_ProtoID );
991                 return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
992         }
993
994 #ifdef USE_ZLIB
995         /* Schreibpuffer leer, aber noch Daten im Kompressionsbuffer?
996          * Dann muss dieser nun geflushed werden! */
997         if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
998 #endif
999
1000         assert( My_Connections[Idx].wdatalen > 0 );
1001
1002         /* Daten schreiben */
1003         len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
1004         if( len < 0 )
1005         {
1006                 /* Operation haette Socket "nur" blockiert ... */
1007                 if( errno == EAGAIN ) return TRUE;
1008
1009                 /* Oops, ein Fehler! */
1010                 Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1011                 Conn_Close( Idx, "Write error!", NULL, FALSE );
1012                 return FALSE;
1013         }
1014
1015         /* Puffer anpassen */
1016         My_Connections[Idx].wdatalen -= len;
1017         memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
1018
1019         return TRUE;
1020 } /* Handle_Write */
1021
1022
1023 LOCAL VOID
1024 New_Connection( INT Sock )
1025 {
1026         /* Neue Client-Verbindung von Listen-Socket annehmen und
1027          * CLIENT-Struktur anlegen. */
1028
1029         struct sockaddr_in new_addr;
1030         INT new_sock, new_sock_len;
1031         RES_STAT *s;
1032         CONN_ID idx;
1033         CLIENT *c;
1034         POINTER *ptr;
1035         LONG new_size;
1036
1037         assert( Sock > NONE );
1038
1039         /* Connection auf Listen-Socket annehmen */
1040         new_sock_len = sizeof( new_addr );
1041         new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
1042         if( new_sock < 0 )
1043         {
1044                 Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
1045                 return;
1046         }
1047
1048         /* Socket initialisieren */
1049         Init_Socket( new_sock );
1050
1051         /* Freie Connection-Struktur suchen */
1052         for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1053         if( idx >= Pool_Size )
1054         {
1055                 new_size = Pool_Size + CONNECTION_POOL;
1056                 
1057                 /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
1058                  * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
1059                 
1060                 if( Conf_MaxConnections > 0 )
1061                 {
1062                         /* Es ist ein Limit konfiguriert */
1063                         if( Pool_Size >= Conf_MaxConnections )
1064                         {
1065                                 /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
1066                                 Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
1067                                 close( new_sock );
1068                                 return;
1069                         }
1070                         if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
1071                 }
1072                 
1073                 /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
1074                  * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
1075                  * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
1076                 ptr = realloc( My_Connections, sizeof( CONNECTION ) * new_size );
1077                 if( ! ptr )
1078                 {
1079                         /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
1080                         ptr = malloc( sizeof( CONNECTION ) * new_size );
1081                         if( ! ptr )
1082                         {
1083                                 /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
1084                                 Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
1085                                 close( new_sock );
1086                                 return;
1087                         }
1088                         
1089                         /* Struktur umkopieren ... */
1090                         memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
1091                         
1092                         Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [malloc()/memcpy()]", new_size );
1093                 }
1094                 else Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [realloc()]", new_size );
1095                 
1096                 My_Connections = ptr;
1097                 Pool_Size = new_size;
1098         }
1099
1100         /* Client-Struktur initialisieren */
1101         c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
1102         if( ! c )
1103         {
1104                 Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1105                 close( new_sock );
1106                 return;
1107         }
1108
1109         /* Verbindung registrieren */
1110         Init_Conn_Struct( idx );
1111         My_Connections[idx].sock = new_sock;
1112         My_Connections[idx].addr = new_addr;
1113
1114         /* Neuen Socket registrieren */
1115         FD_SET( new_sock, &My_Sockets );
1116         if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1117
1118         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 );
1119
1120         /* Hostnamen ermitteln */
1121         strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
1122         Client_SetHostname( c, My_Connections[idx].host );
1123         s = Resolve_Addr( &new_addr );
1124         if( s )
1125         {
1126                 /* Sub-Prozess wurde asyncron gestartet */
1127                 My_Connections[idx].res_stat = s;
1128         }
1129         
1130         /* Penalty-Zeit setzen */
1131         Conn_SetPenalty( idx, 4 );
1132 } /* New_Connection */
1133
1134
1135 LOCAL CONN_ID
1136 Socket2Index( INT Sock )
1137 {
1138         /* zum Socket passende Connection suchen */
1139
1140         CONN_ID idx;
1141
1142         assert( Sock > NONE );
1143
1144         for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1145
1146         if( idx >= Pool_Size )
1147         {
1148                 /* die Connection wurde vermutlich (wegen eines
1149                  * Fehlers) bereits wieder abgebaut ... */
1150                 Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1151                 return NONE;
1152         }
1153         else return idx;
1154 } /* Socket2Index */
1155
1156
1157 LOCAL VOID
1158 Read_Request( CONN_ID Idx )
1159 {
1160         /* Daten von Socket einlesen und entsprechend behandeln.
1161          * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1162
1163         INT len, bsize;
1164 #ifdef USE_ZLIB
1165         CLIENT *c;
1166 #endif
1167
1168         assert( Idx > NONE );
1169         assert( My_Connections[Idx].sock > NONE );
1170
1171         /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
1172          * ansonsten koennen Daten ggf. nicht umkopiert werden. */
1173         bsize = READBUFFER_LEN;
1174 #ifdef USE_ZLIB
1175         c = Client_GetFromConn( Idx );
1176         if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) && ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN )) bsize = ZREADBUFFER_LEN;
1177 #endif
1178
1179 #ifdef USE_ZLIB
1180         if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1181 #else
1182         if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
1183 #endif
1184         {
1185                 /* Der Lesepuffer ist voll */
1186                 Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1187                 Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
1188                 return;
1189         }
1190
1191 #ifdef USE_ZLIB
1192         if( My_Connections[Idx].options & CONN_ZIP )
1193         {
1194                 len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 );
1195                 if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1196         }
1197         else
1198 #endif
1199         {
1200                 len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, bsize - My_Connections[Idx].rdatalen - 1, 0 );
1201                 if( len > 0 ) My_Connections[Idx].rdatalen += len;
1202         }
1203
1204         if( len == 0 )
1205         {
1206                 /* Socket wurde geschlossen */
1207                 Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
1208                 Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
1209                 return;
1210         }
1211
1212         if( len < 0 )
1213         {
1214                 /* Operation haette Socket "nur" blockiert ... */
1215                 if( errno == EAGAIN ) return;
1216
1217                 /* Fehler beim Lesen */
1218                 Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1219                 Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
1220                 return;
1221         }
1222
1223         /* Connection-Statistik aktualisieren */
1224         My_Connections[Idx].bytes_in += len;
1225
1226         /* Timestamp aktualisieren */
1227         My_Connections[Idx].lastdata = time( NULL );
1228
1229         Handle_Buffer( Idx );
1230 } /* Read_Request */
1231
1232
1233 LOCAL BOOLEAN
1234 Handle_Buffer( CONN_ID Idx )
1235 {
1236         /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1237          * Wurde ein Request verarbeitet, so wird TRUE geliefert,
1238          * ansonsten FALSE (auch bei Fehlern). */
1239
1240 #ifndef STRICT_RFC
1241         CHAR *ptr1, *ptr2;
1242 #endif
1243         CHAR *ptr;
1244         INT len, delta;
1245         BOOLEAN action, result;
1246 #ifdef USE_ZLIB
1247         BOOLEAN old_z;
1248 #endif
1249
1250         result = FALSE;
1251         do
1252         {
1253 #ifdef USE_ZLIB
1254                 /* ggf. noch unkomprimiete Daten weiter entpacken */
1255                 if( My_Connections[Idx].options & CONN_ZIP )
1256                 {
1257                         if( ! Unzip_Buffer( Idx )) return FALSE;
1258                 }
1259 #endif
1260         
1261                 if( My_Connections[Idx].rdatalen < 1 ) break;
1262
1263                 /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1264                  * RFC 2812. Haben wir eine? */
1265                 My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1266                 ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1267         
1268                 if( ptr ) delta = 2;
1269 #ifndef STRICT_RFC
1270                 else
1271                 {
1272                         /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1273                          * machen soetwas viele Clients, u.a. "mIRC" :-( */
1274                         ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1275                         ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1276                         delta = 1;
1277                         if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1278                         else if( ptr1 ) ptr = ptr1;
1279                         else if( ptr2 ) ptr = ptr2;
1280                 }
1281 #endif
1282         
1283                 action = FALSE;
1284                 if( ptr )
1285                 {
1286                         /* Ende der Anfrage wurde gefunden */
1287                         *ptr = '\0';
1288                         len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1289                         if( len > ( COMMAND_LEN - 1 ))
1290                         {
1291                                 /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1292                                  * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1293                                  * empfangen wird, wird der Client disconnectiert. */
1294                                 Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
1295                                 Conn_Close( Idx, NULL, "Request too long", TRUE );
1296                                 return FALSE;
1297                         }
1298
1299 #ifdef USE_ZLIB
1300                         /* merken, ob Stream bereits komprimiert wird */
1301                         old_z = My_Connections[Idx].options & CONN_ZIP;
1302 #endif
1303
1304                         if( len > delta )
1305                         {
1306                                 /* Es wurde ein Request gelesen */
1307                                 if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
1308                                 else action = TRUE;
1309                         }
1310
1311                         /* Puffer anpassen */
1312                         My_Connections[Idx].rdatalen -= len;
1313                         memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1314
1315 #ifdef USE_ZLIB
1316                         if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1317                         {
1318                                 /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1319                                  * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1320                                  * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1321                                 {
1322                                         if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1323                                         {
1324                                                 /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1325                                                 Log( LOG_ALERT, "Can't move read buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
1326                                                 return FALSE;
1327                                         }
1328                                         memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1329                                         My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1330                                         My_Connections[Idx].rdatalen = 0;
1331                                         Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1332                                 }
1333                         }
1334 #endif
1335                 }
1336                 
1337                 if( action ) result = TRUE;
1338         } while( action );
1339         
1340         return result;
1341 } /* Handle_Buffer */
1342
1343
1344 LOCAL VOID
1345 Check_Connections( VOID )
1346 {
1347         /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1348          * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1349          * auch das nicht "hilft", Client disconnectieren. */
1350
1351         CLIENT *c;
1352         LONG i;
1353
1354         for( i = 0; i < Pool_Size; i++ )
1355         {
1356                 if( My_Connections[i].sock == NONE ) continue;
1357
1358                 c = Client_GetFromConn( i );
1359                 if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1360                 {
1361                         /* verbundener User, Server oder Service */
1362                         if( My_Connections[i].lastping > My_Connections[i].lastdata )
1363                         {
1364                                 /* es wurde bereits ein PING gesendet */
1365                                 if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1366                                 {
1367                                         /* Timeout */
1368                                         Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1369                                         Conn_Close( i, NULL, "Ping timeout", TRUE );
1370                                 }
1371                         }
1372                         else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1373                         {
1374                                 /* es muss ein PING gesendet werden */
1375                                 Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1376                                 My_Connections[i].lastping = time( NULL );
1377                                 Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1378                         }
1379                 }
1380                 else
1381                 {
1382                         /* noch nicht vollstaendig aufgebaute Verbindung */
1383                         if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1384                         {
1385                                 /* Timeout */
1386                                 Log( LOG_DEBUG, "Connection %d timed out ...", i );
1387                                 Conn_Close( i, NULL, "Timeout", FALSE );
1388                         }
1389                 }
1390         }
1391 } /* Check_Connections */
1392
1393
1394 LOCAL VOID
1395 Check_Servers( VOID )
1396 {
1397         /* Pruefen, ob Server-Verbindungen aufgebaut werden
1398          * muessen bzw. koennen */
1399
1400         RES_STAT *s;
1401         LONG idx, n;
1402         INT i;
1403
1404         /* Wenn "Passive-Mode" aktiv: nicht verbinden */
1405         if( NGIRCd_Passive ) return;
1406
1407         for( i = 0; i < Conf_Server_Count; i++ )
1408         {
1409                 /* Ist ein Hostname und Port definiert? */
1410                 if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue;
1411
1412                 /* Haben wir schon eine Verbindung? */
1413                 for( n = 0; n < Pool_Size; n++ )
1414                 {
1415                         if( My_Connections[n].sock == NONE ) continue;
1416                         
1417                         /* Verbindung zu diesem Server? */
1418                         if( My_Connections[n].our_server == i )
1419                         {
1420                                 /* Komplett aufgebaute Verbindung? */
1421                                 if( My_Connections[n].sock > NONE ) break;
1422
1423                                 /* IP schon aufgeloest? */
1424                                 if( My_Connections[n].res_stat == NULL ) New_Server( i, n );
1425                         }
1426
1427                         /* Verbindung in dieser Server-Gruppe? */
1428                         if(( My_Connections[n].our_server != NONE ) && ( Conf_Server[i].group != NONE ))
1429                         {
1430                                 if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break;
1431                         }
1432                 }
1433                 if( n < Pool_Size ) continue;
1434
1435                 /* Wann war der letzte Connect-Versuch? */
1436                 if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1437
1438                 /* Okay, Verbindungsaufbau versuchen */
1439                 Conf_Server[i].lasttry = time( NULL );
1440
1441                 /* Freie Connection-Struktur suschen */
1442                 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1443                 if( idx >= Pool_Size )
1444                 {
1445                         Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1446                         return;
1447                 }
1448                 Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1449
1450                 /* Verbindungs-Struktur initialisieren */
1451                 Init_Conn_Struct( idx );
1452                 My_Connections[idx].sock = SERVER_WAIT;
1453                 My_Connections[idx].our_server = i;
1454
1455                 /* Hostnamen in IP aufloesen (Default bzw. im Fehlerfall: versuchen, den
1456                  * konfigurierten Text direkt als IP-Adresse zu verwenden ... */
1457                 strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
1458                 strcpy( My_Connections[idx].host, Conf_Server[i].host );
1459                 s = Resolve_Name( Conf_Server[i].host );
1460                 if( s )
1461                 {
1462                         /* Sub-Prozess wurde asyncron gestartet */
1463                         My_Connections[idx].res_stat = s;
1464                 }
1465         }
1466 } /* Check_Servers */
1467
1468
1469 LOCAL VOID
1470 New_Server( INT Server, CONN_ID Idx )
1471 {
1472         /* Neue Server-Verbindung aufbauen */
1473
1474         struct sockaddr_in new_addr;
1475         struct in_addr inaddr;
1476         INT res, new_sock;
1477         CLIENT *c;
1478
1479         assert( Server > NONE );
1480         assert( Idx > NONE );
1481
1482         /* Wurde eine gueltige IP-Adresse gefunden? */
1483         if( ! Conf_Server[Server].ip[0] )
1484         {
1485                 /* Nein. Verbindung wieder freigeben: */
1486                 Init_Conn_Struct( Idx );
1487                 Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1488                 return;
1489         }
1490
1491         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 );
1492
1493 #ifdef HAVE_INET_ATON
1494         if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1495 #else
1496         memset( &inaddr, 0, sizeof( inaddr ));
1497         inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1498         if( inaddr.s_addr == (unsigned)-1 )
1499 #endif
1500         {
1501                 /* Konnte Adresse nicht konvertieren */
1502                 Init_Conn_Struct( Idx );
1503                 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 );
1504                 return;
1505         }
1506
1507         memset( &new_addr, 0, sizeof( new_addr ));
1508         new_addr.sin_family = AF_INET;
1509         new_addr.sin_addr = inaddr;
1510         new_addr.sin_port = htons( Conf_Server[Server].port );
1511
1512         new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1513         if ( new_sock < 0 )
1514         {
1515                 Init_Conn_Struct( Idx );
1516                 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1517                 return;
1518         }
1519
1520         if( ! Init_Socket( new_sock )) return;
1521
1522         res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1523         if(( res != 0 ) && ( errno != EINPROGRESS ))
1524         {
1525                 Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1526                 close( new_sock );
1527                 Init_Conn_Struct( Idx );
1528                 return;
1529         }
1530
1531         /* Client-Struktur initialisieren */
1532         c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1533         if( ! c )
1534         {
1535                 close( new_sock );
1536                 Init_Conn_Struct( Idx );
1537                 Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1538                 return;
1539         }
1540         Client_SetIntroducer( c, c );
1541         Client_SetToken( c, TOKEN_OUTBOUND );
1542
1543         /* Verbindung registrieren */
1544         My_Connections[Idx].sock = new_sock;
1545         My_Connections[Idx].addr = new_addr;
1546         strcpy( My_Connections[Idx].host, Conf_Server[Server].host );
1547
1548         /* Neuen Socket registrieren */
1549         FD_SET( new_sock, &My_Sockets );
1550         FD_SET( new_sock, &My_Connects );
1551         if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1552         
1553         Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1554 } /* New_Server */
1555
1556
1557 LOCAL VOID
1558 Init_Conn_Struct( LONG Idx )
1559 {
1560         /* Connection-Struktur initialisieren */
1561
1562         My_Connections[Idx].sock = NONE;
1563         My_Connections[Idx].res_stat = NULL;
1564         My_Connections[Idx].host[0] = '\0';
1565         My_Connections[Idx].rbuf[0] = '\0';
1566         My_Connections[Idx].rdatalen = 0;
1567         My_Connections[Idx].wbuf[0] = '\0';
1568         My_Connections[Idx].wdatalen = 0;
1569         My_Connections[Idx].our_server = NONE;
1570         My_Connections[Idx].lastdata = time( NULL );
1571         My_Connections[Idx].lastping = 0;
1572         My_Connections[Idx].lastprivmsg = time( NULL );
1573         My_Connections[Idx].delaytime = 0;
1574         My_Connections[Idx].bytes_in = 0;
1575         My_Connections[Idx].bytes_out = 0;
1576         My_Connections[Idx].flag = 0;
1577         My_Connections[Idx].options = 0;
1578
1579 #ifdef USE_ZLIB
1580         My_Connections[Idx].zip.rbuf[0] = '\0';
1581         My_Connections[Idx].zip.rdatalen = 0;
1582         My_Connections[Idx].zip.wbuf[0] = '\0';
1583         My_Connections[Idx].zip.wdatalen = 0;
1584         My_Connections[Idx].zip.bytes_in = 0;
1585         My_Connections[Idx].zip.bytes_out = 0;
1586 #endif
1587 } /* Init_Conn_Struct */
1588
1589
1590 LOCAL BOOLEAN
1591 Init_Socket( INT Sock )
1592 {
1593         /* Socket-Optionen setzen */
1594
1595         INT on = 1;
1596
1597 #ifdef O_NONBLOCK       /* A/UX kennt das nicht? */
1598         if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1599         {
1600                 Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1601                 close( Sock );
1602                 return FALSE;
1603         }
1604 #endif
1605         if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1606         {
1607                 Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1608                 /* dieser Fehler kann ignoriert werden. */
1609         }
1610
1611         return TRUE;
1612 } /* Init_Socket */
1613
1614
1615 LOCAL VOID
1616 Read_Resolver_Result( INT r_fd )
1617 {
1618         /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1619          * und entsprechende Connection aktualisieren */
1620
1621         CHAR result[HOST_LEN];
1622         CLIENT *c;
1623         INT len, i;
1624
1625         FD_CLR( r_fd, &Resolver_FDs );
1626
1627         /* Anfrage vom Parent lesen */
1628         len = read( r_fd, result, HOST_LEN - 1 );
1629         if( len < 0 )
1630         {
1631                 /* Fehler beim Lesen aus der Pipe */
1632                 close( r_fd );
1633                 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1634                 return;
1635         }
1636         result[len] = '\0';
1637
1638         /* zugehoerige Connection suchen */
1639         for( i = 0; i < Pool_Size; i++ )
1640         {
1641                 if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break;
1642         }
1643         if( i >= Pool_Size )
1644         {
1645                 /* Opsa! Keine passende Connection gefunden!? Vermutlich
1646                  * wurde sie schon wieder geschlossen. */
1647                 close( r_fd );
1648                 Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1649                 return;
1650         }
1651
1652         Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
1653         
1654         /* Aufraeumen */
1655         close( My_Connections[i].res_stat->pipe[0] );
1656         close( My_Connections[i].res_stat->pipe[1] );
1657         free( My_Connections[i].res_stat );
1658         My_Connections[i].res_stat = NULL;
1659
1660         if( My_Connections[i].sock > NONE )
1661         {
1662                 /* Eingehende Verbindung: Hostnamen setzen */
1663                 c = Client_GetFromConn( i );
1664                 assert( c != NULL );
1665                 strcpy( My_Connections[i].host, result );
1666                 Client_SetHostname( c, result );
1667         }
1668         else
1669         {
1670                 /* Ausgehende Verbindung (=Server): IP setzen */
1671                 assert( My_Connections[i].our_server > NONE );
1672                 strcpy( Conf_Server[My_Connections[i].our_server].ip, result );
1673         }
1674
1675         /* Penalty-Zeit zurueck setzen */
1676         Conn_ResetPenalty( i );
1677 } /* Read_Resolver_Result */
1678
1679
1680 #ifdef USE_ZLIB
1681
1682 LOCAL BOOLEAN
1683 Zip_Buffer( CONN_ID Idx, CHAR *Data, INT Len )
1684 {
1685         /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
1686          * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
1687
1688         assert( Idx > NONE );
1689         assert( Data != NULL );
1690         assert( Len > 0 );
1691
1692         /* Ist noch Platz im Kompressions-Puffer? */
1693         if( ZWRITEBUFFER_LEN - My_Connections[Idx].zip.wdatalen < Len + 50 )
1694         {
1695                 /* Nein! Puffer zunaechst leeren ...*/
1696                 if( ! Zip_Flush( Idx )) return FALSE;
1697         }
1698
1699         /* Daten kopieren */
1700         memmove( My_Connections[Idx].zip.wbuf + My_Connections[Idx].zip.wdatalen, Data, Len );
1701         My_Connections[Idx].zip.wdatalen += Len;
1702
1703         return TRUE;
1704 } /* Zip_Buffer */
1705
1706
1707 LOCAL BOOLEAN
1708 Zip_Flush( CONN_ID Idx )
1709 {
1710         /* Daten komprimieren und in Schreibpuffer kopieren.
1711          * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
1712
1713         INT result, out_len;
1714         z_stream *out;
1715
1716         out = &My_Connections[Idx].zip.out;
1717
1718         out->next_in = My_Connections[Idx].zip.wbuf;
1719         out->avail_in = My_Connections[Idx].zip.wdatalen;
1720         out->next_out = My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen;
1721         out->avail_out = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen;
1722
1723         result = deflate( out, Z_SYNC_FLUSH );
1724         if(( result != Z_OK ) || ( out->avail_in > 0 ))
1725         {
1726                 Log( LOG_ALERT, "Compression error: code %d!?", result );
1727                 Conn_Close( Idx, "Compression error!", NULL, FALSE );
1728                 return FALSE;
1729         }
1730
1731         out_len = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - out->avail_out;
1732         My_Connections[Idx].wdatalen += out_len;
1733         My_Connections[Idx].bytes_out += out_len;
1734         My_Connections[Idx].zip.bytes_out += My_Connections[Idx].zip.wdatalen;
1735         My_Connections[Idx].zip.wdatalen = 0;
1736         
1737         return TRUE;
1738 } /* Zip_Flush */
1739
1740
1741 LOCAL BOOLEAN
1742 Unzip_Buffer( CONN_ID Idx )
1743 {
1744         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
1745          * wird FALSE geliefert, ansonsten TRUE. Der Fall, dass keine
1746          * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
1747
1748         INT result, in_len, out_len;
1749         z_stream *in;
1750
1751         assert( Idx > NONE );
1752
1753         if( My_Connections[Idx].zip.rdatalen <= 0 ) return TRUE;
1754
1755         in = &My_Connections[Idx].zip.in;
1756
1757         in->next_in = My_Connections[Idx].zip.rbuf;
1758         in->avail_in = My_Connections[Idx].zip.rdatalen;
1759         in->next_out = My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen;
1760         in->avail_out = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1;
1761
1762         result = inflate( in, Z_SYNC_FLUSH );
1763         if( result != Z_OK )
1764         {
1765                 Log( LOG_ALERT, "Decompression error: code %d (ni=%d, ai=%d, no=%d, ao=%d)!?", result, in->next_in, in->avail_in, in->next_out, in->avail_out );
1766                 Conn_Close( Idx, "Decompression error!", NULL, FALSE );
1767                 return FALSE;
1768         }
1769
1770         in_len = My_Connections[Idx].zip.rdatalen - in->avail_in;
1771         out_len = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1 - in->avail_out;
1772         My_Connections[Idx].rdatalen += out_len;
1773
1774         if( in->avail_in > 0 )
1775         {
1776                 /* es konnten nicht alle Daten entpackt werden, vermutlich war
1777                  * im Ziel-Puffer kein Platz mehr. Umkopieren ... */
1778                 My_Connections[Idx].zip.rdatalen -= in_len;
1779                 memmove( My_Connections[Idx].zip.rbuf, My_Connections[Idx].zip.rbuf + in_len, My_Connections[Idx].zip.rdatalen );
1780         }
1781         else My_Connections[Idx].zip.rdatalen = 0;
1782         My_Connections[Idx].zip.bytes_in += out_len;
1783
1784         return TRUE;
1785 } /* Unzip_Buffer */
1786
1787
1788 #endif
1789
1790
1791 /* -eof- */