⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registrar.cpp

📁 与secs相关的hsms协议
💻 CPP
字号:
/*    *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net) * *   This file is part of FREESECS. * *   FREESECS is free software: you can redistribute it and/or modify *   it under the terms of the GNU General Public License as published by *   the Free Software Foundation, either version 3 of the License, or *   (at your option) any later version. * *   FREESECS is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU General Public License for more details. * *   You should have received a copy of the GNU General Public License *   along with FREESECS, see COPYING. *   If not, see <http://www.gnu.org/licenses/>. */#include <sys/stat.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <fcntl.h>#include "log.h"#include "hsms_signums.h"#include "hsms_cnx.h"#include "hsms_factory.h"#include "hsmsd_interface.h"#include "user_agent.h"#include "registrar.h"#define REGISTRAR_LOG_LEVEL 20#define DIR_PERMS (S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH)#define FIFO_PERMS (S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)using namespace freesecs;extern hsms_factory_t hsms_factory;hsmsd_client_registrar_t::hsmsd_client_registrar_t(){    struct stat statbuf;    pid_t childpid;    if(0 == stat(HSMSD_FIFO_DIR, &statbuf))    {        if(-1 == (childpid = fork()))        {            TRACE_ERROR("HSMSD: failed to fork");            throw;        }        if(0 == childpid) /*child node*/        {            execl("/bin/rm", "rm", "-rf", HSMSD_FIFO_DIR, NULL);            exit(1);        }                if(childpid != wait(NULL)) /*parent node*/        {            TRACE_ERROR("HSMSD: failed to wait for child completion");            throw;        }    }    if(mkdir(HSMSD_FIFO_DIR, DIR_PERMS))    {            TRACE_ERROR("HSMSD: failed to create dir %s", HSMSD_FIFO_DIR);            throw;    }        mode_t old_umask = umask(~FIFO_PERMS);        if(-1 == mkfifo(HSMSD_REGISTER_FIFO_IN, FIFO_PERMS) && (errno != EEXIST))    {        umask(old_umask);        TRACE_ERROR("HSMSD: failed to create registering FIFO %s", HSMSD_REGISTER_FIFO);        throw;    }    if(-1 == mkfifo(HSMSD_REGISTER_FIFO_OUT, FIFO_PERMS) &&(errno != EEXIST))    {        umask(old_umask);        TRACE_ERROR("HSMSD: failed to create registering FIFO %s", HSMSD_REGISTER_FIFO);        throw;    }    umask(old_umask);    if(-1 == (_fd_in = open(HSMSD_REGISTER_FIFO_IN, O_RDWR)))    {        TRACE_ERROR("HSMSD: failed to open registering FIFO %s", HSMSD_REGISTER_FIFO);        throw;    }    if(-1 == (_fd_out = open(HSMSD_REGISTER_FIFO_OUT, O_RDWR)))    {        TRACE_ERROR("HSMSD: failed to open registering FIFO %s", HSMSD_REGISTER_FIFO);        throw;    }    _ar = new async_reception_t(_fd_in, "hsms client registrar");    _ar->data_recvd_signal.add_handler(this, &hsmsd_client_registrar_t::data_rcvd_handler);    _ar->recv_error_signal.add_handler(this, &hsmsd_client_registrar_t::rcv_error_handler);    TRACE_DEBUG(REGISTRAR_LOG_LEVEL, "HSMSD: start receiving from %s...", HSMSD_REGISTER_FIFO);    _ar->start_recv(&register_req, sizeof(register_req));}hsmsd_client_registrar_t::~hsmsd_client_registrar_t(){    _ar->stop_recv();    close(_fd_in);    close(_fd_out);}voidhsmsd_client_registrar_t::process(){    sleep(10);}int hsmsd_client_registrar_t::data_rcvd_handler(const size_t& num_received){    TRACE_DEBUG(REGISTRAR_LOG_LEVEL, "HSMSD: received %d bytes from %s", num_received, HSMSD_REGISTER_FIFO);    if(num_received == sizeof(register_req))    {        process_user_request();    }    else    {        TRACE_ERROR("HSMSD: received %d bytes from %s. Expected %d bytes.",                                     num_received, HSMSD_REGISTER_FIFO, sizeof(register_req));    }    _ar->start_recv(&register_req, sizeof(register_req));    return 0;}int hsmsd_client_registrar_t::rcv_error_handler(const int& err){    TRACE_DEBUG(REGISTRAR_LOG_LEVEL, "HSMSD: received err %d from %s", HSMSD_REGISTER_FIFO);    return 0;}template<typename T>struct compare_name : public std::binary_function<T, const char*, bool>{    bool operator()(const T &t, const char *name) const    {        return 0 == strcmp(t->name(), name);    }};void hsmsd_client_registrar_t::process_user_request(){       hsmsd_register_rsp_t register_rsp;    char    client_fifo_name_ctl_in[128],            client_fifo_name_ctl_out[128],             client_fifo_name_data_in[128],            client_fifo_name_data_out[128];    register_rsp.rsp_code = hsmsd_register_rsp_t::REG_OK;    snprintf(client_fifo_name_ctl_in, sizeof(client_fifo_name_ctl_in),                       HSMSD_CLIENT_FIFO_CTL_IN_PATTERN, register_req.cli_pid, rand()&0xff);    snprintf(client_fifo_name_ctl_out, sizeof(client_fifo_name_ctl_out),                       HSMSD_CLIENT_FIFO_CTL_OUT_PATTERN, register_req.cli_pid, rand()&0xff);    snprintf(client_fifo_name_data_in, sizeof(client_fifo_name_data_in),                       HSMSD_CLIENT_FIFO_DATA_IN_PATTERN, register_req.cli_pid, rand()&0xff);    snprintf(client_fifo_name_data_out, sizeof(client_fifo_name_data_out),                       HSMSD_CLIENT_FIFO_DATA_OUT_PATTERN, register_req.cli_pid, rand()&0xff);    snprintf(register_rsp.pipe_name_ctl_req,                       sizeof(register_rsp.pipe_name_ctl_req), client_fifo_name_ctl_in);    snprintf(register_rsp.pipe_name_ctl_rsp,                       sizeof(register_rsp.pipe_name_ctl_rsp), client_fifo_name_ctl_out);    snprintf(register_rsp.pipe_name_data_req,                       sizeof(register_rsp.pipe_name_data_req), client_fifo_name_data_in);    snprintf(register_rsp.pipe_name_data_rsp,                       sizeof(register_rsp.pipe_name_data_rsp), client_fifo_name_data_out);    /*TODO: error check according to rsp codes*/    TRACE_DEBUG(REGISTRAR_LOG_LEVEL, "HSMSD: number of active: %d, number of passive: %d",                       hsms_factory.active.size(), hsms_factory.passive.size());    std::vector<hsms_factory_t::active_cnx_ptr_t>::iterator it_active =                      find_if(hsms_factory.active.begin(), hsms_factory.active.end(),                                 std::bind2nd(compare_name<hsms_factory_t::active_cnx_ptr_t>(),                                 register_req.cnx_name));    if(hsms_factory.active.end() != it_active)    {        _clients.push_back(pclient_t(new user_agent_t(client_fifo_name_ctl_in                                                        ,client_fifo_name_ctl_out                                                        ,client_fifo_name_data_in                                                        ,client_fifo_name_data_out                                                        ,*((user_agent_t::pcnx_t*)&*it_active))));    }    else    {        std::vector<hsms_factory_t::passive_cnx_ptr_t>::iterator it_passive =                        find_if(hsms_factory.passive.begin(), hsms_factory.passive.end(),                                 std::bind2nd(compare_name<hsms_factory_t::passive_cnx_ptr_t>(),                                 register_req.cnx_name));        if(hsms_factory.passive.end() != it_passive)        {            _clients.push_back(pclient_t(new user_agent_t(client_fifo_name_ctl_in                                                            ,client_fifo_name_ctl_out                                                            ,client_fifo_name_data_in                                                            ,client_fifo_name_data_out                                                            ,*((user_agent_t::pcnx_t*)&*it_passive))));        }        else        {            TRACE_ERROR("HSMSD: process_user_request: invalid cnx name: %s", register_req.cnx_name);            register_rsp.rsp_code = hsmsd_register_rsp_t::REG_INVALID_NAME;        }    }    if(sizeof(register_rsp) != write(_fd_out, &register_rsp, sizeof(register_rsp)))    {        TRACE_ERROR("HSMSD: process_user_request: error responding to register request");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -