Cogs.Core
Address.cpp
1#include "Address.h"
2
3#if !defined( EMSCRIPTEN )
4
5#ifndef _WIN32
6#include <arpa/inet.h>
7#endif
8
9Cogs::Network::AddrIn::AddrIn(const char* str):
10 addr{}
11{
12 inet_pton(family(), str, &addr);
13}
14
15Cogs::Network::AddrIn::AddrIn(const std::string& str):
16 addr{}
17{
18 inet_pton(family(), str.c_str(), &addr);
19}
20
21Cogs::Network::AddrIn6::AddrIn6(const char* str):
22 addr{}
23{
24 inet_pton(family(), str, &addr);
25}
26
27std::string Cogs::Network::AddrIn::string() const
28{
29 std::string str(INET_ADDRSTRLEN, '\0');
30 inet_ntop(family(), &addr, str.data(), static_cast<socklen_t>(str.size()));
31
32 // Use .c_str() so that the returned string has a correct size.
33 return str.c_str();
34}
35
36std::string Cogs::Network::AddrIn6::string() const
37{
38 std::string str(INET6_ADDRSTRLEN, '\0');
39 inet_ntop(family(), &addr, str.data(), static_cast<socklen_t>(str.size()));
40
41 // Use .c_str() so that the returned string has a correct size.
42 return str.c_str();
43}
44
45#endif