Add ClientRegistry to limit Max connections 29/162929/5
authorLee Sung Jun <sjun221.lee@samsung.com>
Wed, 6 Dec 2017 08:09:08 +0000 (17:09 +0900)
committerLee Sung Jun <sjun221.lee@samsung.com>
Fri, 8 Dec 2017 08:33:39 +0000 (17:33 +0900)
Change-Id: I8331b072a3f895da43a139843ae55ac18cd30c81
Signed-off-by: Lee Sung Jun <sjun221.lee@samsung.com>
server/server.cpp
server/server.h

index 54ee678..f9752ac 100644 (file)
@@ -47,6 +47,8 @@ DevicePolicyManager::DevicePolicyManager() :
        PolicyEventNotifier::init();
 
        setPrivilegeChecker(std::bind(&DevicePolicyManager::checkPeerPrivilege, this, _1, _2));
+       setNewConnectionCallback(std::bind(&DevicePolicyManager::checkNewConnection, this, _1));
+       setCloseConnectionCallback(std::bind(&DevicePolicyManager::checkCloseConnection, this, _1));
 
        expose(this, "", (int)(DevicePolicyManager::enroll)(std::string, uid_t));
        expose(this, "", (int)(DevicePolicyManager::disenroll)(std::string, uid_t));
@@ -129,3 +131,32 @@ bool DevicePolicyManager::checkPeerPrivilege(const rmi::Credentials& cred, const
 
        return true;
 }
+
+bool DevicePolicyManager::checkNewConnection(const rmi::Connection& connection)
+{
+       auto pid = (connection.getPeerCredentials()).pid;
+       if(clientRegistry.count(pid)) {
+               if(clientRegistry[pid] >= MAX_CLIENT_CONNECTIONS) {
+                       return false;
+               } else {
+                       clientRegistry[pid]++;
+               }
+       } else {
+               clientRegistry.emplace(pid, 1);
+       }
+
+       return true;
+}
+
+bool DevicePolicyManager::checkCloseConnection(const rmi::Connection& connection)
+{
+       auto pid = (connection.getPeerCredentials()).pid;
+       if(clientRegistry.count(pid)) {
+               clientRegistry[pid]--;
+               if(clientRegistry[pid] == 0) {
+                       clientRegistry.erase(pid);
+               }
+       }
+
+       return true;
+}
index 1613981..cbf6a03 100644 (file)
@@ -28,6 +28,8 @@
 #include "plugin.h"
 #include "sql-backend.h"
 
+#define MAX_CLIENT_CONNECTIONS 10
+
 class DevicePolicyManager : public rmi::Service {
 public:
        DevicePolicyManager();
@@ -43,8 +45,13 @@ private:
        int disenroll(const std::string& name, uid_t uid);
 
        bool checkPeerPrivilege(const rmi::Credentials& cred, const std::string& privilege);
+       bool checkNewConnection(const rmi::Connection& connection);
+       bool checkCloseConnection(const rmi::Connection& connection);
 
 private:
+       typedef std::unordered_map<int, int> ClientRegistry;
+       ClientRegistry clientRegistry;
+
        std::vector<AbstractPolicyProvider *> policyList;
        std::unique_ptr<PolicyLoader> policyLoader;
 };