#include "portab.h"
#include "io.h"
-static char UNUSED id[] = "$Id: conn.c,v 1.171 2005/08/27 23:33:11 alex Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.180 2005/09/11 11:42:48 fw Exp $";
#include "imp.h"
#include <assert.h>
/* Send statistics to client if registered as user: */
if ((c != NULL) && (Client_Type(c) == CLIENT_USER)) {
Conn_WriteStr( Idx,
- "NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.",
- Client_ID(Client_ThisServer()), NOTICE_TXTPREFIX,
+ ":%s NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.",
+ Client_ID(Client_ThisServer()), Client_ID(c),
+ NOTICE_TXTPREFIX,
(double)My_Connections[Idx].bytes_in / 1024,
(double)My_Connections[Idx].bytes_out / 1024);
}
int len;
unsigned int wdatalen;
- Log(LOG_DEBUG, "Handle_Write");
assert( Idx > NONE );
if ( My_Connections[Idx].sock < 0 ) {
- Log(LOG_WARNING, "Handle_Write() on closed socket, idx %d", Idx);
+#ifdef DEBUG
+ Log(LOG_DEBUG,
+ "Handle_Write() on closed socket, connection %d", Idx);
+#endif
return false;
}
- assert( My_Connections[Idx].sock > NONE );
+
+#ifdef DEBUG
+ Log(LOG_DEBUG, "Handle_Write() called for connection %d ...", Idx);
+#endif
wdatalen = array_bytes(&My_Connections[Idx].wbuf );
#ifdef ZLIB
- if(( wdatalen == 0 ) && ( ! array_bytes(&My_Connections[Idx].zip.wbuf))) {
+ if (wdatalen == 0 && !array_bytes(&My_Connections[Idx].zip.wbuf)) {
io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
return true;
}
- /* write buffer empty, but not compression buf? -> flush compression buf. */
- if( wdatalen == 0 ) Zip_Flush( Idx );
+ /* write buffer empty, but not compression buffer?
+ * -> flush compression buffer! */
+ if (wdatalen == 0)
+ Zip_Flush(Idx);
#else
- if( wdatalen == 0 ) {
+ if (wdatalen == 0) {
io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
return true;
}
#endif
- wdatalen = array_bytes(&My_Connections[Idx].wbuf ); /* Zip_Flush may change wbuf */
- len = write( My_Connections[Idx].sock, array_start(&My_Connections[Idx].wbuf), wdatalen );
+ /* Zip_Flush() may have changed the write buffer ... */
+ wdatalen = array_bytes(&My_Connections[Idx].wbuf);
+
+ len = write(My_Connections[Idx].sock,
+ array_start(&My_Connections[Idx].wbuf), wdatalen );
+
if( len < 0 ) {
- if( errno == EAGAIN || errno == EINTR)
+ if (errno == EAGAIN || errno == EINTR)
return true;
- 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 );
+ 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;
}
#endif
char *ptr;
int len, delta;
- bool action, result;
+ bool result;
#ifdef ZLIB
bool old_z;
#endif
result = false;
- do {
+ for (;;) {
/* Check penalty */
if( My_Connections[Idx].delaytime > time( NULL )) return result;
#ifdef ZLIB
}
#endif
- action = false;
if( ! ptr )
break;
return false;
}
+ if (len <= 2) { /* request was empty (only '\r\n') */
+ array_moveleft(&My_Connections[Idx].rbuf, 1, delta); /* delta is either 1 or 2 */
+ break;
+ }
#ifdef ZLIB
/* remember if stream is already compressed */
old_z = My_Connections[Idx].options & CONN_ZIP;
#endif
- if( len > delta )
- {
- /* A Request was read */
- My_Connections[Idx].msg_in++;
- if( ! Parse_Request( Idx, (char*)array_start(&My_Connections[Idx].rbuf) )) return false;
- else action = true;
+ My_Connections[Idx].msg_in++;
+ if (!Parse_Request(Idx, (char*)array_start(&My_Connections[Idx].rbuf) ))
+ return false;
+
+ result = true;
- array_moveleft(&My_Connections[Idx].rbuf, 1, len);
+ array_moveleft(&My_Connections[Idx].rbuf, 1, len);
#ifdef DEBUG
- Log(LOG_DEBUG, "%d byte left in rbuf", array_bytes(&My_Connections[Idx].rbuf));
+ Log(LOG_DEBUG,
+ "Connection %d: %d bytes left in read buffer.",
+ Idx, array_bytes(&My_Connections[Idx].rbuf));
#endif
- }
-
#ifdef ZLIB
- if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) && ( array_bytes(&My_Connections[Idx].rbuf) > 0 ))
+ if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) &&
+ ( array_bytes(&My_Connections[Idx].rbuf) > 0 ))
{
/* The last Command activated Socket-Compression.
* Data that was read after that needs to be copied to Unzip-buf
* for decompression */
if( array_bytes(&My_Connections[Idx].rbuf)> ZREADBUFFER_LEN ) {
- /* No space left */
- Log( LOG_ALERT, "Can't move receive buffer: No space left in unzip buffer (need %d bytes)!", array_bytes(&My_Connections[Idx].rbuf ));
+ Log( LOG_ALERT, "Connection %d: No space left in unzip buf (need %u bytes)!",
+ Idx, array_bytes(&My_Connections[Idx].rbuf ));
return false;
}
if (!array_copy( &My_Connections[Idx].zip.rbuf, &My_Connections[Idx].rbuf ))
array_trunc(&My_Connections[Idx].rbuf);
#ifdef DEBUG
- Log( LOG_DEBUG, "Moved already received data (%d bytes) to uncompression buffer.", array_bytes(&My_Connections[Idx].zip.rbuf));
+ Log( LOG_DEBUG, "Moved already received data (%u bytes) to uncompression buffer.",
+ array_bytes(&My_Connections[Idx].zip.rbuf));
#endif /* DEBUG */
}
#endif /* ZLIB */
- if( action ) result = true;
- } while( action );
+ }
return result;
} /* Handle_Buffer */
CONN_ID i;
for( i = 0; i < Pool_Size; i++ ) {
- if( My_Connections[i].sock == NONE ) continue;
+ if (My_Connections[i].sock < 0)
+ continue;
c = Client_GetFromConn( i );
if( c && (( Client_Type( c ) == CLIENT_USER ) || ( Client_Type( c ) == CLIENT_SERVER ) || ( Client_Type( c ) == CLIENT_SERVICE )))
CONN_ID idx;
int i, n;
- /* Serach all connections, are there results from the resolver? */
- for( idx = 0; idx < Pool_Size; idx++ )
- {
+ /* Search all connections, are there results from the resolver? */
+ for( idx = 0; idx < Pool_Size; idx++ ) {
if( My_Connections[idx].sock != SERVER_WAIT ) continue;
/* IP resolved? */
}
/* Check all configured servers */
- for( i = 0; i < MAX_SERVERS; i++ )
- {
+ for( i = 0; i < MAX_SERVERS; i++ ) {
/* Valid outgoing server which isn't already connected or disabled? */
- if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) || ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED )) continue;
+ if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) ||
+ ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED ))
+ continue;
/* Is there already a connection in this group? */
- if( Conf_Server[i].group > NONE )
- {
- for( n = 0; n < MAX_SERVERS; n++ )
- {
- if( n == i ) continue;
- if(( Conf_Server[n].conn_id > NONE ) && ( Conf_Server[n].group == Conf_Server[i].group )) break;
+ if( Conf_Server[i].group > NONE ) {
+ for (n = 0; n < MAX_SERVERS; n++) {
+ if (n == i) continue;
+ if ((Conf_Server[n].conn_id > NONE) &&
+ (Conf_Server[n].group == Conf_Server[i].group))
+ break;
}
- if( n < MAX_SERVERS ) continue;
+ if (n < MAX_SERVERS) continue;
}
/* Check last connect attempt? */
- if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry ) continue;
+ if( Conf_Server[i].lasttry > time( NULL ) - Conf_ConnectRetry )
+ continue;
/* Okay, try to connect now */
Conf_Server[i].lasttry = time( NULL );
/* Search free connection structure */
for( idx = 0; idx < Pool_Size; idx++ ) if( My_Connections[idx].sock == NONE ) break;
- if( idx >= Pool_Size )
- {
- Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!", Pool_Size );
+ if (idx >= Pool_Size) {
+ Log( LOG_ALERT, "Can't establist server connection: connection limit reached (%d)!",
+ Pool_Size );
return;
}
#ifdef DEBUG
/* Did we get a valid IP address? */
if( ! Conf_Server[Server].ip[0] ) {
/* No. Free connection structure and abort: */
- Log( LOG_ERR, "Can't connect to \"%s\" (connection %d): ip address unknown!", Conf_Server[Server].host, Idx );
- goto out;
+ Log( LOG_ERR, "Can't connect to \"%s\": ip address unknown!", Conf_Server[Server].host );
+ Init_Conn_Struct( Idx );
+ Conf_Server[Server].conn_id = NONE;
+ return;
}
- Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d (connection %d) ... ", Conf_Server[Server].host, Conf_Server[Server].ip, Conf_Server[Server].port, Idx );
+ Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d ... ", Conf_Server[Server].host,
+ Conf_Server[Server].ip, Conf_Server[Server].port );
#ifdef HAVE_INET_ATON
if( inet_aton( Conf_Server[Server].ip, &inaddr ) == 0 )
Log( LOG_DEBUG, "Resolver: started, fd %d", r_fd );
/* Search associated connection ... */
- for( i = 0; i < Pool_Size; i++ )
- {
+ for( i = 0; i < Pool_Size; i++ ) {
if(( My_Connections[i].sock != NONE )
&& ( My_Connections[i].res_stat != NULL )
&& ( My_Connections[i].res_stat->pipe[0] == r_fd ))
break;
}
- if( i >= Pool_Size )
- {
+ if( i >= Pool_Size ) {
/* Ops, none found? Probably the connection has already
* been closed!? We'll ignore that ... */
io_close( r_fd );
int i, cnt;
cnt = 0;
- for( i = 0; i < Pool_Size; i++ )
- {
+ for( i = 0; i < Pool_Size; i++ ) {
if(( My_Connections[i].sock > NONE ) && ( My_Connections[i].addr.sin_addr.s_addr == addr_in.sin_addr.s_addr )) cnt++;
}
return cnt;