2 * @brief Resolve hostnames and ip addresses
4 /* Copyright (C) 2017 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef XAPIAN_INCLUDED_RESOLVER_H
22 #define XAPIAN_INCLUDED_RESOLVER_H
25 #include "safenetdb.h"
26 #include "safesyssocket.h"
28 #include "xapian/error.h"
33 struct addrinfo
* result
= NULL
;
36 class const_iterator
{
39 explicit const_iterator(struct addrinfo
* p_
) : p(p_
) { }
41 struct addrinfo
& operator*() const {
49 const_iterator
operator++(int) {
50 struct addrinfo
* old_p
= p
;
52 return const_iterator(old_p
);
55 bool operator==(const const_iterator
& o
) {
59 bool operator!=(const const_iterator
& o
) {
64 Resolver(const std::string
& host
, int port
, int flags
= 0) {
65 // RFC 3493 has an extra sentence in its definition of
66 // AI_ADDRCONFIG which POSIX doesn't:
68 // "The loopback address is not considered for this case as valid
69 // as a configured address."
71 // Some platforms implement this version rather than POSIX (notably
72 // glibc on Linux). Others implement POSIX (from looking at the
73 // man pages, these include FreeBSD 11.0).
75 // In most cases, this extra sentence is arguably helpful - e.g. it
76 // means that you won't get IPv6 addresses just because the system
77 // has IPv6 loopback (and similarly for IPv4).
79 // However, it behaves unhelpfully if the *only* interface
80 // configured is loopback - in this situation, AI_ADDRCONFIG means
81 // that you won't get an IPv4 address (as there's no IPv4 address
82 // configured ignoring loopback) and you won't get an IPv6 address
83 // (as there's no IPv6 address configured ignoring loopback).
85 // It's generally rare that systems with only loopback would want
86 // to use the remote backend, but a real example is testsuites
87 // (including our own) running on autobuilders which deliberately
88 // close off network access.
90 // To allow such cases to work on Linux (and other platforms which
91 // follow the RFC rather than POSIX in this detail) we avoid using
92 // AI_ADDRCONFIG for 127.0.0.1, ::1 and localhost. There are
93 // other ways to write these IP addresses and other hostnames may
94 // map onto them, but this just needs to work for the standard
95 // cases which a testsuite might use.
96 if (host
!= "::1" && host
!= "127.0.0.1" && host
!= "localhost") {
97 flags
|= AI_ADDRCONFIG
;
99 flags
|= AI_NUMERICSERV
;
101 struct addrinfo hints
;
102 std::memset(&hints
, 0, sizeof(struct addrinfo
));
103 hints
.ai_family
= AF_INET
;
104 hints
.ai_socktype
= SOCK_STREAM
;
105 hints
.ai_flags
= flags
;
106 hints
.ai_protocol
= 0;
108 const char * node
= host
.empty() ? NULL
: host
.c_str();
109 int r
= getaddrinfo(node
, str(port
).c_str(), &hints
, &result
);
111 throw Xapian::NetworkError("Couldn't resolve host " + host
,
117 if (result
) freeaddrinfo(result
);
120 const_iterator
begin() const {
121 return const_iterator(result
);
124 const_iterator
end() const {
125 return const_iterator(NULL
);
129 #endif // XAPIAN_INCLUDED_RESOLVER_H