diff options
Diffstat (limited to 'src/manager/main/socket-manager.h')
-rw-r--r-- | src/manager/main/socket-manager.h | 259 |
1 files changed, 145 insertions, 114 deletions
diff --git a/src/manager/main/socket-manager.h b/src/manager/main/socket-manager.h index 4e98dd5d..412d9f00 100644 --- a/src/manager/main/socket-manager.h +++ b/src/manager/main/socket-manager.h @@ -45,125 +45,156 @@ class Cynara; class SocketManager : public GenericSocketManager { public: - class Exception { - public: - DECLARE_EXCEPTION_TYPE(CKM::Exception, Base) - DECLARE_EXCEPTION_TYPE(Base, InitFailed) - }; + class Exception { + public: + DECLARE_EXCEPTION_TYPE(CKM::Exception, Base) + DECLARE_EXCEPTION_TYPE(Base, InitFailed) + }; - SocketManager(); - virtual ~SocketManager(); - virtual void MainLoop(); - virtual void MainLoopStop(); + SocketManager(); + virtual ~SocketManager(); + virtual void MainLoop(); + virtual void MainLoopStop(); - virtual void CynaraSocket(int oldFd, int newFd, bool isRW); - void SecurityStatus(int sock, int counter, bool allowed); + virtual void CynaraSocket(int oldFd, int newFd, bool isRW); + void SecurityStatus(int sock, int counter, bool allowed); - virtual void RegisterSocketService(GenericSocketService *service); - virtual void Close(ConnectionID connectionID); - virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer); - virtual void SecurityCheck(ConnectionID connectionID); + virtual void RegisterSocketService(GenericSocketService *service); + virtual void Close(ConnectionID connectionID); + virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer); + virtual void SecurityCheck(ConnectionID connectionID); protected: - void CreateDomainSocket( - GenericSocketService *service, - const GenericSocketService::ServiceDescription &desc); - int CreateDomainSocketHelp( - const GenericSocketService::ServiceDescription &desc); - int GetSocketFromSystemD( - const GenericSocketService::ServiceDescription &desc); - - void ReadyForRead(int sock); - void ReadyForWrite(int sock); - void ReadyForAccept(int sock); - void ProcessQueue(void); - void NotifyMe(void); - void CloseSocket(int sock); - - struct SocketDescription { - bool isOpen() { return m_flags & OPEN; } - bool isListen() { return m_flags & LISTEN; } - bool isCynara() { return m_flags & CYNARA; } - bool isTimeout() { return m_flags & TIMEOUT; } - void setOpen(bool isSet) { isSet ? m_flags |= OPEN : m_flags &= ~OPEN; } - void setListen(bool isSet) { isSet ? m_flags |= LISTEN : m_flags &= ~LISTEN; } - void setCynara(bool isSet) { isSet ? m_flags |= CYNARA : m_flags &= ~CYNARA; } - void setTimeout(bool isSet) { isSet ? m_flags |= TIMEOUT : m_flags &= ~TIMEOUT; } - - InterfaceID interfaceID; - GenericSocketService *service; - time_t timeout; - RawBuffer rawBuffer; - int counter; - std::string cynaraPrivilege; - std::string cynaraUser; - std::string cynaraClient; - - SocketDescription() - : interfaceID(-1) - , service(nullptr) - , timeout(::time(nullptr)) - , counter(0) - , m_flags(0) - { - } - - private: - static const char LISTEN = 1 << 0; - static const char OPEN = 1 << 1; - static const char CYNARA = 1 << 2; - static const char TIMEOUT = 1 << 3; - int m_flags; - }; - - SocketDescription& CreateDefaultReadSocketDescription(int sock, bool timeout); - - typedef std::vector<SocketDescription> SocketDescriptionVector; - - // support for generic event Queue - typedef std::function<void(void)> EventFunction; - template <typename E> - void AddEvent(E event) - { - CreateEvent([this, event]() {this->Handle(event);}); - } - void CreateEvent(EventFunction fun); - - struct WriteEvent { - ConnectionID connectionID; - RawBuffer rawBuffer; - }; - - struct CloseEvent : public ConnectionID {}; - struct SecurityEvent : public ConnectionID {}; - - void Handle(const WriteEvent &event); - void Handle(const CloseEvent &event); - void Handle(const SecurityEvent &event); - // support for generic event Queue - - struct Timeout { - time_t time; - int sock; - bool operator<(const Timeout &second) const - { - return time > second.time; // mininum first! - } - }; - - SocketDescriptionVector m_socketDescriptionVector; - fd_set m_readSet; - fd_set m_writeSet; - int m_maxDesc; - bool m_working; - std::mutex m_eventQueueMutex; - std::queue<EventFunction> m_eventQueue; - int m_notifyMe[2]; - int m_counter; - std::priority_queue<Timeout> m_timeoutQueue; - CommMgr m_commMgr; - std::unique_ptr<Cynara> m_cynara; - std::vector<GenericSocketService*> m_serviceVector; + void CreateDomainSocket( + GenericSocketService *service, + const GenericSocketService::ServiceDescription &desc); + int CreateDomainSocketHelp( + const GenericSocketService::ServiceDescription &desc); + int GetSocketFromSystemD( + const GenericSocketService::ServiceDescription &desc); + + void ReadyForRead(int sock); + void ReadyForWrite(int sock); + void ReadyForAccept(int sock); + void ProcessQueue(void); + void NotifyMe(void); + void CloseSocket(int sock); + + struct SocketDescription { + bool isOpen() + { + return m_flags & OPEN; + } + + bool isListen() + { + return m_flags & LISTEN; + } + + bool isCynara() + { + return m_flags & CYNARA; + } + + bool isTimeout() + { + return m_flags & TIMEOUT; + } + + void setOpen(bool isSet) + { + isSet ? m_flags |= OPEN : m_flags &= ~OPEN; + } + + void setListen(bool isSet) + { + isSet ? m_flags |= LISTEN : m_flags &= ~LISTEN; + } + + void setCynara(bool isSet) + { + isSet ? m_flags |= CYNARA : m_flags &= ~CYNARA; + } + + void setTimeout(bool isSet) + { + isSet ? m_flags |= TIMEOUT : m_flags &= ~TIMEOUT; + } + + InterfaceID interfaceID; + GenericSocketService *service; + time_t timeout; + RawBuffer rawBuffer; + int counter; + std::string cynaraPrivilege; + std::string cynaraUser; + std::string cynaraClient; + + SocketDescription() : + interfaceID(-1), + service(nullptr), + timeout(::time(nullptr)), + counter(0), + m_flags(0) {} + + private: + static const char LISTEN = 1 << 0; + static const char OPEN = 1 << 1; + static const char CYNARA = 1 << 2; + static const char TIMEOUT = 1 << 3; + int m_flags; + }; + + SocketDescription &CreateDefaultReadSocketDescription(int sock, bool timeout); + + typedef std::vector<SocketDescription> SocketDescriptionVector; + + // support for generic event Queue + typedef std::function<void(void)> EventFunction; + template <typename E> + void AddEvent(E event) + { + CreateEvent([this, event]() { + this->Handle(event); + }); + } + void CreateEvent(EventFunction fun); + + struct WriteEvent { + ConnectionID connectionID; + RawBuffer rawBuffer; + }; + + struct CloseEvent : public ConnectionID {}; + struct SecurityEvent : public ConnectionID {}; + + void Handle(const WriteEvent &event); + void Handle(const CloseEvent &event); + void Handle(const SecurityEvent &event); + // support for generic event Queue + + struct Timeout { + time_t time; + int sock; + bool operator<(const Timeout &second) const + { + return time > second.time; // mininum first! + } + }; + + SocketDescriptionVector m_socketDescriptionVector; + fd_set m_readSet; + fd_set m_writeSet; + int m_maxDesc; + bool m_working; + std::mutex m_eventQueueMutex; + std::queue<EventFunction> m_eventQueue; + int m_notifyMe[2]; + int m_counter; + std::priority_queue<Timeout> m_timeoutQueue; + CommMgr m_commMgr; + std::unique_ptr<Cynara> m_cynara; + std::vector<GenericSocketService *> m_serviceVector; }; } // namespace CKM |