-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmsbbschildprotocol.h
1172 lines (1048 loc) · 32.7 KB
/
smsbbschildprotocol.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* smsbbschildprotocol
*
*
*
*/
#ifndef SMS_51AD2DAD_B486_4391_A641_84C4719FF7DE
#define SMS_51AD2DAD_B486_4391_A641_84C4719FF7DE
#include <cc++/socket.h>
#include <sqlplus.hh>
#include "sms.h"
#include "smsbbschildprivilegechecker.h"
#include "smsmysqlfeecodegetter.h"
#include <asm/errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sstream>
#include <vector>
#include <algorithm>
#ifdef CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif
#include "smsbbsprotocoldefine.h"
#include "smslogger.h"
#include "smstcpstream.h"
#include "smsdiskstorage.h"
namespace SMS {
#define SEND_NO_CHECK 1
#define SEND_CHECK 0
struct bindRequest{
DWORD sn;
std::string userID;
std::string mobileNumber;
byte isBind;
};
const char CODES[]="0123456789";
/* {{{ class CBBSChildProtocolTCPSocket
* 程序的Server Socket实现
*/
class CBBSChildProtocolTCPSocket : public TCPSocket
{
CSMSBBSChildPrivilegeChecker* m_privilegeChecker;
protected:
bool onAccept(const InetHostAddress &ia, tpport_t port);
public:
CBBSChildProtocolTCPSocket(InetAddress &ia, tpport_t port, CSMSBBSChildPrivilegeChecker* privilegeChecker);
};
CBBSChildProtocolTCPSocket::CBBSChildProtocolTCPSocket(InetAddress &ia, tpport_t port, CSMSBBSChildPrivilegeChecker* privilegeChecker) :
TCPSocket(ia, port),m_privilegeChecker(privilegeChecker) {};
bool CBBSChildProtocolTCPSocket::onAccept(const InetHostAddress &ia, tpport_t port){
return true;
};
/* class CBBSChildProtocolTCPSocket
* }}} */
class CSMSBBSChildProtocol: public CSMSProtocol{
int m_state;
CSMSTcpStream *m_pStream;
enum { ready,headLenghtUnkown, headIncomplete, bodyIncomplete };
DWORD m_serial;
CSMSBBSChildPrivilegeChecker *m_pChildPrivilegeChecker;
CSMSStorage* m_pSMSStorage;
char m_childCode[SMS_MAXCHILDCODE_LEN+1];
char m_childName[SMS_MAXCHILDNAME_LEN+1];
Connection m_conn;
int m_listenPort;
CSMSLogger m_SMSLogger;
int m_defaultMoneyLimit;
CSMSFeeCodeGetter* m_pSMSFeeCodeGetter;
std::vector<struct bindRequest*> m_requestList;
private:
DWORD getSerial(){ //产生序列号
return m_serial++;
}
/* {{{ doSendMsg()
* 向下游发送消息
*/
int doSendMsg(void* msg, DWORD len){
int rc=0;
if (m_pStream==NULL){
return ERROR;
}
syslog(LOG_ERR," send msg to child ,length=%d",len);
//todo: 中断恢复与处理
rc=m_pStream->write((char*)msg,len);
if ( rc<len) {
syslog(LOG_ERR, "do send msg error: %d" , errno);
return ERROR;
}
return SUCCESS;
}
/* doSendMsg()
* }}} */
/* {{{ doReply()函数
*
* 返回消息处理结果
*/
int doReply(int msgType, const byte SerialNo[4], const byte pid[4]){
char * msg;
DWORD len;
len=sizeof(SMS_BBS_HEADER);
msg=(char *)malloc(len);
memset(msg,0,len);
((PSMS_BBS_HEADER)(msg))->Type=msgType;
memcpy(((PSMS_BBS_HEADER)(msg))->SerialNo, SerialNo,4);
memcpy(((PSMS_BBS_HEADER)(msg))->pid, pid, 4);
sms_longToByte(((PSMS_BBS_HEADER)(msg))->msgLength,0);
int retCode=doSendMsg(msg,len);
free( msg);
return retCode;
}
/* doRely()
*}}} */
/* {{{ generateSMS()
* 生成内部格式SMS
*/
int generateSMS(DWORD userCode, const char* targetNumber, const char* feeTargetNumber, const char* msgContent, DWORD msgLen , byte feeType , SMSMessage** sms, DWORD *smsLen){
*smsLen=sizeof(SMSMessage)+msgLen;
*sms=(PSMSMessage) malloc(*smsLen);
if (*sms==NULL) {
return NOENOUGHMEMORY;
}
memset(*sms,0,*smsLen);
(*sms)->length=*smsLen;
if (userCode)
snprintf((*sms)->SenderNumber , MOBILENUMBERLENGTH , "%s%d" , m_childCode, userCode);
else
snprintf((*sms)->SenderNumber , MOBILENUMBERLENGTH , "%s" , m_childCode);
strncpy((*sms)->TargetNumber , targetNumber , MOBILENUMBERLENGTH);
(*sms)->TargetNumber[MOBILENUMBERLENGTH]=0;
strncpy((*sms)->FeeTargetNumber , feeTargetNumber , MOBILENUMBERLENGTH);
(*sms)->FeeTargetNumber[MOBILENUMBERLENGTH]=0;
(*sms)->SMSBodyLength=msgLen;
memcpy((*sms)->SMSBody, msgContent, msgLen);
(*sms)->sendTime=time(NULL);
strncpy((*sms)->childCode,m_childCode,SMS_MAXCHILDCODE_LEN);
(*sms)->childCode[SMS_MAXCHILDCODE_LEN]=0;
(*sms)->FeeType=feeType;
(*sms)->FeeMoney=10;
return SUCCESS;
}
/* generateSMS()
* }}} */
/* {{{ convertSMS()
* 转化SMS为内部格式
*/
int convertSMS(PSMS_BBS_BBSSENDSMS msg, SMSMessage** sms, DWORD *smsLen){
return generateSMS(sms_byteToLong(msg->UserID),msg->DstMobileNo, msg->SrcMobileNo,msg->MsgTxt,sms_byteToLong(msg->MsgTxtLen),6, sms,smsLen);
}
/* convertSMS()
* }}} */
int countMoney(const char* mobileNumber, const char* usrID, int feeMoney) {
try{
Query query=m_conn.query();
query<< "select * from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNumber<<"' and UPPER(srcID)=UPPER('"<<usrID<<"') and todayMoney+"<<feeMoney<<"<=moneyLimit";
Result res=query.store();
if (res.size()!=0) {
std::stringstream sql;
sql<< "update SMSRegister_TB set todayMoney=todayMoney+"<<feeMoney<<" where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNumber<<"' and UPPER(srcID)=UPPER('"<<usrID<<"') ";
query.exec(sql.str());
return SMS_BBS_CMD_OK;
} else {
syslog(LOG_ERR,"%s %s 's todayMoneyLimit exeeded", usrID, mobileNumber);
return SMS_BBS_CMD_EXCEEDMONEY_LIMIT;
}
} catch ( BadQuery er) {
syslog(LOG_ERR," mysql query err : %s", er.error.c_str());
}
return SMS_BBS_CMD_DB_ERROR;
}
/* {{{ sendSMS()
* 向上游发送短信
*
*/
int sendSMS(PSMSMessage sms,const char* usrID, int bCheck=SEND_CHECK) {
if (bCheck!=SEND_NO_CHECK) {
int feeMoney=0;
int retCode;
if (m_pSMSFeeCodeGetter->getFee(sms->FeeType,&feeMoney)!=SUCCESS){
return SMS_BBS_CMD_DB_ERROR;
}
if (usrID!=NULL) {
if ((retCode=countMoney(sms->FeeTargetNumber,usrID,feeMoney))!=SMS_BBS_CMD_OK) {
return retCode;
}
}
}
if (m_pSMSStorage->writeSMStoStorage(sms->SenderNumber,sms->TargetNumber,(char *)sms,sms->length)==SUCCESS) {
return SMS_BBS_CMD_OK;
} else {
return SMS_BBS_CMD_ERR;
}
}
/* sendSMS()
* }}} */
/* {{{ generateValidateNum
* 生成注册码
*
*/
void generateValidateNum(char* validateNo, int validNumLen){
int codeLen=strlen(CODES);
int t;
srand(time(NULL));
syslog(LOG_ERR," %d %d ", codeLen, validNumLen);
for (int i=0;i<validNumLen;i++){
t=0+(int) (((double)codeLen)*rand()/(RAND_MAX+1.0));
validateNo[i]=CODES[t];
syslog(LOG_ERR,"%c",validateNo[i]);
}
validateNo[validNumLen]=0;
}
/* generateValidateNum()
* ))) */
/* {{{ getValidateNum()
*
* 获取注册码
*/
int getValidateNum(const char* mobileNo, const char* srcID, char* validateNo, int validNumLen){
try {
Query query=m_conn.query();
query<< "select * from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"')";
Result res=query.store();
if (res.size()!=0) {
Row row=*(res.begin());
if (strlen(row["ValidatationNumber"])!=0) {
strncpy(validateNo,row["ValidatationNumber"],validNumLen);
validateNo[validNumLen]=0;
return SUCCESS;
}
}
generateValidateNum(validateNo,validNumLen);
std::stringstream sql;
sql<< "replace into SMSRegister_TB(childCode, MobilePhoneNumber, ValidatationNumber,srcID,moneyLimit) values( '"
<<m_childCode<<"' , '"<<mobileNo<<"' , '" <<validateNo <<"', '"<<srcID<<"',"<<m_defaultMoneyLimit<< " )";
query.exec(sql.str());
return SUCCESS;
} catch ( BadQuery er) {
syslog(LOG_ERR," getValidateNum -- mysql query err : %s", er.error.c_str());
return SMS_BBS_CMD_DB_ERROR;
}
}
/* getValidateNum
* }}} */
/* {{{ doSendRegisterSMS()
* 发送注册短信
*
*/
int doSendRegisterSMS(const char* targetMobileNo, const char* usrID){
char validateNo[SMS_BBS_VALID_LEN+1];
int retCode=getValidateNum(targetMobileNo,usrID, validateNo,SMS_BBS_VALID_LEN);
if (retCode!=SUCCESS) {
return retCode;
}
PSMSMessage sms;
DWORD smsLen;
char msg[101];
snprintf(msg, 100, "您的注册码是:%s", validateNo);
if (generateSMS(0,targetMobileNo, targetMobileNo,msg,strlen(msg),6, &sms,&smsLen)==NOENOUGHMEMORY) {
syslog(LOG_ERR,"Fatal Error: no enough memory for SMS convertion!system exited!");
exit(0);
}
retCode=sendSMS(sms,usrID);
free(sms);
return retCode;
}
/* doSendRegisterSMS()
* }}} */
/* {{{ doRegisterValidation()
* 使用注册码进行手机绑定认证
*/
int doRegisterValidation(const char* mobileNo, const char* srcID, const char* validateNo){
try {
Query query=m_conn.query();
query<< "select * from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"') ";
Result res=query.store();
if (res.size()!=0) {
Row row=*(res.begin());
if (strlen(row["ValidatationNumber"])!=0) {
if (strcmp(validateNo,row["ValidatationNumber"])){
return SMS_BBS_CMD_ERR;
}
std::stringstream sql;
sql<< "update SMSRegister_TB set ValidatationNumber='' where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"') ";
query.exec(sql.str());
return SMS_BBS_CMD_OK;
}
}
return SMS_BBS_CMD_NO_VALIDCODE;
} catch ( BadQuery er) {
syslog(LOG_ERR,"doRegisterValidation -- mysql query err : %s", er.error.c_str());
return SMS_BBS_CMD_DB_ERROR;
}
}
/* doRegisterValidation()
* }}} */
/* {{{ doUnregister()
* 取消手机绑定
*/
int doUnregister(const char* mobileNo,const char* srcID){
try {
Query query=m_conn.query();
query<< "select * from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"') ";
Result res=query.store();
if (res.size()!=0) {
std::stringstream sql;
sql << "delete from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"') ";
query.exec(sql.str());
return SMS_BBS_CMD_OK;
}
return SMS_BBS_CMD_OK;
} catch ( BadQuery er) {
syslog(LOG_ERR,"doUnregister -- mysql query err : %s", er.error.c_str());
return SMS_BBS_CMD_DB_ERROR;
}
}
/* doUnregister()
* ))) */
int doRegisterSMS(const char* mobileNo,const char* srcID){
try {
Query query=m_conn.query();
query<< "select * from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"') ";
Result res=query.store();
syslog(LOG_ERR,"%s %s %s",m_childCode,mobileNo,srcID);
if (res.size()!=0) {
return NOSEVEREERROR;
} else {
std::stringstream sql;
sql<< "insert into SMSRegister_TB(childCode, MobilePhoneNumber, ValidatationNumber, srcID, moneyLimit) values( '"
<<m_childCode<<"' , '"<<mobileNo<<"' , '', '"<<srcID<<"',"<<m_defaultMoneyLimit<<" )";
query.exec(sql.str());
}
return SUCCESS;
} catch ( BadQuery er) {
syslog(LOG_ERR,"doRegisterSMS -- mysql query err : %s", er.error.c_str());
return FAILED;
}
}
int doUnregisterSMS(const char* mobileNo,const char* srcID){
try {
Query query=m_conn.query();
query<< "select * from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"') ";
Result res=query.store();
if (res.size()!=0) {
std::stringstream sql;
sql << "delete from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNo<<"' and UPPER(srcID)=UPPER('"<<srcID<<"') ";
query.exec(sql.str());
return SUCCESS;
}
return NOSEVEREERROR;
} catch ( BadQuery er) {
syslog(LOG_ERR,"doUnregisterSMS -- mysql query err : %s", er.error.c_str());
return FAILED;
}
}
/* {{{ doReplyRegisterRequest()
* 绑定请求回应
*/
int doReplyRegisterRequest(const char* mobileNo, byte isSucceed, DWORD smsSerialNo) {
//todo: 错误处理与恢复
vector<struct bindRequest*>::iterator i;
struct bindRequest* p;
//block 上行短信;保护m_requestList;
sigset_t sigmask,oldmask;
sigemptyset(&sigmask);
sigaddset(&sigmask,SIGUSR1);
sigaddset(&sigmask,SIGUSR2);
sigprocmask(SIG_BLOCK,&sigmask,&oldmask);
for (i=m_requestList.begin();i!=m_requestList.end();i++) {
p=*i;
if (p->sn==smsSerialNo)
break;
}
if (i==m_requestList.end())
return SUCCESS;
m_requestList.erase(i);
sigprocmask(SIG_SETMASK,&oldmask,NULL);
int retCode;
char msg[101];
switch(isSucceed) {
case 0:
if (p->isBind==SMS_BBS_USR_REQUIRE_BIND) {
retCode=doRegisterSMS(p->mobileNumber.c_str(),p->userID.c_str());
switch (retCode) {
case ERROR:
return SUCCESS;
case SUCCESS:
snprintf(msg, 100, "您的手机号与id:%s绑定成功!", p->userID.c_str());
break;
default:
snprintf(msg, 100, "您的手机号与id:%s已处于绑定状态,请不要重复绑定.", p->userID.c_str());
break;
}
} else {
retCode=doUnregisterSMS(p->mobileNumber.c_str(),p->userID.c_str());
switch (retCode) {
case ERROR:
return SUCCESS;
case SUCCESS:
snprintf(msg, 100, "您的手机号与id:%s已成功解除绑定.", p->userID.c_str());
break;
default:
snprintf(msg, 100, "您的手机号与id:%s并没有绑定.", p->userID.c_str());
break;
}
}
break;
case 1:
snprintf(msg, 100, "您输入的id: %s 不存在!", p->userID.c_str());
break;
case 2:
snprintf(msg, 100, "您使用的手机与您在bbs上设定的手机号不一致!", p->userID.c_str());
break;
default:
snprintf(msg, 100, "bbs在处理您的绑定请求时出错!");
return SUCCESS;
}
PSMSMessage sms;
DWORD smsLen;
if (generateSMS(0,p->mobileNumber.c_str(), p->mobileNumber.c_str(),msg,strlen(msg),6, &sms,&smsLen)==NOENOUGHMEMORY) {
syslog(LOG_ERR,"Fatal Error: no enough memory for SMS convertion!system exited!");
exit(0);
}
sendSMS(sms,p->userID.c_str(),SEND_NO_CHECK);
free(sms);
delete(p);
return SUCCESS;
}
/* doReplyRegisterRequest()
*/
/* doSend()
* 处理SMS发送请求
*
*/
int doSend(PSMS_BBS_BBSSENDSMS msg){
if (!m_pChildPrivilegeChecker->canSendSMS(msg->SrcMobileNo,msg->srcUserID)){
return SMS_BBS_CMD_SMS_VALIDATE_FAILED;
}
PSMSMessage sms;
DWORD smsLen;
int retCode;
if (convertSMS(msg,&sms,&smsLen)==NOENOUGHMEMORY) {
syslog(LOG_ERR,"Fatal Error: no enough memory for SMS convertion!system exited!");
exit(0);
}
retCode=sendSMS(sms,msg->srcUserID);
free(sms);
return retCode;
}
/* doSend()
*/
/* {{{ OnCMDOK()
* 接收到回应成功消息时的处理函数
*/
int OnCMDOK(DWORD serialNo){
//todo: 错误处理与恢复
syslog(LOG_ERR," bbs send msg OK reply serial No is %d",serialNo);
return SUCCESS;
}
/* OnCMDOK()
* }}} */
/* {{{ OnCMDError()
* 接收到回应失败消息时的处理函数
*/
int OnCMDError(DWORD serialNo){
//todo: 错误处理与恢复
syslog(LOG_ERR," bbs send msg Error reply serial No is %d",serialNo);
return SUCCESS;
}
/* OnCMDError()
* }}} */
/* {{{ dispatchMessage()
* 消息派发函数,将消息转化为具体的函数调用
*
*/
int dispatchMessage( char* msg, DWORD len) {
byte msgType;
DWORD smsSerialNo, msgLen , pid;
msgType=(PSMS_BBS_HEADER(msg))->Type;
msgLen=sms_byteToLong((PSMS_BBS_HEADER(msg))->msgLength);
smsSerialNo=sms_byteToLong((PSMS_BBS_HEADER(msg))->SerialNo);
pid=sms_byteToLong((PSMS_BBS_HEADER(msg))->pid);
if ( (msgLen+sizeof(SMS_BBS_HEADER))!=len) {
doReply(SMS_BBS_CMD_LENGTH_ERR, (PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
return QUIT;
}
int retCode=0;
#ifdef DEBUG
syslog(LOG_ERR,"dispatching message...");
syslog(LOG_ERR," msg type %d",msgType);
syslog(LOG_ERR," msg sn %d",smsSerialNo);
#endif
switch (msgType) {
case SMS_BBS_CMD_LOGIN:
syslog(LOG_ERR," bbs send login msg error! have logined! serial No is %d", smsSerialNo);
doReply(SMS_BBS_CMD_ERR,(PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
break;
case SMS_BBS_CMD_LOGOUT:
doReply(SMS_BBS_CMD_OK,(PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
retCode=QUIT;
break;
case SMS_BBS_CMD_REG:
doReply(doSendRegisterSMS((PSMS_BBS_REGISTERMOBILEPACKET(msg))->MobileNo,(PSMS_BBS_REGISTERMOBILEPACKET(msg))->cUserID),(PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
break;
case SMS_BBS_CMD_CHECK:
doReply(doRegisterValidation((PSMS_BBS_REGISTERVALIDATIONPACKET(msg))->MobileNo,(PSMS_BBS_REGISTERVALIDATIONPACKET(msg))->cUserID, (PSMS_BBS_REGISTERVALIDATIONPACKET(msg))->ValidateNo),(PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
break;
case SMS_BBS_CMD_UNREG:
doReply(doUnregister((PSMS_BBS_UNREGISTERMOBILEPACKET(msg))->MobileNo,(PSMS_BBS_UNREGISTERMOBILEPACKET(msg))->cUserID),(PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
break;
case SMS_BBS_CMD_REQUEST:
syslog(LOG_ERR," bbs send REQUEST msg! error! serial No is %d",smsSerialNo);
break;
case SMS_BBS_CMD_REQUESTREPLY:
doReplyRegisterRequest((PSMS_BBS_BINDREQUESTREPLYPACKET(msg))->MobileNo,(PSMS_BBS_BINDREQUESTREPLYPACKET(msg))->isSucceed, smsSerialNo);
break;
case SMS_BBS_CMD_BBSSEND:
doReply(doSend((PSMS_BBS_BBSSENDSMS)msg),(PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
break;
case SMS_BBS_CMD_OK:
OnCMDOK(smsSerialNo);
break;
case SMS_BBS_CMD_ERR:
OnCMDError(smsSerialNo);
break;
case SMS_BBS_CMD_LINK:
doReply(SMS_BBS_CMD_LINK, (PSMS_BBS_HEADER(msg))->SerialNo,(PSMS_BBS_HEADER(msg))->pid);
break;
default:
syslog(LOG_ERR," bbs send msg type : %d serial No is %d",msgType, smsSerialNo);
return QUIT;
break;
}
return retCode;
}
/* dispatchMessage()
*/
int doStartChild(const char* childName){
int fd;
char buf[201];
char readBuf[31];
char writeBuf[31];
int len;
snprintf(buf,200,"%s%s.pid",SMSETCDIR, childName);
buf[200]=0;
fd=open(buf,O_RDWR|O_CREAT);
if (fd<0) {
syslog(LOG_ERR,"can't open pid file: %s %d",buf,errno);
return 0;
}
if (flock(fd,LOCK_EX)<0) {
syslog(LOG_ERR,"lock pid file %s error: %di", buf, errno);
close(fd);
return 0;
}
len=read(fd,readBuf,30);
if (len<0) {
syslog(LOG_ERR,"can't read pid file %s: %d" ,buf ,errno);
flock(fd,LOCK_UN);
close(fd);
return 0;
}
readBuf[len]=0;
if (len>0) {
kill(atoi(readBuf),SIGKILL);
}
lseek(fd,0,SEEK_SET);
snprintf(writeBuf,30,"%d",getpid());
writeBuf[30]=0;
write(fd,writeBuf,strlen(writeBuf));
flock(fd,LOCK_UN);
close(fd);
return 0;
}
/* {{{ OnAccept()
*
* 从客户端读取消息
*/
int OnAccept(CSMSTcpStream* pStream){
char buf[1000];
int errCount=0;
int i,ret,l;
int len=0;
byte msgType=0;
l=sizeof(SMS_BBS_HEADER);
i=pStream->read(buf+len,l);
if (i<l) {
syslog(LOG_ERR," read login msg header error %d!", errno);
return -1;
}
len+=i;
msgType=(PSMS_BBS_HEADER(buf))->Type;
#ifdef DEBUG
DWORD smsSerialNo, msgLen ;
msgLen=sms_byteToLong((PSMS_BBS_HEADER(buf))->msgLength);
smsSerialNo=sms_byteToLong((PSMS_BBS_HEADER(buf))->SerialNo);
syslog(LOG_ERR,"login msg head length %d",msgLen);
syslog(LOG_ERR,"login msg sn %d",smsSerialNo);
#endif
if ( (msgType!=SMS_BBS_CMD_LOGIN) ) {
syslog(LOG_ERR, "login msg head type error");
return -1;
}
i=pStream->read(buf+len,msgLen);
if (i<l) {
syslog(LOG_ERR," read login msg header body error %d!", errno);
return -1;
}
len+=i;
syslog(LOG_ERR," %s login %s", (PSMS_BBS_LOGINPACKET(buf))->user,(PSMS_BBS_LOGINPACKET(buf))->password);
m_pStream=pStream;
InetHostAddress addr=pStream->getPeer();
in_addr address=addr.getAddress();
if (m_pChildPrivilegeChecker->loginUser(address.s_addr,(PSMS_BBS_LOGINPACKET(buf))->user,(PSMS_BBS_LOGINPACKET(buf))->password,m_childCode,m_childName,&m_defaultMoneyLimit)==FALSE){
doReply(SMS_BBS_CMD_ERR,(PSMS_BBS_HEADER(buf))->SerialNo,(PSMS_BBS_HEADER(buf))->pid);
syslog(LOG_ERR,"connection failed!");
return -1;
}
doStartChild(m_childName);
closelog();
snprintf( RCL::__z_self, 16, "sms_c_bbs_%s", m_childName ) ;
openlog(RCL::__z_self, LOG_PID, LOG_LOCAL0);
syslog(LOG_ERR, "%s connected!", m_childName);
char inbox[201];
char inboxback[201];
snprintf(inbox,200,"%sinbox/bbs_%s",SMSHOME,m_childName);
snprintf(inboxback,200,"%sinbox.back/bbs_%s",SMSHOME,m_childName);
doReply(SMS_BBS_CMD_OK,(PSMS_BBS_HEADER(buf))->SerialNo,(PSMS_BBS_HEADER(buf))->pid);
m_pSMSStorage= new CSMSDiskStorage(this,SMSHOME "outbox/deliver", inbox, inboxback);
m_pSMSStorage->init();
m_pSMSStorage->OnNotify();
for (;;){
len=0;
l=sizeof(SMS_BBS_HEADER);
i=pStream->read(buf,l);
if (i<l) {
syslog(LOG_ERR," read msg header error %d!", errno);
return 0;
}
len+=i;
#ifdef DEBUG
msgType=(PSMS_BBS_HEADER(buf))->Type;
#endif
msgLen=sms_byteToLong((PSMS_BBS_HEADER(buf))->msgLength);
#ifdef DEBUG
smsSerialNo=sms_byteToLong((PSMS_BBS_HEADER(buf))->SerialNo);
{
char sbuf[5000];
for (i=0;i<l;i++){
sprintf(sbuf+i*3, "%02X ",buf[i]);
}
syslog(LOG_ERR,"msg header: %s",sbuf);
}
syslog(LOG_ERR,"msg head length %d",msgLen);
syslog(LOG_ERR,"msg sn %d",smsSerialNo);
#endif
i=pStream->read(buf+len,msgLen);
if (i<msgLen) {
syslog(LOG_ERR," read msg header error %d!", errno);
return 0;
}
len+=i;
if (dispatchMessage(buf,len)==QUIT) {
return 0;
}
}
m_pStream=NULL;
return 0;
}
/* OnAccept()
* }}} */
/* {{{ getSMSType()
* 获取短消息类别
*/
int getSMSType(const char * targetMobileNo) {
int prefixLen=strlen(m_childCode);
if ( strlen(targetMobileNo)<prefixLen){
return SMS_BBS_TYPE_NONE;
}
if ( strlen(targetMobileNo)==prefixLen){
return SMS_BBS_TYPE_COMMAND;
}
return SMS_BBS_TYPE_COMMON;
}
/* getSMSType()
* }}} */
/* {{{ getTargetID()
* 获取短消息目标ID
*/
DWORD getTargetID(const char * targetMobileNo) {
int prefixLen=strlen(m_childCode);
if (strlen(targetMobileNo)<=prefixLen){
return 0L;
}
return atoi(targetMobileNo+(prefixLen));
}
/* getTargetID()
* }}} */
/* {{{ deliverSMS()
* 向下游传递短消息
*/
int deliverSMS(PSMSMessage msg) {
PSMS_BBS_GWSENDSMS sms;
DWORD smsLen=sizeof(SMS_BBS_GWSENDSMS)+msg->SMSBodyLength;
syslog(LOG_ERR,"delivering msg ....");
sms=(PSMS_BBS_GWSENDSMS)malloc(smsLen);
if (sms==NULL) {
syslog(LOG_ERR,"Fatal Error: can't alloc enough memory for sms convertion!");
exit(-1);
}
memset(sms,0,smsLen);
sms->header.Type=SMS_BBS_CMD_GWSEND;
sms_longToByte( (sms->header.SerialNo), getSerial());
sms_longToByte( (sms->header.msgLength),smsLen-sizeof(SMS_BBS_HEADER) );
sms_longToByte( sms->UserID, getTargetID(msg->TargetNumber) );
strncpy(sms->SrcMobileNo, msg->SenderNumber , MOBILENUMBERLENGTH);
sms_longToByte((sms->MsgTxtLen) , msg->SMSBodyLength);
memcpy(sms->MsgTxt, msg->SMSBody, msg->SMSBodyLength);
int retCode=doSendMsg(sms,smsLen);
syslog(LOG_ERR,"deliver sms to child, retCode=%d",retCode);
free(sms);
return retCode;
}
/* deliverSMS()
* ))) */
int doSendRegisterMsg(const char* mobileNumber, const char * usrID, byte isBind) {
SMS_BBS_BINDREQUESTPACKET sms;
DWORD smsLen=sizeof(SMS_BBS_BINDREQUESTPACKET);
struct bindRequest* pRequest=new struct bindRequest;
memset(&sms,0,smsLen);
sms.header.Type=SMS_BBS_CMD_REQUEST;
pRequest->sn=getSerial();
sms_longToByte( (sms.header.SerialNo), pRequest->sn);
pRequest->userID=usrID;
pRequest->mobileNumber=mobileNumber;
pRequest->isBind=isBind;
m_requestList.push_back(pRequest) ;
sms_longToByte( (sms.header.msgLength),smsLen-sizeof(SMS_BBS_HEADER) );
strncpy(sms.MobileNo, mobileNumber, MOBILENUMBERLENGTH);
sms.MobileNo[MOBILENUMBERLENGTH]=0;
strncpy(sms.cUserID, usrID, SMS_BBS_ID_LEN);
sms.cUserID[SMS_BBS_ID_LEN]=0;
sms.Bind=isBind;
int retCode=doSendMsg(&sms,smsLen);
return retCode;
}
int doRegisterCommand(const char* mobileNumber, const char * usrID) {
return doSendRegisterMsg(mobileNumber,usrID, SMS_BBS_USR_REQUIRE_BIND);
}
int doUnRegisterCommand(const char* mobileNumber, const char * usrID) {
return doSendRegisterMsg(mobileNumber,usrID, SMS_BBS_USR_REQUIRE_UNBIND);
}
int doSetMoneyLimit(const char* mobileNumber, const char * usrID, int limit){
try{
Query query=m_conn.query();
std::stringstream sql;
sql<< "update SMSRegister_TB set moneyLimit="<<limit<<" where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNumber<<"' and UPPER(srcID)=UPPER('"<<usrID<<"') ";
query.exec(sql.str());
return SUCCESS;
} catch ( BadQuery er) {
syslog(LOG_ERR," mysql query err : %s", er.error.c_str());
}
return ERROR;
}
int doSetMoneyLimitCommand(const char* mobileNumber, const char * usrID, int limit) {
int retCode=doSetMoneyLimit(mobileNumber,usrID,limit);
if (retCode!=SUCCESS) {
return retCode;
}
PSMSMessage sms;
DWORD smsLen;
char msg[101];
snprintf(msg, 100, "您已成功设置bbs每日短信发送限额为 %d.%02d 元", limit/100, limit%100);
if (generateSMS(0,mobileNumber, mobileNumber,msg,strlen(msg),6, &sms,&smsLen)==NOENOUGHMEMORY) {
syslog(LOG_ERR,"Fatal Error: no enough memory for SMS convertion!system exited!");
exit(0);
}
retCode=(sendSMS(sms,usrID)==SMS_BBS_CMD_OK)?SUCCESS:ERROR;
free(sms);
return retCode;
}
int doZeroTodayTotal(const char* mobileNumber, const char * usrID){
try{
Query query=m_conn.query();
std::stringstream sql;
sql<< "update SMSRegister_TB set todayMoney=0 where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNumber<<"' and UPPER(srcID)=UPPER('"<<usrID<<"') ";
query.exec(sql.str());
return SUCCESS;
} catch ( BadQuery er) {
syslog(LOG_ERR," mysql query err : %s", er.error.c_str());
}
return ERROR;
}
int doZeroTodayTotalCommand(const char* mobileNumber, const char * usrID) {
int retCode=doZeroTodayTotal(mobileNumber,usrID);
if (retCode!=SUCCESS) {
return retCode;
}
PSMSMessage sms;
DWORD smsLen;
char msg[101];
snprintf(msg, 100, "您已成功清零本日bbs短信累积发送金额");
if (generateSMS(0,mobileNumber, mobileNumber,msg,strlen(msg),6, &sms,&smsLen)==NOENOUGHMEMORY) {
syslog(LOG_ERR,"Fatal Error: no enough memory for SMS convertion!system exited!");
exit(0);
}
retCode=(sendSMS(sms,usrID)==SMS_BBS_CMD_OK)?SUCCESS:ERROR;
free(sms);
return retCode;
}
int doGetMoneyLimit(const char* mobileNumber, const char * usrID, int* pLimit){
try{
Query query=m_conn.query();
query<< "select moneyLimit from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNumber<<"' and UPPER(srcID)=UPPER('"<<usrID<<"') ";
Result res=query.store();
if (res.size()!=0) {
Row row=*(res.begin());
*pLimit=atoi(row["moneyLimit"]);
return SUCCESS;
}
} catch ( BadQuery er) {
syslog(LOG_ERR," mysql query err : %s", er.error.c_str());
}
return FAILED;
}
int doGetMoneyLimitCommand(const char* mobileNumber, const char * usrID) {
int limit;
int retCode=doGetMoneyLimit(mobileNumber,usrID,&limit);
if (retCode!=SUCCESS) {
return retCode;
}
PSMSMessage sms;
DWORD smsLen;
char msg[101];
snprintf(msg, 100, "您当前的bbs日短信发送限额为 %d.%02d 元", limit/100, limit %100);
if (generateSMS(0,mobileNumber, mobileNumber,msg,strlen(msg),6, &sms,&smsLen)==NOENOUGHMEMORY) {
syslog(LOG_ERR,"Fatal Error: no enough memory for SMS convertion!system exited!");
exit(0);
}
retCode=(sendSMS(sms,usrID)==SMS_BBS_CMD_OK)?SUCCESS:ERROR;
free(sms);
return retCode;
}
int doGetTodayTotal(const char* mobileNumber, const char * usrID, int* pTotal){
try{
Query query=m_conn.query();
query<< "select todayMoney from SMSRegister_TB where childCode='"<<m_childCode<<"' and MobilePhoneNumber='"<<mobileNumber<<"' and UPPER(srcID)=UPPER('"<<usrID<<"') ";
Result res=query.store();
if (res.size()!=0) {
Row row=*(res.begin());
*pTotal=atoi(row["todayMoney"]);
return SUCCESS;
}
} catch ( BadQuery er) {
syslog(LOG_ERR," mysql query err : %s", er.error.c_str());
}
return FAILED;
}
int doGetTodayTotalCommand(const char* mobileNumber, const char * usrID) {
int total;
int retCode=doGetTodayTotal(mobileNumber,usrID,&total);
if (retCode!=SUCCESS) {
return retCode;
}
PSMSMessage sms;
DWORD smsLen;
char msg[101];
snprintf(msg, 100, "您本日在bbs上已收发短信共 %d.%02d 元", total/100, total%100);
if (generateSMS(0,mobileNumber, mobileNumber,msg,strlen(msg),6, &sms,&smsLen)==NOENOUGHMEMORY) {
syslog(LOG_ERR,"Fatal Error: no enough memory for SMS convertion!system exited!");
exit(0);
}
retCode=(sendSMS(sms,usrID)==SMS_BBS_CMD_OK)?SUCCESS:ERROR;
free(sms);
return retCode;
}
/* {{{ processRegisterSMS()
* 处理用户命令短消息
*/
int processCommandSMS(PSMSMessage msg) {
char usrID[SMS_BBS_MAX_COMMAND_SMS_LEN+1]="";
char command[SMS_BBS_MAX_COMMAND_SMS_LEN+1]="";
char option1[SMS_BBS_MAX_COMMAND_SMS_LEN+1]="";
char option2[SMS_BBS_MAX_COMMAND_SMS_LEN+1]="";
char buf[SMS_BBS_MAX_COMMAND_SMS_LEN+1];
int retCode;
if ( (msg->SMSBodyLength>SMS_BBS_MAX_COMMAND_SMS_LEN)){
syslog(LOG_ERR,"received error registe sms!");