Common/TraversalServer: Make use of fmt over sprintf where applicable

Resolves some deprecation warnings on macOS. This is better anyway, given fmt
has generic type formatting.
This commit is contained in:
Lioncache 2023-12-18 10:48:42 -05:00
parent 1764b13423
commit dc85194fac
2 changed files with 7 additions and 5 deletions

View File

@ -329,7 +329,7 @@ endif()
if(UNIX)
# Posix networking code needs to be fixed for Windows
add_executable(traversal_server TraversalServer.cpp)
target_link_libraries(traversal_server PRIVATE common)
target_link_libraries(traversal_server PRIVATE common fmt::fmt)
if(SYSTEMD_FOUND)
target_link_libraries(traversal_server PRIVATE ${SYSTEMD_LIBRARIES})
endif()

View File

@ -18,6 +18,8 @@
#include <utility>
#include <vector>
#include <fmt/format.h>
#ifdef HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
@ -177,17 +179,17 @@ static sockaddr_in6 MakeSinAddr(const Common::TraversalInetAddress& addr)
static void GetRandomHostId(Common::TraversalHostId* hostId)
{
char buf[9];
char buf[9]{};
const u32 num = Common::Random::GenerateValue<u32>();
sprintf(buf, "%08x", num);
fmt::format_to_n(buf, sizeof(buf) - 1, "{:08x}", num);
memcpy(hostId->data(), buf, 8);
}
static const char* SenderName(sockaddr_in6* addr)
{
static char buf[INET6_ADDRSTRLEN + 10];
static char buf[INET6_ADDRSTRLEN + 10]{};
inet_ntop(PF_INET6, &addr->sin6_addr, buf, sizeof(buf));
sprintf(buf + strlen(buf), ":%d", ntohs(addr->sin6_port));
fmt::format_to(buf + strlen(buf), ":{}", ntohs(addr->sin6_port));
return buf;
}