diff --git a/console_cmds.c b/console_cmds.c index d344d8e958..f9b8d72717 100644 --- a/console_cmds.c +++ b/console_cmds.c @@ -657,8 +657,8 @@ DEF_CONSOLE_CMD(ConSet) { return NULL; } - // setting the server name - if (strcmp(argv[1],"port") == 0) { + // setting the server port + if (strcmp(argv[1],"server_port") == 0) { if (argc == 3 && atoi(argv[2]) != 0) { _network_server_port = atoi(argv[2]); IConsolePrintF(_iconsole_color_warning, "Server-port changed to '%d'", _network_server_port); @@ -670,7 +670,22 @@ DEF_CONSOLE_CMD(ConSet) { return NULL; } -#endif + // setting the server ip + if (strcmp(argv[1],"server_bind_ip") == 0 || strcmp(argv[1],"server_ip_bind") == 0 || + strcmp(argv[1],"server_ip") == 0 || strcmp(argv[1],"server_bind") == 0) { + if (argc == 3) { + _network_server_bind_ip = inet_addr(argv[2]); + snprintf(_network_server_bind_ip_host, sizeof(_network_server_bind_ip_host), "%s", inet_ntoa(*(struct in_addr *)&_network_server_bind_ip)); + IConsolePrintF(_iconsole_color_warning, "Server-bind-ip changed to '%s'", _network_server_bind_ip_host); + IConsolePrintF(_iconsole_color_warning, "Changes will take effect the next time you start a server."); + } else { + IConsolePrintF(_iconsole_color_default, "Current server-bind-ip is '%s'", _network_server_bind_ip_host); + IConsolePrint(_iconsole_color_warning, "Usage: set server_bind_ip ."); + } + return NULL; + } + +#endif /* ENABLE_NETWORK */ // Patch-options if (strcmp(argv[1],"patch") == 0) { @@ -688,7 +703,17 @@ DEF_CONSOLE_CMD(ConSet) { } - IConsolePrintF(_iconsole_color_error,"Unknown setting"); + IConsolePrint(_iconsole_color_error, "Unknown setting"); + IConsolePrint(_iconsole_color_error, "Known settings are:"); +#ifdef ENABLE_NETWORK + IConsolePrint(_iconsole_color_error, " - server_pw \"\""); + IConsolePrint(_iconsole_color_error, " - company_pw \"\""); + IConsolePrint(_iconsole_color_error, " - name \"\""); + IConsolePrint(_iconsole_color_error, " - servername \"\""); + IConsolePrint(_iconsole_color_error, " - server_port "); + IConsolePrint(_iconsole_color_error, " - server_bind_ip "); +#endif /* ENABLE_NETWORK */ + IConsolePrint(_iconsole_color_error, " - patch []"); return NULL; } diff --git a/network.c b/network.c index ddab48f2c0..836ccb6e01 100644 --- a/network.c +++ b/network.c @@ -623,7 +623,7 @@ bool NetworkListen(void) port = _network_server_port; - DEBUG(net, 1) ("[NET] Listening on port %d", port); + DEBUG(net, 1) ("[NET] Listening on %s:%d", _network_server_bind_ip_host, port); ls = socket(AF_INET, SOCK_STREAM, 0); if (ls == INVALID_SOCKET) { @@ -652,7 +652,7 @@ bool NetworkListen(void) } sin.sin_family = AF_INET; - sin.sin_addr.s_addr = 0; + sin.sin_addr.s_addr = _network_server_bind_ip; sin.sin_port = htons(port); if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) { @@ -841,13 +841,17 @@ bool NetworkServerStart(void) { if (!_network_available) return false; + /* Call the pre-scripts */ + IConsoleCmdExec("exec scripts/pre_server.scr 0"); + if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0"); + NetworkInitialize(); if (!NetworkListen()) return false; // Try to start UDP-server _network_udp_server = true; - _network_udp_server = NetworkUDPListen(0, _network_server_port); + _network_udp_server = NetworkUDPListen(_network_server_bind_ip, _network_server_port); _network_server = true; _networking = true; @@ -1157,6 +1161,11 @@ void NetworkStartUp(void) _network_available = true; _network_dedicated = false; + /* Load the ip from the openttd.cfg */ + _network_server_bind_ip = inet_addr(_network_server_bind_ip_host); + /* And put the data back in it in case it was an invalid ip */ + snprintf(_network_server_bind_ip_host, sizeof(_network_server_bind_ip_host), "%s", inet_ntoa(*(struct in_addr *)&_network_server_bind_ip)); + /* Generate an unique id when there is none yet */ if (_network_unique_id[0] == '\0') NetworkGenerateUniqueId(); diff --git a/network.h b/network.h index 7b8674211d..d7092383dc 100644 --- a/network.h +++ b/network.h @@ -138,6 +138,11 @@ VARDEF uint16 _network_game_count; VARDEF uint16 _network_lobby_company_count; VARDEF uint _network_server_port; +/* We use bind_ip and bind_ip_host, where bind_ip_host is the readable form of + bind_ip_host, and bind_ip the numeric value, because we want a nice number + in the openttd.cfg, but we wants to use the uint32 internally.. */ +VARDEF uint32 _network_server_bind_ip; +VARDEF char _network_server_bind_ip_host[NETWORK_HOSTNAME_LENGTH]; VARDEF bool _is_network_server; // Does this client wants to be a network-server? VARDEF char _network_server_name[NETWORK_NAME_LENGTH]; diff --git a/network_udp.c b/network_udp.c index 030901aee1..dd249591b6 100644 --- a/network_udp.c +++ b/network_udp.c @@ -182,7 +182,7 @@ bool NetworkUDPListen(uint32 host, uint16 port) sin.sin_port = htons(port); if (bind(udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) { - DEBUG(net, 1) ("[NET][UDP] error: bind failed on port %i", port); + DEBUG(net, 1) ("[NET][UDP] error: bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port); return false; } @@ -200,7 +200,7 @@ bool NetworkUDPListen(uint32 host, uint16 port) else _udp_client_socket = udp; - DEBUG(net, 1)("[NET][UDP] Listening on port %d", port); + DEBUG(net, 1)("[NET][UDP] Listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port); return true; } diff --git a/scripts/pre_dedicated.scr.example b/scripts/pre_dedicated.scr.example new file mode 100644 index 0000000000..c4a9e2e8fa --- /dev/null +++ b/scripts/pre_dedicated.scr.example @@ -0,0 +1,2 @@ +set server_ip 0.0.0.0 +set server_port 3979 diff --git a/scripts/pre_server.scr.example b/scripts/pre_server.scr.example new file mode 100644 index 0000000000..25e8756f20 --- /dev/null +++ b/scripts/pre_server.scr.example @@ -0,0 +1 @@ +set server_port 3979 diff --git a/scripts/readme.txt b/scripts/readme.txt index 99535e37cf..3cd4e4c323 100644 --- a/scripts/readme.txt +++ b/scripts/readme.txt @@ -6,6 +6,8 @@ OpenTTD supports scripts. - 'on_client.scr' is executed when you join a server as client - 'on_server.scr' is executed when you start a server / dedicated server - 'on_dedicated.scr' is additionally executed when you start a dedicated server + - 'pre_server.scr' is executed before the server is started + - 'pre_dedeicated.scr' is additionally executed when you start a dedicated server For examples how a script can look, check the .example examples. diff --git a/settings.c b/settings.c index ef8862e20c..dfa22e53d9 100644 --- a/settings.c +++ b/settings.c @@ -725,6 +725,7 @@ static const SettingDesc misc_settings[] = { static const SettingDesc network_settings[] = { {"sync_freq", SDT_UINT16 | SDT_NOSAVE, (void*)100, &_network_sync_freq, NULL}, {"frame_freq", SDT_UINT8 | SDT_NOSAVE, (void*)0, &_network_frame_freq, NULL}, + {"server_bind_ip", SDT_STRINGBUF | (lengthof(_network_server_bind_ip_host) << 16), NULL, &_network_server_bind_ip_host, NULL}, {"server_port", SDT_UINT, (void*)NETWORK_DEFAULT_PORT, &_network_server_port, NULL}, {"player_name", SDT_STRINGBUF | (lengthof(_network_player_name) << 16), NULL, &_network_player_name, NULL}, {"server_password", SDT_STRINGBUF | (lengthof(_network_game_info.server_password) << 16), NULL, &_network_game_info.server_password, NULL}, diff --git a/settings_gui.c b/settings_gui.c index ed7076199c..20a5696a10 100644 --- a/settings_gui.c +++ b/settings_gui.c @@ -1005,7 +1005,7 @@ void ConsoleSetPatchSetting(char *name, char *value) /* We did not found the patch setting */ if (!found) { - IConsolePrintF(_iconsole_color_warning, "'%s' is an unkown patch settings", name); + IConsolePrintF(_iconsole_color_warning, "'%s' is an unkown patch setting", name); return; } @@ -1063,7 +1063,7 @@ void ConsoleGetPatchSetting(char *name) /* We did not found the patch setting */ if (!found) { - IConsolePrintF(_iconsole_color_warning, "'%s' is an unkown patch settings", name); + IConsolePrintF(_iconsole_color_warning, "'%s' is an unkown patch setting", name); return; }