-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathX360WebServer.cpp
More file actions
869 lines (728 loc) · 24 KB
/
X360WebServer.cpp
File metadata and controls
869 lines (728 loc) · 24 KB
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
//--------------------------------------------------------------------------------------
//[QBS]Quadrant i took SimpleSocket sample project from xbox xdk gutted out the main function replacing
// it with my html server as i do not yet know how to create projects from scratch on xbox360
//I used Release_LTCG
// Used with MSVC 2010
//--------------------------------------------------------------------------------------
#include <xtl.h>
#include <xbdm.h>
#include <malloc.h>
#include <stdio.h>
#include <winsockx.h>
#include "AtgConsole.h"
#include "AtgInput.h"
#include "AtgUtil.h"
#include "X360WebServer.h"//[QBS]
#include <malloc.h>
#include <xonline.h>
#include <xaudio2.h>
#include <xhv2.h>
#include <process.h>
#include <stdlib.h>
#include <string.h>
// use stl to manage remote console data
#include <list>
#include <set>
#pragma warning(disable:4127 4244 4702 4996) // we use some infinite loops, disable "conditional expression constant" warning
//--------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------
const UINT NONCE_SIZE = 8; // 8-byte nonce for recognition of
// broadcast return
const UINT MAX_DATA_SIZE = 64; // maximum length of string data to send
const UINT MIN_DATA_SIZE = 8; // minimum length of string data to send
const DWORD MSG_FREQ_IN_MS = 1000; // time (in ms) between messages
const UINT MAX_SEEK_RETRY = 5; // number of times to seek before giving
// up and hosting
const USHORT PORT_NUM = 1000; // For maximum efficiency, all Xbox network
// traffic should be on port 1000
//--------------------------------------------------------------------------------------
// Message IDs
//--------------------------------------------------------------------------------------
enum MessageID
{
MessageID_Seeking, // broadcast: looking for a host
MessageID_Found, // broadcast: response to a SEEKING message
MessageID_Data // data message, demonstrating communication
};
struct SMessage
{
MessageID m_id;
};
struct SSeekingMessage : public SMessage
{
BYTE m_Nonce[NONCE_SIZE]; // the nonce is used so we recognize a reply
};
struct SFoundMessage : public SMessage
{
BYTE m_Nonce[NONCE_SIZE]; // nonce of the sender of the seeking message
XNADDR m_xnaddr; // XNADDR of the host
XNKID m_xnkid; // Key ID to use for secure communication
XNKEY m_xnkey; // Key to use for secure communication
};
struct SDataMessage : public SMessage
{
UINT m_uiSequenceNumber; // integer data
SIZE_T m_size; // message size (for verification)
CHAR m_strData[MAX_DATA_SIZE + 1]; // string data (variable length)
inline SIZE_T GetSize()
{
return
sizeof( m_id ) +
sizeof( m_uiSequenceNumber ) +
sizeof( m_size ) +
strlen( m_strData ) + 1;
}
};
union UMessage
{
SMessage m_Message;
SSeekingMessage m_Seeking;
SFoundMessage m_Found;
SDataMessage m_Data;
};
//--------------------------------------------------------------------------------------
// Globals
//--------------------------------------------------------------------------------------
SOCKET g_socket; // socket we use for transmitting/receiving
XNADDR g_xnaddr; // our own XNADDR
XNKID g_xnkid; // key ID for session
XNKEY g_xnkey; // key for session
DWORD g_dwLastSend; // tick count of last send
IN_ADDR g_sinPeer; // address of our peer
ATG::Console g_console; // console for output
UMessage g_message; // received message
//--------------------------------------------------------------------------------------
// Enumerated types
//--------------------------------------------------------------------------------------
enum SENDMESSAGE_TYPE
{
SM_PEER = FALSE,
SM_BROADCAST = TRUE
};
//--------------------------------------------------------------------------------------
// Declarations
//--------------------------------------------------------------------------------------
VOID Initialize( VOID );
VOID Shutdown( VOID );
#define MAX_BUFFER 8096//[qbs]1024 // Maximum buffer size to receive at once.
#define HTTP_PORT 80 // Port for the webserver...
WSADATA Data; // Storage..
SOCKADDR_IN ServerSockAddr,ClientSockAddr; // Socket address.
SOCKET ServerSocket; // The actual socket.
int RUNNING; // Flag that the server is running.
void http_request(void *); // prototype..
CRITICAL_SECTION serverLock;
void Socket_Bypass()
{
InitializeCriticalSection(&serverLock);//[QBS] is this for files or for network stuff ??
XNetStartupParams xnsp;
memset(&xnsp, 0, sizeof(xnsp));
xnsp.cfgSizeOfStruct = sizeof(XNetStartupParams);
xnsp.cfgFlags = XNET_STARTUP_BYPASS_SECURITY;
int err = XNetStartup(&xnsp);
XNADDR pxna;
while (XNetGetTitleXnAddr(&pxna) == 0);
WSADATA wsaData;
err = WSAStartup(MAKEWORD(2, 2), &wsaData);
ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
int setting = 1;
setsockopt(ServerSocket, SOL_SOCKET, 0x5801, (char*)(&setting), sizeof(int));
}
void print_client_addr(struct sockaddr_in addr)
{
/*
printf("Client: %d.%d.%d.%d\n",
addr.sin_addr.s_addr & 0xff,
(addr.sin_addr.s_addr & 0xff00) >> 8,
(addr.sin_addr.s_addr & 0xff0000) >> 16,
(addr.sin_addr.s_addr & 0xff000000) >> 24);
*/
//[QBS]21-05-2021 Print Client IP to console
g_console.Format( "Client: %d.%d.%d.%d\n",
(addr.sin_addr.s_addr & 0xff000000) >> 24,
(addr.sin_addr.s_addr & 0xff0000) >> 16,
(addr.sin_addr.s_addr & 0xff00) >> 8,
addr.sin_addr.s_addr & 0xff);
}
//--------------------------------------------------------------------------------------
// Main
//
// Main game loop; drive the state machine
//--------------------------------------------------------------------------------------
VOID __cdecl main()
{
int status;
int count;
int addrLen = sizeof(SOCKADDR_IN);
// SOCKADDR_IN ClientSockAddr;
SOCKET *ClientSocket;
DWORD threadID;
// Initialize the application
Initialize();
printf("\nHttp WebServer on port: %d\nVersion 1.2 Beta\nBuild: %s Time: %s\n\nCoded By: Quadrant2005\n\n",HTTP_PORT,__DATE__,__TIME__);
g_console.Format( "\nHttp WebServer on port: %d\nVersion 1.2 Beta\nBuild: %s Time: %s\n\nCoded By: Quadrant2005\n\n",HTTP_PORT,__DATE__,__TIME__);
//[QBS]s ethernet check
DWORD dwStatus = XNetGetEthernetLinkStatus();
int m_bIsOnline = ( dwStatus & XNET_ETHERNET_LINK_ACTIVE ) != 0;
if( !m_bIsOnline )
{
printf("NO ETHERNET LINK ACTIVE\n");
g_console.Format( "NO ETHERNET LINK ACTIVE\n");
}
else
{
printf("ETHERNET LINK ACTIVE\n");
g_console.Format( "ETHERNET LINK ACTIVE\n");
}
//[QBS]e
// Init windows sockets:
status = WSAStartup(MAKEWORD(1, 1), &Data);
if (status)
{
printf("ERROR: WSAStartup didn't work\n");
g_console.Format( "ERROR: WSAStartup didn't work\n");
// exit(1);
}
//
// Init some variables:
memset(&ServerSockAddr, 0, sizeof(ServerSockAddr));
count = 0;
RUNNING = 1; // Initially running =)
Socket_Bypass();//[QBS] needs sorting
EnterCriticalSection(&serverLock);//[QBS] is this for files or for network stuff ??
// Create the socket:
ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
if( ServerSocket == INVALID_SOCKET )
{
ATG::FatalError( "Failed to create socket, error %d.\n", WSAGetLastError() );
}
//
// Setup some socket info:
SOCKADDR_IN ServerSockAddr;
ServerSockAddr.sin_port = htons(HTTP_PORT); // Port of the socket.
ServerSockAddr.sin_family = AF_INET; // Socket is "internet" type.
ServerSockAddr.sin_addr.s_addr = htonl(INADDR_ANY); // default IP address.
if (ServerSocket == INVALID_SOCKET)
{
printf("ERROR: socket() unsucessful..\n");
g_console.Format( "ERROR: socket() unsucessful..\n");
status = WSACleanup();
if (status == SOCKET_ERROR)
printf("WSACleanup() failed!\n");
g_console.Format( "WSACleanup() failed!\n");
//exit(1);
}
//[QBS]s26-01-2021
// after setting these undocumented flags on a socket they should then run unencrypted
BOOL bBroadcast = TRUE;
if( setsockopt(ServerSocket, SOL_SOCKET, 0x5802, (PCSTR)&bBroadcast, sizeof(BOOL) ) != 0 )//PATCHED!
{
g_console.Format( "Failed to set socket to 5802, error");
// return 0;
}
if( setsockopt(ServerSocket, SOL_SOCKET, 0x5801, (PCSTR)&bBroadcast, sizeof(BOOL) ) != 0 )//PATCHED!
{
g_console.Format( "Failed to set socket to 5801, error");
// return 0;
}
//[QBS]e26-01-2021
//
// Attach the socket to the local machine:
if (bind(ServerSocket, (LPSOCKADDR) &ServerSockAddr, sizeof(ServerSockAddr)) != 0 )
{
// ATG::FatalError( "Failed to bind socket, error %d.\n", WSAGetLastError() );
printf("ERROR: bind() failed!\n");
g_console.Format( "ERROR: bind() failed!\n");
status = WSACleanup();
if (status == SOCKET_ERROR)
{
printf("WSACleanup() failed!\n");
g_console.Format( "WSACleanup() failed!\n");
}
}
// Tell the socket to "listen" for incoming connections!
status = listen(ServerSocket, 1);
if (status == SOCKET_ERROR)
{
printf("ERROR: listen() failed!\n");
g_console.Format( "ERROR: listen() failed!\n");
status = WSACleanup();
if (status == SOCKET_ERROR)
printf("WSACleanup() failed!\n");
g_console.Format( "WSACleanup() failed!\n");
// exit(1);
}
//
//[QBS]s26-01-2021
XNADDR addr;
XNetGetTitleXnAddr(&addr);
char ip[16];
sprintf_s(ip, 16, "%d.%d.%d.%d", (byte)addr.ina.S_un.S_un_b.s_b1,
(byte)addr.ina.S_un.S_un_b.s_b2,
(byte)addr.ina.S_un.S_un_b.s_b3,
(byte)addr.ina.S_un.S_un_b.s_b4
);
// g_console.Format("HTTPServer", "XNetGetTitleXnAddr returned %s",ip);
// Now wait for incoming connections and handle them when needed:
printf("Html Server started! %s:%i\n",ip,HTTP_PORT);
g_console.Format( "Html Server started! %s:%i\n",ip,HTTP_PORT);
printf("------------------------\n");
g_console.Format( "------------------------\n");
while (RUNNING)
{
// Accept a connection when one comes in. Waits here if no incoming
// connections are pending:
ClientSocket = (SOCKET *) malloc(sizeof(SOCKET));
if (!ClientSocket)
{
printf("ERROR: out of memory!\n");
g_console.Format( "ERROR: out of memory!\n");
closesocket(ServerSocket);
status = WSACleanup();
if (status == SOCKET_ERROR)
printf("WSACleanup() failed!\n");
g_console.Format( "WSACleanup() failed!\n");
// exit(1);
}
*ClientSocket = accept(ServerSocket, (LPSOCKADDR) &ClientSockAddr, &addrLen);
// printf("cl->ip %s\n",inet_ntoa(ClientSockAddr.sin_addr));
// Log_Printf("cl->ip %s\n",inet_ntoa(ClientSockAddr.sin_addr));
print_client_addr (ClientSockAddr);//[QBS]17-01-2021
if (*ClientSocket == INVALID_SOCKET)
{//[QBS]added
printf("ERROR: accept() failed!\n");
g_console.Format( "ERROR: accept() failed!\n");
}//[QBS]added
else
{
// Otherwise we have a valid connection, so SERVE it!
threadID = _beginthread(http_request, 0, (VOID *) ClientSocket);
if (threadID == -1)
{
printf("ERROR: _beginthread() failed!\n");
g_console.Format( "ERROR: _beginthread() failed!\n");
status = closesocket(*ClientSocket);
if (status == SOCKET_ERROR)
printf("ERROR: closesocket() failed!\n");
g_console.Format( "ERROR: closesocket() failed!\n");
}
// sleep(1);
// Get ready for next request...
if (ClientSocket)
ClientSocket = NULL;
}
}
closesocket(ServerSocket);
status = WSACleanup();
if (status == SOCKET_ERROR)
printf("WSACleanup() failed!\n");
LeaveCriticalSection(&serverLock);//[QBS] is this for files or for network stuff ??
printf("------------------------\n");
g_console.Format( "------------------------\n");
printf("Html Server shutdown.\n");
g_console.Format( "Html Server shutdown.\n");
Shutdown();
}
//--------------------------------------------------------------------------------------
// Initialize
//
// Set up SNL and socket
//--------------------------------------------------------------------------------------
VOID Initialize( VOID )
{
// Initialize the console
HRESULT hr = g_console.Create( "game:\\Media\\Fonts\\Arial_12.xpr", 0xff0000ff, 0xffffffff );
if( FAILED( hr ) )
{
ATG::FatalError( "Console initialization failed.\n" );
}
g_console.Format( "*** INITIALIZING ***\n" );
// Start up the SNL with default initialization parameters
/*//[QBS]
if( XNetStartup( NULL ) != 0 )
{
ATG::FatalError( "XNetStartup failed.\n" );
}
*/
// Start up Winsock
WORD wVersion = MAKEWORD( 2, 2 ); // request version 2.2 of Winsock
WSADATA wsaData;
INT err = WSAStartup( wVersion, &wsaData );
if( err != 0 )
{
ATG::FatalError( "WSAStartup failed, error %d.\n", err );
}
// Verify that we got the right version of Winsock
if( wsaData.wVersion != wVersion )
{
ATG::FatalError( "Failed to get proper version of Winsock, got %d.%d.\n",
LOBYTE( wsaData.wVersion ), HIBYTE( wsaData.wVersion ) );
}
/*
// Initialize the socket
g_socket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if( g_socket == INVALID_SOCKET )
{
ATG::FatalError( "Failed to create socket, error %d.\n", WSAGetLastError() );
}
// Bind the socket
SOCKADDR_IN sa;
sa.sin_family = AF_INET; // IP family
sa.sin_addr.s_addr = INADDR_ANY; // Use the only IP that's available to us
sa.sin_port = htons( PORT_NUM ); // Port (should be 1000)
if( bind( g_socket, ( const sockaddr* )&sa, sizeof( sa ) ) != 0 )
{
ATG::FatalError( "Failed to bind socket, error %d.\n", WSAGetLastError() );
}
// Set the socket to nonblocking
unsigned long iUnblocking = 1;
if( ioctlsocket( g_socket, FIONBIO, &iUnblocking ) != 0 )
{
ATG::FatalError( "Failed to set socket to nonblocking, error %d\n", WSAGetLastError() );
}
// Permit the socket to send broadcasts
BOOL bBroadcast = TRUE;
if( setsockopt(
g_socket, SOL_SOCKET, SO_BROADCAST, ( PCSTR )&bBroadcast, sizeof( BOOL ) ) != 0 )
{
ATG::FatalError( "Failed to set socket to broadcast-capable, error %d\n",
WSAGetLastError() );
}
// Get our own XNADDR
DWORD dwRet;
do
{
dwRet = XNetGetTitleXnAddr( &g_xnaddr );
} while( dwRet == XNET_GET_XNADDR_PENDING );
if( dwRet & XNET_GET_XNADDR_NONE )
{
ATG::FatalError( "Unable to find an XNADDR.\n" );
}
g_console.Format( "XNADDR: %02X:%02X:%02X:%02X:%02X:%02X\n",
g_xnaddr.abEnet[0], g_xnaddr.abEnet[1], g_xnaddr.abEnet[2],
g_xnaddr.abEnet[3], g_xnaddr.abEnet[4], g_xnaddr.abEnet[5] );
*/
return;
}
//--------------------------------------------------------------------------------------
// Shutdown
//
// Tear everything down
//--------------------------------------------------------------------------------------
VOID Shutdown( VOID )
{
g_console.Format( "*** SHUTTING DOWN ***\n" );
// Close our socket, if any
if( g_socket != INVALID_SOCKET )
{
closesocket( g_socket );
g_socket = INVALID_SOCKET;
}
// Terminate Winsock
WSACleanup();
// Terminate the SNL
XNetCleanup();
return;
}
//################################################################################
//
// This is the thread function that handles the client's request:
CRITICAL_SECTION fileLock;
void http_request(void *ptr)
{
char buf[MAX_BUFFER],
buf2[MAX_BUFFER];
int status,
count;
char tmp,
*p,
*q;
SOCKET *ClientSocket = (SOCKET *) ptr;
FILE *H;
int sent_test_page=0;
int outchars;
// Init the variables:
status = count = 0;
memset(buf, 0, MAX_BUFFER);
InitializeCriticalSection(&fileLock);//[QBS] XBOX SECURITY FILE ACCESS
EnterCriticalSection(&fileLock);//[QBS] XBOX SECURITY FILE ACCESS
// 1st, get the requested filename/path:
count = recv(*ClientSocket, buf, sizeof(buf), 0);
if (!count || count == SOCKET_ERROR)
{
printf("Connection terminated early\n");
g_console.Format( "Connection terminated early\n" );
status = shutdown(*ClientSocket, 2);
if (status == SOCKET_ERROR)
{
printf("Shutdown of client unsuccessful\n");
g_console.Format( "Shutdown of client unsuccessful\n");
status = closesocket(*ClientSocket);
}
if (status == SOCKET_ERROR)
printf("closesocket() unsuccessful\n");
g_console.Format( "closesocket() unsuccessful\n" );
free(ClientSocket);
return;
}
//[QBS]start
printf("clients details: %s\n", buf);
g_console.Format( "clients details: %s\n", buf);
//[QBS]end
// chop at the first newline character for simplicity...
for (p = buf; p && *p != '\n'; p++);
if (*p == '\n')
*p = '\0'; // terminate the string early =)
// printf("request for: %s\n", buf+6);
// Log_Printf ("request for: %s\n", buf+6);
printf("request for: %s\n", buf);
g_console.Format( "request for: %s\n", buf);
//##########TEST########
// return;
//######################
//
// Now ideally here you would check to see what the client accepts
// and what HTTP protocol version the client is using, but to keep this
// short, i'm gonna' assume we are using HTTP/1.0
//
// A request line should now look like this...
//
// "GET /filename HTTP/1.0"
//
// So we must chop some junk out of there...
p = strstr(buf, "GET");
if (!p)
{
printf("Invalid request line %s!\n", buf);
g_console.Format( "Invalid request line %s!\n", buf);
closesocket(*ClientSocket);
free(ClientSocket);
return;
}
p += (strlen("GET") + 1); // move to the filename
//[QBS] if (*p == '/')
if (*p == '/' || *p == '\\' )
p++; // we dont want this char either..
//
// Ok, now we just have to chop off at the end of the filename:
q = strstr(p, "HTTP");
/*
if (!q)
{
printf("Invalid request line %s!\n", buf);
status = shutdown(*ClientSocket, 2);
if (status == SOCKET_ERROR)
printf("Shutdown of client unsuccessful\n");
status = closesocket(*ClientSocket);
if (status == SOCKET_ERROR)
printf("closesocket() unsuccessful\n");
free(ClientSocket);
return;
}
*/
q--; // we really wanna' back up one to zap
*q = '\0'; // the trailing space...
//##########TEST########
// return;
//######################
//
// Now we are ready for busines... so open the file!
printf("file: %s\n", p); // uncomment if you wanna' see the filename
g_console.Format( "file: %s\n", p);
//[QBS] Filter out these extention types
if (strcmp(q-3,"sys")==0)
{
printf ("FILE TYPE NOT ALLOWED\n");
g_console.Format( "FILE TYPE sys NOT ALLOWED\n");
return;
}
if (strcmp(q-3,"exe")==0)
{
printf ("FILE TYPE NOT ALLOWED\n");
g_console.Format( "FILE TYPE exe NOT ALLOWED\n");
return;
}
if (strcmp(q-3,"xex")==0)
{
printf ("FILE TYPE NOT ALLOWED\n");
g_console.Format( "FILE TYPE xex NOT ALLOWED\n");
return;
}
if (strcmp(q-3,"com")==0)
{
printf ("FILE TYPE NOT ALLOWED\n");
g_console.Format( "FILE TYPE com NOT ALLOWED\n");
return;
}
if (strcmp(q-3,"bat")==0)
{
printf ("FILE TYPE NOT ALLOWED\n");
g_console.Format( "FILE TYPE bat NOT ALLOWED\n");
return;
}
if (strcmp(q-3,"txt")==0)
{
printf ("FILE TYPE NOT ALLOWED\n");
g_console.Format( "FILE TYPE txt NOT ALLOWED\n");
return;
}
//##########
outchars = strlen(buf);//get length (includes carriage return)
//debug info
if (outchars)
{
printf ("cl->svr [%s] in buf [%d] chars\n",p,outchars-5);
g_console.Format( "cl->svr [%s] in buf [%d] chars\n",p,outchars-5);
}
if (strcmp(q-3,"?ip")==0)//works
{
memset(buf, 0, sizeof(buf));
XNADDR addr;
XNetGetTitleXnAddr(&addr);
char serverip[16];
sprintf_s(serverip, 16, "%d.%d.%d.%d", (byte)addr.ina.S_un.S_un_b.s_b1,
(byte)addr.ina.S_un.S_un_b.s_b2,
(byte)addr.ina.S_un.S_un_b.s_b3,
(byte)addr.ina.S_un.S_un_b.s_b4
);
sprintf_s(buf, "<HTML><HEAD><TITLE>TEST SEND PAGE</TITLE>%s",serverip);
send(*ClientSocket, buf, strlen(buf)+1, 0);
sent_test_page++;
return;
}
//########
//########
return;//DISABLE NORMAL HTTP SERVER RESPONSES (Serving files from the xbox still seems to need something adding mmm...
//########
if (!strlen(p)) // Then we have a request for the "default"
p = "index.html"; // html file. This varies from server to
// p = "Hdd:\\DEMOS\\index.html"; // html file. This varies from server to
// server, but I'm gonna' use index.html as
// my default file. In a real package this
// would be yet another option in a config
// file.
//######START
//Hdd:\\QuadKill\\scripts.rpf
/*
FILE *fp;
int c;
char buffer[1024];
// EnterCriticalSection(&Read);
fp = fopen("Hdd:\\logs\\test.txt","r");
while(1) {
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}
*/
//#######END
//FILE* f ;
//fopen_s(&f,("hdd1:\\demos\\test.png"),"wb");
//fwrite(buff.GetData(),(size_t)buff.GetDataLength(),1,f);
//fclose(f);
H = fopen(p, "rb");//read binary
errno_t err;
err = fopen_s(&H,p, "rb");//read binary
/*
if( err == 0 )
{
printf( "The file 'crt_fopen_s.c' was opened\n" );
}
else
{
printf( "The file 'crt_fopen_s.c' was not opened\n" );
}*/
// if (!H)
if (!err)
{
// The file doesn't exist! so warn the client:
memset(buf2, 0, MAX_BUFFER);
//
//[QBS]
if (!sent_test_page)
{
sprintf_s(buf2, "\n<BODY bgColor=#ffffff>\n<TABLE border=0 width=600>\n<TBODY>\n<TR>\n<TD><IMG height=113 src='headbang.gif' width=113></TD><TD><TABLE border=0><TBODY><TR><TD><FONT face='Verdana, Arial, Helvetica, sans-serif' size=5><IMG height=20 src='404Error.gif' width=344></FONT></TD></TR><TR><TD height=25><FONT face='Verdana, Arial, Helvetica, sans-serif' size=2><B>The page (%s) you requested does not exist<BR></B></FONT></TD></TR><TR><TD><FONT face='Verdana, Arial, Helvetica, sans-serif' size=2><B>It may have been renamed or moved or the page's address was mistyped. You should <A href='http://www.quake2.tzo.com/'>go to our Home Page</A> and look for the information you need from there.</B></FONT></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></BODY></HTML>",p);
sent_test_page=0;
}
//[QBS]end
//
// Now send the data:
count = send(*ClientSocket, buf2, strlen(buf2)+1, 0);
if (count != (int)(strlen(buf2)+1))
{
printf("Connection terminated.\n");
g_console.Format( "Connection terminated.\n");
status = shutdown(*ClientSocket, 2);
if (status == SOCKET_ERROR)
{
printf("Shutdown of client unsuccessful\n");
g_console.Format( "Shutdown of client unsuccessful\n");
//
status = closesocket(*ClientSocket);
}
if (status == SOCKET_ERROR)
{
printf("closesocket() unsuccessful\n");
g_console.Format( "closesocket() unsuccessful\n");
//
free(ClientSocket);
}
//
return;
}
}
else
{
// Now send the data:
while(!feof(H))
{
tmp = fgetc(H); // Stupid browsers (or protocol?) seem to want
// their data spoon fed, 1 byte at a time.
if (!feof(H))
{
count = send(*ClientSocket, &tmp, 1, 0);
if (count != 1)
{
printf("Connection terminated.\n");
g_console.Format( "Connection terminated.\n");
fclose(H);
status = shutdown(*ClientSocket, 2);
if (status == SOCKET_ERROR)
{
printf("Shutdown of client unsuccessful\n");
g_console.Format( "Shutdown of client unsuccessful\n");
status = closesocket(*ClientSocket);
}
if (status == SOCKET_ERROR)
{
printf("closesocket() unsuccessful\n");
g_console.Format( "closesocket() unsuccessful\n");
free(ClientSocket);
}
return;
}
}
}
fclose(H);
LeaveCriticalSection(&fileLock);//XBOX SECURITY FILE ACCESS
}
//
// We are done, so close the socket and free the memory:
status = shutdown(*ClientSocket, 2);
if (status == SOCKET_ERROR)
{
printf("Shutdown of client unsuccessful\n");
g_console.Format( "Shutdown of client unsuccessful\n");
status = closesocket(*ClientSocket);
}
if (status == SOCKET_ERROR)
{
printf("closesocket() unsuccessful\n");
g_console.Format( "closesocket() unsuccessful\n");
free(ClientSocket);
}
}