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