Add LiStringifyPortFlags() helper function

This commit is contained in:
Cameron Gutman 2020-12-23 13:42:28 -06:00
parent ce546b12b0
commit cca2ba9aab
2 changed files with 32 additions and 0 deletions

View File

@ -77,6 +77,33 @@ unsigned short LiGetPortFromPortFlagIndex(int portFlagIndex)
}
}
void LiStringifyPortFlags(unsigned int portFlags, const char* separator, char* outputBuffer, int outputBufferLength)
{
// Initialize the output buffer to an empty string
outputBuffer[0] = 0;
// If there is no separator specified, use an empty string
if (separator == NULL) {
separator = "";
}
int offset = 0;
for (int i = 0; i < PORT_FLAGS_MAX_COUNT; i++) {
if (portFlags & (1U << i)) {
const char* protoStr = LiGetProtocolFromPortFlagIndex(i) == IPPROTO_UDP ? "UDP" : "TCP";
offset += snprintf(&outputBuffer[offset], outputBufferLength - offset, "%s%s %u",
offset != 0 ? separator : "",
protoStr,
LiGetPortFromPortFlagIndex(i));
if (outputBufferLength - offset <= 0) {
// snprintf() will return the desired length if the buffer is too small,
// so it is possible for this calculation to be negative.
break;
}
}
}
}
unsigned int LiTestClientConnectivity(const char* testServer, unsigned short referencePort, unsigned int testPortFlags)
{
unsigned int failingPortFlags;

View File

@ -575,6 +575,11 @@ int LiGetProtocolFromPortFlagIndex(int portFlagIndex);
// Returns the port number for the specified port index
unsigned short LiGetPortFromPortFlagIndex(int portFlagIndex);
// Populates the output buffer with a stringified list of the port flags set in the input argument.
// The second and subsequent entries will be prepended by 'separator' (if provided).
// If the output buffer is too small, the output will be truncated to fit the provided buffer.
void LiStringifyPortFlags(unsigned int portFlags, const char* separator, char* outputBuffer, int outputBufferLength);
// This function may be used to test if the local network is blocking Moonlight's ports. It requires
// a test server running on an Internet-reachable host. To perform a test, pass in the DNS hostname
// of the test server, a reference TCP port to ensure the test host is reachable at all (something