-LOCAL VOID Handle_Read PARAMS(( INT sock ));
-LOCAL BOOLEAN Handle_Write PARAMS(( CONN_ID Idx ));
-LOCAL VOID New_Connection PARAMS(( INT Sock ));
-LOCAL CONN_ID Socket2Index PARAMS(( INT Sock ));
-LOCAL VOID Read_Request PARAMS(( CONN_ID Idx ));
-LOCAL BOOLEAN Try_Write PARAMS(( CONN_ID Idx ));
-LOCAL BOOLEAN Handle_Buffer PARAMS(( CONN_ID Idx ));
-LOCAL VOID Check_Connections PARAMS(( VOID ));
-LOCAL VOID Check_Servers PARAMS(( VOID ));
-LOCAL VOID Init_Conn_Struct PARAMS(( CONN_ID Idx ));
-LOCAL BOOLEAN Init_Socket PARAMS(( INT Sock ));
-LOCAL VOID New_Server PARAMS(( INT Server, CONN_ID Idx ));
-LOCAL VOID Read_Resolver_Result PARAMS(( INT r_fd ));
-LOCAL VOID Simple_Message PARAMS(( INT Sock, CHAR *Msg ));
-
-LOCAL fd_set My_Listeners;
-LOCAL fd_set My_Sockets;
-LOCAL fd_set My_Connects;
-
-#ifdef USE_TCPWRAP
-INT allow_severity = LOG_INFO;
-INT deny_severity = LOG_ERR;
+static bool Handle_Write PARAMS(( CONN_ID Idx ));
+static int New_Connection PARAMS(( int Sock ));
+static CONN_ID Socket2Index PARAMS(( int Sock ));
+static void Read_Request PARAMS(( CONN_ID Idx ));
+static bool Handle_Buffer PARAMS(( CONN_ID Idx ));
+static void Check_Connections PARAMS(( void ));
+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, const char *Msg ));
+static int Count_Connections PARAMS(( struct sockaddr_in addr ));
+static int NewListener PARAMS(( const UINT16 Port ));
+
+static array My_Listeners;
+static array My_ConnArray;
+
+#ifdef TCPWRAP
+int allow_severity = LOG_INFO;
+int deny_severity = LOG_ERR;
+#endif
+
+static void server_login PARAMS((CONN_ID idx));
+
+static void cb_Read_Resolver_Result PARAMS(( int sock, UNUSED short what));
+static void cb_Connect_to_Server PARAMS(( int sock, UNUSED short what));
+static void cb_clientserver PARAMS((int sock, short what));
+
+static void
+cb_listen(int sock, short irrelevant)
+{
+ (void) irrelevant;
+ New_Connection( sock );
+}
+
+
+static void
+cb_connserver(int sock, UNUSED short what)
+{
+ int res, err;
+ socklen_t sock_len;
+ CLIENT *c;
+ CONN_ID idx = Socket2Index( sock );
+ if (idx <= NONE) {
+ LogDebug("cb_connserver wants to write on unknown socket?!");
+ io_close(sock);
+ return;
+ }
+
+ assert( what & IO_WANTWRITE);
+
+ /* connect() finished, get result. */
+ sock_len = sizeof( err );
+ res = getsockopt( My_Connections[idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
+ assert( sock_len == sizeof( err ));
+
+ /* Error while connecting? */
+ if ((res != 0) || (err != 0)) {
+ 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[Conf_GetServer(idx)].port,
+ idx, strerror(err));
+
+ /* Clean up the CLIENT structure (to avoid silly log
+ * messages) and call Conn_Close() to do the rest. */
+ c = Conn_GetClient(idx);
+ if (c)
+ Client_DestroyNow(c);
+
+ Conn_Close(idx, "Can't connect!", NULL, false);
+
+ return;
+ }
+
+ Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
+ server_login(idx);
+}
+
+
+static void
+server_login(CONN_ID idx)
+{
+ Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", idx,
+ My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port );
+
+ io_event_setcb( My_Connections[idx].sock, cb_clientserver);
+ io_event_add( My_Connections[idx].sock, IO_WANTREAD|IO_WANTWRITE);
+
+ /* Send PASS and SERVER command to peer */
+ Conn_WriteStr( idx, "PASS %s %s", Conf_Server[Conf_GetServer( idx )].pwd_out, NGIRCd_ProtoID );
+ Conn_WriteStr( idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
+}
+
+
+static void
+cb_clientserver(int sock, short what)
+{
+ CONN_ID idx = Socket2Index( sock );
+ if (idx <= NONE) {
+#ifdef DEBUG
+ Log(LOG_WARNING, "WTF: cb_clientserver wants to write on unknown socket?!");