Added IPAddressList capability and whitelist/blacklist capability.
This commit is contained in:
		
							parent
							
								
									2b9749c4b2
								
							
						
					
					
						commit
						5e5e626d28
					
				
							
								
								
									
										23
									
								
								IPAddressList.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								IPAddressList.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										26
									
								
								IPAddressList.h
									
									
									
									
									
										Normal 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 | ||||||
| @ -13,7 +13,7 @@ CurrentFileName        := | |||||||
| CurrentFilePath        := | CurrentFilePath        := | ||||||
| CurrentFileFullPath    := | CurrentFileFullPath    := | ||||||
| User                   :=Brad Arant | User                   :=Brad Arant | ||||||
| Date                   :=23/06/19 | Date                   :=27/07/19 | ||||||
| CodeLitePath           :=/home/bradarant/.codelite | CodeLitePath           :=/home/bradarant/.codelite | ||||||
| LinkerName             :=/usr/bin/x86_64-linux-gnu-g++ | LinkerName             :=/usr/bin/x86_64-linux-gnu-g++ | ||||||
| SharedObjectLinkerName :=/usr/bin/x86_64-linux-gnu-g++ -shared -fPIC | 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 | 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) \
 | 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)/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 | $(IntermediateDirectory)/Service.cpp$(PreprocessSuffix): Service.cpp | ||||||
| 	$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(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) | -include $(IntermediateDirectory)/*$(DependSuffix) | ||||||
| ##
 | ##
 | ||||||
|  | |||||||
| @ -138,6 +138,8 @@ | |||||||
|     <File Name="TerminalSession.cpp"/> |     <File Name="TerminalSession.cpp"/> | ||||||
|     <File Name="Service.cpp"/> |     <File Name="Service.cpp"/> | ||||||
|     <File Name="Service.h"/> |     <File Name="Service.h"/> | ||||||
|  |     <File Name="IPAddressList.h"/> | ||||||
|  |     <File Name="IPAddressList.cpp"/> | ||||||
|   </VirtualDirectory> |   </VirtualDirectory> | ||||||
|   <Dependencies Name="Debug"/> |   <Dependencies Name="Debug"/> | ||||||
|   <Dependencies Name="Release"/> |   <Dependencies Name="Release"/> | ||||||
|  | |||||||
| @ -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 | ||||||
|  | |||||||
							
								
								
									
										25
									
								
								Socket.cpp
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								Socket.cpp
									
									
									
									
									
								
							| @ -37,7 +37,7 @@ namespace core { | |||||||
|       this->length = length; |       this->length = length; | ||||||
|    } |    } | ||||||
|     |     | ||||||
|    void Socket::onRegistered() { |    void Socket::onRegistered() {       | ||||||
|       onConnected(); |       onConnected(); | ||||||
|    } |    } | ||||||
|     |     | ||||||
| @ -47,27 +47,18 @@ namespace core { | |||||||
|     |     | ||||||
|    void Socket::eventReceived(struct epoll_event event) { |    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) { |       if(event.events & EPOLLRDHUP) { | ||||||
| 	 Log(LOG_DEBUG_2) << "Socket " << descriptor << " received disconnect from client."; |          Log(LOG_DEBUG_2) << "Socket " << descriptor << " received disconnect from client."; | ||||||
| 	 shutdown(); |          shutdown(); | ||||||
| 	 return; |          return; | ||||||
|       } |       } | ||||||
|        |        | ||||||
|       if(event.events & EPOLLIN)  |       if(event.events & EPOLLIN)  | ||||||
| 	receiveData(buffer, length); |         receiveData(buffer, length); | ||||||
|        |           | ||||||
|       if(event.events & EPOLLOUT)  |       if(event.events & EPOLLOUT)  | ||||||
| 	writeSocket(); |         writeSocket(); | ||||||
|        |           | ||||||
|       enable(true); |       enable(true); | ||||||
|    } |    } | ||||||
|     |     | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								Socket.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Socket.h
									
									
									
									
									
								
							| @ -37,6 +37,10 @@ namespace core { | |||||||
|       Socket(EPoll &ePoll); |       Socket(EPoll &ePoll); | ||||||
|       ~Socket(); |       ~Socket(); | ||||||
| 
 | 
 | ||||||
|  |       ///
 | ||||||
|  | 
 | ||||||
|  |       void shutdown(); | ||||||
|  | 
 | ||||||
|       void setDescriptor(int descriptor); ///<Set the descriptor for the socket.
 |       void setDescriptor(int descriptor); ///<Set the descriptor for the socket.
 | ||||||
|     |     | ||||||
|       int getDescriptor(); ///< Get the descriptor for the socket.   
 |       int getDescriptor(); ///< Get the descriptor for the socket.   
 | ||||||
| @ -122,8 +126,6 @@ namespace core { | |||||||
|       ///
 |       ///
 | ||||||
|     |     | ||||||
|       virtual void onDataReceived(std::string data) = 0; ///< Called when data is received from the socket.
 |       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.
 |       /// receiveData will read the data from the socket and place it in the socket buffer.
 | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ | |||||||
| #include "EPoll.h" | #include "EPoll.h" | ||||||
| #include "Session.h" | #include "Session.h" | ||||||
| #include "Exception.h" | #include "Exception.h" | ||||||
|  | #include "Log.h" | ||||||
| 
 | 
 | ||||||
| namespace core { | namespace core { | ||||||
| 
 | 
 | ||||||
| @ -22,14 +23,27 @@ namespace core { | |||||||
|    } |    } | ||||||
|        |        | ||||||
|    void TCPServerSocket::onDataReceived(std::string data) { |    void TCPServerSocket::onDataReceived(std::string data) { | ||||||
|       Log(LOG_DEBUG_2) << "Connection request on socket " << getDescriptor() << ".";       |       Log(LOG_DEBUG_2) << "Connection request on socket " << getDescriptor() << ".";             | ||||||
|       Session *session = accept(); |       Session *session = accept(); | ||||||
|       service.sessions.push_back(session);    |       if(session) service.sessions.push_back(session);    | ||||||
|    } |    } | ||||||
|     |     | ||||||
|    Session * TCPServerSocket::accept() {       |    Session * TCPServerSocket::accept() {       | ||||||
|       Session *session = getSocketAccept();       |       Session *session = getSocketAccept();       | ||||||
|       session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength)); |       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);    |       ePoll.registerSocket(session);    | ||||||
|       Log(LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << "."; |       Log(LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << "."; | ||||||
|       return session; |       return session; | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ | |||||||
| #include "Socket.h" | #include "Socket.h" | ||||||
| #include "TCPSocket.h" | #include "TCPSocket.h" | ||||||
| #include "Service.h" | #include "Service.h" | ||||||
|  | #include "IPAddressList.h" | ||||||
| 
 | 
 | ||||||
| namespace core { | namespace core { | ||||||
|     |     | ||||||
| @ -31,6 +32,7 @@ namespace core { | |||||||
|       /// @param port the port number that the socket will listen on.
 |       /// @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.
 |       /// @param commandName the name of the command used to invoke the status display for this object.
 | ||||||
|       /// @return the instance of the BMATCPServerSocket.
 |       /// @return the instance of the BMATCPServerSocket.
 | ||||||
|  |       ///
 | ||||||
|        |        | ||||||
|       TCPServerSocket(EPoll &ePoll, Service &service, IPAddress address); |       TCPServerSocket(EPoll &ePoll, Service &service, IPAddress address); | ||||||
|        |        | ||||||
| @ -40,8 +42,27 @@ namespace core { | |||||||
|        |        | ||||||
|       ~TCPServerSocket(); |       ~TCPServerSocket(); | ||||||
| 
 | 
 | ||||||
|  |       /// 
 | ||||||
|  |       /// The service object reference used to access associated server information such as sessions 
 | ||||||
|  |       /// and other extended managers.
 | ||||||
|  |       ///
 | ||||||
|  |        | ||||||
|       Service &service; |       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: |     protected: | ||||||
|        |        | ||||||
| //      virtual void init();
 | //      virtual void init();
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user