-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 */
-
-
-GLOBAL VOID
-Conn_SetOption( CONN_ID Idx, INT Option )
-{
- /* Option fuer Verbindung setzen.
- * Initial sind alle Optionen _nicht_ gesetzt. */
-
- assert( Idx > NONE );
- assert( Option != 0 );
-
- My_Connections[Idx].options |= Option;
-} /* Conn_SetOption */
-
-
-GLOBAL VOID
-Conn_UnsetOption( CONN_ID Idx, INT Option )
-{
- /* Option fuer Verbindung loeschen */
-
- assert( Idx > NONE );
- assert( Option != 0 );
-
- My_Connections[Idx].options &= ~Option;
-} /* Conn_UnsetOption */
-
-
-GLOBAL INT
-Conn_Options( CONN_ID Idx )
-{
- assert( Idx > NONE );
- return My_Connections[Idx].options;
-} /* Conn_Options */
-
-
-#ifdef USE_ZLIB
-
-GLOBAL BOOLEAN
-Conn_InitZip( CONN_ID Idx )
-{
- /* Kompression fuer Link initialisieren */
-
- assert( Idx > NONE );
-
- My_Connections[Idx].zip.in.avail_in = 0;
- My_Connections[Idx].zip.in.total_in = 0;
- My_Connections[Idx].zip.in.total_out = 0;
- My_Connections[Idx].zip.in.zalloc = NULL;
- My_Connections[Idx].zip.in.zfree = NULL;
- My_Connections[Idx].zip.in.data_type = Z_ASCII;
-
- if( inflateInit( &My_Connections[Idx].zip.in ) != Z_OK )
- {
- /* Fehler! */
- Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib inflate)!", Idx );
- return FALSE;
- }
-
- My_Connections[Idx].zip.out.total_in = 0;
- My_Connections[Idx].zip.out.total_in = 0;
- My_Connections[Idx].zip.out.zalloc = NULL;
- My_Connections[Idx].zip.out.zfree = NULL;
- My_Connections[Idx].zip.out.data_type = Z_ASCII;
-
- if( deflateInit( &My_Connections[Idx].zip.out, Z_DEFAULT_COMPRESSION ) != Z_OK )
- {
- /* Fehler! */
- Log( LOG_ALERT, "Can't initialize compression on connection %d (zlib deflate)!", Idx );
- return FALSE;
- }
-
- My_Connections[Idx].zip.bytes_in = My_Connections[Idx].bytes_in;
- My_Connections[Idx].zip.bytes_out = My_Connections[Idx].bytes_out;
-
- Log( LOG_INFO, "Enabled link compression (zlib) on connection %d.", Idx );
- Conn_SetOption( Idx, CONN_ZIP );
-
- return TRUE;
-} /* Conn_InitZip */
-
-#endif
-
-
-LOCAL BOOLEAN
-Try_Write( CONN_ID Idx )
-{
- /* Versuchen, Daten aus dem Schreib-Puffer in den
- * Socket zu schreiben. */
-
- fd_set write_socket;
- struct timeval tv;
-
- assert( Idx > NONE );
- assert( My_Connections[Idx].sock > NONE );
- assert( My_Connections[Idx].wdatalen > 0 );
-
- /* Timeout initialisieren: 0 Sekunden, also nicht blockieren */
- tv.tv_sec = 0; tv.tv_usec = 0;
-
- FD_ZERO( &write_socket );
- FD_SET( My_Connections[Idx].sock, &write_socket );
- if( select( My_Connections[Idx].sock + 1, NULL, &write_socket, NULL, &tv ) == -1 )
- {
- /* Fehler! */
- if( errno != EINTR )
- {
- 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;
- }
- }
-
- 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:
- * - neue Clients annehmen,
- * - Daten von Clients verarbeiten,
- * - Resolver-Rueckmeldungen annehmen. */
-
- CONN_ID idx;
-
- assert( Sock > NONE );
-
- if( FD_ISSET( Sock, &My_Listeners ))
- {
- /* es ist einer unserer Listener-Sockets: es soll
- * also eine neue Verbindung aufgebaut werden. */
-
- New_Connection( Sock );
- }
- else if( FD_ISSET( Sock, &Resolver_FDs ))
- {
- /* Rueckmeldung von einem Resolver Sub-Prozess */
-
- Read_Resolver_Result( Sock );
- }
- else
- {
- /* Ein Client Socket: entweder ein User oder Server */
-
- idx = Socket2Index( Sock );
- if( idx > NONE ) Read_Request( idx );
- }
-} /* Handle_Read */
-
-
-LOCAL BOOLEAN
-Handle_Write( CONN_ID Idx )
-{
- /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */
-
- INT len, res, err;
-
- assert( Idx > NONE );
- assert( My_Connections[Idx].sock > NONE );
-
- if( FD_ISSET( My_Connections[Idx].sock, &My_Connects ))
- {
- /* es soll nichts geschrieben werden, sondern ein
- * connect() hat ein Ergebnis geliefert */
-
- FD_CLR( My_Connections[Idx].sock, &My_Connects );
-
- /* Ergebnis des connect() ermitteln */
- len = sizeof( err );
- res = getsockopt( My_Connections[Idx].sock, SOL_SOCKET, SO_ERROR, &err, &len );
- assert( len == sizeof( err ));
-
- /* Fehler aufgetreten? */
- if(( res != 0 ) || ( err != 0 ))
- {
- /* Fehler! */
- if( res != 0 ) Log( LOG_CRIT, "getsockopt (connection %d): %s!", Idx, strerror( errno ));
- else Log( LOG_CRIT, "Can't connect socket to \"%s:%d\" (connection %d): %s!", My_Connections[Idx].host, Conf_Server[My_Connections[Idx].our_server].port, Idx, strerror( err ));
-
- /* Socket etc. pp. aufraeumen */
- FD_CLR( My_Connections[Idx].sock, &My_Sockets );
- close( My_Connections[Idx].sock );
- Init_Conn_Struct( Idx );
-
- /* Bei Server-Verbindungen lasttry-Zeitpunkt auf "jetzt" setzen */
- Conf_Server[My_Connections[Idx].our_server].lasttry = time( NULL );
-
- return FALSE;