Skip to content

Commit

Permalink
Change: Make a shortened network revision string for use in server qu…
Browse files Browse the repository at this point in the history
…eries
  • Loading branch information
nielsmh committed Jan 28, 2019
1 parent 932870f commit 87a6962
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/network/core/game.h
Expand Up @@ -56,6 +56,8 @@ struct NetworkGameInfo : NetworkServerGameInfo {
byte map_set; ///< Graphical set
};

const char * GetNetworkRevisionString();

#endif /* ENABLE_NETWORK */

#endif /* NETWORK_CORE_GAME_H */
1 change: 1 addition & 0 deletions src/network/core/tcp_http.cpp
Expand Up @@ -45,6 +45,7 @@ NetworkHTTPSocketHandler::NetworkHTTPSocketHandler(SOCKET s,
redirect_depth(depth),
sock(s)
{
/* Not using GetNetworRevisionString() since there are no field length limitations here */
size_t bufferSize = strlen(url) + strlen(host) + strlen(_openttd_revision) + (data == NULL ? 0 : strlen(data)) + 128;
char *buffer = AllocaM(char, bufferSize);

Expand Down
42 changes: 41 additions & 1 deletion src/network/network.cpp
Expand Up @@ -1100,13 +1100,53 @@ void NetworkShutDown()
NetworkCoreShutdown();
}

/**
* Get the network version string used by this build.
* The returned string is guaranteed to be at most NETWORK_REVISON_LENGTH bytes.
*/
const char * GetNetworkRevisionString()
{
/* This will be allocated on heap and never free'd, but only once so not a "real" leak. */
static char *network_revision = nullptr;
/* We want at least this many characters of the git hash, if the base string becomes truncatad. */
const uint MIN_GITHASH_LENGTH = 8;

if (!network_revision) {
/* Start by taking a chance on the full revision string. */
network_revision = stredup(_openttd_revision);
/* If it's too long, truncate it. */
if (strlen(network_revision) >= NETWORK_REVISION_LENGTH - 1) {
network_revision[NETWORK_REVISION_LENGTH - 1] = '\0';

/* Prepare a prefix of the git hash.
* Size is length + 1 for terminator, +2 for -g prefix, +1 for optional M suffix. */
char githash_prefix[MIN_GITHASH_LENGTH + 4] = "-g";
for (uint i = 0; i < MIN_GITHASH_LENGTH; i++) {
githash_prefix[i + 2] = _openttd_revision_hash[i];
}
if (_openttd_revision_modified == 2) {
githash_prefix[MIN_GITHASH_LENGTH + 2] = 'M';
githash_prefix[MIN_GITHASH_LENGTH + 3] = '\0';
}

if (strstr(network_revision, githash_prefix) == nullptr) {
/* The prefix was not found, forcibly replace the last part of the version string with it. */
strecpy(network_revision + NETWORK_REVISION_LENGTH - strlen(githash_prefix) - 1, githash_prefix, network_revision + NETWORK_REVISION_LENGTH);
}
}
}

assert(strlen(network_revision) < NETWORK_REVISION_LENGTH); // strlen does not include terminator, constant does, hence strictly less than
return network_revision;
}

/**
* Checks whether the given version string is compatible with our version.
* @param other the version string to compare to
*/
bool IsNetworkCompatibleVersion(const char *other)
{
return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
return strncmp(GetNetworkRevisionString(), other, NETWORK_REVISION_LENGTH - 1) == 0;
}

#endif /* ENABLE_NETWORK */
2 changes: 1 addition & 1 deletion src/network/network_admin.cpp
Expand Up @@ -172,7 +172,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendWelcome()
Packet *p = new Packet(ADMIN_PACKET_SERVER_WELCOME);

p->Send_string(_settings_client.network.server_name);
p->Send_string(_openttd_revision);
p->Send_string(_openttd_revision); // Not GetNetworRevisionString() since there is no field length limitation here
p->Send_bool (_network_dedicated);

p->Send_string(_network_game_info.map_name);
Expand Down
2 changes: 1 addition & 1 deletion src/network/network_client.cpp
Expand Up @@ -343,7 +343,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendJoin()
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);

Packet *p = new Packet(PACKET_CLIENT_JOIN);
p->Send_string(_openttd_revision);
p->Send_string(GetNetworkRevisionString());
p->Send_uint32(_openttd_newgrf_version);
p->Send_string(_settings_client.network.client_name); // Client name
p->Send_uint8 (_network_join_as); // PlayAs
Expand Down
2 changes: 1 addition & 1 deletion src/network/network_udp.cpp
Expand Up @@ -191,7 +191,7 @@ void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *p, Networ

strecpy(ngi.map_name, _network_game_info.map_name, lastof(ngi.map_name));
strecpy(ngi.server_name, _settings_client.network.server_name, lastof(ngi.server_name));
strecpy(ngi.server_revision, _openttd_revision, lastof(ngi.server_revision));
strecpy(ngi.server_revision, GetNetworkRevisionString(), lastof(ngi.server_revision));

Packet packet(PACKET_UDP_SERVER_RESPONSE);
this->SendNetworkGameInfo(&packet, &ngi);
Expand Down

0 comments on commit 87a6962

Please sign in to comment.