2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 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
20 static char UNUSED id[] = "$Id: conn.c,v 1.187 2006/02/02 21:00:21 fw Exp $";
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <netinet/in.h>
40 #ifdef HAVE_NETINET_IP_H
41 # include <netinet/ip.h>
44 #ifdef HAVE_ARPA_INET_H
45 # include <arpa/inet.h>
47 # define PF_INET AF_INET
51 # include <stdint.h> /* e.g. for Mac OS X */
55 # include <tcpd.h> /* for TCP Wrappers */
70 #include "conn-func.h"
76 # include "rendezvous.h"
82 #define SERVER_WAIT (NONE - 1)
85 static bool Handle_Write PARAMS(( CONN_ID Idx ));
86 static int New_Connection PARAMS(( int Sock ));
87 static CONN_ID Socket2Index PARAMS(( int Sock ));
88 static void Read_Request PARAMS(( CONN_ID Idx ));
89 static bool Handle_Buffer PARAMS(( CONN_ID Idx ));
90 static void Check_Connections PARAMS(( void ));
91 static void Check_Servers PARAMS(( void ));
92 static void Init_Conn_Struct PARAMS(( CONN_ID Idx ));
93 static bool Init_Socket PARAMS(( int Sock ));
94 static void New_Server PARAMS(( int Server, CONN_ID Idx ));
95 static void Simple_Message PARAMS(( int Sock, char *Msg ));
96 static int Count_Connections PARAMS(( struct sockaddr_in addr ));
97 static int NewListener PARAMS(( const UINT16 Port ));
99 static array My_Listeners;
102 int allow_severity = LOG_INFO;
103 int deny_severity = LOG_ERR;
106 static void server_login PARAMS((CONN_ID idx));
108 static void cb_Read_Resolver_Result PARAMS(( int sock, UNUSED short what));
109 static void cb_clientserver PARAMS((int sock, short what));
112 cb_listen(int sock, short irrelevant)
115 New_Connection( sock );
120 cb_connserver(int sock, UNUSED short what)
125 CONN_ID idx = Socket2Index( sock );
128 Log(LOG_DEBUG, "cb_connserver wants to write on unknown socket?!");
134 assert( what & IO_WANTWRITE);
136 /* connect() finished, get result. */
137 sock_len = sizeof( err );
138 res = getsockopt( My_Connections[idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
139 assert( sock_len == sizeof( err ));
141 /* Error while connecting? */
142 if ((res != 0) || (err != 0)) {
144 Log(LOG_CRIT, "getsockopt (connection %d): %s!",
145 idx, strerror(errno));
148 "Can't connect socket to \"%s:%d\" (connection %d): %s!",
149 My_Connections[idx].host,
150 Conf_Server[Conf_GetServer(idx)].port,
153 /* Clean up the CLIENT structure (to avoid silly log
154 * messages) and call Conn_Close() to do the rest. */
155 c = Client_GetFromConn(idx);
157 Client_DestroyNow(c);
159 Conn_Close(idx, "Can't connect!", NULL, false);
164 Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
170 server_login(CONN_ID idx)
172 Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", idx,
173 My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port );
175 io_event_setcb( My_Connections[idx].sock, cb_clientserver);
176 io_event_add( My_Connections[idx].sock, IO_WANTREAD|IO_WANTWRITE);
178 /* Send PASS and SERVER command to peer */
179 Conn_WriteStr( idx, "PASS %s %s", Conf_Server[Conf_GetServer( idx )].pwd_out, NGIRCd_ProtoID );
180 Conn_WriteStr( idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
185 cb_clientserver(int sock, short what)
187 CONN_ID idx = Socket2Index( sock );
190 Log(LOG_WARNING, "WTF: cb_clientserver wants to write on unknown socket?!");
196 if (what & IO_WANTREAD)
199 if (what & IO_WANTWRITE)
207 /* Modul initialisieren: statische Strukturen "ausnullen". */
211 /* Speicher fuer Verbindungs-Pool anfordern */
212 Pool_Size = CONNECTION_POOL;
213 if( Conf_MaxConnections > 0 )
215 /* konfiguriertes Limit beachten */
216 if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
218 My_Connections = (CONNECTION *) calloc( Pool_Size, sizeof( CONNECTION ) );
219 if( ! My_Connections )
221 /* Speicher konnte nicht alloziert werden! */
222 Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
226 Log( LOG_DEBUG, "Allocated connection pool for %d items (%ld bytes).", Pool_Size, sizeof( CONNECTION ) * Pool_Size );
229 array_free( &My_Listeners );
231 /* Connection-Struktur initialisieren */
232 for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
234 /* Global write counter */
242 /* Modul abmelden: alle noch offenen Connections
243 * schliessen und freigeben. */
248 Log( LOG_DEBUG, "Shutting down all connections ..." );
251 Conn_ExitListeners();
253 /* Sockets schliessen */
254 for( idx = 0; idx < Pool_Size; idx++ ) {
255 if( My_Connections[idx].sock > NONE ) {
256 Conn_Close( idx, NULL, NGIRCd_SignalRestart ?
257 "Server going down (restarting)":"Server going down", true );
262 free( My_Connections );
263 My_Connections = NULL;
265 io_library_shutdown();
270 ports_initlisteners(array *a, void (*func)(int,short))
272 unsigned int created = 0, len;
276 len = array_length(a, sizeof (UINT16));
277 port = array_start(a);
279 fd = NewListener( *port );
284 if (!io_event_create( fd, IO_WANTREAD, func )) {
285 Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!",
286 fd, (unsigned int) *port, strerror(errno));
300 Conn_InitListeners( void )
302 /* Initialize ports on which the server should accept connections */
304 unsigned int created;
306 if (!io_library_init(CONNECTION_POOL)) {
307 Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno));
311 created = ports_initlisteners(&Conf_ListenPorts, cb_listen);
314 } /* Conn_InitListeners */
318 Conn_ExitListeners( void )
320 /* Close down all listening sockets */
324 Rendezvous_UnregisterListeners( );
327 arraylen = array_length(&My_Listeners, sizeof (int));
328 Log( LOG_INFO, "Shutting down all listening sockets (%d total)...", arraylen );
329 fd = array_start(&My_Listeners);
335 Log( LOG_DEBUG, "Listening socket %d closed.", *fd );
339 array_free(&My_Listeners);
340 } /* Conn_ExitListeners */
343 /* return new listening port file descriptor or -1 on failure */
345 NewListener( const UINT16 Port )
347 /* Create new listening socket on specified port */
349 struct sockaddr_in addr;
350 struct in_addr inaddr;
353 char name[CLIENT_ID_LEN], *info;
356 /* Server-"Listen"-Socket initialisieren */
357 memset( &addr, 0, sizeof( addr ));
358 memset( &inaddr, 0, sizeof( inaddr ));
359 addr.sin_family = AF_INET;
360 addr.sin_port = htons( Port );
361 if( Conf_ListenAddress[0] )
363 #ifdef HAVE_INET_ATON
364 if( inet_aton( Conf_ListenAddress, &inaddr ) == 0 )
366 inaddr.s_addr = inet_addr( Conf_ListenAddress );
367 if( inaddr.s_addr == (unsigned)-1 )
370 Log( LOG_CRIT, "Can't listen on %s:%u: can't convert ip address %s!",
371 Conf_ListenAddress, Port, Conf_ListenAddress );
375 else inaddr.s_addr = htonl( INADDR_ANY );
376 addr.sin_addr = inaddr;
378 sock = socket( PF_INET, SOCK_STREAM, 0);
380 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
384 if( ! Init_Socket( sock )) return -1;
386 if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 ) {
387 Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
392 if( listen( sock, 10 ) != 0 ) {
393 Log( LOG_CRIT, "Can't listen on socket: %s!", strerror( errno ));
398 /* keep fd in list so we can close it when ngircd restarts/shuts down */
399 if (!array_catb( &My_Listeners,(char*) &sock, sizeof(int) )) {
400 Log( LOG_CRIT, "Can't add socket to My_Listeners array: %s!", strerror( errno ));
405 if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock );
406 else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock );
409 /* Get best server description text */
410 if( ! Conf_ServerInfo[0] ) info = Conf_ServerName;
413 /* Use server info string */
415 if( Conf_ServerInfo[0] == '[' )
417 /* Cut off leading hostname part in "[]" */
418 info = strchr( Conf_ServerInfo, ']' );
422 while( *info == ' ' ) info++;
425 if( ! info ) info = Conf_ServerInfo;
428 /* Add port number to description if non-standard */
429 if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port );
430 else strlcpy( name, info, sizeof( name ));
432 /* Register service */
433 Rendezvous_Register( name, MDNS_TYPE, Port );
442 /* "Main Loop.": Loop until a signal (for shutdown or restart) arrives.
443 * Call io_dispatch() to check for read/writeable sockets every second
444 * Wait for status change on pending connections (e.g: when the hostname has been resolved)
445 * check for penalty/timeouts
446 * handle input buffers
449 unsigned int wdatalen;
454 while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart )) {
458 Rendezvous_Handler( );
461 /* Should the configuration be reloaded? */
462 if (NGIRCd_SignalRehash) {
466 /* Check configured servers and established links */
468 Check_Connections( );
472 /* noch volle Lese-Buffer suchen */
473 for( i = 0; i < Pool_Size; i++ ) {
474 if(( My_Connections[i].sock > NONE ) && ( array_bytes(&My_Connections[i].rbuf) > 0 ) &&
475 ( My_Connections[i].delaytime < t ))
477 /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
478 if (Handle_Buffer( i )) timeout = false;
482 /* noch volle Schreib-Puffer suchen */
483 for( i = 0; i < Pool_Size; i++ ) {
484 if ( My_Connections[i].sock <= NONE )
487 wdatalen = array_bytes(&My_Connections[i].wbuf);
490 if (( wdatalen > 0 ) || ( array_bytes(&My_Connections[i].zip.wbuf)> 0 ))
495 /* Socket der Verbindung in Set aufnehmen */
496 io_event_add( My_Connections[i].sock, IO_WANTWRITE );
500 /* von welchen Sockets koennte gelesen werden? */
501 for (i = 0; i < Pool_Size; i++ ) {
502 if ( My_Connections[i].sock <= NONE )
505 if (Resolve_INPROGRESS(&My_Connections[i].res_stat)) {
506 /* wait for completion of Resolver Sub-Process */
507 io_event_del( My_Connections[i].sock, IO_WANTREAD );
511 if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING ))
512 continue; /* wait for completion of connect() */
514 if( My_Connections[i].delaytime > t ) {
515 /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
516 io_event_del( My_Connections[i].sock, IO_WANTREAD );
519 io_event_add( My_Connections[i].sock, IO_WANTREAD );
522 /* (re-)set timeout - tv_sec/usec are undefined after io_dispatch() returns */
524 tv.tv_sec = timeout ? 1 : 0;
526 /* wait for activity */
527 i = io_dispatch( &tv );
528 if (i == -1 && errno != EINTR ) {
529 Log(LOG_EMERG, "Conn_Handler(): io_dispatch(): %s!", strerror(errno));
530 Log(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
535 if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
536 else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
541 * Write a text string into the socket of a connection.
542 * This function automatically appends CR+LF to the string and validates that
543 * the result is a valid IRC message (oversized messages are shortened, for
544 * example). Then it calls the Conn_Write() function to do the actual sending.
545 * @param Idx Index fo the connection.
546 * @param Format Format string, see printf().
547 * @return true on success, false otherwise.
551 Conn_WriteStr( CONN_ID Idx, char *Format, ... )
554 Conn_WriteStr( Idx, Format, va_alist )
560 char buffer[COMMAND_LEN];
564 assert( Idx > NONE );
565 assert( Format != NULL );
568 va_start( ap, Format );
572 if (vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) >= COMMAND_LEN - 2 ) {
574 * The string that should be written to the socket is longer
575 * than the allowed size of COMMAND_LEN bytes (including both
576 * the CR and LF characters). This can be caused by the
577 * IRC_WriteXXX() functions when the prefix of this server had
578 * to be added to an already "quite long" command line which
579 * has been received from a regular IRC client, for example.
581 * We are not allowed to send such "oversized" messages to
582 * other servers and clients, see RFC 2812 2.3 and 2813 3.3
583 * ("these messages SHALL NOT exceed 512 characters in length,
584 * counting all characters including the trailing CR-LF").
586 * So we have a big problem here: we should send more bytes
587 * to the network than we are allowed to and we don't know
588 * the originator (any more). The "old" behaviour of blaming
589 * the receiver ("next hop") is a bad idea (it could be just
590 * an other server only routing the message!), so the only
591 * option left is to shorten the string and to hope that the
592 * result is still somewhat useful ...
596 strcpy (buffer + sizeof(buffer) - strlen(CUT_TXTSUFFIX) - 2 - 1,
602 Log(LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer);
605 strlcat( buffer, "\r\n", sizeof( buffer ));
606 ok = Conn_Write( Idx, buffer, strlen( buffer ));
607 My_Connections[Idx].msg_out++;
611 } /* Conn_WriteStr */
615 Conn_Write( CONN_ID Idx, char *Data, unsigned int Len )
617 /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird
618 * der Client disconnectiert und false geliefert. */
620 assert( Idx > NONE );
621 assert( Data != NULL );
624 /* Ist der entsprechende Socket ueberhaupt noch offen? In einem
625 * "Handler-Durchlauf" kann es passieren, dass dem nicht mehr so
626 * ist, wenn einer von mehreren Conn_Write()'s fehlgeschlagen ist.
627 * In diesem Fall wird hier einfach ein Fehler geliefert. */
628 if( My_Connections[Idx].sock <= NONE )
631 Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
636 /* Pruefen, ob im Schreibpuffer genuegend Platz ist. Ziel ist es,
637 * moeglichts viel im Puffer zu haben und _nicht_ gleich alles auf den
638 * Socket zu schreiben (u.a. wg. Komprimierung). */
639 if( array_bytes(&My_Connections[Idx].wbuf) >= WRITEBUFFER_LEN) {
640 /* Der Puffer ist dummerweise voll. Jetzt versuchen, den Puffer
641 * zu schreiben, wenn das nicht klappt, haben wir ein Problem ... */
642 if( ! Handle_Write( Idx )) return false;
644 /* check again: if our writebuf is twice als large as the initial limit: Kill connection */
645 if( array_bytes(&My_Connections[Idx].wbuf) >= (WRITEBUFFER_LEN*2)) {
646 Log( LOG_NOTICE, "Write buffer overflow (connection %d)!", Idx );
647 Conn_Close( Idx, "Write buffer overflow!", NULL, false );
653 if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
654 /* Daten komprimieren und in Puffer kopieren */
655 if( ! Zip_Buffer( Idx, Data, Len )) return false;
660 /* Daten in Puffer kopieren */
661 if (!array_catb( &My_Connections[Idx].wbuf, Data, Len ))
664 My_Connections[Idx].bytes_out += Len;
667 /* Adjust global write counter */
675 Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
677 /* Close connection. Open pipes of asyncronous resolver
678 * sub-processes are closed down. */
684 double in_z_k, out_z_k;
688 assert( Idx > NONE );
690 /* Is this link already shutting down? */
691 if( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ISCLOSING )) {
692 /* Conn_Close() has been called recursively for this link;
693 * probabe reason: Handle_Write() failed -- see below. */
695 Log( LOG_DEBUG, "Recursive request to close connection: %d", Idx );
700 assert( My_Connections[Idx].sock > NONE );
702 /* Mark link as "closing" */
703 Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCLOSING );
705 if( LogMsg ) txt = LogMsg;
707 if( ! txt ) txt = "Reason unknown";
709 Log( LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx, LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ));
711 /* Search client, if any */
712 c = Client_GetFromConn( Idx );
714 /* Should the client be informed? */
717 /* Send statistics to client if registered as user: */
718 if ((c != NULL) && (Client_Type(c) == CLIENT_USER)) {
720 ":%s NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.",
721 Client_ID(Client_ThisServer()), Client_ID(c),
723 (double)My_Connections[Idx].bytes_in / 1024,
724 (double)My_Connections[Idx].bytes_out / 1024);
728 /* Send ERROR to client (see RFC!) */
730 Conn_WriteStr(Idx, "ERROR :%s", FwdMsg);
732 Conn_WriteStr(Idx, "ERROR :Closing connection.");
735 /* Try to write out the write buffer. Note: Handle_Write() eventually
736 * removes the CLIENT structure associated with this connection if an
737 * error occurs! So we have to re-check if there is still an valid
738 * CLIENT structure after calling Handle_Write() ...*/
739 (void)Handle_Write( Idx );
741 /* Search client, if any (re-check!) */
742 c = Client_GetFromConn( Idx );
744 /* Shut down socket */
745 if( ! io_close( My_Connections[Idx].sock ))
747 /* Oops, we can't close the socket!? This is ... ugly! */
748 Log( LOG_CRIT, "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
751 /* Mark socket as invalid: */
752 My_Connections[Idx].sock = NONE;
754 /* If there is still a client, unregister it now */
755 if( c ) Client_Destroy( c, LogMsg, FwdMsg, true );
757 /* Calculate statistics and log information */
758 in_k = (double)My_Connections[Idx].bytes_in / 1024;
759 out_k = (double)My_Connections[Idx].bytes_out / 1024;
761 if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
762 in_z_k = (double)My_Connections[Idx].zip.bytes_in / 1024;
763 out_z_k = (double)My_Connections[Idx].zip.bytes_out / 1024;
764 in_p = (int)(( in_k * 100 ) / in_z_k );
765 out_p = (int)(( out_k * 100 ) / out_z_k );
766 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 );
771 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 );
774 /* cancel running resolver */
775 if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat)) {
776 Resolve_Shutdown(&My_Connections[Idx].res_stat);
779 /* Servers: Modify time of next connect attempt? */
780 Conf_UnsetServer( Idx );
783 /* Clean up zlib, if link was compressed */
784 if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
785 inflateEnd( &My_Connections[Idx].zip.in );
786 deflateEnd( &My_Connections[Idx].zip.out );
787 array_free(&My_Connections[Idx].zip.rbuf);
788 array_free(&My_Connections[Idx].zip.wbuf);
792 array_free(&My_Connections[Idx].rbuf);
793 array_free(&My_Connections[Idx].wbuf);
794 /* Clean up connection structure (=free it) */
795 Init_Conn_Struct( Idx );
798 Log( LOG_DEBUG, "Shutdown of connection %d completed.", Idx );
804 Conn_SyncServerStruct( void )
806 /* Synchronize server structures (connection IDs):
807 * connections <-> configuration */
813 for( i = 0; i < Pool_Size; i++ ) {
814 /* Established connection? */
815 if (My_Connections[i].sock < 0)
818 /* Server connection? */
819 client = Client_GetFromConn( i );
820 if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue;
822 for( c = 0; c < MAX_SERVERS; c++ )
824 /* Configured server? */
825 if( ! Conf_Server[c].host[0] ) continue;
828 if( strcmp( Conf_Server[c].name, Client_ID( client )) == 0 ) Conf_Server[c].conn_id = i;
831 } /* SyncServerStruct */
835 Handle_Write( CONN_ID Idx )
837 /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
840 unsigned int wdatalen;
842 assert( Idx > NONE );
843 if ( My_Connections[Idx].sock < 0 ) {
846 "Handle_Write() on closed socket, connection %d", Idx);
850 assert( My_Connections[Idx].sock > NONE );
853 Log(LOG_DEBUG, "Handle_Write() called for connection %d ...", Idx);
856 wdatalen = array_bytes(&My_Connections[Idx].wbuf );
858 if (wdatalen == 0 && !array_bytes(&My_Connections[Idx].zip.wbuf)) {
859 io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
863 /* write buffer empty, but not compression buffer?
864 * -> flush compression buffer! */
869 io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
874 /* Zip_Flush() may have changed the write buffer ... */
875 wdatalen = array_bytes(&My_Connections[Idx].wbuf);
877 len = write(My_Connections[Idx].sock,
878 array_start(&My_Connections[Idx].wbuf), wdatalen );
881 if (errno == EAGAIN || errno == EINTR)
884 Log(LOG_ERR, "Write error on connection %d (socket %d): %s!",
885 Idx, My_Connections[Idx].sock, strerror(errno));
886 Conn_Close(Idx, "Write error!", NULL, false);
890 /* move any data not yet written to beginning */
891 array_moveleft(&My_Connections[Idx].wbuf, 1, len);
898 New_Connection( int Sock )
900 /* Neue Client-Verbindung von Listen-Socket annehmen und
901 * CLIENT-Struktur anlegen. */
904 struct request_info req;
906 struct sockaddr_in new_addr;
907 int new_sock, new_sock_len;
913 assert( Sock > NONE );
914 /* Connection auf Listen-Socket annehmen */
915 new_sock_len = sizeof( new_addr );
916 new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
919 Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
924 /* Validate socket using TCP Wrappers */
925 request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL );
927 if( ! hosts_access( &req ))
930 Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr ));
931 Simple_Message( new_sock, "ERROR :Connection refused" );
937 /* Socket initialisieren */
938 Init_Socket( new_sock );
940 /* Check IP-based connection limit */
941 cnt = Count_Connections( new_addr );
942 if(( Conf_MaxConnectionsIP > 0 ) && ( cnt >= Conf_MaxConnectionsIP ))
944 /* Access denied, too many connections from this IP address! */
945 Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP address!", inet_ntoa( new_addr.sin_addr ), cnt);
946 Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" );
951 /* Freie Connection-Struktur suchen */
952 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
953 if( idx >= Pool_Size )
955 new_size = Pool_Size + CONNECTION_POOL;
957 /* Im bisherigen Pool wurde keine freie Connection-Struktur mehr gefunden.
958 * Wenn erlaubt und moeglich muss nun der Pool vergroessert werden: */
960 if( Conf_MaxConnections > 0 )
962 /* Es ist ein Limit konfiguriert */
963 if( Pool_Size >= Conf_MaxConnections )
965 /* Mehr Verbindungen duerfen wir leider nicht mehr annehmen ... */
966 Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
967 Simple_Message( new_sock, "ERROR :Connection limit reached" );
971 if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections;
973 if( new_size < Pool_Size )
975 Log( LOG_ALERT, "Can't accept connection: limit (%d) reached -- overflow!", Pool_Size );
976 Simple_Message( new_sock, "ERROR :Connection limit reached" );
981 ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size );
983 Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
984 Simple_Message( new_sock, "ERROR: Internal error" );
990 Log( LOG_DEBUG, "Allocated new connection pool for %ld items (%ld bytes). [realloc()]", new_size, sizeof( CONNECTION ) * new_size );
993 /* Adjust pointer to new block */
994 My_Connections = (CONNECTION *)ptr;
996 /* Initialize new items */
997 for( idx = Pool_Size; idx < new_size; idx++ ) Init_Conn_Struct( idx );
1000 /* Adjust new pool size */
1001 Pool_Size = new_size;
1004 /* Client-Struktur initialisieren */
1005 c = Client_NewLocal( idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false );
1007 Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
1008 Simple_Message( new_sock, "ERROR :Internal error" );
1013 /* Verbindung registrieren */
1014 Init_Conn_Struct( idx );
1015 My_Connections[idx].sock = new_sock;
1016 My_Connections[idx].addr = new_addr;
1018 /* Neuen Socket registrieren */
1019 if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) {
1020 Simple_Message( new_sock, "ERROR :Internal error" );
1021 Conn_Close( idx, "io_event_create() failed", NULL, false );
1025 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 );
1027 /* Hostnamen ermitteln */
1028 strlcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ), sizeof( My_Connections[idx].host ));
1029 Client_SetHostname( c, My_Connections[idx].host );
1031 Resolve_Addr(&My_Connections[idx].res_stat, &new_addr,
1032 My_Connections[idx].sock, cb_Read_Resolver_Result);
1034 /* Penalty-Zeit setzen */
1035 Conn_SetPenalty( idx, 4 );
1037 } /* New_Connection */
1041 Socket2Index( int Sock )
1043 /* zum Socket passende Connection suchen */
1047 assert( Sock > NONE );
1049 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break;
1051 if( idx >= Pool_Size )
1053 /* die Connection wurde vermutlich (wegen eines
1054 * Fehlers) bereits wieder abgebaut ... */
1056 Log( LOG_DEBUG, "Socket2Index: can't get connection for socket %d!", Sock );
1061 } /* Socket2Index */
1065 Read_Request( CONN_ID Idx )
1067 /* Daten von Socket einlesen und entsprechend behandeln.
1068 * Tritt ein Fehler auf, so wird der Socket geschlossen. */
1074 assert( Idx > NONE );
1075 assert( My_Connections[Idx].sock > NONE );
1078 if (( array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN ) ||
1079 ( array_bytes(&My_Connections[Idx].zip.rbuf) >= ZREADBUFFER_LEN ))
1081 if ( array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN )
1084 /* Der Lesepuffer ist voll */
1085 Log( LOG_ERR, "Receive buffer overflow (connection %d): %d bytes!", Idx,
1086 array_bytes(&My_Connections[Idx].rbuf));
1087 Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
1091 len = read( My_Connections[Idx].sock, readbuf, sizeof readbuf -1 );
1093 Log( LOG_INFO, "%s:%d (%s) is closing the connection ...",
1094 My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port),
1095 inet_ntoa( My_Connections[Idx].addr.sin_addr ));
1096 Conn_Close( Idx, "Socket closed!", "Client closed connection", false );
1101 if( errno == EAGAIN ) return;
1102 Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx,
1103 My_Connections[Idx].sock, strerror( errno ));
1104 Conn_Close( Idx, "Read error!", "Client closed connection", false );
1108 if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
1109 if (!array_catb( &My_Connections[Idx].zip.rbuf, readbuf, len)) {
1110 Log( LOG_ERR, "Could not append recieved data to zip input buffer (connn %d): %d bytes!", Idx, len );
1111 Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
1118 if (!array_cats( &My_Connections[Idx].rbuf, readbuf )) {
1119 Log( LOG_ERR, "Could not append recieved data to input buffer (connn %d): %d bytes!", Idx, len );
1120 Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
1124 /* Update connection statistics */
1125 My_Connections[Idx].bytes_in += len;
1127 /* Update timestamp of last data received if this connection is
1128 * registered as a user, server or service connection. Don't update
1129 * otherwise, so users have at least Conf_PongTimeout seconds time to
1130 * register with the IRC server -- see Check_Connections(). */
1131 c = Client_GetFromConn(Idx);
1132 if (c && (Client_Type(c) == CLIENT_USER
1133 || Client_Type(c) == CLIENT_SERVER
1134 || Client_Type(c) == CLIENT_SERVICE))
1135 My_Connections[Idx].lastdata = time(NULL);
1137 /* Look at the data in the (read-) buffer of this connection */
1139 } /* Read_Request */
1143 Handle_Buffer( CONN_ID Idx )
1145 /* Handle Data in Connections Read-Buffer.
1146 * Return true if a reuqest was handled, false otherwise (also returned on errors). */
1160 if( My_Connections[Idx].delaytime > time( NULL )) return result;
1162 /* unpack compressed data */
1163 if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP ))
1164 if( ! Unzip_Buffer( Idx )) return false;
1167 if (0 == array_bytes(&My_Connections[Idx].rbuf))
1170 if (!array_cat0_temporary(&My_Connections[Idx].rbuf)) /* make sure buf is NULL terminated */
1173 /* A Complete Request end with CR+LF, see RFC 2812. */
1174 ptr = strstr( array_start(&My_Connections[Idx].rbuf), "\r\n" );
1176 if( ptr ) delta = 2; /* complete request */
1179 /* Check for non-RFC-compliant request (only CR or LF)? Unfortunately,
1180 * there are quite a few clients that do this (incl. "mIRC" :-( */
1181 ptr1 = strchr( array_start(&My_Connections[Idx].rbuf), '\r' );
1182 ptr2 = strchr( array_start(&My_Connections[Idx].rbuf), '\n' );
1184 if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
1185 else if( ptr1 ) ptr = ptr1;
1186 else if( ptr2 ) ptr = ptr2;
1193 /* End of request found */
1196 len = ( ptr - (char*) array_start(&My_Connections[Idx].rbuf)) + delta;
1198 if( len < 0 || len > ( COMMAND_LEN - 1 )) {
1199 /* Request must not exceed 512 chars (incl. CR+LF!), see
1200 * RFC 2812. Disconnect Client if this happens. */
1201 Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!",
1202 Idx, array_bytes(&My_Connections[Idx].rbuf), COMMAND_LEN - 1 );
1203 Conn_Close( Idx, NULL, "Request too long", true );
1207 if (len <= 2) { /* request was empty (only '\r\n') */
1208 array_moveleft(&My_Connections[Idx].rbuf, 1, delta); /* delta is either 1 or 2 */
1212 /* remember if stream is already compressed */
1213 old_z = My_Connections[Idx].options & CONN_ZIP;
1216 My_Connections[Idx].msg_in++;
1217 if (!Parse_Request(Idx, (char*)array_start(&My_Connections[Idx].rbuf) ))
1222 array_moveleft(&My_Connections[Idx].rbuf, 1, len);
1225 "Connection %d: %d bytes left in read buffer.",
1226 Idx, array_bytes(&My_Connections[Idx].rbuf));
1229 if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) &&
1230 ( array_bytes(&My_Connections[Idx].rbuf) > 0 ))
1232 /* The last Command activated Socket-Compression.
1233 * Data that was read after that needs to be copied to Unzip-buf
1234 * for decompression */
1235 if( array_bytes(&My_Connections[Idx].rbuf)> ZREADBUFFER_LEN ) {
1236 Log( LOG_ALERT, "Connection %d: No space left in unzip buf (need %u bytes)!",
1237 Idx, array_bytes(&My_Connections[Idx].rbuf ));
1240 if (!array_copy( &My_Connections[Idx].zip.rbuf, &My_Connections[Idx].rbuf ))
1243 array_trunc(&My_Connections[Idx].rbuf);
1245 Log( LOG_DEBUG, "Moved already received data (%u bytes) to uncompression buffer.",
1246 array_bytes(&My_Connections[Idx].zip.rbuf));
1253 } /* Handle_Buffer */
1257 Check_Connections( void )
1259 /* check if connections are alive. if not, play PING-PONG first.
1260 * if this doesn't help either, disconnect client. */
1264 for( i = 0; i < Pool_Size; i++ ) {
1265 if (My_Connections[i].sock < 0)
1268 c = Client_GetFromConn( i );
1269 if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
1271 /* connected User, Server or Service */
1272 if( My_Connections[i].lastping > My_Connections[i].lastdata ) {
1273 /* we already sent a ping */
1274 if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout ) {
1277 Log( LOG_DEBUG, "Connection %d: Ping timeout: %d seconds.", i, Conf_PongTimeout );
1279 Conn_Close( i, NULL, "Ping timeout", true );
1282 else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout ) {
1283 /* we need to sent a PING */
1285 Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
1287 My_Connections[i].lastping = time( NULL );
1288 Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( )));
1293 /* The connection is not fully established yet, so
1294 * we don't do the PING-PONG game here but instead
1295 * disconnect the client after "a short time" if it's
1296 * still not registered. */
1298 if (My_Connections[i].lastdata <
1299 time(NULL) - Conf_PongTimeout) {
1302 "Unregistered connection %d timed out ...",
1305 Conn_Close(i, NULL, "Timeout", false);
1309 } /* Check_Connections */
1313 Check_Servers( void )
1315 /* Check if we can establish further server links */
1321 /* Search all connections, are there results from the resolver? */
1322 for( idx = 0; idx < Pool_Size; idx++ ) {
1323 if( My_Connections[idx].sock != SERVER_WAIT ) continue;
1326 if (Resolve_SUCCESS(&My_Connections[idx].res_stat))
1327 New_Server(Conf_GetServer( idx ), idx);
1330 /* Check all configured servers */
1331 for( i = 0; i < MAX_SERVERS; i++ ) {
1332 /* Valid outgoing server which isn't already connected or disabled? */
1333 if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) ||
1334 ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED ))
1337 /* Is there already a connection in this group? */
1338 if( Conf_Server[i].group > NONE ) {
1339 for (n = 0; n < MAX_SERVERS; n++) {
1340 if (n == i) continue;
1341 if ((Conf_Server[n].conn_id > NONE) &&
1342 (Conf_Server[n].group == Conf_Server[i].group))
1345 if (n < MAX_SERVERS) continue;
1348 /* Check last connect attempt? */
1349 time_now = time(NULL);
1350 if( Conf_Server[i].lasttry > (time_now - Conf_ConnectRetry))
1353 /* Okay, try to connect now */
1354 Conf_Server[i].lasttry = time_now;
1356 /* Search free connection structure */
1357 for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
1358 if (idx >= Pool_Size) {
1359 Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!",
1364 Log( LOG_DEBUG, "Preparing connection %d for \"%s\" ...", idx, Conf_Server[i].host );
1367 Init_Conn_Struct( idx );
1368 My_Connections[idx].sock = SERVER_WAIT;
1369 Conf_Server[i].conn_id = idx;
1371 /* Resolve Hostname. If this fails, try to use it as an IP address */
1372 strlcpy( Conf_Server[i].ip, Conf_Server[i].host, sizeof( Conf_Server[i].ip ));
1373 strlcpy( My_Connections[idx].host, Conf_Server[i].host, sizeof( My_Connections[idx].host ));
1375 assert(Resolve_Getfd(&My_Connections[idx].res_stat) < 0);
1377 Resolve_Name(&My_Connections[idx].res_stat, Conf_Server[i].host, cb_Read_Resolver_Result);
1379 } /* Check_Servers */
1383 New_Server( int Server, CONN_ID Idx )
1385 /* Establish new server link */
1387 struct sockaddr_in new_addr;
1388 struct in_addr inaddr;
1392 assert( Server > NONE );
1393 assert( Idx > NONE );
1395 Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d ... ", Conf_Server[Server].host,
1396 Conf_Server[Server].ip, Conf_Server[Server].port );
1398 #ifdef HAVE_INET_ATON
1399 if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
1401 memset( &inaddr, 0, sizeof( inaddr ));
1402 inaddr.s_addr = inet_addr( Conf_Server[Server].ip );
1403 if( inaddr.s_addr == (unsigned)-1 )
1406 /* Can't convert IP address */
1407 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 );
1411 memset( &new_addr, 0, sizeof( new_addr ));
1412 new_addr.sin_family = AF_INET;
1413 new_addr.sin_addr = inaddr;
1414 new_addr.sin_port = htons( Conf_Server[Server].port );
1416 new_sock = socket( PF_INET, SOCK_STREAM, 0 );
1417 if ( new_sock < 0 ) {
1418 /* Can't create socket */
1419 Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
1423 if( ! Init_Socket( new_sock )) return;
1425 res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
1426 if(( res != 0 ) && ( errno != EINPROGRESS )) {
1427 /* Can't connect socket */
1428 Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
1433 /* Client-Struktur initialisieren */
1434 c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, false );
1436 /* Can't create new client structure */
1437 Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
1442 Client_SetIntroducer( c, c );
1443 Client_SetToken( c, TOKEN_OUTBOUND );
1445 /* Register connection */
1446 My_Connections[Idx].sock = new_sock;
1447 My_Connections[Idx].addr = new_addr;
1448 strlcpy( My_Connections[Idx].host, Conf_Server[Server].host, sizeof( My_Connections[Idx].host ));
1450 /* Register new socket */
1451 if (!io_event_create( new_sock, IO_WANTWRITE, cb_connserver)) {
1452 Log( LOG_ALERT, "io_event_create(): could not add fd %d", strerror(errno));
1453 Conn_Close( Idx, "io_event_create() failed", NULL, false );
1458 Log( LOG_DEBUG, "Registered new connection %d on socket %d.", Idx, My_Connections[Idx].sock );
1460 Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCONNECTING );
1463 Init_Conn_Struct( Idx );
1464 Conf_Server[Server].conn_id = NONE;
1469 Init_Conn_Struct( CONN_ID Idx )
1471 time_t now = time( NULL );
1472 /* Connection-Struktur initialisieren */
1474 memset( &My_Connections[Idx], 0, sizeof ( CONNECTION ));
1475 My_Connections[Idx].sock = NONE;
1476 My_Connections[Idx].lastdata = now;
1477 My_Connections[Idx].lastprivmsg = now;
1478 Resolve_Init(&My_Connections[Idx].res_stat);
1479 } /* Init_Conn_Struct */
1483 Init_Socket( int Sock )
1485 /* Initialize socket (set options) */
1489 if (!io_setnonblock(Sock)) {
1490 Log( LOG_CRIT, "Can't enable non-blocking mode for socket: %s!", strerror( errno ));
1495 /* Don't block this port after socket shutdown */
1497 if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &value, (socklen_t)sizeof( value )) != 0 )
1499 Log( LOG_ERR, "Can't set socket option SO_REUSEADDR: %s!", strerror( errno ));
1500 /* ignore this error */
1503 /* Set type of service (TOS) */
1504 #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
1505 value = IPTOS_LOWDELAY;
1507 Log( LOG_DEBUG, "Setting option IP_TOS on socket %d to IPTOS_LOWDELAY (%d).", Sock, value );
1509 if( setsockopt( Sock, SOL_IP, IP_TOS, &value, (socklen_t)sizeof( value )) != 0 )
1511 Log( LOG_ERR, "Can't set socket option IP_TOS: %s!", strerror( errno ));
1512 /* ignore this error */
1521 cb_Read_Resolver_Result( int r_fd, UNUSED short events )
1523 /* Read result of resolver sub-process from pipe and update the
1524 * apropriate connection/client structure(s): hostname and/or
1525 * IDENT user name.*/
1532 char readbuf[HOST_LEN + 2 + CLIENT_USER_LEN];
1534 char readbuf[HOST_LEN + 1];
1538 Log( LOG_DEBUG, "Resolver: Got callback on fd %d, events %d", r_fd, events );
1541 /* Search associated connection ... */
1542 for( i = 0; i < Pool_Size; i++ ) {
1543 if(( My_Connections[i].sock != NONE )
1544 && ( Resolve_Getfd(&My_Connections[i].res_stat) == r_fd ))
1547 if( i >= Pool_Size ) {
1548 /* Ops, none found? Probably the connection has already
1549 * been closed!? We'll ignore that ... */
1552 Log( LOG_DEBUG, "Resolver: Got callback for unknown connection!?" );
1557 /* Read result from pipe */
1558 len = Resolve_Read(&My_Connections[i].res_stat, readbuf, sizeof readbuf -1);
1562 readbuf[len] = '\0';
1563 identptr = strchr(readbuf, '\n');
1564 assert(identptr != NULL);
1566 Log( LOG_CRIT, "Resolver: Got malformed result!");
1572 Log( LOG_DEBUG, "Got result from resolver: \"%s\" (%u bytes read).", readbuf, len);
1574 /* Okay, we got a complete result: this is a host name for outgoing
1575 * connections and a host name and IDENT user name (if enabled) for
1576 * incoming connections.*/
1577 if( My_Connections[i].sock > NONE ) {
1578 /* Incoming connection. Search client ... */
1579 c = Client_GetFromConn( i );
1580 assert( c != NULL );
1582 /* Only update client information of unregistered clients */
1583 if( Client_Type( c ) == CLIENT_UNKNOWN ) {
1584 strlcpy(My_Connections[i].host, readbuf, sizeof( My_Connections[i].host));
1585 Client_SetHostname( c, readbuf);
1589 Log( LOG_INFO, "IDENT lookup for connection %ld: \"%s\".", i, identptr);
1590 Client_SetUser( c, identptr, true );
1592 Log( LOG_INFO, "IDENT lookup for connection %ld: no result.", i );
1597 else Log( LOG_DEBUG, "Resolver: discarding result for already registered connection %d.", i );
1600 /* Outgoing connection (server link): set the IP address
1601 * so that we can connect to it in the main loop. */
1603 /* Search server ... */
1604 n = Conf_GetServer( i );
1607 strlcpy( Conf_Server[n].ip, readbuf, sizeof( Conf_Server[n].ip ));
1610 /* Reset penalty time */
1611 Conn_ResetPenalty( i );
1614 if (My_Connections[i].sock == SERVER_WAIT) {
1615 n = Conf_GetServer( i );
1618 Conf_Server[n].conn_id = NONE;
1619 Init_Conn_Struct(i);
1622 } /* cb_Read_Resolver_Result */
1626 Simple_Message( int Sock, char *Msg )
1628 char buf[COMMAND_LEN];
1629 /* Write "simple" message to socket, without using compression
1630 * or even the connection write buffers. Used e.g. for error
1631 * messages by New_Connection(). */
1632 assert( Sock > NONE );
1633 assert( Msg != NULL );
1635 strlcpy( buf, Msg, sizeof buf - 2);
1636 strlcat( buf, "\r\n", sizeof buf);
1637 (void)write( Sock, buf, strlen( buf ) );
1638 } /* Simple_Error */
1642 Count_Connections( struct sockaddr_in addr_in )
1647 for( i = 0; i < Pool_Size; i++ ) {
1648 if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].addr.sin_addr.s_addr == addr_in.sin_addr.s_addr )) cnt++;
1651 } /* Count_Connections */