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