Added IPAddressList capability and whitelist/blacklist capability.

This commit is contained in:
Brad Arant 2019-07-27 17:23:23 -07:00
parent 2b9749c4b2
commit 5e5e626d28
9 changed files with 111 additions and 24 deletions

23
IPAddressList.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "IPAddressList.h"
namespace core {
IPAddressList::IPAddressList() {}
std::map<std::string, IPAddress> 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();
}
}

26
IPAddressList.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef __IPAddressList_h__
#define __IPAddressList_h__
#include "includes"
#include "IPAddress.h"
namespace core {
class IPAddressList {
public:
IPAddressList();
std::map<std::string, IPAddress> getList();
bool add(IPAddress ipAddress);
bool remove(IPAddress ipAddress);
bool contains(std::string ipAddress);
private:
std::map<std::string, IPAddress> list;
std::map<std::string, IPAddress>::iterator it = list.begin();
};
}
#endif

View File

@ -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)
##

View File

@ -138,6 +138,8 @@
<File Name="TerminalSession.cpp"/>
<File Name="Service.cpp"/>
<File Name="Service.h"/>
<File Name="IPAddressList.h"/>
<File Name="IPAddressList.cpp"/>
</VirtualDirectory>
<Dependencies Name="Debug"/>
<Dependencies Name="Release"/>

View File

@ -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

View File

@ -47,26 +47,17 @@ 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);
}

View File

@ -37,6 +37,10 @@ namespace core {
Socket(EPoll &ePoll);
~Socket();
///
void shutdown();
void setDescriptor(int descriptor); ///<Set the descriptor for the socket.
int getDescriptor(); ///< Get the descriptor for the socket.
@ -123,8 +127,6 @@ namespace core {
virtual void onDataReceived(std::string data) = 0; ///< Called when data is received from the socket.
void shutdown();
///
/// receiveData will read the data from the socket and place it in the socket buffer.
/// TLS layer overrides this to be able to read from SSL.

View File

@ -2,6 +2,7 @@
#include "EPoll.h"
#include "Session.h"
#include "Exception.h"
#include "Log.h"
namespace core {
@ -24,12 +25,25 @@ namespace core {
void TCPServerSocket::onDataReceived(std::string data) {
Log(LOG_DEBUG_2) << "Connection request on socket " << getDescriptor() << ".";
Session *session = accept();
service.sessions.push_back(session);
if(session) service.sessions.push_back(session);
}
Session * TCPServerSocket::accept() {
Session *session = getSocketAccept();
session->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;

View File

@ -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();