23 #include "common_priv.h"
40 int family,
unsigned portnum) {
43 hints.ai_flags = AI_ADDRCONFIG;
44 hints.ai_family = family;
45 hints.ai_socktype = SOCK_STREAM;
46 if (0 == hostname || *hostname ==
'\0') {
47 hints.ai_flags |= AI_PASSIVE;
52 lookupstatus = getaddrinfo(hostname, port, &hints, &res);
53 switch (lookupstatus) {
73 }
while (lookupstatus != 0);
76 for (addrinfo *rp = res; rp != 0; rp = rp->ai_next) {
88 return Lookup(0, servname, AF_UNSPEC, 0);
92 return Lookup(0, servname.c_str(), AF_UNSPEC, 0);
96 return Lookup(hostname, 0, AF_UNSPEC, 0);
100 return Lookup(hostname.c_str(), 0, AF_UNSPEC, 0);
107 addr.
address.
in.sin_addr.s_addr = INADDR_ANY;
108 addr.
length =
sizeof(sockaddr_in);
110 list.push_back(addr);
115 return Lookup(hostname, servname, AF_UNSPEC, 0);
119 return Lookup(hostname.c_str(), servname.c_str(), AF_UNSPEC, 0);
123 return Lookup(hostname, 0, AF_UNSPEC, serv);
127 return Lookup(hostname.c_str(), 0, AF_UNSPEC, serv);
133 std::string::const_iterator it, e;
135 it = hostname.begin();
136 bool in_address =
false;
137 bool at_port =
false;
171 return Lookup(h, p, AF_UNSPEC, 0);
175 length = info->ai_addrlen;
180 length = info->ai_addrlen;
182 uint16_t port = (uint16_t)portnum;
202 std::vector<char> hostname(NI_MAXHOST,
'\0');
204 std::ostringstream oss;
206 int flags = NI_NUMERICSERV;
207 if (numerichost) { flags |= NI_NUMERICHOST; }
209 &hostname[0], hostname.size(),
212 switch (lookupstatus) {
219 #if !defined(__APPLE__)
221 hostname.resize(hostname.size()*2,
'\0');
238 std::vector<char> servname(NI_MAXSERV,
'\0');
240 std::ostringstream oss;
244 &servname[0], servname.size(),
246 switch (lookupstatus) {
253 #if !defined(__APPLE__)
255 servname.resize(servname.size()*2,
'\0');
283 return (
unsigned)ntohs(retval);
299 ASSERT(
false,
"Unknown address type.");
305 GetLen() =
sizeof(sockaddr_storage);
312 GetLen() =
sizeof(sockaddr_storage);
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)
An abstraction of a socket address with convenience methods.
std::string GetHostName(bool numerichost=true) const
void SetFromPeerName(int fd)
Fill this SocketAddress with data from the other side of the connection represented by fd...
static SockAddrList CreateIPFromServ(const char *servname)
Return a list of valid socket addresses for the given service name All CreateIP functions create IP a...
void SetFromSockName(int fd)
Fill this SocketAddress with data from this side of the connection represented by fd...
std::string GetServName() const
static SockAddrList CreateIPFromHost(const char *hostname)
static SockAddrList CreateIP(unsigned serv)
Return a list of valid socket address for the given service number or port number.
std::vector< SocketAddress > SockAddrList
union SocketAddress::address_ address