2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
12 * $Id: conn.c,v 1.96 2002/11/26 23:07:24 alex Exp $
14 * connect.h: Verwaltung aller Netz-Verbindungen ("connections")
29 #include <sys/socket.h>
31 #include <sys/types.h>
33 #include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
38 #define PF_INET AF_INET
42 #include <stdint.h> /* u.a. fuer Mac OS X */
64 #define SERVER_WAIT (NONE - 1)
68 typedef struct _ZipData
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!) */
81 typedef struct _Connection
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 */
100 ZIPDATA zip; /* Kompressionsinformationen */
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 ));
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 ));
126 LOCAL fd_set My_Listeners;
127 LOCAL fd_set My_Sockets;
128 LOCAL fd_set My_Connects;
130 LOCAL CONNECTION *My_Connections;
131 LOCAL LONG Pool_Size;
137 /* Modul initialisieren: statische Strukturen "ausnullen". */
141 /* Speicher fuer Verbindungs-Pool anfordern */
142 Pool_Size = CONNECTION_POOL;
143 if( Conf_MaxConnections > 0 )
145 /* konfiguriertes Limit beachten */
146 if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
148 My_Connections = malloc( sizeof( CONNECTION ) * Pool_Size );
149 if( ! My_Connections )
151 /* Speicher konnte nicht alloziert werden! */
152 Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
155 Log( LOG_DEBUG, "Allocted connection pool for %ld items.", Pool_Size );
157 /* zu Beginn haben wir keine Verbindungen */
158 FD_ZERO( &My_Listeners );
159 FD_ZERO( &My_Sockets );
160 FD_ZERO( &My_Connects );
162 /* Groesster File-Descriptor fuer select() */
165 /* Connection-Struktur initialisieren */
166 for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
173 /* Modul abmelden: alle noch offenen Connections
174 * schliessen und freigeben. */
179 /* Sockets schliessen */
180 Log( LOG_DEBUG, "Shutting down all connections ..." );
181 for( i = 0; i < Conn_MaxFD + 1; i++ )
183 if( FD_ISSET( i, &My_Sockets ))
185 for( idx = 0; idx < Pool_Size; idx++ )
187 if( My_Connections[idx].sock == i ) break;
189 if( FD_ISSET( i, &My_Listeners ))
192 Log( LOG_DEBUG, "Listening socket %d closed.", i );
194 else if( FD_ISSET( i, &My_Connects ))
197 Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
199 else if( idx < Pool_Size )
201 if( NGIRCd_Restart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
202 else Conn_Close( idx, NULL, "Server going down", TRUE );
206 Log( LOG_WARNING, "Closing unknown connection %d ...", i );
212 free( My_Connections );
213 My_Connections = NULL;
219 Conn_InitListeners( VOID )
221 /* Ports, auf denen der Server Verbindungen entgegennehmen
222 * soll, initialisieren */
227 for( i = 0; i < Conf_ListenPorts_Count; i++ )
229 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
230 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
233 } /* Conn_InitListeners */
237 Conn_ExitListeners( VOID )
239 /* Alle "Listen-Sockets" schliessen */
243 Log( LOG_INFO, "Shutting down all listening sockets ..." );
244 for( i = 0; i < Conn_MaxFD + 1; i++ )
246 if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
249 Log( LOG_DEBUG, "Listening socket %d closed.", i );
252 } /* Conn_ExitListeners */
256 Conn_NewListener( CONST UINT Port )
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.*/
262 struct sockaddr_in addr;
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 );
271 /* Socket erzeugen */
272 sock = socket( PF_INET, SOCK_STREAM, 0);
275 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
279 if( ! Init_Socket( sock )) return FALSE;
282 if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
284 Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
289 /* in "listen mode" gehen :-) */
290 if( listen( sock, 10 ) != 0 )
292 Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
297 /* Neuen Listener in Strukturen einfuegen */
298 FD_SET( sock, &My_Listeners );
299 FD_SET( sock, &My_Sockets );
301 if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
303 Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
306 } /* Conn_NewListener */
312 /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
313 * werden dabei durchgefuehrt, bis der Server terminieren oder neu
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.
324 fd_set read_sockets, write_sockets;
330 start = time( NULL );
331 while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart ))
337 Check_Connections( );
339 /* noch volle Lese-Buffer suchen */
340 for( i = 0; i < Pool_Size; i++ )
342 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
344 /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
345 if( Handle_Buffer( i )) timeout = FALSE;
349 /* noch volle Schreib-Puffer suchen */
350 FD_ZERO( &write_sockets );
351 for( i = 0; i < Pool_Size; i++ )
354 if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
356 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
359 /* Socket der Verbindung in Set aufnehmen */
360 FD_SET( My_Connections[i].sock, &write_sockets );
363 /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
364 for( i = 0; i < Pool_Size; i++ )
366 if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects ))) FD_SET( My_Connections[i].sock, &write_sockets );
369 /* von welchen Sockets koennte gelesen werden? */
371 read_sockets = My_Sockets;
372 for( i = 0; i < Pool_Size; i++ )
374 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
376 /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
377 FD_CLR( My_Connections[i].sock, &read_sockets );
379 if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
381 /* Hier laeuft noch ein asyncrones connect() */
382 FD_CLR( My_Connections[i].sock, &read_sockets );
384 if( My_Connections[i].delaytime > t )
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 );
391 for( i = 0; i < Conn_MaxFD + 1; i++ )
393 /* Pipes von Resolver Sub-Prozessen aufnehmen */
394 if( FD_ISSET( i, &Resolver_FDs ))
396 FD_SET( i, &read_sockets );
400 /* Timeout initialisieren */
402 if( timeout ) tv.tv_sec = TIME_RES;
405 /* Auf Aktivitaet warten */
406 i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
409 /* keine Veraenderung an den Sockets */
414 /* Fehler (z.B. Interrupt) */
417 Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
418 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
424 /* Koennen Daten geschrieben werden? */
425 for( i = 0; i < Conn_MaxFD + 1; i++ )
427 if( ! FD_ISSET( i, &write_sockets )) continue;
429 /* Es kann geschrieben werden ... */
430 idx = Socket2Index( i );
431 if( idx == NONE ) continue;
433 if( ! Handle_Write( idx ))
435 /* Fehler beim Schreiben! Diesen Socket nun
436 * auch aus dem Read-Set entfernen: */
437 FD_CLR( i, &read_sockets );
441 /* Daten zum Lesen vorhanden? */
442 for( i = 0; i < Conn_MaxFD + 1; i++ )
444 if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
452 Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
455 Conn_WriteStr( Idx, Format, va_alist )
461 /* String in Socket schreiben. CR+LF wird von dieser Funktion
462 * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
463 * getrennt und FALSE geliefert. */
465 CHAR buffer[COMMAND_LEN];
469 assert( Idx > NONE );
470 assert( Format != NULL );
473 va_start( ap, Format );
477 if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
479 Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
480 Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
485 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
488 strcat( buffer, "\r\n" );
489 ok = Conn_Write( Idx, buffer, strlen( buffer ));
493 } /* Conn_WriteStr */
497 Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
499 /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
500 * der Client disconnectiert und FALSE geliefert. */
502 assert( Idx > NONE );
503 assert( Data != NULL );
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 )
512 Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
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 )
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;
525 /* nun neu pruefen: */
526 if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
528 Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
529 Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
535 if( My_Connections[Idx].options & CONN_ZIP )
537 /* Daten komprimieren und in Puffer kopieren */
538 if( ! Zip_Buffer( Idx, Data, Len )) return FALSE;
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;
554 Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
556 /* Verbindung schliessen. Evtl. noch von Resolver
557 * Sub-Prozessen offene Pipes werden geschlossen. */
562 DOUBLE in_z_k, out_z_k;
566 assert( Idx > NONE );
567 assert( My_Connections[Idx].sock > NONE );
569 c = Client_GetFromConn( Idx );
573 /* Statistik an Client melden, wenn User */
574 if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
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 );
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;
585 if( close( My_Connections[Idx].sock ) != 0 )
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 ));
591 in_k = (DOUBLE)My_Connections[Idx].bytes_in / 1024;
592 out_k = (DOUBLE)My_Connections[Idx].bytes_out / 1024;
594 if( My_Connections[Idx].options & CONN_ZIP )
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 );
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 );
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;
614 if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
616 if( My_Connections[Idx].res_stat )
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 );
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 ))
628 /* Okay, die Verbindung stand schon "genuegend lange":
629 * lasttry-Zeitpunkt so setzen, dass der naechste
630 * Verbindungsversuch in RECONNECT_DELAY Sekunden
632 Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
636 /* Ggf. zlib abmelden */
637 if( Conn_Options( Idx ) & CONN_ZIP )
639 inflateEnd( &My_Connections[Idx].zip.in );
640 deflateEnd( &My_Connections[Idx].zip.out );
644 /* Connection-Struktur loeschen (=freigeben) */
645 Init_Conn_Struct( Idx );
650 Conn_UpdateIdle( CONN_ID Idx )
652 /* Idle-Timer zuruecksetzen */
654 assert( Idx > NONE );
655 My_Connections[Idx].lastprivmsg = time( NULL );
660 Conn_GetIdle( CONN_ID Idx )
662 /* Idle-Time einer Verbindung liefern (in Sekunden) */
664 assert( Idx > NONE );
665 return time( NULL ) - My_Connections[Idx].lastprivmsg;
670 Conn_LastPing( CONN_ID Idx )
672 /* Zeitpunkt des letzten PING liefern */
674 assert( Idx > NONE );
675 return My_Connections[Idx].lastping;
676 } /* Conn_LastPing */
680 Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
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. */
689 assert( Idx > NONE );
690 assert( Seconds >= 0 );
692 t = time( NULL ) + Seconds;
693 if( t > My_Connections[Idx].delaytime ) My_Connections[Idx].delaytime = t;
694 } /* Conn_SetPenalty */
698 Conn_ResetPenalty( CONN_ID Idx )
700 assert( Idx > NONE );
701 My_Connections[Idx].delaytime = 0;
702 } /* Conn_ResetPenalty */
706 Conn_ClearFlags( VOID )
708 /* Alle Connection auf "nicht-markiert" setzen */
712 for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
713 } /* Conn_ClearFlags */
717 Conn_Flag( CONN_ID Idx )
719 /* Ist eine Connection markiert (TRUE) oder nicht? */
721 assert( Idx > NONE );
722 return My_Connections[Idx].flag;
727 Conn_SetFlag( CONN_ID Idx, INT Flag )
729 /* Connection markieren */
731 assert( Idx > NONE );
732 My_Connections[Idx].flag = Flag;
739 /* Connection-Struktur der ersten Verbindung liefern;
740 * Ist keine Verbindung vorhanden, wird NONE geliefert. */
744 for( i = 0; i < Pool_Size; i++ )
746 if( My_Connections[i].sock != NONE ) return i;
753 Conn_Next( CONN_ID Idx )
755 /* Naechste Verbindungs-Struktur liefern; existiert keine
756 * weitere, so wird NONE geliefert. */
760 assert( Idx > NONE );
762 for( i = Idx + 1; i < Pool_Size; i++ )
764 if( My_Connections[i].sock != NONE ) return i;
771 Conn_SetServer( CONN_ID Idx, INT ConfServer )
773 /* Connection als Server markieren: Index des konfigurierten
774 * Servers speichern. Verbindung muss bereits bestehen! */
776 assert( Idx > NONE );
777 assert( My_Connections[Idx].sock > NONE );
779 My_Connections[Idx].our_server = ConfServer;
780 } /* Conn_SetServer */
784 Conn_SetOption( CONN_ID Idx, INT Option )
786 /* Option fuer Verbindung setzen.
787 * Initial sind alle Optionen _nicht_ gesetzt. */
789 assert( Idx > NONE );
790 assert( Option != 0 );
792 My_Connections[Idx].options |= Option;
793 } /* Conn_SetOption */
797 Conn_UnsetOption( CONN_ID Idx, INT Option )
799 /* Option fuer Verbindung loeschen */
801 assert( Idx > NONE );
802 assert( Option != 0 );
804 My_Connections[Idx].options &= ~Option;
805 } /* Conn_UnsetOption */
809 Conn_Options( CONN_ID Idx )
811 assert( Idx > NONE );
812 return My_Connections[Idx].options;
819 Conn_InitZip( CONN_ID Idx )
821 /* Kompression fuer Link initialisieren */
823 assert( Idx > NONE );
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;
832 if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
835 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
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;
845 if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
848 Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
852 My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
853 My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
855 Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
856 Conn_SetOption( Idx, CONN_ZIP );
865 Try_Write( CONN_ID Idx )
867 /* Versuchen, Daten aus dem Schreib-Puffer in den
868 * Socket zu schreiben. */
873 assert( Idx > NONE );
874 assert( My_Connections[Idx].sock > NONE );
875 assert( My_Connections[Idx].wdatalen > 0 );
877 /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
878 tv.tv_sec = 0; tv.tv_usec = 0;
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 )
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 );
893 if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
899 Handle_Read( INT Sock )
901 /* Aktivitaet auf einem Socket verarbeiten:
902 * - neue Clients annehmen,
903 * - Daten von Clients verarbeiten,
904 * - Resolver-Rueckmeldungen annehmen. */
908 assert( Sock > NONE );
910 if( FD_ISSET( Sock, &My_Listeners ))
912 /* es ist einer unserer Listener-Sockets: es soll
913 * also eine neue Verbindung aufgebaut werden. */
915 New_Connection( Sock );
917 else if( FD_ISSET( Sock, &Resolver_FDs ))
919 /* Rueckmeldung von einem Resolver Sub-Prozess */
921 Read_Resolver_Result( Sock );
925 /* Ein Client Socket: entweder ein User oder Server */
927 idx = Socket2Index( Sock );
928 if( idx > NONE ) Read_Request( idx );
934 Handle_Write( CONN_ID Idx )
936 /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
940 assert( Idx > NONE );
941 assert( My_Connections[Idx].sock > NONE );
943 if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
945 /* es soll nichts geschrieben werden, sondern ein
946 * connect() hat ein Ergebnis geliefert */
948 FD_CLR( My_Connections[Idx].sock, &My_Connects );
950 /* Ergebnis des connect() ermitteln */
952 res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
953 assert( len == sizeof( err ));
955 /* Fehler aufgetreten? */
956 if(( res != 0 ) || ( err != 0 ))
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 ));
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 );
967 /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
968 Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
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 );
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 );
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 );
985 assert( My_Connections[Idx].wdatalen > 0 );
987 /* Daten schreiben */
988 len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
991 /* Operation haette Socket "nur" blockiert ... */
992 if( errno == EAGAIN ) return TRUE;
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 );
1000 /* Puffer anpassen */
1001 My_Connections[Idx].wdatalen -= len;
1002 memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
1005 } /* Handle_Write */
1009 New_Connection( INT Sock )
1011 /* Neue Client-Verbindung von Listen-Socket annehmen und
1012 * CLIENT-Struktur anlegen. */
1014 struct sockaddr_in new_addr;
1015 INT new_sock, new_sock_len;
1022 assert( Sock > NONE );
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 );
1029 Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
1033 /* Socket initialisieren */
1034 Init_Socket( new_sock );
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 )
1040 new_size = Pool_Size + CONNECTION_POOL;
1042 /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
1043 * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
1045 if( Conf_MaxConnections > 0 )
1047 /* Es ist ein Limit konfiguriert */
1048 if( Pool_Size >= Conf_MaxConnections )
1050 /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
1051 Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
1055 if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
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 );
1064 /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
1065 ptr = malloc( sizeof( CONNECTION ) * new_size );
1068 /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
1069 Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
1074 /* Struktur umkopieren ... */
1075 memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
1077 Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [malloc()/memcpy()]", new_size );
1079 else Log( LOG_DEBUG, "Allocated new connection pool for %ld items. [realloc()]", new_size );
1081 My_Connections = ptr;
1082 Pool_Size = new_size;
1085 /* Client-Struktur initialisieren */
1086 c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
1089 Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1094 /* Verbindung registrieren */
1095 Init_Conn_Struct( idx );
1096 My_Connections[idx].sock = new_sock;
1097 My_Connections[idx].addr = new_addr;
1099 /* Neuen Socket registrieren */
1100 FD_SET( new_sock, &My_Sockets );
1101 if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
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 );
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 );
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;
1116 /* Penalty-Zeit setzen */
1117 Conn_SetPenalty( idx, 4 );
1118 } /* New_Connection */
1122 Socket2Index( INT Sock )
1124 /* zum Socket passende Connection suchen */
1128 assert( Sock > NONE );
1130 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1132 if( idx >= Pool_Size )
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 );
1140 } /* Socket2Index */
1144 Read_Request( CONN_ID Idx )
1146 /* Daten von Socket einlesen und entsprechend behandeln.
1147 * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1151 assert( Idx > NONE );
1152 assert( My_Connections[Idx].sock > NONE );
1155 if(( READBUFFER_LEN - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1157 if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 1 < 1 )
1160 /* Der Lesepuffer ist voll */
1161 Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1162 Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
1167 if( My_Connections[Idx].options & CONN_ZIP )
1169 len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 );
1170 if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1175 len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, READBUFFER_LEN - My_Connections[Idx].rdatalen - 1, 0 );
1176 if( len > 0 ) My_Connections[Idx].rdatalen += len;
1181 /* Socket wurde geschlossen */
1182 Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
1183 Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
1189 /* Operation haette Socket "nur" blockiert ... */
1190 if( errno == EAGAIN ) return;
1192 /* Fehler beim Lesen */
1193 Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1194 Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
1198 /* Connection-Statistik aktualisieren */
1199 My_Connections[Idx].bytes_in += len;
1201 /* Timestamp aktualisieren */
1202 My_Connections[Idx].lastdata = time( NULL );
1204 Handle_Buffer( Idx );
1205 } /* Read_Request */
1209 Handle_Buffer( CONN_ID Idx )
1211 /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1212 * Wurde ein Request verarbeitet, so wird TRUE geliefert,
1213 * ansonsten FALSE (auch bei Fehlern). */
1220 BOOLEAN action, result;
1226 /* ggf. noch unkomprimiete Daten weiter entpacken */
1227 if( My_Connections[Idx].options & CONN_ZIP )
1229 if( ! Unzip_Buffer( Idx )) return FALSE;
1233 if( My_Connections[Idx].rdatalen < 1 ) break;
1235 /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1236 * RFC 2812. Haben wir eine? */
1237 My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1238 ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1240 if( ptr ) delta = 2;
1244 /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1245 * machen soetwas viele Clients, u.a. "mIRC" :-( */
1246 ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1247 ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1249 if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1250 else if( ptr1 ) ptr = ptr1;
1251 else if( ptr2 ) ptr = ptr2;
1258 /* Ende der Anfrage wurde gefunden */
1260 len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1261 if( len > ( COMMAND_LEN - 1 ))
1263 /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1264 * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1265 * empfangen wird, wird der Client disconnectiert. */
1266 Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
1267 Conn_Close( Idx, NULL, "Request too long", TRUE );
1273 /* Es wurde ein Request gelesen */
1274 if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
1278 /* Puffer anpassen */
1279 My_Connections[Idx].rdatalen -= len;
1280 memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1283 if( action ) result = TRUE;
1287 } /* Handle_Buffer */
1291 Check_Connections( VOID )
1293 /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1294 * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1295 * auch das nicht "hilft", Client disconnectieren. */
1300 for( i = 0; i < Pool_Size; i++ )
1302 if( My_Connections[i].sock == NONE ) continue;
1304 c = Client_GetFromConn( i );
1305 if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1307 /* verbundener User, Server oder Service */
1308 if( My_Connections[i].lastping > My_Connections[i].lastdata )
1310 /* es wurde bereits ein PING gesendet */
1311 if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1314 Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1315 Conn_Close( i, NULL, "Ping timeout", TRUE );
1318 else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1320 /* es muss ein PING gesendet werden */
1321 Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1322 My_Connections[i].lastping = time( NULL );
1323 Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1328 /* noch nicht vollstaendig aufgebaute Verbindung */
1329 if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1332 Log( LOG_DEBUG, "Connection %d timed out ...", i );
1333 Conn_Close( i, NULL, "Timeout", FALSE );
1337 } /* Check_Connections */
1341 Check_Servers( VOID )
1343 /* Pruefen, ob Server-Verbindungen aufgebaut werden
1344 * muessen bzw. koennen */
1350 /* Wenn "Passive-Mode" aktiv: nicht verbinden */
1351 if( NGIRCd_Passive ) return;
1353 for( i = 0; i < Conf_Server_Count; i++ )
1355 /* Ist ein Hostname und Port definiert? */
1356 if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 )) continue;
1358 /* Haben wir schon eine Verbindung? */
1359 for( n = 0; n < Pool_Size; n++ )
1361 if( My_Connections[n].sock == NONE ) continue;
1363 /* Verbindung zu diesem Server? */
1364 if( My_Connections[n].our_server == i )
1366 /* Komplett aufgebaute Verbindung? */
1367 if( My_Connections[n].sock > NONE ) break;
1369 /* IP schon aufgeloest? */
1370 if( My_Connections[n].res_stat == NULL ) New_Server( i, n );
1373 /* Verbindung in dieser Server-Gruppe? */
1374 if(( My_Connections[n].our_server != NONE ) && ( Conf_Server[i].group != NONE ))
1376 if( Conf_Server[My_Connections[n].our_server].group == Conf_Server[i].group ) break;
1379 if( n < Pool_Size ) continue;
1381 /* Wann war der letzte Connect-Versuch? */
1382 if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1384 /* Okay, Verbindungsaufbau versuchen */
1385 Conf_Server[i].lasttry = time( NULL );
1387 /* Freie Connection-Struktur suschen */
1388 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1389 if( idx >= Pool_Size )
1391 Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1394 Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1396 /* Verbindungs-Struktur initialisieren */
1397 Init_Conn_Struct( idx );
1398 My_Connections[idx].sock = SERVER_WAIT;
1399 My_Connections[idx].our_server = i;
1401 /* Hostnamen in IP aufloesen (Default bzw. im Fehlerfall: versuchen, den
1402 * konfigurierten Text direkt als IP-Adresse zu verwenden ... */
1403 strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host );
1404 strcpy( My_Connections[idx].host, Conf_Server[i].host );
1405 s = Resolve_Name( Conf_Server[i].host );
1408 /* Sub-Prozess wurde asyncron gestartet */
1409 My_Connections[idx].res_stat = s;
1412 } /* Check_Servers */
1416 New_Server( INT Server, CONN_ID Idx )
1418 /* Neue Server-Verbindung aufbauen */
1420 struct sockaddr_in new_addr;
1421 struct in_addr inaddr;
1425 assert( Server > NONE );
1426 assert( Idx > NONE );
1428 /* Wurde eine gueltige IP-Adresse gefunden? */
1429 if( ! Conf_Server[Server].ip[0] )
1431 /* Nein. Verbindung wieder freigeben: */
1432 Init_Conn_Struct( Idx );
1433 Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1437 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 );
1439 #ifdef HAVE_INET_ATON
1440 if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1442 memset( &inaddr, 0, sizeof( inaddr ));
1443 inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1444 if( inaddr.s_addr == (unsigned)-1 )
1447 /* Konnte Adresse nicht konvertieren */
1448 Init_Conn_Struct( Idx );
1449 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 );
1453 memset( &new_addr, 0, sizeof( new_addr ));
1454 new_addr.sin_family = AF_INET;
1455 new_addr.sin_addr = inaddr;
1456 new_addr.sin_port = htons( Conf_Server[Server].port );
1458 new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1461 Init_Conn_Struct( Idx );
1462 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1466 if( ! Init_Socket( new_sock )) return;
1468 res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1469 if(( res != 0 ) && ( errno != EINPROGRESS ))
1471 Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1473 Init_Conn_Struct( Idx );
1477 /* Client-Struktur initialisieren */
1478 c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1482 Init_Conn_Struct( Idx );
1483 Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1486 Client_SetIntroducer( c, c );
1487 Client_SetToken( c, TOKEN_OUTBOUND );
1489 /* Verbindung registrieren */
1490 My_Connections[Idx].sock = new_sock;
1491 My_Connections[Idx].addr = new_addr;
1492 strcpy( My_Connections[Idx].host, Conf_Server[Server].host );
1494 /* Neuen Socket registrieren */
1495 FD_SET( new_sock, &My_Sockets );
1496 FD_SET( new_sock, &My_Connects );
1497 if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1499 Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1504 Init_Conn_Struct( LONG Idx )
1506 /* Connection-Struktur initialisieren */
1508 My_Connections[Idx].sock = NONE;
1509 My_Connections[Idx].res_stat = NULL;
1510 My_Connections[Idx].host[0] = '\0';
1511 My_Connections[Idx].rbuf[0] = '\0';
1512 My_Connections[Idx].rdatalen = 0;
1513 My_Connections[Idx].wbuf[0] = '\0';
1514 My_Connections[Idx].wdatalen = 0;
1515 My_Connections[Idx].our_server = NONE;
1516 My_Connections[Idx].lastdata = time( NULL );
1517 My_Connections[Idx].lastping = 0;
1518 My_Connections[Idx].lastprivmsg = time( NULL );
1519 My_Connections[Idx].delaytime = 0;
1520 My_Connections[Idx].bytes_in = 0;
1521 My_Connections[Idx].bytes_out = 0;
1522 My_Connections[Idx].flag = 0;
1523 My_Connections[Idx].options = 0;
1526 My_Connections[Idx].zip.rbuf[0] = '\0';
1527 My_Connections[Idx].zip.rdatalen = 0;
1528 My_Connections[Idx].zip.wbuf[0] = '\0';
1529 My_Connections[Idx].zip.wdatalen = 0;
1530 My_Connections[Idx].zip.bytes_in = 0;
1531 My_Connections[Idx].zip.bytes_out = 0;
1533 } /* Init_Conn_Struct */
1537 Init_Socket( INT Sock )
1539 /* Socket-Optionen setzen */
1543 #ifdef O_NONBLOCK /* A/UX kennt das nicht? */
1544 if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1546 Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1551 if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1553 Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1554 /* dieser Fehler kann ignoriert werden. */
1562 Read_Resolver_Result( INT r_fd )
1564 /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1565 * und entsprechende Connection aktualisieren */
1567 CHAR result[HOST_LEN];
1571 FD_CLR( r_fd, &Resolver_FDs );
1573 /* Anfrage vom Parent lesen */
1574 len = read( r_fd, result, HOST_LEN - 1 );
1577 /* Fehler beim Lesen aus der Pipe */
1579 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1584 /* zugehoerige Connection suchen */
1585 for( i = 0; i < Pool_Size; i++ )
1587 if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break;
1589 if( i >= Pool_Size )
1591 /* Opsa! Keine passende Connection gefunden!? Vermutlich
1592 * wurde sie schon wieder geschlossen. */
1594 Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1598 Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
1601 close( My_Connections[i].res_stat->pipe[0] );
1602 close( My_Connections[i].res_stat->pipe[1] );
1603 free( My_Connections[i].res_stat );
1604 My_Connections[i].res_stat = NULL;
1606 if( My_Connections[i].sock > NONE )
1608 /* Eingehende Verbindung: Hostnamen setzen */
1609 c = Client_GetFromConn( i );
1610 assert( c != NULL );
1611 strcpy( My_Connections[i].host, result );
1612 Client_SetHostname( c, result );
1614 Conn_WriteStr( i, "NOTICE AUTH :%sGot your hostname.", NOTICE_TXTPREFIX );
1618 /* Ausgehende Verbindung (=Server): IP setzen */
1619 assert( My_Connections[i].our_server > NONE );
1620 strcpy( Conf_Server[My_Connections[i].our_server].ip, result );
1623 /* Penalty-Zeit zurueck setzen */
1624 Conn_ResetPenalty( i );
1625 } /* Read_Resolver_Result */
1631 Zip_Buffer( CONN_ID Idx, CHAR *Data, INT Len )
1633 /* Daten zum Komprimieren im "Kompressions-Puffer" sammeln.
1634 * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
1636 assert( Idx > NONE );
1637 assert( Data != NULL );
1640 /* Ist noch Platz im Kompressions-Puffer? */
1641 if( ZWRITEBUFFER_LEN - My_Connections[Idx].zip.wdatalen < Len + 50 )
1643 /* Nein! Puffer zunaechst leeren ...*/
1644 if( ! Zip_Flush( Idx )) return FALSE;
1647 /* Daten kopieren */
1648 memmove( My_Connections[Idx].zip.wbuf + My_Connections[Idx].zip.wdatalen, Data, Len );
1649 My_Connections[Idx].zip.wdatalen += Len;
1656 Zip_Flush( CONN_ID Idx )
1658 /* Daten komprimieren und in Schreibpuffer kopieren.
1659 * Es wird TRUE bei Erfolg, sonst FALSE geliefert. */
1661 INT result, out_len;
1664 out = &My_Connections[Idx].zip.out;
1666 out->next_in = My_Connections[Idx].zip.wbuf;
1667 out->avail_in = My_Connections[Idx].zip.wdatalen;
1668 out->next_out = My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen;
1669 out->avail_out = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen;
1671 result = deflate( out, Z_SYNC_FLUSH );
1672 if(( result != Z_OK ) || ( out->avail_in > 0 ))
1674 Log( LOG_ALERT, "Compression error: code %d!?", result );
1675 Conn_Close( Idx, "Compression error!", NULL, FALSE );
1679 out_len = WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - out->avail_out;
1680 My_Connections[Idx].wdatalen += out_len;
1681 My_Connections[Idx].bytes_out += out_len;
1682 My_Connections[Idx].zip.bytes_out += My_Connections[Idx].zip.wdatalen;
1683 My_Connections[Idx].zip.wdatalen = 0;
1690 Unzip_Buffer( CONN_ID Idx )
1692 /* Daten entpacken und in Lesepuffer kopieren. Bei Fehlern
1693 * wird FALSE geliefert, ansonsten TRUE. Der Fall, dass keine
1694 * Daten mehr zu entpacken sind, ist _kein_ Fehler! */
1696 INT result, in_len, out_len;
1699 assert( Idx > NONE );
1701 if( My_Connections[Idx].zip.rdatalen <= 0 ) return TRUE;
1703 in = &My_Connections[Idx].zip.in;
1705 in->next_in = My_Connections[Idx].zip.rbuf;
1706 in->avail_in = My_Connections[Idx].zip.rdatalen;
1707 in->next_out = My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen;
1708 in->avail_out = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1;
1710 result = inflate( in, Z_SYNC_FLUSH );
1711 if( result != Z_OK )
1713 Log( LOG_ALERT, "Decompression error: code %d!?", result );
1714 Conn_Close( Idx, "Decompression error!", NULL, FALSE );
1718 in_len = My_Connections[Idx].zip.rdatalen - in->avail_in;
1719 out_len = READBUFFER_LEN - My_Connections[Idx].rdatalen - 1 - in->avail_out;
1720 My_Connections[Idx].rdatalen += out_len;
1722 if( in->avail_in > 0 )
1724 /* es konnten nicht alle Daten entpackt werden, vermutlich war
1725 * im Ziel-Puffer kein Platz mehr. Umkopieren ... */
1726 My_Connections[Idx].zip.rdatalen -= in_len;
1727 memmove( My_Connections[Idx].zip.rbuf, My_Connections[Idx].zip.rbuf + in_len, My_Connections[Idx].zip.rdatalen );
1729 else My_Connections[Idx].zip.rdatalen = 0;
1730 My_Connections[Idx].zip.bytes_in += out_len;
1733 } /* Unzip_Buffer */