| 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 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/mtclient/mt_net.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:58:40 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Mastertracker network code |
| 26 | * |
| 27 | * $Log: not supported by cvs2svn $ |
| 28 | * |
| 29 | * 13 10/03/01 12:44a Kevin |
| 30 | * Smaller version of pxo packets |
| 31 | * |
| 32 | * 12 9/02/99 3:16p Kevin |
| 33 | * Macintosh byte ordering fixes |
| 34 | * |
| 35 | * 11 8/24/99 5:47p Kevin |
| 36 | * Macintosh crap |
| 37 | * |
| 38 | * 10 4/19/99 8:36p Jeff |
| 39 | * Compile under Linux |
| 40 | * |
| 41 | * 9 4/19/99 7:56p Kevin |
| 42 | * ifdef'd out some win32 specific stuff |
| 43 | * |
| 44 | * 8 3/02/99 5:50p Kevin |
| 45 | * Ouch. Duplicate structures existed and were conflicting. |
| 46 | * |
| 47 | * 7 12/23/98 6:38p Kevin |
| 48 | * All UDP data (except gamespy) now uses one (registered) port number |
| 49 | * |
| 50 | * 6 10/01/98 11:37a Kevin |
| 51 | * UI fixes and stuff |
| 52 | * |
| 53 | * 5 9/16/98 8:06p Jason |
| 54 | * got mastertracker working with the dedicated server |
| 55 | * |
| 56 | * 4 9/04/98 1:51p Kevin |
| 57 | * implemented asyncronous gethostbyname |
| 58 | * |
| 59 | * 3 8/25/98 6:33p Kevin |
| 60 | * PXO screens |
| 61 | * |
| 62 | * 2 6/01/98 10:10a Kevin |
| 63 | * Added DLL connection interface and auto update DLL |
| 64 | * |
| 65 | * 1 5/21/98 11:21a Kevin |
| 66 | * |
| 67 | * $NoKeywords: $ |
| 68 | */ |
| 69 | |
| 70 | #ifdef WIN32 |
| 71 | #include <windows.h> |
| 72 | #include <winsock.h> |
| 73 | #include <wsipx.h> |
| 74 | #endif |
| 75 | |
| 76 | #include <memory.h> |
| 77 | #include "pstypes.h" |
| 78 | #include "networking.h" |
| 79 | #include "mtgametrack.h" |
| 80 | #include "mt_net.h" |
| 81 | #include "byteswap.h" |
| 82 | |
| 83 | #define LOGIN_LEN 33 |
| 84 | #define REAL_NAME_LEN 66 |
| 85 | #define PASSWORD_LEN 17 |
| 86 | #define EMAIL_LEN 100 |
| 87 | #define TRACKER_ID_LEN 10 |
| 88 | #define PILOT_NAME_LEN 20 |
| 89 | |
| 90 | #ifdef WIN32 |
| 91 | #pragma pack(push, pxo) |
| 92 | #endif |
| 93 | #pragma pack(1) // Single byte alignment! |
| 94 | struct vmt_descent3_struct { |
| 95 | char tracker_id[TRACKER_ID_LEN]; |
| 96 | char pilot_name[PILOT_NAME_LEN]; |
| 97 | int rank; |
| 98 | |
| 99 | int kills; |
| 100 | int deaths; |
| 101 | int suicides; |
| 102 | int online_time; |
| 103 | int games_played; |
| 104 | uint32_t security; |
| 105 | uint8_t virgin_pilot; // This pilot was just created if TRUE |
| 106 | uint32_t lateral_thrust; |
| 107 | uint32_t rotational_thrust; |
| 108 | uint32_t sliding_pct; // Percentage of the time you were sliding |
| 109 | uint32_t checksum; // This value needs to be equal to whatever the checksum is once the packet is decoded |
| 110 | uint32_t pad; // just to provide room for out 4 byte encryption boundry only needed on the client side for now |
| 111 | }; |
| 112 | #define DESCENT3_BLOCK_SIZE (sizeof(vmt_descent3_struct) - 4) |
| 113 | #ifdef WIN32 |
| 114 | #pragma pack(pop, pxo) |
| 115 | #else |
| 116 | #pragma pack() |
| 117 | #endif |
| 118 | |
| 119 | #include "mtpilottrack.h" |
| 120 | |
| 121 | extern int MT_Initialized; |
| 122 | // Mastertracker sockets |
| 123 | SOCKET TCP_game_tracker_socket; |
| 124 | SOCKET TCP_pilot_tracker_socket; |
| 125 | SOCKET TCP_chat_server_socket; |
| 126 | |
| 127 | // Mastertracker addresses (we resolve them on init, and don't want to have to do it again) |
| 128 | SOCKADDR_IN gtrackaddr; |
| 129 | SOCKADDR_IN ptrackaddr; |
| 130 | SOCKADDR_IN ctrackaddr; |
| 131 | SOCKADDR_IN sockaddr; |
| 132 | |
| 133 | #define DLLmprintf(...) DLLDebug_ConsolePrintf(__VA_ARGS__) |
| 134 | |
| 135 | #define NW_AGHBN_CANCEL 1 |
| 136 | #define NW_AGHBN_LOOKUP 2 |
| 137 | #define NW_AGHBN_READ 3 |
| 138 | |
| 139 | // extern void DLLDebug_ConsolePrintf (int n, const char *format, ... ); |
| 140 | typedef void (*Debug_ConsolePrintf_fp)(int n, const char *format, ...); |
| 141 | |
| 142 | extern Debug_ConsolePrintf_fp DLLDebug_ConsolePrintf; |
| 143 | |
| 144 | typedef int (*nw_Asyncgethostbyname_fp)(uint32_t *ip, int command, const char *hostname); |
| 145 | extern nw_Asyncgethostbyname_fp DLLnw_Asyncgethostbyname; |
| 146 | |
| 147 | typedef int (*nw_SendWithID_fp)(uint8_t id, uint8_t *data, int len, network_address *who_to); |
| 148 | extern nw_SendWithID_fp DLLnw_SendWithID; |
| 149 | typedef int (*PollUI_fp)(); |
| 150 | extern PollUI_fp DLLPollUI; |
| 151 | extern bool *DLLDedicated_server; |
| 152 | |
| 153 | int SendGameTrackerPacker(void *packet) { |
| 154 | game_packet_header *g_pack = (game_packet_header *)packet; |
| 155 | |
| 156 | network_address send_address; |
| 157 | memset(&send_address, 0, sizeof(network_address)); |
| 158 | SOCKADDR_IN *inaddr = (SOCKADDR_IN *)>rackaddr; |
| 159 | memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 160 | send_address.port = htons(inaddr->sin_port); |
| 161 | send_address.connection_type = NP_TCP; |
| 162 | |
| 163 | return DLLnw_SendWithID(PXO_NETID_GAME_TRACKER, (uint8_t *)g_pack, INTEL_INT(g_pack->len), &send_address); |
| 164 | } |
| 165 | |
| 166 | void InitMTSockets() { |
| 167 | #ifdef WIN32 |
| 168 | WSADATA ws_data; |
| 169 | WORD ver = MAKEWORD(1, 1); |
| 170 | #endif |
| 171 | int rcode; |
| 172 | uint32_t ip; |
| 173 | #ifdef WIN32 |
| 174 | int error = WSAStartup(ver, &ws_data); |
| 175 | if (error != 0) { |
| 176 | DLLmprintf(0, "There was an error initializing networking! Error=%d\n", error); |
| 177 | return; |
| 178 | } else |
| 179 | #endif |
| 180 | { |
| 181 | DLLmprintf(0, "Network initted successfully!\n"); |
| 182 | } |
| 183 | |
| 184 | { |
| 185 | static HOSTENT lhe; |
| 186 | memset(&sockaddr, 0, sizeof(SOCKADDR_IN)); |
| 187 | gtrackaddr.sin_family = AF_INET; |
| 188 | gtrackaddr.sin_addr.s_addr = INADDR_ANY; |
| 189 | gtrackaddr.sin_port = 0; |
| 190 | |
| 191 | gtrackaddr.sin_family = AF_INET; |
| 192 | // This would be a good place to resolve the IP based on a domain name |
| 193 | DLLmprintf(0, "Looking up hostname: %s.\n", GAMETRACKERNAME); |
| 194 | |
| 195 | DLLnw_Asyncgethostbyname(&ip, NW_AGHBN_LOOKUP, GAMETRACKERNAME); |
| 196 | |
| 197 | do { |
| 198 | rcode = DLLnw_Asyncgethostbyname(&ip, NW_AGHBN_READ, GAMETRACKERNAME); |
| 199 | |
| 200 | if (!*DLLDedicated_server) { |
| 201 | if (99 == DLLPollUI()) { |
| 202 | return; |
| 203 | } |
| 204 | } |
| 205 | } while (rcode == 0); |
| 206 | |
| 207 | if (rcode != 1) { |
| 208 | DLLmprintf(0, "Unable to gethostbyname(\"%s\").\n", GAMETRACKERNAME); |
| 209 | DLLmprintf(0, "WSAGetLastError() returned %d.\n", WSAGetLastError()); |
| 210 | DLLnw_Asyncgethostbyname(nullptr, NW_AGHBN_CANCEL, nullptr); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | memcpy(>rackaddr.sin_addr.s_addr, &ip, 4); //&iaddr, 4); |
| 215 | gtrackaddr.sin_port = htons(GAMEPORT); |
| 216 | InitGameTrackerClient(GT_D3TNG); |
| 217 | } |
| 218 | { |
| 219 | |
| 220 | memset(&sockaddr, 0, sizeof(SOCKADDR_IN)); |
| 221 | sockaddr.sin_family = AF_INET; |
| 222 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
| 223 | sockaddr.sin_port = 0; // htons(REGPORT); |
| 224 | DLLmprintf(0, "Looking up hostname: %s.\n", PILOTTRACKERNAME); |
| 225 | DLLnw_Asyncgethostbyname(&ip, NW_AGHBN_LOOKUP, PILOTTRACKERNAME); |
| 226 | do { |
| 227 | rcode = DLLnw_Asyncgethostbyname(&ip, NW_AGHBN_READ, PILOTTRACKERNAME); |
| 228 | if (!*DLLDedicated_server) { |
| 229 | if (99 == DLLPollUI()) { |
| 230 | return; |
| 231 | } |
| 232 | } |
| 233 | } while (rcode == 0); |
| 234 | |
| 235 | if (rcode != 1) { |
| 236 | DLLmprintf(0, "Unable to gethostbyname(\"%s\").\n", PILOTTRACKERNAME); |
| 237 | DLLmprintf(0, "WSAGetLastError() returned %d.\n", WSAGetLastError()); |
| 238 | DLLnw_Asyncgethostbyname(nullptr, NW_AGHBN_CANCEL, nullptr); |
| 239 | return; |
| 240 | } |
| 241 | ptrackaddr.sin_family = AF_INET; |
| 242 | memcpy(&ptrackaddr.sin_addr.s_addr, &ip, 4); |
| 243 | ptrackaddr.sin_port = htons(REGPORT); |
| 244 | InitPilotTrackerClient(); |
| 245 | } |
| 246 | MT_Initialized = 1; |
| 247 | } |
| 248 | |
| 249 | void CloseMTSockets() { |
| 250 | #ifdef WIN32 |
| 251 | if (WSACleanup()) { |
| 252 | DLLmprintf(0, "Error closing wsock!\n"); |
| 253 | } |
| 254 | #endif |
| 255 | } |
| 256 | |
| 257 | int SendPilotTrackerPacket(void *packet) { |
| 258 | udp_packet_header *p_pack = (udp_packet_header *)packet; |
| 259 | |
| 260 | network_address send_address; |
| 261 | memset(&send_address, 0, sizeof(network_address)); |
| 262 | SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&ptrackaddr; |
| 263 | memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 264 | send_address.port = htons(inaddr->sin_port); |
| 265 | send_address.connection_type = NP_TCP; |
| 266 | return DLLnw_SendWithID(PXO_NETID_USER_TRACKER, (uint8_t *)p_pack, INTEL_SHORT(p_pack->len), &send_address); |
| 267 | } |