1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /**
9  * @file config.cpp Configuration of the connection strings for network stuff using environment variables.
10  */
11 
12 #include "../../stdafx.h"
13 
14 #include <cstdlib>
15 #include "../../string_func.h"
16 
17 #include "../../safeguards.h"
18 
19 /**
20  * Get the environment variable using std::getenv and when it is an empty string (or nullptr), return a fallback value instead.
21  * @param variable The environment variable to read from.
22  * @param fallback The fallback in case the environment variable is not set.
23  * @return The environment value, or when that does not exist the given fallback value.
24  */
GetEnv(const char * variable,const char * fallback)25 static const char *GetEnv(const char *variable, const char *fallback)
26 {
27 	const char *value = std::getenv(variable);
28 	return StrEmpty(value) ? fallback : value;
29 }
30 
31 /**
32  * Get the connection string for the game coordinator from the environment variable OTTD_COORDINATOR_CS,
33  * or when it has not been set a hard coded default DNS hostname of the production server.
34  * @return The game coordinator's connection string.
35  */
NetworkCoordinatorConnectionString()36 const char *NetworkCoordinatorConnectionString()
37 {
38 	return GetEnv("OTTD_COORDINATOR_CS", "coordinator.openttd.org");
39 }
40 
41 /**
42  * Get the connection string for the STUN server from the environment variable OTTD_STUN_CS,
43  * or when it has not been set a hard coded default DNS hostname of the production server.
44  * @return The STUN server's connection string.
45  */
NetworkStunConnectionString()46 const char *NetworkStunConnectionString()
47 {
48 	return GetEnv("OTTD_STUN_CS", "stun.openttd.org");
49 }
50 
51 /**
52  * Get the connection string for the content server from the environment variable OTTD_CONTENT_SERVER_CS,
53  * or when it has not been set a hard coded default DNS hostname of the production server.
54  * @return The content server's connection string.
55  */
NetworkContentServerConnectionString()56 const char *NetworkContentServerConnectionString()
57 {
58 	return GetEnv("OTTD_CONTENT_SERVER_CS", "content.openttd.org");
59 }
60 
61 /**
62  * Get the connection string for the content mirror from the environment variable OTTD_CONTENT_MIRROR_CS,
63  * or when it has not been set a hard coded default DNS hostname of the production server.
64  * @return The content mirror's connection string.
65  */
NetworkContentMirrorConnectionString()66 const char *NetworkContentMirrorConnectionString()
67 {
68 	return GetEnv("OTTD_CONTENT_MIRROR_CS", "binaries.openttd.org");
69 }
70