From 2830b8dd6d1ca4cde44dd849769bc71d093968d4 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Fri, 18 Aug 2017 09:12:55 +0200 Subject: [PATCH] Important Bug fixes: - Remote Client connect not working properly - Set Client Port no on connect - Correctly handle create failure - Show error on fail - Input() fail to transfer data if Handle & Channel not same name - Search for linked handles --- SelectableCore.cpp | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/SelectableCore.cpp b/SelectableCore.cpp index a5cb8f6..6aff571 100644 --- a/SelectableCore.cpp +++ b/SelectableCore.cpp @@ -733,7 +733,8 @@ int CSelectableCore::OpenRemoteClientSocket( THandle * Handle ) { THandle ** RemoteClient; int ClientFD; - char ClientAddress[100]; + char ClientAddress[50]; + char ClientPort[20]; char ClientName[100]; socklen_t addr_len; @@ -767,6 +768,8 @@ int CSelectableCore::OpenRemoteClientSocket( THandle * Handle ) // Return client address strcpy( ClientAddress, inet_ntoa(address.sin_addr) ); + sprintf( ClientPort, "%d", ntohs(address.sin_port) ); + // Get end of client list RemoteClient = &FirstHandle; @@ -777,7 +780,10 @@ int CSelectableCore::OpenRemoteClientSocket( THandle * Handle ) // Create Remote Client Handle sprintf( ClientName, "%s-%d", Handle->Name, ClientFD ); *RemoteClient = CreateHandle( ClientName, false ); - SetSocketHandle( *RemoteClient, ctRemoteClient, ClientAddress, 0, Handle->KeepAlive ); + if (!SetSocketHandle( *RemoteClient, ctRemoteClient, ClientAddress, ClientPort, Handle->KeepAlive )) { + if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - TCP Server failed to configure Remote TCP Client connection (%s)", Name, Handle->Name, strerror(errno) ); + return -1; + } // Copy Parent Buffer setup SetBuffers( *RemoteClient, ((Handle->InBuffer)? Handle->InBuffer->Size() : 0), @@ -1064,13 +1070,15 @@ bool CSelectableCore::Read( THandle * Handle ) { // Incoming client request ClientFD = OpenRemoteClientSocket( Handle ); - if (ClientFD != -1) { - // Add to Select Lists - if (Selector) { - Selector->Add( ClientFD, true, true, this ); - } - return true; + if (ClientFD == -1) { + return false; } + + // Add to Select Lists + if (Selector) { + Selector->Add( ClientFD, true, true, this ); + } + return true; } else if ((Handle->Type == ctRemoteClient) || (Handle->Type == ctClient)) { @@ -1310,13 +1318,19 @@ int CSelectableCore::Input( const char * ChannelName, const char * Data, int Len return 0; } - // Get File handle - if (!(Handle = GetHandle( ChannelName ))) { + // Find Linked handle + Handle = FirstHandle; + while( Handle && (!Handle->Channel || strcasecmp( ChannelName, Handle->Channel->Name ))) { + Handle = Handle->Next; + } + + // Check if any handle found + if (!Handle) { // Handle not found if (Log) Log->Message( LogLevel, dlHigh, "%s: Channel '%s' - Input rejected, Handle not found", Name, ChannelName ); return 0; } - else if (Handle->Channel && !Handle->Channel->InputEnabled) { + else if (!Handle->Channel->InputEnabled) { // Handle is not open if (Log) Log->Message( LogLevel, dlHigh, "%s: Channel '%s' - Input rejected, Channel input disabled", Name, ChannelName ); return 0;