1 #include "api/internal/io/HostInfo_p.h"
2 using namespace BamTools;
3 using namespace BamTools::Internal;
7 # include "api/internal/io/NetWin_p.h"
9 # include "api/internal/io/NetUnix_p.h"
12 #include <iostream> // debug
14 // standard C++ includes
20 // -------------------------
22 // -------------------------
24 HostInfo::HostInfo(void)
25 : m_error(HostInfo::NoError)
28 HostInfo::HostInfo(const HostInfo& other)
29 : m_hostName(other.m_hostName)
30 , m_addresses(other.m_addresses)
31 , m_error(other.m_error)
32 , m_errorString(other.m_errorString)
35 HostInfo::~HostInfo(void) { }
37 vector<HostAddress> HostInfo::Addresses(void) const {
41 HostInfo::ErrorType HostInfo::GetError(void) const {
45 string HostInfo::GetErrorString(void) const {
49 string HostInfo::HostName(void) const {
53 void HostInfo::SetAddresses(const std::vector<HostAddress>& addresses) {
54 m_addresses = addresses;
57 void HostInfo::SetError(const HostInfo::ErrorType error) {
61 void HostInfo::SetErrorString(const std::string& errorString) {
62 m_errorString = errorString;
65 void HostInfo::SetHostName(const string& name) {
69 // ------------------------------
70 // HostInfo::Lookup(host, port)
71 // ------------------------------
73 HostInfo HostInfo::Lookup(const string& hostname, const string& port) {
76 set<HostAddress> uniqueAddresses;
83 address.SetAddress(hostname);
85 // if hostname is an IP string ('0.0.0.0' or IPv6 format)
86 // do reverse lookup for host domain name
88 // TODO: might just remove this... not sure if proper 'hostname' from IP string is needed
90 // so far, haven't been able to successfully fetch a domain name with reverse DNS
91 // getnameinfo() on test sites just returns original IP string. BUT this is likely a rare
92 // case that client code tries to use an IP string and the connection should work fine
93 // anyway. GetHostName() just won't quite show what I was hoping for. :(
94 if ( address.HasIPAddress() ) {
96 const uint16_t portNum = static_cast<uint16_t>( atoi(port.c_str()) );
101 BT_SOCKLEN_T saSize = 0;
104 if ( address.GetProtocol() == HostAddress::IPv4Protocol ) {
105 sa = (sockaddr*)&sa4;
106 saSize = sizeof(sa4);
107 memset(&sa4, 0, sizeof(sa4));
108 sa4.sin_family = AF_INET;
109 sa4.sin_addr.s_addr = htonl(address.GetIPv4Address());
110 sa4.sin_port = htons(portNum);
114 else if ( address.GetProtocol() == HostAddress::IPv4Protocol ){
115 sa = (sockaddr*)&sa6;
116 saSize = sizeof(sa6);
117 memset(&sa6, 0, sizeof(sa6));
118 sa6.sin6_family = AF_INET6;
119 memcpy(sa6.sin6_addr.s6_addr, address.GetIPv6Address().data, sizeof(sa6.sin6_addr.s6_addr));
120 sa6.sin6_port = htons(portNum);
123 // unknown (should be unreachable)
124 else BT_ASSERT_X(false, "HostInfo::Lookup: unknown network protocol");
126 // lookup name for IP
127 char hbuf[NI_MAXHOST];
128 char serv[NI_MAXSERV];
129 if ( sa && (getnameinfo(sa, saSize, hbuf, sizeof(hbuf), serv, sizeof(serv), 0) == 0) )
130 result.SetHostName(string(hbuf));
132 // if no domain name found, just use the original address's IP string
133 if ( result.HostName().empty() )
134 result.SetHostName(address.GetIPString());
136 // store address in HostInfo
137 uniqueAddresses.insert(address);
140 // otherwise, hostname is a domain name ('www.foo.bar')
141 // do 'normal' lookup
144 cout << "HostInfo::Lookup() - looking up addresses for domain name: " << hostname << endl;
146 // setup address lookup 'hints'
148 memset(&hints, 0, sizeof(hints));
149 hints.ai_family = AF_UNSPEC; // allow either IPv4 or IPv6
150 hints.ai_socktype = SOCK_STREAM; // for TCP
151 hints.ai_protocol = IPPROTO_TCP;
153 // fetch addresses for requested hostname/port
155 int status = getaddrinfo(hostname.c_str(), port.c_str(), &hints, &res );
160 cout << "HostInfo::Lookup() - found addresses" << endl;
162 // iterate over all IP addresses found
164 for ( ; p != NULL; p = p->ai_next ) {
167 if ( p->ai_family == AF_INET ) {
168 sockaddr_in* ipv4 = (sockaddr_in*)p->ai_addr;
169 HostAddress a( ntohl(ipv4->sin_addr.s_addr) );
170 cout << "\t" << a.GetIPString() << endl;
171 uniqueAddresses.insert(a);
175 else if ( p->ai_family == AF_INET6 ) {
176 sockaddr_in6* ipv6 = (sockaddr_in6*)p->ai_addr;
177 HostAddress a(ipv6->sin6_addr.s6_addr);
178 cout << "\t" << a.GetIPString() << endl;
179 uniqueAddresses.insert(a);
183 // if we iterated, but no addresses were stored
184 if ( uniqueAddresses.empty() && (p == NULL) ) {
185 result.SetError(HostInfo::UnknownError);
186 result.SetErrorString("HostInfo: unknown address types found");
190 // handle error cases
194 || status == EAI_FAIL
196 || status == EAI_NODATA // officially deprecated, but just in case we happen to hit it
197 # endif // EAI_NODATA
200 WSAGetLastError() == WSAHOST_NOT_FOUND
201 || WSAGetLastError() == WSANO_DATA
202 || WSAGetLastError() == WSANO_RECOVERY
206 result.SetError(HostInfo::HostNotFound);
207 result.SetErrorString("HostInfo: host not found");
210 result.SetError(HostInfo::UnknownError);
211 result.SetErrorString("HostInfo: unknown error encountered");
218 // store fetched addresses (converting set -> vector) in result & return
219 result.SetAddresses( vector<HostAddress>(uniqueAddresses.begin(), uniqueAddresses.end()) );