forked from g8bpq/linbpq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatHTMLConfig.c
815 lines (626 loc) · 21.9 KB
/
ChatHTMLConfig.c
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
/*
Copyright 2001-2018 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 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 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
#include "bpqchat.h"
extern char OurNode[10];
// Flags Equates
#define GETTINGUSER 1
#define GETTINGBBS 2
#define CHATMODE 4
#define GETTINGTITLE 8
#define GETTINGMESSAGE 16
#define CHATLINK 32 // Link to another Chat Node
#define SENDTITLE 64
#define SENDBODY 128
#define WAITPROMPT 256 // Waiting for prompt after message
extern char PassError[];
extern char BusyError[];
extern char NodeTail[];
extern BOOL APRSApplConnected;
extern char ChatConfigName[250];
extern char OtherNodesList[1000];
extern char ChatWelcomeMsg[1000];
extern USER *user_hd;
extern LINK *link_hd;
extern UCHAR BPQDirectory[260];
#define MaxSockets 64
extern ChatCIRCUIT ChatConnections[MaxSockets+1];
extern int NumberofChatStreams;
extern int SMTPMsgs;
extern int ChatApplNum;
extern int MaxChatStreams;
extern char Position[81];
extern char PopupText[251];
extern int PopupMode;
#include "httpconnectioninfo.h"
static struct HTTPConnectionInfo * SessionList; // active bbs config sessions
static struct HTTPConnectionInfo * AllocateSession(char Appl);
static struct HTTPConnectionInfo * FindSession(char * Key);
VOID ProcessUserUpdate(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
VOID ProcessMsgFwdUpdate(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
VOID SendConfigPage(char * Reply, int * ReplyLen, char * Key);
VOID ProcessConfUpdate(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
VOID ProcessUIUpdate(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
VOID SendUserSelectPage(char * Reply, int * ReplyLen, char * Key);
VOID SendFWDSelectPage(char * Reply, int * ReplyLen, char * Key);
int EncryptPass(char * Pass, char * Encrypt);
VOID ProcessFWDUpdate(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
VOID SendStatusPage(char * Reply, int * ReplyLen, char * Key);
VOID SendChatStatusPage(char * Reply, int * ReplyLen, char * Key);
VOID SendUIPage(char * Reply, int * ReplyLen, char * Key);
static VOID GetParam(char * input, char * key, char * value);
BOOL GetConfig(char * ConfigName);
VOID ProcessChatDisUser(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
int APIENTRY SessionControl(int stream, int command, int param);
int APIENTRY GetNumberofPorts();
int APIENTRY GetPortNumber(int portslot);
UCHAR * APIENTRY GetPortDescription(int portslot, char * Desc);
struct PORTCONTROL * APIENTRY GetPortTableEntryFromSlot(int portslot);
VOID SendHouseKeeping(char * Reply, int * ReplyLen, char * Key);
VOID SendWelcomePage(char * Reply, int * ReplyLen, char * Key);
VOID SaveWelcome(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Key);
VOID GetMallocedParam(char * input, char * key, char ** value);
VOID SaveMessageText(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
VOID SaveHousekeeping(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Key);
VOID SaveWP(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Key);
int SetupNodeMenu(char * Buff);
VOID SendFwdSelectPage(char * Reply, int * ReplyLen, char * Key);
VOID SendFwdDetails(struct HTTPConnectionInfo * Session, char * Reply, int * ReplyLen, char * Key);
VOID SendFwdMainPage(char * Reply, int * ReplyLen, char * Key);
VOID SaveFwdCommon(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
VOID SaveFwdDetails(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest);
char ** SeparateMultiString(char * MultiString);
VOID SendChatConfigPage(char * Reply, int * ReplyLen, char * Key);
VOID SaveChatInfo(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Key);
int rtlink (char * Call);
char * APIENTRY GetBPQDirectory();
VOID SaveChatConfigFile(char * Config);
BOOL GetChatConfig(char * Config);
char * GetTemplateFromFile(int Version, char * FN);
static char UNC[] = "";
static char CHKD[] = "checked=checked ";
static char sel[] = "selected";
char ChatSignon[] = "<html><head><title>BPQ32 Chat Server Access</title></head><body background=\"/background.jpg\">"
"<h3 align=center>BPQ32 Chat Server %s Access</h3>"
"<h3 align=center>Please enter Callsign and Password to access the Chat Server</h3>"
"<form method=post action=/Chat/Signon?Chat>"
"<table align=center bgcolor=white>"
"<tr><td>User</td><td><input type=text name=user tabindex=1 size=20 maxlength=50 /></td></tr>"
"<tr><td>Password</td><td><input type=password name=password tabindex=2 size=20 maxlength=50 /></td></tr></table>"
"<p align=center><input type=submit value=Submit /><input type=submit value=Cancel name=Cancel /></form>";
char ChatPage[] = "<html><head><title>%s's Chat Server</title></head>"
"<body background=\"/background.jpg\"><h3 align=center>BPQ32 Chat Node %s</h3><P>"
"<P align=center><table border=1 cellpadding=2 bgcolor=white><tr>"
"<td><a href=/Chat/ChatStatus?%s>Status</a></td>"
"<td><a href=/Chat/ChatConf?%s>Configuration</a></td>"
"<td><a href=/>Node Menu</a></td>"
"</tr></table>";
static char LostSession[] = "<html><body>"
"<form style=\"font-family: monospace; text-align: center;\" method=post action=/Chat/Lost?%s>"
"Sorry, Session had been lost<br><br> "
"<input name=Submit value=Restart type=submit> <input type=submit value=Exit name=Cancel><br></form>";
char * ChatConfigTemplate = NULL;
char * ChatStatusTemplate = NULL;
static int compare(const void *arg1, const void *arg2)
{
// Compare Calls. Fortunately call is at start of stuct
return _stricmp(*(char**)arg1 , *(char**)arg2);
}
int SendChatHeader(char * Reply, char * Key)
{
return sprintf(Reply, ChatPage, OurNode, OurNode, Key, Key);
}
void ProcessChatHTTPMessage(struct HTTPConnectionInfo * Session, char * Method, char * URL, char * input, char * Reply, int * RLen)
{
char * Conxtext = 0, * NodeURL;
int ReplyLen;
char * Key;
char Appl = 'M';
NodeURL = strtok_s(URL, "?", &Conxtext);
Key = Session->Key;
if (strcmp(Method, "POST") == 0)
{
if (_stricmp(NodeURL, "/Chat/Header") == 0)
{
*RLen = SendChatHeader(Reply, Session->Key);
return;
}
if (_stricmp(NodeURL, "/Chat/ChatConfig") == 0)
{
if (ChatConfigTemplate)
free(ChatConfigTemplate);
ChatConfigTemplate = GetTemplateFromFile(2, "ChatConfig.txt");
NodeURL[strlen(NodeURL)] = ' '; // Undo strtok
SaveChatInfo(Session, input, Reply, RLen, Key);
return ;
}
if (_stricmp(NodeURL, "/Chat/ChatDisSession") == 0)
{
ProcessChatDisUser(Session, input, Reply, RLen, Key);
return ;
}
// End of POST section
}
if ((_stricmp(NodeURL, "/chat/Chat.html") == 0) || (_stricmp(NodeURL, "/chat/Header") == 0))
{
*RLen = SendChatHeader(Reply, Session->Key);
return;
}
if ((_stricmp(NodeURL, "/Chat/ChatStatus") == 0) || (_stricmp(NodeURL, "/Chat/ChatDisSession") == 0))
{
if (ChatStatusTemplate)
free(ChatStatusTemplate);
ChatStatusTemplate = GetTemplateFromFile(1, "ChatStatus.txt");
SendChatStatusPage(Reply, RLen, Key);
return;
}
if (_stricmp(NodeURL, "/Chat/ChatConf") == 0)
{
if (ChatConfigTemplate)
free(ChatConfigTemplate);
ChatConfigTemplate = GetTemplateFromFile(2, "ChatConfig.txt");
SendChatConfigPage(Reply, RLen, Key);
return;
}
ReplyLen = sprintf(Reply, ChatSignon, OurNode, OurNode);
*RLen = ReplyLen;
}
static VOID GetParam(char * input, char * key, char * value)
{
char * ptr = strstr(input, key);
char Param[2048];
char * ptr1, * ptr2;
char c;
if (ptr)
{
ptr2 = strchr(ptr, '&');
if (ptr2) *ptr2 = 0;
strcpy(Param, ptr + strlen(key));
if (ptr2) *ptr2 = '&'; // Restore string
// Undo any % transparency
ptr1 = Param;
ptr2 = Param;
c = *(ptr1++);
while (c)
{
if (c == '%')
{
int n;
int m = *(ptr1++) - '0';
if (m > 9) m = m - 7;
n = *(ptr1++) - '0';
if (n > 9) n = n - 7;
*(ptr2++) = m * 16 + n;
}
else if (c == '+')
*(ptr2++) = ' ';
else
*(ptr2++) = c;
c = *(ptr1++);
}
*(ptr2++) = 0;
strcpy(value, Param);
}
}
static VOID GetCheckBox(char * input, char * key, int * value)
{
char * ptr = strstr(input, key);
if (ptr)
*value = 1;
else
*value = 0;
}
VOID SaveChatInfo(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Key)
{
int ReplyLen = 0;
char * input;
struct UserInfo * USER = NULL;
char Temp[80];
char Nodes[10000] = "";
char * ptr1, * ptr2;
input = strstr(MsgPtr, "\r\n\r\n"); // End of headers
if (input)
{
if (strstr(input, "Cancel=Cancel"))
{
*RLen = SendChatHeader(Reply, Session->Key);
return;
}
GetParam(input, "ApplNum=", Temp);
ChatApplNum = atoi(Temp);
GetParam(input, "Streams=", Temp);
MaxChatStreams = atoi(Temp);
GetParam(input, "nodes=", Nodes);
ptr1 = Nodes;
ptr2 = OtherNodesList;
// Now we just save with crlf in place
strcpy(OtherNodesList, Nodes);
/*
while (*ptr1)
{
if ((*ptr1) == 13)
{
*(ptr2++) = ' ';
ptr1 += 2;
}
else
*(ptr2++) = *(ptr1++);
}
*ptr2 = 0;
*/
GetParam(input, "Posn=", Position);
GetParam(input, "MapText=", PopupText);
GetParam(input, "welcome=", ChatWelcomeMsg);
// Replace cr lf in string with $W
ptr1 = ChatWelcomeMsg;
scan2:
ptr1 = strstr(ptr1, "\r\n");
if (ptr1)
{
*(ptr1++)='$'; // put in cr
*(ptr1++)='W'; // put in lf
goto scan2;
}
GetCheckBox(input, "PopType=Click", &PopupMode);
if (strstr(input, "Restart=Restart+Links"))
{
char * ptr1, * ptr2, * Context;
node_close();
Sleep(2);
// Dont call removelinks - they may still be attached to a circuit. Just clear header
link_hd = NULL;
// Set up other nodes list. rtlink messes with the string so pass copy
ptr2 = ptr1 = strtok_s(_strdup(OtherNodesList), "\r\n", &Context);
while (ptr1)
{
rtlink(ptr1);
ptr1 = strtok_s(NULL, "\r\n", &Context);
}
free(ptr2);
if (user_hd) // Any Users?
makelinks(); // Bring up links
}
if (strstr(input, "UpdateMap=Update+Map"))
{
char Msg[500];
int len;
len = sprintf(Msg, "INFO %s|%s|%d|\r", Position, PopupText, PopupMode);
if (len < 256)
Send_MON_Datagram(Msg, len);
}
SaveChatConfigFile(ChatConfigName);
GetChatConfig(ChatConfigName);
}
SendChatConfigPage(Reply, RLen, Key);
return;
}
VOID ProcessChatDisUser(struct HTTPConnectionInfo * Session, char * MsgPtr, char * Reply, int * RLen, char * Rest)
{
char * input;
char * ptr;
input = strstr(MsgPtr, "\r\n\r\n"); // End of headers
if (input)
{
ptr = strstr(input, "Stream=");
if (ptr)
{
int Stream = atoi(ptr + 7);
SessionControl(Stream, 2, 0);
}
}
SendChatStatusPage(Reply, RLen, Rest);
}
VOID SendChatConfigPage(char * Reply, int * ReplyLen, char * Key)
{
int Len;
char Nodes[10000];
char Text[1000];
char * ptr1, * ptr2;
// Replace spaces in Node List with CR/LF
ptr1 = OtherNodesList;
ptr2 = Nodes;
if (strchr(OtherNodesList, 13)) // New format maybe with connect scripts
{
// OtherNodesList alresdy has crlf
strcpy(Nodes, OtherNodesList);
}
else
{
while (*ptr1)
{
if ((*ptr1) == ' ')
{
*(ptr2++) = 13;
*(ptr2++) = 10;
ptr1++ ;
}
else
*(ptr2++) = *(ptr1++);
}
*ptr2 = 0;
}
// Replace " in Text with "
ptr1 = PopupText;
ptr2 = Text;
while (*ptr1)
{
if ((*ptr1) == '"')
{
*(ptr2++) = '&';
*(ptr2++) = 'q';
*(ptr2++) = 'u';
*(ptr2++) = 'o';
*(ptr2++) = 't';
*(ptr2++) = ';';
ptr1++ ;
}
else
*(ptr2++) = *(ptr1++);
}
*ptr2 = 0;
// Replace $W in Welcome Message with cr lf
ptr2 = ptr1 = _strdup(ChatWelcomeMsg);
scan:
ptr1 = strstr(ptr1, "$W");
if (ptr1)
{
*(ptr1++)=13; // put in cr
*(ptr1++)=10; // put in lf
goto scan;
}
Len = sprintf(Reply, ChatConfigTemplate,
OurNode, Key, Key, Key,
ChatApplNum, MaxChatStreams, Nodes, Position,
(PopupMode) ? UNC : CHKD,
(PopupMode) ? CHKD : UNC, Text, ptr2);
free(ptr2);
*ReplyLen = Len;
}
VOID SendChatStatusPage(char * Reply, int * ReplyLen, char * Key)
{
int Len = 0;
USER *user;
char * Alias;
char * Topic;
LINK *link;
char Streams[8192];
char Users[8192];
char Links[8192];
ChatCIRCUIT * conn;
int i = 0, n;
Users[0] = 0;
for (user = user_hd; user; user = user->next)
{
if ((user->node == 0) || (user->node->alias == 0))
Alias = "(Corrupt Alias)";
else
Alias = user->node->alias;
if ((user->topic == 0) || (user->topic->name == 0))
Topic = "(Corrupt Topic)";
else
Topic = user->topic->name;
Len += sprintf(&Users[Len], "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td></tr>",
user->call, Alias, user->name, Topic, (int)(time(NULL) - user->lastrealmsgtime), user->qth);
}
Links[0] = 0;
Len = 0;
for (link = link_hd; link; link = link->next)
{
if (link->flags & p_linked )
if (link->supportsPolls)
Len += sprintf(&Links[Len], "<tr><td>%s</td><td>Open   RTT %d</td></tr>", link->call, link->RTT);
else
Len += sprintf(&Links[Len], "<tr><td>%s</td><td>Open</td></tr>", link->call);
else if (link->flags & (p_linked | p_linkini))
Len += sprintf(&Links[Len], "<tr><td>%s</td><td>Connecting</td></tr>", link->call);
else if (link->flags & p_linkfailed)
Len += sprintf(&Links[Len], "<tr><td>%s</td><td>Connect failed</td></tr>", link->call);
else
Len += sprintf(&Links[Len], "<tr><td>%s</td><td>Idle</td></tr>", link->call);
}
Len = 0;
Streams[0] = 0;
for (n = 0; n < NumberofChatStreams; n++)
{
conn=&ChatConnections[n];
i = conn->BPQStream;
if (!conn->Active)
{
Len += sprintf(&Streams[Len], "<tr><td onclick= SelectRow(%d) id=cell_%d>Idle</td><td> </td><td> </td><td> </td><td> </td></tr>", i, i);
}
else
{
if (conn->Flags & CHATLINK)
{
if (conn->BPQStream > 64 || conn->u.link == 0)
Len += sprintf(&Streams[Len], "<tr><td onclick= SelectRow(%d) id=cell_%d>** Corrupt ChatLink **</td>"
"<td> </td><td> </td><td> </td><td> </td></tr>", i, i);
else
Len += sprintf(&Streams[Len], "<tr><td onclick='SelectRow(%d)' id='cell_%d'>"
"%s</td><td>%s</td><td>%d</td><td>%s</td><td>%d</td></tr>",
i, i, "Chat Link", conn->u.link->alias, conn->BPQStream,
"", conn->OutputQueueLength - conn->OutputGetPointer);
}
else
if ((conn->Flags & CHATMODE) && conn->topic)
{
Len += sprintf(&Streams[Len], "<tr><td onclick='SelectRow(%d)' id='cell_%d'>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%d</td></tr>",
i, i, conn->u.user->name, conn->u.user->call, conn->BPQStream,
conn->topic->topic->name, conn->OutputQueueLength - conn->OutputGetPointer);
}
else
{
if (conn->UserPointer == 0)
Len += sprintf(&Streams[Len], "Logging in");
else
{
Len += sprintf(&Streams[Len], "<tr><td onclick='SelectRow(%d)' id='cell_%d'>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%d</td></tr>",
i, i, conn->UserPointer->Name, conn->UserPointer->Call, conn->BPQStream,
"CHAT", conn->OutputQueueLength - conn->OutputGetPointer);
}
}
}
}
Len = sprintf(Reply, ChatStatusTemplate, OurNode, OurNode, Key, Key, Key, Streams, Users, Links);
*ReplyLen = Len;
}
static struct HTTPConnectionInfo * AllocateSession(char Appl)
{
int KeyVal;
struct HTTPConnectionInfo * Session = zalloc(sizeof(struct HTTPConnectionInfo));
if (Session == NULL)
return NULL;
KeyVal = (int)time(NULL);
sprintf(Session->Key, "%c%012X", Appl, KeyVal);
if (SessionList)
Session->Next = SessionList;
SessionList = Session;
return Session;
}
static struct HTTPConnectionInfo * FindSession(char * Key)
{
struct HTTPConnectionInfo * Session = SessionList;
while (Session)
{
if (strcmp(Session->Key, Key) == 0)
return Session;
Session = Session->Next;
}
return NULL;
}
#ifdef WIN32
static char PipeFileName[] = "\\\\.\\pipe\\BPQChatWebPipe";
static DWORD WINAPI InstanceThread(LPVOID lpvParam)
// This routine is a thread processing function to read from and reply to a client
// via the open pipe connection passed from the main loop. Note this allows
// the main loop to continue executing, potentially creating more threads of
// of this procedure to run concurrently, depending on the number of incoming
// client connections.
{
DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0;
BOOL fSuccess = FALSE;
HANDLE hPipe = NULL;
char Buffer[4096];
char OutBuffer[100000];
char * MsgPtr;
int InputLen = 0;
int OutputLen = 0;
struct HTTPConnectionInfo Session;
char URL[4096];
char * Context, * Method;
int n;
char * ptr;
// Debugprintf("InstanceThread created, receiving and processing messages.");
// The thread's parameter is a handle to a pipe object instance.
hPipe = (HANDLE) lpvParam;
// Read client requests from the pipe. This simplistic code only allows messages
// up to BUFSIZE characters in length.
n = ReadFile(hPipe, &Session, sizeof (struct HTTPConnectionInfo), &n, NULL);
fSuccess = ReadFile(hPipe, Buffer, 4096, &InputLen, NULL);
if (!fSuccess || InputLen == 0)
{
if (GetLastError() == ERROR_BROKEN_PIPE)
Debugprintf("InstanceThread: client disconnected.", GetLastError());
else
Debugprintf("InstanceThread ReadFile failed, GLE=%d.", GetLastError());
}
else
{
Buffer[InputLen] = 0;
MsgPtr = &Buffer[0];
strcpy(URL, MsgPtr);
ptr = strstr(URL, " HTTP");
if (ptr)
*ptr = 0;
Method = strtok_s(URL, " ", &Context);
ProcessChatHTTPMessage(&Session, Method, Context, MsgPtr, OutBuffer, &OutputLen);
WriteFile(hPipe, &Session, sizeof (struct HTTPConnectionInfo), &n, NULL);
WriteFile(hPipe, OutBuffer, OutputLen, &cbWritten, NULL);
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
}
return 1;
}
static DWORD WINAPI PipeThreadProc(LPVOID lpvParam)
{
BOOL fConnected = FALSE;
DWORD dwThreadId = 0;
HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
// The main loop creates an instance of the named pipe and
// then waits for a client to connect to it. When the client
// connects, a thread is created to handle communications
// with that client, and this loop is free to wait for the
// next client connect request. It is an infinite loop.
for (;;)
{
hPipe = CreateNamedPipe(
PipeFileName, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_BYTE | // message type pipe
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
4096, // output buffer size
4096, // input buffer size
0, // client time-out
NULL); // default security attribute
if (hPipe == INVALID_HANDLE_VALUE)
{
Debugprintf("CreateNamedPipe failed, GLE=%d.\n", GetLastError());
return -1;
}
// Wait for the client to connect; if it succeeds,
// the function returns a nonzero value. If the function
// returns zero, GetLastError returns ERROR_PIPE_CONNECTED.
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if (fConnected)
{
// Create a thread for this client.
hThread = CreateThread(
NULL, // no security attribute
0, // default stack size
InstanceThread, // thread proc
(LPVOID) hPipe, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (hThread == NULL)
{
Debugprintf("CreateThread failed, GLE=%d.\n", GetLastError());
return -1;
}
else CloseHandle(hThread);
}
else
// The client could not connect, so close the pipe.
CloseHandle(hPipe);
}
return 0;
}
BOOL CreateChatPipeThread()
{
DWORD ThreadId;
CreateThread(NULL, 0, PipeThreadProc, 0, 0, &ThreadId);
return TRUE;
}
static char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static char *dat[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static VOID FormatTime(char * Time, time_t cTime)
{
struct tm * TM;
TM = gmtime(&cTime);
sprintf(Time, "%s, %02d %s %3d %02d:%02d:%02d GMT", dat[TM->tm_wday], TM->tm_mday, month[TM->tm_mon],
TM->tm_year + 1900, TM->tm_hour, TM->tm_min, TM->tm_sec);
}
#endif