From 5e5e626d2801d00440e6f66d8ff1c2eaed63528f Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Sat, 27 Jul 2019 17:23:23 -0700 Subject: [PATCH] Added IPAddressList capability and whitelist/blacklist capability. --- IPAddressList.cpp | 23 +++++++++++++++++++++++ IPAddressList.h | 26 ++++++++++++++++++++++++++ ServerCore.mk | 12 ++++++++++-- ServerCore.project | 2 ++ ServerCore.txt | 2 +- Socket.cpp | 25 ++++++++----------------- Socket.h | 6 ++++-- TCPServerSocket.cpp | 18 ++++++++++++++++-- TCPServerSocket.h | 21 +++++++++++++++++++++ 9 files changed, 111 insertions(+), 24 deletions(-) create mode 100644 IPAddressList.cpp create mode 100644 IPAddressList.h diff --git a/IPAddressList.cpp b/IPAddressList.cpp new file mode 100644 index 0000000..b658c81 --- /dev/null +++ b/IPAddressList.cpp @@ -0,0 +1,23 @@ +#include "IPAddressList.h" + +namespace core { + + IPAddressList::IPAddressList() {} + + std::map IPAddressList::getList() { + return list; + } + + bool IPAddressList::add(IPAddress ipAddress) { + list.insert(std::make_pair(ipAddress.getClientAddress(), ipAddress)); + } + + bool IPAddressList::remove(IPAddress ipAddress) { + + } + + bool IPAddressList::contains(std::string ipAddress) { + return list.find(ipAddress) != list.end(); + } + +} diff --git a/IPAddressList.h b/IPAddressList.h new file mode 100644 index 0000000..4079fec --- /dev/null +++ b/IPAddressList.h @@ -0,0 +1,26 @@ +#ifndef __IPAddressList_h__ +#define __IPAddressList_h__ + +#include "includes" +#include "IPAddress.h" + +namespace core { + + class IPAddressList { + + public: + IPAddressList(); + + std::map getList(); + bool add(IPAddress ipAddress); + bool remove(IPAddress ipAddress); + bool contains(std::string ipAddress); + + private: + std::map list; + std::map::iterator it = list.begin(); + }; + +} + +#endif diff --git a/ServerCore.mk b/ServerCore.mk index bc960f2..570b61c 100644 --- a/ServerCore.mk +++ b/ServerCore.mk @@ -13,7 +13,7 @@ CurrentFileName := CurrentFilePath := CurrentFileFullPath := User :=Brad Arant -Date :=23/06/19 +Date :=27/07/19 CodeLitePath :=/home/bradarant/.codelite LinkerName :=/usr/bin/x86_64-linux-gnu-g++ SharedObjectLinkerName :=/usr/bin/x86_64-linux-gnu-g++ -shared -fPIC @@ -62,7 +62,7 @@ AS := /usr/bin/x86_64-linux-gnu-as CodeLiteDir:=/usr/share/codelite Objects0=$(IntermediateDirectory)/Command.cpp$(ObjectSuffix) $(IntermediateDirectory)/ConsoleServer.cpp$(ObjectSuffix) $(IntermediateDirectory)/ConsoleSession.cpp$(ObjectSuffix) $(IntermediateDirectory)/EPoll.cpp$(ObjectSuffix) $(IntermediateDirectory)/Exception.cpp$(ObjectSuffix) $(IntermediateDirectory)/File.cpp$(ObjectSuffix) $(IntermediateDirectory)/IPAddress.cpp$(ObjectSuffix) $(IntermediateDirectory)/Log.cpp$(ObjectSuffix) $(IntermediateDirectory)/Session.cpp$(ObjectSuffix) $(IntermediateDirectory)/Socket.cpp$(ObjectSuffix) \ $(IntermediateDirectory)/TCPServerSocket.cpp$(ObjectSuffix) $(IntermediateDirectory)/TCPSocket.cpp$(ObjectSuffix) $(IntermediateDirectory)/Thread.cpp$(ObjectSuffix) $(IntermediateDirectory)/Timer.cpp$(ObjectSuffix) $(IntermediateDirectory)/TLSServerSocket.cpp$(ObjectSuffix) $(IntermediateDirectory)/TLSSession.cpp$(ObjectSuffix) $(IntermediateDirectory)/UDPServerSocket.cpp$(ObjectSuffix) $(IntermediateDirectory)/UDPSocket.cpp$(ObjectSuffix) $(IntermediateDirectory)/CommandList.cpp$(ObjectSuffix) $(IntermediateDirectory)/TerminalSession.cpp$(ObjectSuffix) \ - $(IntermediateDirectory)/Service.cpp$(ObjectSuffix) + $(IntermediateDirectory)/Service.cpp$(ObjectSuffix) $(IntermediateDirectory)/IPAddressList.cpp$(ObjectSuffix) @@ -263,6 +263,14 @@ $(IntermediateDirectory)/Service.cpp$(DependSuffix): Service.cpp $(IntermediateDirectory)/Service.cpp$(PreprocessSuffix): Service.cpp $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/Service.cpp$(PreprocessSuffix) Service.cpp +$(IntermediateDirectory)/IPAddressList.cpp$(ObjectSuffix): IPAddressList.cpp $(IntermediateDirectory)/IPAddressList.cpp$(DependSuffix) + $(CXX) $(IncludePCH) $(SourceSwitch) "/home/bradarant/barant/ServerCore/IPAddressList.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/IPAddressList.cpp$(ObjectSuffix) $(IncludePath) +$(IntermediateDirectory)/IPAddressList.cpp$(DependSuffix): IPAddressList.cpp + @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/IPAddressList.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/IPAddressList.cpp$(DependSuffix) -MM IPAddressList.cpp + +$(IntermediateDirectory)/IPAddressList.cpp$(PreprocessSuffix): IPAddressList.cpp + $(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/IPAddressList.cpp$(PreprocessSuffix) IPAddressList.cpp + -include $(IntermediateDirectory)/*$(DependSuffix) ## diff --git a/ServerCore.project b/ServerCore.project index 2699676..4af529c 100644 --- a/ServerCore.project +++ b/ServerCore.project @@ -138,6 +138,8 @@ + + diff --git a/ServerCore.txt b/ServerCore.txt index 9699d85..81a6551 100644 --- a/ServerCore.txt +++ b/ServerCore.txt @@ -1 +1 @@ -./Debug/Command.cpp.o ./Debug/ConsoleServer.cpp.o ./Debug/ConsoleSession.cpp.o ./Debug/EPoll.cpp.o ./Debug/Exception.cpp.o ./Debug/File.cpp.o ./Debug/IPAddress.cpp.o ./Debug/Log.cpp.o ./Debug/Session.cpp.o ./Debug/Socket.cpp.o ./Debug/TCPServerSocket.cpp.o ./Debug/TCPSocket.cpp.o ./Debug/Thread.cpp.o ./Debug/Timer.cpp.o ./Debug/TLSServerSocket.cpp.o ./Debug/TLSSession.cpp.o ./Debug/UDPServerSocket.cpp.o ./Debug/UDPSocket.cpp.o ./Debug/CommandList.cpp.o ./Debug/TerminalSession.cpp.o ./Debug/Service.cpp.o +./Debug/Command.cpp.o ./Debug/ConsoleServer.cpp.o ./Debug/ConsoleSession.cpp.o ./Debug/EPoll.cpp.o ./Debug/Exception.cpp.o ./Debug/File.cpp.o ./Debug/IPAddress.cpp.o ./Debug/Log.cpp.o ./Debug/Session.cpp.o ./Debug/Socket.cpp.o ./Debug/TCPServerSocket.cpp.o ./Debug/TCPSocket.cpp.o ./Debug/Thread.cpp.o ./Debug/Timer.cpp.o ./Debug/TLSServerSocket.cpp.o ./Debug/TLSSession.cpp.o ./Debug/UDPServerSocket.cpp.o ./Debug/UDPSocket.cpp.o ./Debug/CommandList.cpp.o ./Debug/TerminalSession.cpp.o ./Debug/Service.cpp.o ./Debug/IPAddressList.cpp.o diff --git a/Socket.cpp b/Socket.cpp index 0c32f5c..dcd7ee8 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -37,7 +37,7 @@ namespace core { this->length = length; } - void Socket::onRegistered() { + void Socket::onRegistered() { onConnected(); } @@ -47,27 +47,18 @@ namespace core { void Socket::eventReceived(struct epoll_event event) { - // std::stringstream stream; - // stream << "Event received on socket " << event.data.fd << ": "; - // if(event.events & EPOLLRDHUP) stream << "EPOLLRDHUP "; - // if(event.events & EPOLLIN) stream << "EPOLLIN "; - // if(event.events & EPOLLOUT) stream << "EPOLLOUT "; - // if(event.events & EPOLLERR) stream << "EPOLLERR "; - // stream << "[" << event.events << "]"; - // BMALog(LOG_DEBUG_4) << stream.str(); - // if(event.events & EPOLLRDHUP) { - Log(LOG_DEBUG_2) << "Socket " << descriptor << " received disconnect from client."; - shutdown(); - return; + Log(LOG_DEBUG_2) << "Socket " << descriptor << " received disconnect from client."; + shutdown(); + return; } if(event.events & EPOLLIN) - receiveData(buffer, length); - + receiveData(buffer, length); + if(event.events & EPOLLOUT) - writeSocket(); - + writeSocket(); + enable(true); } diff --git a/Socket.h b/Socket.h index 2246c6a..ac5b39f 100644 --- a/Socket.h +++ b/Socket.h @@ -37,6 +37,10 @@ namespace core { Socket(EPoll &ePoll); ~Socket(); + /// + + void shutdown(); + void setDescriptor(int descriptor); ///setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength)); + + if(blackList && blackList->contains(session->ipAddress.getClientAddress())) { + session->shutdown(); + Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is blacklisted and was denied a connection."; + return NULL; + } + + if(whiteList && !whiteList->contains(session->ipAddress.getClientAddress())) { + session->shutdown(); + Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is not authorized and was denied a connection."; + return NULL; + } + ePoll.registerSocket(session); Log(LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << "."; return session; diff --git a/TCPServerSocket.h b/TCPServerSocket.h index c604d7e..070f336 100644 --- a/TCPServerSocket.h +++ b/TCPServerSocket.h @@ -4,6 +4,7 @@ #include "Socket.h" #include "TCPSocket.h" #include "Service.h" +#include "IPAddressList.h" namespace core { @@ -31,6 +32,7 @@ namespace core { /// @param port the port number that the socket will listen on. /// @param commandName the name of the command used to invoke the status display for this object. /// @return the instance of the BMATCPServerSocket. + /// TCPServerSocket(EPoll &ePoll, Service &service, IPAddress address); @@ -40,8 +42,27 @@ namespace core { ~TCPServerSocket(); + /// + /// The service object reference used to access associated server information such as sessions + /// and other extended managers. + /// + Service &service; + /// + /// If not NULL the blacklist object can be assigned to this server socket and the server + /// IP addresses connecting to the server attempting to accept a socket are contained in + /// this list then the connection is rejected and no accept is granted. + /// + + IPAddressList *blackList; + /// + /// If not NULL the blacklist object can be assigned to this server socket and the server + /// IP addresses connecting to the server attempting to accept a socket are contained in + /// this list then the connection is rejected and no accept is granted. + /// + IPAddressList *whiteList; + protected: // virtual void init();