]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn.c
- Vor dem Schliessen einer Verbindung wird versucht, den Buffer zu leeren.
[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.98 2002/11/28 16:56:20 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                 /* Statistik an Client melden, wenn User */
574                 if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
575                 {
576                         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 );
577                 }
578
579                 /* ERROR an Client schicken (von RFC so vorgesehen!) */
580                 if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
581                 else Conn_WriteStr( Idx, "ERROR :Closing connection." );
582                 if( My_Connections[Idx].sock == NONE ) return;
583         }
584
585         /* zunaechst versuchen, noch im Schreibpuffer vorhandene
586          * Daten auf den Socket zu schreiben ... */
587         Try_Write( Idx );
588
589         if( close( My_Connections[Idx].sock ) != 0 )
590         {
591                 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 ));
592         }
593         else
594         {
595                 in_k = (DOUBLE)My_Connections[Idx].bytes_in / 1024;
596                 out_k = (DOUBLE)My_Connections[Idx].bytes_out / 1024;
597 #ifdef USE_ZLIB
598                 if( My_Connections[Idx].options & CONN_ZIP )
599                 {
600                         in_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_in / 1024;
601                         out_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_out / 1024;
602                         in_p = (INT)(( in_k * 100 ) / in_z_k );
603                         out_p = (INT)(( out_k * 100 ) / out_z_k );
604                         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 );
605                 }
606                 else
607 #endif
608                 {
609                         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 );
610                 }
611         }
612         
613         /* Socket als "ungueltig" markieren */
614         FD_CLR( My_Connections[Idx].sock, &My_Sockets );
615         FD_CLR( My_Connections[Idx].sock, &My_Connects );
616         My_Connections[Idx].sock = NONE;
617
618         if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
619
620         if( My_Connections[Idx].res_stat )
621         {
622                 /* Resolver-Strukturen freigeben, wenn noch nicht geschehen */
623                 FD_CLR( My_Connections[Idx].res_stat->pipe[0], &Resolver_FDs );
624                 close( My_Connections[Idx].res_stat->pipe[0] );
625                 close( My_Connections[Idx].res_stat->pipe[1] );
626                 free( My_Connections[Idx].res_stat );
627         }
628
629         /* Startzeit des naechsten Connect-Versuchs modifizieren? */
630         if(( My_Connections[Idx].our_server > NONE ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry <  time( NULL ) - Conf_ConnectRetry ))
631         {
632                 /* Okay, die Verbindung stand schon "genuegend lange":
633                  * lasttry-Zeitpunkt so setzen, dass der naechste
634                  * Verbindungsversuch in RECONNECT_DELAY Sekunden
635                  * gestartet wird. */
636                 Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
637         }
638
639 #ifdef USE_ZLIB
640         /* Ggf. zlib abmelden */
641         if( Conn_Options( Idx ) & CONN_ZIP )
642         {
643                 inflateEnd( &My_Connections[Idx].zip.in );
644                 deflateEnd( &My_Connections[Idx].zip.out );
645         }
646 #endif
647
648         /* Connection-Struktur loeschen (=freigeben) */
649         Init_Conn_Struct( Idx );
650 } /* Conn_Close */
651
652
653 GLOBAL VOID
654 Conn_UpdateIdle( CONN_ID Idx )
655 {
656         /* Idle-Timer zuruecksetzen */
657
658         assert( Idx > NONE );
659         My_Connections[Idx].lastprivmsg = time( NULL );
660 }
661
662
663 GLOBAL time_t
664 Conn_GetIdle( CONN_ID Idx )
665 {
666         /* Idle-Time einer Verbindung liefern (in Sekunden) */
667
668         assert( Idx > NONE );
669         return time( NULL ) - My_Connections[Idx].lastprivmsg;
670 } /* Conn_GetIdle */
671
672
673 GLOBAL time_t
674 Conn_LastPing( CONN_ID Idx )
675 {
676         /* Zeitpunkt des letzten PING liefern */
677
678         assert( Idx > NONE );
679         return My_Connections[Idx].lastping;
680 } /* Conn_LastPing */
681
682
683 GLOBAL VOID
684 Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
685 {
686         /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
687          * waehrend dieser Zeit wird der entsprechende Socket vom Server
688          * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
689          * dieser Funktion nur erhoeht, nicht aber verringert werden. */
690         
691         time_t t;
692         
693         assert( Idx > NONE );
694         assert( Seconds >= 0 );
695         
696         t = time( NULL ) + Seconds;
697         if( t > My_Connections[Idx].delaytime ) My_Connections[Idx].delaytime = t;
698 } /* Conn_SetPenalty */
699
700
701 GLOBAL VOID
702 Conn_ResetPenalty( CONN_ID Idx )
703 {
704         assert( Idx > NONE );
705         My_Connections[Idx].delaytime = 0;
706 } /* Conn_ResetPenalty */
707
708
709 GLOBAL VOID
710 Conn_ClearFlags( VOID )
711 {
712         /* Alle Connection auf "nicht-markiert" setzen */
713
714         LONG i;
715
716         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
717 } /* Conn_ClearFlags */
718
719
720 GLOBAL INT
721 Conn_Flag( CONN_ID Idx )
722 {
723         /* Ist eine Connection markiert (TRUE) oder nicht? */
724
725         assert( Idx > NONE );
726         return My_Connections[Idx].flag;
727 } /* Conn_Flag */
728
729
730 GLOBAL VOID
731 Conn_SetFlag( CONN_ID Idx, INT Flag )
732 {
733         /* Connection markieren */
734
735         assert( Idx > NONE );
736         My_Connections[Idx].flag = Flag;
737 } /* Conn_SetFlag */
738
739
740 GLOBAL CONN_ID
741 Conn_First( VOID )
742 {
743         /* Connection-Struktur der ersten Verbindung liefern;
744          * Ist keine Verbindung vorhanden, wird NONE geliefert. */
745
746         LONG i;
747         
748         for( i = 0; i < Pool_Size; i++ )
749         {
750                 if( My_Connections[i].sock != NONE ) return i;
751         }
752         return NONE;
753 } /* Conn_First */
754
755
756 GLOBAL CONN_ID
757 Conn_Next( CONN_ID Idx )
758 {
759         /* Naechste Verbindungs-Struktur liefern; existiert keine
760          * weitere, so wird NONE geliefert. */
761
762         LONG i = NONE;
763
764         assert( Idx > NONE );
765         
766         for( i = Idx + 1; i < Pool_Size; i++ )
767         {
768                 if( My_Connections[i].sock != NONE ) return i;
769         }
770         return NONE;
771 } /* Conn_Next */
772
773
774 GLOBAL VOID
775 Conn_SetServer( CONN_ID Idx, INT ConfServer )
776 {
777         /* Connection als Server markieren: Index des konfigurierten
778          * Servers speichern. Verbindung muss bereits bestehen! */
779         
780         assert( Idx > NONE );
781         assert( My_Connections[Idx].sock > NONE );
782         
783         My_Connections[Idx].our_server = ConfServer;
784 } /* Conn_SetServer */
785
786
787 GLOBAL VOID
788 Conn_SetOption( CONN_ID Idx, INT Option )
789 {
790         /* Option fuer Verbindung setzen.
791          * Initial sind alle Optionen _nicht_ gesetzt. */
792
793         assert( Idx > NONE );
794         assert( Option != 0 );
795
796         My_Connections[Idx].options |= Option;
797 } /* Conn_SetOption */
798
799
800 GLOBAL VOID
801 Conn_UnsetOption( CONN_ID Idx, INT Option )
802 {
803         /* Option fuer Verbindung loeschen */
804
805         assert( Idx > NONE );
806         assert( Option != 0 );
807
808         My_Connections[Idx].options &= ~Option;
809 } /* Conn_UnsetOption */
810
811
812 GLOBAL INT
813 Conn_Options( CONN_ID Idx )
814 {
815         assert( Idx > NONE );
816         return My_Connections[Idx].options;
817 } /* Conn_Options */
818
819
820 #ifdef USE_ZLIB
821
822 GLOBAL BOOLEAN
823 Conn_InitZip( CONN_ID Idx )
824 {
825         /* Kompression fuer Link initialisieren */
826
827         assert( Idx > NONE );
828
829         My_Connections[Idx].zip.in.avail_in = 0;
830         My_Connections[Idx].zip.in.total_in = 0;
831         My_Connections[Idx].zip.in.total_out = 0;
832         My_Connections[Idx].zip.in.zalloc = NULL;
833         My_Connections[Idx].zip.in.zfree = NULL;
834         My_Connections[Idx].zip.in.data_type = Z_ASCII;
835
836         if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
837         {
838                 /* Fehler! */
839                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
840                 return FALSE;
841         }
842         
843         My_Connections[Idx].zip.out.total_in = 0;
844         My_Connections[Idx].zip.out.total_in = 0;
845         My_Connections[Idx].zip.out.zalloc = NULL;
846         My_Connections[Idx].zip.out.zfree = NULL;
847         My_Connections[Idx].zip.out.data_type = Z_ASCII;
848
849         if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
850         {
851                 /* Fehler! */
852                 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
853                 return FALSE;
854         }
855
856         My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
857         My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
858
859         Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
860         Conn_SetOption( Idx, CONN_ZIP );
861
862         return TRUE;
863 } /* Conn_InitZip */
864
865 #endif
866
867
868 LOCAL BOOLEAN
869 Try_Write( CONN_ID Idx )
870 {
871         /* Versuchen, Daten aus dem Schreib-Puffer in den Socket zu
872          * schreiben. TRUE wird geliefert, wenn entweder keine Daten
873          * zum Versenden vorhanden sind oder erfolgreich bearbeitet
874          * werden konnten. Im Fehlerfall wird FALSE geliefert und
875          * die Verbindung geschlossen. */
876
877         fd_set write_socket;
878         struct timeval tv;
879
880         assert( Idx > NONE );
881         assert( My_Connections[Idx].sock > NONE );
882
883         /* sind ueberhaupt Daten vorhanden? */
884 #ifdef USE_ZLIB
885         if(( ! My_Connections[Idx].wdatalen > 0 ) && ( ! My_Connections[Idx].zip.wdatalen )) return TRUE;
886 #else
887         if( ! My_Connections[Idx].wdatalen > 0 ) return TRUE;
888 #endif
889
890         /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
891         tv.tv_sec = 0; tv.tv_usec = 0;
892
893         FD_ZERO( &write_socket );
894         FD_SET( My_Connections[Idx].sock, &write_socket );
895         if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
896         {
897                 /* Fehler! */
898                 if( errno != EINTR )
899                 {
900                         Log( LOG_ALERT, "Try_Write(): select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
901                         Conn_Close( Idx, "Server error!", NULL, FALSE );
902                         return FALSE;
903                 }
904         }
905
906         if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
907         else return TRUE;
908 } /* Try_Write */
909
910
911 LOCAL VOID
912 Handle_Read( INT Sock )
913 {
914         /* Aktivitaet auf einem Socket verarbeiten:
915          *  - neue Clients annehmen,
916          *  - Daten von Clients verarbeiten,
917          *  - Resolver-Rueckmeldungen annehmen. */
918
919         CONN_ID idx;
920
921         assert( Sock > NONE );
922
923         if( FD_ISSET( Sock, &My_Listeners ))
924         {
925                 /* es ist einer unserer Listener-Sockets: es soll
926                  * also eine neue Verbindung aufgebaut werden. */
927
928                 New_Connection( Sock );
929         }
930         else if( FD_ISSET( Sock, &Resolver_FDs ))
931         {
932                 /* Rueckmeldung von einem Resolver Sub-Prozess */
933
934                 Read_Resolver_Result( Sock );
935         }
936         else
937         {
938                 /* Ein Client Socket: entweder ein User oder Server */
939
940                 idx = Socket2Index( Sock );
941                 if( idx > NONE ) Read_Request( idx );
942         }
943 } /* Handle_Read */
944
945
946 LOCAL BOOLEAN
947 Handle_Write( CONN_ID Idx )
948 {
949         /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
950
951         INT len, res, err;
952
953         assert( Idx > NONE );
954         assert( My_Connections[Idx].sock > NONE );
955
956         if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
957         {
958                 /* es soll nichts geschrieben werden, sondern ein
959                  * connect() hat ein Ergebnis geliefert */
960
961                 FD_CLR( My_Connections[Idx].sock, &My_Connects );
962
963                 /* Ergebnis des connect() ermitteln */
964                 len = sizeof( err );
965                 res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
966                 assert( len == sizeof( err ));
967
968                 /* Fehler aufgetreten? */
969                 if(( res != 0 ) || ( err != 0 ))
970                 {
971                         /* Fehler! */
972                         if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
973                         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 ));
974
975                         /* Socket etc. pp. aufraeumen */
976                         FD_CLR( My_Connections[Idx].sock, &My_Sockets );
977                         close( My_Connections[Idx].sock );
978                         Init_Conn_Struct( Idx );
979
980                         /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
981                         Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
982
983                         return FALSE;
984                 }
985                 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 );
986
987                 /* PASS und SERVER verschicken */
988                 Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd_out, NGIRCd_ProtoID );
989                 return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
990         }
991
992 #ifdef USE_ZLIB
993         /* Schreibpuffer leer, aber noch Daten im Kompressionsbuffer?
994          * Dann muss dieser nun geflushed werden! */
995         if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
996 #endif
997
998         assert( My_Connections[Idx].wdatalen > 0 );
999
1000         /* Daten schreiben */
1001         len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
1002         if( len < 0 )
1003         {
1004                 /* Operation haette Socket "nur" blockiert ... */
1005                 if( errno == EAGAIN ) return TRUE;
1006
1007                 /* Oops, ein Fehler! */
1008                 Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1009                 Conn_Close( Idx, "Write error!", NULL, FALSE );
1010                 return FALSE;
1011         }
1012
1013         /* Puffer anpassen */
1014         My_Connections[Idx].wdatalen -= len;
1015         memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
1016
1017         return TRUE;
1018 } /* Handle_Write */
1019
1020
1021 LOCAL VOID
1022 New_Connection( INT Sock )
1023 {
1024         /* Neue Client-Verbindung von Listen-Socket annehmen und
1025          * CLIENT-Struktur anlegen. */
1026
1027         struct sockaddr_in new_addr;
1028         INT new_sock, new_sock_len;
1029         RES_STAT *s;
1030         CONN_ID idx;
1031         CLIENT *c;
1032         POINTER *ptr;
1033         LONG new_size;
1034
1035         assert( Sock > NONE );
1036
1037         /* Connection auf Listen-Socket annehmen */
1038         new_sock_len = sizeof( new_addr );
1039         new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
1040         if( new_sock < 0 )
1041         {
1042                 Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
1043                 return;
1044         }
1045
1046         /* Socket initialisieren */
1047         Init_Socket( new_sock );
1048
1049         /* Freie Connection-Struktur suchen */
1050         for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1051         if( idx >= Pool_Size )
1052         {
1053                 new_size = Pool_Size + CONNECTION_POOL;
1054                 
1055                 /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
1056                  * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
1057                 
1058                 if( Conf_MaxConnections > 0 )
1059                 {
1060                         /* Es ist ein Limit konfiguriert */
1061                         if( Pool_Size >= Conf_MaxConnections )
1062                         {
1063                                 /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
1064                                 Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
1065                                 close( new_sock );
1066                                 return;
1067                         }
1068                         if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
1069                 }
1070                 
1071                 /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
1072                  * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
1073                  * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
1074                 ptr = realloc( My_Connections, sizeof( CONNECTION ) * new_size );
1075                 if( ! ptr )
1076                 {
1077                         /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
1078                         ptr = malloc( sizeof( CONNECTION ) * new_size );
1079                         if( ! ptr )
1080                         {
1081                                 /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
1082                                 Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
1083                                 close( new_sock );
1084                                 return;
1085                         }
1086                         
1087                         /* Struktur umkopieren ... */
1088                         memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
1089                         
1090                         Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [malloc()/memcpy()]", new_size );
1091                 }
1092                 else Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [realloc()]", new_size );
1093                 
1094                 My_Connections = ptr;
1095                 Pool_Size = new_size;
1096         }
1097
1098         /* Client-Struktur initialisieren */
1099         c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
1100         if( ! c )
1101         {
1102                 Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1103                 close( new_sock );
1104                 return;
1105         }
1106
1107         /* Verbindung registrieren */
1108         Init_Conn_Struct( idx );
1109         My_Connections[idx].sock = new_sock;
1110         My_Connections[idx].addr = new_addr;
1111
1112         /* Neuen Socket registrieren */
1113         FD_SET( new_sock, &My_Sockets );
1114         if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1115
1116         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 );
1117
1118         /* Hostnamen ermitteln */
1119         strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
1120         Client_SetHostname( c, My_Connections[idx].host );
1121         s = Resolve_Addr( &new_addr );
1122         if( s )
1123         {
1124                 /* Sub-Prozess wurde asyncron gestartet */
1125                 Conn_WriteStr( idx, "NOTICE AUTH :%sLooking up your hostname ...", NOTICE_TXTPREFIX );
1126                 My_Connections[idx].res_stat = s;
1127         }
1128         
1129         /* Penalty-Zeit setzen */
1130         Conn_SetPenalty( idx, 4 );
1131 } /* New_Connection */
1132
1133
1134 LOCAL CONN_ID
1135 Socket2Index( INT Sock )
1136 {
1137         /* zum Socket passende Connection suchen */
1138
1139         CONN_ID idx;
1140
1141         assert( Sock > NONE );
1142
1143         for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1144
1145         if( idx >= Pool_Size )
1146         {
1147                 /* die Connection wurde vermutlich (wegen eines
1148                  * Fehlers) bereits wieder abgebaut ... */
1149                 Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1150                 return NONE;
1151         }
1152         else return idx;
1153 } /* Socket2Index */
1154
1155
1156 LOCAL VOID
1157 Read_Request( CONN_ID Idx )
1158 {
1159         /* Daten von Socket einlesen und entsprechend behandeln.
1160          * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1161
1162         INT len, bsize;
1163         CLIENT *c;
1164
1165         assert( Idx > NONE );
1166         assert( My_Connections[Idx].sock > NONE );
1167
1168         /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
1169          * ansonsten koennen Daten ggf. nicht umkopiert werden. */
1170         bsize = READBUFFER_LEN;
1171 #ifdef USE_ZLIB
1172         c = Client_GetFromConn( Idx );
1173         if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) && ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN )) bsize = ZREADBUFFER_LEN;
1174 #endif
1175
1176 #ifdef USE_ZLIB
1177         if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1178 #else
1179         if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
1180 #endif
1181         {
1182                 /* Der Lesepuffer ist voll */
1183                 Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1184                 Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
1185                 return;
1186         }
1187
1188 #ifdef USE_ZLIB
1189         if( My_Connections[Idx].options & CONN_ZIP )
1190         {
1191                 len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 );
1192                 if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1193         }
1194         else
1195 #endif
1196         {
1197                 len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, bsize - My_Connections[Idx].rdatalen - 1, 0 );
1198                 if( len > 0 ) My_Connections[Idx].rdatalen += len;
1199         }
1200
1201         if( len == 0 )
1202         {
1203                 /* Socket wurde geschlossen */
1204                 Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
1205                 Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
1206                 return;
1207         }
1208
1209         if( len < 0 )
1210         {
1211                 /* Operation haette Socket "nur" blockiert ... */
1212                 if( errno == EAGAIN ) return;
1213
1214                 /* Fehler beim Lesen */
1215                 Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1216                 Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
1217                 return;
1218         }
1219
1220         /* Connection-Statistik aktualisieren */
1221         My_Connections[Idx].bytes_in += len;
1222
1223         /* Timestamp aktualisieren */
1224         My_Connections[Idx].lastdata = time( NULL );
1225
1226         Handle_Buffer( Idx );
1227 } /* Read_Request */
1228
1229
1230 LOCAL BOOLEAN
1231 Handle_Buffer( CONN_ID Idx )
1232 {
1233         /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1234          * Wurde ein Request verarbeitet, so wird TRUE geliefert,
1235          * ansonsten FALSE (auch bei Fehlern). */
1236
1237 #ifndef STRICT_RFC
1238         CHAR *ptr1, *ptr2;
1239 #endif
1240         CHAR *ptr;
1241         INT len, delta;
1242         BOOLEAN action, result;
1243 #ifdef USE_ZLIB
1244         BOOLEAN old_z;
1245 #endif
1246
1247         result = FALSE;
1248         do
1249         {
1250 #ifdef USE_ZLIB
1251                 /* ggf. noch unkomprimiete Daten weiter entpacken */
1252                 if( My_Connections[Idx].options & CONN_ZIP )
1253                 {
1254                         if( ! Unzip_Buffer( Idx )) return FALSE;
1255                 }
1256 #endif
1257         
1258                 if( My_Connections[Idx].rdatalen < 1 ) break;
1259
1260                 /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1261                  * RFC 2812. Haben wir eine? */
1262                 My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1263                 ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1264         
1265                 if( ptr ) delta = 2;
1266 #ifndef STRICT_RFC
1267                 else
1268                 {
1269                         /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1270                          * machen soetwas viele Clients, u.a. "mIRC" :-( */
1271                         ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1272                         ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1273                         delta = 1;
1274                         if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1275                         else if( ptr1 ) ptr = ptr1;
1276                         else if( ptr2 ) ptr = ptr2;
1277                 }
1278 #endif
1279         
1280                 action = FALSE;
1281                 if( ptr )
1282                 {
1283                         /* Ende der Anfrage wurde gefunden */
1284                         *ptr = '\0';
1285                         len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1286                         if( len > ( COMMAND_LEN - 1 ))
1287                         {
1288                                 /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1289                                  * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1290                                  * empfangen wird, wird der Client disconnectiert. */
1291                                 Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
1292                                 Conn_Close( Idx, NULL, "Request too long", TRUE );
1293                                 return FALSE;
1294                         }
1295
1296 #ifdef USE_ZLIB
1297                         /* merken, ob Stream bereits komprimiert wird */
1298                         old_z = My_Connections[Idx].options & CONN_ZIP;
1299 #endif
1300
1301                         if( len > delta )
1302                         {
1303                                 /* Es wurde ein Request gelesen */
1304                                 if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
1305                                 else action = TRUE;
1306                         }
1307
1308                         /* Puffer anpassen */
1309                         My_Connections[Idx].rdatalen -= len;
1310                         memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1311
1312 #ifdef USE_ZLIB
1313                         if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1314                         {
1315                                 /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1316                                  * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1317                                  * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1318                                 {
1319                                         if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1320                                         {
1321                                                 /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1322                                                 Log( LOG_ALERT, "Can't move read buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
1323                                                 return FALSE;
1324                                         }
1325                                         memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1326                                         My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1327                                         My_Connections[Idx].rdatalen = 0;
1328                                         Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1329                                 }
1330                         }
1331 #endif
1332                 }
1333                 
1334                 if( action ) result = TRUE;
1335         } while( action );
1336         
1337         return result;
1338 } /* Handle_Buffer */
1339
1340
1341 LOCAL VOID
1342 Check_Connections( VOID )
1343 {
1344         /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1345          * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1346          * auch das nicht "hilft", Client disconnectieren. */
1347
1348         CLIENT *c;
1349         LONG i;
1350
1351         for( i = 0; i < Pool_Size; i++ )
1352         {
1353                 if( My_Connections[i].sock == NONE ) continue;
1354
1355                 c = Client_GetFromConn( i );
1356                 if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1357                 {
1358                         /* verbundener User, Server oder Service */
1359                         if( My_Connections[i].lastping > My_Connections[i].lastdata )
1360                         {
1361                                 /* es wurde bereits ein PING gesendet */
1362                                 if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1363                                 {
1364                                         /* Timeout */
1365                                         Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1366                                         Conn_Close( i, NULL, "Ping timeout", TRUE );
1367                                 }
1368                         }
1369                         else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1370                         {
1371                                 /* es muss ein PING gesendet werden */
1372                                 Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1373                                 My_Connections[i].lastping = time( NULL );
1374                                 Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1375                         }
1376                 }
1377                 else
1378                 {
1379                         /* noch nicht vollstaendig aufgebaute Verbindung */
1380                         if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1381                         {
1382                                 /* Timeout */
1383                                 Log( LOG_DEBUG, "Connection %d timed out ...", i );
1384                                 Conn_Close( i, NULL, "Timeout", FALSE );
1385                         }
1386                 }
1387         }
1388 } /* Check_Connections */
1389
1390
1391 LOCAL VOID
1392 Check_Servers( VOID )
1393 {
1394         /* Pruefen, ob Server-Verbindungen aufgebaut werden
1395          * muessen bzw. koennen */
1396
1397         RES_STAT *s;
1398         LONG idx, n;
1399         INT i;
1400
1401         /* Wenn "Passive-Mode" aktiv: nicht verbinden */
1402         if( NGIRCd_Passive ) return;
1403
1404         for( i = 0; i < Conf_Server_Count; i++ )
1405         {
1406                 /* Ist ein Hostname und Port definiert? */
1407                 if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue;
1408
1409                 /* Haben wir schon eine Verbindung? */
1410                 for( n = 0; n < Pool_Size; n++ )
1411                 {
1412                         if( My_Connections[n].sock == NONE ) continue;
1413                         
1414                         /* Verbindung zu diesem Server? */
1415                         if( My_Connections[n].our_server == i )
1416                         {
1417                                 /* Komplett aufgebaute Verbindung? */
1418                                 if( My_Connections[n].sock > NONE ) break;
1419
1420                                 /* IP schon aufgeloest? */
1421                                 if( My_Connections[n].res_stat == NULL ) New_Server( i, n );
1422                         }
1423
1424                         /* Verbindung in dieser Server-Gruppe? */
1425                         if(( My_Connections[n].our_server != NONE ) && ( Conf_Server[i].group != NONE ))
1426                         {
1427                                 if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break;
1428                         }
1429                 }
1430                 if( n < Pool_Size ) continue;
1431
1432                 /* Wann war der letzte Connect-Versuch? */
1433                 if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1434
1435                 /* Okay, Verbindungsaufbau versuchen */
1436                 Conf_Server[i].lasttry = time( NULL );
1437
1438                 /* Freie Connection-Struktur suschen */
1439                 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1440                 if( idx >= Pool_Size )
1441                 {
1442                         Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1443                         return;
1444                 }
1445                 Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1446
1447                 /* Verbindungs-Struktur initialisieren */
1448                 Init_Conn_Struct( idx );
1449                 My_Connections[idx].sock = SERVER_WAIT;
1450                 My_Connections[idx].our_server = i;
1451
1452                 /* Hostnamen in IP aufloesen (Default bzw. im Fehlerfall: versuchen, den
1453                  * konfigurierten Text direkt als IP-Adresse zu verwenden ... */
1454                 strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
1455                 strcpy( My_Connections[idx].host, Conf_Server[i].host );
1456                 s = Resolve_Name( Conf_Server[i].host );
1457                 if( s )
1458                 {
1459                         /* Sub-Prozess wurde asyncron gestartet */
1460                         My_Connections[idx].res_stat = s;
1461                 }
1462         }
1463 } /* Check_Servers */
1464
1465
1466 LOCAL VOID
1467 New_Server( INT Server, CONN_ID Idx )
1468 {
1469         /* Neue Server-Verbindung aufbauen */
1470
1471         struct sockaddr_in new_addr;
1472         struct in_addr inaddr;
1473         INT res, new_sock;
1474         CLIENT *c;
1475
1476         assert( Server > NONE );
1477         assert( Idx > NONE );
1478
1479         /* Wurde eine gueltige IP-Adresse gefunden? */
1480         if( ! Conf_Server[Server].ip[0] )
1481         {
1482                 /* Nein. Verbindung wieder freigeben: */
1483                 Init_Conn_Struct( Idx );
1484                 Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1485                 return;
1486         }
1487
1488         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 );
1489
1490 #ifdef HAVE_INET_ATON
1491         if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1492 #else
1493         memset( &inaddr, 0, sizeof( inaddr ));
1494         inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1495         if( inaddr.s_addr == (unsigned)-1 )
1496 #endif
1497         {
1498                 /* Konnte Adresse nicht konvertieren */
1499                 Init_Conn_Struct( Idx );
1500                 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 );
1501                 return;
1502         }
1503
1504         memset( &new_addr, 0, sizeof( new_addr ));
1505         new_addr.sin_family = AF_INET;
1506         new_addr.sin_addr = inaddr;
1507         new_addr.sin_port = htons( Conf_Server[Server].port );
1508
1509         new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1510         if ( new_sock < 0 )
1511         {
1512                 Init_Conn_Struct( Idx );
1513                 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1514                 return;
1515         }
1516
1517         if( ! Init_Socket( new_sock )) return;
1518
1519         res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1520         if(( res != 0 ) && ( errno != EINPROGRESS ))
1521         {
1522                 Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1523                 close( new_sock );
1524                 Init_Conn_Struct( Idx );
1525                 return;
1526         }
1527
1528         /* Client-Struktur initialisieren */
1529         c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1530         if( ! c )
1531         {
1532                 close( new_sock );
1533                 Init_Conn_Struct( Idx );
1534                 Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1535                 return;
1536         }
1537         Client_SetIntroducer( c, c );
1538         Client_SetToken( c, TOKEN_OUTBOUND );
1539
1540         /* Verbindung registrieren */
1541         My_Connections[Idx].sock = new_sock;
1542         My_Connections[Idx].addr = new_addr;
1543         strcpy( My_Connections[Idx].host, Conf_Server[Server].host );
1544
1545         /* Neuen Socket registrieren */
1546         FD_SET( new_sock, &My_Sockets );
1547         FD_SET( new_sock, &My_Connects );
1548         if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1549         
1550         Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1551 } /* New_Server */
1552
1553
1554 LOCAL VOID
1555 Init_Conn_Struct( LONG Idx )
1556 {
1557         /* Connection-Struktur initialisieren */
1558
1559         My_Connections[Idx].sock = NONE;
1560         My_Connections[Idx].res_stat = NULL;
1561         My_Connections[Idx].host[0] = '\0';
1562         My_Connections[Idx].rbuf[0] = '\0';
1563         My_Connections[Idx].rdatalen = 0;
1564         My_Connections[Idx].wbuf[0] = '\0';
1565         My_Connections[Idx].wdatalen = 0;
1566         My_Connections[Idx].our_server = NONE;
1567         My_Connections[Idx].lastdata = time( NULL );
1568         My_Connections[Idx].lastping = 0;
1569         My_Connections[Idx].lastprivmsg = time( NULL );
1570         My_Connections[Idx].delaytime = 0;
1571         My_Connections[Idx].bytes_in = 0;
1572         My_Connections[Idx].bytes_out = 0;
1573         My_Connections[Idx].flag = 0;
1574         My_Connections[Idx].options = 0;
1575
1576 #ifdef USE_ZLIB
1577         My_Connections[Idx].zip.rbuf[0] = '\0';
1578         My_Connections[Idx].zip.rdatalen = 0;
1579         My_Connections[Idx].zip.wbuf[0] = '\0';
1580         My_Connections[Idx].zip.wdatalen = 0;
1581         My_Connections[Idx].zip.bytes_in = 0;
1582         My_Connections[Idx].zip.bytes_out = 0;
1583 #endif
1584 } /* Init_Conn_Struct */
1585
1586
1587 LOCAL BOOLEAN
1588 Init_Socket( INT Sock )
1589 {
1590         /* Socket-Optionen setzen */
1591
1592         INT on = 1;
1593
1594 #ifdef O_NONBLOCK       /* A/UX kennt das nicht? */
1595         if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1596         {
1597                 Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1598                 close( Sock );
1599                 return FALSE;
1600         }
1601 #endif
1602         if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1603         {
1604                 Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1605                 /* dieser Fehler kann ignoriert werden. */
1606         }
1607
1608         return TRUE;
1609 } /* Init_Socket */
1610
1611
1612 LOCAL VOID
1613 Read_Resolver_Result( INT r_fd )
1614 {
1615         /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1616          * und entsprechende Connection aktualisieren */
1617
1618         CHAR result[HOST_LEN];
1619         CLIENT *c;
1620         INT len, i;
1621
1622         FD_CLR( r_fd, &Resolver_FDs );
1623
1624         /* Anfrage vom Parent lesen */
1625         len = read( r_fd, result, HOST_LEN - 1 );
1626         if( len < 0 )
1627         {
1628                 /* Fehler beim Lesen aus der Pipe */
1629                 close( r_fd );
1630                 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1631                 return;
1632         }
1633         result[len] = '\0';
1634
1635         /* zugehoerige Connection suchen */
1636         for( i = 0; i < Pool_Size; i++ )
1637         {
1638                 if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break;
1639         }
1640         if( i >= Pool_Size )
1641         {
1642                 /* Opsa! Keine passende Connection gefunden!? Vermutlich
1643                  * wurde sie schon wieder geschlossen. */
1644                 close( r_fd );
1645                 Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1646                 return;
1647         }
1648
1649         Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
1650         
1651         /* Aufraeumen */
1652         close( My_Connections[i].res_stat->pipe[0] );
1653         close( My_Connections[i].res_stat->pipe[1] );
1654         free( My_Connections[i].res_stat );
1655         My_Connections[i].res_stat = NULL;
1656
1657         if( My_Connections[i].sock > NONE )
1658         {
1659                 /* Eingehende Verbindung: Hostnamen setzen */
1660                 c = Client_GetFromConn( i );
1661                 assert( c != NULL );
1662                 strcpy( My_Connections[i].host, result );
1663                 Client_SetHostname( c, result );
1664
1665                 Conn_WriteStr( i, "NOTICE AUTH :%sGot your hostname.", NOTICE_TXTPREFIX );
1666         }
1667         else
1668         {
1669                 /* Ausgehende Verbindung (=Server): IP setzen */
1670                 assert( My_Connections[i].our_server > NONE );
1671                 strcpy( Conf_Server[My_Connections[i].our_server].ip, result );
1672         }
1673
1674         /* Penalty-Zeit zurueck setzen */
1675         Conn_ResetPenalty( i );
1676 } /* Read_Resolver_Result */
1677
1678
1679 #ifdef USE_ZLIB
1680
1681 LOCAL BOOLEAN
1682 Zip_Buffer( CONN_ID Idx, CHAR *Data, INT Len )
1683 {
1684         /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
1685          * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
1686
1687         assert( Idx > NONE );
1688         assert( Data != NULL );
1689         assert( Len > 0 );
1690
1691         /* Ist noch Platz im Kompressions-Puffer? */
1692         if( ZWRITEBUFFER_LEN - My_Connections[Idx].zip.wdatalen < Len + 50 )
1693         {
1694                 /* Nein! Puffer zunaechst leeren ...*/
1695                 if( ! Zip_Flush( Idx )) return FALSE;
1696         }
1697
1698         /* Daten kopieren */
1699         memmove( My_Connections[Idx].zip.wbuf + My_Connections[Idx].zip.wdatalen, Data, Len );
1700         My_Connections[Idx].zip.wdatalen += Len;
1701
1702         return TRUE;
1703 } /* Zip_Buffer */
1704
1705
1706 LOCAL BOOLEAN
1707 Zip_Flush( CONN_ID Idx )
1708 {
1709         /* Daten komprimieren und in Schreibpuffer kopieren.
1710          * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
1711
1712         INT result, out_len;
1713         z_stream *out;
1714
1715         out = &My_Connections[Idx].zip.out;
1716
1717         out->next_in = My_Connections[Idx].zip.wbuf;
1718         out->avail_in = My_Connections[Idx].zip.wdatalen;
1719         out->next_out = My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen;
1720         out->avail_out = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen;
1721
1722         result = deflate( out, Z_SYNC_FLUSH );
1723         if(( result != Z_OK ) || ( out->avail_in > 0 ))
1724         {
1725                 Log( LOG_ALERT, "Compression error: code %d!?", result );
1726                 Conn_Close( Idx, "Compression error!", NULL, FALSE );
1727                 return FALSE;
1728         }
1729
1730         out_len = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - out->avail_out;
1731         My_Connections[Idx].wdatalen += out_len;
1732         My_Connections[Idx].bytes_out += out_len;
1733         My_Connections[Idx].zip.bytes_out += My_Connections[Idx].zip.wdatalen;
1734         My_Connections[Idx].zip.wdatalen = 0;
1735         
1736         return TRUE;
1737 } /* Zip_Flush */
1738
1739
1740 LOCAL BOOLEAN
1741 Unzip_Buffer( CONN_ID Idx )
1742 {
1743         /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
1744          * wird FALSE geliefert, ansonsten TRUE. Der Fall, dass keine
1745          * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
1746
1747         INT result, in_len, out_len;
1748         z_stream *in;
1749
1750         assert( Idx > NONE );
1751
1752         if( My_Connections[Idx].zip.rdatalen <= 0 ) return TRUE;
1753
1754         in = &My_Connections[Idx].zip.in;
1755
1756         in->next_in = My_Connections[Idx].zip.rbuf;
1757         in->avail_in = My_Connections[Idx].zip.rdatalen;
1758         in->next_out = My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen;
1759         in->avail_out = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1;
1760
1761         result = inflate( in, Z_SYNC_FLUSH );
1762         if( result != Z_OK )
1763         {
1764                 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 );
1765                 Conn_Close( Idx, "Decompression error!", NULL, FALSE );
1766                 return FALSE;
1767         }
1768
1769         in_len = My_Connections[Idx].zip.rdatalen - in->avail_in;
1770         out_len = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1 - in->avail_out;
1771         My_Connections[Idx].rdatalen += out_len;
1772
1773         if( in->avail_in > 0 )
1774         {
1775                 /* es konnten nicht alle Daten entpackt werden, vermutlich war
1776                  * im Ziel-Puffer kein Platz mehr. Umkopieren ... */
1777                 My_Connections[Idx].zip.rdatalen -= in_len;
1778                 memmove( My_Connections[Idx].zip.rbuf, My_Connections[Idx].zip.rbuf + in_len, My_Connections[Idx].zip.rdatalen );
1779         }
1780         else My_Connections[Idx].zip.rdatalen = 0;
1781         My_Connections[Idx].zip.bytes_in += out_len;
1782
1783         return TRUE;
1784 } /* Unzip_Buffer */
1785
1786
1787 #endif
1788
1789
1790 /* -eof- */