11 addrinfo_to_string(
const addrinfo *ai)
13 std::ostringstream os;
22 if (h.find(
':') == string::npos)
25 os <<
'[' << h <<
"]:" << p;
33 struct gai_category_impl :
public std::error_category {
34 const char *name() const noexcept
override {
return "DNS"; }
35 string message(
int err)
const override {
return gai_strerror(err); }
39 const std::error_category &
42 static gai_category_impl cat;
48 cat_host_service(
const char *host,
const char *service)
52 if (std::strchr(host,
':')) {
71 const char *service,
int family)
74 std::memset(&hints, 0,
sizeof(hints));
75 hints.ai_socktype = socktype;
76 hints.ai_family = family;
77 hints.ai_flags = AI_ADDRCONFIG;
78 int err = getaddrinfo(host, service, &hints, &res);
81 cat_host_service(host, service));
86 string *host,
string *serv)
88 char hostbuf[NI_MAXHOST];
89 char servbuf[NI_MAXSERV];
90 int err = getnameinfo(sa, salen, hostbuf,
sizeof(hostbuf),
91 servbuf,
sizeof(servbuf),
92 NI_NUMERICHOST|NI_NUMERICSERV);
94 throw std::system_error(err,
gai_category(),
"getnameinfo");
104 unique_sock s(socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol));
109 if (connect(s.
get().fd_, ai->ai_addr, ai->ai_addrlen) == -1
110 && errno != EINPROGRESS)
119 errno = EADDRNOTAVAIL;
120 for (; ai && !s; ai = ai->ai_next)
127 tcp_connect(
const char *host,
const char *service,
int family)
135 addrinfo hints, *res;
136 std::memset(&hints, 0,
sizeof(hints));
137 hints.ai_socktype = SOCK_STREAM;
138 hints.ai_family = family;
139 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
140 int err = getaddrinfo(
nullptr, service ? service :
"0", &hints, &res);
142 throw std::system_error(err,
gai_category(),
"AI_PASSIVE");
152 if (bind(s.
get().fd_, ai->ai_addr, ai->ai_addrlen) == -1)
154 if (listen (s.
get().fd_, backlog) == -1)
unique_sock tcp_connect(const addrinfo *ai)
Try connecting to every addrinfo in a list until one succeeds.
void throw_sockerr(const char *)
Throw a system_error exception for the last socket error.
unique_sock tcp_connect1(const addrinfo *ai, bool ndelay)
Try connecting to the first addrinfo in a linked list.
Most of the xdrpp library is encapsulated in the xdr namespace.
std::unique_ptr< addrinfo, delete_addrinfo > unique_addrinfo
Automatically garbage-collected addrinfo pointer.
void set_nonblock(sock_t s)
Set the O_NONBLOCK flag on a socket.
unique_addrinfo get_addrinfo(const char *host, int socktype, const char *service, int family)
Wrapper around getaddrinfo that returns a garbage-collected xdr::unique_addrinfo. ...
const std::error_category & gai_category()
Category for system errors dealing with DNS (getaddrinfo, etc.).
unique_sock tcp_listen(const char *service, int family, int backlog)
Create bind a listening TCP socket.
sock_t get() const
Return the file descriptor number, but maintain ownership.
Abstract away the type of a socket (for windows).
Simplified support for creating sockets.
void get_numinfo(const sockaddr *sa, socklen_t salen, string *host, string *serv)
Return printable versions of numeric host and port number.