-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms18dxprotocol.h
executable file
·246 lines (204 loc) · 6.16 KB
/
sms18dxprotocol.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
#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"
#ifdef CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif
#include "smstcpstream.h"
namespace SMS {
#include "18dx.h"
class CSMS18DXProtocol: public CSMSProtocol{
CSMSLogger* m_pSMSLogger;
TCPSocket *m_pServiceSocket;
CSMSTcpStream m_tcp;
int isMsgValid(POAKSREQTRANSFERMOINFO pHead,char* buf, int len, SMSMessage** msg, unsigned int * msgLen){
*msgLen=sizeof(SMSMessage)+len;
*msg=(SMSMessage*) new char[*msgLen];
memset(*msg,0,*msgLen);
(*msg)->length=*msgLen;
strncpy((*msg)->SenderNumber , pHead->szMobileNo , MOBILENUMBERLENGTH);
(*msg)->SenderNumber[MOBILENUMBERLENGTH]=0;
strncpy((*msg)->TargetNumber , (pHead->szSPCode)+4 , MOBILENUMBERLENGTH);
(*msg)->TargetNumber[MOBILENUMBERLENGTH];
(*msg)->FeeTargetNumber[0]=0;
(*msg)->SMSBodyLength=len;
memcpy((*msg)->SMSBody, buf, len);
(*msg)->sendTime=time(NULL);
(*msg)->arriveTime=time(NULL);
strncpy((*msg)->parentID,"18dx",SMS_PARENTID_LEN);
(*msg)->parentID[SMS_PARENTID_LEN]=0;
(*msg)->FeeType=0;
return 0;
}
int OnAccept(CSMSTcpStream* pStream,CSMSStorage* pSMSStorage){
OAKSREQTRANSFERMOINFO head;
char buf[1000];
int errCount=0;
int i,size;
int len=0;
size=sizeof(OAKSREQTRANSFERMOINFO);
i=pStream->read(&head,size);
if (i<size) {
syslog(LOG_ERR, "read msg head error %d ", i);
return -1;
}
i=pStream->read(buf,head.nLenMsg);
if (i<head.nLenMsg) {
syslog(LOG_ERR, "read msg head body error");
return -1;
}
PSMSMessage formatedMsg;
unsigned int msgLen;
syslog(LOG_ERR,"start convert 18dx sms..");
if (!isMsgValid(&head,buf,head.nLenMsg, &formatedMsg,&msgLen)){
pSMSStorage->writeSMStoStorage(formatedMsg->SenderNumber,formatedMsg->TargetNumber,(char *)formatedMsg,msgLen);
delete formatedMsg;
} else {
return -1;
}
return 0;
}
public:
CSMS18DXProtocol() {
m_pSMSLogger=NULL;
m_pServiceSocket=NULL;
m_pSMSLogger=NULL;
}
/* {{{ Run(CSMSStorage* pSMSStorage) */
int Run(CSMSStorage* pSMSStorage){
m_pSMSLogger=new CSMSLogger;
pSMSStorage->init();
// pSMSStorage->OnNotify();
InetAddress addr;
CSMSTcpStream tcp;
try {
m_pServiceSocket=new TCPSocket(addr,atoi(testport));
while(m_pServiceSocket->isPendingConnection()){
tcp.open(*m_pServiceSocket);
if (!tcp) {
continue;
}
switch(fork()){
case 0:
delete m_pServiceSocket;
OnAccept(&tcp,pSMSStorage);
tcp.close();
exit(0);
break;
case -1:
syslog(LOG_ERR,"fork error");
exit(-1);
break;
default:
tcp.close();
}
}
} catch (Socket *socket)
{
tpport_t port;
InetAddress saddr = (InetAddress)socket->getPeer(&port);
syslog(LOG_ERR,"socket error %s : %d", saddr.getHostname(), port);
if(socket->getErrorNumber() == Socket::errResourceFailure)
{
syslog(LOG_ERR, "bind failed; no resources" );
}
if(socket->getErrorNumber() == Socket::errBindingFailed)
{
syslog(LOG_ERR, "bind failed; port busy" );
}
}
return 0;
}
/* }}} */
/* {{{ Send(SMSMessage* msg) */
int Send(SMSMessage* msg){
char addr[100];
int retCode;
snprintf(addr,sizeof(addr),"%s:%s",host_18dx,port_18dx);
syslog(LOG_ERR,"send message to %s",addr);
if (!m_tcp) {
m_tcp.open(addr);
}
if (!m_tcp){
syslog(LOG_ERR,"can't connect to %s" ,addr);
return FAILED;
}
sigset_t sigmask, oldmask;
sigemptyset(&sigmask);
sigaddset(&sigmask,SIGUSR1);
sigprocmask(SIG_BLOCK,&sigmask,&oldmask);
int lenPack= sizeof(OAKSREQSMZIXIASENDTEXT)+msg->SMSBodyLength;
char* buffer=new char[lenPack];
POAKSREQSMZIXIASENDTEXT ps=(POAKSREQSMZIXIASENDTEXT)buffer;
memset(ps,0,lenPack);
ps->header.dwType= (OAKSID_SM_ZIXIASENDTEXT | OAKSID_REQ);
ps->header.dwLength=(lenPack-sizeof(OAKSREQHEADER));
ps->nSerialID=(1213242);
strcpy(ps->szSrcMobileNo,msg->FeeTargetNumber);
strcpy(ps->szDstMobileNo,msg->TargetNumber);
ps->nFeeID=msg->FeeType;
strncpy(ps->szMobileID, msg->SenderNumber, MOBILE_ID_LEN);
ps->szMobileID[MOBILE_ID_LEN]=0;
syslog(LOG_ERR, "send no %s to 18dx ",ps->szMobileID);
ps->lenText=msg->SMSBodyLength;
memcpy(ps+1,msg->SMSBody,msg->SMSBodyLength);
retCode=m_tcp.write(ps,lenPack);
char* buf=new char[sizeof(OAKSACKSMZIXIASENDTEXT)];
if (retCode>0) {
retCode = m_tcp.read(buf,sizeof(OAKSACKSMZIXIASENDTEXT),6000);
}
if ( TIMEOUT==retCode ){
m_tcp.close();
syslog(LOG_ERR,"send msg timeout, faint" );
return FAILED;
}
if (retCode<=0) {
m_tcp.close();
syslog(LOG_ERR,"send msg - write failed: %d", retCode);
return FAILED;
}
syslog(LOG_ERR,"send msg return %d",(POAKSACKSMZIXIASENDTEXT(buf))->header.dwResult);
DWORD result=(POAKSACKSMZIXIASENDTEXT(buf))->header.dwResult;
delete[] buf;
delete[] buffer;
if (result!=OAKSBIT_SUCCESS) {
switch (result) {
case OAKSERR_SM_INVALIDID:
case OAKSERR_SM_INVALIDSENDNO:
case OAKSERR_SM_INVALIDRECVNO:
case OAKSERR_SM_MSGTOOLENGTH:
case OAKSERR_SM_INVALIDMOBILENO:
case OAKSERR_SM_INVALIDSEND:
case OAKSERR_SM_INVALIDMSGID:
return ERROR;
default:
return FAILED;
}
}else {
m_pSMSLogger->logIt(msg->SenderNumber, msg->TargetNumber,msg->FeeTargetNumber,msg->FeeType,msg->childCode,"58181888" ,msg->sendTime,time(NULL),msg->arriveTime,msg->SMSBody,msg->SMSBodyLength, msg->FeeMoney);
}
sigprocmask(SIG_SETMASK, &oldmask, NULL);
return SUCCESS;
}
/* }}} */
~CSMS18DXProtocol() {
delete m_pSMSLogger;
}
};
}
#endif