From cca2ba9aabfc0e9ae18b980e7ecc08abb3888ec1 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 23 Dec 2020 13:42:28 -0600 Subject: [PATCH] Add LiStringifyPortFlags() helper function --- src/ConnectionTester.c | 27 +++++++++++++++++++++++++++ src/Limelight.h | 5 +++++ 2 files changed, 32 insertions(+) diff --git a/src/ConnectionTester.c b/src/ConnectionTester.c index 9aba560..c5fd58e 100644 --- a/src/ConnectionTester.c +++ b/src/ConnectionTester.c @@ -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; diff --git a/src/Limelight.h b/src/Limelight.h index cc089ad..86803ed 100644 --- a/src/Limelight.h +++ b/src/Limelight.h @@ -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