| 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/dedicated_server.cpp $ |
| 21 | * $Revision: 1.5 $ |
| 22 | * $Date: 2004/03/11 03:46:52 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Dedicated server code |
| 26 | * |
| 27 | * $Log: dedicated_server.cpp,v $ |
| 28 | * Revision 1.5 2004/03/11 03:46:52 kevinb |
| 29 | * Merge of 1.5 code from windows. Hopefully I didn't break anything! |
| 30 | * |
| 31 | * Revision 1.4 2001/01/13 21:48:45 icculus |
| 32 | * patched to (re)compile on win32. |
| 33 | * |
| 34 | * Revision 1.3 2000/06/03 14:33:49 icculus |
| 35 | * Merge with Outrage 1.4 tree... |
| 36 | * |
| 37 | * |
| 38 | * 67 10/17/01 4:52p Matt |
| 39 | * Use safe printf() to fix buffer overflow. |
| 40 | * |
| 41 | * 66 7/09/01 5:45p Matt |
| 42 | * Added buffer overflow checking to dedicated server read. |
| 43 | * |
| 44 | * 65 4/20/00 4:46p Matt |
| 45 | * Dedicated server messages now support sending to one player (or team) |
| 46 | * |
| 47 | * 64 4/20/00 2:23p Matt |
| 48 | * Bail from parse routine if null command; used to have an Int3(). Fixed |
| 49 | * the crash when ":" was typed in the dedicated server console. |
| 50 | * |
| 51 | * 63 3/23/00 2:34p Matt |
| 52 | * Fixed table error (max string len) that caused crashes when parsing |
| 53 | * MultiSettingsFile keywords. |
| 54 | * |
| 55 | * 62 10/21/99 5:39p Matt |
| 56 | * Mac merge |
| 57 | * |
| 58 | * 61 8/23/99 5:12p Kevin |
| 59 | * Fixed double frame interval calling for DMFC |
| 60 | * |
| 61 | * 60 7/20/99 12:57p Jason |
| 62 | * added dedicated server message_of_the_day |
| 63 | * |
| 64 | * 59 7/15/99 12:47p Jeff |
| 65 | * fixed dedicated server quiting when a player logs out from telnet. |
| 66 | * Adding logout command |
| 67 | * |
| 68 | * 58 7/14/99 8:57p Jeff |
| 69 | * don't parse out semi-colons |
| 70 | * |
| 71 | * 57 7/09/99 3:49p Jason |
| 72 | * fixed bugs/issues for patch |
| 73 | * |
| 74 | * 56 7/06/99 5:52p Kevin |
| 75 | * PXO & multiplayer fixes for the patch |
| 76 | * |
| 77 | * 55 6/10/99 4:12p Kevin |
| 78 | * Fixed SetLevel in dedicated server for HEAT.NET, and added |
| 79 | * level_names.str for level name localization. |
| 80 | * |
| 81 | * 54 5/24/99 9:55p Jason |
| 82 | * fixed stupid dedicated server ship allow thing. I swear I'm going to |
| 83 | * start killing people who keep adding things to multiplayer when they |
| 84 | * really don't know what they are doing. STOP! |
| 85 | * |
| 86 | * 53 5/12/99 1:57p Jason |
| 87 | * fixed yet more buggy/ugly code |
| 88 | * |
| 89 | * 52 5/10/99 5:35p Kevin |
| 90 | * New command line options for heat and scoring API enhancements |
| 91 | * |
| 92 | * 51 5/09/99 1:34p Kevin |
| 93 | * Added diffuculty level system to multiplayer |
| 94 | * |
| 95 | * 50 5/07/99 5:00p Jeff |
| 96 | * handle errors better with a dedicated server if it couldn't load the |
| 97 | * multiplayer dll |
| 98 | * |
| 99 | * 49 5/02/99 2:32p Kevin |
| 100 | * fixed various dedicated server problems. |
| 101 | * |
| 102 | * $NoKeywords: $ |
| 103 | */ |
| 104 | |
| 105 | #include <cstdarg> |
| 106 | #include <cstdio> |
| 107 | |
| 108 | #include <string.h> |
| 109 | #include <stdlib.h> |
| 110 | |
| 111 | #if !defined(POSIX) |
| 112 | typedef int socklen_t; |
| 113 | #endif |
| 114 | |
| 115 | #include "pstypes.h" |
| 116 | #include "pserror.h" |
| 117 | #include "pstring.h" |
| 118 | #include "cfile.h" |
| 119 | #include "inffile.h" |
| 120 | #include "dedicated_server.h" |
| 121 | #include "multi.h" |
| 122 | #include "args.h" |
| 123 | #include "AppConsole.h" |
| 124 | #include "ddio.h" |
| 125 | #include "newui.h" |
| 126 | #include "ui.h" |
| 127 | #include "multi_dll_mgr.h" |
| 128 | #include "multi_ui.h" |
| 129 | #include "Mission.h" |
| 130 | #include "multi_server.h" |
| 131 | #include "Macros.h" |
| 132 | #include "game.h" |
| 133 | #include "mem.h" |
| 134 | #include "stringtable.h" |
| 135 | #include "multi_save_settings.h" |
| 136 | #include "objinfo.h" |
| 137 | #include "rtperformance.h" |
| 138 | #include "player.h" |
| 139 | #include "stringtable.h" |
| 140 | #include "init.h" |
| 141 | #include "ship.h" |
| 142 | #include "hud.h" |
| 143 | #include "networking.h" |
| 144 | |
| 145 | |
| 146 | bool Dedicated_server = false; |
| 147 | |
| 148 | static int Dedicated_start_level = 1; |
| 149 | static int Dummy_dedicated_var; |
| 150 | static char Dummy_dedicated_string[_MAX_PATH]; |
| 151 | |
| 152 | // If true, allow remote connections |
| 153 | static int Dedicated_allow_remote = 0; |
| 154 | static uint16_t Dedicated_listen_port = 2092; |
| 155 | static char dedicated_telnet_password[65]; |
| 156 | static int Dedicated_num_teams = 1; |
| 157 | |
| 158 | static int RunServerConfigs(); |
| 159 | static int DedicatedServerLex(const char *command); |
| 160 | static void SetCVarNone(int index); |
| 161 | |
| 162 | extern int CheckMissionForScript(char *mission, char *script, int dedicated_server_num_teams); |
| 163 | |
| 164 | extern char Multi_message_of_the_day[]; |
| 165 | extern char PXO_hosted_lobby_name[]; |
| 166 | // These define the types of variables that can be set in the code through |
| 167 | // the dedicated server |
| 168 | |
| 169 | static cvar_entry CVars[] = { |
| 170 | {"PPS", CVAR_TYPE_INT, &Netgame.packets_per_second, 2, 20, CVAR_GAMEINIT}, // 0 |
| 171 | {"TimeLimit", CVAR_TYPE_INT, &Netgame.timelimit, 0, 10000, CVAR_GAMEINIT}, |
| 172 | {"KillGoal", CVAR_TYPE_INT, &Netgame.killgoal, 0, 10000, CVAR_GAMEINIT}, |
| 173 | {"RespawnTime", CVAR_TYPE_INT, &Netgame.respawn_time, 0, 10000, CVAR_GAMEINIT}, |
| 174 | {"GameName", CVAR_TYPE_STRING, &Netgame.name, -1, NETGAME_NAME_LEN, CVAR_GAMEINIT}, // 4 |
| 175 | {"MissionName", CVAR_TYPE_STRING, &Netgame.mission, -1, MSN_NAMELEN, CVAR_GAMEINIT}, |
| 176 | {"Scriptname", CVAR_TYPE_STRING, &Netgame.scriptname, -1, NETGAME_SCRIPT_LEN, CVAR_GAMEINIT}, |
| 177 | {"ConnectionName", CVAR_TYPE_STRING, &Netgame.connection_name, -1, PAGENAME_LEN, CVAR_GAMEINIT}, |
| 178 | {"Quit", CVAR_TYPE_NONE, NULL, -1, -1, CVAR_GAMEPLAY}, // 8 |
| 179 | {"EndLevel", CVAR_TYPE_NONE, NULL, -1, -1, CVAR_GAMEPLAY}, |
| 180 | #if 0 // def DEMO |
| 181 | {"MaxPlayers",CVAR_TYPE_INT,&Netgame.max_players,2,16,CVAR_GAMEINIT}, //10 |
| 182 | #else |
| 183 | {"MaxPlayers", CVAR_TYPE_INT, &Netgame.max_players, 2, MAX_PLAYERS, CVAR_GAMEINIT}, // 10 |
| 184 | #endif |
| 185 | {"Say", CVAR_TYPE_STRING, NULL, -1, 200, CVAR_GAMEPLAY}, // 11 |
| 186 | {"UseSmoothing", CVAR_TYPE_INT, &Dummy_dedicated_var, 0, 1, CVAR_GAMEINIT}, // 12 |
| 187 | {"SendRotVel", CVAR_TYPE_INT, &Dummy_dedicated_var, 0, 1, CVAR_GAMEINIT}, // 13 |
| 188 | {"MultiSettingsFile", CVAR_TYPE_STRING, &Dummy_dedicated_string, -1, 40, CVAR_GAMEINIT}, // 14 |
| 189 | {"DisallowPowerup", CVAR_TYPE_STRING, &Dummy_dedicated_string, -1, 40, CVAR_GAMEINIT | CVAR_GAMEPLAY}, // 15 |
| 190 | {"AllowPowerup", CVAR_TYPE_STRING, &Dummy_dedicated_string, -1, 40, CVAR_GAMEINIT | CVAR_GAMEPLAY}, // 16 |
| 191 | {"StartRtLog", CVAR_TYPE_NONE, NULL, -1, -1, CVAR_GAMEPLAY}, // 17 |
| 192 | {"StopRtLog", CVAR_TYPE_NONE, NULL, -1, -1, CVAR_GAMEPLAY}, // 18 |
| 193 | {"PXOUsername", CVAR_TYPE_STRING, &Auto_login_name, -1, 50, CVAR_GAMEINIT}, // 19 |
| 194 | {"PXOPassword", CVAR_TYPE_STRING, &Auto_login_pass, -1, 50, CVAR_GAMEINIT}, // 20 |
| 195 | {"BrightPlayers", CVAR_TYPE_INT, &Dummy_dedicated_var, -1, -1, CVAR_GAMEINIT}, // 21 |
| 196 | {"Peer2Peer", CVAR_TYPE_INT, &Dummy_dedicated_var, -1, -1, CVAR_GAMEINIT}, // 22 |
| 197 | {"AccurateCollisions", CVAR_TYPE_INT, &Dummy_dedicated_var, -1, -1, CVAR_GAMEINIT}, // 23 |
| 198 | {"ConsolePassword", CVAR_TYPE_STRING, &dedicated_telnet_password, -1, 65, CVAR_GAMEINIT | CVAR_GAMEPLAY}, // 24 |
| 199 | {"AllowRemoteConsole", CVAR_TYPE_INT, &Dedicated_allow_remote, 0, 1, CVAR_GAMEINIT}, // 25 |
| 200 | {"RemoteConsolePort", CVAR_TYPE_INT, &Dedicated_listen_port, 1, 65535, CVAR_GAMEINIT}, // 26 |
| 201 | {"Permissable", CVAR_TYPE_INT, &Dummy_dedicated_var, -1, -1, CVAR_GAMEINIT}, // 27 |
| 202 | {"NumTeams", CVAR_TYPE_INT, &Dedicated_num_teams, 2, 4, CVAR_GAMEINIT}, // 28 |
| 203 | {"PXOHostingLobby", CVAR_TYPE_STRING, &PXO_hosted_lobby_name, -1, 100, CVAR_GAMEINIT | CVAR_GAMEPLAY}, // 29 |
| 204 | {"RandomizeRespawn", CVAR_TYPE_INT, &Dummy_dedicated_var, -1, -1, CVAR_GAMEINIT}, // 30 |
| 205 | {"AllowMouselook", CVAR_TYPE_INT, &Dummy_dedicated_var, -1, -1, CVAR_GAMEINIT}, // 31 |
| 206 | {"AudioTauntDelay", CVAR_TYPE_FLOAT, NULL, -1, -1, CVAR_GAMEINIT}, // 32 |
| 207 | {"SetLevel", CVAR_TYPE_INT, NULL, -1, -1, CVAR_GAMEINIT | CVAR_GAMEPLAY}, // 33 |
| 208 | {"SetDifficulty", CVAR_TYPE_INT, NULL, 0, 4, CVAR_GAMEINIT}, // 34 |
| 209 | {"MOTD", CVAR_TYPE_STRING, &Multi_message_of_the_day, -1, HUD_MESSAGE_LENGTH * 2, CVAR_GAMEINIT}, // 35 |
| 210 | }; |
| 211 | |
| 212 | #define CVAR_TIMELIMIT 1 |
| 213 | #define CVAR_KILLGOAL 2 |
| 214 | #define CVAR_QUIT 8 |
| 215 | #define CVAR_ENDLEVEL 9 |
| 216 | #define CVAR_MAXPLAYERS 10 |
| 217 | #define CVAR_MESSAGE 11 |
| 218 | #define CVAR_USESMOOTHING 12 |
| 219 | #define CVAR_SENDROTVEL 13 |
| 220 | #define CVAR_SETTINGS 14 |
| 221 | #define CVAR_DISALLOW 15 |
| 222 | #define CVAR_ALLOW 16 |
| 223 | #define CVAR_STARTLOG 17 |
| 224 | #define CVAR_STOPLOG 18 |
| 225 | #define CVAR_BRIGHTPLAYERS 21 |
| 226 | #define CVAR_PEER2PEER 22 |
| 227 | #define CVAR_ACCURATE_COLL 23 |
| 228 | #define CVAR_PERMISSABLE 27 |
| 229 | #define CVAR_RANDOMIZERESPAWN 30 |
| 230 | #define CVAR_ALLOWMOUSELOOKERS 31 |
| 231 | #define CVAR_AUDIOTAUNTDELAY 32 |
| 232 | #define CVAR_SETLEVEL 33 |
| 233 | #define CVAR_SETDIFF 34 |
| 234 | #define CVAR_MOTD 35 |
| 235 | |
| 236 | #define MAX_CVARS (sizeof(CVars) / sizeof(cvar_entry)) |
| 237 | |
| 238 | // Takes the data that the config file has an runs a server based on that data |
| 239 | // Returns true if ok, false if something is wrong |
| 240 | int RunServerConfigs() { |
| 241 | // Load the connection DLL |
| 242 | if (LoadMultiDLL(Netgame.connection_name)) { |
| 243 | CallMultiDLL(MT_AUTO_START); |
| 244 | if (!MultiDLLGameStarting) { |
| 245 | PrintDedicatedMessage(TXT_DS_COULDNTINIT, Netgame.connection_name); |
| 246 | PrintDedicatedMessage("\n"); |
| 247 | return 0; |
| 248 | } else { |
| 249 | PrintDedicatedMessage(TXT_DS_DLLINIT); |
| 250 | PrintDedicatedMessage("\n"); |
| 251 | } |
| 252 | } else { |
| 253 | PrintDedicatedMessage(TXT_DS_CONNECTLOADERR, Netgame.connection_name); |
| 254 | PrintDedicatedMessage("\n"); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | // Put in the correct mission names for the oem builds |
| 259 | #if (defined(OEM) || defined(DEMO)) |
| 260 | if (stricmp("polaris.d3l", Netgame.mission) == 0) { |
| 261 | strcpy(Netgame.mission_name, "Polaris"); |
| 262 | } else if (stricmp("taurus.d3l", Netgame.mission) == 0) { |
| 263 | strcpy(Netgame.mission_name, "Taurus"); |
| 264 | } else if (stricmp("thecore.d3l", Netgame.mission) == 0) { |
| 265 | strcpy(Netgame.mission_name, "The Core"); |
| 266 | } |
| 267 | #else |
| 268 | const char *p = GetMissionName(Netgame.mission); |
| 269 | strcpy(Netgame.mission_name, p); |
| 270 | #endif |
| 271 | |
| 272 | // Load the mission |
| 273 | if (!LoadMission(Netgame.mission)) { |
| 274 | PrintDedicatedMessage(TXT_DS_LOADMISSIONERR, Netgame.mission); |
| 275 | PrintDedicatedMessage("\n"); |
| 276 | return 0; |
| 277 | } else { |
| 278 | PrintDedicatedMessage(TXT_DS_MISSIONLOADED, Netgame.mission); |
| 279 | PrintDedicatedMessage("\n"); |
| 280 | } |
| 281 | |
| 282 | if (Current_mission.num_levels >= Dedicated_start_level) { |
| 283 | Current_mission.cur_level = Dedicated_start_level; |
| 284 | } else { |
| 285 | Current_mission.cur_level = 1; |
| 286 | } |
| 287 | // Start the actual server |
| 288 | |
| 289 | int teams = CheckMissionForScript(Netgame.mission, Netgame.scriptname, Dedicated_num_teams); |
| 290 | |
| 291 | if (teams == -1) { |
| 292 | PrintDedicatedMessage(TXT_MSNNOTCOMPATIBLE); |
| 293 | PrintDedicatedMessage("\n"); |
| 294 | return 0; |
| 295 | } else { |
| 296 | MultiStartServer(0, Netgame.scriptname, teams); |
| 297 | strcpy(Players[0].callsign, TXT_DS_SERVERNAME); |
| 298 | } |
| 299 | |
| 300 | InitDedicatedSocket(Dedicated_listen_port); |
| 301 | |
| 302 | return 1; |
| 303 | } |
| 304 | |
| 305 | int DedicatedServerLex(const char *command) { |
| 306 | for (int i = 0; i < MAX_CVARS; i++) |
| 307 | if (stricmp(CVars[i].varname, command) == 0) |
| 308 | return i; |
| 309 | |
| 310 | return INFFILE_ERROR; |
| 311 | } |
| 312 | |
| 313 | // Reads in the server config file for a dedicated server |
| 314 | // Returns true if everything is ok |
| 315 | int LoadServerConfigFile() { |
| 316 | InfFile inf; |
| 317 | char operand[INFFILE_LINELEN]; // operand |
| 318 | int t = FindArgChar("-dedicated", 'd'); |
| 319 | |
| 320 | // int t=FindArg ("-dedicated"); |
| 321 | if (!t) { |
| 322 | PrintDedicatedMessage(TXT_DS_BADCOMMANDLINE); |
| 323 | PrintDedicatedMessage("\n"); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | // Set our defaults |
| 328 | MultiResetSettings(); |
| 329 | |
| 330 | // Make all ships available |
| 331 | for (int i = 0; i < MAX_SHIPS; i++) { |
| 332 | if (Ships[i].used) |
| 333 | PlayerSetShipPermission(-1, Ships[i].name, true); |
| 334 | } |
| 335 | |
| 336 | if (GameArgs[t + 1][0]) { |
| 337 | strcpy(Netgame.server_config_name, GameArgs[t + 1]); |
| 338 | } else { |
| 339 | PrintDedicatedMessage(TXT_DS_MISSINGCONFIG); |
| 340 | PrintDedicatedMessage("\n"); |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | // open file |
| 345 | if (!inf.Open(Netgame.server_config_name, "[server config file]", DedicatedServerLex)) { |
| 346 | PrintDedicatedMessage(TXT_DS_BADCONFIG, Netgame.server_config_name); |
| 347 | PrintDedicatedMessage("\n"); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | // check if valid dedicated server file |
| 352 | while (inf.ReadLine()) { |
| 353 | int cmd; |
| 354 | |
| 355 | while ((cmd = inf.ParseLine(operand, INFFILE_LINELEN)) > INFFILE_ERROR) { |
| 356 | SetCVar(CVars[cmd].varname, operand, true); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | inf.Close(); |
| 361 | |
| 362 | if (!RunServerConfigs()) |
| 363 | return 0; |
| 364 | |
| 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | // Starts a dedicated server and loads the server config file |
| 369 | void StartDedicatedServer() { |
| 370 | int t = FindArgChar("-dedicated", 'd'); |
| 371 | // int t=FindArg ("-dedicated"); |
| 372 | if (!t) |
| 373 | return; |
| 374 | |
| 375 | Dedicated_server = true; |
| 376 | } |
| 377 | |
| 378 | // Sets the value for a cvar NONE type |
| 379 | void SetCVarNone(int index) { |
| 380 | if (CVars[index].type != CVAR_TYPE_NONE) |
| 381 | return; // Only handles ints |
| 382 | |
| 383 | // Do command specific stuff here |
| 384 | if (index == CVAR_QUIT) { |
| 385 | if (NetPlayers[Player_num].sequence == NETSEQ_PLAYING) { |
| 386 | MultiLeaveGame(); |
| 387 | SetFunctionMode(QUIT_MODE); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (index == CVAR_ENDLEVEL) { |
| 392 | if (NetPlayers[Player_num].sequence == NETSEQ_PLAYING) { |
| 393 | MultiEndLevel(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (index == CVAR_STARTLOG) |
| 398 | rtp_StartLog(); |
| 399 | |
| 400 | if (index == CVAR_STOPLOG) |
| 401 | rtp_StopLog(); |
| 402 | } |
| 403 | |
| 404 | // Sets the value for a cvar INT type |
| 405 | void SetCVarInt(int index, int val) { |
| 406 | void *dest_variable; |
| 407 | |
| 408 | if (CVars[index].type != CVAR_TYPE_INT) |
| 409 | return; // Only handles ints |
| 410 | |
| 411 | dest_variable = CVars[index].dest_variable; |
| 412 | |
| 413 | if (!(CVars[index].var_min == -1 && CVars[index].var_max == -1)) { |
| 414 | if (val < CVars[index].var_min) |
| 415 | val = (int)CVars[index].var_min; |
| 416 | |
| 417 | if (val > CVars[index].var_max) |
| 418 | val = (int)CVars[index].var_max; |
| 419 | } |
| 420 | |
| 421 | // Do command specific stuff here |
| 422 | switch (index) { |
| 423 | case CVAR_TIMELIMIT: { |
| 424 | if (val > 0) |
| 425 | Netgame.flags |= NF_TIMER; |
| 426 | else |
| 427 | Netgame.flags &= ~NF_TIMER; |
| 428 | } break; |
| 429 | |
| 430 | case CVAR_KILLGOAL: { |
| 431 | if (val > 0) |
| 432 | Netgame.flags |= NF_KILLGOAL; |
| 433 | else |
| 434 | Netgame.flags &= ~NF_KILLGOAL; |
| 435 | } break; |
| 436 | |
| 437 | case CVAR_MAXPLAYERS: { |
| 438 | if (val > MAX_PLAYERS) |
| 439 | val = MAX_PLAYERS; |
| 440 | else if (val < 2) |
| 441 | val = 2; |
| 442 | |
| 443 | } break; |
| 444 | |
| 445 | case CVAR_BRIGHTPLAYERS: { |
| 446 | if (val) |
| 447 | Netgame.flags |= NF_BRIGHT_PLAYERS; |
| 448 | else |
| 449 | Netgame.flags &= ~NF_BRIGHT_PLAYERS; |
| 450 | } break; |
| 451 | |
| 452 | case CVAR_USESMOOTHING: { |
| 453 | if (val) |
| 454 | Netgame.flags |= NF_USE_SMOOTHING; |
| 455 | else |
| 456 | Netgame.flags &= ~NF_USE_SMOOTHING; |
| 457 | } break; |
| 458 | |
| 459 | case CVAR_PEER2PEER: { |
| 460 | if (val) { |
| 461 | Netgame.flags |= NF_PEER_PEER; |
| 462 | Netgame.flags &= ~NF_PERMISSABLE; |
| 463 | } else |
| 464 | Netgame.flags &= ~NF_PEER_PEER; |
| 465 | } break; |
| 466 | |
| 467 | case CVAR_PERMISSABLE: { |
| 468 | if (val) { |
| 469 | Netgame.flags |= NF_PERMISSABLE; |
| 470 | Netgame.flags &= ~NF_PEER_PEER; |
| 471 | } else |
| 472 | Netgame.flags &= ~NF_PERMISSABLE; |
| 473 | } break; |
| 474 | |
| 475 | case CVAR_RANDOMIZERESPAWN: { |
| 476 | if (val) { |
| 477 | Netgame.flags |= NF_RANDOMIZE_RESPAWN; |
| 478 | } else |
| 479 | Netgame.flags &= ~NF_RANDOMIZE_RESPAWN; |
| 480 | } break; |
| 481 | |
| 482 | case CVAR_ACCURATE_COLL: { |
| 483 | if (val) |
| 484 | Netgame.flags |= NF_USE_ACC_WEAP; |
| 485 | else |
| 486 | Netgame.flags &= ~NF_USE_ACC_WEAP; |
| 487 | } break; |
| 488 | |
| 489 | case CVAR_SENDROTVEL: { |
| 490 | if (val) |
| 491 | Netgame.flags |= NF_SENDROTVEL; |
| 492 | else |
| 493 | Netgame.flags &= ~NF_SENDROTVEL; |
| 494 | } break; |
| 495 | case CVAR_SETLEVEL: { |
| 496 | if (NetPlayers[Player_num].sequence == NETSEQ_PLAYING) { |
| 497 | if (val >= 1 && val <= Current_mission.num_levels) { |
| 498 | Multi_next_level = val; |
| 499 | MultiEndLevel(); |
| 500 | } |
| 501 | } else { |
| 502 | if (val >= 1) { |
| 503 | Dedicated_start_level = val; |
| 504 | } |
| 505 | } |
| 506 | } break; |
| 507 | case CVAR_ALLOWMOUSELOOKERS: { |
| 508 | if (val) |
| 509 | Netgame.flags |= NF_ALLOW_MLOOK; |
| 510 | else |
| 511 | Netgame.flags &= ~NF_ALLOW_MLOOK; |
| 512 | } break; |
| 513 | case CVAR_SETDIFF: { |
| 514 | Netgame.difficulty = val; |
| 515 | } break; |
| 516 | } |
| 517 | |
| 518 | if (dest_variable) |
| 519 | *((int *)dest_variable) = val; |
| 520 | } |
| 521 | |
| 522 | // Sets the value for a cvar FLOAT type |
| 523 | void SetCVarFloat(int index, float val) { |
| 524 | void *dest_variable; |
| 525 | |
| 526 | if (CVars[index].type != CVAR_TYPE_FLOAT) |
| 527 | return; // Only handles ints |
| 528 | |
| 529 | dest_variable = CVars[index].dest_variable; |
| 530 | |
| 531 | if (!(CVars[index].var_min == -1 && CVars[index].var_max == -1)) { |
| 532 | if (val < CVars[index].var_min) |
| 533 | val = (float)CVars[index].var_min; |
| 534 | |
| 535 | if (val > CVars[index].var_max) |
| 536 | val = (float)CVars[index].var_max; |
| 537 | } |
| 538 | |
| 539 | if (dest_variable) |
| 540 | *((float *)dest_variable) = val; |
| 541 | |
| 542 | // Do command specific stuff here |
| 543 | switch (index) { |
| 544 | case CVAR_AUDIOTAUNTDELAY: |
| 545 | MultiSetAudioTauntTime(val); |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | // Sets the value for a cvar string type |
| 550 | void SetCVarString(int index, const char *val) { |
| 551 | void *dest_variable; |
| 552 | |
| 553 | if (CVars[index].type != CVAR_TYPE_STRING) |
| 554 | return; // Only handles ints |
| 555 | |
| 556 | if (index == CVAR_MESSAGE) { |
| 557 | // say the message to everyone |
| 558 | char str[255]; |
| 559 | |
| 560 | int to_whom; |
| 561 | const char *msg = GetMessageDestination(val, &to_whom); |
| 562 | |
| 563 | if (to_whom != MULTI_SEND_MESSAGE_ALL) |
| 564 | val = msg; |
| 565 | |
| 566 | std::snprintf(str, sizeof(str), TXT_HUDSAY, Players[Player_num].callsign, val); |
| 567 | |
| 568 | MultiSendMessageFromServer(GR_RGB(0, 128, 255), str, to_whom); |
| 569 | |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | if (index == CVAR_SETTINGS) { |
| 574 | if (MultiLoadSettings(val)) { |
| 575 | PrintDedicatedMessage(TXT_DS_SETTINGSLOADED); |
| 576 | PrintDedicatedMessage("\n"); |
| 577 | } else { |
| 578 | PrintDedicatedMessage(TXT_DS_SETTINGSERR); |
| 579 | PrintDedicatedMessage("\n"); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if (index == CVAR_DISALLOW) { |
| 584 | int objid = FindObjectIDName(IGNORE_TABLE(val)); |
| 585 | if (objid != -1) { |
| 586 | PrintDedicatedMessage(TXT_DS_DISALLOWOBJECT, Object_info[objid].name); |
| 587 | PrintDedicatedMessage("\n"); |
| 588 | Object_info[objid].multi_allowed = 0; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (index == CVAR_ALLOW) { |
| 593 | int objid = FindObjectIDName(IGNORE_TABLE(val)); |
| 594 | if (objid != -1) { |
| 595 | PrintDedicatedMessage(TXT_DS_ALLOWOBJECTS, Object_info[objid].name); |
| 596 | PrintDedicatedMessage("\n"); |
| 597 | Object_info[objid].multi_allowed = 1; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | dest_variable = CVars[index].dest_variable; |
| 602 | |
| 603 | if (dest_variable) { |
| 604 | strncpy((char *)dest_variable, val, CVars[index].var_max); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | // The accessor function that sets the value of a cvar |
| 609 | void SetCVar(const char *cvar_string, const char *cvar_argument, bool game_init) { |
| 610 | int i; |
| 611 | int done = 0; |
| 612 | |
| 613 | for (i = 0; i < MAX_CVARS && !done; i++) { |
| 614 | if (!(stricmp(cvar_string, CVars[i].varname))) { |
| 615 | done = 1; |
| 616 | |
| 617 | if (game_init) { |
| 618 | if (!(CVars[i].permissions & CVAR_GAMEINIT)) { |
| 619 | PrintDedicatedMessage(TXT_DS_CANTUSEINIT); |
| 620 | PrintDedicatedMessage("\n"); |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | } else { |
| 625 | if (!(CVars[i].permissions & CVAR_GAMEPLAY)) { |
| 626 | PrintDedicatedMessage(TXT_DS_CANTUSEPLAY); |
| 627 | PrintDedicatedMessage("\n"); |
| 628 | continue; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (CVars[i].type != CVAR_TYPE_NONE && i != CVAR_MESSAGE) { |
| 633 | PrintDedicatedMessage(TXT_DS_VARSET, cvar_string, cvar_argument); |
| 634 | PrintDedicatedMessage("\n"); |
| 635 | } |
| 636 | |
| 637 | switch (CVars[i].type) { |
| 638 | case CVAR_TYPE_INT: { |
| 639 | int val = atoi(cvar_argument); |
| 640 | SetCVarInt(i, val); |
| 641 | } break; |
| 642 | case CVAR_TYPE_NONE: { |
| 643 | SetCVarNone(i); |
| 644 | } break; |
| 645 | case CVAR_TYPE_FLOAT: { |
| 646 | float val = (float)atof(cvar_argument); |
| 647 | SetCVarFloat(i, val); |
| 648 | } break; |
| 649 | case CVAR_TYPE_STRING: { |
| 650 | SetCVarString(i, cvar_argument); |
| 651 | } break; |
| 652 | default: |
| 653 | Int3(); // Get Jason, no cvar argument defined |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // The accessor function that sets the value of a cvar...INT only |
| 661 | void SetCVar(const char *cvar_string, int cvar_argument) { |
| 662 | int i; |
| 663 | int done = 0; |
| 664 | |
| 665 | for (i = 0; i < MAX_CVARS && !done; i++) { |
| 666 | if (!(stricmp(cvar_string, CVars[i].varname))) { |
| 667 | done = 1; |
| 668 | switch (CVars[i].type) { |
| 669 | case CVAR_TYPE_INT: { |
| 670 | SetCVarInt(i, cvar_argument); |
| 671 | } break; |
| 672 | |
| 673 | default: |
| 674 | Int3(); // You are using the wrong accessor function...this is for INTS only! |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // The accessor function that sets the value of a cvar...FLOAT only |
| 682 | void SetCVar(const char *cvar_string, float cvar_argument) { |
| 683 | int i; |
| 684 | int done = 0; |
| 685 | |
| 686 | for (i = 0; i < MAX_CVARS && !done; i++) { |
| 687 | if (!(stricmp(cvar_string, CVars[i].varname))) { |
| 688 | done = 1; |
| 689 | switch (CVars[i].type) { |
| 690 | case CVAR_TYPE_FLOAT: { |
| 691 | SetCVarFloat(i, cvar_argument); |
| 692 | } break; |
| 693 | |
| 694 | default: |
| 695 | Int3(); // You are using the wrong accessor function...this is for INTS only! |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | void ParseLine(char *srcline, char *command, char *operand, int cmdlen, int oprlen) { |
| 703 | // tokenize line. |
| 704 | char *opr, *cmd; |
| 705 | |
| 706 | if (strlen(srcline) == 0) |
| 707 | return; |
| 708 | |
| 709 | // parse out command and operand (command=str) |
| 710 | // m_lineptr = command, opr = operand, nextcmd = next lineptr. |
| 711 | cmd = strtok(srcline, " \t=:"); |
| 712 | if (cmd) |
| 713 | opr = strtok(NULL, ""); |
| 714 | else |
| 715 | return; // null command, so bail |
| 716 | |
| 717 | // clean out trailing and preceeding trash (tabs, spaces, etc) |
| 718 | CleanupStr(command, cmd, cmdlen); |
| 719 | if (opr) |
| 720 | CleanupStr(operand, opr, oprlen); |
| 721 | else |
| 722 | operand[0] = 0; |
| 723 | } |
| 724 | |
| 725 | // Called once per frame for the dedicated server |
| 726 | void DoDedicatedServerFrame() { |
| 727 | char str[255]; |
| 728 | char command[255] = {}; // operand |
| 729 | char operand[255]; // operand |
| 730 | |
| 731 | ListenDedicatedSocket(); |
| 732 | DedicatedReadTelnet(); |
| 733 | |
| 734 | // CallGameDLL (EVT_GAME_INTERVAL,&DLLInfo); |
| 735 | |
| 736 | if (ServerTimeout) { |
| 737 | float now = timer_GetTime(); |
| 738 | int sincelastpacket = ((float)(now - LastPacketReceived)) / 60; |
| 739 | // See if it's time to shutdown... |
| 740 | if (sincelastpacket > ServerTimeout) { |
| 741 | int quitno = DedicatedServerLex("Quit"); |
| 742 | if (quitno >= 0) |
| 743 | SetCVar(CVars[quitno].varname, "", false); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | bool new_command = con_Input(str, 255); |
| 748 | |
| 749 | if (!new_command) |
| 750 | return; |
| 751 | |
| 752 | if (str[0] == '$') { |
| 753 | DLLInfo.input_string = str; |
| 754 | CallGameDLL(EVT_CLIENT_INPUT_STRING, &DLLInfo); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | ParseLine(str, command, operand, 255, 255); |
| 759 | |
| 760 | if (!command[0]) |
| 761 | return; |
| 762 | |
| 763 | int index = DedicatedServerLex(command); |
| 764 | |
| 765 | if (index < 0) { |
| 766 | PrintDedicatedMessage(TXT_DS_BADCOMMAND); |
| 767 | PrintDedicatedMessage("\n"); |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | // Everything ok, set command |
| 772 | SetCVar(CVars[index].varname, operand, false); |
| 773 | } |
| 774 | |
| 775 | // Prints a message to the console if the dedicated server is active |
| 776 | void PrintDedicatedMessage(const char *fmt, ...) { |
| 777 | if (!Dedicated_server) |
| 778 | return; |
| 779 | |
| 780 | char buf[CON_MAX_STRINGLEN]; |
| 781 | std::va_list args; |
| 782 | |
| 783 | va_start(args, fmt); |
| 784 | std::vsnprintf(buf, CON_MAX_STRINGLEN, fmt, args); |
| 785 | va_end(args); |
| 786 | |
| 787 | con_Printf(buf); |
| 788 | DedicatedSocketputs(buf); |
| 789 | } |
| 790 | |
| 791 | #if defined(POSIX) |
| 792 | #include <netinet/in.h> |
| 793 | #include <arpa/inet.h> |
| 794 | #include <sys/socket.h> |
| 795 | #include <unistd.h> |
| 796 | |
| 797 | #include "linux_fix.h" |
| 798 | #include "errno.h" |
| 799 | #define BOOL bool |
| 800 | #ifndef SOCKET |
| 801 | #define SOCKET int |
| 802 | #endif |
| 803 | #define SOCKADDR_IN sockaddr_in |
| 804 | #define SOCKADDR sockaddr |
| 805 | #define INVALID_SOCKET -1 |
| 806 | |
| 807 | // Winsock = sockets error translation |
| 808 | |
| 809 | #define WSAEWOULDBLOCK EWOULDBLOCK |
| 810 | #define WSAEINVAL EINVAL |
| 811 | #define WSAENOPROTOOPT ENOPROTOOPT |
| 812 | |
| 813 | #ifndef WSAGetLastError |
| 814 | #define WSAGetLastError() errno |
| 815 | #endif |
| 816 | |
| 817 | #define SOCKET_ERROR -1 |
| 818 | |
| 819 | #endif |
| 820 | |
| 821 | struct dedicated_socket { |
| 822 | SOCKET sock; |
| 823 | SOCKADDR_IN addr; |
| 824 | char input[255]; |
| 825 | bool validated; |
| 826 | dedicated_socket *prev; |
| 827 | dedicated_socket *next; |
| 828 | }; |
| 829 | |
| 830 | dedicated_socket *Head_sock = NULL; |
| 831 | |
| 832 | SOCKET dedicated_listen_socket = INVALID_SOCKET; |
| 833 | |
| 834 | #define DEDICATED_LOGIN_PROMPT TXT_DS_ENTERPASS |
| 835 | |
| 836 | void InitDedicatedSocket(uint16_t port) { |
| 837 | SOCKADDR_IN sock_addr; |
| 838 | |
| 839 | memset(&sock_addr, 0, sizeof(SOCKADDR_IN)); |
| 840 | sock_addr.sin_family = AF_INET; |
| 841 | uint32_t my_ip; |
| 842 | my_ip = nw_GetThisIP(); |
| 843 | memcpy(&sock_addr.sin_addr.s_addr, &my_ip, sizeof(uint32_t)); |
| 844 | sock_addr.sin_port = htons(port); |
| 845 | |
| 846 | dedicated_listen_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 847 | |
| 848 | if (INVALID_SOCKET == dedicated_listen_socket) { |
| 849 | mprintf(0, "Unable to create listen socket for dedicated server!\n"); |
| 850 | return; |
| 851 | } |
| 852 | if (SOCKET_ERROR == bind(dedicated_listen_socket, (SOCKADDR *)&sock_addr, sizeof(sock_addr))) { |
| 853 | mprintf(0, "Unable to bind listen socket for dedicated server!\n"); |
| 854 | return; |
| 855 | } |
| 856 | if (listen(dedicated_listen_socket, 1)) { |
| 857 | mprintf(0, "Unable to listen on dedicated server socket!\n"); |
| 858 | return; |
| 859 | } |
| 860 | make_nonblocking(dedicated_listen_socket); |
| 861 | } |
| 862 | |
| 863 | void ListenDedicatedSocket(void) { |
| 864 | SOCKET incoming_socket; |
| 865 | SOCKADDR_IN conn_addr; |
| 866 | // int addrlen = sizeof(conn_addr); |
| 867 | socklen_t addrlen = sizeof(conn_addr); |
| 868 | incoming_socket = accept(dedicated_listen_socket, (SOCKADDR *)&conn_addr, &addrlen); |
| 869 | if (INVALID_SOCKET != incoming_socket) { |
| 870 | // Make the socket non-blocking |
| 871 | |
| 872 | make_nonblocking(incoming_socket); |
| 873 | |
| 874 | if (!Dedicated_allow_remote) { |
| 875 | // Check to see if this came in from the local address |
| 876 | uint32_t localhost = inet_addr("127.0.0.1"); |
| 877 | if (memcmp(&localhost, &conn_addr.sin_addr, sizeof(localhost)) != 0) { |
| 878 | mprintf(0, "Rejecting connection from remote host!\n"); |
| 879 | PrintDedicatedMessage(TXT_DS_REJECTREMOTE, inet_ntoa(conn_addr.sin_addr)); |
| 880 | PrintDedicatedMessage("\n"); |
| 881 | shutdown(incoming_socket, 2); |
| 882 | #if defined(WIN32) |
| 883 | closesocket(incoming_socket); |
| 884 | #else |
| 885 | close(incoming_socket); |
| 886 | #endif |
| 887 | return; |
| 888 | } |
| 889 | } |
| 890 | // At this point it's considered a valid connection |
| 891 | PrintDedicatedMessage(TXT_DS_NEWCONNECT, inet_ntoa(conn_addr.sin_addr)); |
| 892 | PrintDedicatedMessage("\n"); |
| 893 | dedicated_socket *new_socket; |
| 894 | new_socket = (dedicated_socket *)mem_malloc(sizeof(dedicated_socket)); |
| 895 | if (Head_sock) |
| 896 | Head_sock->prev = new_socket; |
| 897 | new_socket->next = Head_sock; |
| 898 | new_socket->prev = NULL; |
| 899 | Head_sock = new_socket; |
| 900 | memcpy(&new_socket->addr, &conn_addr, sizeof(new_socket->addr)); |
| 901 | new_socket->input[0] = '\0'; |
| 902 | new_socket->sock = incoming_socket; |
| 903 | new_socket->validated = false; |
| 904 | // Give the new connection the login prompt |
| 905 | send(new_socket->sock, DEDICATED_LOGIN_PROMPT, strlen(DEDICATED_LOGIN_PROMPT) + 1, 0); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | void DedicatedSocketputs(char *str) { |
| 910 | char newstr[300]; |
| 911 | char buf[2]; |
| 912 | |
| 913 | memset(newstr, 0, 300); |
| 914 | int len = strlen(str); |
| 915 | for (int i = 0; i < len; i++) { |
| 916 | if (str[i] == '\n') { |
| 917 | strcat(newstr, "\r\n"); |
| 918 | } else { |
| 919 | buf[0] = str[i]; |
| 920 | buf[1] = '\0'; |
| 921 | strcat(newstr, buf); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | dedicated_socket *conn; |
| 926 | conn = Head_sock; |
| 927 | while (conn) { |
| 928 | if (conn->validated) { |
| 929 | send(conn->sock, newstr, strlen(newstr) + 1, 0); |
| 930 | } |
| 931 | conn = conn->next; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | dedicated_socket *DedicatedLogoutTelnet(dedicated_socket *conn) { |
| 936 | // user is no longer valid (so we don't attempt to send them a message |
| 937 | conn->validated = false; |
| 938 | |
| 939 | PrintDedicatedMessage(TXT_DS_REMOTECLOSE, inet_ntoa(conn->addr.sin_addr)); |
| 940 | PrintDedicatedMessage("\n"); |
| 941 | |
| 942 | shutdown(conn->sock, 2); |
| 943 | #if defined(WIN32) |
| 944 | closesocket(conn->sock); |
| 945 | #else |
| 946 | close(conn->sock); |
| 947 | #endif |
| 948 | |
| 949 | dedicated_socket *prev; |
| 950 | prev = conn->prev; |
| 951 | if (prev) { |
| 952 | prev->next = conn->next; |
| 953 | } else { |
| 954 | Head_sock = conn->next; |
| 955 | if (Head_sock) |
| 956 | Head_sock->prev = NULL; |
| 957 | } |
| 958 | |
| 959 | mem_free(conn); |
| 960 | return prev; |
| 961 | } |
| 962 | |
| 963 | void DedicatedReadTelnet(void) { |
| 964 | dedicated_socket *conn; |
| 965 | char buf[5]; |
| 966 | conn = Head_sock; |
| 967 | |
| 968 | while (conn) { |
| 969 | int bytesread = 1; |
| 970 | while (bytesread >= 0 && conn) { |
| 971 | // Read one byte at a time, looking for a CR |
| 972 | bytesread = recv(conn->sock, buf, 1, 0); |
| 973 | if (bytesread == 0) { |
| 974 | // Connection closed. Remove it from the list, and inform the other(?) users. |
| 975 | conn = DedicatedLogoutTelnet(conn); |
| 976 | break; |
| 977 | } else if (bytesread > 0) { |
| 978 | buf[1] = '\0'; |
| 979 | // When we see a CR, process the line |
| 980 | if ((buf[0] == 0x0a) || (buf[0] == 0x0d)) { |
| 981 | if (conn->input[0] == 0) { |
| 982 | continue; |
| 983 | } |
| 984 | send(conn->sock, "\r\n", strlen("\r\n"), 0); |
| 985 | if (conn->validated) { |
| 986 | char command[255] = {}; // operand |
| 987 | char operand[255]; // operand |
| 988 | |
| 989 | if (!stricmp(conn->input, "logout")) { |
| 990 | // log the connection out |
| 991 | conn = DedicatedLogoutTelnet(conn); |
| 992 | } else { |
| 993 | // Process the string |
| 994 | PrintDedicatedMessage("[%s] %s\n", inet_ntoa(conn->addr.sin_addr), conn->input); |
| 995 | if (conn->input[0] == '$') { |
| 996 | DLLInfo.input_string = conn->input; |
| 997 | CallGameDLL(EVT_CLIENT_INPUT_STRING, &DLLInfo); |
| 998 | conn->input[0] = '\0'; |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | ParseLine(conn->input, command, operand, 255, 255); |
| 1003 | conn->input[0] = '\0'; |
| 1004 | if (!command[0]) |
| 1005 | return; |
| 1006 | |
| 1007 | int index = DedicatedServerLex(command); |
| 1008 | |
| 1009 | if (index < 0) { |
| 1010 | PrintDedicatedMessage(TXT_DS_BADCOMMAND); |
| 1011 | PrintDedicatedMessage("\n"); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | // Everything ok, set command |
| 1016 | SetCVar(CVars[index].varname, operand, false); |
| 1017 | } |
| 1018 | } else { |
| 1019 | // All we want to see from them is the correct password |
| 1020 | if (strcmp(conn->input, dedicated_telnet_password) == 0) { |
| 1021 | // They logged in ok! |
| 1022 | conn->validated = true; |
| 1023 | send(conn->sock, "\r\n", strlen("\r\n") + 1, 0); |
| 1024 | PrintDedicatedMessage(TXT_DS_REMOTELOGGEDIN, inet_ntoa(conn->addr.sin_addr)); |
| 1025 | PrintDedicatedMessage("\n"); |
| 1026 | conn->input[0] = '\0'; |
| 1027 | } else { |
| 1028 | PrintDedicatedMessage(TXT_DS_BADPASS, inet_ntoa(conn->addr.sin_addr)); |
| 1029 | PrintDedicatedMessage("\n"); |
| 1030 | shutdown(conn->sock, 2); |
| 1031 | #if defined(WIN32) |
| 1032 | closesocket(conn->sock); |
| 1033 | #else |
| 1034 | close(conn->sock); |
| 1035 | #endif |
| 1036 | } |
| 1037 | } |
| 1038 | } else { |
| 1039 | if (buf[0] == 8) { |
| 1040 | // handle backspace |
| 1041 | char delstr[5]; |
| 1042 | delstr[0] = 8; |
| 1043 | delstr[1] = ' '; |
| 1044 | delstr[2] = 8; |
| 1045 | delstr[3] = '\0'; |
| 1046 | conn->input[strlen(conn->input) - 1] = '\0'; |
| 1047 | buf[1] = '\0'; |
| 1048 | send(conn->sock, delstr, strlen(delstr), 0); |
| 1049 | } else { |
| 1050 | if (strlen(conn->input) + bytesread >= sizeof(conn->input)) { |
| 1051 | conn->input[0] = 0; |
| 1052 | const char *s = "\n\rCommand line too long\n\r"; |
| 1053 | send(conn->sock, s, strlen(s), 0); |
| 1054 | } else { |
| 1055 | strcat(conn->input, buf); |
| 1056 | send(conn->sock, buf, 1, 0); |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | if (conn) |
| 1063 | conn = conn->next; |
| 1064 | } |
| 1065 | } |