16 #include <arpa/inet.h> 37 size_t colonpos = host.rfind(
':');
38 if (colonpos == std::string::npos)
46 Resolve(host.c_str(), port.c_str());
56 if (
sockaddr()->sa_family == AF_INET)
58 if (inet_ntop(AF_INET,
59 &
sockaddr_in()->sin_addr, str,
sizeof(str)) ==
nullptr)
61 sLOG <<
"Error in inet_ntop: " << strerror(errno);
66 else if (
sockaddr()->sa_family == AF_INET6)
68 if (inet_ntop(AF_INET6,
69 &
sockaddr_in6()->sin6_addr, str,
sizeof(str)) ==
nullptr)
71 sLOG <<
"Error in inet_ntop: " << strerror(errno);
89 struct addrinfo* result;
90 struct addrinfo hints;
92 memset(&hints, 0,
sizeof(
struct addrinfo));
93 hints.ai_family = AF_INET;
94 hints.ai_socktype = SOCK_STREAM;
96 hints.ai_protocol = 0;
98 int gai = getaddrinfo(hostname, servicename, &hints, &result);
108 freeaddrinfo(result);
119 struct addrinfo* result;
120 struct addrinfo hints;
122 memset(&hints, 0,
sizeof(
struct addrinfo));
123 hints.ai_family = AF_INET;
124 hints.ai_socktype = SOCK_STREAM;
126 hints.ai_protocol = 0;
128 int s = getaddrinfo(hostname, servicename, &hints, &result);
135 freeaddrinfo(result);
142 const char* defaultservice) {
145 std::string::size_type colonpos = host.rfind(
':');
146 if (colonpos == std::string::npos)
150 host.erase(colonpos);
152 return ResolveOne(host.c_str(), servicename.c_str());
155 std::vector<SocketAddress>
157 std::vector<SocketAddress> salist;
159 struct addrinfo* result;
160 struct addrinfo hints;
162 memset(&hints, 0,
sizeof(
struct addrinfo));
163 hints.ai_family = AF_UNSPEC;
164 hints.ai_socktype = SOCK_STREAM;
166 hints.ai_protocol = 0;
168 int s = getaddrinfo(hostname, servicename, &hints, &result);
173 for (
struct addrinfo* ap = result; ap !=
nullptr; ap = ap->ai_next)
175 salist.push_back(
SocketAddress(ap->ai_addr, ap->ai_addrlen));
178 freeaddrinfo(result);
188 sin->sin_family = AF_INET;
189 if (inet_pton(AF_INET, ipstring, &sin->sin_addr) <= 0) {
193 sin->sin_port = htons(port);
201 sin6->sin6_family = AF_INET6;
202 if (inet_pton(AF_INET6, ipstring, &sin6->sin6_addr) <= 0) {
203 sin6->sin6_family = 0;
206 sin6->sin6_port = htons(port);
static SocketAddress ResolveOne(const char *hostname, const char *servicename=nullptr)
static SocketAddress ResolveWithPort(const char *hostname, const char *defaultservice)
#define sLOG
Default logging method: output if the local debug variable is true.
bool Resolve(const char *hostname, const char *servicename=nullptr)
SocketAddress is a super class used to unify the two different IPv4 and IPv6 socket address represent...
SocketAddress()
Create empty invalid address object by clearing all bytes.
IPv6Address()
Create uninitialized IPv6 address.
union thrill::net::tcp::SocketAddress::SockAddrUnion sockaddr_
static std::vector< SocketAddress > ResolveAll(const char *hostname, const char *servicename=nullptr)
friend std::ostream & operator<<(std::ostream &os, const SocketAddress &sa)
Make the socket address ostream-able: outputs address:port.
uint16_t GetPort() const
Return the currently set port address in host byte-order.
static by_string to_string(int val)
convert to string
const char * GetResolveError() const
struct sockaddr_in * sockaddr_in()
Cast the enclosed sockaddr into the sockaddr_in IPv4 structure.
std::string ToStringHost() const
Return the enclosed socket address as a string without the port number.
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
IPv4Address()
Create uninitialized IPv4 address.
struct sockaddr * sockaddr()
Return pointer to enclosed address as a generic sockattr struct.
std::string ToStringHostPort() const
Return the enclosed socket address as a string with the port number.
struct sockaddr_in6 * sockaddr_in6()
Cast the enclosed sockaddr into the sockaddr_in6 IPv6 structure.