| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * $Logfile: /DescentIII/Main/mtclient/mtpilottracker.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:58:40 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Pilot tracker client code |
| 26 | * |
| 27 | * $Log: not supported by cvs2svn $ |
| 28 | * |
| 29 | * 15 10/28/99 3:13p Kevin |
| 30 | * fixed major badness with byte ordering 'fix' |
| 31 | * |
| 32 | * 14 9/02/99 3:16p Kevin |
| 33 | * Macintosh byte ordering fixes |
| 34 | * |
| 35 | * 13 9/01/99 5:24p Kevin |
| 36 | * Macintosh byte ordering problems |
| 37 | * |
| 38 | * 12 7/21/99 9:11a Kevin |
| 39 | * bandwidth reduction act #1 |
| 40 | * |
| 41 | * 11 4/21/99 11:15a Kevin |
| 42 | * sectret code to tell the differences between oem, demo and retail |
| 43 | * builds on the tracker |
| 44 | * |
| 45 | * 10 4/19/99 8:36p Jeff |
| 46 | * Compile under Linux |
| 47 | * |
| 48 | * 9 3/02/99 5:50p Kevin |
| 49 | * Ouch. Duplicate structures existed and were conflicting. |
| 50 | * |
| 51 | * 8 1/11/99 6:02p Kevin |
| 52 | * Fixed warning |
| 53 | * |
| 54 | * 7 12/23/98 6:38p Kevin |
| 55 | * All UDP data (except gamespy) now uses one (registered) port number |
| 56 | * |
| 57 | * 6 11/03/98 9:27a Kevin |
| 58 | * Added PXO message of the day |
| 59 | * |
| 60 | * 5 10/30/98 11:25a Kevin |
| 61 | * Message of the day for PXO |
| 62 | * |
| 63 | * 4 8/31/98 10:14a Kevin |
| 64 | * Misc. multi-UI fixes |
| 65 | * |
| 66 | * 3 8/25/98 6:33p Kevin |
| 67 | * PXO screens |
| 68 | * |
| 69 | * 2 6/01/98 10:10a Kevin |
| 70 | * Added DLL connection interface and auto update DLL |
| 71 | * |
| 72 | * 1 5/25/98 4:12p Kevin |
| 73 | * |
| 74 | * 10 5/14/98 6:08p Kevin |
| 75 | * fixed encryption bug |
| 76 | * |
| 77 | * 9 5/14/98 5:19p Kevin |
| 78 | * Encrypted pilot data |
| 79 | * |
| 80 | * 8 5/07/98 9:47a Kevin |
| 81 | * Made pilot reading more robust |
| 82 | * |
| 83 | * 7 5/04/98 1:22p Kevin |
| 84 | * Mastertracker |
| 85 | * |
| 86 | * 6 5/04/98 12:53p Kevin |
| 87 | * Fixed mastertracker resend stuff |
| 88 | * |
| 89 | * 5 5/01/98 12:28p Kevin |
| 90 | * Added new user registration button for MT |
| 91 | * |
| 92 | * 4 4/30/98 3:50p Kevin |
| 93 | * Mastertracker pilot stats |
| 94 | * |
| 95 | * 3 4/28/98 12:51p Kevin |
| 96 | * Master tracker login |
| 97 | * |
| 98 | * 2 4/27/98 7:12p Kevin |
| 99 | * Mastertracker pilot tracker |
| 100 | * |
| 101 | * 1 4/27/98 5:29p Kevin |
| 102 | * Initial Version |
| 103 | * |
| 104 | * $NoKeywords: $ |
| 105 | */ |
| 106 | |
| 107 | #ifdef WIN32 |
| 108 | #include <windows.h> |
| 109 | #endif |
| 110 | |
| 111 | #include <cstdlib> |
| 112 | #include <cstring> |
| 113 | |
| 114 | #include "pstypes.h" |
| 115 | #include "networking.h" |
| 116 | #include "mt_net.h" |
| 117 | #include "byteswap.h" |
| 118 | |
| 119 | #define LOGIN_LEN 33 |
| 120 | #define REAL_NAME_LEN 66 |
| 121 | #define PASSWORD_LEN 17 |
| 122 | #define EMAIL_LEN 100 |
| 123 | #define TRACKER_ID_LEN 10 |
| 124 | #define PILOT_NAME_LEN 20 |
| 125 | |
| 126 | #ifdef WIN32 |
| 127 | #pragma pack(push, pxo) |
| 128 | #endif |
| 129 | #pragma pack(1) |
| 130 | struct vmt_descent3_struct { |
| 131 | char tracker_id[TRACKER_ID_LEN]; |
| 132 | char pilot_name[PILOT_NAME_LEN]; |
| 133 | int rank; |
| 134 | |
| 135 | int kills; |
| 136 | int deaths; |
| 137 | int suicides; |
| 138 | int online_time; |
| 139 | int games_played; |
| 140 | uint32_t security; |
| 141 | uint8_t virgin_pilot; // This pilot was just created if TRUE |
| 142 | uint32_t lateral_thrust; |
| 143 | uint32_t rotational_thrust; |
| 144 | uint32_t sliding_pct; // Percentage of the time you were sliding |
| 145 | uint32_t checksum; // This value needs to be equal to whatever the checksum is once the packet is decoded |
| 146 | uint32_t pad; // just to provide room for out 4 byte encryption boundry only needed on the client side for now |
| 147 | }; |
| 148 | #define DESCENT3_BLOCK_SIZE (sizeof(vmt_descent3_struct) - 4) |
| 149 | #ifdef WIN32 |
| 150 | #pragma pack(pop, pxo) |
| 151 | #else |
| 152 | #pragma pack() |
| 153 | #endif |
| 154 | |
| 155 | #include "mtpilottrack.h" |
| 156 | |
| 157 | #define DLLmprintf(...) DLLDebug_ConsolePrintf(__VA_ARGS__) |
| 158 | |
| 159 | typedef void (*Debug_ConsolePrintf_fp)(int n, const char *format, ...); |
| 160 | |
| 161 | extern Debug_ConsolePrintf_fp DLLDebug_ConsolePrintf; |
| 162 | |
| 163 | typedef float (*timer_GetTime_fp)(); |
| 164 | extern timer_GetTime_fp DLLtimer_GetTime; |
| 165 | |
| 166 | typedef void (*HandlePilotData_fp)(uint8_t *data, int len, network_address *from); |
| 167 | |
| 168 | typedef int (*nw_RegisterCallback_fp)(HandlePilotData_fp, uint8_t id); |
| 169 | extern nw_RegisterCallback_fp DLLnw_RegisterCallback; |
| 170 | |
| 171 | typedef int (*nw_DoReceiveCallbacks_fp)(); |
| 172 | extern nw_DoReceiveCallbacks_fp DLLnw_DoReceiveCallbacks; |
| 173 | |
| 174 | #define VALID_STATE_IDLE 1 |
| 175 | #define VALID_STATE_WAITING 2 |
| 176 | #define VALID_STATE_VALID 3 |
| 177 | #define VALID_STATE_INVALID 4 |
| 178 | #define VALID_STATE_TIMEOUT 5 |
| 179 | |
| 180 | #define VERSION_STATE_IDLE 1 |
| 181 | #define VERSION_STATE_WAITING 2 |
| 182 | #define VERSION_STATE_RECEIVED 3 |
| 183 | #define VERSION_STATE_TIMEOUT 5 |
| 184 | |
| 185 | #define STATE_WAITING_MOTD 8 |
| 186 | #define STATE_HAVE_MOTD 9 |
| 187 | |
| 188 | #define MAX_MOTDLEN 1000 |
| 189 | // Variables |
| 190 | |
| 191 | int D3WriteState; |
| 192 | int D3ReadState; |
| 193 | float D3LastSent; |
| 194 | float D3FirstSent; |
| 195 | |
| 196 | float D3LastSentWrite; |
| 197 | float D3FirstSentWrite; |
| 198 | |
| 199 | udp_packet_header d3_pilot_req, d3_pilot_write, motd_req; |
| 200 | pilot_request *d3_pr; |
| 201 | vmt_descent3_struct *ReadD3Pilot; |
| 202 | vmt_descent3_struct WriteD3Pilot; |
| 203 | |
| 204 | udp_packet_header PacketHeader, VersionPacketHeader; |
| 205 | validate_id_request *ValidIDReq; |
| 206 | |
| 207 | int MOTDState; |
| 208 | |
| 209 | float MOTDFirstSent; |
| 210 | float MOTDLastSent; |
| 211 | |
| 212 | char *Motdptr; |
| 213 | uint32_t Motd_maxlen; |
| 214 | |
| 215 | int ValidState; |
| 216 | |
| 217 | float ValidFirstSent; |
| 218 | float ValidLastSent; |
| 219 | |
| 220 | int VersionState; |
| 221 | |
| 222 | float VersionFirstSent; |
| 223 | float VersionLastSent; |
| 224 | char *VersionURL; |
| 225 | |
| 226 | uint32_t ValidSecurity; |
| 227 | |
| 228 | char *Psztracker_id; |
| 229 | |
| 230 | char ProccesingPilot[PILOT_NAME_LEN]; |
| 231 | |
| 232 | typedef int (*PollUI_fp)(); |
| 233 | extern PollUI_fp DLLPollUI; |
| 234 | |
| 235 | int InitPilotTrackerClient() { |
| 236 | |
| 237 | D3WriteState = STATE_IDLE; |
| 238 | D3ReadState = STATE_IDLE; |
| 239 | |
| 240 | ReadD3Pilot = nullptr; |
| 241 | ValidState = VALID_STATE_IDLE; |
| 242 | |
| 243 | VersionState = VERSION_STATE_IDLE; |
| 244 | |
| 245 | srand(DLLtimer_GetTime()); |
| 246 | ValidSecurity = (uint32_t)rand(); |
| 247 | // Build the request packet |
| 248 | PacketHeader.type = UNT_LOGIN_AUTH_REQUEST; // UNT_VALIDAT_ID_REQUEST; |
| 249 | PacketHeader.len = INTEL_SHORT(PACKED_HEADER_ONLY_SIZE + sizeof(validate_id_request)); |
| 250 | ValidIDReq = (validate_id_request *)&PacketHeader.data; |
| 251 | |
| 252 | d3_pr = (pilot_request *)&d3_pilot_req.data; |
| 253 | |
| 254 | DLLnw_RegisterCallback(HandlePilotData, PXO_NETID_USER_TRACKER); |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | // Returns: |
| 259 | //-3 Error -- Called with NULL, but no request is waiting |
| 260 | //-2 Error -- Already sending data (hasn't timed out yet) |
| 261 | //-1 Timeout trying to send pilot data |
| 262 | // 0 Sending |
| 263 | // 1 Data successfully sent |
| 264 | |
| 265 | // Call with NULL to poll |
| 266 | // Call with valid pointer to a vmt_descent3_struct to initiate send |
| 267 | int SendD3PilotData(vmt_descent3_struct *d3_pilot) { |
| 268 | // First check the network |
| 269 | PollPTrackNet(); |
| 270 | |
| 271 | if (d3_pilot == nullptr) { |
| 272 | if (D3WriteState == STATE_IDLE) { |
| 273 | return -3; |
| 274 | } |
| 275 | if (D3WriteState == STATE_SENDING_PILOT) { |
| 276 | return 0; |
| 277 | } |
| 278 | if (D3WriteState == STATE_WROTE_PILOT) { |
| 279 | // We wrote this pilot, and now we are about to inform the app, so back to idle |
| 280 | D3WriteState = STATE_IDLE; |
| 281 | return 1; |
| 282 | } |
| 283 | if (D3WriteState == STATE_TIMED_OUT) { |
| 284 | // We gave up on sending this pilot, and now we are about to inform the app, so back to idle |
| 285 | D3WriteState = STATE_IDLE; |
| 286 | |
| 287 | return -1; |
| 288 | } |
| 289 | if (D3WriteState == STATE_WRITE_PILOT_FAILED) { |
| 290 | // The tracker said this dude couldn't be written |
| 291 | D3WriteState = STATE_IDLE; |
| 292 | |
| 293 | return 3; |
| 294 | } |
| 295 | } else if (D3WriteState == STATE_IDLE) { |
| 296 | // New request, send out the req, and go for it. |
| 297 | |
| 298 | D3WriteState = STATE_SENDING_PILOT; |
| 299 | |
| 300 | D3LastSentWrite = DLLtimer_GetTime(); |
| 301 | D3FirstSentWrite = DLLtimer_GetTime(); |
| 302 | |
| 303 | d3_pilot_write.len = INTEL_SHORT(PACKED_HEADER_ONLY_SIZE + DESCENT3_BLOCK_SIZE); |
| 304 | d3_pilot_write.type = UNT_PILOT_DATA_WRITE; |
| 305 | d3_pilot_write.code = INTEL_INT(CMD_GAME_DESCENT3); |
| 306 | d3_pilot_write.security = d3_pilot->security; |
| 307 | |
| 308 | d3_pilot->deaths = INTEL_INT(d3_pilot->deaths); |
| 309 | d3_pilot->kills = INTEL_INT(d3_pilot->kills); |
| 310 | d3_pilot->online_time = INTEL_INT(d3_pilot->online_time); |
| 311 | d3_pilot->games_played = INTEL_INT(d3_pilot->games_played); |
| 312 | d3_pilot->lateral_thrust = INTEL_INT(d3_pilot->lateral_thrust); |
| 313 | d3_pilot->rank = INTEL_INT(d3_pilot->rank); |
| 314 | d3_pilot->rotational_thrust = INTEL_INT(d3_pilot->rotational_thrust); |
| 315 | d3_pilot->sliding_pct = INTEL_INT(d3_pilot->sliding_pct); |
| 316 | d3_pilot->suicides = INTEL_INT(d3_pilot->suicides); |
| 317 | |
| 318 | #if (!(defined(OEM) || defined(DEMO))) |
| 319 | // This is the secret number we will use to determine if it's |
| 320 | // a game where we want to update the player's rank or not. |
| 321 | d3_pilot->lateral_thrust = INTEL_INT(0x142); |
| 322 | #endif |
| 323 | memcpy(&d3_pilot_write.data, d3_pilot, DESCENT3_BLOCK_SIZE); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | return -2; |
| 328 | } |
| 329 | |
| 330 | // Returns: |
| 331 | //-3 Error -- Called with NULL, but no request is waiting |
| 332 | //-2 Error -- Already waiting on data (hasn't timed out yet) |
| 333 | //-1 Timeout waiting for pilot data |
| 334 | // 0 Waiting for data |
| 335 | // 1 Data received |
| 336 | // 2 Get Cancelled |
| 337 | |
| 338 | // Call with NULL to poll |
| 339 | // Call with -1 to cancel wait |
| 340 | // Call with valid pointer to a vmt_descent3_struct to get a response |
| 341 | int GetD3PilotData(vmt_descent3_struct *d3_pilot, char *pilot_name, char *tracker_id) { |
| 342 | // First check the network |
| 343 | PollPTrackNet(); |
| 344 | |
| 345 | if (d3_pilot == nullptr) { |
| 346 | if (D3ReadState == STATE_IDLE) { |
| 347 | return -3; |
| 348 | } |
| 349 | if (D3ReadState == STATE_READING_PILOT) { |
| 350 | return 0; |
| 351 | } |
| 352 | if (D3ReadState == STATE_RECEIVED_PILOT) { |
| 353 | // We got this pilot, and now we are about to inform the app, so back to idle |
| 354 | D3ReadState = STATE_IDLE; |
| 355 | ReadD3Pilot = nullptr; |
| 356 | |
| 357 | return 1; |
| 358 | } |
| 359 | if (D3ReadState == STATE_TIMED_OUT) { |
| 360 | // We gave up on this pilot, and now we are about to inform the app, so back to idle |
| 361 | D3ReadState = STATE_IDLE; |
| 362 | ReadD3Pilot = nullptr; |
| 363 | return -1; |
| 364 | } |
| 365 | if (D3ReadState == STATE_PILOT_NOT_FOUND) { |
| 366 | // The tracker said this dude is not found. |
| 367 | D3ReadState = STATE_IDLE; |
| 368 | ReadD3Pilot = nullptr; |
| 369 | return 3; |
| 370 | } |
| 371 | |
| 372 | } else if (D3ReadState == STATE_IDLE) { |
| 373 | // New request, send out the req, and go for it. |
| 374 | |
| 375 | D3ReadState = STATE_READING_PILOT; |
| 376 | ReadD3Pilot = d3_pilot; |
| 377 | D3LastSent = DLLtimer_GetTime(); |
| 378 | D3FirstSent = DLLtimer_GetTime(); // DLLtimer_GetTime(); |
| 379 | |
| 380 | d3_pilot_req.len = INTEL_SHORT(PACKED_HEADER_ONLY_SIZE + sizeof(pilot_request)); |
| 381 | d3_pilot_req.type = UNT_PILOT_DATA_READ; |
| 382 | d3_pilot_req.code = INTEL_INT(CMD_GAME_DESCENT3); |
| 383 | strcpy(d3_pr->pilot_name, pilot_name); |
| 384 | strcpy(d3_pr->tracker_id, tracker_id); |
| 385 | strcpy(ProccesingPilot, pilot_name); |
| 386 | return 0; |
| 387 | } |
| 388 | return -2; |
| 389 | } |
| 390 | |
| 391 | int GetD3PilotDataCancel() { |
| 392 | // First check the network |
| 393 | PollPTrackNet(); |
| 394 | |
| 395 | if (D3ReadState == STATE_IDLE) { |
| 396 | return -3; |
| 397 | } else { |
| 398 | // Cancel this baby |
| 399 | D3ReadState = STATE_IDLE; |
| 400 | ReadD3Pilot = nullptr; |
| 401 | return 2; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Send an ACK to the server |
| 406 | void AckServer(uint32_t sig) { |
| 407 | udp_packet_header ack_pack; |
| 408 | |
| 409 | ack_pack.type = UNT_CONTROL; |
| 410 | ack_pack.sig = sig; |
| 411 | ack_pack.code = INTEL_INT(CMD_CLIENT_RECEIVED); |
| 412 | ack_pack.len = INTEL_SHORT(PACKED_HEADER_ONLY_SIZE); |
| 413 | |
| 414 | SendPilotTrackerPacket(&ack_pack); |
| 415 | // sendto(pilotsock,(char *)&ack_pack,PACKED_HEADER_ONLY_SIZE,0,(SOCKADDR *)&ptrackaddr,sizeof(SOCKADDR_IN)); |
| 416 | } |
| 417 | |
| 418 | void IdlePTrack() { |
| 419 | // socket |
| 420 | if (D3ReadState == STATE_READING_PILOT) { |
| 421 | if ((DLLtimer_GetTime() - D3FirstSent) >= PILOT_REQ_TIMEOUT) { |
| 422 | D3ReadState = STATE_TIMED_OUT; |
| 423 | |
| 424 | } else if ((DLLtimer_GetTime() - D3LastSent) >= PILOT_REQ_RESEND_TIME) { |
| 425 | // Send 'da packet |
| 426 | // sendto(pilotsock,(char *)&d3_pilot_req,d3_pilot_req.len,0,(SOCKADDR *)&ptrackaddr,sizeof(SOCKADDR_IN)); |
| 427 | SendPilotTrackerPacket(&d3_pilot_req); |
| 428 | D3LastSent = DLLtimer_GetTime(); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (D3WriteState == STATE_SENDING_PILOT) { |
| 433 | if ((DLLtimer_GetTime() - D3FirstSentWrite) >= PILOT_REQ_TIMEOUT) { |
| 434 | D3WriteState = STATE_TIMED_OUT; |
| 435 | |
| 436 | } else if ((DLLtimer_GetTime() - D3LastSentWrite) >= PILOT_REQ_RESEND_TIME) { |
| 437 | // Send 'da packet |
| 438 | SendPilotTrackerPacket(&d3_pilot_write); |
| 439 | // sendto(pilotsock,(char *)&d3_pilot_write,d3_pilot_write.len,0,(SOCKADDR *)&ptrackaddr,sizeof(SOCKADDR_IN)); |
| 440 | D3LastSentWrite = DLLtimer_GetTime(); |
| 441 | } |
| 442 | } |
| 443 | if (MOTDState == STATE_WAITING_MOTD) { |
| 444 | if ((DLLtimer_GetTime() - MOTDFirstSent) >= PILOT_REQ_TIMEOUT) { |
| 445 | MOTDState = STATE_TIMED_OUT; |
| 446 | |
| 447 | } else if ((DLLtimer_GetTime() - MOTDLastSent) >= PILOT_REQ_RESEND_TIME) { |
| 448 | // Send 'da packet |
| 449 | SendPilotTrackerPacket(&motd_req); |
| 450 | MOTDLastSent = DLLtimer_GetTime(); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void PollPTrackNet() { |
| 456 | IdlePTrack(); |
| 457 | DLLnw_DoReceiveCallbacks(); |
| 458 | } |
| 459 | |
| 460 | int Motd_version = 0; |
| 461 | |
| 462 | void HandlePilotData(uint8_t *data, int len, network_address *from) { |
| 463 | udp_packet_header inpacket; |
| 464 | vmt_descent3_struct *d3_pilot; |
| 465 | memcpy(&inpacket, data, sizeof(inpacket)); |
| 466 | inpacket.len = INTEL_SHORT(inpacket.len) - 1; |
| 467 | if (len) { |
| 468 | switch (inpacket.type) { |
| 469 | case UNT_MOTD_RESPONSE: |
| 470 | if (MOTDState == STATE_WAITING_MOTD) { |
| 471 | if (Motdptr) { |
| 472 | if ((inpacket.len - PACKED_HEADER_ONLY_SIZE) > Motd_maxlen) |
| 473 | inpacket.len = PACKED_HEADER_ONLY_SIZE + Motd_maxlen - 1; |
| 474 | strncpy(Motdptr, (char *)inpacket.data, inpacket.len - PACKED_HEADER_ONLY_SIZE); |
| 475 | Motdptr[inpacket.len - PACKED_HEADER_ONLY_SIZE] = '\0'; |
| 476 | } |
| 477 | MOTDState = STATE_HAVE_MOTD; |
| 478 | } |
| 479 | break; |
| 480 | case UNT_PILOT_DATA_RESPONSE: |
| 481 | if (inpacket.code == CMD_GAME_DESCENT3) { |
| 482 | if (D3ReadState == STATE_READING_PILOT) { |
| 483 | d3_pilot = (vmt_descent3_struct *)&inpacket.data; |
| 484 | xorcode(d3_pilot, DESCENT3_BLOCK_SIZE, inpacket.security); |
| 485 | if (stricmp(ProccesingPilot, d3_pilot->pilot_name) == 0) { |
| 486 | // Copy the data |
| 487 | memcpy(ReadD3Pilot, &inpacket.data, DESCENT3_BLOCK_SIZE); |
| 488 | // Set the state |
| 489 | D3ReadState = STATE_RECEIVED_PILOT; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | break; |
| 494 | case UNT_PILOT_READ_FAILED: |
| 495 | if (inpacket.code == CMD_GAME_DESCENT3) { |
| 496 | if (D3ReadState == STATE_READING_PILOT) { |
| 497 | D3ReadState = STATE_PILOT_NOT_FOUND; |
| 498 | } |
| 499 | } |
| 500 | break; |
| 501 | case UNT_PILOT_WRITE_SUCCESS: |
| 502 | if (inpacket.code == CMD_GAME_DESCENT3) { |
| 503 | if (D3WriteState == STATE_SENDING_PILOT) { |
| 504 | D3WriteState = STATE_WROTE_PILOT; |
| 505 | } |
| 506 | } |
| 507 | break; |
| 508 | case UNT_PILOT_WRITE_FAILED: |
| 509 | if (inpacket.code == CMD_GAME_DESCENT3) { |
| 510 | if (D3WriteState == STATE_SENDING_PILOT) { |
| 511 | D3WriteState = STATE_WRITE_PILOT_FAILED; |
| 512 | } |
| 513 | } |
| 514 | break; |
| 515 | case UNT_CONTROL: |
| 516 | switch (INTEL_INT(inpacket.code)) { |
| 517 | case CMD_VALIDATED_USER_ACK: |
| 518 | if (ValidState == VALID_STATE_WAITING) { |
| 519 | ValidState = VALID_STATE_VALID; |
| 520 | } |
| 521 | break; |
| 522 | case CMD_VALIDATED_USER_NAK: |
| 523 | if (ValidState == VALID_STATE_WAITING) { |
| 524 | ValidState = VALID_STATE_INVALID; |
| 525 | } |
| 526 | break; |
| 527 | } |
| 528 | case UNT_LOGIN_NO_AUTH: |
| 529 | if (ValidState == VALID_STATE_WAITING) { |
| 530 | ValidState = VALID_STATE_INVALID; |
| 531 | } |
| 532 | break; |
| 533 | case UNT_LOGIN_AUTHENTICATED: |
| 534 | if (ValidState == VALID_STATE_WAITING) { |
| 535 | ValidState = VALID_STATE_VALID; |
| 536 | strcpy(Psztracker_id, (const char *)&inpacket.data); |
| 537 | // memcpy(&Motd_version,inpacket.data[strlen(Psztracker_id)+1],sizeof(int)); |
| 538 | Motd_version = inpacket.xcode; |
| 539 | } |
| 540 | break; |
| 541 | case UNT_TRACKER_VERSION_RSP: |
| 542 | if (VersionState == VERSION_STATE_WAITING) { |
| 543 | // This is a version response possibly with the URL to download a file at the end. |
| 544 | strcpy(VersionURL, (const char *)&inpacket.data); |
| 545 | VersionState = VERSION_STATE_RECEIVED; |
| 546 | } |
| 547 | break; |
| 548 | default: |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | AckServer(inpacket.sig); |
| 553 | } |
| 554 | |
| 555 | // Call with a valid struct to validate a user |
| 556 | // Call with NULL to poll |
| 557 | |
| 558 | // Return codes: |
| 559 | // -3 Still waiting (returned if we were waiting for a tracker response and ValidateUser was called with a non-NULL |
| 560 | // value -2 Timeout waiting for tracker to respond -1 User invalid |
| 561 | // 0 Still waiting for response from tracker/Idle |
| 562 | // 1 User valid |
| 563 | int ValidateUser(validate_id_request *valid_id, char *trackerid) { |
| 564 | ValidIdle(); |
| 565 | if (valid_id == NULL) { |
| 566 | switch (ValidState) { |
| 567 | case VALID_STATE_IDLE: |
| 568 | return 0; |
| 569 | break; |
| 570 | case VALID_STATE_WAITING: |
| 571 | return 0; |
| 572 | break; |
| 573 | case VALID_STATE_VALID: |
| 574 | ValidState = VALID_STATE_IDLE; |
| 575 | return 1; |
| 576 | break; |
| 577 | case VALID_STATE_INVALID: |
| 578 | ValidState = VALID_STATE_IDLE; |
| 579 | return -1; |
| 580 | case VALID_STATE_TIMEOUT: |
| 581 | ValidState = VALID_STATE_IDLE; |
| 582 | return -2; |
| 583 | } |
| 584 | return 0; |
| 585 | } else { |
| 586 | // if(ValidState==VALID_STATE_IDLE) |
| 587 | { |
| 588 | /* |
| 589 | //First, flush the input buffer for the socket |
| 590 | uint32_t bytesin; |
| 591 | udp_packet_header inpacket; |
| 592 | do |
| 593 | { |
| 594 | bytesin = ReceivePilotTracker(&inpacket); |
| 595 | }while(bytesin && (bytesin!=-1)); |
| 596 | */ |
| 597 | Psztracker_id = trackerid; |
| 598 | ValidSecurity = (uint32_t)rand(); |
| 599 | PacketHeader.security = ValidSecurity; |
| 600 | // strcpy(ValidIDReq->tracker_id,valid_id->tracker_id); |
| 601 | strcpy(ValidIDReq->login, valid_id->login); |
| 602 | strcpy(ValidIDReq->password, valid_id->password); |
| 603 | SendPilotTrackerPacket(&PacketHeader); |
| 604 | ValidState = VALID_STATE_WAITING; |
| 605 | ValidFirstSent = DLLtimer_GetTime(); |
| 606 | ValidLastSent = DLLtimer_GetTime(); |
| 607 | return 0; |
| 608 | } |
| 609 | // else |
| 610 | // { |
| 611 | // return -3; |
| 612 | // } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | void ValidIdle() { |
| 617 | PollPTrackNet(); |
| 618 | if (ValidState == VALID_STATE_WAITING) { |
| 619 | if ((DLLtimer_GetTime() - ValidFirstSent) >= PILOT_REQ_TIMEOUT) { |
| 620 | ValidState = VALID_STATE_TIMEOUT; |
| 621 | |
| 622 | } else if ((DLLtimer_GetTime() - ValidLastSent) >= PILOT_REQ_RESEND_TIME) { |
| 623 | // Send 'da packet |
| 624 | SendPilotTrackerPacket(&PacketHeader); |
| 625 | ValidLastSent = DLLtimer_GetTime(); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | // This code will modify 4 bytes at a time, so make sure to pad it!!! |
| 630 | void xorcode(void *data, uint32_t len, uint32_t hash) { |
| 631 | return; |
| 632 | uint32_t i = 0; |
| 633 | uint32_t *src = (uint32_t *)&data; |
| 634 | |
| 635 | while (i < len) { |
| 636 | *src = *src ^ hash; |
| 637 | src++; |
| 638 | i += sizeof(uint32_t); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | void VersionIdle() { |
| 643 | PollPTrackNet(); |
| 644 | if (VersionState == VERSION_STATE_WAITING) { |
| 645 | if ((DLLtimer_GetTime() - VersionFirstSent) >= PILOT_REQ_TIMEOUT) { |
| 646 | VersionState = VERSION_STATE_TIMEOUT; |
| 647 | |
| 648 | } else if ((DLLtimer_GetTime() - VersionLastSent) >= PILOT_REQ_RESEND_TIME) { |
| 649 | // Send 'da packet |
| 650 | SendPilotTrackerPacket(&VersionPacketHeader); |
| 651 | VersionLastSent = DLLtimer_GetTime(); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | // Return codes: |
| 657 | // -3 Still waiting (returned if we were waiting for a tracker response and VersionCheck was called with a non-NULL |
| 658 | // value -2 Timeout waiting for tracker to respond |
| 659 | // 0 Still waiting for response from tracker/Idle |
| 660 | // 1 Version received (URL[0] will not be NULL if we have the URL which means we need a newer version) |
| 661 | // Call once with the real version, then poll with the version set to 0 URL is a pointer to a char buff(max size == 200) |
| 662 | // that will be filled if/when we get a URL response |
| 663 | int MTAVersionCheck(uint32_t oldver, char *URL) { |
| 664 | |
| 665 | int res = DLLPollUI(); |
| 666 | if (res == 99) { |
| 667 | VersionState = VERSION_STATE_TIMEOUT; |
| 668 | } |
| 669 | VersionIdle(); |
| 670 | if (oldver == 0) { |
| 671 | switch (VersionState) { |
| 672 | case VERSION_STATE_IDLE: |
| 673 | return 0; |
| 674 | break; |
| 675 | case VERSION_STATE_WAITING: |
| 676 | return 0; |
| 677 | break; |
| 678 | case VERSION_STATE_RECEIVED: |
| 679 | VersionState = VERSION_STATE_IDLE; |
| 680 | return 1; |
| 681 | case VERSION_STATE_TIMEOUT: |
| 682 | VersionState = VERSION_STATE_IDLE; |
| 683 | return -2; |
| 684 | } |
| 685 | return 0; |
| 686 | } else { |
| 687 | if (VersionState == VERSION_STATE_IDLE) { |
| 688 | /* |
| 689 | //First, flush the input buffer for the socket |
| 690 | uint32_t bytesin; |
| 691 | udp_packet_header inpacket; |
| 692 | do |
| 693 | { |
| 694 | bytesin = ReceivePilotTracker(&inpacket); |
| 695 | }while(bytesin && (bytesin!=-1)); |
| 696 | */ |
| 697 | VersionURL = URL; |
| 698 | VersionPacketHeader.len = INTEL_SHORT(PACKED_HEADER_ONLY_SIZE); |
| 699 | VersionPacketHeader.type = UNT_TRACKER_VERSION_REQ; |
| 700 | VersionPacketHeader.xcode = INTEL_SHORT(CMD_GAME_DESCENT3); |
| 701 | VersionPacketHeader.code = INTEL_INT(oldver); |
| 702 | |
| 703 | SendPilotTrackerPacket(&VersionPacketHeader); |
| 704 | VersionState = VERSION_STATE_WAITING; |
| 705 | VersionFirstSent = DLLtimer_GetTime(); |
| 706 | VersionLastSent = DLLtimer_GetTime(); |
| 707 | return 0; |
| 708 | } else { |
| 709 | return -3; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | int GetD3MOTD(char *szmotd, int maxlen) { |
| 715 | // First check the network |
| 716 | PollPTrackNet(); |
| 717 | |
| 718 | if (szmotd == nullptr) { |
| 719 | if (MOTDState == STATE_WAITING_MOTD) { |
| 720 | return 0; |
| 721 | } |
| 722 | if (MOTDState == STATE_TIMED_OUT) { |
| 723 | MOTDState = STATE_IDLE; |
| 724 | return -1; |
| 725 | } |
| 726 | if (MOTDState == STATE_HAVE_MOTD) { |
| 727 | MOTDState = STATE_IDLE; |
| 728 | return 1; |
| 729 | } |
| 730 | return -3; |
| 731 | } else if (MOTDState == STATE_IDLE) { |
| 732 | // New request, send out the req, and go for it. |
| 733 | |
| 734 | MOTDState = STATE_WAITING_MOTD; |
| 735 | Motdptr = szmotd; |
| 736 | Motd_maxlen = maxlen; |
| 737 | MOTDFirstSent = DLLtimer_GetTime(); |
| 738 | MOTDLastSent = DLLtimer_GetTime(); |
| 739 | |
| 740 | motd_req.len = INTEL_SHORT(PACKED_HEADER_ONLY_SIZE); |
| 741 | motd_req.type = UNT_MOTD_REQUEST; |
| 742 | motd_req.code = INTEL_INT(CMD_GAME_DESCENT3); |
| 743 | return 0; |
| 744 | } |
| 745 | return -2; |
| 746 | } |
| 747 | |
| 748 | int GetD3MOTDCancel() { |
| 749 | // First check the network |
| 750 | PollPTrackNet(); |
| 751 | |
| 752 | if (MOTDState == STATE_IDLE) { |
| 753 | return -3; |
| 754 | } else { |
| 755 | // Cancel this baby |
| 756 | MOTDState = STATE_IDLE; |
| 757 | Motdptr = nullptr; |
| 758 | ReadD3Pilot = nullptr; |
| 759 | return 2; |
| 760 | } |
| 761 | } |