C++ API re-factored: establish and keep socket connection to the service.
authorMaciej J. Karpiuk <m.karpiuk2@samsung.com>
Thu, 9 Oct 2014 14:11:24 +0000 (16:11 +0200)
committerMaciej J. Karpiuk <m.karpiuk2@samsung.com>
Tue, 17 Feb 2015 10:00:03 +0000 (11:00 +0100)
C API still does create and destroy socket per each API call.

Change-Id: I99f90da5dca8e7ddc615b7d39d8a0a26055984d6

src/manager/client/client-common.cpp
src/manager/client/client-common.h
src/manager/client/client-control.cpp
src/manager/client/client-manager-impl.cpp
src/manager/client/client-manager-impl.h

index 7bc2ee4..8b4d424 100644 (file)
@@ -51,10 +51,127 @@ void centKeyClientEnableLogSystem(void) {
     CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM_CLIENT");
 }
 
-int waitForSocket(int sock, int event, int timeout) {
+} // namespace anonymous
+
+namespace CKM {
+
+SockRAII::SockRAII() : m_sock(-1) {}
+
+SockRAII::~SockRAII()
+{
+    Disconnect();
+}
+
+int SockRAII::Connect(char const * const interface)
+{
+    int sock = -1;
+    try
+    {
+        if(!interface) {
+            LogError("No valid interface address given.");
+            throw CKM_API_ERROR_INPUT_PARAM;
+        }
+
+        sock = socket(AF_UNIX, SOCK_STREAM, 0);
+        if(sock < 0) {
+            LogError("Error creating socket: " << CKM::GetErrnoString(errno));
+            throw CKM_API_ERROR_SOCKET;
+        }
+
+        // configure non-blocking mode
+        int flags;
+        if((flags = fcntl(sock, F_GETFL, 0)) < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
+        {
+            LogError("Error in fcntl: " << CKM::GetErrnoString(errno));
+            throw CKM_API_ERROR_SOCKET;
+        }
+
+        sockaddr_un clientAddr;
+        memset(&clientAddr, 0, sizeof(clientAddr));
+        clientAddr.sun_family = AF_UNIX;
+        if(strlen(interface) >= sizeof(clientAddr.sun_path))
+        {
+            LogError("Error: interface name " << interface << "is too long. Max len is:" << sizeof(clientAddr.sun_path));
+            throw CKM_API_ERROR_INPUT_PARAM;
+        }
+
+        strcpy(clientAddr.sun_path, interface);
+        LogDebug("ClientAddr.sun_path = " << interface);
+
+        int retval = TEMP_FAILURE_RETRY(::connect(sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr)));
+        if(-1 == retval)
+        {
+            switch(errno)
+            {
+                case EACCES:
+                    LogError("Access denied");
+                    throw CKM_API_ERROR_ACCESS_DENIED;
+                    break;
+
+                case EINPROGRESS:
+                {
+                    if( 0 >= WaitForSocket(POLLOUT, POLL_TIMEOUT))
+                    {
+                        LogError("Error in WaitForSocket.");
+                        throw CKM_API_ERROR_SOCKET;
+                    }
+
+                    int error = 0;
+                    size_t len = sizeof(error);
+                    retval = getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len);
+                    if(-1 == retval)
+                    {
+                        LogError("Error in getsockopt: " << CKM::GetErrnoString(error));
+                        throw CKM_API_ERROR_SOCKET;
+                    }
+
+                    if(error == EACCES)
+                    {
+                        LogError("Access denied");
+                        throw CKM_API_ERROR_ACCESS_DENIED;
+                    }
+
+                    if(error != 0)
+                    {
+                        LogError("Error in connect: " << CKM::GetErrnoString(error));
+                        throw CKM_API_ERROR_SOCKET;
+                    }
+
+                    break;
+                }
+
+                default:
+                    LogError("Error connecting socket: " << CKM::GetErrnoString(errno));
+                    throw CKM_API_ERROR_SOCKET;
+                    break;
+            }
+        }
+    }
+    catch(int & error_code) {
+        if(sock > 0)
+            close(sock);
+        return error_code;
+    }
+
+    m_sock = sock;
+    return CKM_API_SUCCESS;
+}
+
+bool SockRAII::isConnected() const {
+    return (m_sock != -1);
+}
+
+void SockRAII::Disconnect() {
+    if( isConnected() )
+        close(m_sock);
+    m_sock = -1;
+}
+
+int SockRAII::WaitForSocket(int event, int timeout)
+{
     int retval;
     pollfd desc[1];
-    desc[0].fd = sock;
+    desc[0].fd = m_sock;
     desc[0].events = event;
 
     while((-1 == (retval = poll(desc, 1, timeout))) && (errno == EINTR)) {
@@ -64,13 +181,16 @@ int waitForSocket(int sock, int event, int timeout) {
 
     if (0 == retval) {
         LogDebug("Poll timeout");
-    } else if (-1 == retval) {
-        int err = errno;
-        LogError("Error in poll: " << CKM::GetErrnoString(err));
+    } else if(-1 == retval) {
+        LogError("Error in poll: " << CKM::GetErrnoString(errno));
     }
     return retval;
 }
 
+int SockRAII::Get() const {
+    return m_sock;
+}
+
 } // namespace anonymous
 
 namespace CKM {
@@ -110,134 +230,117 @@ bool AliasSupport::isLabelEmpty() const {
     return m_label.empty();
 }
 
+ServiceConnection::ServiceConnection(char const * const service_interface) {
+    if(service_interface)
+        m_service_interface = std::string(service_interface);
+}
 
-int connectSocket(int& sock, char const * const interface) {
-    sockaddr_un clientAddr;
-    int flags;
-
-    if (sock != -1) // guard
-        close(sock);
-
-    sock = socket(AF_UNIX, SOCK_STREAM, 0);
-    if (sock < 0) {
-        int err = errno;
-        LogError("Error creating socket: " << GetErrnoString(err));
-        return CKM_API_ERROR_SOCKET;
-    }
-
-    if ((flags = fcntl(sock, F_GETFL, 0)) < 0 ||
-        fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
-    {
-        int err = errno;
-        LogError("Error in fcntl: " << GetErrnoString(err));
-        return CKM_API_ERROR_SOCKET;
-    }
-
-    memset(&clientAddr, 0, sizeof(clientAddr));
-
-    clientAddr.sun_family = AF_UNIX;
+int ServiceConnection::processRequest( const CKM::RawBuffer &send_buf,
+                                       CKM::MessageBuffer &recv_buf) {
+    int ec;
+    if(CKM_API_SUCCESS != (ec = send(send_buf)))
+        return ec;
 
-    if (strlen(interface) >= sizeof(clientAddr.sun_path)) {
-        LogError("Error: interface name " << interface << "is too long. Max len is:" <<
-                 sizeof(clientAddr.sun_path));
-        return CKM_API_ERROR_SOCKET;
-    }
+    return receive(recv_buf);
+}
 
-    strcpy(clientAddr.sun_path, interface);
+int ServiceConnection::Connect()
+{
+    // cleanup
+    if( isConnected() )
+        Disconnect();
 
-    LogDebug("ClientAddr.sun_path = " << interface);
+    return SockRAII::Connect(m_service_interface.c_str());
+}
 
-    int retval = TEMP_FAILURE_RETRY(
-        connect(sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr)));
-    if ((retval == -1) && (errno == EINPROGRESS)) {
-        if (0 >= waitForSocket(sock, POLLOUT, POLL_TIMEOUT)) {
-            LogError("Error in waitForSocket.");
-            return CKM_API_ERROR_SOCKET;
-        }
-        int error = 0;
-        socklen_t len = sizeof(error);
-        retval = getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len);
-
-        if (-1 == retval) {
-            int err = errno;
-            LogError("Error in getsockopt: " << GetErrnoString(err));
-            return CKM_API_ERROR_SOCKET;
+int ServiceConnection::send(const CKM::RawBuffer &send_buf)
+{
+    if( ! isConnected() )
+    {
+        int ec;
+        if(CKM_API_SUCCESS != (ec = ServiceConnection::Connect()))
+        {
+            LogError("send failed, connect fail code: " << ec);
+            return ec;
         }
+    }
 
-        if (error == EACCES) {
-            LogError("Access denied");
-            return CKM_API_ERROR_ACCESS_DENIED;
+    int ec = CKM_API_SUCCESS;
+    ssize_t done = 0;
+    while((send_buf.size() - done) > 0)
+    {
+        if( 0 >= WaitForSocket(POLLOUT, POLL_TIMEOUT)) {
+            LogError("Error in WaitForSocket.");
+            ec = CKM_API_ERROR_SOCKET;
+            break;
         }
 
-        if (error != 0) {
-            LogError("Error in connect: " << GetErrnoString(error));
-            return CKM_API_ERROR_SOCKET;
+        ssize_t temp = TEMP_FAILURE_RETRY(write(Get(), &send_buf[done], send_buf.size() - done));
+        if(-1 == temp) {
+            LogError("Error in write: " << CKM::GetErrnoString(errno));
+            ec = CKM_API_ERROR_SOCKET;
+            break;
         }
 
-        return CKM_API_SUCCESS;
+        done += temp;
     }
 
-    if (-1 == retval) {
-        int err = errno;
-        LogError("Error connecting socket: " << GetErrnoString(err));
-        if (err == EACCES)
-            return CKM_API_ERROR_ACCESS_DENIED;
-        return CKM_API_ERROR_SOCKET;
-    }
+    if(ec != CKM_API_SUCCESS)
+        Disconnect();
 
-    return CKM_API_SUCCESS;
+    return ec;
 }
 
-int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv) {
-    int ret;
-    SockRAII sock;
-    ssize_t done = 0;
-    char buffer[2048];
-
-    if (CKM_API_SUCCESS != (ret = sock.Connect(interface))) {
-        LogError("Error in SockRAII");
-        return ret;
+int ServiceConnection::receive(CKM::MessageBuffer &recv_buf)
+{
+    if( ! isConnected() )
+    {
+        LogError("Not connected!");
+        return CKM_API_ERROR_SOCKET;
     }
 
-    while ((send.size() - done) > 0) {
-        if (0 >= waitForSocket(sock.Get(), POLLOUT, POLL_TIMEOUT)) {
-            LogError("Error in poll(POLLOUT)");
-            return CKM_API_ERROR_SOCKET;
-        }
-        ssize_t temp = TEMP_FAILURE_RETRY(write(sock.Get(), &send[done], send.size() - done));
-        if (-1 == temp) {
-            int err = errno;
-            LogError("Error in write: " << GetErrnoString(err));
-            return CKM_API_ERROR_SOCKET;
+    int ec = CKM_API_SUCCESS;
+    const size_t c_recv_buf_len = 2048;
+    char buffer[c_recv_buf_len];
+    do
+    {
+        if( 0 >= WaitForSocket(POLLIN, POLL_TIMEOUT)) {
+            LogError("Error in WaitForSocket.");
+            ec = CKM_API_ERROR_SOCKET;
+            break;
         }
-        done += temp;
-    }
 
-    do {
-        if (0 >= waitForSocket(sock.Get(), POLLIN, POLL_TIMEOUT)) {
-            LogError("Error in poll(POLLIN)");
-            return CKM_API_ERROR_SOCKET;
-        }
-        ssize_t temp = TEMP_FAILURE_RETRY(read(sock.Get(), buffer, 2048));
-        if (-1 == temp) {
-            int err = errno;
-            LogError("Error in read: " << GetErrnoString(err));
-            return CKM_API_ERROR_SOCKET;
+        ssize_t temp = TEMP_FAILURE_RETRY(read(Get(), buffer, sizeof(buffer)));
+        if(-1 == temp) {
+            LogError("Error in read: " << CKM::GetErrnoString(errno));
+            ec = CKM_API_ERROR_SOCKET;
+            break;
         }
 
         if (0 == temp) {
             LogError("Read return 0/Connection closed by server(?)");
-            return CKM_API_ERROR_SOCKET;
+            ec = CKM_API_ERROR_SOCKET;
+            break;
         }
 
-        RawBuffer raw(buffer, buffer+temp);
-        recv.Push(raw);
-    } while(!recv.Ready());
-    return CKM_API_SUCCESS;
+        CKM::RawBuffer raw(buffer, buffer+temp);
+        recv_buf.Push(raw);
+    }
+    while(!recv_buf.Ready());
+
+    if(ec != CKM_API_SUCCESS)
+        Disconnect();
+
+    return ec;
+}
+
+ServiceConnection::~ServiceConnection()
+{
 }
 
 int try_catch(const std::function<int()>& func)
 {
+    int retval = CKM_API_ERROR_UNKNOWN;
     try {
         return func();
     } catch (MessageBuffer::Exception::Base &e) {
@@ -247,7 +350,7 @@ int try_catch(const std::function<int()>& func)
     } catch (...) {
         LogError("Unknown exception occured");
     }
-    return CKM_API_ERROR_UNKNOWN;
+    return retval;
 }
 
 void try_catch_async(const std::function<void()>& func, const std::function<void(int)>& error)
index 4adfb0d..04ed36d 100644 (file)
@@ -57,36 +57,48 @@ class AliasSupport
         Label m_label;
 };
 
-int connectSocket(int& sock, char const * const interface);
+class SockRAII {
+    public:
+        SockRAII();
 
-int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv);
+        NONCOPYABLE(SockRAII);
 
+        virtual ~SockRAII();
 
-class SockRAII {
-public:
-    SockRAII()
-      : m_sock(-1)
-    {}
+        int Connect(char const * const interface);
+        void Disconnect();
+        bool isConnected() const;
+        int Get() const;
+
+    protected:
+        int WaitForSocket(int event, int timeout);
+
+    private:
+        int m_sock;
+};
+
+class ServiceConnection : public SockRAII
+{
+    public:
+        ServiceConnection(char const * const service_interface);
 
-    NONCOPYABLE(SockRAII);
+        // roundtrip: send and receive
+        int processRequest(const CKM::RawBuffer &send_buf,
+                           CKM::MessageBuffer &recv_buf);
 
-    virtual ~SockRAII() {
-        if (m_sock > -1)
-            close(m_sock);
-    }
+        // blocking
+        int send(const CKM::RawBuffer &send_buf);
+        int receive(CKM::MessageBuffer &recv_buf);
 
-    int Connect(char const * const interface) {
-        return CKM::connectSocket(m_sock, interface);
-    }
+        virtual ~ServiceConnection();
 
-    int Get() const {
-        return m_sock;
-    }
+    private:
+        std::string m_service_interface;
 
-private:
-    int m_sock;
+        int Connect();
 };
 
+
 /*
  * Decorator function that performs frequently repeated exception handling in
  * SS client API functions. Accepts lambda expression as an argument.
index 534b109..2abdef7 100644 (file)
@@ -32,7 +32,7 @@ namespace CKM {
 
 class ControlImpl : public Control {
 public:
-    ControlImpl(){}
+    ControlImpl() : m_controlConnection(SERVICE_SOCKET_CKM_CONTROL) {}
     ControlImpl(const ControlImpl &) = delete;
     ControlImpl(ControlImpl &&) = delete;
     ControlImpl& operator=(const ControlImpl &) = delete;
@@ -48,14 +48,10 @@ public:
             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::UNLOCK_USER_KEY),
                                                  user,
                                                  password);
-            int retCode = sendToServer(
-                SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
 
-            if (CKM_API_SUCCESS != retCode) {
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             recv.Deserialize(retCode);
 
@@ -70,16 +66,11 @@ public:
             }
 
             MessageBuffer recv;
-            auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::LOCK_USER_KEY),
-                                                 user);
-            int retCode = sendToServer(
-                SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
-
-            if (CKM_API_SUCCESS != retCode) {
+            auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::LOCK_USER_KEY), user);
+
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             recv.Deserialize(retCode);
 
@@ -94,16 +85,11 @@ public:
             }
 
             MessageBuffer recv;
-            auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::REMOVE_USER_DATA),
-                                                 user);
-            int retCode = sendToServer(
-                SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
-
-            if (CKM_API_SUCCESS != retCode) {
+            auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::REMOVE_USER_DATA), user);
+
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             recv.Deserialize(retCode);
 
@@ -124,14 +110,9 @@ public:
                     oldPassword,
                     newPassword);
 
-            int retCode = sendToServer(
-                SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
-
-            if (CKM_API_SUCCESS != retCode) {
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             recv.Deserialize(retCode);
 
@@ -151,14 +132,9 @@ public:
                     user,
                     newPassword);
 
-            int retCode = sendToServer(
-                SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
-
-            if (CKM_API_SUCCESS != retCode) {
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             recv.Deserialize(retCode);
 
@@ -173,16 +149,11 @@ public:
             }
 
             MessageBuffer recv;
-            auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::REMOVE_APP_DATA),
-                                                 smackLabel);
-            int retCode = sendToServer(
-                SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
-
-            if (CKM_API_SUCCESS != retCode) {
+            auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::REMOVE_APP_DATA), smackLabel);
+
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             recv.Deserialize(retCode);
 
@@ -194,14 +165,10 @@ public:
         return try_catch([&] {
             MessageBuffer recv;
             auto send = MessageBuffer::Serialize(static_cast<int>(ControlCommand::UPDATE_CC_MODE));
-            int retCode = sendToServer(
-                SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
 
-            if (CKM_API_SUCCESS != retCode) {
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             recv.Deserialize(retCode);
 
@@ -223,18 +190,15 @@ public:
                                                  alias,
                                                  accessor,
                                                  static_cast<int>(granted));
-            int retCode = sendToServer(
-                    SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
 
-            if (CKM_API_SUCCESS != retCode) {
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             int command;
             int counter;
             recv.Deserialize(command, counter, retCode);
+
             return retCode;
         });
     }
@@ -251,23 +215,22 @@ public:
                                                  owner,
                                                  alias,
                                                  accessor);
-            int retCode = sendToServer(
-                    SERVICE_SOCKET_CKM_CONTROL,
-                send.Pop(),
-                recv);
 
-            if (CKM_API_SUCCESS != retCode) {
+            int retCode = m_controlConnection.processRequest(send.Pop(), recv);
+            if (CKM_API_SUCCESS != retCode)
                 return retCode;
-            }
 
             int command;
             int counter;
             recv.Deserialize(command, counter, retCode);
+
             return retCode;
         });
     }
 
     virtual ~ControlImpl(){}
+private:
+    CKM::ServiceConnection m_controlConnection;
 };
 
 ControlShPtr Control::create() {
index fb66bcb..f6ffdb2 100644 (file)
@@ -45,14 +45,13 @@ namespace CKM {
 bool ManagerImpl::s_isInit = false;
 
 ManagerImpl::ManagerImpl()
-  : m_counter(0)
+  : m_counter(0), m_storageConnection(SERVICE_SOCKET_CKM_STORAGE), m_ocspConnection(SERVICE_SOCKET_OCSP)
 {
     // TODO secure with mutex
     if (!s_isInit) {
         s_isInit = true;
         clientInitialize();
     }
-
 }
 
 
@@ -76,14 +75,9 @@ int ManagerImpl::saveBinaryData(
                                              rawData,
                                              PolicySerializable(policy));
 
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
-
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
@@ -133,14 +127,10 @@ int ManagerImpl::removeBinaryData(const Alias &alias, DBDataType dataType)
                                              static_cast<int>(dataType),
                                              helper.getName(),
                                              helper.getLabel());
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
@@ -186,19 +176,15 @@ int ManagerImpl::getBinaryData(
                                              helper.getName(),
                                              helper.getLabel(),
                                              password);
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
         int tmpDataType;
-        recv.Deserialize(command, counter, retCode, tmpDataType,rawData);
+        recv.Deserialize(command, counter, retCode, tmpDataType, rawData);
         recvDataType = static_cast<DBDataType>(tmpDataType);
 
         if (counter != m_counter) {
@@ -291,14 +277,10 @@ int ManagerImpl::getBinaryDataAliasVector(DBDataType dataType, AliasVector &alia
         auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::GET_LIST),
                                              m_counter,
                                              static_cast<int>(dataType));
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
@@ -404,14 +386,10 @@ int ManagerImpl::createKeyPair(
                                              PolicySerializable(policyPublicKey),
                                              privateKeyAlias,
                                              publicKeyAlias);
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
@@ -431,7 +409,8 @@ int getCertChain(
     int counter,
     const CertificateShPtr &certificate,
     const T &sendData,
-    CertificateShPtrVector &certificateChainVector)
+    CertificateShPtrVector &certificateChainVector,
+    ServiceConnection & service_connection)
 {
     return try_catch([&] {
 
@@ -440,14 +419,10 @@ int getCertChain(
                                              counter,
                                              certificate->getDER(),
                                              sendData);
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = service_connection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int retCommand;
         int retCounter;
@@ -490,7 +465,8 @@ int ManagerImpl::getCertificateChain(
         ++m_counter,
         certificate,
         rawBufferVector,
-        certificateChainVector);
+        certificateChainVector,
+        m_storageConnection);
 }
 
 int ManagerImpl::getCertificateChain(
@@ -503,7 +479,8 @@ int ManagerImpl::getCertificateChain(
         ++m_counter,
         certificate,
         untrustedCertificates,
-        certificateChainVector);
+        certificateChainVector,
+        m_storageConnection);
 }
 
 int ManagerImpl::createSignature(
@@ -528,18 +505,13 @@ int ManagerImpl::createSignature(
                                              message,
                                              static_cast<int>(hash),
                                              static_cast<int>(padding));
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
-
         recv.Deserialize(command, counter, retCode, signature);
 
         if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE))
@@ -575,18 +547,13 @@ int ManagerImpl::verifySignature(
                                              signature,
                                              static_cast<int>(hash),
                                              static_cast<int>(padding));
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
-
         recv.Deserialize(command, counter, retCode);
 
         if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE))
@@ -612,17 +579,11 @@ int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspSta
 
         auto send = MessageBuffer::Serialize(my_counter, rawCertChain);
 
-        int retCode = sendToServer(
-            SERVICE_SOCKET_OCSP,
-            send.Pop(),
-            recv);
-
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_ocspConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int counter;
-
         recv.Deserialize(counter, retCode, ocspStatus);
 
         if (my_counter != counter) {
@@ -646,14 +607,10 @@ int ManagerImpl::allowAccess(const Alias &alias,
                                              alias,
                                              accessor,
                                              static_cast<int>(granted));
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
@@ -677,14 +634,10 @@ int ManagerImpl::denyAccess(const Alias &alias, const Label &accessor)
                                              my_counter,
                                              alias,
                                              accessor);
-        int retCode = sendToServer(
-            SERVICE_SOCKET_CKM_STORAGE,
-            send.Pop(),
-            recv);
 
-        if (CKM_API_SUCCESS != retCode) {
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
             return retCode;
-        }
 
         int command;
         int counter;
index 2810ce9..94dd881 100644 (file)
@@ -21,7 +21,7 @@
 #pragma once
 
 #include <protocols.h>
-
+#include <client-common.h>
 #include <ckm/ckm-type.h>
 #include <ckm/ckm-key.h>
 #include <ckm/ckm-manager.h>
@@ -132,6 +132,8 @@ protected:
 
     int m_counter;
     static bool s_isInit;
+    CKM::ServiceConnection m_storageConnection;
+    CKM::ServiceConnection m_ocspConnection;
 };
 
 } // namespace CKM