diff options
author | Patric Stout <truebrain@openttd.org> | 2021-04-20 16:26:07 +0200 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-04-20 17:34:04 +0200 |
commit | 31897eaa7de865d41b46f7158c70f94debe1ec66 (patch) | |
tree | 5575e5bf60b6555d291a0bb25aa43fc35f6980e1 /src/network | |
parent | 05612d60ae0af94b9313d5a8b78ebf58a3eeab66 (diff) | |
download | openttd-31897eaa7de865d41b46f7158c70f94debe1ec66.tar.xz |
Codechange: split ParseConnectionString into two functions
One also looks for a company, the other doesn't. There were more
uses of the latter than the first, leaving very weird code all
over the place.
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/core/tcp_http.cpp | 4 | ||||
-rw-r--r-- | src/network/network.cpp | 38 | ||||
-rw-r--r-- | src/network/network_func.h | 3 |
3 files changed, 36 insertions, 9 deletions
diff --git a/src/network/core/tcp_http.cpp b/src/network/core/tcp_http.cpp index be5a2c39a..d88ea711d 100644 --- a/src/network/core/tcp_http.cpp +++ b/src/network/core/tcp_http.cpp @@ -203,10 +203,8 @@ int NetworkHTTPSocketHandler::HandleHeader() *url = '\0'; /* Fetch the hostname, and possible port number. */ - const char *company = nullptr; const char *port = nullptr; - ParseConnectionString(&company, &port, hname); - if (company != nullptr) return_error("[tcp/http] invalid hostname"); + ParseConnectionString(&port, hname); NetworkAddress address(hname, port == nullptr ? 80 : atoi(port)); diff --git a/src/network/network.cpp b/src/network/network.cpp index f4640191c..e0355ce2e 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -447,6 +447,36 @@ static void CheckPauseOnJoin() } /** + * Converts a string to ip/port + * Format: IP:port + * + * 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. + */ +void ParseConnectionString(const char **port, char *connection_string) +{ + bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':')); + for (char *p = connection_string; *p != '\0'; p++) { + switch (*p) { + case '[': + ipv6 = true; + break; + + case ']': + ipv6 = false; + break; + + case ':': + if (ipv6) break; + *port = p + 1; + *p = '\0'; + break; + } + } +} + +/** * Converts a string to ip/port/company * Format: IP:port#company * @@ -454,11 +484,10 @@ static void CheckPauseOnJoin() * be set to the company and port strings given by the user, inside the memory area originally * occupied by connection_string. */ -void ParseConnectionString(const char **company, const char **port, char *connection_string) +void ParseGameConnectionString(const char **company, const char **port, char *connection_string) { bool ipv6 = (strchr(connection_string, ':') != strrchr(connection_string, ':')); - char *p; - for (p = connection_string; *p != '\0'; p++) { + for (char *p = connection_string; *p != '\0'; p++) { switch (*p) { case '[': ipv6 = true; @@ -592,7 +621,6 @@ void NetworkAddServer(const char *b) { if (*b != '\0') { const char *port = nullptr; - const char *company = nullptr; char host[NETWORK_HOSTNAME_LENGTH]; uint16 rport; @@ -601,7 +629,7 @@ void NetworkAddServer(const char *b) strecpy(_settings_client.network.connect_to_ip, b, lastof(_settings_client.network.connect_to_ip)); rport = NETWORK_DEFAULT_PORT; - ParseConnectionString(&company, &port, host); + ParseConnectionString(&port, host); if (port != nullptr) rport = atoi(port); NetworkUDPQueryServer(NetworkAddress(host, rport), true); diff --git a/src/network/network_func.h b/src/network/network_func.h index cbb89820c..d14dd290f 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -43,7 +43,8 @@ void NetworkReboot(); void NetworkDisconnect(bool blocking = false, bool close_admins = true); void NetworkGameLoop(); void NetworkBackgroundLoop(); -void ParseConnectionString(const char **company, const char **port, char *connection_string); +void ParseConnectionString(const char **port, char *connection_string); +void ParseGameConnectionString(const char **company, const char **port, char *connection_string); void NetworkStartDebugLog(NetworkAddress address); void NetworkPopulateCompanyStats(NetworkCompanyStats *stats); |