2001-12-12 18:18:38 +01:00
|
|
|
|
/*
|
|
|
|
|
* ngIRCd -- The Next Generation IRC Daemon
|
|
|
|
|
* Copyright (c)2001 by Alexander Barton (alex@barton.de)
|
|
|
|
|
*
|
|
|
|
|
* Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
|
|
|
|
|
* der GNU General Public License (GPL), wie von der Free Software Foundation
|
|
|
|
|
* herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
|
|
|
|
|
* der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
|
|
|
|
|
* Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
|
|
|
|
|
* der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
|
|
|
|
|
*
|
2001-12-13 03:04:16 +01:00
|
|
|
|
* $Id: conn.c,v 1.4 2001/12/13 02:04:16 alex Exp $
|
2001-12-12 18:18:38 +01:00
|
|
|
|
*
|
|
|
|
|
* connect.h: Verwaltung aller Netz-Verbindungen ("connections")
|
|
|
|
|
*
|
|
|
|
|
* $Log: conn.c,v $
|
2001-12-13 03:04:16 +01:00
|
|
|
|
* Revision 1.4 2001/12/13 02:04:16 alex
|
|
|
|
|
* - boesen "Speicherschiesser" in Log() gefixt.
|
|
|
|
|
*
|
2001-12-13 02:33:09 +01:00
|
|
|
|
* Revision 1.3 2001/12/13 01:33:09 alex
|
|
|
|
|
* - Conn_Handler() unterstuetzt nun einen Timeout.
|
|
|
|
|
* - fuer Verbindungen werden keine FILE-Handles mehr benutzt.
|
|
|
|
|
* - kleinere "Code Cleanups" ;-)
|
|
|
|
|
*
|
2001-12-13 00:32:02 +01:00
|
|
|
|
* Revision 1.2 2001/12/12 23:32:02 alex
|
|
|
|
|
* - diverse Erweiterungen und Verbesserungen (u.a. sind nun mehrere
|
|
|
|
|
* Verbindungen und Listen-Sockets moeglich).
|
|
|
|
|
*
|
2001-12-12 18:18:38 +01:00
|
|
|
|
* Revision 1.1 2001/12/12 17:18:38 alex
|
|
|
|
|
* - Modul zur Verwaltung aller Netzwerk-Verbindungen begonnen.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <portab.h>
|
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
|
|
#include <imp.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
|
#include <stdint.h> /* u.a. fuer Mac OS X */
|
|
|
|
|
#endif
|
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
|
#include "ngircd.h"
|
2001-12-12 18:18:38 +01:00
|
|
|
|
#include "log.h"
|
2001-12-13 00:32:02 +01:00
|
|
|
|
#include "tool.h"
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
|
|
#include <exp.h>
|
|
|
|
|
#include "conn.h"
|
|
|
|
|
|
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
|
#define MAX_CONNECTIONS 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _Connection
|
|
|
|
|
{
|
|
|
|
|
INT sock; /* Socket Handle */
|
|
|
|
|
struct sockaddr_in addr; /* Adresse des Client */
|
|
|
|
|
} CONNECTION;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID Handle_Socket( INT sock );
|
|
|
|
|
|
|
|
|
|
LOCAL VOID New_Connection( INT Sock );
|
|
|
|
|
|
|
|
|
|
LOCAL INT Socket2Index( INT Sock );
|
|
|
|
|
|
|
|
|
|
LOCAL VOID Close_Connection( INT Idx );
|
2001-12-13 02:33:09 +01:00
|
|
|
|
LOCAL VOID Read_Request( INT Idx );
|
|
|
|
|
|
|
|
|
|
LOCAL BOOLEAN Send( INT Idx, CHAR *Data );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL fd_set My_Listener;
|
|
|
|
|
LOCAL fd_set My_Sockets;
|
|
|
|
|
|
|
|
|
|
LOCAL INT My_Max_Fd;
|
|
|
|
|
|
|
|
|
|
LOCAL CONNECTION My_Connections[MAX_CONNECTIONS];
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL VOID Conn_Init( VOID )
|
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
INT i;
|
|
|
|
|
|
|
|
|
|
/* zu Beginn haben wir keine Verbindungen */
|
|
|
|
|
FD_ZERO( &My_Listener );
|
|
|
|
|
FD_ZERO( &My_Sockets );
|
|
|
|
|
|
|
|
|
|
My_Max_Fd = 0;
|
|
|
|
|
|
|
|
|
|
/* Connection-Struktur initialisieren */
|
|
|
|
|
for( i = 0; i < MAX_CONNECTIONS; i++ )
|
|
|
|
|
{
|
|
|
|
|
My_Connections[i].sock = -1;
|
|
|
|
|
}
|
2001-12-12 18:18:38 +01:00
|
|
|
|
} /* Conn_Init */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL VOID Conn_Exit( VOID )
|
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
INT idx, i;
|
|
|
|
|
|
|
|
|
|
/* Sockets schliessen */
|
|
|
|
|
for( i = 0; i < My_Max_Fd + 1; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( FD_ISSET( i, &My_Sockets ))
|
|
|
|
|
{
|
|
|
|
|
for( idx = 0; idx < MAX_CONNECTIONS; idx++ )
|
|
|
|
|
{
|
|
|
|
|
if( My_Connections[idx].sock == i ) break;
|
|
|
|
|
}
|
|
|
|
|
if( idx < MAX_CONNECTIONS ) Close_Connection( idx );
|
|
|
|
|
else if( FD_ISSET( i, &My_Listener ))
|
|
|
|
|
{
|
|
|
|
|
close( i );
|
|
|
|
|
Log( LOG_INFO, "Closed listening socket %d.", i );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
close( i );
|
|
|
|
|
Log( LOG_WARNING, "Unknown connection %d closed.", i );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-12-12 18:18:38 +01:00
|
|
|
|
} /* Conn_Exit */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL BOOLEAN Conn_New_Listener( CONST INT Port )
|
|
|
|
|
{
|
|
|
|
|
/* Neuen Listen-Socket erzeugen: der Server wartet dann
|
|
|
|
|
* auf dem angegebenen Port auf Verbindungen. */
|
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
INT sock, on = 1;
|
|
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
|
/* Server-"Listen"-Socket initialisieren */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
memset( &addr, 0, sizeof( addr ));
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
addr.sin_port = htons( Port );
|
|
|
|
|
addr.sin_addr.s_addr = htonl( INADDR_ANY );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
|
|
/* Socket erzeugen */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
sock = socket( PF_INET, SOCK_STREAM, 0);
|
2001-12-13 03:04:16 +01:00
|
|
|
|
if( sock < 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
Log( LOG_ALERT, "Can't create socket: %s", strerror( errno ));
|
2001-12-12 18:18:38 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Socket-Optionen setzen */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
if( fcntl( sock, F_SETFL, O_NONBLOCK ) != 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
Log( LOG_ALERT, "Can't enable non-blocking mode: %s", strerror( errno ));
|
|
|
|
|
close( sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
|
if( setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)sizeof( on )) != 0)
|
2001-12-12 18:18:38 +01:00
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
Log( LOG_CRIT, "Can't set socket options: %s", strerror( errno ));
|
2001-12-12 18:18:38 +01:00
|
|
|
|
/* dieser Fehler kann ignoriert werden. */
|
|
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
|
/* an Port binden */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
if( bind( sock, (struct sockaddr *)&addr, (socklen_t)sizeof( addr )) != 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
Log( LOG_ALERT, "Can't bind socket: %s", strerror( errno ));
|
|
|
|
|
close( sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* in "listen mode" gehen :-) */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
if( listen( sock, 10 ) != 0 )
|
2001-12-12 18:18:38 +01:00
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
Log( LOG_ALERT, "Can't listen on soecket: %s", strerror( errno ));
|
|
|
|
|
close( sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
/* Neuen Listener in Strukturen einfuegen */
|
|
|
|
|
FD_SET( sock, &My_Listener );
|
|
|
|
|
FD_SET( sock, &My_Sockets );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
|
if( sock > My_Max_Fd ) My_Max_Fd = sock;
|
|
|
|
|
|
|
|
|
|
Log( LOG_INFO, "Now listening on port %d, socked %d.", Port, sock );
|
|
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
|
return TRUE;
|
|
|
|
|
} /* Conn_New_Listener */
|
|
|
|
|
|
|
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
|
GLOBAL VOID Conn_Handler( INT Timeout )
|
2001-12-12 18:18:38 +01:00
|
|
|
|
{
|
2001-12-13 00:32:02 +01:00
|
|
|
|
fd_set read_sockets;
|
2001-12-13 02:33:09 +01:00
|
|
|
|
struct timeval tv;
|
2001-12-13 00:32:02 +01:00
|
|
|
|
INT i;
|
2001-12-13 02:33:09 +01:00
|
|
|
|
|
|
|
|
|
/* Timeout initialisieren */
|
|
|
|
|
tv.tv_sec = Timeout;
|
|
|
|
|
tv.tv_usec = 0;
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
read_sockets = My_Sockets;
|
2001-12-13 02:33:09 +01:00
|
|
|
|
if( select( My_Max_Fd + 1, &read_sockets, NULL, NULL, &tv ) == -1 )
|
2001-12-13 00:32:02 +01:00
|
|
|
|
{
|
2001-12-13 02:33:09 +01:00
|
|
|
|
if( errno != EINTR ) Log( LOG_ALERT, "select(): %s", strerror( errno ));
|
2001-12-13 00:32:02 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for( i = 0; i < My_Max_Fd + 1; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( FD_ISSET( i, &read_sockets )) Handle_Socket( i );
|
|
|
|
|
}
|
|
|
|
|
} /* Conn_Handler */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID Handle_Socket( INT Sock )
|
|
|
|
|
{
|
|
|
|
|
/* Aktivitaet auf einem Socket verarbeiten */
|
|
|
|
|
|
|
|
|
|
INT idx;
|
|
|
|
|
|
|
|
|
|
if( FD_ISSET( Sock, &My_Listener ))
|
|
|
|
|
{
|
|
|
|
|
/* es ist einer unserer Listener-Sockets: es soll
|
|
|
|
|
* also eine neue Verbindung aufgebaut werden. */
|
|
|
|
|
|
|
|
|
|
New_Connection( Sock );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Ein Client Socket: entweder ein User oder Server */
|
|
|
|
|
|
|
|
|
|
idx = Socket2Index( Sock );
|
2001-12-13 02:33:09 +01:00
|
|
|
|
Read_Request( idx );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
}
|
|
|
|
|
} /* Handle_Socket */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID New_Connection( INT Sock )
|
|
|
|
|
{
|
|
|
|
|
struct sockaddr_in new_addr;
|
|
|
|
|
INT new_sock, new_sock_len, idx;
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
|
new_sock_len = sizeof( new_addr );
|
2001-12-13 03:04:16 +01:00
|
|
|
|
new_sock = accept( Sock, (struct sockaddr *)&new_addr, (socklen_t *)&new_sock_len );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
if( new_sock < 0 )
|
|
|
|
|
{
|
|
|
|
|
Log( LOG_CRIT, "Can't accept connection: %s", strerror( errno ));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Freie Connection-Struktur suschen */
|
|
|
|
|
for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock < 0 ) break;
|
|
|
|
|
if( idx >= MAX_CONNECTIONS )
|
2001-12-12 18:18:38 +01:00
|
|
|
|
{
|
2001-12-13 02:33:09 +01:00
|
|
|
|
Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", MAX_CONNECTIONS );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
close( new_sock );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
/* Verbindung registrieren */
|
|
|
|
|
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;
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
|
Send( idx, "hello world!\n" );
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
2001-12-13 00:32:02 +01:00
|
|
|
|
Log( LOG_INFO, "Accepted connection from %s:%d.", inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port));
|
|
|
|
|
} /* New_Connection */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL INT Socket2Index( INT Sock )
|
|
|
|
|
{
|
|
|
|
|
INT idx;
|
|
|
|
|
|
|
|
|
|
for( idx = 0; idx < MAX_CONNECTIONS; idx++ ) if( My_Connections[idx].sock == Sock ) break;
|
|
|
|
|
assert( idx < MAX_CONNECTIONS );
|
|
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
|
} /* Socket2Index */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID Close_Connection( INT Idx )
|
|
|
|
|
{
|
|
|
|
|
/* Verbindung schlie<69>en */
|
|
|
|
|
|
|
|
|
|
assert( My_Connections[Idx].sock >= 0 );
|
|
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
|
if( close( My_Connections[Idx].sock ) != 0 )
|
2001-12-13 00:32:02 +01:00
|
|
|
|
{
|
|
|
|
|
Log( LOG_ERR, "Error closing connection with %s:%d - %s", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port), strerror( errno ));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log( LOG_INFO, "Closed connection with %s:%d.", inet_ntoa( My_Connections[Idx].addr.sin_addr ), ntohs( My_Connections[Idx].addr.sin_port ));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FD_CLR( My_Connections[Idx].sock, &My_Sockets );
|
|
|
|
|
My_Connections[Idx].sock = -1;
|
|
|
|
|
} /* Close_Connection */
|
|
|
|
|
|
|
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
|
LOCAL VOID Read_Request( INT Idx )
|
2001-12-13 00:32:02 +01:00
|
|
|
|
{
|
2001-12-13 02:33:09 +01:00
|
|
|
|
/* Daten von Socket einlesen und entsprechend behandeln.
|
|
|
|
|
* Tritt ein Fehler auf, so wird der Socket geschlossen. */
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
|
|
|
|
#define SIZE 256
|
|
|
|
|
|
|
|
|
|
CHAR buffer[SIZE];
|
2001-12-13 02:33:09 +01:00
|
|
|
|
INT len;
|
2001-12-13 00:32:02 +01:00
|
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
|
len = recv( My_Connections[Idx].sock, buffer, SIZE, 0 );
|
|
|
|
|
|
|
|
|
|
if( len == 0 )
|
|
|
|
|
{
|
|
|
|
|
/* Socket wurde geschlossen */
|
|
|
|
|
Close_Connection( Idx );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( len < 0 )
|
2001-12-13 00:32:02 +01:00
|
|
|
|
{
|
2001-12-13 02:33:09 +01:00
|
|
|
|
/* Fehler beim Lesen */
|
|
|
|
|
Log( LOG_ALERT, "Read error on socket %d!", My_Connections[Idx].sock );
|
2001-12-13 00:32:02 +01:00
|
|
|
|
Close_Connection( Idx );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngt_Trim_Str( buffer );
|
|
|
|
|
printf( " in: '%s'\n", buffer );
|
|
|
|
|
} /* Read_Data */
|
2001-12-12 18:18:38 +01:00
|
|
|
|
|
|
|
|
|
|
2001-12-13 02:33:09 +01:00
|
|
|
|
LOCAL BOOLEAN Send( INT Idx, CHAR *Data )
|
|
|
|
|
{
|
|
|
|
|
/* Daten in Socket schreiben, ggf. in mehreren Stuecken. Tritt
|
|
|
|
|
* ein Fehler auf, so wird die Verbindung beendet und FALSE
|
|
|
|
|
* als Rueckgabewert geliefert. */
|
|
|
|
|
|
|
|
|
|
INT n, sent, len;
|
|
|
|
|
|
|
|
|
|
sent = 0;
|
|
|
|
|
len = strlen( Data );
|
|
|
|
|
|
|
|
|
|
while( sent < len )
|
|
|
|
|
{
|
|
|
|
|
n = send( My_Connections[Idx].sock, Data + sent, len - sent, 0 );
|
|
|
|
|
if( n <= 0 )
|
|
|
|
|
{
|
|
|
|
|
/* Oops, ein Fehler! */
|
|
|
|
|
Log( LOG_ALERT, "Write error on socket %d!", My_Connections[Idx].sock );
|
|
|
|
|
Close_Connection( Idx );
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
sent += n;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
} /* Send */
|
|
|
|
|
|
|
|
|
|
|
2001-12-12 18:18:38 +01:00
|
|
|
|
/* -eof- */
|