Added anf fixed.

This commit is contained in:
Saturneic 2019-02-04 16:31:54 +08:00
parent 726ee78691
commit 1d3b8ef25f
9 changed files with 404 additions and 88 deletions

View File

@ -21,7 +21,7 @@
92C34C282205C94600AB38D3 /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15221FD725F009C5F11 /* addr.cpp */; };
92C34C292205C95F00AB38D3 /* socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15321FD725F009C5F11 /* socket.cpp */; };
92C34C37220747A200AB38D3 /* sha1.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92C34C36220747A200AB38D3 /* sha1.cpp */; };
92C34C3B22074B6500AB38D3 /* rsa.c in Sources */ = {isa = PBXBuildFile; fileRef = 92C34C3A22074B6500AB38D3 /* rsa.c */; };
92C34C3B22074B6500AB38D3 /* rsa.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92C34C3A22074B6500AB38D3 /* rsa.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -68,7 +68,8 @@
92C34C36220747A200AB38D3 /* sha1.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = sha1.cpp; path = src/sha1.cpp; sourceTree = "<group>"; };
92C34C38220747B300AB38D3 /* sha1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = sha1.h; path = include/sha1.h; sourceTree = "<group>"; };
92C34C3922074B5B00AB38D3 /* rsa.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = rsa.h; path = include/rsa.h; sourceTree = "<group>"; };
92C34C3A22074B6500AB38D3 /* rsa.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = rsa.c; path = src/rsa.c; sourceTree = "<group>"; };
92C34C3A22074B6500AB38D3 /* rsa.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = rsa.cpp; path = src/rsa.cpp; sourceTree = "<group>"; };
92C34C3C2207F37A00AB38D3 /* rng.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = rng.h; path = include/rng.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -118,6 +119,7 @@
9277A14C21FD7246009C5F11 /* clock.h */,
9277A14621FD7246009C5F11 /* cmap.h */,
9277A14921FD7246009C5F11 /* compute.h */,
92C34C3C2207F37A00AB38D3 /* rng.h */,
92C34C38220747B300AB38D3 /* sha1.h */,
9277A14B21FD7246009C5F11 /* cpart.h */,
9277A14A21FD7246009C5F11 /* cthread.h */,
@ -134,7 +136,7 @@
9277A15121FD724C009C5F11 /* src */ = {
isa = PBXGroup;
children = (
92C34C3A22074B6500AB38D3 /* rsa.c */,
92C34C3A22074B6500AB38D3 /* rsa.cpp */,
9277A15221FD725F009C5F11 /* addr.cpp */,
9277A15C21FD725F009C5F11 /* clock.cpp */,
9277A15921FD725F009C5F11 /* cmap.cpp */,
@ -225,7 +227,7 @@
9277A190220079DB009C5F11 /* cproj_proj.cpp in Sources */,
92C34C37220747A200AB38D3 /* sha1.cpp in Sources */,
9277A16021FD725F009C5F11 /* md5.cpp in Sources */,
92C34C3B22074B6500AB38D3 /* rsa.c in Sources */,
92C34C3B22074B6500AB38D3 /* rsa.cpp in Sources */,
9277A16521FD725F009C5F11 /* cmap.cpp in Sources */,
9277A16221FD725F009C5F11 /* main.cpp in Sources */,
92C34C272205C63A00AB38D3 /* server.cpp in Sources */,

185
include/rng.h Executable file
View File

@ -0,0 +1,185 @@
#pragma once
//
// The code in this file is free and unencumbered software released
// into the public domain.
//
// <http://creativecommons.org/publicdomain/zero/1.0/>.
//
#ifndef RNG_H
#define RNG_H
#include <cstdint>
#include <random>
#include <thread>
#if defined(_MSC_VER)
/* Microsoft C/C++-compatible compiler
* Adaptive implementations
*/
#include <intrin.h>
#define _rdtsc() __rdtsc()
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
/* GCC-compatible compiler, targeting x86/x86-64
* NO implementation needed
*/
#include <x86intrin.h>
#elif defined(__GNUC__) && defined(__ARM_NEON__)
/* GCC-compatible compiler, targeting ARM with NEON
* Adaptive implementations
*/
#include <arm_neon.h>
#elif defined(__GNUC__) && defined(__IWMMXT__)
/* GCC-compatible compiler, targeting ARM with WMMX
* Adaptive implementations
*/
#include <mmintrin.h>
#elif (defined(__GNUC__) || defined(__xlC__)) && (defined(__VEC__) || defined(__ALTIVEC__))
/* XLC or GCC-compatible compiler, targeting PowerPC with VMX/VSX
* Adaptive implementations
*/
#include <altivec.h>
#elif defined(__GNUC__) && defined(__SPE__)
/* GCC-compatible compiler, targeting PowerPC with SPE
* Adaptive implementations
*/
#include <spe.h>
#endif
namespace rng {
//
// Bitwise circular left shift.
//
static inline std::uint64_t
rotl(const std::uint64_t x, int k)
{
return (x << k) | (x >> (64 - k));
}
//
// A simple random seed generator based on the entropy coming from the
// system thread/process scheduler. This is rather slow but seeds are
// normally generated very infrequently.
//
struct tsc_seed
{
using result_type = std::uint64_t;
result_type operator()()
{
std::uint64_t base = _rdtsc();
std::uint64_t seed = base & 0xff;
for (int i = 1; i < 8; i++) {
std::this_thread::yield();
seed |= ((_rdtsc() - base) & 0xff) << (i << 3);
}
return seed;
}
};
//
// A random seed generator based on std::random_device.
//
struct random_device_seed
{
using result_type = std::uint64_t;
result_type operator()()
{
std::random_device rd;
if (sizeof(result_type) > sizeof(std::random_device::result_type))
return rd() | (result_type{rd()} << 32);
else
return rd();
}
};
//
// A random number generator with 64-bit internal state. It is based on
// the code from here: http://xoroshiro.di.unimi.it/splitmix64.c
//
struct rng64
{
using result_type = std::uint64_t;
std::uint64_t state;
rng64(std::uint64_t seed = 1) : state{seed} {}
result_type operator()()
{
std::uint64_t z = (state += UINT64_C(0x9E3779B97F4A7C15));
z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
return z ^ (z >> 31);
}
};
//
// A random number generator with 128-bit internal state. It is based on
// the code from here: http://xoroshiro.di.unimi.it/xoroshiro128plus.c
//
struct rng128
{
using result_type = std::uint64_t;
std::uint64_t state[2];
rng128(std::uint64_t seed[2]) : state{seed[0], seed[1]} {}
rng128(std::uint64_t s0, std::uint64_t s1) : state{s0, s1} {}
rng128(std::uint64_t seed = 1)
{
rng64 seeder(seed);
state[0] = seeder();
state[1] = seeder();
}
result_type operator()()
{
const uint64_t s0 = state[0];
uint64_t s1 = state[1];
const uint64_t value = s0 + s1;
s1 ^= s0;
state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
state[1] = rotl(s1, 36);
return value;
}
// This is the jump function for the generator. It is equivalent
// to 2 ^ 64 calls to next(); it can be used to generate 2 ^ 64
// non-overlapping subsequences for parallel computations.
void jump()
{
static const std::uint64_t j[] = {0xbeac0467eba5facb, 0xd86b048b86aa9922};
std::uint64_t s0 = 0, s1 = 0;
for (std::size_t i = 0; i < sizeof j / sizeof j[0]; i++) {
for (int b = 0; b < 64; b++) {
if ((j[i] & UINT64_C(1) << b) != 0) {
s0 ^= state[0];
s1 ^= state[1];
}
operator()();
}
}
state[0] = s0;
state[1] = s1;
}
};
} // namespace rng
#endif // RNG_H

View File

@ -5,9 +5,6 @@
// This is the header file for the library librsaencrypt.a
// Change this line to the file you'd like to use as a source of primes.
// The format of the file should be one prime per line.
char *PRIME_SOURCE_FILE = "primes.txt";
struct public_key_class{
@ -23,7 +20,7 @@ struct private_key_class{
// This function generates public and private keys, then stores them in the structures you
// provide pointers to. The 3rd argument should be the text PRIME_SOURCE_FILE to have it use
// the location specified above in this header.
void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, const char *PRIME_SOURCE_FILE);
void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, string PRIME_SOURCE_FILE);
// This function will encrypt the data pointed to by message. It returns a pointer to a heap
// array containing the encrypted data, or NULL upon failure. This pointer should be freed when

View File

@ -13,6 +13,7 @@
#include "net.h"
#include "cpart.h"
#include "cthread.h"
#include "rsa.h"
class Server;
@ -25,6 +26,12 @@ struct compute_result{
vector<int> *fargs_out;
};
//请求数据包
struct request {
uint64_t r_id;
string type;
string data;
};
//通用数据包类
class packet{
@ -67,10 +74,11 @@ struct server_info{
class Server{
protected:
// 缓存通用数据包
vector<packet> packets_in;
list<packet *> packets_in;
// 缓存带标签的二进制串管理结构
vector<raw_data> rawdata_in;
list<raw_data *> rawdata_in;
struct server_info tsi;
sqlite3 *psql;
public:
// 服务器类的接收套接字对象与发送套接字对象
SocketUDPServer socket;
@ -83,13 +91,14 @@ public:
// 重新设置服务器的发送IP地址
void SetSendIP(string ip_addr);
// 将结构数据包转换成二进制串
static raw_data Packet2Rawdata(packet tpkt);
static void Packet2Rawdata(packet &tpkt, raw_data &rdt);
// 将通用二进制串转换为通用数据包
static packet Rawdata2Packet(raw_data trdta);
// 释放二进制串占用的空间
static void freeRawdataServer(struct raw_data trdt);
// 释放通用数据包包占用
static void freePcaketServer(struct packet tpkt);
// 释放计算结果包占用的空间
static void freeCPURServer(struct compute_result tcpur);
// 给二进制串贴上识别标签
@ -99,9 +108,11 @@ public:
// 检查消息串是否为一个贴上标签的二进制串
static bool CheckRawMsg(char *p_rdt, ssize_t size);
// 处理一个已贴上标签的原始二进制串,获得其包含的信息
static raw_data ProcessSignedRawMsg(char *p_rdt, ssize_t size);
static void ProcessSignedRawMsg(char *p_rdt, ssize_t size, raw_data &rdt);
// 服务器守护线程
friend void *serverDeamon(void *psvr);
// 处理RawData
void ProcessRawData(void);
};
@ -116,6 +127,17 @@ public:
static compute_result Packet2CPUR(packet *tpkt);
};
class SQEServer:public Server{
protected:
// 请求数据包
list<request> req_list;
// 服务器公私钥
public_key_class pkc;
private_key_class prc;
public:
SQEServer(void);
};
//设置服务器守护程序的时钟
void setServerClock(Server *psvr, int clicks);
//服务器守护线程

View File

@ -7,6 +7,7 @@
100% Public Domain
*/
#include "type.h"
#include "stdint.h"
typedef struct
@ -41,4 +42,6 @@ void SHA1(
const char *str,
int len);
void SHA1_Easy(string &hexresult , string &str, size_t len);
#endif /* SHA1_H */

View File

@ -14,6 +14,10 @@
#include "cpart.h"
#include "cmap.h"
#include "cthread.h"
#include "sha1.h"
#include "rsa.h"
extern string PRIME_SOURCE_FILE;
int update(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets);
int construct(string instruct,vector<string> &config, vector<string> &lconfig, vector<string> &target);
@ -119,48 +123,81 @@ int init(string instruct, vector<string> &configs, vector<string> &lconfigs, vec
sqlite3_stmt *psqlsmt;
sqlite3_open("info.db", &psql);
const char *pzTail;
try {
if(targets[0] == "server"){
sql::table_create(psql, "server_info", {
{"name","TEXT"},
{"tag","TEXT"},
{"admin_key_sha1","TEXT"},
{"msqes_ip","TEXT"},
{"msqes_port","INT"},
{"msqes_key","TEXT"},
{"msqes_rsa_public","TEXT"},
{"sqes_public","NONE"},
{"sqes_private","NONE"},
{"key_sha1","TEXT"}
});
sql::table_create(psql, "sqes_info", {
{"sqes_ip","TEXT PRIMARY KEY"},
{"sqes_port","INT"},
{"sqes_key","TEXT"},
{"rsa_public","TEXT"},
sql::insert_info(psql, &psqlsmt, "server_info", {
{"sqes_public","?1"},
{"sqes_private","?2"},
{"key_sha1","?3"},
});
} catch (const char *error_info) {
if(!strcmp(error_info, "fail to create table")){
if(!config_search(configs, "-f")){
printf("\033[33mWarning: Have Already run init process.Try configure -f to continue.\n\033[0m");
return 0;
}
else{
string sql_quote = "DELETE FROM server_info;";
sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail);
int rtn = sqlite3_step(psqlsmt);
if(rtn == SQLITE_DONE){
struct public_key_class npbkc;
struct private_key_class nprkc;
rsa_gen_keys(&npbkc, &nprkc, PRIME_SOURCE_FILE);
sqlite3_bind_blob(psqlsmt, 1, &npbkc, sizeof(public_key_class), SQLITE_TRANSIENT);
sqlite3_bind_blob(psqlsmt, 2, &nprkc, sizeof(private_key_class), SQLITE_TRANSIENT);
if(targets[1].size() < 6) error::printWarning("Key is too weak.");
string sha1_hex;
SHA1_Easy(sha1_hex, targets[1], targets.size());
sqlite3_bind_text(psqlsmt, 3, sha1_hex.data(), -1, SQLITE_TRANSIENT);
if(sqlite3_step(psqlsmt) != SQLITE_DONE){
sql::printError(psql);
}
sqlite3_finalize(psqlsmt);
error::printSuccess("Succeed.");
sqlite3_close(psql);
return 0;
}
else{
try {
sql::table_create(psql, "client_info", {
{"name","TEXT"},
{"tag","TEXT"},
{"admin_key_sha1","TEXT"},
{"msqes_ip","TEXT"},
{"msqes_port","INT"},
{"msqes_key","TEXT"},
{"msqes_rsa_public","TEXT"},
});
sql::table_create(psql, "sqes_info", {
{"sqes_ip","TEXT PRIMARY KEY"},
{"sqes_port","INT"},
{"sqes_key","TEXT"},
{"rsa_public","TEXT"},
});
} catch (const char *error_info) {
if(!strcmp(error_info, "fail to create table")){
if(!config_search(configs, "-f")){
printf("\033[33mWarning: Have Already run init process.Try configure -f to continue.\n\033[0m");
return 0;
}
else{
const char *error = sqlite3_errmsg(psql);
int errorcode = sqlite3_extended_errcode(psql);
printf("\033[31mSQL Error: [%d]%s\n\033[0m",errorcode,error);
throw error;
string sql_quote = "DELETE FROM client_info;";
sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail);
int rtn = sqlite3_step(psqlsmt);
if(rtn == SQLITE_DONE){
}
else{
const char *error = sqlite3_errmsg(psql);
int errorcode = sqlite3_extended_errcode(psql);
printf("\033[31mSQL Error: [%d]%s\n\033[0m",errorcode,error);
throw error;
}
sqlite3_finalize(psqlsmt);
}
sqlite3_finalize(psqlsmt);
}
}
}
}
sql::insert_info(psql, &psqlsmt, "server_info", {
sql::insert_info(psql, &psqlsmt, "client_info", {
{"name","?1"},
{"tag","?2"}
});
@ -191,7 +228,7 @@ int set(string instruct, vector<string> &configs, vector<string> &lconfigs, vect
if(sqlite3_open("info.db", &psql) == SQLITE_ERROR){
sql::printError(psql);
}
string sql_quote = "SELECT count(*) FROM sqlite_master WHERE name = 'server_info';";
string sql_quote = "SELECT count(*) FROM sqlite_master WHERE name = 'client_info';";
sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail);
sqlite3_step(psqlsmt);
int if_find = sqlite3_column_int(psqlsmt, 0);
@ -202,11 +239,11 @@ int set(string instruct, vector<string> &configs, vector<string> &lconfigs, vect
}
sqlite3_finalize(psqlsmt);
if(targets[0] == "square"){
sql_quote = "UPDATE server_info SET msqes_ip = ?1, msqes_port = ?2 WHERE rowid = 1;";
sql_quote = "UPDATE client_info SET msqes_ip = ?1, msqes_port = ?2 WHERE rowid = 1;";
sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail);
if(!Addr::checkValidIP(targets[1])){
error::printError("Args(ipaddr) abnomal.");
error::printError("Args(ipaddr) is abnomal.");
sqlite3_finalize(psqlsmt);
sqlite3_close(psql);
return -1;
@ -219,7 +256,7 @@ int set(string instruct, vector<string> &configs, vector<string> &lconfigs, vect
ss>>port;
if(port > 0 && port <= 65535);
else{
error::printError("Args(port) abnomal.");
error::printError("Args(port) is abnomal.");
sqlite3_finalize(psqlsmt);
sqlite3_close(psql);
return -1;
@ -231,8 +268,34 @@ int set(string instruct, vector<string> &configs, vector<string> &lconfigs, vect
}
sqlite3_finalize(psqlsmt);
}
else if (targets[1] == "key"){
else if (targets[0] == "key"){
if(targets[1] == "admin"){
string hexresult;
SHA1_Easy(hexresult, targets[2], targets[2].size());
if(targets[1].size() < 6){
error::printWarning("Key is too weak.");
}
sql_quote = "UPDATE client_info SET admin_key_sha1 = ?1 WHERE rowid = 1;";
sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail);
sqlite3_bind_text(psqlsmt, 1, hexresult.data(), -1, SQLITE_TRANSIENT);
if(sqlite3_step(psqlsmt) != SQLITE_DONE){
sql::printError(psql);
}
sqlite3_finalize(psqlsmt);
}
else if(targets[1] == "square"){
sql_quote = "UPDATE client_info SET msqes_key = ?1 WHERE rowid = 1;";
sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail);
sqlite3_bind_text(psqlsmt, 1, targets[2].data(), -1, SQLITE_TRANSIENT);
if(sqlite3_step(psqlsmt) != SQLITE_DONE){
sql::printError(psql);
}
sqlite3_finalize(psqlsmt);
}
else{
error::printError("Args(type) is abnormal.");
return -1;
}
}
error::printSuccess("Succeed.");
sqlite3_close(psql);
@ -240,6 +303,10 @@ int set(string instruct, vector<string> &configs, vector<string> &lconfigs, vect
}
int server(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets){
sqlite3 *psql;
sqlite3_stmt *psqlsmt;
const char *pzTail;
initClock();
setThreadsClock();
Server nsvr;

View File

@ -1,3 +1,5 @@
#include "type.h"
#include "rsa.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@ -5,21 +7,14 @@
#include <time.h>
#include <string.h>
// Change this line to the file you'd like to use as a source of primes.
// The format of the file should be one prime per line.
string PRIME_SOURCE_FILE = "primes.txt";
char buffer[1024];
const int MAX_DIGITS = 50;
int i,j = 0;
struct public_key_class{
long long modulus;
long long exponent;
};
struct private_key_class{
long long modulus;
long long exponent;
};
// This should totally be in the math library.
long long gcd(long long a, long long b)
@ -61,11 +56,11 @@ long long rsa_modExp(long long b, long long e, long long m)
// Calling this function will generate a public and private key and store them in the pointers
// it is given.
void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, char *PRIME_SOURCE_FILE)
void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv, string PRIME_SOURCE_FILE)
{
FILE *primes_list;
if(!(primes_list = fopen(PRIME_SOURCE_FILE, "r"))){
fprintf(stderr, "Problem reading %s\n", PRIME_SOURCE_FILE);
if(!(primes_list = fopen(PRIME_SOURCE_FILE.data(), "r"))){
fprintf(stderr, "Problem reading %s\n", PRIME_SOURCE_FILE.data());
exit(1);
}
@ -147,7 +142,7 @@ void rsa_gen_keys(struct public_key_class *pub, struct private_key_class *priv,
long long *rsa_encrypt(const char *message, const unsigned long message_size,
const struct public_key_class *pub)
{
long long *encrypted = malloc(sizeof(long long)*message_size);
long long *encrypted = (long long *) malloc(sizeof(long long)*message_size);
if(encrypted == NULL){
fprintf(stderr,
"Error: Heap allocation failed.\n");
@ -172,8 +167,8 @@ char *rsa_decrypt(const long long *message,
}
// We allocate space to do the decryption (temp) and space for the output as a char array
// (decrypted)
char *decrypted = malloc(message_size/sizeof(long long));
char *temp = malloc(message_size);
char *decrypted = (char *) malloc(message_size/sizeof(long long));
char *temp = (char *) malloc(message_size);
if((decrypted == NULL) || (temp == NULL)){
fprintf(stderr,
"Error: Heap allocation failed.\n");

View File

@ -24,6 +24,7 @@ void setServerClock(Server *psvr, int clicks){
Server::Server(int port, string send_ip,int send_port):socket(port),send_socket(send_ip,send_port){
socket.UDPSetFCNTL();
}
void Server::SetSendPort(int port){
@ -82,11 +83,10 @@ packet CNodeServer::CPURS2Packet(compute_result tcpur){
return rawpkt;
}
raw_data Server::Packet2Rawdata(packet tpkt){
raw_data rdta;
void Server::Packet2Rawdata(packet &tpkt, raw_data &rdt){
char *data = (char *)malloc(BUFSIZ);
memset(data, 0, BUFSIZ);
rdta.data = data;
rdt.data = data;
char *idx = data;
string fdata;
// 写入包ID信息
@ -100,27 +100,24 @@ raw_data Server::Packet2Rawdata(packet tpkt){
memcpy(idx, (*i).second, (*i).first);
idx += (*i).first;
}
rdta.size = idx - data;
return rdta;
rdt.size = idx - data;
}
packet Server::Rawdata2Packet(raw_data trdta){
packet pkt;
char *idx = trdta.data;
Server::Rawdata2Packet(packet &tpkt, raw_data &trdt){
char *idx = trdt.data;
// 数据包ID
uint32_t uint;
memcpy(&pkt.type, idx, sizeof(uint32_t));
memcpy(&tpkt.type, idx, sizeof(uint32_t));
idx += sizeof(uint32_t);
// 数据包主体
while(idx - trdta.data < trdta.size){
while(idx - trdt.data < trdt.size){
memcpy(&uint, idx, sizeof(uint32_t));
idx += sizeof(uint32_t);
void *data = malloc(uint);
memcpy(data, idx, uint);
idx += uint;
pkt.buffs.push_back({uint,data});
tpkt.buffs.push_back({uint,data});
}
return pkt;
}
compute_result CNodeServer::Packet2CPUR(packet *tpkt){
@ -184,6 +181,7 @@ void Server::SignedRawdata(struct raw_data *trdt,string info){
idx += trdt->size;
memcpy(idx, &trdt->tail, sizeof(uint32_t));
trdt->msg = msg;
}
int Server::SentRawdata(struct raw_data *trdt){
@ -204,17 +202,14 @@ bool Server::CheckRawMsg(char *p_rdt, ssize_t size){
else return false;
}
raw_data Server::ProcessSignedRawMsg(char *p_rdt, ssize_t size){
raw_data trdt;
trdt.data = (char *)malloc(size-3*sizeof(uint32_t));
memcpy(&trdt.info, p_rdt+sizeof(uint32_t), sizeof(uint32_t));
memcpy(trdt.data, p_rdt+sizeof(uint32_t)*2, size-3*sizeof(uint32_t));
trdt.size = size-3*sizeof(uint32_t);
return trdt;
void ProcessSignedRawMsg(char *p_rdt, ssize_t size, raw_data &rdt){
rdt.data = (char *)malloc(size-3*sizeof(uint32_t));
memcpy(&rdt.info, p_rdt+sizeof(uint32_t), sizeof(uint32_t));
memcpy(rdt.data, p_rdt+sizeof(uint32_t)*2, size-3*sizeof(uint32_t));
rdt.size = size-3*sizeof(uint32_t);
}
void *serverDeamon(void *pvcti){
clock_thread_info *pcti = (clock_thread_info *) pvcti;
Server *psvr = (Server *) pcti->args;
//cout<<"Server Deamon Checked."<<endl;
@ -230,8 +225,9 @@ void *serverDeamon(void *pvcti){
// 记录有效数据包
if(Server::CheckRawMsg(str, tlen)){
printf("Get\n");
raw_data trdt = Server::ProcessSignedRawMsg(str, tlen);
psvr->rawdata_in.push_back(trdt);
raw_data *ptrdt = new raw_data();
Server::ProcessSignedRawMsg(str, tlen, *ptrdt);
psvr->rawdata_in.push_back(ptrdt);
}
}
free(str);
@ -239,3 +235,41 @@ void *serverDeamon(void *pvcti){
clockThreadFinish(pcti->tid);
pthread_exit(NULL);
}
void Server::ProcessRawData(void){
for(auto prdt : rawdata_in){
if(memcmp(prdt->info, "SPKT", sizeof(uint32_t))){
packet *pnpkt = new packet();
Rawdata2Packet(pnpkt,prdt);
packets_in.push_back(pnpkt);
delete prdt;
}
else{
delete prdt;
}
}
rawdata_in.clear();
}
SQEServer::SQEServer(void){
if(sqlite3_open("info.db", &psql) == SQLITE_ERROR){
sql::printError(psql);
throw "database is abnormal";
}
sqlite3_stmt psqlsmt;
const char *pzTail;
// 从数据库获得服务器的公私钥
string sql_quote = "select sqes_public,sqes_private from server_info where rowid = 1;";
sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, pzTail);
if(sqlite3_step(psqlsmt) != SQLITE_ROW){
sql::printError(psql);
throw "database is abnormal";
}
Byte *tbyt = sqlite3_column_blob(psqlsmt, 0);
memcpy(&pkc, tbyt, sizeof(public_key_class));
tbyt = sqlite3_column_blob(psqlsmt, 1);
memcpy(&prc, tbyt, sizeof(private_key_class));
sqlite3_finalize(psqlsmt);
}

View File

@ -294,3 +294,14 @@ void SHA1(
hash_out[20] = '\0';
}
void SHA1_Easy(string &hexresult , string &str, size_t len){
char thexresult[41];
char result[20];
SHA1( result, str.data(), (int)str.size());
/*format the hash for comparison */
for(int offset = 0; offset < 20; offset++) {
sprintf( ( thexresult + (2*offset)), "%02x", result[offset]&0xff);
}
hexresult = thexresult;
}