1 |
|
2 |
#include <winsock2.h> |
3 |
#include "stdint.h" |
4 |
#include "Flatline_Net.h" |
5 |
#include "Flatline_Win32.h" |
6 |
#include "Flatline_Client.h" |
7 |
#include "Mariusnet_Defs.h" |
8 |
#include "Daodan_Console.h" |
9 |
#include "Oni_Symbols.h" |
10 |
#include <stdio.h> |
11 |
|
12 |
#pragma comment(lib, "wininet.lib") |
13 |
|
14 |
|
15 |
char MariusLogin[32]={0}; |
16 |
unsigned char MariusPassword[16]={0}; |
17 |
|
18 |
m_room RoomList[64] = {0}; |
19 |
unsigned char RoomListGotten = 0; |
20 |
|
21 |
unsigned char MariusNet_LoggedIn = 0; |
22 |
|
23 |
unsigned char MsNet_Running = 0; |
24 |
int Marius_Socket = -1; |
25 |
int Room_Socket = -1; |
26 |
sockaddr_in RoomAddr = {0}; |
27 |
int PlayerID = 0; |
28 |
|
29 |
m_gameinfo StaticGameList[64] = {0}; |
30 |
enum |
31 |
{ |
32 |
glAddGame, |
33 |
glRemoveGame, |
34 |
glUpdateGame, |
35 |
}; |
36 |
|
37 |
/* |
38 |
|
39 |
RGBA green = {0, 0xFF, 0, 0}; |
40 |
RGBA red = {0, 0, 0xFF, 0}; |
41 |
|
42 |
RGBA grey = {0x80,0x80,0x80,0x80}; |
43 |
*/ |
44 |
extern RGBA red; |
45 |
//extern RGBA blue; |
46 |
RGBA blue = {0xFF, 0, 0, 0}; |
47 |
RGBA cyan = {0xFF, 0xFF, 0, 0}; |
48 |
RGBA white = {0xFF, 0xFF, 0xFF, 0}; |
49 |
extern RGBA green; |
50 |
extern RGBA grey; |
51 |
|
52 |
typedef struct |
53 |
{ |
54 |
uint16_t x; |
55 |
uint16_t y; |
56 |
|
57 |
} IMtPoint2D; |
58 |
void* DrawInstance = 0; |
59 |
void MSNet_DrawGames() |
60 |
{ |
61 |
|
62 |
if(!DrawInstance){ |
63 |
void* TSFFTahoma; |
64 |
TMrInstance_GetDataPtr( 'TSFF', "Tahoma", &TSFFTahoma); |
65 |
TSrContext_New( TSFFTahoma, 7, 1, 1, 0, &DrawInstance); |
66 |
} |
67 |
if(DrawInstance){ |
68 |
const int white = 0x00FFFFFF; |
69 |
const int green = 0x0000FF00; |
70 |
const int red = 0x00FF0000; |
71 |
const int blue = 0x000000FF; |
72 |
int i; |
73 |
char DrawString[255]; |
74 |
const int LineHeight = 15; |
75 |
IMtPoint2D StartLocation = {500, 20}; |
76 |
IMtPoint2D DrawLocation = StartLocation; |
77 |
TSrContext_SetShade(DrawInstance, white); |
78 |
TSrContext_DrawText(DrawInstance, "Mariusnet Games:", 255, 0, &DrawLocation); |
79 |
TSrContext_SetShade(DrawInstance, white); |
80 |
DrawLocation.y += LineHeight; |
81 |
|
82 |
for(i = 0; i < 64; i++) |
83 |
{ |
84 |
|
85 |
if(StaticGameList[i].gameID) |
86 |
{ |
87 |
DrawLocation.x = StartLocation.x - 10; |
88 |
sprintf(DrawString, "%i.", i ); |
89 |
TSrContext_DrawText(DrawInstance, DrawString, 255, 0, &DrawLocation); |
90 |
DrawLocation.x += 20; |
91 |
TSrContext_DrawText(DrawInstance, StaticGameList[i].g.Buffer, 255, 0, &DrawLocation); |
92 |
DrawLocation.y += LineHeight; |
93 |
} |
94 |
} |
95 |
|
96 |
} |
97 |
} |
98 |
|
99 |
|
100 |
int MariusNet_OutgoingPacketWrapper(int socket, const sockaddr* address, marius_packet* data, short datalen) |
101 |
{ |
102 |
data->header.PacketSize = htonl(datalen); |
103 |
//NetUDPSocket_Send( socket, address, (char*)data, sizeof(marius_header) ); |
104 |
//datalen -= sizeof(marius_header); |
105 |
//(char*)data += sizeof(marius_header); |
106 |
return NetUDPSocket_Send( socket, address, (char*)data, datalen); |
107 |
} |
108 |
|
109 |
int PacketCached = 0; |
110 |
int MariusNet_IncomingPacketWrapper(int socket, char buffer[], int bufferlength, int flags) |
111 |
{ |
112 |
int inSize = 0; |
113 |
unsigned fullSize = -1; |
114 |
static char cacheBuffer[1440] = {0}; |
115 |
unsigned char NeedMoreData = 0; |
116 |
if(PacketCached) |
117 |
{ |
118 |
unsigned int PacketSize = ntohl(((marius_packet*)cacheBuffer)->header.PacketSize); |
119 |
/* |
120 |
if(ntohs(((marius_packet*)cacheBuffer)->header.PacketSignature) != 0xDEAD) |
121 |
{ |
122 |
PacketCached = PacketSize = 0; |
123 |
NeedMoreData = 1; |
124 |
} |
125 |
*/ |
126 |
|
127 |
|
128 |
|
129 |
if(PacketCached >= PacketSize) |
130 |
{ |
131 |
memcpy( buffer, cacheBuffer, PacketSize ); |
132 |
PacketCached -= PacketSize; |
133 |
} |
134 |
else |
135 |
{ |
136 |
memcpy( buffer, cacheBuffer, PacketCached ); |
137 |
inSize += PacketCached; |
138 |
NeedMoreData = 1; |
139 |
PacketCached = 0; |
140 |
} |
141 |
if(PacketCached < 0) |
142 |
{ |
143 |
DDrConsole_PrintF("Warning, took too much from cache"); |
144 |
PacketCached = 0; |
145 |
} |
146 |
else if (PacketCached > 0)// && ntohs((short*)(cacheBuffer + PacketSize)) == 0xDEAD) |
147 |
{ |
148 |
memcpy( cacheBuffer, cacheBuffer + PacketSize, PacketCached ); |
149 |
} |
150 |
else |
151 |
{ |
152 |
memset(cacheBuffer, 0, 1440); |
153 |
} |
154 |
|
155 |
if(!NeedMoreData) return PacketSize; |
156 |
|
157 |
} |
158 |
do |
159 |
{ |
160 |
int tempSize = recv(socket, buffer + inSize, bufferlength, flags); |
161 |
if(tempSize == SOCKET_ERROR) |
162 |
{ |
163 |
NetCatchError(); |
164 |
return SOCKET_ERROR; |
165 |
} |
166 |
|
167 |
if(fullSize == -1) |
168 |
{ |
169 |
if(ntohs(((marius_packet*)buffer)->header.PacketSignature) != 0xDEAD) |
170 |
{ |
171 |
DDrConsole_PrintF("Bad header signature...closing Mariusnet connection"); |
172 |
return SOCKET_ERROR; |
173 |
} |
174 |
fullSize = ntohl(((marius_packet*)buffer)->header.PacketSize); |
175 |
} |
176 |
inSize += tempSize; |
177 |
|
178 |
} |
179 |
while(inSize < fullSize); |
180 |
|
181 |
//double packet madness |
182 |
if(inSize > fullSize |
183 |
&& ntohs(*(short*)(buffer + fullSize)) == 0xDEAD |
184 |
&& ntohl(((marius_packet*)(buffer + fullSize))->header.PacketSize) > 0 |
185 |
&& inSize - fullSize > sizeof(marius_header) |
186 |
) |
187 |
{ |
188 |
memcpy(cacheBuffer, buffer + fullSize, inSize - fullSize); |
189 |
|
190 |
PacketCached = inSize - fullSize; |
191 |
} |
192 |
else PacketCached = 0; |
193 |
return inSize; |
194 |
} |
195 |
|
196 |
|
197 |
sockaddr_in Marius_Server; |
198 |
|
199 |
|
200 |
unsigned char MariusNet_Initialize() |
201 |
{ |
202 |
hostent* host_info = 0; |
203 |
//host_info = gethostbyname("myth.mariusnet.com"); |
204 |
|
205 |
host_info = gethostbyname("metaserver.lhowon.org"); |
206 |
if(!host_info) |
207 |
{ |
208 |
DDrConsole_PrintF("Error, could not resolve myth.mariusnet.com"); |
209 |
return 1; |
210 |
} |
211 |
Marius_Server.sin_port = htons(6321); |
212 |
Marius_Server.sin_family = AF_INET; |
213 |
Marius_Server.sin_addr.S_un.S_addr = *(ULONG*)host_info->h_addr_list[0]; |
214 |
return 0; |
215 |
} |
216 |
|
217 |
void Initialize_MPacket( marius_packet* packet, short ID ) |
218 |
{ |
219 |
memset( packet, 0, sizeof(marius_packet) ); |
220 |
packet->header.PacketSignature = htons(0xDEAD); |
221 |
packet->header.PacketId = htons(ID); |
222 |
|
223 |
} |
224 |
|
225 |
void Initialize_LoginPacket( marius_packet* packet) |
226 |
{ |
227 |
int NameSize = 0; |
228 |
int TeamSize = 0; |
229 |
char (*test)[] = packet; |
230 |
Initialize_MPacket(packet, pt_PlayerLogin); |
231 |
|
232 |
packet->login.UpdateAppearance = 1; |
233 |
packet->login.Platform = htons(1); |
234 |
strncpy(packet->login.LoginId, MariusLogin, 32); |
235 |
packet->login.EncryptionType = htons(1); |
236 |
strcpy(packet->login.AppName, "MARATHON" ); //ONI ;) |
237 |
strncpy(packet->login.BuildDate, __DATE__, 32 ); |
238 |
strncpy(packet->login.BuildTime, __TIME__, 32 ); |
239 |
|
240 |
|
241 |
packet->login.PlayerInfo.clientVersion = htons(5000); |
242 |
NameSize = sprintf(packet->login.PlayerInfo.Name, MariusLogin); |
243 |
//TeamSize = sprintf(packet->login.PlayerInfo.Name + NameSize, "TCTF") + 1; |
244 |
|
245 |
packet->login.PlayerInfoSize = htons(40 + NameSize + TeamSize); |
246 |
} |
247 |
|
248 |
char EncryptionTypes[][32] = |
249 |
{ |
250 |
"Plaintext", |
251 |
"Braindead Simple", |
252 |
}; |
253 |
|
254 |
enum { |
255 |
SyntaxError, |
256 |
GamesNotAllowed, |
257 |
InvalidVersion, |
258 |
BadUserOrPassword, |
259 |
UserNotLoggedIn, |
260 |
BadMetaserverVersion, |
261 |
UserAlreadyLoggedIn, |
262 |
UnknownGameType, |
263 |
LoginSuccessful, |
264 |
LogoutSuccessful, |
265 |
PlayerNotInRoom, |
266 |
GameAlreadyExists, |
267 |
AccountAlreadyLoggedIn, |
268 |
RoomFull, |
269 |
AccountLocked, |
270 |
NotSupported |
271 |
}; |
272 |
|
273 |
static const char* sRoomNames[] = { |
274 |
"Crows Bridge", |
275 |
"Otter Ferry", |
276 |
"White Falls", |
277 |
"Silvermines", |
278 |
"Shoal", |
279 |
"Madrigal", |
280 |
"Tyr", |
281 |
"Ash", |
282 |
"Scales", |
283 |
"Covenant", |
284 |
"Muirthemne", |
285 |
"Seven Gates", |
286 |
"Bagrada", |
287 |
"The Barrier", |
288 |
"Forest Heart", |
289 |
"The Ermine", |
290 |
"The Dire Marsh", |
291 |
"The Highlands", |
292 |
"The Drowned Kingdom", |
293 |
"The Great Devoid", |
294 |
"Shiver", |
295 |
"The Caterthuns", |
296 |
"Soulblighter", |
297 |
"Balor", |
298 |
"Sons of Myrgard", |
299 |
"Heart of the Stone", |
300 |
"Arrival", |
301 |
"Ingue Ferroque", |
302 |
"Vimy Ridge", |
303 |
"Stilwell Road" |
304 |
}; |
305 |
void MSNet_LoginError_Output( int code ) |
306 |
{ |
307 |
switch(code) |
308 |
{ |
309 |
case(BadUserOrPassword): |
310 |
DDrConsole_PrintF( "Login denied: bad username or password." ); |
311 |
break; |
312 |
case(UserAlreadyLoggedIn): |
313 |
DDrConsole_PrintF( "Login denied: that user is already logged in."); |
314 |
break; |
315 |
case(AccountAlreadyLoggedIn): |
316 |
DDrConsole_PrintF( "Login denied: that account is already logged in."); |
317 |
break; |
318 |
case(RoomFull): |
319 |
DDrConsole_PrintF( "Login denied: room is full!?"); |
320 |
break; |
321 |
case(AccountLocked): |
322 |
DDrConsole_PrintF( "Login denied: your account is locked."); |
323 |
break; |
324 |
default: |
325 |
DDrConsole_PrintF("There was a problem connecting to the server" |
326 |
"that tracks Internet games. Please try again later."); |
327 |
break; |
328 |
} |
329 |
|
330 |
} |
331 |
|
332 |
void MSNet_HandleChat( m_message* msg ) |
333 |
{ |
334 |
//BGRA *cough* |
335 |
/*RGBA chatcolor; |
336 |
chatcolor.R = (msg->PrimaryColor.Red); |
337 |
chatcolor.G = (msg->PrimaryColor.Green); |
338 |
chatcolor.B = (msg->PrimaryColor.Blue); |
339 |
chatcolor.A = 0;*/ |
340 |
char Message[1024]; |
341 |
int NameLen = /*THIS MIGHT BE BAD*/ snprintf( Message, 1024, "%s", msg->Message ) + 1; |
342 |
/*THIS MIGHT BE BAD*/ snprintf( Message, 1024 - NameLen, "%s: %s", Message, msg->Message + NameLen ); |
343 |
//DDrConsole_PrintColored( msg->Message, 0, chatcolor,white ); |
344 |
DDrConsole_Print(Message); |
345 |
} |
346 |
|
347 |
void MSNet_SendChat( char* msg ) |
348 |
{ |
349 |
marius_packet mPacket = {0}; |
350 |
int len; |
351 |
if(!MsNet_Running || !MariusNet_LoggedIn) return; |
352 |
|
353 |
Initialize_MPacket(&mPacket, pt_ChatMessage); |
354 |
|
355 |
|
356 |
mPacket.message.SecondaryColor.Red = mPacket.message.SecondaryColor.Flags = -1; |
357 |
|
358 |
|
359 |
|
360 |
//mPacket.message.Message = 'Gumby'; |
361 |
len = sprintf(mPacket.message.Message, "O %s", msg ); |
362 |
mPacket.message.Message[1] = 0; |
363 |
mPacket.message.SenderId = htonl(PlayerID); |
364 |
//mPacket.message.TargetId = -1; |
365 |
MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, |
366 |
sizeof(marius_header) + sizeof(m_message) - (255 - len) ); |
367 |
|
368 |
|
369 |
} |
370 |
|
371 |
void MSNet_RoomExit() |
372 |
{ |
373 |
if(MariusNet_LoggedIn) |
374 |
{ |
375 |
if( Room_Socket != -1) closesocket(Room_Socket); |
376 |
memset(StaticGameList, 0, sizeof(m_gameinfo) * 64); |
377 |
MsNet_Running = 0; |
378 |
MariusNet_LoggedIn = 0; |
379 |
|
380 |
} |
381 |
return; |
382 |
} |
383 |
|
384 |
|
385 |
int MSNet_Cleanup() |
386 |
{ |
387 |
if( Marius_Socket != -1) closesocket(Marius_Socket); |
388 |
Marius_Socket = -1; |
389 |
if( Room_Socket != -1) closesocket(Room_Socket); |
390 |
Room_Socket = -1; |
391 |
memset(RoomList, 0, sizeof(m_room) * 64 ); |
392 |
memset(StaticGameList, 0, sizeof(m_gameinfo) * 64); |
393 |
DDrConsole_PrintF("The metaserver connection will now close." ); |
394 |
MsNet_Running = 0; |
395 |
MariusNet_LoggedIn = 0; |
396 |
return 1; |
397 |
} |
398 |
|
399 |
|
400 |
|
401 |
void MSNet_HandleGameList( m_gameinfo* PacketGames, int PacketSize ) |
402 |
{ |
403 |
m_gameinfo TempGameList[64] = {0}; |
404 |
int i, k; |
405 |
for( i = 0; PacketSize > 0; i++ ) |
406 |
{ |
407 |
int ThisSize = sizeof(m_gameinfo) + ntohs(PacketGames->len) - sizeof(m_gamedescription); |
408 |
|
409 |
memcpy(TempGameList + i,PacketGames, sizeof(m_gameinfo) - 128); |
410 |
|
411 |
/*THIS MIGHT BE BAD*/ snprintf( TempGameList[i].g.Buffer, 128, "%s", PacketGames->g.Buffer); |
412 |
PacketSize -= ThisSize; |
413 |
PacketGames = (m_gameinfo*)((char*)PacketGames + ThisSize); |
414 |
|
415 |
} |
416 |
for( i = 0; TempGameList[i].gameID; i++) |
417 |
{ |
418 |
int DesiredID = 0; |
419 |
switch(TempGameList[i].verb) |
420 |
{ |
421 |
case(glRemoveGame): |
422 |
//Remove game from list |
423 |
DDrConsole_PrintF("%s has ended", TempGameList[i].g.Buffer); |
424 |
for(k = 0; k < 64; k++) |
425 |
{ |
426 |
if(TempGameList[i].gameID == StaticGameList[k].gameID) |
427 |
{ |
428 |
memset(StaticGameList + k, 0, sizeof(m_gameinfo) ); |
429 |
break; |
430 |
} |
431 |
|
432 |
} |
433 |
break; |
434 |
|
435 |
case(glAddGame): |
436 |
//Add game to list |
437 |
DDrConsole_PrintF("%s has started", TempGameList[i].g.Buffer); |
438 |
case(glUpdateGame): |
439 |
//Update list |
440 |
|
441 |
//If updating, look for the game to update, otherwise look for empty |
442 |
if(TempGameList[i].verb) DesiredID = TempGameList[i].gameID; |
443 |
for(k = 0; k < 64; k++) |
444 |
{ |
445 |
if(DesiredID == StaticGameList[k].gameID) |
446 |
{ |
447 |
memcpy(StaticGameList + k, TempGameList + i, sizeof(m_gameinfo) ); |
448 |
break; |
449 |
} |
450 |
|
451 |
} |
452 |
break; |
453 |
break; |
454 |
} |
455 |
//TempGame |
456 |
} |
457 |
//Do stuff with the games. |
458 |
} |
459 |
|
460 |
int MSNet_CreateGame( m_announcegame* Game ) |
461 |
{ |
462 |
marius_packet mPacket; |
463 |
Initialize_MPacket(&mPacket, 104); |
464 |
mPacket.newgame = *Game; |
465 |
MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, |
466 |
sizeof(marius_header) + sizeof(m_announcegame)); |
467 |
} |
468 |
|
469 |
/* |
470 |
Initialize_MPacket(&mPacket, 104); |
471 |
mPacket.newgame.g.maxplayers = -1; |
472 |
mPacket.newgame.g.clientversion = htonl(0xc136e436); |
473 |
mPacket.newgame.g.maxteams = -1; |
474 |
//sprintf(mPacket.newgame.g.Buffer, "Oni!"); |
475 |
memcpy(mPacket.newgame.g.Buffer, "Blam!\0This is Oni, bitch.\0", 50); |
476 |
sent_bytes = MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, |
477 |
sizeof(marius_header) + sizeof(m_announcegame) + 50); |
478 |
*/ |
479 |
|
480 |
int MSNet_Room_Join( short Room, char* Key) |
481 |
{ |
482 |
marius_packet mPacket = {0}; |
483 |
|
484 |
char incomingData[1400] = {0}; |
485 |
marius_packet* incomingPacket = (marius_packet*)incomingData; |
486 |
int sent_bytes = 0; |
487 |
|
488 |
sockaddr_in SockAddr; |
489 |
RoomAddr.sin_port = RoomList[Room].Port; |
490 |
RoomAddr.sin_family = AF_INET; |
491 |
RoomAddr.sin_addr.S_un.S_addr = RoomList[Room].IPAddress; |
492 |
|
493 |
Room_Socket = NetTCPSocket_Create(RoomList[Room].Port, &SockAddr); |
494 |
|
495 |
DDrConsole_PrintF("Joining room %hi", Room); |
496 |
|
497 |
if(Room_Socket == -1) |
498 |
{ |
499 |
DDrConsole_PrintF("Failed to initialize room socket!"); |
500 |
return MSNet_Cleanup(); |
501 |
} |
502 |
|
503 |
if(connect( Room_Socket, (sockaddr *)&RoomAddr, sizeof(sockaddr_in))) |
504 |
{ |
505 |
NetCatchError(); |
506 |
MariusNet_LoggedIn = 0; |
507 |
return MSNet_Cleanup(); |
508 |
} |
509 |
|
510 |
Initialize_MPacket(&mPacket, pt_ChatRoomLogin); |
511 |
memcpy(mPacket.chatroom_join.RoomKey, Key, 32); |
512 |
strcpy(mPacket.chatroom_join.Name, MariusLogin); |
513 |
sent_bytes = MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, |
514 |
sizeof(marius_header) + 33 + strlen(MariusLogin)); |
515 |
|
516 |
|
517 |
|
518 |
Initialize_MPacket(&mPacket, pt_RoomPlayerInfo); |
519 |
mPacket.player_info.clientVersion = htons(5000); |
520 |
sprintf(mPacket.player_info.Name, MariusLogin); |
521 |
sent_bytes = MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, |
522 |
sizeof(marius_header) + 40 + strlen(MariusLogin)); |
523 |
|
524 |
if(MariusNet_IncomingPacketWrapper(Room_Socket, incomingData, 1400, 0) == SOCKET_ERROR) |
525 |
{ |
526 |
return MSNet_Cleanup(); |
527 |
} |
528 |
|
529 |
if(ntohs(incomingPacket->header.PacketId)== pt_LoginInfo) |
530 |
{ |
531 |
//DDrConsole_PrintF("Login failed: %s", incomingPacket->login_denied.DenialMessage ); |
532 |
MSNet_LoginError_Output(ntohl(incomingPacket->login_denied.code)); |
533 |
return MSNet_Cleanup(); |
534 |
} |
535 |
|
536 |
MariusNet_LoggedIn = 1; |
537 |
|
538 |
|
539 |
while(1) |
540 |
{ |
541 |
if(MariusNet_IncomingPacketWrapper(Room_Socket, incomingData, 1400, 0) == SOCKET_ERROR) |
542 |
{ |
543 |
return 0; |
544 |
} |
545 |
switch(ntohs(incomingPacket->header.PacketId)) |
546 |
{ |
547 |
case pt_RoomList: |
548 |
case pt_PlayerInfo: |
549 |
case pt_PlayerList: |
550 |
//Do nothing for now |
551 |
break; |
552 |
case pt_GameList: |
553 |
MSNet_HandleGameList( incomingPacket->gamelist.Games, |
554 |
ntohl(incomingPacket->header.PacketSize) - sizeof(marius_header)); |
555 |
break; |
556 |
case pt_BlueBarMsg: |
557 |
DDrConsole_PrintColored(incomingPacket->motd.Message, 0, cyan, grey ); |
558 |
break; |
559 |
case pt_ChatMessage: |
560 |
MSNet_HandleChat(&incomingPacket->message); |
561 |
break; |
562 |
case pt_KeepAlive: |
563 |
MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, incomingData, |
564 |
sizeof(marius_header)); |
565 |
break; |
566 |
default: |
567 |
DDrConsole_PrintF("Got packet of type %hi", ntohs(incomingPacket->header.PacketId)); |
568 |
break; |
569 |
} |
570 |
} |
571 |
|
572 |
return 0; |
573 |
} |
574 |
|
575 |
DWORD WINAPI MSNet_Handler(void* unused) |
576 |
{ |
577 |
int Port = 4156; |
578 |
|
579 |
|
580 |
char RoomToken[33] = {0}; |
581 |
int i = 0; |
582 |
|
583 |
|
584 |
|
585 |
sockaddr_in Marius_Addr = {0}; |
586 |
if(MsNet_Running) |
587 |
{ |
588 |
DDrConsole_PrintF("The metaserver handler is already running!"); |
589 |
} |
590 |
MsNet_Running = 1; |
591 |
|
592 |
if(MariusNet_LoggedIn) |
593 |
{ |
594 |
DDrConsole_PrintF("You're already logged in!"); |
595 |
return MSNet_Cleanup(); |
596 |
} |
597 |
|
598 |
NetPlatform_Initalize(); |
599 |
|
600 |
|
601 |
if(MariusNet_Initialize()) |
602 |
{ |
603 |
return MSNet_Cleanup(); |
604 |
} |
605 |
|
606 |
//Horribly set up. |
607 |
while( (Marius_Socket == -1 || Marius_Socket == 0 )&& Port < 6641) |
608 |
{ |
609 |
|
610 |
//Marius_Socket = NetTCPSocket_Create(Port, &Marius_Addr); |
611 |
//NetUDPSocket_Close( Marius_Socket ); |
612 |
Marius_Socket = NetTCPSocket_Create(Port, &Marius_Addr); |
613 |
//Port++; |
614 |
} |
615 |
|
616 |
if(Marius_Socket != -1) |
617 |
{ |
618 |
unsigned char incomingData[1400] = {0}; |
619 |
short incomingLength = 0; |
620 |
marius_packet* incomingPacket = &incomingData; |
621 |
//Initialize |
622 |
marius_packet mPacket; |
623 |
int sent_bytes; |
624 |
Initialize_LoginPacket(&mPacket); |
625 |
|
626 |
DDrConsole_PrintF("Logging into Mariusnet <%s>...", inet_ntoa(Marius_Server.sin_addr)); |
627 |
if(connect( Marius_Socket, &Marius_Server, sizeof(sockaddr_in))) |
628 |
{ |
629 |
NetCatchError(); |
630 |
return MSNet_Cleanup(); |
631 |
} |
632 |
DDrConsole_PrintF("Sending Player Info..."); |
633 |
sent_bytes = MariusNet_OutgoingPacketWrapper(Marius_Socket, (sockaddr*)&Marius_Server, (char*)&mPacket, |
634 |
sizeof(marius_header) + sizeof(m_player_login) - (sizeof(m_player_info) - ntohs(mPacket.login.PlayerInfoSize) ) ); |
635 |
/* |
636 |
if(!NetUDPSocket_Recieve(Marius_Socket, (sockaddr*)&Marius_Addr,incomingData, &incomingLength)) |
637 |
{ |
638 |
DDrConsole_PrintF("Data: %s Length %i", incomingData, incomingLength); |
639 |
return 0; |
640 |
} |
641 |
*/ |
642 |
if(MariusNet_IncomingPacketWrapper(Marius_Socket, incomingData, 1400, 0) == SOCKET_ERROR) |
643 |
{ |
644 |
return MSNet_Cleanup(); |
645 |
} |
646 |
|
647 |
if(ntohs(incomingPacket->header.PacketId)== pt_EncryptionKey) |
648 |
{ |
649 |
//recv( Marius_Socket, incomingData, 1440, 0); |
650 |
DDrConsole_PrintF("Encryption Type %s", EncryptionTypes[ntohs(incomingPacket->salt.EncryptionType)] ); |
651 |
|
652 |
Initialize_MPacket(&mPacket, pt_Password); |
653 |
if(incomingPacket->salt.EncryptionType) |
654 |
{ |
655 |
|
656 |
unsigned char* Hash = mPacket.password.passHash; |
657 |
unsigned char* Salt = incomingPacket->salt.salt; |
658 |
unsigned char Pass[16] = {0}; |
659 |
|
660 |
memset(Pass, 0x23, 16); |
661 |
strcpy(Pass, MariusPassword ); |
662 |
|
663 |
for( i = 0; i < 16; i++ ) Hash[i] = Pass[i]^Salt[i]; |
664 |
for( i = 1; i < 16; i++ ) Hash[i] = Hash[i]^Hash[i-1]; |
665 |
for( i = 1; i < 16; i++ ) |
666 |
{ |
667 |
short value = ~( Hash[i]*Hash[i-1] ); |
668 |
Hash[i] = (unsigned char) value; |
669 |
|
670 |
} |
671 |
} |
672 |
else |
673 |
{ |
674 |
strncpy(mPacket.password.passHash, MariusPassword, 16); |
675 |
} |
676 |
|
677 |
DDrConsole_PrintF("Sending Password..."); |
678 |
sent_bytes = MariusNet_OutgoingPacketWrapper(Marius_Socket, (sockaddr*)&Marius_Server, (char*)&mPacket, sizeof(marius_header) + sizeof(m_password) ); |
679 |
|
680 |
if(!NetUDPSocket_Recieve(Marius_Socket, (sockaddr*)&Marius_Addr,incomingData, &incomingLength)) |
681 |
{ |
682 |
return MSNet_Cleanup(); |
683 |
} |
684 |
DDrConsole_PrintF("Password ACK!"); |
685 |
if(ntohs(incomingPacket->header.PacketId)== pt_PasswordAck) |
686 |
{ |
687 |
|
688 |
Initialize_MPacket(&mPacket, pt_Localization); |
689 |
mPacket.localization.one = htonl(1); |
690 |
mPacket.localization.two = htonl(2); |
691 |
mPacket.localization.three = htonl(3); |
692 |
mPacket.localization.zero = 0; |
693 |
sent_bytes = MariusNet_OutgoingPacketWrapper(Marius_Socket, (sockaddr*)&Marius_Server, (char*)&mPacket, sizeof(marius_header) + sizeof(m_localization) ); |
694 |
|
695 |
if(MariusNet_IncomingPacketWrapper(Marius_Socket, incomingData, 1400, 0) == SOCKET_ERROR) |
696 |
{ |
697 |
return MSNet_Cleanup(); |
698 |
} |
699 |
} |
700 |
} |
701 |
|
702 |
if(ntohs(incomingPacket->header.PacketId)== pt_UserLoggedIn) |
703 |
{ |
704 |
PlayerID = ntohl(incomingPacket->login_success.userID); |
705 |
//Not sure if this is a string or byte array, so I allocated 33 bytes, |
706 |
//so it always zero terminates. |
707 |
memcpy(RoomToken, incomingPacket->login_success.Token, 32); |
708 |
DDrConsole_PrintF("Logged into Mariusnet!"); |
709 |
//strcpy_s( player_name, 32, MariusLogin ); |
710 |
strncpy(player_name,MariusLogin,32); |
711 |
} |
712 |
else if(ntohs(incomingPacket->header.PacketId)== pt_LoginInfo) |
713 |
{ |
714 |
//DDrConsole_PrintF("Login failed: %s", incomingPacket->login_denied.DenialMessage ); |
715 |
MSNet_LoginError_Output(ntohl(incomingPacket->login_denied.code)); |
716 |
return MSNet_Cleanup(); |
717 |
} |
718 |
else |
719 |
{ |
720 |
return MSNet_Cleanup(); |
721 |
} |
722 |
|
723 |
|
724 |
do |
725 |
{ |
726 |
if(MariusNet_IncomingPacketWrapper(Marius_Socket, incomingData, 1400, 0) == SOCKET_ERROR) |
727 |
{ |
728 |
return MSNet_Cleanup(); |
729 |
} |
730 |
if(ntohs(incomingPacket->header.PacketId)== pt_RoomList) |
731 |
{ |
732 |
DDrConsole_PrintF("Got room list!"); |
733 |
RoomListGotten = 1; |
734 |
memcpy(RoomList, incomingPacket->roomlist.Rooms, |
735 |
ntohl(incomingPacket->header.PacketSize) - sizeof(marius_header)); |
736 |
} |
737 |
else if(ntohs(incomingPacket->header.PacketId)== pt_PlayerInfo) |
738 |
{ |
739 |
//What do we do with this?! |
740 |
DDrConsole_PrintF("Got player info!"); |
741 |
} |
742 |
else |
743 |
{ |
744 |
DDrConsole_PrintF("Invalid packet type %hi", ntohs(incomingPacket->header.PacketId)); |
745 |
return MSNet_Cleanup(); |
746 |
} |
747 |
} |
748 |
while(PacketCached || !RoomListGotten); |
749 |
|
750 |
for(i = 0; i < 64, RoomList[i].IPAddress != 0 ; i++) |
751 |
{ |
752 |
int IP = ntohl(RoomList[i].IPAddress); |
753 |
DDrConsole_PrintF("Room %i %s IP %s Port %hi Players %hi", |
754 |
i, |
755 |
sRoomNames[ntohs(RoomList[i].RoomIndex)], |
756 |
inet_ntoa(*(in_addr*)&IP), |
757 |
ntohs(RoomList[i].Port), |
758 |
ntohs(RoomList[i].PlayerCount) |
759 |
); |
760 |
} |
761 |
|
762 |
closesocket(Marius_Socket); |
763 |
|
764 |
MSNet_Room_Join( 0, RoomToken ); |
765 |
|
766 |
|
767 |
} |
768 |
return 0; |
769 |
} |
770 |
|
771 |
uint16_t ONICALL mnet_joingame(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], int* dontuse1, int* dontuse2, sl_arg* ret) |
772 |
{ |
773 |
int index = args[0].val.value_int32; |
774 |
if(index >= 0) |
775 |
{ |
776 |
FLcConnect(StaticGameList[index].ipAddress, StaticGameList[index].port); |
777 |
} |
778 |
return 0; |
779 |
} |
780 |
|
781 |
unsigned char MSNet_Login(char* username, char* password) |
782 |
{ |
783 |
/*THIS MIGHT BE BAD*/ snprintf(MariusLogin, 32, "%s", username); |
784 |
/*THIS MIGHT BE BAD*/ snprintf(MariusPassword, 16, "%s", password); |
785 |
CreateThread(NULL, 0, MSNet_Handler, NULL, 0, 0); |
786 |
|
787 |
return 0; |
788 |
} |
789 |
|