+ FD_CLR( My_Connections[Idx].sock, &My_Sockets );
+ My_Connections[Idx].sock = NONE;
+} /* Conn_Close */
+
+
+LOCAL BOOLEAN Try_Write( CONN_ID Idx )
+{
+ /* Versuchen, Daten aus dem Schreib-Puffer in den
+ * Socket zu schreiben. */
+
+ fd_set write_socket;
+
+ assert( Idx >= 0 );
+ assert( My_Connections[Idx].sock >= 0 );
+ assert( My_Connections[Idx].wdatalen > 0 );
+
+ FD_ZERO( &write_socket );
+ FD_SET( My_Connections[Idx].sock, &write_socket );
+ if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, 0 ) == -1 )
+ {
+ /* Fehler! */
+ if( errno != EINTR )
+ {
+ Log( LOG_ALERT, "select(): %s!", strerror( errno ));
+ Conn_Close( Idx, NULL );
+ return FALSE;
+ }
+ }
+
+ if( FD_ISSET( My_Connections[Idx].sock, &write_socket )) return Handle_Write( Idx );
+ else return TRUE;
+} /* Try_Write */
+
+
+LOCAL VOID Handle_Read( INT Sock )
+{
+ /* Aktivitaet auf einem Socket verarbeiten */
+
+ CONN_ID idx;
+
+ assert( Sock >= 0 );
+
+ if( FD_ISSET( Sock, &My_Listener ))
+ {
+ /* es ist einer unserer Listener-Sockets: es soll
+ * also eine neue Verbindung aufgebaut werden. */
+
+ New_Connection( Sock );
+ }
+ else
+ {
+ /* Ein Client Socket: entweder ein User oder Server */
+
+ idx = Socket2Index( Sock );
+ Read_Request( idx );
+ }
+} /* Handle_Read */
+
+
+LOCAL BOOLEAN Handle_Write( CONN_ID Idx )
+{
+ /* Daten aus Schreibpuffer versenden */
+
+ INT len;
+
+ assert( Idx >= 0 );
+ assert( My_Connections[Idx].sock >= 0 );
+ assert( My_Connections[Idx].wdatalen > 0 );
+
+ /* Daten schreiben */
+ len = send( My_Connections[Idx].sock, My_Connections[Idx].wbuf, My_Connections[Idx].wdatalen, 0 );
+ if( len < 0 )
+ {
+ /* Oops, ein Fehler! */
+ Log( LOG_ALERT, "Write error (buffer) on connection %d: %s!", Idx, strerror( errno ));
+ Conn_Close( Idx, NULL );
+ return FALSE;
+ }
+
+ /* Puffer anpassen */
+ My_Connections[Idx].wdatalen -= len;
+ memmove( My_Connections[Idx].wbuf, My_Connections[Idx].wbuf + len, My_Connections[Idx].wdatalen );
+
+ return TRUE;
+} /* Handle_Write */
+
+
+LOCAL VOID New_Connection( INT Sock )
+{
+ /* Neue Client-Verbindung von Listen-Socket annehmen und
+ * CLIENT-Struktur anlegen. */
+
+ struct sockaddr_in new_addr;
+ INT new_sock, new_sock_len;
+ CONN_ID idx;
+
+ assert( Sock >= 0 );
+
+ new_sock_len = sizeof( new_addr );
+ new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
+ if( new_sock < 0 )
+ {
+ Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno ));
+ return;
+ }
+
+ /* Freie Connection-Struktur suschen */
+ for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock < 0 ) break;
+ if( idx >= MAX_CONNECTIONS )
+ {
+ Log( LOG_ALERT, "Can't accept connection: limit reached (%d)!", MAX_CONNECTIONS );
+ close( new_sock );
+ return;
+ }
+
+ /* Client-Struktur initialisieren */
+ if( ! Client_New_Local( idx, inet_ntoa( new_addr.sin_addr )))
+ {
+ Log( LOG_ALERT, "Can't accept connection: can't create client structure!" );
+ close( new_sock );
+ return;
+ }
+
+ /* Verbindung registrieren */
+ My_Connections[idx].sock = new_sock;
+ My_Connections[idx].addr = new_addr;
+ My_Connections[idx].rdatalen = 0;
+ My_Connections[idx].wdatalen = 0;
+ My_Connections[idx].lastdata = time( NULL );
+ My_Connections[idx].lastping = 0;
+
+ /* Neuen Socket registrieren */
+ FD_SET( new_sock, &My_Sockets );
+
+ if( new_sock > My_Max_Fd ) My_Max_Fd = new_sock;
+
+ Log( LOG_NOTICE, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock );
+} /* New_Connection */
+
+
+LOCAL CONN_ID Socket2Index( INT Sock )
+{
+ /* zum Socket passende Connection suchen */
+
+ CONN_ID idx;
+
+ assert( Sock >= 0 );
+
+ for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == Sock ) break;
+
+ assert( idx < MAX_CONNECTIONS );
+ return idx;
+} /* Socket2Index */
+
+
+LOCAL VOID Read_Request( CONN_ID Idx )
+{
+ /* Daten von Socket einlesen und entsprechend behandeln.
+ * Tritt ein Fehler auf, so wird der Socket geschlossen. */
+
+ INT len;
+
+ assert( Idx >= 0 );
+ assert( My_Connections[Idx].sock >= 0 );
+
+ len = recv( My_Connections[Idx].sock, My_Connections[Idx].rbuf + My_Connections[Idx].rdatalen, READBUFFER_LEN - My_Connections[Idx].rdatalen, 0 );
+ My_Connections[Idx].rbuf[READBUFFER_LEN] = '\0';
+
+ if( len == 0 )
+ {
+ /* Socket wurde geschlossen */
+ Log( LOG_INFO, "%s:%d is closing the connection ...", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port));
+ Conn_Close( Idx, NULL );
+ return;
+ }
+
+ if( len < 0 )
+ {
+ /* Fehler beim Lesen */
+ Log( LOG_ALERT, "Read error on connection %d: %s!", Idx, strerror( errno ));
+ Conn_Close( Idx, NULL );
+ return;
+ }
+
+ /* Lesebuffer updaten */
+ My_Connections[Idx].rdatalen += len;
+ assert( My_Connections[Idx].rdatalen <= READBUFFER_LEN );
+ My_Connections[Idx].rbuf[My_Connections[Idx].rdatalen] = '\0';
+
+ if( My_Connections[Idx].rdatalen > MAX_CMDLEN )
+ {
+ /* Eine Anfrage darf(!) nicht laenger als 512 Zeichen
+ * (incl. CR+LF!) werden; vgl. RFC 2812. Wenn soetwas
+ * empfangen wird, wird der Client disconnectiert. */
+ Log( LOG_ALERT, "Request too long (connection %d)!", Idx );
+ Conn_Close( Idx, "Request too long!" );
+ return;
+ }
+
+ /* Timestamp aktualisieren */
+ My_Connections[Idx].lastdata = time( NULL );
+
+ Handle_Buffer( Idx );
+} /* Read_Request */
+
+
+LOCAL VOID Handle_Buffer( CONN_ID Idx )
+{
+ CHAR *ptr, *ptr1, *ptr2;
+ INT len, delta;
+
+ /* Eine komplette Anfrage muss mit CR+LF enden, vgl.
+ * RFC 2812. Haben wir eine? */
+ ptr = strstr( My_Connections[Idx].rbuf, "\r\n" );
+
+ if( ptr ) delta = 2;
+ else
+ {
+ /* Nicht RFC-konforme Anfrage mit nur CR oder LF? Leider
+ * machen soetwas viele Clients, u.a. "mIRC" :-( */
+ ptr1 = strchr( My_Connections[Idx].rbuf, '\r' );
+ ptr2 = strchr( My_Connections[Idx].rbuf, '\n' );
+ delta = 1;
+ if( ptr1 && ptr2 ) ptr = ptr1 > ptr2 ? ptr2 : ptr1;
+ else if( ptr1 ) ptr = ptr1;
+ else if( ptr2 ) ptr = ptr2;
+ }
+
+ if( ptr )
+ {
+ /* Ende der Anfrage wurde gefunden */
+ *ptr = '\0';
+ len = ( ptr - My_Connections[Idx].rbuf ) + delta;
+ if( len > delta )
+ {
+ /* Es wurde ein Request gelesen */
+ if( ! Parse_Request( Idx, My_Connections[Idx].rbuf )) return;
+ }
+ else Log( LOG_DEBUG, "Got null-request (connection %d).", Idx );
+
+ /* Puffer anpassen */
+ My_Connections[Idx].rdatalen -= len;
+ memmove( My_Connections[Idx].rbuf, My_Connections[Idx].rbuf + len, My_Connections[Idx].rdatalen );
+ }
+} /* Handle_Buffer */
+
+
+LOCAL VOID Check_Connections( VOID )
+{
+ /* Pruefen, ob Verbindungen noch "alive" sind */
+
+ INT i;
+
+ for( i = 0; i < MAX_CONNECTIONS; i++ )
+ {
+ if( My_Connections[i].sock != NONE )
+ {
+ if( My_Connections[i].lastping > My_Connections[i].lastdata )
+ {
+ /* es wurde bereits ein PING gesendet */
+ if( My_Connections[i].lastping < time( NULL ) - Conf_PONG_Timeout )
+ {
+ /* Timeout */
+ Log( LOG_NOTICE, "Connection %d: Ping timeout." );
+ Conn_Close( i, "Ping timeout" );
+ }
+ }
+ else if( My_Connections[i].lastdata < time( NULL ) - Conf_PING_Timeout )
+ {
+ /* es muss ein PING gesendet werden */
+ Log( LOG_DEBUG, "Connection %d: sending PING ...", i );
+ My_Connections[i].lastping = time( NULL );
+ Conn_WriteStr( i, "PING :%s", This_Server->nick );
+ }
+ }
+ }
+} /* Conn_Check */