-
Notifications
You must be signed in to change notification settings - Fork 1
/
main-server.cpp
1465 lines (1303 loc) · 38 KB
/
main-server.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
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
/***************************************************************************
main-server.cpp - description
-------------------
begin : Sat Oct 30 1999
copyright : (C) 1999 by Team FXML
email : fxml@exite.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "stdafx.h"
#include "main-server.h"
#include <fstream>
#include <iostream>
using namespace std;
///////////////////////////////////////////////////////
//
// Name: ProcessSocket
// Params: sdPass - socket to read/write to
// Returns: none
// Description: The worker thread function. listens to socket
///////////////////////////////////////////////////////
void ProcessSocket(void* sdPass)
{
SOCKET *sd= (SOCKET*)(sdPass);
//init OLE
CoInitialize(NULL);
//read and write the socket
ReadAndWriteSocket((*sd));
CoUninitialize();
//end the thread
_endthread();
}
///////////////////////////////////////////////////////
//
// Name: DoWinsock
// Params: pcAddress - localhost address
// nPort - port to listen to
// Returns: int
// Description: Creates new thread for each socket request. The main
// socket function.
///////////////////////////////////////////////////////
int DoWinsock(int nPort, BOOL* bGo)
{
LogEvent("DoWinsock reports establishing listener", 1);
SOCKET ListeningSocket = SetUpListener(htons(nPort));
if (ListeningSocket == INVALID_SOCKET) {
LogEvent(WSAGetLastErrorMessage("establish listener"), 3);
return 3;
}
InitializeCriticalSection(&gcsLock);
while ((*bGo)) {
LogEvent("DoWinsock reports waiting for a connection", 1);
sockaddr_in sinRemote;
SOCKET sd = AcceptConnection(ListeningSocket, sinRemote);
if (sd != INVALID_SOCKET) {
LogEvent(((std::string)"DoWinsock reports connection established from" + (std::string)inet_ntoa(sinRemote.sin_addr)).c_str() , 1);
}
else {
LogEvent(WSAGetLastErrorMessage("accept connection"),2);
return 3;
}
InitializeGlobalVariables();
//if too many threads active, do not accept connection
if ((gmSockets.size() > gnMaxThreadCount) || (!IsValidConnection(inet_ntoa(sinRemote.sin_addr))))
{
ShutdownConnection(sd);
}
else
{
//insert socket into open socket list
EnterCriticalSection(&gcsLock);
try
{
gmSockets.insert(SOCKETMAP::value_type(sd, sd));
}
catch(...)
{
LogEvent("Exception thrown in socketmap insert", 2);
};
LeaveCriticalSection(&gcsLock);
//start processing
_beginthread(ProcessSocket, 0, &sd);
}
}
//close any outstanding connections
SOCKETMAP::iterator iSocket;
for (iSocket = gmSockets.begin(); iSocket != gmSockets.end(); iSocket++)
{
ShutdownConnection((*iSocket).first);
gmSockets.erase(iSocket);
}
DeleteCriticalSection(&gcsLock);
return 0;
}
///////////////////////////////////////////////////////
//
// Name: SetUpListener
// Params: pcAddress - localhost address
// nPort - port to listen to
// Returns: Socket
// Description: Sets up a listener on the given interface and port, returning the
// listening socket if successful; if not, returns INVALID_SOCKET.
///////////////////////////////////////////////////////
SOCKET SetUpListener(int nPort)
{
SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd != INVALID_SOCKET) {
sockaddr_in sinInterface;
sinInterface.sin_family = AF_INET;
sinInterface.sin_addr.s_addr = htonl (ADDR_ANY);
sinInterface.sin_port = nPort;
if (bind(sd, (sockaddr*)&sinInterface,
sizeof(sockaddr_in)) != SOCKET_ERROR) {
listen(sd, SOMAXCONN);
return sd;
}
}
return INVALID_SOCKET;
}
///////////////////////////////////////////////////////
//
// Name: AcceptConnection
// Params: ListeningSocket - socket to accept
// sinRemote - remote client address
// Returns: Socket
// Description: Accepts a socket connection. If error
// returns INVALID_SOCKET
///////////////////////////////////////////////////////
SOCKET AcceptConnection(SOCKET ListeningSocket, sockaddr_in& sinRemote)
{
int nAddrSize = sizeof(sinRemote);
return accept(ListeningSocket, (sockaddr*)&sinRemote, &nAddrSize);
}
///////////////////////////////////////////////////////
//
// Name: ReadAndWriteSocket
// Params: sd - socket to read/write from
// Returns: BOOLEAN success
// Description: Reads XML query from socket, writes
// XML result set
///////////////////////////////////////////////////////
bool ReadAndWriteSocket(SOCKET sd)
{
// Read data from client
char acReadBuffer[kBufferSize];
int nReadBytes = 0; //number of bytes read
std::string sRead, sTemp, sResult; //string to read, and result
int nFind; //result var
BOOL bAllDone = FALSE;//whether the transaction is finished or not
//variables for setting up timeout
fd_set read_fds;
struct timeval TimeOut;
FD_ZERO(&read_fds);
FD_SET(sd, &read_fds);
TimeOut.tv_sec = gnThreadTimeout; //use timeout of durartion gnThreadTimeout
TimeOut.tv_usec = 0;
try
{
if (select(FD_SETSIZE, &read_fds, NULL, NULL, &TimeOut) != SOCKET_ERROR)
{
if (FD_ISSET(sd, &read_fds))
{
do {
nReadBytes = recv(sd, acReadBuffer, kBufferSize, 0);
if (nReadBytes > 0) {
LogEvent("ReadWriteSocket reports receiving client data", 1);
//copy over inbound buffer
sTemp = acReadBuffer;
sRead.append(sTemp.substr(0,nReadBytes));
sTemp.erase();
//search for the end of XML. If found, we have received
//all data. If not found, keep receiving data
nFind = sRead.find("</request>");
if (nFind > 0)
{
//parse out request
FXMLParser fxParse; //xml parser
FXMLElement *fxElem = NULL;
try
{
fxElem = fxParse.ParseString(sRead.c_str()); //parse string
}
catch(...)
{
LogEvent("ReadAndWriteSocket reports fXML Parser exception.", 2);
break;
}
FXMLElement *fxRet = NULL;
std::string sConnectionString, sSQL;
//if string is not XML , exit
if ((fxElem == NULL) || (fxParse.sError.length() > 0))
{
LogEvent("ReadWriteSocket reports receiving invalid XML string", 2);
break;
}
//get connection string
fxRet = FindChildByName("connectionstring", fxElem, fxElem->mChildren->begin());
if (fxRet != NULL)
sConnectionString = fxRet->Value();
else
{
LogEvent("ReadWriteSocket reports no connection string found.",2);
break;
}
fxRet = NULL;
//get SQL
fxRet = FindChildByName("sql", fxElem, fxElem->mChildren->begin());
if (fxRet != NULL)
sSQL = fxRet->Value();
else
{
LogEvent("ReadWriteSocket reports no SQL string found.",2);
break;
}
if ((sConnectionString.length() == 0) || (sSQL.length() == 0)) break;
//now sub in > for < , etc..
sSQL = SubStrReplace(sSQL, "<", "<" );
sSQL = SubStrReplace(sSQL, ">", ">");
sSQL = SubStrReplace(sSQL, "&", "&");
LogEvent(((std::string)"ReadWriteSocket reports executing SQL:" + sSQL).c_str(), 1);
//now execute SQL
sResult = ExecSQL(sConnectionString, sSQL);
//now send back response
nReadBytes = sResult.length();
int nSentBytes = 0;
while (nSentBytes < nReadBytes) {
nSentBytes = send(sd, sResult.c_str() + nSentBytes,
nReadBytes - nSentBytes, 0);
if (nSentBytes <= 0)
{
//closed connection or something
LogEvent("ReadWriteSocket reports no data to return", 1);
break;
}
}//while
bAllDone = TRUE;
sRead = "";
} //nfind > 0
//look for termination request
}//nreadbytes > 0
} while ((nReadBytes > 0) && (!bAllDone));
}//FD_ISSET
}//select
else if (nReadBytes == SOCKET_ERROR) {
LogEvent("ReadWriteSocket reports socket error after data return. Not necessarily a problem.", 1);
}
if (!bAllDone) LogEvent("ReadWriteSocket reports invalid data received, no SQL executed.", 2);
}
catch(...)
{
LogEvent("ReadAndWriteSocket reports fatal exception reading data.",2);
}
try
{
SOCKETMAP::iterator iSocket; //socket iterator
if (ShutdownConnection(sd)) {
LogEvent("ReadAndWriteSocket reports closing connection", 1);
}
else {
LogEvent(WSAGetLastErrorMessage("shutdown connection"),2);
}
EnterCriticalSection(&gcsLock);
try
{
iSocket = gmSockets.find(sd);
gmSockets.erase(iSocket);
}
catch(...)
{
}
LeaveCriticalSection(&gcsLock);
}
catch(...)
{
LogEvent("ReadAndWriteSocket reports fatal exception closing connection.",2);
}
return true;
}
///////////////////////////////////////////////////////
//
// Name: ExecSQL
// Params: sConnectionString - Connection string to use
// sSQL - SQL to execute
// Returns: XML result of SQL statements
// Description: Executes SQL on specified database.
// Returns result set in XML
///////////////////////////////////////////////////////
std::string ExecSQL(std::string sConnectionString, std::string sSQL)
{
USES_CONVERSION;
//result string
std::string sResult = "<?xml version=\"1.0\"?>\r\n";
_variant_t vArray; //result variant
VARIANT vNull;
HRESULT hr;
_ConnectionPtr Conn1; //the connection to the database
_CommandPtr Cmd1; //the command object
_RecordsetPtr rsResult; //recordset to be returned
varPass stPass; //to pass to getvar xml
try{
hr = Conn1.CreateInstance(__uuidof(Connection));
Conn1->ConnectionString = T2OLE(sConnectionString.c_str());
Conn1->ConnectionTimeout = gnSQLTimeout;
Conn1->CommandTimeout = gnSQLTimeout;
Conn1->Open(L"", L"", L"", -1);
hr = Cmd1.CreateInstance(__uuidof(Command));
Cmd1->ActiveConnection = Conn1;
//now, execute the SQL Statement
Cmd1->CommandText = T2OLE((char*)sSQL.c_str());
//create the recordset object
hr = rsResult.CreateInstance(_uuidof(Recordset));
rsResult->PutRefSource(Cmd1);
//initialize the optional parameters
VariantInit(&vNull);
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
//execute the SQL command directly
rsResult->Open(T2OLE((char*)sSQL.c_str()), vNull, adOpenForwardOnly, adLockReadOnly, adCmdText);
//if the recordset is closed, no data is returned. Successful execution
if (rsResult->State == adStateClosed )
{
Conn1 = NULL;
rsResult = NULL;
Cmd1 = NULL;
sResult += "<result state=\"success\">\r\n</result>\r\n";
return sResult;
}
if (rsResult->adoEOF == VARIANT_FALSE)
{
rsResult->Move(0);
if (gnUseMSDTD == 1)
{
//use MS DTD to retrieve data
sResult = RecordSetToMSDTD(rsResult);
}
else if (gnUseMSDTD == 2)
{
//convert recordset to string, then to our DTD
sResult = RecordSetToMSDTD(rsResult);
sResult = MSDTDToOurDTD(sResult);
}
else /*if (gnUseMSDTD == 0)*/
{
//get the result set rows
vArray = rsResult->GetRows(adGetRowsRest);
//convert array to string
//return the string
stPass.vValue = vArray;
stPass.bsValue = L"";
VariantToXML(&stPass, rsResult);
sResult += OLE2T(stPass.bsValue);
}
}
else
{
Conn1 = NULL;
rsResult = NULL;
Cmd1 = NULL;
sResult += "<result state=\"success\">\r\n</result>\r\n";
return sResult;
}
}
catch(_com_error &e)
{
std::string sError = GetADOError(Conn1);
sResult += (std::string)"<result state=\"failure\">\r\n<error>" + sError + (std::string)"</error>\r\n</result>\r\n";
}
catch(...)
{
sResult += (std::string)"<result state=\"failure\">\r\n<error>Unknown Error</error>\r\n</result>\r\n";
};
//close everything up!
Conn1 = NULL;
rsResult = NULL;
Cmd1 = NULL;
return sResult;
}
///////////////////////////////////////////////////////
//
// Name: GetADOError
// Params: Conn1 - connection pointer
// Returns: ADO error string
// Description: determines ADO error(s) that occurred
///////////////////////////////////////////////////////
std::string GetADOError(_ConnectionPtr Conn1)
{
USES_CONVERSION;
ErrorsPtr Errs1 = NULL;
ErrorPtr Err1 = NULL;
std::string sRet("ADO Error. Description: ");
long nCount;
try
{
if( Conn1 == NULL ) return sRet + (std::string)"NULL Connection";
// Enumerate Errors Collection and display properties of each object.
Errs1 = Conn1->GetErrors();
nCount = Errs1->GetCount();
// Loop through Errors Collection
for( long i = 0; i < nCount; i++ )
{
// Get Error Item
Err1 = Errs1->GetItem((_variant_t)((long)i) );
sRet += (std::string)" Description: " + (std::string)OLE2T(Err1->GetDescription());
}
if( Errs1 != NULL ) Errs1->Release();
if( Err1 != NULL ) Err1->Release();
}
catch(...)
{
sRet += " Unknown Error";
}
return sRet;
}
///////////////////////////////////////////////////////
//
// Name: MSDTDToOurDTD
// Params: sInput - string to convert
// Returns: XML string in our format
// Description: converts from XML string in MS format to our format
///////////////////////////////////////////////////////
std::string MSDTDToOurDTD(std::string sInput)
{
FXMLParser fxParse; //xml parser
FXMLElement *fxElem = NULL;
std::string sResult = "<?xml version=\"1.0\" ?>\r\n";
FXMLElement *fxRet = NULL; //search element
std::list<FXMLElement*>::iterator iChildLoc; //iterator for children
std::map<std::string, std::string> mChildAttributes; //attributes
std::map<std::string, std::string>::iterator iAttribLoc;
try
{
fxElem = fxParse.ParseString(sInput.c_str()); //parse string
}
catch(...)
{
LogEvent("MSDTDToOurDTD reports fXML Parser exception.", 2);
return sResult + (std::string)"<result state=\"failure\">\r\n<error>Unknown exception</error>\r\n</result>\r\n";
}
//the idea is to iterate through each rs:data column and print it out in our format
fxRet = FindChildByName("rs:data", fxElem, fxElem->mChildren->begin());
if (fxRet != NULL)
{
if (fxRet->mChildren != NULL)
{
if (!fxRet->mChildren->empty())
{
sResult += "<result state=\"success\">\r\n";
for (iChildLoc = fxRet->mChildren->begin(); iChildLoc != fxRet->mChildren->end(); ++iChildLoc)
{
sResult += "<row>\r\n";
if (*iChildLoc)
{
//get the attribute list here
mChildAttributes = (*iChildLoc)->mAttributes;
if (!mChildAttributes.empty())
{
for (iAttribLoc = mChildAttributes.begin(); iAttribLoc != mChildAttributes.end(); ++iAttribLoc)
{
//always print out the name too because if a field is null,
//it will not print out!
sResult += "<column name=\"" + (*iAttribLoc).first + "\">" + (*iAttribLoc).second + "</column>\r\n";
}
}
}
sResult += "</row>\r\n";
}
}
}
}
sResult += "</result>\r\n";
return sResult;
}
//converts recordset to MS dtd
std::string RecordSetToMSDTD(_RecordsetPtr rsResult)
{
std::string sRet = "ERROR: Conversion failure"; //return string
std::string sTempName = "c:\\temp\\msxml.tmp"; //temp file name
//first, get temp file name to save recordset to
DWORD dwTmpResult; //temporary results
LPTSTR lpTmpDir = (char*)malloc(256);
LPTSTR lpTmpFileName = (char*)malloc(256); //the directory names
DWORD dwBufferSize = 256; //buffer size
LPCTSTR lpcPrefix = "DTZ"; //prefix for the temp file
UINT uResult; //result of the temp file
UINT uPass = 0;
std::string sBuffer(""); //line of file to read
BOOLEAN bError = FALSE;
dwTmpResult = GetTempPath(dwBufferSize, lpTmpDir);
if (dwTmpResult != 0)
{
uResult = ::GetTempFileNameA(lpTmpDir, lpcPrefix, uPass, lpTmpFileName);
//now save the file name by accessing the map
sTempName = lpTmpFileName;
}
if (!sTempName.empty())
{
//save the recordser to disk
try
{
remove(sTempName.c_str());
rsResult->Save(sTempName.c_str(), adPersistXML);
}
catch(_com_error e)
{
sRet = e.ErrorMessage();
bError = TRUE;
}
//now read file into string
std::ifstream File(sTempName.c_str(), ios::in);
//open the file
if(File)
{
//read file into string
if (!bError)
{
sRet = "";
while(!File.eof())
{
std::getline(File, sBuffer);
sBuffer.append("\r\n");
sRet.append(sBuffer);
}
}
//now remove the file
File.close();
remove(sTempName.c_str());
}
}
free(lpTmpDir);
free(lpTmpFileName);
return sRet;
}
///////////////////////////////////////////////////////
//
// Name: FindChildByName
// Params: fxElement - FXMLElement to search
// sChildName - child name to return
// Returns: fxElement with name sChildName, or NULL if not found
// Description: searches lr an fxElement for child name sChildName
// returns fxElement or NULL if not found
///////////////////////////////////////////////////////
FXMLElement* FindChildByName(std::string sChildName, FXMLElement* fxElement, std::list<FXMLElement*>::iterator iChild)
{
//recursively search through child nodes until element name is found
if (fxElement == NULL)
{
LogEvent("FindChildByName reports no child found.", 2);
return NULL; //sanity check
}
std::list<FXMLElement*>::iterator iChildLoc; //iterator for children
FXMLElement *fxRet = NULL; //return element
std::string sCompare = alltrim(_strlwr((char*)fxElement->Name().c_str()));
//stopping condition
if (sCompare.compare((std::string)alltrim(_strlwr((char*)sChildName.c_str()))) == 0)
{
return fxElement;
}
if (fxElement->mChildren != NULL)
{
if (!fxElement->mChildren->empty())
{
for (iChildLoc = fxElement->mChildren->begin(); iChildLoc != fxElement->mChildren->end(); ++iChildLoc)
{
if (*iChildLoc)
{
//recursive call here
fxRet = FindChildByName(sChildName, (*iChildLoc), iChildLoc);
if (fxRet != NULL) return fxRet;
}
}
}
}
return NULL;
}
///////////////////////////////////////////////////////
//
// Name: LogEvent
// Params: pFormat - Message string
// nSeverity - severity of event
// Returns: none
// Description: Logs Event to event log IF severity is >= gnDebugLevel
//nSeverity = 1 for informational
// = 2 for warnings
// = 3 for errors
///////////////////////////////////////////////////////
void LogEvent(LPCTSTR pFormat, short nSeverity)
{
TCHAR chMsg[2048];
HANDLE hEventSource;
LPTSTR lpszStrings[1];
va_list pArg;
//ensure event is severe enough to log
if (nSeverity < gnDebugLevel) return;
if (strlen(pFormat) > 2040) return;
va_start(pArg, pFormat);
_vstprintf(chMsg, pFormat, pArg);
va_end(pArg);
lpszStrings[0] = chMsg;
/* Get a handle to use with ReportEvent(). */
hEventSource = RegisterEventSource(NULL, gsServiceName);
if (hEventSource != NULL)
{
/* Write to event log. */
WORD wEventSource = EVENTLOG_INFORMATION_TYPE;
if (nSeverity == 1) wEventSource = EVENTLOG_INFORMATION_TYPE;
else if (nSeverity == 2) wEventSource = EVENTLOG_WARNING_TYPE;
else if (nSeverity == 3) wEventSource = EVENTLOG_ERROR_TYPE;
ReportEvent(hEventSource, wEventSource, 0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL);
DeregisterEventSource(hEventSource);
}
}
//code to trim whitespace
/* Remove trailing whitespace */
char * rtrim(char *str) {
char *s, *p; int c;
s = p = str;
while ((c = *s++)) if (c > ' ') p = s;
*p = '\0';
return str;
}
/* Remove leading whitespace */
char * ltrim(char *str) {
char *s, *p;
for (s = p = str; *s && *s <= ' '; s++) ;
while ((*p++ = *s++)) ;
return str;
}
/* Combination of the two */
char * alltrim(char *str) {
return rtrim(ltrim(str));
}
///////////////////////////////////////////////////////
//
// Name: VariantToXML
// Params: vPass - variant containing array to convert
// Returns: XML string
// Description: Converts a variant array into xml string
// Encapsulates all data in CDATA segment to prevent
// problems when passing back binary/tagged data
///////////////////////////////////////////////////////
DWORD VariantToXML(void * vPass, _RecordsetPtr rsResult)
{
CComBSTR bsXML = "<result state=\"success\">\r\n";
HRESULT hr; //result
SAFEARRAY* psa = NULL; //array
long lUBound, lURowBound; //upper bound
long lIndex[2]; //index
_variant_t vResult; //result
_variant_t vIn = ((varPass*)vPass)->vValue;
try
{
if (vIn.vt == (VT_ARRAY | VT_VARIANT))
{
psa = V_ARRAY(&vIn);
//get the size of the 2nd dimension
hr = SafeArrayGetUBound(psa, 2, &lUBound);
if (FAILED(hr)) goto ErrorExit;
//get size of first dim
hr = SafeArrayGetUBound(psa, 1, &lURowBound);
if (FAILED(hr)) goto ErrorExit;
for (lIndex[1] = 0; lIndex[1] <= lUBound; lIndex[1]++)
{
bsXML += "<row>\r\n";
for (lIndex[0] = 0; lIndex[0] <= lURowBound; lIndex[0]++)
{
VariantInit(&vResult);
//start column heading
//print column names just for the first set of cols returned
if (lIndex[1] == 0)
{
bsXML += "<column name=\"";
bsXML += (CComBSTR)((BSTR)rsResult->Fields->Item[lIndex[0]]->Name);
bsXML += "\">";
}
else
bsXML += "<column>";
try{
//convert var to string
SafeArrayGetElement(psa, &lIndex[0], &vResult);
bsXML += CComBSTR((VarToStr(vResult)).c_str());
}
catch(...)
{}
bsXML += "</column>\r\n";
//clear variant
VariantClear(&vResult);
}
bsXML += "</row>\r\n";
}
}
}
catch(...)
{
LogEvent("VariantToXML reports exception", 2);
}
ErrorExit:
bsXML += "</result>\r\n";
((varPass*)vPass)->bsValue = bsXML;
return 0;
}
///////////////////////////////////////////////////////
//
// Name: VarToStr
// Params: vrVar - variant to convert to string
// Returns: string
// Description: Converts a variant into a string
///////////////////////////////////////////////////////
inline std::string VarToStr(VARIANT vrVar){
long lTemp=0; //temp long
short sTemp=0; //temp short
char cbuf[50];
std::string sCurTemp; //temp string for currency
USES_CONVERSION;
try
{
if (vrVar.scode == DISP_E_PARAMNOTFOUND){
return "";
}
switch (vrVar.vt){
case VT_BSTR:
{
if (gbUseCData)
{
//usr cdata
sCurTemp = "![CDATA[";
sCurTemp += SubStrReplace((std::string)OLE2T(vrVar.bstrVal), "]]>", "]]&");
sCurTemp += "]]>";
}
else
{
sCurTemp = OLE2T(vrVar.bstrVal);
sCurTemp = SubStrReplace(sCurTemp, "&", "&");
sCurTemp = SubStrReplace(sCurTemp, "<", "<");
sCurTemp = SubStrReplace(sCurTemp, ">", ">");
}
return sCurTemp;
break;
}
case VT_I2:
{
sTemp=vrVar.iVal;
_itoa(sTemp,cbuf,10);
return (std::string)cbuf;
break;
}
case VT_I4:
{
lTemp=vrVar.lVal;
_ltoa(lTemp,cbuf,10);
return (std::string)cbuf;
break;
}
case VT_R8:
{
sprintf(cbuf, "%0.5f", vrVar.dblVal);
return (std::string)cbuf;
}
case VT_R4:
sprintf(cbuf, "%e", (double)V_R4(&vrVar));
return (std::string)cbuf;
case VT_DECIMAL:
sprintf(cbuf, "%d", V_I4(&vrVar));
return (std::string)cbuf;
case VT_BOOL:
return ( V_BOOL(&vrVar) ? _T("TRUE") : _T("FALSE"));
case VT_DATE:
SYSTEMTIME sysTime;
VariantTimeToSystemTime(vrVar.date, &sysTime);
sprintf(cbuf, "%d-%d-%d %d:%d:%d", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
return (std::string)cbuf;
case VT_CY:
_i64toa( vrVar.cyVal.int64, cbuf, 10 );
sCurTemp = cbuf;
return sCurTemp.substr(0,sCurTemp.length() - 4) + (std::string)"." + sCurTemp.substr(sCurTemp.length() - 4, 4);
case (VT_ARRAY | VT_UI1):
{
//blob
int nTemp;
char **cBigPass = new char*;
if (VarToBuf(&vrVar, cBigPass, &nTemp))
{
sCurTemp = "![CDATA[";
sCurTemp += (std::string)(*cBigPass);
sCurTemp += "]]>";
free(cBigPass);
return sCurTemp;
}
else
return (std::string)"BINARY DATA";
}
default:return "";
}
}
catch(...)
{
LogEvent("VarToStr reports exception", 2);
}
return "";
}
///////////////////////////////////////////////////////
//
// Name: SubStrReplace
// Params: cOriginal - original string to search
// cSearch - char to search for
// cReplace - char to replace
// Returns: string with cSearch replaced by cReplace
// Description: Searches and replaces occurences of cSearch
// in cOriginal with cReplace
///////////////////////////////////////////////////////
std::string SubStrReplace(std::string cOriginal, char* cSearch, char* cReplace)
{
std::string sRet = cOriginal; //set return string
int nFind; //location of found string
try
{
//search for char string
nFind = sRet.find(cSearch, 0);
while (nFind >= 0)
{
sRet = sRet.substr(0, nFind) + (std::string)cReplace + sRet.substr(nFind + strlen(cSearch), sRet.length() - nFind);
nFind = sRet.find(cSearch, nFind + strlen(cReplace));
}
}
catch(...)
{
LogEvent("SubStrReplace reports an exception.", 2);
}
return sRet;
}
///////////////////////////////////////////////////////
//
// Name: InitializeGlobalVariables
// Params: none
// Returns: none
// Description: Re-reads registry and sets globals accordingly
///////////////////////////////////////////////////////
void InitializeGlobalVariables()
{
HKEY hRegKey; //for the registry
DWORD dwDataType; //registry data type
DWORD dwDataSize = 256; //registry data size
char szHostsAllowString[256] ; //connection string
char szDataString[256]; //event detail setting
char szHostsDenyString[256]; //DMS PAth setting
// Set host and port.
gnMaxThreadCount = 5;
gnSQLTimeout = 15;
gnThreadTimeout = 30;
gsHostsAllow = "*";
gsHostsDeny = "";
gbUseCData = FALSE;
gnUseMSDTD = 0; //default
gnDebugLevel = 1; //default
//get debug level
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SOFTWARE\\ODBCSocketServer",
0, KEY_READ, &hRegKey) == ERROR_SUCCESS)
{
if (RegQueryValueEx (hRegKey, "DebugLevel", NULL, &dwDataType, (LPBYTE) &szDataString, &dwDataSize) == ERROR_SUCCESS)
gnDebugLevel = atoi(szDataString);
dwDataSize = 256;