2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2003 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.119 2003/03/07 17:16:49 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> /* e.g. for Mac OS X */
47 #include <tcpd.h> /* for TCP Wrappers */
61 #include "conn-func.h"
67 #include "rendezvous.h"
73 #define SERVER_WAIT (NONE - 1)
76 LOCAL VOID Handle_Read PARAMS(( INT sock ));
77 LOCAL BOOLEAN Handle_Write PARAMS(( CONN_ID Idx ));
78 LOCAL VOID New_Connection PARAMS(( INT Sock ));
79 LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
80 LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
81 LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
82 LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
83 LOCAL VOID Check_Connections PARAMS(( VOID ));
84 LOCAL VOID Check_Servers PARAMS(( VOID ));
85 LOCAL VOID Init_Conn_Struct PARAMS(( CONN_ID Idx ));
86 LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock ));
87 LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx ));
88 LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd ));
89 LOCAL VOID Simple_Message PARAMS(( INT Sock, CHAR *Msg ));
91 LOCAL fd_set My_Listeners;
92 LOCAL fd_set My_Sockets;
93 LOCAL fd_set My_Connects;
96 INT allow_severity = LOG_INFO;
97 INT deny_severity = LOG_ERR;
104 /* Modul initialisieren: statische Strukturen "ausnullen". */
108 /* Speicher fuer Verbindungs-Pool anfordern */
109 Pool_Size = CONNECTION_POOL;
110 if( Conf_MaxConnections > 0 )
112 /* konfiguriertes Limit beachten */
113 if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
115 My_Connections = malloc( sizeof( CONNECTION ) * Pool_Size );
116 if( ! My_Connections )
118 /* Speicher konnte nicht alloziert werden! */
119 Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
122 Log( LOG_DEBUG, "Allocted connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
124 /* zu Beginn haben wir keine Verbindungen */
125 FD_ZERO( &My_Listeners );
126 FD_ZERO( &My_Sockets );
127 FD_ZERO( &My_Connects );
129 /* Groesster File-Descriptor fuer select() */
132 /* Connection-Struktur initialisieren */
133 for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
135 /* Global write counter */
143 /* Modul abmelden: alle noch offenen Connections
144 * schliessen und freigeben. */
149 Log( LOG_DEBUG, "Shutting down all connections ..." );
152 Rendezvous_UnregisterListeners( );
155 /* Sockets schliessen */
156 for( i = 0; i < Conn_MaxFD + 1; i++ )
158 if( FD_ISSET( i, &My_Sockets ))
160 for( idx = 0; idx < Pool_Size; idx++ )
162 if( My_Connections[idx].sock == i ) break;
164 if( FD_ISSET( i, &My_Listeners ))
167 Log( LOG_DEBUG, "Listening socket %d closed.", i );
169 else if( FD_ISSET( i, &My_Connects ))
172 Log( LOG_DEBUG, "Connection %d closed during creation (socket %d).", idx, i );
174 else if( idx < Pool_Size )
176 if( NGIRCd_SignalRestart ) Conn_Close( idx, NULL, "Server going down (restarting)", TRUE );
177 else Conn_Close( idx, NULL, "Server going down", TRUE );
181 Log( LOG_WARNING, "Closing unknown connection %d ...", i );
187 free( My_Connections );
188 My_Connections = NULL;
194 Conn_InitListeners( VOID )
196 /* Initialize ports on which the server should accept connections */
201 for( i = 0; i < Conf_ListenPorts_Count; i++ )
203 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
204 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
207 } /* Conn_InitListeners */
211 Conn_ExitListeners( VOID )
213 /* Close down all listening sockets */
218 Rendezvous_UnregisterListeners( );
221 Log( LOG_INFO, "Shutting down all listening sockets ..." );
222 for( i = 0; i < Conn_MaxFD + 1; i++ )
224 if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners ))
227 Log( LOG_DEBUG, "Listening socket %d closed.", i );
230 } /* Conn_ExitListeners */
234 Conn_NewListener( CONST UINT Port )
236 /* Create new listening socket on specified port */
238 struct sockaddr_in addr;
241 CHAR name[CLIENT_ID_LEN], *info;
244 /* Server-"Listen"-Socket initialisieren */
245 memset( &addr, 0, sizeof( addr ));
246 addr.sin_family = AF_INET;
247 addr.sin_port = htons( Port );
248 addr.sin_addr.s_addr = htonl( INADDR_ANY );
250 /* Socket erzeugen */
251 sock = socket( PF_INET, SOCK_STREAM, 0);
254 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
258 if( ! Init_Socket( sock )) return FALSE;
261 if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
263 Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
268 /* in "listen mode" gehen :-) */
269 if( listen( sock, 10 ) != 0 )
271 Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
276 /* Neuen Listener in Strukturen einfuegen */
277 FD_SET( sock, &My_Listeners );
278 FD_SET( sock, &My_Sockets );
280 if( sock > Conn_MaxFD ) Conn_MaxFD = sock;
282 Log( LOG_INFO, "Now listening on port %d (socket %d).", Port, sock );
285 /* Get best server description text */
286 if( ! Conf_ServerInfo[0] ) info = Conf_ServerName;
289 /* Use server info string */
291 if( Conf_ServerInfo[0] == '[' )
293 /* Cut off leading hostname part in "[]" */
294 info = strchr( Conf_ServerInfo, ']' );
298 while( *info == ' ' ) info++;
301 if( ! info ) info = Conf_ServerInfo;
304 /* Add port number to description if non-standard */
305 if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port );
306 else strlcpy( name, info, sizeof( name ));
308 /* Register service */
309 Rendezvous_Register( name, RENDEZVOUS_TYPE, Port );
313 } /* Conn_NewListener */
319 /* "Hauptschleife": Aktive Verbindungen ueberwachen. Folgende Aktionen
320 * werden dabei durchgefuehrt, bis der Server terminieren oder neu
323 * - neue Verbindungen annehmen,
324 * - Server-Verbindungen aufbauen,
325 * - geschlossene Verbindungen loeschen,
326 * - volle Schreibpuffer versuchen zu schreiben,
327 * - volle Lesepuffer versuchen zu verarbeiten,
328 * - Antworten von Resolver Sub-Prozessen annehmen.
331 fd_set read_sockets, write_sockets;
337 start = time( NULL );
338 while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart ))
343 Rendezvous_Handler( );
346 /* Should the configuration be reloaded? */
347 if( NGIRCd_SignalRehash ) NGIRCd_Rehash( );
349 /* Check configured servers and established links */
351 Check_Connections( );
353 /* noch volle Lese-Buffer suchen */
354 for( i = 0; i < Pool_Size; i++ )
356 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].rdatalen > 0 ))
358 /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
359 if( Handle_Buffer( i )) timeout = FALSE;
363 /* noch volle Schreib-Puffer suchen */
364 FD_ZERO( &write_sockets );
365 for( i = 0; i < Pool_Size; i++ )
368 if(( My_Connections[i].sock > NONE ) && (( My_Connections[i].wdatalen > 0 ) || ( My_Connections[i].zip.wdatalen > 0 )))
370 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].wdatalen > 0 ))
373 /* Socket der Verbindung in Set aufnehmen */
374 FD_SET( My_Connections[i].sock, &write_sockets );
378 /* Sockets mit im Aufbau befindlichen ausgehenden Verbindungen suchen */
379 for( i = 0; i < Pool_Size; i++ )
381 if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects ))) FD_SET( My_Connections[i].sock, &write_sockets );
384 /* von welchen Sockets koennte gelesen werden? */
386 read_sockets = My_Sockets;
387 for( i = 0; i < Pool_Size; i++ )
389 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].host[0] == '\0' ))
391 /* Hier muss noch auf den Resolver Sub-Prozess gewartet werden */
392 FD_CLR( My_Connections[i].sock, &read_sockets );
394 if(( My_Connections[i].sock > NONE ) && ( FD_ISSET( My_Connections[i].sock, &My_Connects )))
396 /* Hier laeuft noch ein asyncrones connect() */
397 FD_CLR( My_Connections[i].sock, &read_sockets );
399 if( My_Connections[i].delaytime > t )
401 /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
402 FD_CLR( My_Connections[i].sock, &read_sockets );
405 for( i = 0; i < Conn_MaxFD + 1; i++ )
407 /* Pipes von Resolver Sub-Prozessen aufnehmen */
408 if( FD_ISSET( i, &Resolver_FDs ))
410 FD_SET( i, &read_sockets );
414 /* Timeout initialisieren */
416 if( timeout ) tv.tv_sec = TIME_RES;
419 /* Auf Aktivitaet warten */
420 i = select( Conn_MaxFD + 1, &read_sockets, &write_sockets, NULL, &tv );
423 /* keine Veraenderung an den Sockets */
428 /* Fehler (z.B. Interrupt) */
431 Log( LOG_EMERG, "Conn_Handler(): select(): %s!", strerror( errno ));
432 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
438 /* Koennen Daten geschrieben werden? */
439 for( i = 0; i < Conn_MaxFD + 1; i++ )
441 if( ! FD_ISSET( i, &write_sockets )) continue;
443 /* Es kann geschrieben werden ... */
444 idx = Socket2Index( i );
445 if( idx == NONE ) continue;
447 if( ! Handle_Write( idx ))
449 /* Fehler beim Schreiben! Diesen Socket nun
450 * auch aus dem Read-Set entfernen: */
451 FD_CLR( i, &read_sockets );
455 /* Daten zum Lesen vorhanden? */
456 for( i = 0; i < Conn_MaxFD + 1; i++ )
458 if( FD_ISSET( i, &read_sockets )) Handle_Read( i );
462 if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
463 else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
469 Conn_WriteStr( CONN_ID Idx, CHAR *Format, ... )
472 Conn_WriteStr( Idx, Format, va_alist )
478 /* String in Socket schreiben. CR+LF wird von dieser Funktion
479 * automatisch angehaengt. Im Fehlerfall wird dir Verbindung
480 * getrennt und FALSE geliefert. */
482 CHAR buffer[COMMAND_LEN];
486 assert( Idx > NONE );
487 assert( Format != NULL );
490 va_start( ap, Format );
494 if( vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) == COMMAND_LEN - 2 )
496 Log( LOG_CRIT, "Text too long to send (connection %d)!", Idx );
497 Conn_Close( Idx, "Text too long to send!", NULL, FALSE );
502 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer );
505 strlcat( buffer, "\r\n", sizeof( buffer ));
506 ok = Conn_Write( Idx, buffer, strlen( buffer ));
507 My_Connections[Idx].msg_out++;
511 } /* Conn_WriteStr */
515 Conn_Write( CONN_ID Idx, CHAR *Data, INT Len )
517 /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
518 * der Client disconnectiert und FALSE geliefert. */
520 assert( Idx > NONE );
521 assert( Data != NULL );
524 /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
525 * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
526 * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
527 * In diesem Fall wird hier einfach ein Fehler geliefert. */
528 if( My_Connections[Idx].sock <= NONE )
530 Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
534 /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
535 * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
536 * Socket zu schreiben (u.a. wg. Komprimierung). */
537 if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
539 /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
540 * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
541 if( ! Try_Write( Idx )) return FALSE;
543 /* nun neu pruefen: */
544 if( WRITEBUFFER_LEN - My_Connections[Idx].wdatalen - Len <= 0 )
546 Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
547 Conn_Close( Idx, "Write buffer overflow!", NULL, FALSE );
553 if( My_Connections[Idx].options & CONN_ZIP )
555 /* Daten komprimieren und in Puffer kopieren */
556 if( ! Zip_Buffer( Idx, Data, Len )) return FALSE;
561 /* Daten in Puffer kopieren */
562 memcpy( My_Connections[Idx].wbuf + My_Connections[Idx].wdatalen, Data, Len );
563 My_Connections[Idx].wdatalen += Len;
564 My_Connections[Idx].bytes_out += Len;
567 /* Adjust global write counter */
575 Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient )
577 /* Close connection. Open pipes of asyncronous resolver
578 * sub-processes are closed down. */
583 DOUBLE in_z_k, out_z_k;
587 assert( Idx > NONE );
588 assert( My_Connections[Idx].sock > NONE );
590 /* Is this link already shutting down? */
591 if( My_Connections[Idx].options & CONN_ISCLOSING )
593 /* Conn_Close() has been called recursively for this link;
594 * probabe reason: Try_Write() failed -- see below. */
598 /* Mark link as "closing" */
599 My_Connections[Idx].options |= CONN_ISCLOSING;
601 /* Search client, if any */
602 c = Client_GetFromConn( Idx );
604 /* Should the client be informed? */
608 /* Send statistics to client if registered as user: */
609 if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
611 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 );
615 /* Send ERROR to client (see RFC!) */
616 if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
617 else Conn_WriteStr( Idx, "ERROR :Closing connection." );
618 if( My_Connections[Idx].sock == NONE ) return;
621 /* Try to write out the write buffer */
622 (VOID)Try_Write( Idx );
624 /* Shut down socket */
625 if( close( My_Connections[Idx].sock ) != 0 )
627 /* Oops, we can't close the socket!? This is fatal! */
628 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 ));
629 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
633 /* Mark socket as invalid: */
634 FD_CLR( My_Connections[Idx].sock, &My_Sockets );
635 FD_CLR( My_Connections[Idx].sock, &My_Connects );
636 My_Connections[Idx].sock = NONE;
638 /* If there is still a client, unregister it now */
639 if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
641 /* Calculate statistics and log information */
642 in_k = (DOUBLE)My_Connections[Idx].bytes_in / 1024;
643 out_k = (DOUBLE)My_Connections[Idx].bytes_out / 1024;
645 if( My_Connections[Idx].options & CONN_ZIP )
647 in_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_in / 1024;
648 out_z_k = (DOUBLE)My_Connections[Idx].zip.bytes_out / 1024;
649 in_p = (INT)(( in_k * 100 ) / in_z_k );
650 out_p = (INT)(( out_k * 100 ) / out_z_k );
651 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 );
656 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 );
659 /* Is there a resolver sub-process running? */
660 if( My_Connections[Idx].res_stat )
662 /* Free resolver structures */
663 FD_CLR( My_Connections[Idx].res_stat->pipe[0], &Resolver_FDs );
664 close( My_Connections[Idx].res_stat->pipe[0] );
665 close( My_Connections[Idx].res_stat->pipe[1] );
666 free( My_Connections[Idx].res_stat );
669 /* Servers: Modify time of next connect attempt? */
670 Conf_UnsetServer( Idx );
673 /* Clean up zlib, if link was compressed */
674 if( Conn_Options( Idx ) & CONN_ZIP )
676 inflateEnd( &My_Connections[Idx].zip.in );
677 deflateEnd( &My_Connections[Idx].zip.out );
681 /* Clean up connection structure (=free it) */
682 Init_Conn_Struct( Idx );
687 Try_Write( CONN_ID Idx )
689 /* Versuchen, Daten aus dem Schreib-Puffer in den Socket zu
690 * schreiben. TRUE wird geliefert, wenn entweder keine Daten
691 * zum Versenden vorhanden sind oder erfolgreich bearbeitet
692 * werden konnten. Im Fehlerfall wird FALSE geliefert und
693 * die Verbindung geschlossen. */
698 assert( Idx > NONE );
699 assert( My_Connections[Idx].sock > NONE );
701 /* sind ueberhaupt Daten vorhanden? */
703 if(( ! My_Connections[Idx].wdatalen > 0 ) && ( ! My_Connections[Idx].zip.wdatalen )) return TRUE;
705 if( ! My_Connections[Idx].wdatalen > 0 ) return TRUE;
708 /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
709 tv.tv_sec = 0; tv.tv_usec = 0;
711 FD_ZERO( &write_socket );
712 FD_SET( My_Connections[Idx].sock, &write_socket );
713 if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
718 Log( LOG_ALERT, "Try_Write(): select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
719 Conn_Close( Idx, "Server error!", NULL, FALSE );
724 if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
730 Handle_Read( INT Sock )
732 /* Aktivitaet auf einem Socket verarbeiten:
733 * - neue Clients annehmen,
734 * - Daten von Clients verarbeiten,
735 * - Resolver-Rueckmeldungen annehmen. */
739 assert( Sock > NONE );
741 if( FD_ISSET( Sock, &My_Listeners ))
743 /* es ist einer unserer Listener-Sockets: es soll
744 * also eine neue Verbindung aufgebaut werden. */
746 New_Connection( Sock );
748 else if( FD_ISSET( Sock, &Resolver_FDs ))
750 /* Rueckmeldung von einem Resolver Sub-Prozess */
752 Read_Resolver_Result( Sock );
756 /* Ein Client Socket: entweder ein User oder Server */
758 idx = Socket2Index( Sock );
759 if( idx > NONE ) Read_Request( idx );
765 Handle_Write( CONN_ID Idx )
767 /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
772 assert( Idx > NONE );
773 assert( My_Connections[Idx].sock > NONE );
775 if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
777 /* es soll nichts geschrieben werden, sondern ein
778 * connect() hat ein Ergebnis geliefert */
780 FD_CLR( My_Connections[Idx].sock, &My_Connects );
782 /* Ergebnis des connect() ermitteln */
784 res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
785 assert( len == sizeof( err ));
787 /* Fehler aufgetreten? */
788 if(( res != 0 ) || ( err != 0 ))
791 if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
792 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 ));
794 /* Clean up socket, connection and client structures */
795 FD_CLR( My_Connections[Idx].sock, &My_Sockets );
796 c = Client_GetFromConn( Idx );
797 if( c ) Client_DestroyNow( c );
798 close( My_Connections[Idx].sock );
799 Init_Conn_Struct( Idx );
801 /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
802 Conf_Server[Conf_GetServer( Idx )].lasttry = time( NULL );
803 Conf_UnsetServer( Idx );
807 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 );
809 /* PASS und SERVER verschicken */
810 Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[Conf_GetServer( Idx )].pwd_out, NGIRCd_ProtoID );
811 return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
815 /* Schreibpuffer leer, aber noch Daten im Kompressionsbuffer?
816 * Dann muss dieser nun geflushed werden! */
817 if( My_Connections[Idx].wdatalen == 0 ) Zip_Flush( Idx );
820 assert( My_Connections[Idx].wdatalen > 0 );
822 /* Daten schreiben */
823 len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
826 /* Operation haette Socket "nur" blockiert ... */
827 if( errno == EAGAIN ) return TRUE;
829 /* Oops, ein Fehler! */
830 Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
831 Conn_Close( Idx, "Write error!", NULL, FALSE );
835 /* Puffer anpassen */
836 My_Connections[Idx].wdatalen -= len;
837 memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
844 New_Connection( INT Sock )
846 /* Neue Client-Verbindung von Listen-Socket annehmen und
847 * CLIENT-Struktur anlegen. */
850 struct request_info req;
852 struct sockaddr_in new_addr;
853 INT new_sock, new_sock_len;
860 assert( Sock > NONE );
862 /* Connection auf Listen-Socket annehmen */
863 new_sock_len = sizeof( new_addr );
864 new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
867 Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
872 /* Validate socket using TCP Wrappers */
873 request_init( &req, RQ_DAEMON, PACKAGE, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL );
874 if( ! hosts_access( &req ))
877 Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr ));
878 Simple_Message( new_sock, "ERROR :Connection refused" );
884 /* Socket initialisieren */
885 Init_Socket( new_sock );
887 /* Freie Connection-Struktur suchen */
888 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
889 if( idx >= Pool_Size )
891 new_size = Pool_Size + CONNECTION_POOL;
893 /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
894 * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
896 if( Conf_MaxConnections > 0 )
898 /* Es ist ein Limit konfiguriert */
899 if( Pool_Size >= Conf_MaxConnections )
901 /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
902 Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
903 Simple_Message( new_sock, "ERROR :Connection limit reached" );
907 if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
909 if( new_size < Pool_Size )
911 Log( LOG_ALERT, "Can't accespt connection: limit (%d) reached -- overflow!", Pool_Size );
912 Simple_Message( new_sock, "ERROR :Connection limit reached" );
917 /* zunaechst realloc() versuchen; wenn das scheitert, malloc() versuchen
918 * und Daten ggf. "haendisch" umkopieren. (Haesslich! Eine wirklich
919 * dynamische Verwaltung waere wohl _deutlich_ besser ...) */
920 ptr = realloc( My_Connections, sizeof( CONNECTION ) * new_size );
923 /* realloc() ist fehlgeschlagen. Nun malloc() probieren: */
924 ptr = malloc( sizeof( CONNECTION ) * new_size );
927 /* Offenbar steht kein weiterer Sepeicher zur Verfuegung :-( */
928 Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
929 Simple_Message( new_sock, "ERROR: Internal error" );
934 /* Struktur umkopieren ... */
935 memcpy( ptr, My_Connections, sizeof( CONNECTION ) * Pool_Size );
937 Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [malloc()/memcpy()]", new_size, sizeof( CONNECTION ) * new_size );
939 else Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
941 /* Adjust pointer to new block */
942 My_Connections = ptr;
944 /* Initialize new items */
945 for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
948 /* Adjust new pool size */
949 Pool_Size = new_size;
952 /* Client-Struktur initialisieren */
953 c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, FALSE );
956 Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
957 Simple_Message( new_sock, "ERROR :Internal error" );
962 /* Verbindung registrieren */
963 Init_Conn_Struct( idx );
964 My_Connections[idx].sock = new_sock;
965 My_Connections[idx].addr = new_addr;
967 /* Neuen Socket registrieren */
968 FD_SET( new_sock, &My_Sockets );
969 if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
971 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 );
973 /* Hostnamen ermitteln */
974 strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host ));
975 Client_SetHostname( c, My_Connections[idx].host );
976 s = Resolve_Addr( &new_addr );
979 /* Sub-Prozess wurde asyncron gestartet */
980 My_Connections[idx].res_stat = s;
983 /* Penalty-Zeit setzen */
984 Conn_SetPenalty( idx, 4 );
985 } /* New_Connection */
989 Socket2Index( INT Sock )
991 /* zum Socket passende Connection suchen */
995 assert( Sock > NONE );
997 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
999 if( idx >= Pool_Size )
1001 /* die Connection wurde vermutlich (wegen eines
1002 * Fehlers) bereits wieder abgebaut ... */
1003 Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1007 } /* Socket2Index */
1011 Read_Request( CONN_ID Idx )
1013 /* Daten von Socket einlesen und entsprechend behandeln.
1014 * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1021 assert( Idx > NONE );
1022 assert( My_Connections[Idx].sock > NONE );
1024 /* wenn noch nicht registriert: maximal mit ZREADBUFFER_LEN arbeiten,
1025 * ansonsten koennen Daten ggf. nicht umkopiert werden. */
1026 bsize = READBUFFER_LEN;
1028 c = Client_GetFromConn( Idx );
1029 if(( Client_Type( c ) != CLIENT_USER ) && ( Client_Type( c ) != CLIENT_SERVER ) && ( Client_Type( c ) != CLIENT_SERVICE ) && ( bsize > ZREADBUFFER_LEN )) bsize = ZREADBUFFER_LEN;
1033 if(( bsize - My_Connections[Idx].rdatalen - 1 < 1 ) || ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen < 1 ))
1035 if( bsize - My_Connections[Idx].rdatalen - 1 < 1 )
1038 /* Der Lesepuffer ist voll */
1039 Log( LOG_ERR, "Read buffer overflow (connection %d): %d bytes!", Idx, My_Connections[Idx].rdatalen );
1040 Conn_Close( Idx, "Read buffer overflow!", NULL, FALSE );
1045 if( My_Connections[Idx].options & CONN_ZIP )
1047 len = recv( My_Connections[Idx].sock, My_Connections[Idx].zip.rbuf + My_Connections[Idx].zip.rdatalen, ( ZREADBUFFER_LEN - My_Connections[Idx].zip.rdatalen ), 0 );
1048 if( len > 0 ) My_Connections[Idx].zip.rdatalen += len;
1053 len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, bsize - My_Connections[Idx].rdatalen - 1, 0 );
1054 if( len > 0 ) My_Connections[Idx].rdatalen += len;
1059 /* Socket wurde geschlossen */
1060 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 ));
1061 Conn_Close( Idx, "Socket closed!", "Client closed connection", FALSE );
1067 /* Operation haette Socket "nur" blockiert ... */
1068 if( errno == EAGAIN ) return;
1070 /* Fehler beim Lesen */
1071 Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno ));
1072 Conn_Close( Idx, "Read error!", "Client closed connection", FALSE );
1076 /* Connection-Statistik aktualisieren */
1077 My_Connections[Idx].bytes_in += len;
1079 /* Timestamp aktualisieren */
1080 My_Connections[Idx].lastdata = time( NULL );
1082 Handle_Buffer( Idx );
1083 } /* Read_Request */
1087 Handle_Buffer( CONN_ID Idx )
1089 /* Daten im Lese-Puffer einer Verbindung verarbeiten.
1090 * Wurde ein Request verarbeitet, so wird TRUE geliefert,
1091 * ansonsten FALSE (auch bei Fehlern). */
1098 BOOLEAN action, result;
1107 /* ggf. noch unkomprimiete Daten weiter entpacken */
1108 if( My_Connections[Idx].options & CONN_ZIP )
1110 if( ! Unzip_Buffer( Idx )) return FALSE;
1114 if( My_Connections[Idx].rdatalen < 1 ) break;
1116 /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
1117 * RFC 2812. Haben wir eine? */
1118 My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
1119 ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
1121 if( ptr ) delta = 2;
1125 /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
1126 * machen soetwas viele Clients, u.a. "mIRC" :-( */
1127 ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
1128 ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
1130 if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1131 else if( ptr1 ) ptr = ptr1;
1132 else if( ptr2 ) ptr = ptr2;
1139 /* Ende der Anfrage wurde gefunden */
1141 len = ( ptr - My_Connections[Idx].rbuf ) + delta;
1142 if( len > ( COMMAND_LEN - 1 ))
1144 /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
1145 * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
1146 * empfangen wird, wird der Client disconnectiert. */
1147 Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", Idx, My_Connections[Idx].rdatalen, COMMAND_LEN - 1 );
1148 Conn_Close( Idx, NULL, "Request too long", TRUE );
1153 /* merken, ob Stream bereits komprimiert wird */
1154 old_z = My_Connections[Idx].options & CONN_ZIP;
1159 /* Es wurde ein Request gelesen */
1160 My_Connections[Idx].msg_in++;
1161 if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return FALSE;
1165 /* Puffer anpassen */
1166 My_Connections[Idx].rdatalen -= len;
1167 memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
1170 if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( My_Connections[Idx].rdatalen > 0 ))
1172 /* Mit dem letzten Befehl wurde Socket-Kompression aktiviert.
1173 * Evtl. schon vom Socket gelesene Daten in den Unzip-Puffer
1174 * umkopieren, damit diese nun zunaechst entkomprimiert werden */
1176 if( My_Connections[Idx].rdatalen > ZREADBUFFER_LEN )
1178 /* Hupsa! Soviel Platz haben wir aber gar nicht! */
1179 Log( LOG_ALERT, "Can't move read buffer: No space left in unzip buffer (need %d bytes)!", My_Connections[Idx].rdatalen );
1182 memcpy( My_Connections[Idx].zip.rbuf, My_Connections[Idx].rbuf, My_Connections[Idx].rdatalen );
1183 My_Connections[Idx].zip.rdatalen = My_Connections[Idx].rdatalen;
1184 My_Connections[Idx].rdatalen = 0;
1185 Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", My_Connections[Idx].zip.rdatalen );
1191 if( action ) result = TRUE;
1195 } /* Handle_Buffer */
1199 Check_Connections( VOID )
1201 /* Pruefen, ob Verbindungen noch "alive" sind. Ist dies
1202 * nicht der Fall, zunaechst PING-PONG spielen und, wenn
1203 * auch das nicht "hilft", Client disconnectieren. */
1208 for( i = 0; i < Pool_Size; i++ )
1210 if( My_Connections[i].sock == NONE ) continue;
1212 c = Client_GetFromConn( i );
1213 if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1215 /* verbundener User, Server oder Service */
1216 if( My_Connections[i].lastping > My_Connections[i].lastdata )
1218 /* es wurde bereits ein PING gesendet */
1219 if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout )
1222 Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1223 Conn_Close( i, NULL, "Ping timeout", TRUE );
1226 else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1228 /* es muss ein PING gesendet werden */
1229 Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1230 My_Connections[i].lastping = time( NULL );
1231 Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1236 /* noch nicht vollstaendig aufgebaute Verbindung */
1237 if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
1240 Log( LOG_DEBUG, "Connection %d timed out ...", i );
1241 Conn_Close( i, NULL, "Timeout", FALSE );
1245 } /* Check_Connections */
1249 Check_Servers( VOID )
1251 /* Check if we can establish further server links */
1257 /* Serach all connections, are there results from the resolver? */
1258 for( idx = 0; idx < Pool_Size; idx++ )
1260 if( My_Connections[idx].sock != SERVER_WAIT ) continue;
1263 if( My_Connections[idx].res_stat == NULL ) New_Server( Conf_GetServer( idx ), idx );
1266 /* Check all configured servers */
1267 for( i = 0; i < MAX_SERVERS; i++ )
1269 /* Valid outgoing server which isn't already connected or disabled? */
1270 if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) || ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED )) continue;
1272 /* Is there already a connection in this group? */
1273 if( Conf_Server[i].group > NONE )
1275 for( n = 0; n < MAX_SERVERS; n++ )
1277 if( n == i ) continue;
1278 if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
1280 if( n < MAX_SERVERS ) continue;
1283 /* Check last connect attempt? */
1284 if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
1286 /* Okay, try to connect now */
1287 Conf_Server[i].lasttry = time( NULL );
1289 /* Search free connection structure */
1290 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1291 if( idx >= Pool_Size )
1293 Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
1296 Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1298 /* Verbindungs-Struktur initialisieren */
1299 Init_Conn_Struct( idx );
1300 My_Connections[idx].sock = SERVER_WAIT;
1301 Conf_Server[i].conn_id = idx;
1303 /* Resolve Hostname. If this fails, try to use it as an IP address */
1304 strlcpy( Conf_Server[i].ip, Conf_Server[i].host, sizeof( Conf_Server[i].ip ));
1305 strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host ));
1306 s = Resolve_Name( Conf_Server[i].host );
1309 /* Sub-Prozess wurde asyncron gestartet */
1310 My_Connections[idx].res_stat = s;
1313 } /* Check_Servers */
1317 New_Server( INT Server, CONN_ID Idx )
1319 /* Neue Server-Verbindung aufbauen */
1321 struct sockaddr_in new_addr;
1322 struct in_addr inaddr;
1326 assert( Server > NONE );
1327 assert( Idx > NONE );
1329 /* Wurde eine gueltige IP-Adresse gefunden? */
1330 if( ! Conf_Server[Server].ip[0] )
1332 /* Nein. Verbindung wieder freigeben: */
1333 Init_Conn_Struct( Idx );
1334 Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
1338 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 );
1340 #ifdef HAVE_INET_ATON
1341 if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1343 memset( &inaddr, 0, sizeof( inaddr ));
1344 inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1345 if( inaddr.s_addr == (unsigned)-1 )
1348 /* Konnte Adresse nicht konvertieren */
1349 Init_Conn_Struct( Idx );
1350 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 );
1354 memset( &new_addr, 0, sizeof( new_addr ));
1355 new_addr.sin_family = AF_INET;
1356 new_addr.sin_addr = inaddr;
1357 new_addr.sin_port = htons( Conf_Server[Server].port );
1359 new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1362 Init_Conn_Struct( Idx );
1363 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1367 if( ! Init_Socket( new_sock )) return;
1369 res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1370 if(( res != 0 ) && ( errno != EINPROGRESS ))
1372 Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1374 Init_Conn_Struct( Idx );
1378 /* Client-Struktur initialisieren */
1379 c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, FALSE );
1383 Init_Conn_Struct( Idx );
1384 Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1387 Client_SetIntroducer( c, c );
1388 Client_SetToken( c, TOKEN_OUTBOUND );
1390 /* Verbindung registrieren */
1391 My_Connections[Idx].sock = new_sock;
1392 My_Connections[Idx].addr = new_addr;
1393 strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
1395 /* Neuen Socket registrieren */
1396 FD_SET( new_sock, &My_Sockets );
1397 FD_SET( new_sock, &My_Connects );
1398 if( new_sock > Conn_MaxFD ) Conn_MaxFD = new_sock;
1400 Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1405 Init_Conn_Struct( CONN_ID Idx )
1407 /* Connection-Struktur initialisieren */
1409 My_Connections[Idx].sock = NONE;
1410 My_Connections[Idx].res_stat = NULL;
1411 My_Connections[Idx].host[0] = '\0';
1412 My_Connections[Idx].rbuf[0] = '\0';
1413 My_Connections[Idx].rdatalen = 0;
1414 My_Connections[Idx].wbuf[0] = '\0';
1415 My_Connections[Idx].wdatalen = 0;
1416 My_Connections[Idx].starttime = time( NULL );
1417 My_Connections[Idx].lastdata = time( NULL );
1418 My_Connections[Idx].lastping = 0;
1419 My_Connections[Idx].lastprivmsg = time( NULL );
1420 My_Connections[Idx].delaytime = 0;
1421 My_Connections[Idx].bytes_in = 0;
1422 My_Connections[Idx].bytes_out = 0;
1423 My_Connections[Idx].msg_in = 0;
1424 My_Connections[Idx].msg_out = 0;
1425 My_Connections[Idx].flag = 0;
1426 My_Connections[Idx].options = 0;
1429 My_Connections[Idx].zip.rbuf[0] = '\0';
1430 My_Connections[Idx].zip.rdatalen = 0;
1431 My_Connections[Idx].zip.wbuf[0] = '\0';
1432 My_Connections[Idx].zip.wdatalen = 0;
1433 My_Connections[Idx].zip.bytes_in = 0;
1434 My_Connections[Idx].zip.bytes_out = 0;
1436 } /* Init_Conn_Struct */
1440 Init_Socket( INT Sock )
1442 /* Socket-Optionen setzen */
1446 #ifdef O_NONBLOCK /* A/UX kennt das nicht? */
1447 if( fcntl( Sock, F_SETFL, O_NONBLOCK ) != 0 )
1449 Log( LOG_CRIT, "Can't enable non-blocking mode: %s!", strerror( errno ));
1454 if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
1456 Log( LOG_ERR, "Can't set socket options: %s!", strerror( errno ));
1457 /* dieser Fehler kann ignoriert werden. */
1465 Read_Resolver_Result( INT r_fd )
1467 /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
1468 * und entsprechende Connection aktualisieren */
1470 CHAR result[HOST_LEN];
1474 FD_CLR( r_fd, &Resolver_FDs );
1476 /* Anfrage vom Parent lesen */
1477 len = read( r_fd, result, HOST_LEN - 1 );
1480 /* Fehler beim Lesen aus der Pipe */
1482 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno ));
1487 /* zugehoerige Connection suchen */
1488 for( i = 0; i < Pool_Size; i++ )
1490 if(( My_Connections[i].sock != NONE ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->pipe[0] == r_fd )) break;
1492 if( i >= Pool_Size )
1494 /* Opsa! Keine passende Connection gefunden!? Vermutlich
1495 * wurde sie schon wieder geschlossen. */
1497 Log( LOG_DEBUG, "Resolver: Got result for unknown connection!?" );
1501 Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result );
1504 close( My_Connections[i].res_stat->pipe[0] );
1505 close( My_Connections[i].res_stat->pipe[1] );
1506 free( My_Connections[i].res_stat );
1507 My_Connections[i].res_stat = NULL;
1509 if( My_Connections[i].sock > NONE )
1511 /* Eingehende Verbindung: Hostnamen setzen */
1512 c = Client_GetFromConn( i );
1513 assert( c != NULL );
1514 strlcpy( My_Connections[i].host, result, sizeof( My_Connections[i].host ));
1515 Client_SetHostname( c, result );
1519 /* Ausgehende Verbindung (=Server): IP setzen */
1520 n = Conf_GetServer( i );
1521 if( n > NONE ) strlcpy( Conf_Server[n].ip, result, sizeof( Conf_Server[n].ip ));
1522 else Log( LOG_ERR, "Got resolver result for non-configured server!?" );
1525 /* Penalty-Zeit zurueck setzen */
1526 Conn_ResetPenalty( i );
1527 } /* Read_Resolver_Result */
1531 Simple_Message( INT Sock, CHAR *Msg )
1533 /* Write "simple" message to socket, without using compression
1534 * or even the connection write buffers. Used e.g. for error
1535 * messages by New_Connection(). */
1537 assert( Sock > NONE );
1538 assert( Msg != NULL );
1540 (VOID)send( Sock, Msg, strlen( Msg ), 0 );
1541 (VOID)send( Sock, "\r\n", 2, 0 );
1542 } /* Simple_Error */