X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fconn.c;h=530be7cfec8d4288919f5d6aa97a0859b42cf8f2;hb=dd3a3bc6039bc1fd1a89ffb834f08665c8035b6a;hp=cf3ac86e8c9225b6cd895e27cb9750b245576665;hpb=93df629b695ed6a60a3bb9e259cb7747c161ce90;p=ngircd-alex.git diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index cf3ac86e..530be7cf 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -17,7 +17,7 @@ #include "portab.h" #include "io.h" -static char UNUSED id[] = "$Id: conn.c,v 1.190 2006/02/16 19:21:57 fw Exp $"; +static char UNUSED id[] = "$Id: conn.c,v 1.195 2006/05/10 21:24:01 alex Exp $"; #include "imp.h" #include @@ -92,7 +92,7 @@ static void Check_Servers PARAMS(( void )); static void Init_Conn_Struct PARAMS(( CONN_ID Idx )); static bool Init_Socket PARAMS(( int Sock )); static void New_Server PARAMS(( int Server )); -static void Simple_Message PARAMS(( int Sock, char *Msg )); +static void Simple_Message PARAMS(( int Sock, const char *Msg )); static int Count_Connections PARAMS(( struct sockaddr_in addr )); static int NewListener PARAMS(( const UINT16 Port )); @@ -152,7 +152,7 @@ cb_connserver(int sock, UNUSED short what) /* Clean up the CLIENT structure (to avoid silly log * messages) and call Conn_Close() to do the rest. */ - c = Client_GetFromConn(idx); + c = Conn_GetClient(idx); if (c) Client_DestroyNow(c); @@ -216,12 +216,13 @@ Conn_Init( void ) if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections; } - if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), Pool_Size)) { + if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), (size_t)Pool_Size)) { Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" ); exit( 1 ); } - /* XXX: My_Connetions/Pool_Size are needed by other parts of the code; remove them */ + /* FIXME: My_Connetions/Pool_Size is needed by other parts of the + * code; remove them! */ My_Connections = (CONNECTION*) array_start(&My_ConnArray); LogDebug("Allocated connection pool for %d items (%ld bytes).", @@ -266,10 +267,11 @@ Conn_Exit( void ) } /* Conn_Exit */ -static unsigned int +static int ports_initlisteners(array *a, void (*func)(int,short)) { - unsigned int created = 0, len; + int created = 0; + size_t len; int fd; UINT16 *port; @@ -301,7 +303,7 @@ Conn_InitListeners( void ) { /* Initialize ports on which the server should accept connections */ - unsigned int created; + int created; if (!io_library_init(CONNECTION_POOL)) { Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno)); @@ -328,7 +330,7 @@ Conn_ExitListeners( void ) Log( LOG_INFO, "Shutting down all listening sockets (%d total)...", arraylen ); fd = array_start(&My_Listeners); while(arraylen--) { - assert(fd); + assert(fd != NULL); assert(*fd >= 0); io_close(*fd); LogDebug("Listening socket %d closed.", *fd ); @@ -354,7 +356,7 @@ NewListener( const UINT16 Port ) /* Server-"Listen"-Socket initialisieren */ memset( &addr, 0, sizeof( addr )); memset( &inaddr, 0, sizeof( inaddr )); - addr.sin_family = AF_INET; + addr.sin_family = (sa_family_t)AF_INET; addr.sin_port = htons( Port ); if( Conf_ListenAddress[0] ) { @@ -381,7 +383,7 @@ NewListener( const UINT16 Port ) if( ! Init_Socket( sock )) return -1; - if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 ) { + if (bind(sock, (struct sockaddr *)&addr, (int)sizeof(addr)) != 0) { Log( LOG_CRIT, "Can't bind socket: %s!", strerror( errno )); close( sock ); return -1; @@ -424,8 +426,11 @@ NewListener( const UINT16 Port ) } /* Add port number to description if non-standard */ - if( Port != 6667 ) snprintf( name, sizeof( name ), "%s (port %u)", info, Port ); - else strlcpy( name, info, sizeof( name )); + if (Port != 6667) + snprintf(name, sizeof name, "%s (port %u)", info, + (unsigned int)Port); + else + strlcpy(name, info, sizeof name); /* Register service */ Rendezvous_Register( name, MDNS_TYPE, Port ); @@ -482,7 +487,7 @@ Conn_Handler( void ) if ( My_Connections[i].sock <= NONE ) continue; - wdatalen = array_bytes(&My_Connections[i].wbuf); + wdatalen = (unsigned int)array_bytes(&My_Connections[i].wbuf); #ifdef ZLIB if (( wdatalen > 0 ) || ( array_bytes(&My_Connections[i].zip.wbuf)> 0 )) @@ -556,6 +561,7 @@ va_dcl #endif { char buffer[COMMAND_LEN]; + size_t len; bool ok; va_list ap; @@ -600,8 +606,8 @@ va_dcl Log(LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer); #endif - strlcat( buffer, "\r\n", sizeof( buffer )); - ok = Conn_Write( Idx, buffer, strlen( buffer )); + len = strlcat( buffer, "\r\n", sizeof( buffer )); + ok = Conn_Write(Idx, buffer, len); My_Connections[Idx].msg_out++; va_end( ap ); @@ -610,7 +616,7 @@ va_dcl GLOBAL bool -Conn_Write( CONN_ID Idx, char *Data, unsigned int Len ) +Conn_Write( CONN_ID Idx, char *Data, size_t Len ) { /* Daten in Socket schreiben. Bei "fatalen" Fehlern wird * der Client disconnectiert und false geliefert. */ @@ -702,7 +708,7 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient ) Log( LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx, LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host, ntohs( My_Connections[Idx].addr.sin_port )); /* Search client, if any */ - c = Client_GetFromConn( Idx ); + c = Conn_GetClient( Idx ); /* Should the client be informed? */ if (InformClient) { @@ -732,7 +738,7 @@ Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient ) (void)Handle_Write( Idx ); /* Search client, if any (re-check!) */ - c = Client_GetFromConn( Idx ); + c = Conn_GetClient( Idx ); /* Shut down socket */ if( ! io_close( My_Connections[Idx].sock )) @@ -807,7 +813,7 @@ Conn_SyncServerStruct( void ) continue; /* Server connection? */ - client = Client_GetFromConn( i ); + client = Conn_GetClient( i ); if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue; for( c = 0; c < MAX_SERVERS; c++ ) @@ -823,13 +829,14 @@ Conn_SyncServerStruct( void ) } /* SyncServerStruct */ +/** + * Send out data of write buffer; connect new sockets. + */ static bool Handle_Write( CONN_ID Idx ) { - /* Daten aus Schreibpuffer versenden bzw. Connection aufbauen */ - - int len; - unsigned int wdatalen; + ssize_t len; + size_t wdatalen; assert( Idx > NONE ); if ( My_Connections[Idx].sock < 0 ) { @@ -875,7 +882,7 @@ Handle_Write( CONN_ID Idx ) } /* move any data not yet written to beginning */ - array_moveleft(&My_Connections[Idx].wbuf, 1, len); + array_moveleft(&My_Connections[Idx].wbuf, 1, (size_t)len); return true; } /* Handle_Write */ @@ -897,11 +904,11 @@ New_Connection( int Sock ) assert( Sock > NONE ); /* Connection auf Listen-Socket annehmen */ - 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 )); + new_sock_len = (int)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 -1; } @@ -946,7 +953,8 @@ New_Connection( int Sock ) return -1; } - if (!array_alloc(&My_ConnArray, sizeof( CONNECTION ), new_sock)) { + if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), + (size_t)new_sock)) { Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" ); Simple_Message( new_sock, "ERROR: Internal error" ); close( new_sock ); @@ -957,7 +965,8 @@ New_Connection( int Sock ) /* Adjust pointer to new block */ My_Connections = array_start(&My_ConnArray); - Pool_Size = new_Pool_Size; + while (Pool_Size < new_Pool_Size) + Init_Conn_Struct(Pool_Size++); } c = Client_NewLocal( new_sock, inet_ntoa( new_addr.sin_addr ), CLIENT_UNKNOWN, false ); @@ -971,6 +980,7 @@ New_Connection( int Sock ) Init_Conn_Struct( new_sock ); My_Connections[new_sock].sock = new_sock; My_Connections[new_sock].addr = new_addr; + My_Connections[new_sock].client = c; /* register callback */ if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) { @@ -1020,7 +1030,7 @@ Read_Request( CONN_ID Idx ) /* Daten von Socket einlesen und entsprechend behandeln. * Tritt ein Fehler auf, so wird der Socket geschlossen. */ - int len; + ssize_t len; char readbuf[1024]; CLIENT *c; @@ -1058,10 +1068,14 @@ Read_Request( CONN_ID Idx ) return; } #ifdef ZLIB - if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) { - if (!array_catb( &My_Connections[Idx].zip.rbuf, readbuf, len)) { - Log( LOG_ERR, "Could not append recieved data to zip input buffer (connn %d): %d bytes!", Idx, len ); - Conn_Close( Idx, "Receive buffer overflow!", NULL, false ); + if (Conn_OPTION_ISSET(&My_Connections[Idx], CONN_ZIP)) { + if (!array_catb(&My_Connections[Idx].zip.rbuf, readbuf, + (size_t) len)) { + Log(LOG_ERR, + "Could not append recieved data to zip input buffer (connn %d): %d bytes!", + Idx, len); + Conn_Close(Idx, "Receive buffer overflow!", NULL, + false); return; } } else @@ -1080,12 +1094,15 @@ Read_Request( CONN_ID Idx ) /* Update timestamp of last data received if this connection is * registered as a user, server or service connection. Don't update * otherwise, so users have at least Conf_PongTimeout seconds time to - * register with the IRC server -- see Check_Connections(). */ - c = Client_GetFromConn(Idx); + * register with the IRC server -- see Check_Connections(). + * Set "lastping", too, so we can handle time shifts backwards ... */ + c = Conn_GetClient(Idx); if (c && (Client_Type(c) == CLIENT_USER || Client_Type(c) == CLIENT_SERVER - || Client_Type(c) == CLIENT_SERVICE)) + || Client_Type(c) == CLIENT_SERVICE)) { My_Connections[Idx].lastdata = time(NULL); + My_Connections[Idx].lastping = My_Connections[Idx].lastdata; + } /* Look at the data in the (read-) buffer of this connection */ Handle_Buffer(Idx); @@ -1101,16 +1118,18 @@ Handle_Buffer( CONN_ID Idx ) char *ptr1, *ptr2; #endif char *ptr; - int len, delta; + size_t len, delta; bool result; + time_t starttime; #ifdef ZLIB bool old_z; #endif + starttime = time(NULL); result = false; for (;;) { /* Check penalty */ - if( My_Connections[Idx].delaytime > time( NULL )) return result; + if( My_Connections[Idx].delaytime > starttime) return result; #ifdef ZLIB /* unpack compressed data */ if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) @@ -1148,7 +1167,7 @@ Handle_Buffer( CONN_ID Idx ) len = ( ptr - (char*) array_start(&My_Connections[Idx].rbuf)) + delta; - if( len < 0 || len > ( COMMAND_LEN - 1 )) { + if( len > ( COMMAND_LEN - 1 )) { /* Request must not exceed 512 chars (incl. CR+LF!), see * RFC 2812. Disconnect Client if this happens. */ Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!", @@ -1201,39 +1220,43 @@ Handle_Buffer( CONN_ID Idx ) static void -Check_Connections( void ) +Check_Connections(void) { /* check if connections are alive. if not, play PING-PONG first. * if this doesn't help either, disconnect client. */ CLIENT *c; CONN_ID i; - for( i = 0; i < Pool_Size; i++ ) { + for (i = 0; i < Pool_Size; i++) { 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 ))) - { + c = Conn_GetClient(i); + if (c && ((Client_Type(c) == CLIENT_USER) + || (Client_Type(c) == CLIENT_SERVER) + || (Client_Type(c) == CLIENT_SERVICE))) { /* connected User, Server or Service */ - if( My_Connections[i].lastping > My_Connections[i].lastdata ) { - /* we already sent a ping */ - if( My_Connections[i].lastping < time( NULL ) - Conf_PongTimeout ) { + if (My_Connections[i].lastping > + My_Connections[i].lastdata) { + /* We already sent a ping */ + if (My_Connections[i].lastping < + time(NULL) - Conf_PongTimeout) { /* Timeout */ - LogDebug("Connection %d: Ping timeout: %d seconds.", - i, Conf_PongTimeout ); - Conn_Close( i, NULL, "Ping timeout", true ); + LogDebug + ("Connection %d: Ping timeout: %d seconds.", + i, Conf_PongTimeout); + Conn_Close(i, NULL, "Ping timeout", + true); } + } else if (My_Connections[i].lastdata < + time(NULL) - Conf_PingTimeout) { + /* We need to send a PING ... */ + LogDebug("Connection %d: sending PING ...", i); + My_Connections[i].lastping = time(NULL); + Conn_WriteStr(i, "PING :%s", + Client_ID(Client_ThisServer())); } - else if( My_Connections[i].lastdata < time( NULL ) - Conf_PingTimeout ) { - /* we need to sent a PING */ - LogDebug("Connection %d: sending PING ...", i ); - My_Connections[i].lastping = time( NULL ); - Conn_WriteStr( i, "PING :%s", Client_ID( Client_ThisServer( ))); - } - } - else - { + } else { /* The connection is not fully established yet, so * we don't do the PING-PONG game here but instead * disconnect the client after "a short time" if it's @@ -1241,7 +1264,9 @@ Check_Connections( void ) if (My_Connections[i].lastdata < time(NULL) - Conf_PongTimeout) { - LogDebug("Unregistered connection %d timed out ...", i); + LogDebug + ("Unregistered connection %d timed out ...", + i); Conn_Close(i, NULL, "Timeout", false); } } @@ -1317,7 +1342,7 @@ New_Server( int Server ) } memset( &new_addr, 0, sizeof( new_addr )); - new_addr.sin_family = AF_INET; + new_addr.sin_family = (sa_family_t)AF_INET; new_addr.sin_addr = inaddr; new_addr.sin_port = htons( Conf_Server[Server].port ); @@ -1329,15 +1354,18 @@ New_Server( int Server ) if( ! Init_Socket( new_sock )) return; - res = connect( new_sock, (struct sockaddr *)&new_addr, sizeof( new_addr )); + res = connect(new_sock, (struct sockaddr *)&new_addr, + (socklen_t)sizeof(new_addr)); if(( res != 0 ) && ( errno != EINPROGRESS )) { Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno )); close( new_sock ); return; } - if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), new_sock)) { - Log( LOG_ALERT, "Cannot allocate memory for server connection (socket %d)", new_sock); + if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), (size_t)new_sock)) { + Log(LOG_ALERT, + "Cannot allocate memory for server connection (socket %d)", + new_sock); close( new_sock ); return; } @@ -1362,6 +1390,7 @@ New_Server( int Server ) Conf_Server[Server].conn_id = new_sock; My_Connections[new_sock].sock = new_sock; My_Connections[new_sock].addr = new_addr; + My_Connections[new_sock].client = c; strlcpy( My_Connections[new_sock].host, Conf_Server[Server].host, sizeof(My_Connections[new_sock].host )); @@ -1519,7 +1548,7 @@ cb_Read_Resolver_Result( int r_fd, UNUSED short events ) * incoming connections.*/ assert ( My_Connections[i].sock >= 0 ); /* Incoming connection. Search client ... */ - c = Client_GetFromConn( i ); + c = Conn_GetClient( i ); assert( c != NULL ); /* Only update client information of unregistered clients */ @@ -1545,9 +1574,10 @@ cb_Read_Resolver_Result( int r_fd, UNUSED short events ) static void -Simple_Message( int Sock, char *Msg ) +Simple_Message( int Sock, const char *Msg ) { char buf[COMMAND_LEN]; + size_t len; /* Write "simple" message to socket, without using compression * or even the connection write buffers. Used e.g. for error * messages by New_Connection(). */ @@ -1555,8 +1585,8 @@ Simple_Message( int Sock, char *Msg ) assert( Msg != NULL ); strlcpy( buf, Msg, sizeof buf - 2); - strlcat( buf, "\r\n", sizeof buf); - (void)write( Sock, buf, strlen( buf ) ); + len = strlcat( buf, "\r\n", sizeof buf); + (void)write(Sock, buf, len); } /* Simple_Error */ @@ -1573,5 +1603,20 @@ Count_Connections( struct sockaddr_in addr_in ) } /* Count_Connections */ +GLOBAL CLIENT * +Conn_GetClient( CONN_ID Idx ) +{ + /* return Client-Structure that belongs to the local Connection Idx. + * If none is found, return NULL. + */ + CONNECTION *c; + assert( Idx >= 0 ); + + c = array_get(&My_ConnArray, sizeof (CONNECTION), (size_t)Idx); + + assert(c != NULL); + + return c ? c->client : NULL; +} /* -eof- */