From 68e26768fcbbd091e6a7ce67c015c2905fc4b7d1 Mon Sep 17 00:00:00 2001 From: Saturneic Date: Sat, 9 Feb 2019 00:05:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E6=95=B0=E5=8F=91=E7=94=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Net.xcodeproj/project.pbxproj | 4 ++-- include/instruct.h | 1 + include/{rng.h => rng.hpp} | 0 include/server.h | 15 ++++++++++----- src/client.cpp | 2 +- src/controller.cpp | 2 +- src/main.cpp | 4 ++++ src/server.cpp | 15 ++++++++++++--- 8 files changed, 31 insertions(+), 12 deletions(-) rename include/{rng.h => rng.hpp} (100%) diff --git a/Net.xcodeproj/project.pbxproj b/Net.xcodeproj/project.pbxproj index b0e7b4d..b025689 100644 --- a/Net.xcodeproj/project.pbxproj +++ b/Net.xcodeproj/project.pbxproj @@ -80,7 +80,7 @@ 92C34C38220747B300AB38D3 /* sha1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = sha1.h; path = include/sha1.h; sourceTree = ""; }; 92C34C3922074B5B00AB38D3 /* rsa.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = rsa.h; path = include/rsa.h; sourceTree = ""; }; 92C34C3A22074B6500AB38D3 /* rsa.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = rsa.cpp; path = src/rsa.cpp; sourceTree = ""; }; - 92C34C3C2207F37A00AB38D3 /* rng.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = rng.h; path = include/rng.h; sourceTree = ""; }; + 92C34C3C2207F37A00AB38D3 /* rng.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = rng.hpp; path = include/rng.hpp; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -130,7 +130,7 @@ 9277A14C21FD7246009C5F11 /* clock.h */, 9277A14621FD7246009C5F11 /* cmap.h */, 9277A14921FD7246009C5F11 /* compute.h */, - 92C34C3C2207F37A00AB38D3 /* rng.h */, + 92C34C3C2207F37A00AB38D3 /* rng.hpp */, 92C34C38220747B300AB38D3 /* sha1.h */, 9277A14B21FD7246009C5F11 /* cpart.h */, 9277A14A21FD7246009C5F11 /* cthread.h */, diff --git a/include/instruct.h b/include/instruct.h index 3bbe6df..37c18b6 100644 --- a/include/instruct.h +++ b/include/instruct.h @@ -19,6 +19,7 @@ #include "cthread.h" #include "sha1.h" #include "rsa.h" +#include "rng.hpp" namespace error{ void printError(string error_info); diff --git a/include/rng.h b/include/rng.hpp similarity index 100% rename from include/rng.h rename to include/rng.hpp diff --git a/include/server.h b/include/server.h index d06b334..e14e380 100644 --- a/include/server.h +++ b/include/server.h @@ -15,7 +15,7 @@ #include "cthread.h" #include "sqlite3.h" #include "rsa.h" -#include "rng.h" +#include "rng.hpp" class Server; @@ -31,7 +31,7 @@ struct compute_result{ //请求数据包 struct request { // 匹配id - rng::rng64 r_id; + uint64_t r_id; // 类型 string type; // 数据 @@ -48,7 +48,7 @@ struct request { struct encrypt_post{ // 明文部分 // 注册客户端id - rng::rng64 client_id; + uint64_t client_id; // 目标ip string ip; // 目标端口 @@ -65,7 +65,7 @@ struct encrypt_post{ //回复数据包 struct respond { - rng::rng64 r_id; + uint64_t r_id; string type; Byte *buff = nullptr; uint32_t buff_size; @@ -90,7 +90,7 @@ public: //注册客户端管理 struct client_register{ // 客户端id - rng::rng64 client_id; + uint64_t client_id; // 通信密钥 rng::rng128 key; @@ -138,6 +138,11 @@ struct server_info{ string key; }; +struct aes_key256{ + uint64_t key[4]; + aes_key256(); +}; + //通用服务器类 class Server{ protected: diff --git a/src/client.cpp b/src/client.cpp index c96dde0..45c3e81 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -77,7 +77,7 @@ void Client::ProcessRequestListener(void){ for(auto &lreq : req_lst){ if(!lreq->active) continue; // 检查回复号与请求号是否相同 - if(!memcmp(&lreq->p_req->r_id,&pres->r_id,sizeof(rng::rng64))){ + if(lreq->p_req->r_id == pres->r_id){ // 调用回调函数 lreq->callback(pres,lreq->args); lreq->active = false; diff --git a/src/controller.cpp b/src/controller.cpp index 4f9041d..046252a 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -342,7 +342,7 @@ int client(string instruct, vector &configs, vector &lconfigs, v public_key_class *ppbc = (public_key_class *)sqlite3_column_blob(psqlsmt, 0); nclt.SetPublicKey(*ppbc); sqlite3_finalize(psqlsmt); - rng::rng128 key + // 已获得主广场服务器的密钥,进行启动客户端守护进程前的准备工作 nclt.NewRequest(&preq, msqe_ip, msqe_port, "client-register request", ""); nclt.NewRequestListener(preq, 30, psql, getSQEPublicKey); diff --git a/src/main.cpp b/src/main.cpp index 0eb4889..1ff427d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,10 @@ #include "instruct.h" +// 初始化随机数引擎 +rng::rng64 rand64(rng::tsc_seed{}()); +rng::rng128 rand128({rng::tsc_seed{}(),rng::tsc_seed{}()}); + int main(int argc, const char *argv[]){ // 命令 string instruct; diff --git a/src/server.cpp b/src/server.cpp index 1296721..5fd5e48 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -10,6 +10,11 @@ #include "server.h" extern list clocks_list; +extern rng::rng64 rand64; +extern rng::rng128 rand128; + + + pthread_mutex_t mutex,mutex_rp,mutex_pktreq,mutex_sndpkt; void setServerClock(Server *psvr, int clicks){ @@ -438,7 +443,7 @@ SQEServer::SQEServer(int port):Server(port){ void SQEServer::Packet2Request(packet &pkt, request &req){ if(pkt.type == REQUSET_TYPE){ - req.r_id = *(rng::rng64 *)pkt.buffs[0].second; + req.r_id = *(uint64_t *)pkt.buffs[0].second; req.type = (const char *)pkt.buffs[1].second; req.data = (const char *)pkt.buffs[2].second; req.t_addr = Addr(*(struct sockaddr_in *)pkt.buffs[3].second); @@ -496,7 +501,7 @@ void SQEServer::ProcessRequset(void){ } void SQEServer::Packet2Respond(packet &pkt, respond &res){ - res.r_id = *(rng::rng64 *)pkt.buffs[0].second; + res.r_id = *(uint64_t *)pkt.buffs[0].second; res.t_addr.SetSockAddr(*(struct sockaddr_in *)pkt.buffs[1].second); res.type = (const char *)pkt.buffs[2].second; res.buff_size = pkt.buffs[3].first; @@ -514,7 +519,7 @@ void SQEServer::Respond2Packet(packet &pkt, respond &res){ } request::request(){ - r_id = rng::tsc_seed{}(); + r_id = rand64(); } void respond::SetBuff(Byte *buff, uint32_t size){ @@ -552,3 +557,7 @@ void Server::ProcessSendPackets(void){ packets_out.remove_if([](auto ppkt){return ppkt == nullptr;}); pthread_mutex_unlock(&mutex_sndpkt); } + +aes_key256::aes_key256(){ + for (int i = 0; i < 4; i++) key[i] = rand64(); +}