X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fconn.c;h=85a9966b9859283926d36603b374040ab793ca84;hp=85a90095a57bf7d4be7ad70b89a2d6ecf39506d9;hb=a0c032b2ff96c04d8a2a5215f4183b99296826c3;hpb=b991de0f15c2f2f312bc487d83389e2938bd45e4 diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index 85a90095..85a9966b 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -9,7 +9,7 @@ * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS. * - * $Id: conn.c,v 1.86 2002/11/02 23:00:45 alex Exp $ + * $Id: conn.c,v 1.93 2002/11/22 17:58:19 alex Exp $ * * connect.h: Verwaltung aller Netz-Verbindungen ("connections") */ @@ -76,7 +76,7 @@ typedef struct _Connection time_t lastprivmsg; /* Letzte PRIVMSG */ time_t delaytime; /* Nicht beachten bis ("penalty") */ LONG bytes_in, bytes_out; /* Counter fuer Statistik */ - BOOLEAN flagged; /* Channel ist "markiert" (vgl. "irc-write"-Modul) */ + INT flag; /* Channel-Flag (vgl. "irc-write"-Modul) */ } CONNECTION; @@ -187,6 +187,43 @@ Conn_Exit( VOID ) } /* Conn_Exit */ +GLOBAL INT +Conn_InitListeners( VOID ) +{ + /* Ports, auf denen der Server Verbindungen entgegennehmen + * soll, initialisieren */ + + INT created, i; + + created = 0; + for( i = 0; i < Conf_ListenPorts_Count; i++ ) + { + if( Conn_NewListener( Conf_ListenPorts[i] )) created++; + else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] ); + } + return created; +} /* Conn_InitListeners */ + + +GLOBAL VOID +Conn_ExitListeners( VOID ) +{ + /* Alle "Listen-Sockets" schliessen */ + + INT i; + + Log( LOG_INFO, "Shutting down all listening sockets ..." ); + for( i = 0; i < Conn_MaxFD + 1; i++ ) + { + if( FD_ISSET( i, &My_Sockets ) && FD_ISSET( i, &My_Listeners )) + { + close( i ); + Log( LOG_DEBUG, "Listening socket %d closed.", i ); + } + } +} /* Conn_ExitListeners */ + + GLOBAL BOOLEAN Conn_NewListener( CONST UINT Port ) { @@ -393,7 +430,7 @@ va_dcl BOOLEAN ok; va_list ap; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( Format != NULL ); #ifdef PROTOTYPES @@ -426,7 +463,7 @@ Conn_Write( CONN_ID Idx, CHAR *Data, INT Len ) /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird * der Client disconnectiert und FALSE geliefert. */ - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( Data != NULL ); assert( Len > 0 ); @@ -479,7 +516,7 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) CLIENT *c; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); c = Client_GetFromConn( Idx ); @@ -500,11 +537,11 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) if( close( My_Connections[Idx].sock ) != 0 ) { - Log( LOG_ERR, "Error closing connection %d (socket %d) with %s:%d - %s!", Idx, My_Connections[Idx].sock, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno )); + Log( LOG_ERR, "Error closing connection %d (socket %d) with %s:%d - %s!", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port), strerror( errno )); } else { - Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed.", Idx, My_Connections[Idx].sock, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port )); + Log( LOG_INFO, "Connection %d (socket %d) with %s:%d closed (%.1fK in/%.1fK out).", Idx, My_Connections[Idx].sock, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port ), (DOUBLE)My_Connections[Idx].bytes_in / 1024, (DOUBLE)My_Connections[Idx].bytes_out / 1024 ); } /* Socket als "ungueltig" markieren */ @@ -524,7 +561,7 @@ Conn_Close( CONN_ID Idx, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN InformClient ) } /* Startzeit des naechsten Connect-Versuchs modifizieren? */ - if(( My_Connections[Idx].our_server >= 0 ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry < time( NULL ) - Conf_ConnectRetry )) + if(( My_Connections[Idx].our_server > NONE ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry < time( NULL ) - Conf_ConnectRetry )) { /* Okay, die Verbindung stand schon "genuegend lange": * lasttry-Zeitpunkt so setzen, dass der naechste @@ -543,7 +580,7 @@ Conn_UpdateIdle( CONN_ID Idx ) { /* Idle-Timer zuruecksetzen */ - assert( Idx >= 0 ); + assert( Idx > NONE ); My_Connections[Idx].lastprivmsg = time( NULL ); } @@ -553,7 +590,7 @@ Conn_GetIdle( CONN_ID Idx ) { /* Idle-Time einer Verbindung liefern (in Sekunden) */ - assert( Idx >= 0 ); + assert( Idx > NONE ); return time( NULL ) - My_Connections[Idx].lastprivmsg; } /* Conn_GetIdle */ @@ -563,7 +600,7 @@ Conn_LastPing( CONN_ID Idx ) { /* Zeitpunkt des letzten PING liefern */ - assert( Idx >= 0 ); + assert( Idx > NONE ); return My_Connections[Idx].lastping; } /* Conn_LastPing */ @@ -578,7 +615,7 @@ Conn_SetPenalty( CONN_ID Idx, time_t Seconds ) time_t t; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( Seconds >= 0 ); t = time( NULL ) + Seconds; @@ -589,7 +626,7 @@ Conn_SetPenalty( CONN_ID Idx, time_t Seconds ) GLOBAL VOID Conn_ResetPenalty( CONN_ID Idx ) { - assert( Idx >= NONE ); + assert( Idx > NONE ); My_Connections[Idx].delaytime = 0; } /* Conn_ResetPenalty */ @@ -601,27 +638,27 @@ Conn_ClearFlags( VOID ) LONG i; - for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flagged = FALSE; + for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0; } /* Conn_ClearFlags */ -GLOBAL BOOLEAN +GLOBAL INT Conn_Flag( CONN_ID Idx ) { /* Ist eine Connection markiert (TRUE) oder nicht? */ - assert( Idx >= NONE ); - return My_Connections[Idx].flagged; + assert( Idx > NONE ); + return My_Connections[Idx].flag; } /* Conn_Flag */ GLOBAL VOID -Conn_SetFlag( CONN_ID Idx ) +Conn_SetFlag( CONN_ID Idx, INT Flag ) { /* Connection markieren */ - assert( Idx >= NONE ); - My_Connections[Idx].flagged = TRUE; + assert( Idx > NONE ); + My_Connections[Idx].flag = Flag; } /* Conn_SetFlag */ @@ -649,7 +686,7 @@ Conn_Next( CONN_ID Idx ) LONG i = NONE; - assert( Idx >= NONE ); + assert( Idx > NONE ); for( i = Idx + 1; i < Pool_Size; i++ ) { @@ -659,6 +696,19 @@ Conn_Next( CONN_ID Idx ) } /* Conn_Next */ +GLOBAL VOID +Conn_SetServer( CONN_ID Idx, INT ConfServer ) +{ + /* Connection als Server markieren: Index des konfigurierten + * Servers speichern. Verbindung muss bereits bestehen! */ + + assert( Idx > NONE ); + assert( My_Connections[Idx].sock > NONE ); + + My_Connections[Idx].our_server = ConfServer; +} /* Conn_SetServer */ + + LOCAL BOOLEAN Try_Write( CONN_ID Idx ) { @@ -667,7 +717,7 @@ Try_Write( CONN_ID Idx ) fd_set write_socket; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); assert( My_Connections[Idx].wdatalen > 0 ); @@ -699,7 +749,7 @@ Handle_Read( INT Sock ) CONN_ID idx; - assert( Sock >= 0 ); + assert( Sock > NONE ); if( FD_ISSET( Sock, &My_Listeners )) { @@ -766,7 +816,7 @@ Handle_Write( CONN_ID Idx ) Log( LOG_DEBUG, "Connection %d with \"%s:%d\" established, now sendig PASS and SERVER ...", Idx, My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port ); /* PASS und SERVER verschicken */ - Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd, NGIRCd_ProtoID ); + Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd_out, NGIRCd_ProtoID ); return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo ); } @@ -776,6 +826,9 @@ Handle_Write( CONN_ID Idx ) len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 ); if( len < 0 ) { + /* Operation haette Socket "nur" blockiert ... */ + if( errno == EAGAIN ) return TRUE; + /* Oops, ein Fehler! */ Log( LOG_ERR, "Write error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno )); Conn_Close( Idx, "Write error!", NULL, FALSE ); @@ -807,8 +860,9 @@ New_Connection( INT Sock ) POINTER *ptr; LONG new_size; - assert( Sock >= 0 ); + assert( Sock > NONE ); + /* Connection auf Listen-Socket annehmen */ new_sock_len = sizeof( new_addr ); new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len ); if( new_sock < 0 ) @@ -817,6 +871,9 @@ New_Connection( INT Sock ) return; } + /* Socket initialisieren */ + Init_Socket( new_sock ); + /* Freie Connection-Struktur suchen */ for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break; if( idx >= Pool_Size ) @@ -887,6 +944,8 @@ New_Connection( INT Sock ) 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 ); /* Hostnamen ermitteln */ + strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr )); + Client_SetHostname( c, My_Connections[idx].host ); s = Resolve_Addr( &new_addr ); if( s ) { @@ -894,12 +953,6 @@ New_Connection( INT Sock ) Conn_WriteStr( idx, "NOTICE AUTH :%sLooking up your hostname ...", NOTICE_TXTPREFIX ); My_Connections[idx].res_stat = s; } - else - { - /* kann Namen nicht aufloesen, daher wird die IP-Adresse verwendet */ - strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr )); - Client_SetHostname( c, My_Connections[idx].host ); - } /* Penalty-Zeit setzen */ Conn_SetPenalty( idx, 4 ); @@ -913,7 +966,7 @@ Socket2Index( INT Sock ) CONN_ID idx; - assert( Sock >= 0 ); + assert( Sock > NONE ); for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == Sock ) break; @@ -936,7 +989,7 @@ Read_Request( CONN_ID Idx ) INT len; - assert( Idx >= 0 ); + assert( Idx > NONE ); assert( My_Connections[Idx].sock > NONE ); if( READBUFFER_LEN - My_Connections[Idx].rdatalen - 2 < 0 ) @@ -959,6 +1012,9 @@ Read_Request( CONN_ID Idx ) if( len < 0 ) { + /* Operation haette Socket "nur" blockiert ... */ + if( errno == EAGAIN ) return; + /* Fehler beim Lesen */ Log( LOG_ERR, "Read error on connection %d (socket %d): %s!", Idx, My_Connections[Idx].sock, strerror( errno )); Conn_Close( Idx, "Read error!", "Client closed connection", FALSE ); @@ -1149,19 +1205,16 @@ Check_Servers( VOID ) My_Connections[idx].sock = SERVER_WAIT; My_Connections[idx].our_server = i; - /* Hostnamen in IP aufloesen */ + /* Hostnamen in IP aufloesen (Default bzw. im Fehlerfall: versuchen, den + * konfigurierten Text direkt als IP-Adresse zu verwenden ... */ + strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host ); + strcpy( My_Connections[idx].host, Conf_Server[i].host ); s = Resolve_Name( Conf_Server[i].host ); if( s ) { /* Sub-Prozess wurde asyncron gestartet */ My_Connections[idx].res_stat = s; } - else - { - /* kann Namen nicht aufloesen: nun versuchen wir einfach, - * den "Text" direkt als IP-Adresse zu verwenden ... */ - strcpy( Conf_Server[My_Connections[idx].our_server].ip, Conf_Server[i].host ); - } } } /* Check_Servers */ @@ -1173,11 +1226,11 @@ New_Server( INT Server, CONN_ID Idx ) struct sockaddr_in new_addr; struct in_addr inaddr; - INT new_sock; + INT res, new_sock; CLIENT *c; - assert( Server >= 0 ); - assert( Idx >= 0 ); + assert( Server > NONE ); + assert( Idx > NONE ); /* Wurde eine gueltige IP-Adresse gefunden? */ if( ! Conf_Server[Server].ip[0] ) @@ -1219,13 +1272,12 @@ New_Server( INT Server, CONN_ID Idx ) if( ! Init_Socket( new_sock )) return; - connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )); - if( errno != EINPROGRESS ) + res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )); + if(( res != 0 ) && ( errno != EINPROGRESS )) { - + Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno )); close( new_sock ); Init_Conn_Struct( Idx ); - Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno )); return; } @@ -1274,7 +1326,7 @@ Init_Conn_Struct( LONG Idx ) My_Connections[Idx].delaytime = 0; My_Connections[Idx].bytes_in = 0; My_Connections[Idx].bytes_out = 0; - My_Connections[Idx].flagged = FALSE; + My_Connections[Idx].flag = 0; } /* Init_Conn_Struct */ @@ -1340,6 +1392,8 @@ Read_Resolver_Result( INT r_fd ) return; } + Log( LOG_DEBUG, "Resolver: %s is \"%s\".", My_Connections[i].host, result ); + /* Aufraeumen */ close( My_Connections[i].res_stat->pipe[0] ); close( My_Connections[i].res_stat->pipe[1] ); @@ -1359,7 +1413,7 @@ Read_Resolver_Result( INT r_fd ) else { /* Ausgehende Verbindung (=Server): IP setzen */ - assert( My_Connections[i].our_server >= 0 ); + assert( My_Connections[i].our_server > NONE ); strcpy( Conf_Server[My_Connections[i].our_server].ip, result ); }