-
Notifications
You must be signed in to change notification settings - Fork 0
/
smscmpp2protocol.h
369 lines (325 loc) · 9.89 KB
/
smscmpp2protocol.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifndef SMS_51AD2DAD_B486_4391_A641_84C4719FF7DE
#define SMS_51AD2DAD_B486_4391_A641_84C4719FF7DE
#include <cc++/socket.h>
#include <sqlplus.hh>
#include <asm/errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sstream>
#include "sms.h"
#include "smslogger.h"
#include "time.h"
#ifdef CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif
#include "smstcpstream.h"
//#define GWIP "210.51.0.210"
#define WAITTIME 60 //100秒
//C、T、N为通路检查参数,具体含义可参考CMPP2 协议6.1
#define LINK_C 180 //3分钟
#define LINK_T 60 //60秒
#define LINK_N 3 //3次
namespace SMS {
#include "cmpp2.h"
#include "md5.h"
class CSMSCMPP2Protocol: public CSMSProtocol{
CSMSLogger* m_pSMSLogger;
int m_connected;
int m_link_test;
time_t m_lasttesttime;
time_t m_lastsendtime;
UINT32 m_serial;
CSMSTcpStream m_tcp;
UINT32 getSerial(){
return m_serial++;
}
int doCMDLogin(const OCTETSTRING* gatewayIP, const OCTETSTRING* spCode, const OCTETSTRING * loginSecret, UINT8 version, time_t timestamp) {
char addr[100];
snprintf(addr,sizeof(addr),"%s:%d",gatewayIP,CMPP_LONGCONNECTION_PORT);
syslog(LOG_ERR,"try to connect to %s",addr);
m_tcp.open(addr);
if (!m_tcp){
syslog(LOG_ERR,"can't connect to %s" ,addr);
return FAILED;
}
int len;
char buf;
PCMPP_HEADER head;
PCMPP_CONNECT packet;
len=sizeof(CMPP_HEADER+CMPP_CONNECT);
buf=(char*)malloc(len);
if (buf==NULL) {
return NOENOUGHMEMORY;
}
memset(buf,0,len);
head=(PCMPP_HEADER)head;
packet=(PCMPP_CONNECT)(buf+sizeof(head));
head->Total_Length=len;
head->Command_Id=CMPP_CMD_CONNECT;
head->Sequence_Id=getSerial();
strncpy(packet->Source_Addr,spCode,6);
char timeStr[20];
struct tm t;
gmtime_r(×tamp,&t);
snprintf(timeStr,"%02d%02d%02d%02d%02d",t.tm_mon+1,tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec);
struct MD5Context md5;
int i;
MD5Init(&md5);
MD5Update(&md5, spCode,strlen(spCode));
for (i=0;i<9+6-strlen(spCode;i++)
MD5Update(&md5,"\0",1);
MD5Update(&md5,loginSecret,strlen(loginSecret));
MD5Update(&md5,timeStr,10);
MD5Final(packet->AuthenticatorSource,&md5);
packet->Version=version;
packet->Timestamp=atoi(timeStr);
int ret=m_tcp.write(buf,len);
free(buf);
if (ret<len) {
syslog(LOG_ERR, "send login packet error: %d ", errno);
return ERROR;
}
return SUCCESS;
}
int doCloseConnect() {
syslog(LOG_ERR,"close connection!");
m_connected=0;
m_tcp.close();
return SUCCESS;
}
int receiveMsg(char* buf, int bufLen, int waitTime) {
int len=sizeof(PCMPP_HEADER);
int rcv=m_tcp.read(buf,Len);
if (rcv<0) {
syslog(LOG_ERR, "receive msg error!: %d",errno);
return rcv;
}
return SUCCESS;
}
int waitForMsgHeader(PCMPP_HEADER pHead, int waitTime ) {
syslog(LOG_ERR," waiting for msg head...");
return receiveMsg(pHead, sizeof(CMPP_HEADER) , waitTime);
}
int dispatchMsg(PCMPP_HEADER pHead, int waitTime ) {
char * buf;
int msgLen=pHead->Total_Length-sizeof(CMMP_HEADER);
syslog(LOG_ERR,"recieving & dispatching msg...");
syslog(LOG_ERR,"msg sn: %d type :0x%X length: %d",pHead->Sequence_Id,pHead->Command_Id, msgLen);
//检查msg类型和长度是否合法
switch (pHead->Command_id) {
case CMPP_CMD_CONNECT_RESP :
if (msgLen!=sizeof(CMPP_CONNECT_RESP)) {
syslog(LOG_ERR," login reply packet length error!");
doSendCommand(getSerial, CMPP_CMD_TERMINATE, NULL,0);
return ERROR;
}
case CMPP_CMD_TERMINATE :
doSendCommand(getSerial, CMPP_CMD_TERMINATE_RESP, NULL,0);
case CMPP_CMD_TERMINATE_RESP :
syslog(LOG_ERR,"recieved terminate msg from gatway!");
return CONNECTIONLOST;
case CMPP_CMD_SUBMIT_RESP:
if (msgLen!=sizeof(CMPP_SUBMIT_RESP)) {
syslog(LOG_ERR," login reply packet length error!");
doSendCommand(getSerial, CMPP_CMD_TERMINATE, NULL,0);
return ERROR;
}
case CMPP_CMD_DELIVER:
break;
case CMPP_CMD_CANCEL_RESP:
if (msgLen!=sizeof(CMPP_CANCEL_RESP)) {
syslog(LOG_ERR," login reply packet length error!");
doSendCommand(getSerial, CMPP_CMD_TERMINATE, NULL,0);
return ERROR;
}
case CMPP_CMD_ACTIVE_TEST:
CMPP_ACTIVE_TEST_RESP packet;
doSendCommand(,pHead->Sequence_Id, CMPP_CMD_ACTIVE_TEST_RESP,&packet,sizeof(CMPP_ACTIVE_TEST_RESP));
return SUCCESS;
case CMPP_CMD_ACTIVE_TEST_RESP:
if (msgLen!=sizeof(CMPP_ACTIVE_TEST_RESP)) {
syslog(LOG_ERR," login reply packet length error!");
doSendCommand(getSerial, CMPP_CMD_TERMINATE, NULL,0);
return ERROR;
}
break;
default:
syslog(LOG_ERR,"recieved err command! we have to terminate connectiong!");
doSendCommand(getSerial, CMPP_CMD_TERMINATE, NULL,0);
return CONNECTIONLOST;
}
buf=(char*)malloc(msgLen);
if (buf==NULL) {
syslog(LOG_ERR,"no NOMERY for msg buffer!");
return NOENOUGHMEMORY;
}
if (receiveMsg(buf,msgLen)!=SUCCESS) {
free(buf);
return ERROR;
}
switch (pHead->Command_id) {
case CMPP_CMD_CONNECT_RESP:
PCMPP_CONNECT_RESP p=(PCMPP_CONNECT_RESP)buf;
if (p->Status==0) {
syslog(LOG_ERR, "login ok!");
m_connected=1;
pSMSStorage->OnNotify();
} else {
syslog(LOG_ERR,"login failed! retCode: %d Version: %0x%X",p->Status,p->Version);
}
break;
case CMPP_CMD_SUBMIT_RESP:
PCMPP_SUBMIT_RESP:
case CMPP_CMD_DELIVER:
case CMPP_CMD_CANCEL_RESP:
case CMPP_CMD_ACTIVE_TEST_RESP:
m_link_test--;
if (m_link_test<0) {
m_link_test=0;
}
break;
}
free(buf);
return SUCCESS;
}
int convertMsgFormat(struct CDeliver* pSMS, SMSMessage** msg, unsigned int * msgLen){
*msgLen=sizeof(SMSMessage)+strlen(pSMS->msg);
*msg=(SMSMessage*) new char[*msgLen];
memset(*msg,0,*msgLen);
(*msg)->length=*msgLen;
strncpy((*msg)->SenderNumber , pSMS->mobile , MOBILENUMBERLENGTH);
(*msg)->SenderNumber[MOBILENUMBERLENGTH]=0;
strncpy((*msg)->TargetNumber , pSMS->dst_num , MOBILENUMBERLENGTH);
(*msg)->TargetNumber[MOBILENUMBERLENGTH];
(*msg)->FeeTargetNumber[0]=0;
(*msg)->SMSBodyLength=strlen(pSMS->msg);
memcpy((*msg)->SMSBody, pSMS->msg, strlen(pSMS->msg));
(*msg)->arriveTime=time(NULL);
strncpy((*msg)->parentID,"5168",SMS_MAXCHILDCODE_LEN);
(*msg)->parentID[SMS_PARENTID_LEN]=0;
(*msg)->FeeType=0;
return 0;
}
public:
CSMSCMPP2Protocol() {
m_pSMSLogger=NULL;
m_connected=0;
m_serial=0;
m_link_test=0;
}
/* {{{ Run(CSMSStorage* pSMSStorage) */
int Run(CSMSStorage* pSMSStorage){
int retCode;
CMPP_HEADER header;
time_t now;
m_pSMSLogger=new CSMSLogger;
pSMSStorage->init();
for(;;) {
m_connected=0;
for(;;) {
time(&now);
if ((retCode=doCMDLogin(CMPP_GWIP, CMPP_SPCODE , CMPPVERSION(2,0) , now))!=SUCCESS) {
syslog(LOG_ERR,"doCMDLogin error: %d",retCode);
sleep(10);
} else {
time(&m_lastsendtime);
break;
}
}
m_link_test=0;
for(;;) {
retCode=waitForMsgHeader(&header,WAITTIME);
syslog(LOG_ERR,"waitForMsg return: %d",retCode);
if (retCode==SUCCESS) {
if ((retCode=dispatchMsg(header,WAITIME))==SUCCESS)
continue;
}
if (retCode!=TIMEOUT) {
break;
}
time(&now);
if (m_connected) {
if ( ( (now-m_lastsendtime)>LINK_C ) || ( m_link_test && (now-m_lasttesttime>LINK_T) ) ){
syslog(LOG_ERR,"try to keep connection!");
now(&m_lasttesttime);
now(&m_lastsendtime);
m_link_test++;
if (m_lint_test>LINK_N) {
break;
}
if (retCode=doCMDActiveTest()!=SUCCESS) {
syslog(LOG_ERR,"apiActive failed!: %d",retCode);
break;
}
}
} else {
syslog(LOG_ERR, "no reply for login request!");
break;
}
}
doCloseConnection();
syslog(LOG_ERR," connection lost!");
}
return 0;
}
/* }}} */
/* {{{ Send(SMSMessage* msg) */
int Send(SMSMessage* msg){
if (m_connected==0) {
return FAILED;
}
sigset_t sigmask, oldmask;
sigemptyset(&sigmask);
sigaddset(&sigmask,SIGUSR1);
sigprocmask(SIG_BLOCK,&sigmask,&oldmask);
int retCode;
char buf[160];
int len;
/*
apiSend( DWORD msg_id1,DWORD msg_id2, char mobile[21],char service_id[10],char src_term[21], char fee_term[21], char msg[160],char udhi,BYTE pid, BYTE isReply, WORD msg_len, BYTE msg_fmt);
参数说明:
msg_id1: 用户信息id号
msg_id2: 保留参数=0
mobile[21]: 接收号码
service_id: 服务代码. 例如-lsxz
src_term[21]: 发送源号码(在接收手机端显示的发送者号码)比如 '51687001'
fee_term[21]: 计费号码(为手机号码,即从哪个手机上收费)
msg[160]: 发送信息
udhi: 头标示(数据为二进制时可能有意义,文本信息填0)
pid: 协议ID(数据为二进制时可能有意义,文本信息填0)
isReply: 是否需要状态报告,目前API内置1,表明需要状态报告。
msg_len: 发送消息长度
msg_fmt: 信息类型 (0:ASCII串 3:短信写卡操作 4:二进制
8:UCS2编码15:含GB汉字)
*/
len=msg->SMSBodyLength;
if (len>159)
len=159;
memcpy(buf,msg->SMSBody,len);
buf[len]=0;
retCode= apiSend(getSerial(), 0,msg->TargetNumber,msg->serviceCode,msg->SenderNumber,msg->FeeTargetNumber, buf,0,0,1,len,15);
syslog(LOG_ERR,"send msg to 5618....");
if (retCode==0) {
m_pSMSLogger->logIt(msg->SenderNumber, msg->TargetNumber,msg->FeeTargetNumber,msg->FeeType,msg->childCode,"58181888" ,msg->sendTime,time(NULL),msg->arriveTime,msg->SMSBody,msg->SMSBodyLength);
} else {
sigprocmask(SIG_SETMASK, &oldmask, NULL);
return FAILED;
}
sigprocmask(SIG_SETMASK, &oldmask, NULL);
return SUCCESS;
}
/* }}} */
~CSMSCMPP2Protocol() {
delete m_pSMSLogger;
}
};
}
#endif