IRC_SERVER
By @hyunjunk (hyunjun2372@gmail.com)
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <cstdio>
5#include <sstream>
7
8std::string InetAddrToString(const struct sockaddr_in& addr)
9{
10 char buf[INET_ADDRSTRLEN + 6 /* :port */ + 1 /* null-terminator */];
11 const uint32_t ip = ntohl(addr.sin_addr.s_addr);
12 const uint16_t port = ntohs(addr.sin_port);
13
14 STATIC_ASSERT(sizeof(addr.sin_addr.s_addr) == sizeof(uint32_t));
15 STATIC_ASSERT(sizeof(addr.sin_port) == sizeof(uint16_t));
16
17 std::snprintf(buf, sizeof(buf), "%d.%d.%d.%d:%d", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF, port);
18 return buf;
19}
#define STATIC_ASSERT(exp)
Definition MacroDefines.hpp:42
std::string InetAddrToString(const struct sockaddr_in &addr)
Definition Utils.hpp:8