2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
11 * Connection management
19 static char UNUSED id[] = "$Id: conn.c,v 1.112 2002/12/30 16:07:23 alex Exp $";
30 #include <sys/socket.h>
32 #include <sys/types.h>
34 #include <netinet/in.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
39 #define PF_INET AF_INET
43 #include <stdint.h> /* u.a. fuer Mac OS X */
64 #define SERVER_WAIT (NONE - 1)
67 LOCAL VOID Handle_Read PARAMS(( INT sock ));
68 LOCAL BOOLEAN Handle_Write PARAMS(( CONN_ID Idx ));
69 LOCAL VOID New_Connection PARAMS(( INT Sock ));
70 LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
71 LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
72 LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
73 LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
74 LOCAL VOID Check_Connections PARAMS(( VOID ));
75 LOCAL VOID Check_Servers PARAMS(( VOID ));
76 LOCAL VOID Init_Conn_Struct PARAMS(( CONN_ID Idx ));
77 LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock ));
78 LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx ));
79 LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd ));
81 LOCAL fd_set My_Listeners;
82 LOCAL fd_set My_Sockets;
83 LOCAL fd_set My_Connects;
91 /* Modul initialisieren: statische Strukturen "ausnullen". */
95 /* Speicher fuer Verbindungs-Pool anfordern */
96 Pool_Size = CONNECTION_POOL;
97 if( Conf_MaxConnections > 0 )
99 /* konfiguriertes Limit beachten */
100 if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
102 My_Connections = malloc( sizeof( CONNECTION ) * Pool_Size );
103 if( ! My_Connections )
105 /* Speicher konnte nicht alloziert werden! */
106 Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
109 Log( LOG_DEBUG, "Allocted connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
111 /* zu Beginn haben wir keine Verbindungen */
112 FD_ZERO( &My_Listeners );
113 FD_ZERO( &My_Sockets );
114 FD_ZERO( &My_Connects );
116 /* Groesster File-Descriptor fuer select() */
119 /* Connection-Struktur initialisieren */
120 for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
122 /* Global write counter */
130 /* Modul abmelden: alle noch offenen Connections
131 * schliessen und freigeben. */
136 /* Sockets schliessen */
137 Log( LOG_DEBUG, "Shutting down all connections ..." );
138 for( i = 0; i < Conn_MaxFD + 1; i++ )
140 if( FD_ISSET( i, &My_Sockets ))
142 for( idx = 0; idx < Pool_Size; idx++ )
144 if( My_Connections[idx].sock == i ) break;
146 if( FD_ISSET( i, &My_Listeners ))
149 Log( LOG_DEBUG, "Listening socket %d closed.", i );
151 else if( FD_ISSET( i, &My_Connects ))
154 Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
156 else if( idx < Pool_Size )
158 if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
159 else Conn_Close( idx, NULL, "Server going down", TRUE );
163 Log( LOG_WARNING, "Closing unknown connection %d ...", i );
169 free( My_Connections );
170 My_Connections = NULL;
176 Conn_InitListeners( VOID )
178 /* Initialize ports on which the server should accept connections */
183 for( i = 0; i < Conf_ListenPorts_Count; i++ )
185 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
186 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
189 } /* Conn_InitListeners */
193 Conn_ExitListeners( VOID )
195 /* Close down all listening sockets */
199 Log( LOG_INFO, "Shutting down all listening sockets ..." );
200 for( i = 0; i < Conn_MaxFD + 1; i++ )
202 if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
205 Log( LOG_DEBUG, "Listening socket %d closed.", i );
208 } /* Conn_ExitListeners */
212 Conn_NewListener( CONST UINT Port )
214 /* Create new listening socket on specified port */
216 struct sockaddr_in addr;
219 /* Server-"Listen"-Socket initialisieren */
220 memset( &addr, 0, sizeof( addr ));
221 addr.sin_family = AF_INET;
222 addr.sin_port = htons( Port );
223 addr.sin_addr.s_addr = htonl( INADDR_ANY );
225 /* Socket erzeugen */
226 sock = socket( PF_INET, SOCK_STREAM, 0);
229 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
233 if( ! Init_Socket( sock )) return FALSE;
236 if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
238 Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
243 /* in "listen mode" gehen :-) */
244 if( listen( sock, 10 ) != 0 )
246 Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
251 /* Neuen Listener in Strukturen einfuegen */
252 FD_SET( sock, &My_Listeners );
253 FD_SET( sock, &My_Sockets );
255 if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
257 Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
260 } /* Conn_NewListener */
266 /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
267 * werden dabei durchgefuehrt, bis der Server terminieren oder neu
270 * - neue Verbindungen annehmen,
271 * - Server-Verbindungen aufbauen,
272 * - geschlossene Verbindungen loeschen,
273 * - volle Schreibpuffer versuchen zu schreiben,
274 * - volle Lesepuffer versuchen zu verarbeiten,
275 * - Antworten von Resolver Sub-Prozessen annehmen.
278 fd_set read_sockets, write_sockets;
284 start = time( NULL );
285 while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart ))
289 /* Should the configuration be reloaded? */
290 if( NGIRCd_SignalRehash ) NGIRCd_Rehash( );
292 /* Check configured servers and established links */
294 Check_Connections( );
296 /* noch volle Lese-Buffer suchen */
297 for( i = 0; i < Pool_Size; i++ )
299 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
301 /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
302 if( Handle_Buffer( i )) timeout = FALSE;
306 /* noch volle Schreib-Puffer suchen */
307 FD_ZERO( &write_sockets );
308 for( i = 0; i < Pool_Size; i++ )
311 if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
313 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
316 /* Socket der Verbindung in Set aufnehmen */
317 FD_SET( My_Connections[i].sock, &write_sockets );
321 /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
322 for( i = 0; i < Pool_Size; i++ )
324 if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects ))) FD_SET( My_Connections[i].sock, &write_sockets );
327 /* von welchen Sockets koennte gelesen werden? */
329 read_sockets = My_Sockets;
330 for( i = 0; i < Pool_Size; i++ )
332 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
334 /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
335 FD_CLR( My_Connections[i].sock, &read_sockets );
337 if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
339 /* Hier laeuft noch ein asyncrones connect() */
340 FD_CLR( My_Connections[i].sock, &read_sockets );
342 if( My_Connections[i].delaytime > t )
344 /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
345 FD_CLR( My_Connections[i].sock, &read_sockets );
348 for( i = 0; i < Conn_MaxFD + 1; i++ )
350 /* Pipes von Resolver Sub-Prozessen aufnehmen */
351 if( FD_ISSET( i, &Resolver_FDs ))
353 FD_SET( i, &read_sockets );
357 /* Timeout initialisieren */
359 if( timeout ) tv.tv_sec = TIME_RES;
362 /* Auf Aktivitaet warten */
363 i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
366 /* keine Veraenderung an den Sockets */
371 /* Fehler (z.B. Interrupt) */
374 Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
375 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
381 /* Koennen Daten geschrieben werden? */
382 for( i = 0; i < Conn_MaxFD + 1; i++ )
384 if( ! FD_ISSET( i, &write_sockets )) continue;
386 /* Es kann geschrieben werden ... */
387 idx = Socket2Index( i );
388 if( idx == NONE ) continue;
390 if( ! Handle_Write( idx ))
392 /* Fehler beim Schreiben! Diesen Socket nun
393 * auch aus dem Read-Set entfernen: */
394 FD_CLR( i, &read_sockets );
398 /* Daten zum Lesen vorhanden? */
399 for( i = 0; i < Conn_MaxFD + 1; i++ )
401 if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
405 if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
406 else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
412 Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
415 Conn_WriteStr( Idx, Format, va_alist )
421 /* String in Socket schreiben. CR+LF wird von dieser Funktion
422 * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
423 * getrennt und FALSE geliefert. */
425 CHAR buffer[COMMAND_LEN];
429 assert( Idx > NONE );
430 assert( Format != NULL );
433 va_start( ap, Format );
437 if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
439 Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
440 Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
445 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
448 strlcat( buffer, "\r\n", sizeof( buffer ));
449 ok = Conn_Write( Idx, buffer, strlen( buffer ));
450 My_Connections[Idx].msg_out++;
454 } /* Conn_WriteStr */
458 Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
460 /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
461 * der Client disconnectiert und FALSE geliefert. */
463 assert( Idx > NONE );
464 assert( Data != NULL );
467 /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
468 * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
469 * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
470 * In diesem Fall wird hier einfach ein Fehler geliefert. */
471 if( My_Connections[Idx].sock <= NONE )
473 Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
477 /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
478 * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
479 * Socket zu schreiben (u.a. wg. Komprimierung). */
480 if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
482 /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
483 * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
484 if( ! Try_Write( Idx )) return FALSE;
486 /* nun neu pruefen: */
487 if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
489 Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
490 Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
496 if( My_Connections[Idx].options & CONN_ZIP )
498 /* Daten komprimieren und in Puffer kopieren */
499 if( ! Zip_Buffer( Idx, Data, Len )) return FALSE;
504 /* Daten in Puffer kopieren */
505 memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
506 My_Connections[Idx].wdatalen += Len;
507 My_Connections[Idx].bytes_out += Len;
510 /* Adjust global write counter */
518 Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
520 /* Close connection. Open pipes of asyncronous resolver
521 * sub-processes are closed down. */
526 DOUBLE in_z_k, out_z_k;
530 assert( Idx > NONE );
531 assert( My_Connections[Idx].sock > NONE );
533 /* Search client, if any */
534 c = Client_GetFromConn( Idx );
536 /* Should the client be informed? */
540 /* Send statistics to client if registered as user: */
541 if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
543 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 );
547 /* Send ERROR to client (see RFC!) */
548 if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
549 else Conn_WriteStr( Idx, "ERROR :Closing connection." );
550 if( My_Connections[Idx].sock == NONE ) return;
553 /* Try to write out the write buffer */
556 /* Shut down socket */
557 if( close( My_Connections[Idx].sock ) != 0 )
559 /* Oops, we can't close the socket!? This is fatal! */
560 Log( LOG_EMERG, "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 ));
561 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
565 /* Mark socket as invalid: */
566 FD_CLR( My_Connections[Idx].sock, &My_Sockets );
567 FD_CLR( My_Connections[Idx].sock, &My_Connects );
568 My_Connections[Idx].sock = NONE;
570 /* If there is still a client, unregister it now */
571 if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
573 /* Calculate statistics and log information */
574 in_k = (DOUBLE)My_Connections[Idx].bytes_in / 1024;
575 out_k = (DOUBLE)My_Connections[Idx].bytes_out / 1024;
577 if( My_Connections[Idx].options & CONN_ZIP )
579 in_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_in / 1024;
580 out_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_out / 1024;
581 in_p = (INT)(( in_k * 100 ) / in_z_k );
582 out_p = (INT)(( out_k * 100 ) / out_z_k );
583 Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).", Idx, 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 );
588 Log( LOG_INFO, "Connection %d with %s:%d closed (in: %.1fk, out: %.1fk).", Idx, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), in_k, out_k );
591 /* Is there a resolver sub-process running? */
592 if( My_Connections[Idx].res_stat )
594 /* Free resolver structures */
595 FD_CLR( My_Connections[Idx].res_stat->pipe[0], &Resolver_FDs );
596 close( My_Connections[Idx].res_stat->pipe[0] );
597 close( My_Connections[Idx].res_stat->pipe[1] );
598 free( My_Connections[Idx].res_stat );
601 /* Servers: Modify time of next connect attempt? */
602 Conf_UnsetServer( Idx );
605 /* Clean up zlib, if link was compressed */
606 if( Conn_Options( Idx ) & CONN_ZIP )
608 inflateEnd( &My_Connections[Idx].zip.in );
609 deflateEnd( &My_Connections[Idx].zip.out );
613 /* Clean up connection structure (=free it) */
614 Init_Conn_Struct( Idx );
619 Conn_UpdateIdle( CONN_ID Idx )
621 /* Idle-Timer zuruecksetzen */
623 assert( Idx > NONE );
624 My_Connections[Idx].lastprivmsg = time( NULL );
629 Conn_GetIdle( CONN_ID Idx )
631 /* Idle-Time einer Verbindung liefern (in Sekunden) */
633 assert( Idx > NONE );
634 return time( NULL ) - My_Connections[Idx].lastprivmsg;
639 Conn_LastPing( CONN_ID Idx )
641 /* Zeitpunkt des letzten PING liefern */
643 assert( Idx > NONE );
644 return My_Connections[Idx].lastping;
645 } /* Conn_LastPing */
649 Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
651 /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
652 * waehrend dieser Zeit wird der entsprechende Socket vom Server
653 * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
654 * dieser Funktion nur erhoeht, nicht aber verringert werden. */
658 assert( Idx > NONE );
659 assert( Seconds >= 0 );
661 t = time( NULL ) + Seconds;
662 if( t > My_Connections[Idx].delaytime ) My_Connections[Idx].delaytime = t;
663 } /* Conn_SetPenalty */
667 Conn_ResetPenalty( CONN_ID Idx )
669 assert( Idx > NONE );
670 My_Connections[Idx].delaytime = 0;
671 } /* Conn_ResetPenalty */
675 Conn_ClearFlags( VOID )
677 /* Alle Connection auf "nicht-markiert" setzen */
681 for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
682 } /* Conn_ClearFlags */
686 Conn_Flag( CONN_ID Idx )
688 /* Ist eine Connection markiert (TRUE) oder nicht? */
690 assert( Idx > NONE );
691 return My_Connections[Idx].flag;
696 Conn_SetFlag( CONN_ID Idx, INT Flag )
698 /* Connection markieren */
700 assert( Idx > NONE );
701 My_Connections[Idx].flag = Flag;
708 /* Connection-Struktur der ersten Verbindung liefern;
709 * Ist keine Verbindung vorhanden, wird NONE geliefert. */
713 for( i = 0; i < Pool_Size; i++ )
715 if( My_Connections[i].sock != NONE ) return i;
722 Conn_Next( CONN_ID Idx )
724 /* Naechste Verbindungs-Struktur liefern; existiert keine
725 * weitere, so wird NONE geliefert. */
729 assert( Idx > NONE );
731 for( i = Idx + 1; i < Pool_Size; i++ )
733 if( My_Connections[i].sock != NONE ) return i;
740 Conn_SetOption( CONN_ID Idx, INT Option )
742 /* Option fuer Verbindung setzen.
743 * Initial sind alle Optionen _nicht_ gesetzt. */
745 assert( Idx > NONE );
746 assert( Option != 0 );
748 My_Connections[Idx].options |= Option;
749 } /* Conn_SetOption */
753 Conn_UnsetOption( CONN_ID Idx, INT Option )
755 /* Option fuer Verbindung loeschen */
757 assert( Idx > NONE );
758 assert( Option != 0 );
760 My_Connections[Idx].options &= ~Option;
761 } /* Conn_UnsetOption */
765 Conn_Options( CONN_ID Idx )
767 assert( Idx > NONE );
768 return My_Connections[Idx].options;
773 Conn_StartTime( CONN_ID Idx )
775 /* Zeitpunkt des Link-Starts liefern (in Sekunden) */
777 assert( Idx > NONE );
778 return My_Connections[Idx].starttime;
783 Conn_SendQ( CONN_ID Idx )
785 /* Laenge der Daten im Schreibbuffer liefern */
787 assert( Idx > NONE );
789 if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.wdatalen;
792 return My_Connections[Idx].wdatalen;
797 Conn_SendMsg( CONN_ID Idx )
799 /* Anzahl gesendeter Nachrichten liefern */
801 assert( Idx > NONE );
802 return My_Connections[Idx].msg_out;
807 Conn_SendBytes( CONN_ID Idx )
809 /* Anzahl gesendeter Bytes (unkomprimiert) liefern */
811 assert( Idx > NONE );
812 return My_Connections[Idx].bytes_out;
813 } /* Conn_SendBytes */
817 Conn_RecvQ( CONN_ID Idx )
819 /* Laenge der Daten im Lesebuffer liefern */
821 assert( Idx > NONE );
823 if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.rdatalen;
826 return My_Connections[Idx].rdatalen;
831 Conn_RecvMsg( CONN_ID Idx )
833 /* Anzahl empfangener Nachrichten liefern */
835 assert( Idx > NONE );
836 return My_Connections[Idx].msg_in;
841 Conn_RecvBytes( CONN_ID Idx )
843 /* Anzahl empfangener Bytes (unkomprimiert) liefern */
845 assert( Idx > NONE );
846 return My_Connections[Idx].bytes_in;
847 } /* Conn_RecvBytes */
851 Conn_ResetWCounter( VOID )
854 } /* Conn_ResetWCounter */
858 Conn_WCounter( VOID )
861 } /* Conn_WCounter */
865 Try_Write( CONN_ID Idx )
867 /* Versuchen, Daten aus dem Schreib-Puffer in den Socket zu
868 * schreiben. TRUE wird geliefert, wenn entweder keine Daten
869 * zum Versenden vorhanden sind oder erfolgreich bearbeitet
870 * werden konnten. Im Fehlerfall wird FALSE geliefert und
871 * die Verbindung geschlossen. */
876 assert( Idx > NONE );
877 assert( My_Connections[Idx].sock > NONE );
879 /* sind ueberhaupt Daten vorhanden? */
881 if(( ! My_Connections[Idx].wdatalen > 0 ) && ( ! My_Connections[Idx].zip.wdatalen )) return TRUE;
883 if( ! My_Connections[Idx].wdatalen > 0 ) return TRUE;
886 /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
887 tv.tv_sec = 0; tv.tv_usec = 0;
889 FD_ZERO( &write_socket );
890 FD_SET( My_Connections[Idx].sock, &write_socket );
891 if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
896 Log( LOG_ALERT, "Try_Write(): select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
897 Conn_Close( Idx, "Server error!", NULL, FALSE );
902 if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
908 Handle_Read( INT Sock )
910 /* Aktivitaet auf einem Socket verarbeiten:
911 * - neue Clients annehmen,
912 * - Daten von Clients verarbeiten,
913 * - Resolver-Rueckmeldungen annehmen. */
917 assert( Sock > NONE );
919 if( FD_ISSET( Sock, &My_Listeners ))
921 /* es ist einer unserer Listener-Sockets: es soll
922 * also eine neue Verbindung aufgebaut werden. */
924 New_Connection( Sock );
926 else if( FD_ISSET( Sock, &Resolver_FDs ))
928 /* Rueckmeldung von einem Resolver Sub-Prozess */
930 Read_Resolver_Result( Sock );
934 /* Ein Client Socket: entweder ein User oder Server */
936 idx = Socket2Index( Sock );
937 if( idx > NONE ) Read_Request( idx );
943 Handle_Write( CONN_ID Idx )
945 /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
949 assert( Idx > NONE );
950 assert( My_Connections[Idx].sock > NONE );
952 if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
954 /* es soll nichts geschrieben werden, sondern ein
955 * connect() hat ein Ergebnis geliefert */
957 FD_CLR( My_Connections[Idx].sock, &My_Connects );
959 /* Ergebnis des connect() ermitteln */
961 res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
962 assert( len == sizeof( err ));
964 /* Fehler aufgetreten? */
965 if(( res != 0 ) || ( err != 0 ))
968 if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
969 else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port, Idx, strerror( err ));
971 /* Socket etc. pp. aufraeumen */
972 FD_CLR( My_Connections[Idx].sock, &My_Sockets );
973 close( My_Connections[Idx].sock );
974 Init_Conn_Struct( Idx );
976 /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
977 Conf_Server[Conf_GetServer( Idx )].lasttry = time( NULL );
978 Conf_UnsetServer( Idx );
982 Log( LOG_DEBUG, "Connection %d with \"%s:%d\" established, now sendig PASS and SERVER ...", Idx, My_Connections[Idx].host, Conf_Server[Conf_GetServer( Idx )].port );
984 /* PASS und SERVER verschicken */
985 Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[Conf_GetServer( Idx )].pwd_out, NGIRCd_ProtoID );
986 return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
990 /* Schreibpuffer leer, aber noch Daten im Kompressionsbuffer?
991 * Dann muss dieser nun geflushed werden! */
992 if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
995 assert( My_Connections[Idx].wdatalen > 0 );
997 /* Daten schreiben */
998 len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
1001 /* Operation haette Socket "nur" blockiert ... */
1002 if( errno == EAGAIN ) return TRUE;
1004 /* Oops, ein Fehler! */
1005 Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1006 Conn_Close( Idx, "Write error!", NULL, FALSE );
1010 /* Puffer anpassen */
1011 My_Connections[Idx].wdatalen -= len;
1012 memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
1015 } /* Handle_Write */
1019 New_Connection( INT Sock )
1021 /* Neue Client-Verbindung von Listen-Socket annehmen und
1022 * CLIENT-Struktur anlegen. */
1024 struct sockaddr_in new_addr;
1025 INT new_sock, new_sock_len;
1032 assert( Sock > NONE );
1034 /* Connection auf Listen-Socket annehmen */
1035 new_sock_len = sizeof( new_addr );
1036 new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
1039 Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
1043 /* Socket initialisieren */
1044 Init_Socket( new_sock );
1046 /* Freie Connection-Struktur suchen */
1047 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1048 if( idx >= Pool_Size )
1050 new_size = Pool_Size + CONNECTION_POOL;
1052 /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
1053 * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
1055 if( Conf_MaxConnections > 0 )
1057 /* Es ist ein Limit konfiguriert */
1058 if( Pool_Size >= Conf_MaxConnections )
1060 /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
1061 Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
1065 if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
1067 if( new_size < Pool_Size )
1069 Log( LOG_ALERT, "Can't accespt connection: limit (%d) reached -- overflow!", Pool_Size );
1074 /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
1075 * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
1076 * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
1077 ptr = realloc( My_Connections, sizeof( CONNECTION ) * new_size );
1080 /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
1081 ptr = malloc( sizeof( CONNECTION ) * new_size );
1084 /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
1085 Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
1090 /* Struktur umkopieren ... */
1091 memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
1093 Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [malloc()/memcpy()]", new_size, sizeof( CONNECTION ) * new_size );
1095 else Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
1097 /* Adjust pointer to new block */
1098 My_Connections = ptr;
1100 /* Initialize new items */
1101 for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
1104 /* Adjust new pool size */
1105 Pool_Size = new_size;
1108 /* Client-Struktur initialisieren */
1109 c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
1112 Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1117 /* Verbindung registrieren */
1118 Init_Conn_Struct( idx );
1119 My_Connections[idx].sock = new_sock;
1120 My_Connections[idx].addr = new_addr;
1122 /* Neuen Socket registrieren */
1123 FD_SET( new_sock, &My_Sockets );
1124 if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1126 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 );
1128 /* Hostnamen ermitteln */
1129 strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host ));
1130 Client_SetHostname( c, My_Connections[idx].host );
1131 s = Resolve_Addr( &new_addr );
1134 /* Sub-Prozess wurde asyncron gestartet */
1135 My_Connections[idx].res_stat = s;
1138 /* Penalty-Zeit setzen */
1139 Conn_SetPenalty( idx, 4 );
1140 } /* New_Connection */
1144 Socket2Index( INT Sock )
1146 /* zum Socket passende Connection suchen */
1150 assert( Sock > NONE );
1152 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1154 if( idx >= Pool_Size )
1156 /* die Connection wurde vermutlich (wegen eines
1157 * Fehlers) bereits wieder abgebaut ... */
1158 Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1162 } /* Socket2Index */
1166 Read_Request( CONN_ID Idx )
1168 /* Daten von Socket einlesen und entsprechend behandeln.
1169 * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1176 assert( Idx > NONE );
1177 assert( My_Connections[Idx].sock > NONE );
1179 /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
1180 * ansonsten koennen Daten ggf. nicht umkopiert werden. */
1181 bsize = READBUFFER_LEN;
1183 c = Client_GetFromConn( Idx );
1184 if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) && ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN )) bsize = ZREADBUFFER_LEN;
1188 if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1190 if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
1193 /* Der Lesepuffer ist voll */
1194 Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1195 Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
1200 if( My_Connections[Idx].options & CONN_ZIP )
1202 len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 );
1203 if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1208 len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, bsize - My_Connections[Idx].rdatalen - 1, 0 );
1209 if( len > 0 ) My_Connections[Idx].rdatalen += len;
1214 /* Socket wurde geschlossen */
1215 Log( LOG_INFO, "%s:%d (%s) is closing the connection ...", My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), inet_ntoa( My_Connections[Idx].addr.sin_addr ));
1216 Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
1222 /* Operation haette Socket "nur" blockiert ... */
1223 if( errno == EAGAIN ) return;
1225 /* Fehler beim Lesen */
1226 Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1227 Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
1231 /* Connection-Statistik aktualisieren */
1232 My_Connections[Idx].bytes_in += len;
1234 /* Timestamp aktualisieren */
1235 My_Connections[Idx].lastdata = time( NULL );
1237 Handle_Buffer( Idx );
1238 } /* Read_Request */
1242 Handle_Buffer( CONN_ID Idx )
1244 /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1245 * Wurde ein Request verarbeitet, so wird TRUE geliefert,
1246 * ansonsten FALSE (auch bei Fehlern). */
1253 BOOLEAN action, result;
1262 /* ggf. noch unkomprimiete Daten weiter entpacken */
1263 if( My_Connections[Idx].options & CONN_ZIP )
1265 if( ! Unzip_Buffer( Idx )) return FALSE;
1269 if( My_Connections[Idx].rdatalen < 1 ) break;
1271 /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1272 * RFC 2812. Haben wir eine? */
1273 My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1274 ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1276 if( ptr ) delta = 2;
1280 /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1281 * machen soetwas viele Clients, u.a. "mIRC" :-( */
1282 ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1283 ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1285 if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1286 else if( ptr1 ) ptr = ptr1;
1287 else if( ptr2 ) ptr = ptr2;
1294 /* Ende der Anfrage wurde gefunden */
1296 len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1297 if( len > ( COMMAND_LEN - 1 ))
1299 /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1300 * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1301 * empfangen wird, wird der Client disconnectiert. */
1302 Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
1303 Conn_Close( Idx, NULL, "Request too long", TRUE );
1308 /* merken, ob Stream bereits komprimiert wird */
1309 old_z = My_Connections[Idx].options & CONN_ZIP;
1314 /* Es wurde ein Request gelesen */
1315 My_Connections[Idx].msg_in++;
1316 if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
1320 /* Puffer anpassen */
1321 My_Connections[Idx].rdatalen -= len;
1322 memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1325 if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1327 /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1328 * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1329 * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1331 if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1333 /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1334 Log( LOG_ALERT, "Can't move read buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
1337 memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1338 My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1339 My_Connections[Idx].rdatalen = 0;
1340 Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1346 if( action ) result = TRUE;
1350 } /* Handle_Buffer */
1354 Check_Connections( VOID )
1356 /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1357 * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1358 * auch das nicht "hilft", Client disconnectieren. */
1363 for( i = 0; i < Pool_Size; i++ )
1365 if( My_Connections[i].sock == NONE ) continue;
1367 c = Client_GetFromConn( i );
1368 if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1370 /* verbundener User, Server oder Service */
1371 if( My_Connections[i].lastping > My_Connections[i].lastdata )
1373 /* es wurde bereits ein PING gesendet */
1374 if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1377 Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1378 Conn_Close( i, NULL, "Ping timeout", TRUE );
1381 else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1383 /* es muss ein PING gesendet werden */
1384 Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1385 My_Connections[i].lastping = time( NULL );
1386 Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1391 /* noch nicht vollstaendig aufgebaute Verbindung */
1392 if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1395 Log( LOG_DEBUG, "Connection %d timed out ...", i );
1396 Conn_Close( i, NULL, "Timeout", FALSE );
1400 } /* Check_Connections */
1404 Check_Servers( VOID )
1406 /* Check if we can establish further server links */
1412 /* Don't connect in "passive mode" */
1413 if( NGIRCd_Passive ) return;
1415 /* Serach all connections, are there results from the resolver? */
1416 for( idx = 0; idx < Pool_Size; idx++ )
1418 if( My_Connections[idx].sock != SERVER_WAIT ) continue;
1421 if( My_Connections[idx].res_stat == NULL ) New_Server( Conf_GetServer( idx ), idx );
1424 /* Check all configured servers */
1425 for( i = 0; i < MAX_SERVERS; i++ )
1427 /* Valid outgoing server which isn't already connected? */
1428 if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) || ( Conf_Server[i].conn_id > NONE )) continue;
1430 /* Is there already a connection in this group? */
1431 if( Conf_Server[i].group > NONE )
1433 for( n = 0; n < MAX_SERVERS; n++ )
1435 if( n == i ) continue;
1436 if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
1438 if( n < MAX_SERVERS ) continue;
1441 /* Check last connect attempt? */
1442 if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1444 /* Okay, try to connect now */
1445 Conf_Server[i].lasttry = time( NULL );
1447 /* Search free connection structure */
1448 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1449 if( idx >= Pool_Size )
1451 Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1454 Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1456 /* Verbindungs-Struktur initialisieren */
1457 Init_Conn_Struct( idx );
1458 My_Connections[idx].sock = SERVER_WAIT;
1459 Conf_Server[i].conn_id = idx;
1461 /* Resolve Hostname. If this fails, try to use it as an IP address */
1462 strlcpy( Conf_Server[i].ip, Conf_Server[i].host, sizeof( Conf_Server[i].ip ));
1463 strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host ));
1464 s = Resolve_Name( Conf_Server[i].host );
1467 /* Sub-Prozess wurde asyncron gestartet */
1468 My_Connections[idx].res_stat = s;
1471 } /* Check_Servers */
1475 New_Server( INT Server, CONN_ID Idx )
1477 /* Neue Server-Verbindung aufbauen */
1479 struct sockaddr_in new_addr;
1480 struct in_addr inaddr;
1484 assert( Server > NONE );
1485 assert( Idx > NONE );
1487 /* Wurde eine gueltige IP-Adresse gefunden? */
1488 if( ! Conf_Server[Server].ip[0] )
1490 /* Nein. Verbindung wieder freigeben: */
1491 Init_Conn_Struct( Idx );
1492 Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1496 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 );
1498 #ifdef HAVE_INET_ATON
1499 if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1501 memset( &inaddr, 0, sizeof( inaddr ));
1502 inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1503 if( inaddr.s_addr == (unsigned)-1 )
1506 /* Konnte Adresse nicht konvertieren */
1507 Init_Conn_Struct( Idx );
1508 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 );
1512 memset( &new_addr, 0, sizeof( new_addr ));
1513 new_addr.sin_family = AF_INET;
1514 new_addr.sin_addr = inaddr;
1515 new_addr.sin_port = htons( Conf_Server[Server].port );
1517 new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1520 Init_Conn_Struct( Idx );
1521 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1525 if( ! Init_Socket( new_sock )) return;
1527 res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1528 if(( res != 0 ) && ( errno != EINPROGRESS ))
1530 Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1532 Init_Conn_Struct( Idx );
1536 /* Client-Struktur initialisieren */
1537 c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1541 Init_Conn_Struct( Idx );
1542 Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1545 Client_SetIntroducer( c, c );
1546 Client_SetToken( c, TOKEN_OUTBOUND );
1548 /* Verbindung registrieren */
1549 My_Connections[Idx].sock = new_sock;
1550 My_Connections[Idx].addr = new_addr;
1551 strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
1553 /* Neuen Socket registrieren */
1554 FD_SET( new_sock, &My_Sockets );
1555 FD_SET( new_sock, &My_Connects );
1556 if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1558 Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1563 Init_Conn_Struct( CONN_ID Idx )
1565 /* Connection-Struktur initialisieren */
1567 My_Connections[Idx].sock = NONE;
1568 My_Connections[Idx].res_stat = NULL;
1569 My_Connections[Idx].host[0] = '\0';
1570 My_Connections[Idx].rbuf[0] = '\0';
1571 My_Connections[Idx].rdatalen = 0;
1572 My_Connections[Idx].wbuf[0] = '\0';
1573 My_Connections[Idx].wdatalen = 0;
1574 My_Connections[Idx].starttime = time( NULL );
1575 My_Connections[Idx].lastdata = time( NULL );
1576 My_Connections[Idx].lastping = 0;
1577 My_Connections[Idx].lastprivmsg = time( NULL );
1578 My_Connections[Idx].delaytime = 0;
1579 My_Connections[Idx].bytes_in = 0;
1580 My_Connections[Idx].bytes_out = 0;
1581 My_Connections[Idx].msg_in = 0;
1582 My_Connections[Idx].msg_out = 0;
1583 My_Connections[Idx].flag = 0;
1584 My_Connections[Idx].options = 0;
1587 My_Connections[Idx].zip.rbuf[0] = '\0';
1588 My_Connections[Idx].zip.rdatalen = 0;
1589 My_Connections[Idx].zip.wbuf[0] = '\0';
1590 My_Connections[Idx].zip.wdatalen = 0;
1591 My_Connections[Idx].zip.bytes_in = 0;
1592 My_Connections[Idx].zip.bytes_out = 0;
1594 } /* Init_Conn_Struct */
1598 Init_Socket( INT Sock )
1600 /* Socket-Optionen setzen */
1604 #ifdef O_NONBLOCK /* A/UX kennt das nicht? */
1605 if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1607 Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1612 if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1614 Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1615 /* dieser Fehler kann ignoriert werden. */
1623 Read_Resolver_Result( INT r_fd )
1625 /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1626 * und entsprechende Connection aktualisieren */
1628 CHAR result[HOST_LEN];
1632 FD_CLR( r_fd, &Resolver_FDs );
1634 /* Anfrage vom Parent lesen */
1635 len = read( r_fd, result, HOST_LEN - 1 );
1638 /* Fehler beim Lesen aus der Pipe */
1640 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1645 /* zugehoerige Connection suchen */
1646 for( i = 0; i < Pool_Size; i++ )
1648 if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break;
1650 if( i >= Pool_Size )
1652 /* Opsa! Keine passende Connection gefunden!? Vermutlich
1653 * wurde sie schon wieder geschlossen. */
1655 Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1659 Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
1662 close( My_Connections[i].res_stat->pipe[0] );
1663 close( My_Connections[i].res_stat->pipe[1] );
1664 free( My_Connections[i].res_stat );
1665 My_Connections[i].res_stat = NULL;
1667 if( My_Connections[i].sock > NONE )
1669 /* Eingehende Verbindung: Hostnamen setzen */
1670 c = Client_GetFromConn( i );
1671 assert( c != NULL );
1672 strlcpy( My_Connections[i].host, result, sizeof( My_Connections[i].host ));
1673 Client_SetHostname( c, result );
1677 /* Ausgehende Verbindung (=Server): IP setzen */
1678 n = Conf_GetServer( i );
1679 if( n > NONE ) strlcpy( Conf_Server[n].ip, result, sizeof( Conf_Server[n].ip ));
1680 else Log( LOG_ERR, "Got resolver result for non-configured server!?" );
1683 /* Penalty-Zeit zurueck setzen */
1684 Conn_ResetPenalty( i );
1685 } /* Read_Resolver_Result */