#include "portab.h"
#include "io.h"
-static char UNUSED id[] = "$Id: conn.c,v 1.160 2005/07/11 14:11:35 fw Exp $";
+static char UNUSED id[] = "$Id: conn.c,v 1.162 2005/07/11 20:58:05 fw Exp $";
#include "imp.h"
#include <assert.h>
LOCAL void New_Server PARAMS(( int Server, CONN_ID Idx ));
LOCAL void Simple_Message PARAMS(( int Sock, char *Msg ));
LOCAL int Count_Connections PARAMS(( struct sockaddr_in addr ));
+LOCAL int NewListener PARAMS(( const UINT16 Port ));
static array My_Listeners;
{
/* Initialize ports on which the server should accept connections */
- int created;
+ int created, fd;
unsigned int i;
if (!io_library_init(CONNECTION_POOL)) {
}
created = 0;
- for( i = 0; i < Conf_ListenPorts_Count; i++ )
- {
- if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
- else Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] );
+ for( i = 0; i < Conf_ListenPorts_Count; i++ ) {
+ fd = NewListener( Conf_ListenPorts[i] );
+ if (fd < 0) {
+ Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] );
+ continue;
+ }
+ if (!io_event_create( fd, IO_WANTREAD, cb_listen )) {
+ Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!",
+ fd, (unsigned int) Conf_ListenPorts[i], strerror(errno));
+ close(fd);
+ continue;
+ }
+ created++;
}
return created;
} /* Conn_InitListeners */
} /* Conn_ExitListeners */
-GLOBAL bool
-Conn_NewListener( const UINT16 Port )
+LOCAL int
+NewListener( const UINT16 Port )
{
/* Create new listening socket on specified port */
#endif
{
Log( LOG_CRIT, "Can't listen on %s:%u: can't convert ip address %s!", Conf_ListenAddress, Port, Conf_ListenAddress );
- return false;
+ return -1;
}
}
else inaddr.s_addr = htonl( INADDR_ANY );
if( sock < 0 )
{
Log( LOG_CRIT, "Can't create socket: %s!", strerror( errno ));
- return false;
+ return -1;
}
- if( ! Init_Socket( sock )) return false;
+ if( ! Init_Socket( sock )) return -1;
/* an Port binden */
if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
{
Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno ));
close( sock );
- return false;
+ return -1;
}
/* in "listen mode" gehen :-) */
{
Log( LOG_CRIT, "Can't listen on soecket: %s!", strerror( errno ));
close( sock );
- return false;
+ return -1;
}
/* Neuen Listener in Strukturen einfuegen */
if (!array_catb( &My_Listeners,(char*) &sock, sizeof(int) )) {
Log( LOG_CRIT, "Can't add socket to My_Listeners array: %s!", strerror( errno ));
close( sock );
- return false;
+ return -1;
}
- io_event_create( sock, IO_WANTREAD, cb_listen );
if( Conf_ListenAddress[0]) Log( LOG_INFO, "Now listening on %s:%d (socket %d).", Conf_ListenAddress, Port, sock );
else Log( LOG_INFO, "Now listening on 0.0.0.0:%d (socket %d).", Port, sock );
/* Register service */
Rendezvous_Register( name, MDNS_TYPE, Port );
#endif
- return true;
-} /* Conn_NewListener */
+ return sock;
+} /* NewListener */
GLOBAL void
return;
}
- len = read( My_Connections[Idx].sock, readbuf, sizeof readbuf );
+ len = read( My_Connections[Idx].sock, readbuf, sizeof readbuf -1 );
if( len == 0 ) {
Log( LOG_INFO, "%s:%d (%s) is closing the connection ...",
My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port),
new_addr.sin_port = htons( Conf_Server[Server].port );
new_sock = socket( PF_INET, SOCK_STREAM, 0 );
- if ( new_sock < 0 )
- {
+ if ( new_sock < 0 ) {
/* Can't create socket */
Init_Conn_Struct( Idx );
Conf_Server[Server].conn_id = NONE;
if( ! Init_Socket( new_sock )) return;
res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr ));
- if(( res != 0 ) && ( errno != EINPROGRESS ))
- {
+ if(( res != 0 ) && ( errno != EINPROGRESS )) {
/* Can't connect socket */
Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
close( new_sock );
/* Client-Struktur initialisieren */
c = Client_NewLocal( Idx, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWNSERVER, false );
- if( ! c )
- {
+ if( ! c ) {
/* Can't create new client structure */
close( new_sock );
Init_Conn_Struct( Idx );