服务端类实现

ServerOperation.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include "TcpSocket.h"
#include "SecKeyShm.h"
#include "TcpServer.h"
#include "RequestCodec.h"
#include "OCCIOP.h"
#include <map>

class ServerInfo
{
public:
char serverID[12]; // 服务器端编号
char dbUse[24]; // 数据库用户名
char dbPasswd[24]; // 数据库密码
char dbSID[24]; // 数据库sid

unsigned short sPort; // 服务器绑定的端口
int maxnode; // 共享内存最大网点树 客户端默认1个
int shmkey; // 共享内存keyid 创建共享内存时使用
};

class ServerOperation
{
public:
ServerOperation(ServerInfo *info);
~ServerOperation();

// 服务器开始工作
void startWork();
// 秘钥协商
int secKeyAgree(RequestMsg* reqmsg, char** outData, int& outLen);
// 秘钥校验
int secKeyCheck();
// 秘钥注销
int secKeyRevoke();
// 秘钥查看
int secKeyView();

friend void* working(void * arg);
// 线程回调也可使用静态成员函数
static void* wrokingHard(void* arg);
static void catchSignal(int num);

private:
void getRandString(int len, char* randBuf);

private:
ServerInfo m_info;
SecKeyShm* m_shm;
TcpServer m_server;
TcpSocket* m_client;
OCCIOP m_occi;
std::map<pthread_t, TcpSocket*> m_listSocket;
static bool m_stop;
};

void* working(void * arg);

ServerOperation.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "ServerOperation.h"
#include <iostream>
#include <pthread.h>
#include <string.h>
#include "RequestFactory.h"
#include "RespondFactory.h"
#include <openssl/hmac.h>
#include <openssl/sha.h>
#include <signal.h>
using namespace std;

bool ServerOperation::m_stop = false; // 静态变量初始化

ServerOperation::ServerOperation(ServerInfo * info)
{
memcpy(&m_info, info, sizeof(ServerInfo));
m_shm = new SecKeyShm(m_info.shmkey, m_info.maxnode);
}

ServerOperation::~ServerOperation()
{

}

void ServerOperation::startWork()
{
//socket-setsockopt-bind-listen
m_server.setListen(m_info.sPort);
pthread_t threadID;
while (1)
{
//accept新的客户端连接
m_client = m_server.acceptConn();

//创建一个子线程
pthread_create(&threadID, NULL, working, this);
//设置子线程为分离属性
pthread_detach(threadID);
m_listSocket.insert(make_pair(threadID, m_client));
}
}

int ServerOperation::secKeyAgree(RequestMsg * reqMsg, char ** outData, int & outLen)
{
//验证消息认证码
char key[64];
unsigned int len;
unsigned char md[SHA256_DIGEST_LENGTH];
char authCode[SHA256_DIGEST_LENGTH * 2 + 1] = {0};

memset(key, 0x00, sizeof(key));
sprintf(key, "@%s+%s@", reqMsg->serverId, reqMsg->clientId);
HMAC(EVP_sha256(), key, strlen(key), (unsigned char *)reqMsg->r1, strlen(reqMsg->r1), md, &len);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(&authCode[2 * i], "%02x", md[i]);
}
//将生成的消息认证码和客户端的r1的消息认证做比对
if (strcmp(authCode, reqMsg->authCode) != 0)
{
cout << "消息认证码错误" << endl;
return -1;
}

//生成随机字符串r2
RespondMsg rspMsg;
memset(&rspMsg, 0x00, sizeof(rspMsg));
getRandString(sizeof(rspMsg.r2), rspMsg.r2);

//将随机字符串r2和r1进行拼接, 然后生成秘钥
char buf[64];
char md[SHA_DIGEST_LENGTH];
char seckey[SHA_DIGEST_LENGTH * 2 + 1];
memset(buf, 0x00, sizeof(buf));
memset(seckey, 0x00, sizeof(seckey));
sprintf(buf, "%s%s", reqMsg->r1, rspMsg.r2);
SHA1((unsigned char *)buf, strlen((char *)buf), md);
for (int i = 0; i<SHA_DIGEST_LENGTH; i++)
{
sprintf(&seckey[i * 2], "%02x", md[i]);
}

//给应答结构体赋值
rspMsg.seckeyid = 1; //获取秘钥ID
rspMsg.rv = 0;
strcpy(rspMsg.serverId, m_info.serverID);
strcpy(rspMsg.clientId, reqMsg->clientId);

//将要发送给客户端的应答结构体进行编码
int dataLen;
char *sendData = NULL;
CodecFactory *factory = new RespondFactory(&rspMsg);
Codec *pCodec = factory->createCodec();
pCodec->msgEncode(&sendData, dataLen);
delete factory;
delete pCodec;

//发送数据给客户端
pthread_t thread = pthread_self();
TcpSocket* socket = m_listSocket[thread];
socket->sendMsg(sendData, dataLen);
free(sendData);

//写秘钥信息到共享内存
NodeSHMInfo node;
memset(&node, 0x00, sizeof(NodeSHMInfo));
node.status = 0;
strcpy(node.seckey, seckey);
strcpy(node.clientID, rspMsg.clientId);
strcpy(node.serverID, m_info.serverID);
node.seckeyID = rspMsg.seckeyid;

//将秘钥信息写入共享内存
m_shm->shmWrite(&node);

//关闭连接
socket->disConnect();

return 0;
}



void ServerOperation::getRandString(int len, char * randBuf)
{
int flag = -1;
// 设置随机种子
srand(time(NULL));
// 随机字符串: A-Z, a-z, 0-9, 特殊字符(!@#$%^&*()_+=)
char chars[] = "!@#$%^&*()_+=";
for (int i = 0; i < len - 1; ++i)
{
flag = rand() % 4;
switch (flag)
{
case 0:
randBuf[i] = 'Z' - rand() % 26;
break;
case 1:
randBuf[i] = 'z' - rand() % 26;
break;
case 3:
randBuf[i] = rand() % 10 + '0';
break;
case 2:
randBuf[i] = chars[rand() % strlen(chars)];
break;
default:
break;
}
}
randBuf[len - 1] = '\0';
}

// 友元函数, 可以在该友元函数中通过对应的类对象调用期私有成员函数或者私有变量
// 子线程 - 进行业务流程处理
void * working(void * arg)
{
//接收数据
pthread_t thread = pthread_self();
ServerOperation *op = (ServerOperation *)arg;
TcpSocket* socket = op->m_listSocket[thread];

char *inData;
int dataLen;
socket->recvMsg(&inData, dataLen);

//解码
CodecFactory *factory = new RequestFactory();
Codec *pCodec = factory->createCodec();
RequestMsg *pMsg = (RequestMsg *)pCodec->msgDecode(inData, dataLen);
delete factory;
//delete pCodec;

//判断clientID是否合法

//判断客户端要请求什么服务
char *outData;

switch(pMsg->cmdType)
{
case RequestCodec::NewOrUpdate:
op->secKeyAgree(pMsg, &outData, dataLen);
break;

case RequestCodec::Check:
op->secKeyCheck();
break;
case RequestCodec::Revoke:
op->secKeyRevoke();
break;
case RequestCodec::View:
op->secKeyView();
break;

default:
break;
}
}