add: 添加通信消息定义,解析和处理

This commit is contained in:
SunnyQjm committed 2020-06-02 09:53:26 +08:00
1 parent 41c0e99dc3
commit f65bc6575e
14 files changed
+560 -14

No files matched your search

+8 -2
View File
@@ -3,8 +3,14 @@ project(MIRCertificationManager)
set(CMAKE_CXX_STANDARD 14)
add_executable(MIRCertificationManager main.cpp utils/RedisUtil.cpp)
target_link_libraries(MIRCertificationManager hiredis)
add_executable(MIRCertificationManager main.cpp
utils/RedisUtil.cpp
utils/RedisUtil.cpp
utils/NDNCertificationUtil.cpp
utils/JsonCppUtil.cpp
Server.cpp
)
target_link_libraries(MIRCertificationManager ndn-cxx boost_system hiredis jsoncpp)
add_executable(installCertificate installCertificate.cpp
utils/FileUtils.cpp
+98
View File
@@ -0,0 +1,98 @@
//
// Created by mingj on 2020/5/31.
//
#include "Server.h"
#include <utility>
#include "utils/JsonCppUtil.h"
#include "message/BaseRequestMessage.h"
#include "message/AddOrUpdateCertificateRequestMessage.h"
#include "message/RemoveCertificateRequestMessage.h"
#include "message/GetLifetimeRequestMessage.h"
#include "message/ExistsCertificateRequestMessage.h"
Server::Server(std::string prefix) :
registerPrefix(std::move(prefix)) {
}
void Server::run() {
// 连接到本地的redis服务
ndnCertificationUtil.connect();
face.setInterestFilter(this->registerPrefix,
[this](const InterestFilter &filter, const Interest &interest) {
this->onInterest(filter, interest);
},
RegisterPrefixSuccessCallback(),
[this](const Name &prefix, const std::string &reason) {
this->onRegisterFailed(prefix, reason);
}
);
face.processEvents();
}
void Server::onInterest(const InterestFilter &filter, const Interest &interest) {
if (interest.getApplicationParameters().value_size() <= 0)
return;
std::string params((char *) (interest.getApplicationParameters().value()),
interest.getApplicationParameters().value_size());
JsonCppUtil jsonCppUtil(params);
Data data(interest.getName());
Json::Value root;
int resCode;
long longResult;
AddOrUpdateCertificateRequestMessage addOrUpdateCertificateRequestMessage;
RemoveCertificateRequestMessage removeCertificateRequestMessage;
GetLifetimeRequestMessage getLifetimeRequestMessage;
ExistsCertificateRequestMessage existsCertificateRequestMessage;
switch (jsonCppUtil.getInt("code")) {
case BaseRequestMessage::ADD_OR_UPDATE_CERTIFICATE: // 下发或更新证书
addOrUpdateCertificateRequestMessage.parse(params);
root["code"] = ndnCertificationUtil.installCert(addOrUpdateCertificateRequestMessage.getCertStr(),
addOrUpdateCertificateRequestMessage.getLifetime(),
addOrUpdateCertificateRequestMessage.isForceUpdate());
root["errMsg"] = addOrUpdateCertificateRequestMessage.getErrorMessage(root["code"].asInt());
break;
case BaseRequestMessage::REMOVE_CERTIFICATE: // 撤销证书
removeCertificateRequestMessage.parse(params);
resCode = ndnCertificationUtil.uninstallCert(removeCertificateRequestMessage.getCertStr());
if (resCode >= 0) {
resCode = 0;
} else {
resCode = resCode;
}
root["code"] = resCode;
root["errMsg"] = removeCertificateRequestMessage.getErrorMessage(resCode);
break;
case BaseRequestMessage::GET_CERTIFICATE_LIFETIME: // 获取证书的生存期
getLifetimeRequestMessage.parse(params);
longResult = ndnCertificationUtil.getCertLifetime(getLifetimeRequestMessage.getCertStr());
root["data"] = (long long int) longResult;
if (longResult >= 0) {
resCode = 0;
} else {
resCode = (int) longResult;
}
root["code"] = resCode;
root["errMsg"] = getLifetimeRequestMessage.getErrorMessage(resCode);
break;
case BaseRequestMessage::EXISTS_CERTIFICATE: // 证书是否存在
existsCertificateRequestMessage.parse(params);
if (ndnCertificationUtil.exists(existsCertificateRequestMessage.getCertStr())) {
root["code"] = 0;
} else {
root["code"] = -1;
}
root["errMsg"] = existsCertificateRequestMessage.getErrorMessage(root["code"].asInt());
break;
default:
std::cerr << "未能处理的请求:" << endl << params << endl;
break;
}
}
void Server::onRegisterFailed(const Name &prefix, const std::string &reason) {
}
+35
View File
@@ -0,0 +1,35 @@
//
// Created by mingj on 2020/5/31.
//
#ifndef MIRCERTIFICATIONMANAGER_SERVER_H
#define MIRCERTIFICATIONMANAGER_SERVER_H
#include <ndn-cxx/face.hpp>
#include <ndn-cxx/security/key-chain.hpp>
#include <string>
#include "utils/NDNCertificationUtil.h"
using namespace ndn;
class Server {
public:
explicit
Server(std::string prefix = "/localmanager/mircertification");
void run();
private:
void onInterest(const InterestFilter &filter, const Interest &interest);
void onRegisterFailed(const Name &prefix, const std::string &reason);
private:
Face face;
KeyChain keyChain;
NDNCertificationUtil ndnCertificationUtil;
std::string registerPrefix;
};
#endif //MIRCERTIFICATIONMANAGER_SERVER_H
+27 -9
View File
@@ -1,5 +1,6 @@
#include <iostream>
#include "utils/RedisUtil.h"
#include "Server.h"
/**
*
@@ -12,14 +13,31 @@
4. 把退出登录的功能也做一做。即从redis中删除证书。加载证书和删除证书的代码不写到NFD中,应单独开发。
* @return
*/
using namespace std;
int main() {
RedisUtil redisUtil;
redisUtil.connect("127.0.0.1", 6379);
std::cout << redisUtil.set("test", "1234") << std::endl;
auto result = redisUtil.get("test1");
std::cout << result << std::endl;
std::cout << "exists: " << redisUtil.exist("test") << std::endl;
redisUtil.del("test");
std::cout << "exists: " << redisUtil.exist("test") << std::endl;
return 0;
Server server;
server.run();
// AddOrUpdateCertificateRequestMessage addOrUpdateCertificateRequestMessage;
// addOrUpdateCertificateRequestMessage.parse("{\n"
// "\"code\": 1,\n"
// "\"data\": {\n"
// "\"certStr\": \"证书的base64编码字符串\",\n"
// "\"lifetime\": 1000,\n"
// "\"forceUpdate\": false\n"
// "}\n"
// "}");
// cout << addOrUpdateCertificateRequestMessage.getCertStr() << endl;
// RedisUtil redisUtil;
// redisUtil.connect("127.0.0.1", 6379);
// std::cout << redisUtil.set("test", "1234") << std::endl;
// auto result = redisUtil.get("test1");
// std::cout << result << std::endl;
// std::cout << "exists: " << redisUtil.exist("test") << std::endl;
// redisUtil.del("test");
// std::cout << "exists: " << redisUtil.exist("test") << std::endl;
// return 0;
}
@@ -0,0 +1,65 @@
//
// Created by mingj on 2020/5/31.
//
#ifndef MIRCERTIFICATIONMANAGER_ADDORUPDATECERTIFICATEREQUESTMESSAGE_H
#define MIRCERTIFICATIONMANAGER_ADDORUPDATECERTIFICATEREQUESTMESSAGE_H
#include "BaseRequestMessage.h"
class AddOrUpdateCertificateRequestMessage : public BaseRequestMessage {
public:
struct AddOrUpdateCertificateRequestData {
std::string certStr = "";
long lifetime = -1;
bool forceUpdate = false;
};
std::string getErrorMessage(int responseCode) override {
switch (responseCode) {
case 0:
return "";
case -1:
return "证书无效";
case -2:
return "存在同名证书,且未覆盖";
default:
return "未知错误";
}
}
BaseRequestMessage *parse(const std::string &jsonStr) override {
Json::Reader reader;
Json::Value root;
reader.parse(jsonStr, root);
this->code = code;
int code = root["code"].asInt();
if (code != 1)
return this;
addOrUpdateCertificateRequestData.certStr = root["data"]["certStr"].asString();
addOrUpdateCertificateRequestData.lifetime = root["data"]["lifetime"].asUInt64();
addOrUpdateCertificateRequestData.forceUpdate = root["data"]["forceUpdate"].asBool();
return this;
}
std::string getCertStr() const {
return addOrUpdateCertificateRequestData.certStr;
}
long getLifetime() const {
return addOrUpdateCertificateRequestData.lifetime;
}
bool isForceUpdate() const {
return addOrUpdateCertificateRequestData.forceUpdate;
}
private:
AddOrUpdateCertificateRequestData addOrUpdateCertificateRequestData;
};
#endif //MIRCERTIFICATIONMANAGER_ADDORUPDATECERTIFICATEREQUESTMESSAGE_H
+67
View File
@@ -0,0 +1,67 @@
//
// Created by mingj on 2020/5/31.
//
#ifndef MIRCERTIFICATIONMANAGER_BASEREQUESTMESSAGE_H
#define MIRCERTIFICATIONMANAGER_BASEREQUESTMESSAGE_H
#include <string>
#include <utility>
#include <jsoncpp/json/json.h>
class BaseRequestMessage {
public:
static const int ADD_OR_UPDATE_CERTIFICATE;
static const int REMOVE_CERTIFICATE;
static const int GET_CERTIFICATE_LIFETIME;
static const int EXISTS_CERTIFICATE;
int getCode() const {
return code;
}
virtual BaseRequestMessage *parse(const std::string &jsonStr) {};
virtual std::string getErrorMessage(int responseCode) {};
// static BaseRequestMessage parse(const std::string &jsonStr) {
// Json::Reader reader;
// Json::Value root;
// reader.parse(jsonStr, root);
//
// int code = root["code"].asInt();
// switch (code) {
// case 1:
// AddOrUpdateCertificateRequestData addOrUpdateCertificateRequestData;
// addOrUpdateCertificateRequestData.certStr = root["data"]["certStr"].asString();
// addOrUpdateCertificateRequestData.lifetime = root["data"]["lifetime"].asUInt64();
// addOrUpdateCertificateRequestData.forceUpdate = root["data"]["forceUpdate"].asBool();
// return BaseRequestMessage<AddOrUpdateCertificateRequestData>(code, AddOrUpdateCertificateRequestData());
// case 2:
// RemoveCertificateRequestData removeCertificateRequestData;
// removeCertificateRequestData.certStr = root["data"]["certStr"].asString();
// return BaseRequestMessage<RemoveCertificateRequestData>(code, removeCertificateRequestData);
// case 3:
// GetCertificateLifetimeRequestData getCertificateLifetimeRequestData;
// getCertificateLifetimeRequestData.certStr = root["data"]["certStr"].asString();
// return BaseRequestMessage<GetCertificateLifetimeRequestData>(code, getCertificateLifetimeRequestData);
// case 4:
// ExistsCertificateRequestData existsCertificateRequestData;
// existsCertificateRequestData.certStr = root["data"]["certStr"].asString();
// return BaseRequestMessage<ExistsCertificateRequestData>(code, existsCertificateRequestData);
// default:
// return BaseRequestMessage<std::string>(code, "");
// }
// }
protected:
int code{0};
};
const int BaseRequestMessage::ADD_OR_UPDATE_CERTIFICATE = 1;
const int BaseRequestMessage::REMOVE_CERTIFICATE = 2;
const int BaseRequestMessage::GET_CERTIFICATE_LIFETIME = 3;
const int BaseRequestMessage::EXISTS_CERTIFICATE = 4;
#endif //MIRCERTIFICATIONMANAGER_BASEREQUESTMESSAGE_H
+47
View File
@@ -0,0 +1,47 @@
//
// Created by mingj on 2020/6/2.
//
#ifndef MIRCERTIFICATIONMANAGER_EXISTSCERTIFICATEREQUESTMESSAGE_H
#define MIRCERTIFICATIONMANAGER_EXISTSCERTIFICATEREQUESTMESSAGE_H
#include "BaseRequestMessage.h"
class ExistsCertificateRequestMessage : BaseRequestMessage {
public:
struct ExistsCertificateRequestData {
std::string certStr = "";
};
std::string getErrorMessage(int responseCode) override {
switch (responseCode) {
case 0:
return "";
case -1:
return "没有对应的条目";
default:
return "未知错误";
}
}
BaseRequestMessage * parse(const std::string &jsonStr) override {
Json::Reader reader;
Json::Value root;
reader.parse(jsonStr, root);
this->code = code;
int code = root["code"].asInt();
if (code != 1)
return this;
existsCertificateRequestData.certStr = root["data"]["certStr"].asString();
return this;
}
std::string getCertStr() const {
return existsCertificateRequestData.certStr;
}
private:
ExistsCertificateRequestData existsCertificateRequestData;
};
#endif //MIRCERTIFICATIONMANAGER_EXISTSCERTIFICATEREQUESTMESSAGE_H
+51
View File
@@ -0,0 +1,51 @@
//
// Created by mingj on 2020/6/2.
//
#ifndef MIRCERTIFICATIONMANAGER_GETLIFETIMEREQUESTMESSAGE_H
#define MIRCERTIFICATIONMANAGER_GETLIFETIMEREQUESTMESSAGE_H
#include "BaseRequestMessage.h"
class GetLifetimeRequestMessage : BaseRequestMessage {
public:
struct GetLifetimeRequestData {
std::string certStr = "";
};
std::string getErrorMessage(int responseCode) override {
if (responseCode > 0) {
return "";
}
switch (responseCode) {
case 0:
return "";
case -2:
return "没有对应的条目";
default:
return "未知错误";
}
}
BaseRequestMessage *parse(const std::string &jsonStr) override {
Json::Reader reader;
Json::Value root;
reader.parse(jsonStr, root);
this->code = code;
int code = root["code"].asInt();
if (code != 1)
return this;
getLifetimeRequestData.certStr = root["data"]["certStr"].asString();
return this;
}
std::string getCertStr() const {
return getLifetimeRequestData.certStr;
}
private:
GetLifetimeRequestData getLifetimeRequestData;
};
#endif //MIRCERTIFICATIONMANAGER_GETLIFETIMEREQUESTMESSAGE_H
+50
View File
@@ -0,0 +1,50 @@
//
// Created by mingj on 2020/6/2.
//
#ifndef MIRCERTIFICATIONMANAGER_REMOVECERTIFICATEREQUESTMESSAGE_H
#define MIRCERTIFICATIONMANAGER_REMOVECERTIFICATEREQUESTMESSAGE_H
#include "BaseRequestMessage.h"
class RemoveCertificateRequestMessage : BaseRequestMessage {
public:
struct RemoveCertificateRequestData {
std::string certStr = "";
};
std::string getErrorMessage(int responseCode) override {
switch (responseCode) {
case 0:
return "";
case -1:
return "证书无效";
case -2:
return "没有对应的条目";
default:
return "未知错误";
}
}
BaseRequestMessage * parse(const std::string &jsonStr) override {
Json::Reader reader;
Json::Value root;
reader.parse(jsonStr, root);
this->code = code;
int code = root["code"].asInt();
if (code != 1)
return this;
removeCertificateRequestData.certStr = root["data"]["certStr"].asString();
return this;
}
std::string getCertStr() const {
return removeCertificateRequestData.certStr;
}
private:
RemoveCertificateRequestData removeCertificateRequestData;
};
#endif //MIRCERTIFICATIONMANAGER_REMOVECERTIFICATEREQUESTMESSAGE_H
+37
View File
@@ -0,0 +1,37 @@
//
// Created by mingj on 18-12-22.
//
#include "JsonCppUtil.h"
JsonCppUtil::JsonCppUtil(const string &jsonStr) {
Json::Reader reader;
//将根节点解析到root当中
reader.parse(jsonStr, this->root);
}
string JsonCppUtil::getString(const string &key) {
return root[key].asString();
}
int JsonCppUtil::getInt(const string &key) {
return root[key].asInt();
}
unsigned long long JsonCppUtil::getull(const string &key) {
return root[key].asUInt64();
}
bool JsonCppUtil::getBool(const string &key) {
return root[key].asBool();
}
double JsonCppUtil::getDouble(const string &key) {
return root[key].asDouble();
}
Json::Value JsonCppUtil::get(const string &key) {
return root[key];
}
+34
View File
@@ -0,0 +1,34 @@
//
// Created by mingj on 18-12-22.
//
#ifndef IP_NDN_STACK_CPP_JSONCPPHELPER_H
#define IP_NDN_STACK_CPP_JSONCPPHELPER_H
#include <jsoncpp/json/json.h>
#include <iostream>
#include <fstream>
using namespace std;
class JsonCppUtil {
public:
explicit JsonCppUtil(const string &jsonStr);
string getString(const string &key);
int getInt(const string &key);
unsigned long long getull(const string &key);
bool getBool(const string &key);
double getDouble(const string &key);
Json::Value get(const string &key);
private:
Json::Value root;
};
#endif //IP_NDN_STACK_CPP_JSONCPPHELPER_H
+20 -1
View File
@@ -45,7 +45,7 @@ bool NDNCertificationUtil::connect(const std::string &host, int port) {
* -1 -> 证书无效
* -2 -> 已经存在
*/
int NDNCertificationUtil::installCert(const std::string &certStr, bool forceUpdate) {
int NDNCertificationUtil::installCert(const std::string &certStr, long lifetime, bool forceUpdate) {
// 首先验证证书是否有效
if (!isValidBlock(certStr))
return -1;
@@ -56,6 +56,7 @@ int NDNCertificationUtil::installCert(const std::string &certStr, bool forceUpda
if (!forceUpdate && redisUtil.exist(cert.getName().toUri()) > 0) {
return -2;
}
redisUtil.setLifeTime(cert.getName().toUri(), lifetime);
return redisUtil.set(cert.getName().toUri(), certStr) == 0;
}
@@ -145,3 +146,21 @@ bool NDNCertificationUtil::verifyData(const ndn::Data &data) {
auto result = ndn::security::verifySignature(data, cert);
return result;
}
/**
* 获取证书剩余存活时间,单位为毫秒;
*
*
* @param key
* @return
* -2 => 返回-2表示key对应的条目不存在(可能是本来就没有这个条目,或者本来有一个条目,但是因为设置了存活期,其存活期已到,被移除了)
* -1 => 返回-1表示key对应的条目存在,但是没有设置过存活期,是持久保存的
* >= 0 => 返回大于等于0的值表示key对应的条目存在,且其剩余的存活时间为返回的值,单位为毫秒
*/
long NDNCertificationUtil::getCertLifetime(const std::string &key) {
return redisUtil.getRemainingTime(key);
}
bool NDNCertificationUtil::exists(const std::string &key) {
return redisUtil.exist(key) > 0;
}
+20 -1
View File
@@ -47,7 +47,7 @@ public:
* -1 -> 证书无效
* -2 -> 已经存在
*/
int installCert(const std::string &certStr, bool forceUpdate = false);
int installCert(const std::string &certStr, long lifetime = -1, bool forceUpdate = false);
/**
* 卸载证书操作
@@ -68,6 +68,25 @@ public:
*/
std::string getCertStrByKeyName(const std::string &keyName);
/**
* 获取证书剩余存活时间,单位为毫秒;
*
*
* @param key
* @return
* -2 => 返回-2表示key对应的条目不存在(可能是本来就没有这个条目,或者本来有一个条目,但是因为设置了存活期,其存活期已到,被移除了)
* -1 => 返回-1表示key对应的条目存在,但是没有设置过存活期,是持久保存的
* >= 0 => 返回大于等于0的值表示key对应的条目存在,且其剩余的存活时间为返回的值,单位为毫秒
*/
long getCertLifetime(const std::string &key);
/**
* 判断某个证书是否存在
* @param key
* @return 如果存在则返回大于0的值,典型值为1
*/
bool exists(const std::string &key);
/**
* 验证兴趣包签名是否有效
* @param interest
+1 -1
View File
@@ -99,7 +99,7 @@ int RedisUtil::setLifeTime(const std::string &key, long lifetime) {
* @param key
* @return
* -2 => 返回-2表示key对应的条目不存在(可能是本来就没有这个条目,或者本来有一个条目,但是因为设置了存活期,其存活期已到,被移除了)
* -1 => 返回-1表示key对应的条目存在,但是没有设置过存活期,是持久保存的
* -1 => 返回-1表示key对应的条目存在,但是没有设置过存活期,是持久保存的
* >= 0 => 返回大于等于0的值表示key对应的条目存在,且其剩余的存活时间为返回的值,单位为毫秒
*/
long RedisUtil::getRemainingTime(const std::string &key) {