Skip to content

Commit

Permalink
stats: use Socks wrapper, use CService to generate our sockaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Sep 5, 2024
1 parent 328c1de commit f3b12a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
35 changes: 17 additions & 18 deletions src/statsd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,25 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename,
return;
}

m_sock = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_sock == INVALID_SOCKET) {
SOCKET hSocket = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (hSocket == INVALID_SOCKET) {
LogPrintf("ERROR: Cannot create socket (socket() returned error %s), cannot init StatsdClient\n",
NetworkErrorString(WSAGetLastError()));
return;
}
m_sock = std::make_unique<Sock>(hSocket);

memset(&m_server, 0, sizeof(m_server));
m_server.sin_family = AF_INET;
m_server.sin_port = htons(m_port);

CNetAddr netaddr(m_server.sin_addr);
if (!LookupHost(m_host, netaddr, true) || !netaddr.GetInAddr(&m_server.sin_addr)) {
LogPrintf("ERROR: LookupHost or GetInAddr failed, cannot init StatsdClient\n");
CNetAddr netaddr;
if (!LookupHost(m_host, netaddr, /*fAllowLookup=*/true)) {
LogPrintf("ERROR: Unable to lookup host %s, cannot init StatsdClient\n", m_host);
return;
}
if (!netaddr.IsIPv4()) {
LogPrintf("ERROR: Host %s on unsupported network, cannot init StatsdClient\n", m_host);
return;
}
if (!CService(netaddr, port).GetSockAddr(reinterpret_cast<struct sockaddr*>(&m_server.first), &m_server.second)) {
LogPrintf("ERROR: Cannot get socket address for %s, cannot init StatsdClient\n", m_host);
return;
}

Expand All @@ -92,11 +97,6 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename,
LogPrintf("StatsdClient initialized to transmit stats to %s:%d\n", m_host, m_port);
}

StatsdClient::~StatsdClient()
{
CloseSocket(m_sock);
}

/* will change the original string */
void StatsdClient::cleanup(std::string& key)
{
Expand Down Expand Up @@ -183,12 +183,11 @@ int StatsdClient::send(const std::string& message)
if (!m_init)
return -3;

int ret = ::sendto(m_sock, message.data(), message.size(), 0, reinterpret_cast<const sockaddr*>(&m_server),
sizeof(m_server));
if (ret == -1) {
if (::sendto(m_sock->Get(), message.data(), message.size(), /*flags=*/0,
reinterpret_cast<struct sockaddr*>(&m_server.first), m_server.second) == SOCKET_ERROR) {
LogPrintf("ERROR: Unable to send message (sendto() returned error %s), host=%s:%d\n",
NetworkErrorString(WSAGetLastError()), m_host, m_port);
return ret;
return -1;
}

return 0;
Expand Down
5 changes: 2 additions & 3 deletions src/statsd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class StatsdClient {
public:
explicit StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
bool enabled);
~StatsdClient();

public:
int inc(const std::string& key, float sample_rate = 1.f);
Expand Down Expand Up @@ -65,8 +64,8 @@ class StatsdClient {
mutable FastRandomContext insecure_rand GUARDED_BY(cs);

bool m_init{false};
SOCKET m_sock{INVALID_SOCKET};
struct sockaddr_in m_server;
std::unique_ptr<Sock> m_sock{nullptr};
std::pair<struct sockaddr_storage, socklen_t> m_server{{}, sizeof(struct sockaddr_storage)};

const uint16_t m_port;
const std::string m_host;
Expand Down

0 comments on commit f3b12a6

Please sign in to comment.