summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-04-29 12:09:03 +0200
committerPatric Stout <github@truebrain.nl>2021-04-29 20:12:11 +0200
commitbe37a2cab831cb645ef0f51dbcc944bd750f6926 (patch)
tree17efb6066721db8ce4974cee192cbd8a8193821a /src/network
parent99f998805ba835f48a8061762fa19761760c7451 (diff)
downloadopenttd-be37a2cab831cb645ef0f51dbcc944bd750f6926.tar.xz
Codechange: use NetworkAddress instead of two host/port variables where possible
This also means we no longer need last_host/last_port, but can just use a single last_joined setting.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/core/address.cpp4
-rw-r--r--src/network/core/config.h1
-rw-r--r--src/network/core/tcp_http.cpp6
-rw-r--r--src/network/network.cpp117
-rw-r--r--src/network/network_func.h10
-rw-r--r--src/network/network_gamelist.cpp2
-rw-r--r--src/network/network_gui.cpp23
7 files changed, 84 insertions, 79 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp
index 8b51e2c08..e91751c33 100644
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -101,8 +101,8 @@ void NetworkAddress::GetAddressAsString(char *buffer, const char *last, bool wit
*/
std::string NetworkAddress::GetAddressAsString(bool with_family)
{
- /* 6 = for the : and 5 for the decimal port number */
- char buf[NETWORK_HOSTNAME_LENGTH + 6 + 7];
+ /* 7 extra are for with_family, which adds " (IPvX)". */
+ char buf[NETWORK_HOSTNAME_PORT_LENGTH + 7];
this->GetAddressAsString(buf, lastof(buf), with_family);
return buf;
}
diff --git a/src/network/core/config.h b/src/network/core/config.h
index 866d1791d..cacc907fa 100644
--- a/src/network/core/config.h
+++ b/src/network/core/config.h
@@ -56,6 +56,7 @@ static const byte NETWORK_MASTER_SERVER_VERSION = 2; ///< What vers
static const uint NETWORK_NAME_LENGTH = 80; ///< The maximum length of the server name and map name, in bytes including '\0'
static const uint NETWORK_COMPANY_NAME_LENGTH = 128; ///< The maximum length of the company name, in bytes including '\0'
static const uint NETWORK_HOSTNAME_LENGTH = 80; ///< The maximum length of the host name, in bytes including '\0'
+static const uint NETWORK_HOSTNAME_PORT_LENGTH = 80 + 6; ///< The maximum length of the host name + port, in bytes including '\0'. The extra six is ":" + port number (with a max of 65536)
static const uint NETWORK_SERVER_ID_LENGTH = 33; ///< The maximum length of the network id of the servers, in bytes including '\0'
static const uint NETWORK_REVISION_LENGTH = 33; ///< The maximum length of the revision, in bytes including '\0'
static const uint NETWORK_PASSWORD_LENGTH = 33; ///< The maximum length of the password, in bytes including '\0' (must be >= NETWORK_SERVER_ID_LENGTH)
diff --git a/src/network/core/tcp_http.cpp b/src/network/core/tcp_http.cpp
index ee74c4507..99eca1cb7 100644
--- a/src/network/core/tcp_http.cpp
+++ b/src/network/core/tcp_http.cpp
@@ -203,11 +203,7 @@ int NetworkHTTPSocketHandler::HandleHeader()
*url = '\0';
- /* Fetch the hostname, and possible port number. */
- const char *port = nullptr;
- ParseConnectionString(&port, hname);
-
- NetworkAddress address(hname, port == nullptr ? 80 : atoi(port));
+ NetworkAddress address = ParseConnectionString(hname, 80);
/* Restore the URL. */
*url = '/';
diff --git a/src/network/network.cpp b/src/network/network.cpp
index c0b2e06fd..0be46bbf3 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -447,14 +447,14 @@ static void CheckPauseOnJoin()
}
/**
- * Converts a string to ip/port
- * Format: IP:port
+ * Converts a string to ip/port/company
+ * Format: IP:port#company
*
* connection_string will be re-terminated to separate out the hostname, port will
* be set to the port strings given by the user, inside the memory area originally
- * occupied by connection_string.
+ * occupied by connection_string. Similar for company, if set.
*/
-void ParseConnectionString(const char **port, char *connection_string)
+void ParseFullConnectionString(const char **company, const char **port, char *connection_string)
{
bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
for (char *p = connection_string; *p != '\0'; p++) {
@@ -467,6 +467,12 @@ void ParseConnectionString(const char **port, char *connection_string)
ipv6 = false;
break;
+ case '#':
+ if (company == nullptr) continue;
+ *company = p + 1;
+ *p = '\0';
+ break;
+
case ':':
if (ipv6) break;
*port = p + 1;
@@ -477,38 +483,48 @@ void ParseConnectionString(const char **port, char *connection_string)
}
/**
- * Converts a string to ip/port/company
- * Format: IP:port#company
+ * Convert a string containing either "hostname" or "hostname:ip" to a
+ * NetworkAddress.
*
- * connection_string will be re-terminated to separate out the hostname, and company and port will
- * be set to the company and port strings given by the user, inside the memory area originally
- * occupied by connection_string.
+ * @param connection_string The string to parse.
+ * @param default_port The default port to set port to if not in connection_string.
+ * @return A valid NetworkAddress of the parsed information.
*/
-void ParseGameConnectionString(const char **company, const char **port, char *connection_string)
+NetworkAddress ParseConnectionString(const char *connection_string, int default_port)
{
- bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':'));
- for (char *p = connection_string; *p != '\0'; p++) {
- switch (*p) {
- case '[':
- ipv6 = true;
- break;
+ char internal_connection_string[NETWORK_HOSTNAME_PORT_LENGTH];
+ strecpy(internal_connection_string, connection_string, lastof(internal_connection_string));
- case ']':
- ipv6 = false;
- break;
+ const char *port = nullptr;
+ ParseFullConnectionString(nullptr, &port, internal_connection_string);
- case '#':
- *company = p + 1;
- *p = '\0';
- break;
+ int rport = port != nullptr ? atoi(port) : default_port;
+ return NetworkAddress(internal_connection_string, rport);
+}
- case ':':
- if (ipv6) break;
- *port = p + 1;
- *p = '\0';
- break;
- }
- }
+/**
+ * Convert a string containing either "hostname" or "hostname:ip" to a
+ * NetworkAddress, where the string can be postfixed with "#company" to
+ * indicate the requested company.
+ *
+ * @param company Pointer to the company variable to set iff indicted.
+ * @param connection_string The string to parse.
+ * @param default_port The default port to set port to if not in connection_string.
+ * @return A valid NetworkAddress of the parsed information.
+ */
+NetworkAddress ParseGameConnectionString(CompanyID *company, const char *connection_string, int default_port)
+{
+ char internal_connection_string[NETWORK_HOSTNAME_PORT_LENGTH + 4]; // 4 extra for the "#" and company
+ strecpy(internal_connection_string, connection_string, lastof(internal_connection_string));
+
+ const char *port_s = nullptr;
+ const char *company_s = nullptr;
+ ParseFullConnectionString(&company_s, &port_s, internal_connection_string);
+
+ if (company_s != nullptr) *company = (CompanyID)atoi(company_s);
+
+ int port = port_s != nullptr ? atoi(port_s) : default_port;
+ return NetworkAddress(internal_connection_string, port);
}
/**
@@ -616,26 +632,17 @@ void NetworkTCPQueryServer(NetworkAddress address)
new TCPQueryConnecter(address);
}
-/* Validates an address entered as a string and adds the server to
+/**
+ * Validates an address entered as a string and adds the server to
* the list. If you use this function, the games will be marked
- * as manually added. */
-void NetworkAddServer(const char *b)
+ * as manually added.
+ * @param connection_string The IP:port to add to the list.
+ */
+void NetworkAddServer(const char *connection_string)
{
- if (*b != '\0') {
- const char *port = nullptr;
- char host[NETWORK_HOSTNAME_LENGTH];
- uint16 rport;
-
- strecpy(host, b, lastof(host));
-
- strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip));
- rport = NETWORK_DEFAULT_PORT;
+ if (StrEmpty(connection_string)) return;
- ParseConnectionString(&port, host);
- if (port != nullptr) rport = atoi(port);
-
- NetworkUDPQueryServer(NetworkAddress(host, rport), true);
- }
+ NetworkUDPQueryServer(ParseConnectionString(connection_string, NETWORK_DEFAULT_PORT), true);
}
/**
@@ -688,16 +695,13 @@ public:
/* Used by clients, to connect to a server */
-void NetworkClientConnectGame(const char *hostname, uint16 port, CompanyID join_as, const char *join_server_password, const char *join_company_password)
+void NetworkClientConnectGame(NetworkAddress &address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
{
if (!_network_available) return;
-
- if (port == 0) return;
-
if (!NetworkValidateClientName()) return;
- strecpy(_settings_client.network.last_host, hostname, lastof(_settings_client.network.last_host));
- _settings_client.network.last_port = port;
+ strecpy(_settings_client.network.last_joined, address.GetAddressAsString(false).c_str(), lastof(_settings_client.network.last_joined));
+
_network_join_as = join_as;
_network_join_server_password = join_server_password;
_network_join_company_password = join_company_password;
@@ -708,7 +712,7 @@ void NetworkClientConnectGame(const char *hostname, uint16 port, CompanyID join_
_network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
ShowJoinStatusWindow();
- new TCPClientConnecter(NetworkAddress(hostname, port));
+ new TCPClientConnecter(address);
}
static void NetworkInitGameInfo()
@@ -1059,13 +1063,12 @@ static void NetworkGenerateServerId()
seprintf(_settings_client.network.network_id, lastof(_settings_client.network.network_id), "%s", hex_output);
}
-void NetworkStartDebugLog(const char *hostname, uint16 port)
+void NetworkStartDebugLog(NetworkAddress &address)
{
extern SOCKET _debug_socket; // Comes from debug.c
- DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", hostname, port);
+ DEBUG(net, 0, "Redirecting DEBUG() to %s", address.GetAddressAsString().c_str());
- NetworkAddress address(hostname, port);
SOCKET s = address.Connect();
if (s == INVALID_SOCKET) {
DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
diff --git a/src/network/network_func.h b/src/network/network_func.h
index a0f163345..967bb6260 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -18,6 +18,7 @@
// #define DEBUG_FAILED_DUMP_COMMANDS
#include "network_type.h"
+#include "core/address.h"
#include "../console_type.h"
#include "../gfx_type.h"
#include "../openttd.h"
@@ -45,14 +46,15 @@ void NetworkReboot();
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
void NetworkGameLoop();
void NetworkBackgroundLoop();
-void ParseConnectionString(const char **port, char *connection_string);
-void ParseGameConnectionString(const char **company, const char **port, char *connection_string);
-void NetworkStartDebugLog(const char *hostname, uint16 port);
+void ParseFullConnectionString(const char **company, const char **port, char *connection_string);
+NetworkAddress ParseConnectionString(const char *connection_string, int default_port);
+NetworkAddress ParseGameConnectionString(CompanyID *company, const char *connection_string, int default_port);
+void NetworkStartDebugLog(NetworkAddress &address);
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
void NetworkUpdateClientInfo(ClientID client_id);
void NetworkClientsToSpectators(CompanyID cid);
-void NetworkClientConnectGame(const char *hostname, uint16 port, CompanyID join_as, const char *join_server_password = nullptr, const char *join_company_password = nullptr);
+void NetworkClientConnectGame(NetworkAddress &address, CompanyID join_as, const char *join_server_password = nullptr, const char *join_company_password = nullptr);
void NetworkClientRequestMove(CompanyID company, const char *pass = "");
void NetworkClientSendRcon(const char *password, const char *command);
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
diff --git a/src/network/network_gamelist.cpp b/src/network/network_gamelist.cpp
index 3ee5099b7..6d4285c85 100644
--- a/src/network/network_gamelist.cpp
+++ b/src/network/network_gamelist.cpp
@@ -151,7 +151,7 @@ void NetworkGameListRequery()
/* item gets mostly zeroed by NetworkUDPQueryServer */
uint8 retries = item->retries;
- NetworkUDPQueryServer(NetworkAddress(item->address));
+ NetworkUDPQueryServer(item->address);
item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
}
}
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index e1f9a791b..3204b2b96 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -472,7 +472,7 @@ public:
EM_ASM(if (window["openttd_server_list"]) openttd_server_list());
#endif
- this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
+ this->last_joined = NetworkGameListAddItem(ParseConnectionString(_settings_client.network.last_joined, NETWORK_DEFAULT_PORT));
this->server = this->last_joined;
if (this->last_joined != nullptr) NetworkUDPQueryServer(this->last_joined->address);
@@ -735,7 +735,7 @@ public:
ShowQueryString(
STR_JUST_RAW_STRING,
STR_NETWORK_SERVER_LIST_ENTER_IP,
- NETWORK_HOSTNAME_LENGTH, // maximum number of characters including '\0'
+ NETWORK_HOSTNAME_PORT_LENGTH, // maximum number of characters including '\0'
this, CS_ALPHANUMERAL, QSF_ACCEPT_UNCHANGED);
break;
@@ -745,8 +745,6 @@ public:
case WID_NG_JOIN: // Join Game
if (this->server != nullptr) {
- seprintf(_settings_client.network.last_host, lastof(_settings_client.network.last_host), "%s", this->server->address.GetHostname());
- _settings_client.network.last_port = this->server->address.GetPort();
ShowNetworkLobbyWindow(this->server);
}
break;
@@ -827,7 +825,10 @@ public:
void OnQueryTextFinished(char *str) override
{
- if (!StrEmpty(str)) NetworkAddServer(str);
+ if (!StrEmpty(str)) {
+ strecpy(_settings_client.network.connect_to_ip, str, lastof(_settings_client.network.connect_to_ip));
+ NetworkAddServer(str);
+ }
}
void OnResize() override
@@ -1469,22 +1470,22 @@ struct NetworkLobbyWindow : public Window {
case WID_NL_JOIN: // Join company
/* Button can be clicked only when it is enabled. */
- NetworkClientConnectGame(_settings_client.network.last_host, _settings_client.network.last_port, this->company);
+ NetworkClientConnectGame(this->server->address, this->company);
break;
case WID_NL_NEW: // New company
- NetworkClientConnectGame(_settings_client.network.last_host, _settings_client.network.last_port, COMPANY_NEW_COMPANY);
+ NetworkClientConnectGame(this->server->address, COMPANY_NEW_COMPANY);
break;
case WID_NL_SPECTATE: // Spectate game
- NetworkClientConnectGame(_settings_client.network.last_host, _settings_client.network.last_port, COMPANY_SPECTATOR);
+ NetworkClientConnectGame(this->server->address, COMPANY_SPECTATOR);
break;
case WID_NL_REFRESH: // Refresh
/* Clear the information so removed companies don't remain */
for (auto &company : this->company_info) company = {};
- NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
+ NetworkTCPQueryServer(this->server->address);
break;
}
}
@@ -1552,7 +1553,9 @@ static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
- NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
+ strecpy(_settings_client.network.last_joined, ngl->address.GetAddressAsString(false).c_str(), lastof(_settings_client.network.last_joined));
+
+ NetworkTCPQueryServer(ngl->address);
new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
}