* 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.73 2002/09/26 15:59:02 alex Exp $
+ * $Id: conn.c,v 1.82 2002/10/14 22:21:00 alex Exp $
*
* connect.h: Verwaltung aller Netz-Verbindungen ("connections")
*/
time_t lastping; /* Letzter PING */
time_t lastprivmsg; /* Letzte PRIVMSG */
time_t delaytime; /* Nicht beachten bis ("penalty") */
+ LONG bytes_in, bytes_out; /* Counter fuer Statistik */
} CONNECTION;
fd_set read_sockets, write_sockets;
struct timeval tv;
time_t start, t;
- INT i;
+ INT i, idx;
start = time( NULL );
while(( ! NGIRCd_Quit ) && ( ! NGIRCd_Restart ))
/* Fehler (z.B. Interrupt) */
if( errno != EINTR )
{
- Log( LOG_EMERG, "select(): %s!", strerror( errno ));
+ Log( LOG_EMERG, "Conn_Handler: select(): %s!", strerror( errno ));
Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
exit( 1 );
}
/* Koennen Daten geschrieben werden? */
for( i = 0; i < Conn_MaxFD + 1; i++ )
{
- if( FD_ISSET( i, &write_sockets )) Handle_Write( Socket2Index( i ));
+ if( ! FD_ISSET( i, &write_sockets )) continue;
+
+ /* Es kann geschrieben werden ... */
+ idx = Socket2Index( i );
+ if( ! idx ) continue;
+
+ if( ! Handle_Write( idx ))
+ {
+ /* Fehler beim Schreiben! Diesen Socket nun
+ * auch aus dem Read-Set entfernen: */
+ FD_CLR( i, &read_sockets );
+ }
}
/* Daten zum Lesen vorhanden? */
* dem nicht mehr so ist, wenn einer von mehreren
* Conn_Write()'s fehlgeschlagen ist. In diesem Fall
* wird hier einfach ein Fehler geliefert. */
- if( ! My_Connections[Idx].sock > NONE )
+ if( My_Connections[Idx].sock <= NONE )
{
Log( LOG_DEBUG, "Skipped write on closed socket (connection %d).", Idx );
return FALSE;
assert( Idx >= 0 );
assert( My_Connections[Idx].sock > NONE );
+ c = Client_GetFromConn( Idx );
+
if( InformClient )
{
+ /* Statistik an Client melden, wenn User */
+ if(( c != NULL ) && ( Client_Type( c ) == CLIENT_USER ))
+ {
+ Conn_WriteStr( Idx, "NOTICE %s :%sConnection statistics: %.1f kb received, %.1f kb sent.", Client_ThisServer( ), NOTICE_TXTPREFIX, (DOUBLE)My_Connections[Idx].bytes_in / 1024, (DOUBLE)My_Connections[Idx].bytes_out / 1024 );
+ }
+
+ /* ERROR an Client schicken (von RFC so vorgesehen!) */
if( FwdMsg ) Conn_WriteStr( Idx, "ERROR :%s", FwdMsg );
else Conn_WriteStr( Idx, "ERROR :Closing connection." );
if( My_Connections[Idx].sock == NONE ) return;
if( close( My_Connections[Idx].sock ) != 0 )
{
- Log( LOG_ERR, "Error closing connection %d with %s:%d - %s!", Idx, 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, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
}
else
{
- Log( LOG_INFO, "Connection %d with %s:%d closed.", Idx, 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.", Idx, My_Connections[Idx].sock, inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
}
+
+ /* Socket als "ungueltig" markieren */
+ FD_CLR( My_Connections[Idx].sock, &My_Sockets );
+ FD_CLR( My_Connections[Idx].sock, &My_Connects );
+ My_Connections[Idx].sock = NONE;
- c = Client_GetFromConn( Idx );
if( c ) Client_Destroy( c, LogMsg, FwdMsg, TRUE );
if( My_Connections[Idx].res_stat )
free( My_Connections[Idx].res_stat );
}
- /* Bei Server-Verbindungen lasttry-Zeitpunkt so setzen, dass
- * der naechste Verbindungsversuch in RECONNECT_DELAY Sekunden
- * gestartet wird. */
- if(( My_Connections[Idx].our_server >= 0 ) && ( Conf_Server[My_Connections[Idx].our_server].lasttry < time( NULL )))
+ /* 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 ))
{
- /* Okay, die Verbindung stand schon "genuegend lange" */
+ /* Okay, die Verbindung stand schon "genuegend lange":
+ * lasttry-Zeitpunkt so setzen, dass der naechste
+ * Verbindungsversuch in RECONNECT_DELAY Sekunden
+ * gestartet wird. */
Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL ) - Conf_ConnectRetry + RECONNECT_DELAY;
}
- FD_CLR( My_Connections[Idx].sock, &My_Sockets );
- FD_CLR( My_Connections[Idx].sock, &My_Connects );
- My_Connections[Idx].sock = NONE;
+ /* Connection-Struktur loeschen (=freigeben) */
+ Init_Conn_Struct( Idx );
} /* Conn_Close */
} /* Conn_SetPenalty */
+GLOBAL VOID
+Conn_ResetPenalty( CONN_ID Idx )
+{
+ assert( Idx >= 0 );
+ My_Connections[Idx].delaytime = 0;
+} /* Conn_ResetPenalty */
+
+
LOCAL BOOLEAN
Try_Write( CONN_ID Idx )
{
/* Fehler! */
if( errno != EINTR )
{
- Log( LOG_ALERT, "select() failed: %s!", strerror( errno ));
+ Log( LOG_ALERT, "Try_Write: select() failed: %s (con=%d, sock=%d)!", strerror( errno ), Idx, My_Connections[Idx].sock );
Conn_Close( Idx, "Server error!", NULL, FALSE );
return FALSE;
}
/* Ein Client Socket: entweder ein User oder Server */
idx = Socket2Index( Sock );
- Read_Request( idx );
+ if( idx > NONE ) Read_Request( idx );
+ else Log( LOG_DEBUG, "Handle_Read: can't get connection for socket %d!", Sock );
}
} /* Handle_Read */
INT len, res, err;
- assert( Idx >= 0 );
+ assert( Idx >= NONE );
assert( My_Connections[Idx].sock > NONE );
if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
/* PASS und SERVER verschicken */
Conn_WriteStr( Idx, "PASS %s %s", Conf_Server[My_Connections[Idx].our_server].pwd, NGIRCd_ProtoID );
- Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
-
- return TRUE;
+ return Conn_WriteStr( Idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
}
assert( My_Connections[Idx].wdatalen > 0 );
if( len < 0 )
{
/* Oops, ein Fehler! */
- Log( LOG_ERR, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
- Conn_Close( Idx, "Write error (buffer)!", NULL, FALSE );
+ 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 );
return FALSE;
}
+ /* Connection-Statistik aktualisieren */
+ My_Connections[Idx].bytes_out += len;
+
/* Puffer anpassen */
My_Connections[Idx].wdatalen -= len;
memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
return;
}
- /* Freie Connection-Struktur suschen */
+ /* Freie Connection-Struktur suchen */
for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == NONE ) break;
if( idx >= MAX_CONNECTIONS )
{
if( s )
{
/* Sub-Prozess wurde asyncron gestartet */
+ Conn_WriteStr( idx, "NOTICE AUTH :%sLooking up your hostname ...", NOTICE_TXTPREFIX );
My_Connections[idx].res_stat = s;
}
else
}
/* Penalty-Zeit setzen */
- Conn_SetPenalty( idx, 1 );
+ Conn_SetPenalty( idx, 4 );
} /* New_Connection */
for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == Sock ) break;
- assert( idx < MAX_CONNECTIONS );
- return idx;
+ if( idx >= MAX_CONNECTIONS ) return NONE;
+ else return idx;
} /* Socket2Index */
if( len < 0 )
{
/* Fehler beim Lesen */
- Log( LOG_ERR, "Read error on connection %d: %s!", Idx, strerror( errno ));
+ 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 );
return;
}
+ /* Connection-Statistik aktualisieren */
+ My_Connections[Idx].bytes_in += len;
+
/* Lesebuffer updaten */
My_Connections[Idx].rdatalen += len;
assert( My_Connections[Idx].rdatalen < READBUFFER_LEN );
My_Connections[Idx].lastping = 0;
My_Connections[Idx].lastprivmsg = time( NULL );
My_Connections[Idx].delaytime = 0;
+ My_Connections[Idx].bytes_in = 0;
+ My_Connections[Idx].bytes_out = 0;
} /* Init_Conn_Struct */
FD_CLR( r_fd, &Resolver_FDs );
/* Anfrage vom Parent lesen */
- len = read( r_fd, result, HOST_LEN);
+ len = read( r_fd, result, HOST_LEN - 1 );
if( len < 0 )
{
/* Fehler beim Lesen aus der Pipe */
assert( c != NULL );
strcpy( My_Connections[i].host, result );
Client_SetHostname( c, result );
+
+ Conn_WriteStr( i, "NOTICE AUTH :%sGot your hostname.", NOTICE_TXTPREFIX );
}
else
{
assert( My_Connections[i].our_server >= 0 );
strcpy( Conf_Server[My_Connections[i].our_server].ip, result );
}
+
+ /* Penalty-Zeit zurueck setzen */
+ Conn_ResetPenalty( i );
} /* Read_Resolver_Result */