+ /* Client-Struktur initialisieren */
+ if( ! Client_NewLocal( 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 */
+ Init_Conn_Struct( idx );
+ My_Connections[idx].sock = new_sock;
+ My_Connections[idx].addr = new_addr;
+
+ /* 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 );
+
+ /* Hostnamen ermitteln */
+ s = Resolve( inet_ntoa( new_addr.sin_addr ));
+ if( s )
+ {
+ /* Sub-Prozess wurde asyncron gestartet */
+ My_Connections[idx].res_stat = s;
+ }
+ else
+ {
+ /* kann Namen nicht aufloesen */
+ strcpy( My_Connections[idx].host, inet_ntoa( new_addr.sin_addr ));
+ }
+} /* 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;
+ }
+
+ /* 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_PongTimeout )
+ {
+ /* Timeout */
+ Log( LOG_INFO, "Connection %d: Ping timeout.", i );
+ Conn_Close( i, "Ping timeout" );
+ }
+ }
+ else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout )
+ {
+ /* 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 */
+
+
+LOCAL VOID Init_Conn_Struct( INT Idx )
+{
+ /* Connection-Struktur initialisieren */
+
+ My_Connections[Idx].sock = NONE;
+ My_Connections[Idx].res_stat = NULL;
+ My_Connections[Idx].host[0] = '\0';
+ My_Connections[Idx].rbuf[0] = '\0';
+ My_Connections[Idx].rdatalen = 0;
+ My_Connections[Idx].wbuf[0] = '\0';
+ My_Connections[Idx].wdatalen = 0;
+ My_Connections[Idx].lastdata = time( NULL );
+ My_Connections[Idx].lastping = 0;
+} /* Init_Conn_Struct */
+
+
+LOCAL RES_STAT *Resolve( CHAR *Host )
+{
+ /* Hostnamen (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
+ * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
+ * Der Host kann dann nicht aufgeloest werden. */
+
+ RES_STAT *s;
+ INT pid;
+
+ s = malloc( sizeof( RES_STAT ));
+ if( ! s )
+ {
+ Log( LOG_ALERT, "Resolver: Can't alloc memory!" );
+ return NULL;
+ }
+
+ if( pipe( s->out_pipe ) != 0 )
+ {
+ free( s );
+ Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
+ return NULL;
+ }
+
+ if( pipe( s->in_pipe ) != 0 )
+ {
+ free( s );
+ Log( LOG_ALERT, "Resolver: Can't create input pipe: %s!", strerror( errno ));
+ return NULL;
+ }
+
+ pid = fork( );
+ if( pid > 0 )
+ {
+ /* Haupt-Prozess */
+ if( write( s->out_pipe[1], Host, strlen( Host ) + 1 ) != ( strlen( Host ) + 1 ))
+ {
+ free( s );
+ Log( LOG_ALERT, "Resolver: Can't write to child: %s!", strerror( errno ));
+ return NULL;
+ }
+
+ FD_SET( s->in_pipe[0], &My_Resolvers );
+ if( s->in_pipe[0] > My_Max_Fd ) My_Max_Fd = s->in_pipe[0];
+
+ s->pid = pid;
+
+ Log( LOG_DEBUG, "Resolver process for \"%s\" (PID %d) created.", Host, pid );
+ return s;
+ }
+ else if( pid == 0 )
+ {
+ /* Sub-Prozess */
+ Log_Init_Resolver( );
+ Do_Resolve( s->out_pipe[0], s->in_pipe[1] );
+ Log_Exit_Resolver( );
+ exit( 0 );
+ }
+ else
+ {
+ /* Fehler */
+ free( s );
+ Log( LOG_ALERT, "Resolver: Can't fork: %s!", strerror( errno ));
+ return NULL;
+ }
+} /* Resolve */
+
+
+LOCAL VOID Read_Resolver_Result( INT r_fd )
+{
+ /* Ergebnis von Resolver Sub-Prozess aus Pipe lesen
+ * und entsprechende Connection aktualisieren */
+
+ CHAR hostname[HOST_LEN];
+ CLIENT *c;
+ INT i;
+
+ FD_CLR( r_fd, &My_Resolvers );
+
+ /* Anfrage vom Parent lesen */
+ if( read( r_fd, hostname, HOST_LEN) < 0 )
+ {
+ /* Fehler beim Lesen aus der Pipe */
+ close( r_fd );
+ Log( LOG_ALERT, "Resolver: Can't read result: %s!", strerror( errno ));
+ return;
+ }
+
+ for( i = 0; i < MAX_CONNECTIONS; i++ )
+ {
+ /* zugehoerige Connection suchen */
+ if(( My_Connections[i].sock >= 0 ) && ( My_Connections[i].res_stat ) && ( My_Connections[i].res_stat->in_pipe[0] == r_fd )) break;
+ }
+
+ if( i >= MAX_CONNECTIONS )
+ {
+ /* Opsa! Keine passende Connection gefunden!? */
+ close( r_fd );
+ Log( LOG_ALERT, "Resolver: Got result for unknown connection!?" );
+ return;
+ }
+
+ /* Aufraeumen */
+ close( My_Connections[i].res_stat->out_pipe[0] );
+ close( My_Connections[i].res_stat->out_pipe[1] );
+ close( My_Connections[i].res_stat->in_pipe[0] );
+ close( My_Connections[i].res_stat->in_pipe[1] );
+ free( My_Connections[i].res_stat );
+ My_Connections[i].res_stat = NULL;
+
+ /* Hostnamen setzen */
+ strcpy( My_Connections[i].host, hostname );
+ c = Client_GetFromConn( i );
+ if( c )
+ {
+ Log( LOG_DEBUG, "Set hostname: \"%s\".", hostname );
+ Client_SetHostname( c, hostname );
+ }
+} /* Read_Resolver_Result */
+
+
+LOCAL VOID Do_Resolve( INT r_fd, INT w_fd )
+{
+ /* Resolver Sub-Prozess: aufzuloesenden Namen aus
+ * der Pipe lesen, Ergebnis in Pipe schreiben. */
+
+ CHAR host[HOST_LEN], res_host[HOST_LEN];
+ struct hostent *h;
+
+ /* Anfrage vom Parent lesen */
+ if( read( r_fd, host, HOST_LEN) < 0 )
+ {
+ Log_Resolver( LOG_ALERT, "Resolver: Can't read from parent: %s!", strerror( errno ));
+ close( r_fd ); close( w_fd );
+ return;
+ }
+ host[HOST_LEN] = '\0';
+
+ Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", host );
+
+ /* Namen aufloesen */
+ h = gethostbyname( host );
+ if( h ) strcpy( res_host, h->h_name );
+ else
+ {
+ Log_Resolver( LOG_WARNING, "Can't resolce host name (code %d)!", h_errno );
+ strcpy( res_host, host );
+ }
+
+ /* Antwort an Parent schreiben */
+ if( write( w_fd, res_host, strlen( res_host ) + 1 ) != ( strlen( res_host ) + 1 ))
+ {
+ Log_Resolver( LOG_ALERT, "Resolver: Can't write to parent: %s!", strerror( errno ));
+ close( r_fd ); close( w_fd );
+ return;
+ }
+
+ Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to \"%s\".", host, res_host );
+} /* Do_Resolve */