This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathhost.cc
88 lines (77 loc) · 2.71 KB
/
host.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2012 M-Lab. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mlab/host.h"
#if defined(OS_FREEBSD)
#include <netinet/in.h>
#endif
#if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_ANDROID) || defined(OS_FREEBSD)
#include <arpa/inet.h>
#include <memory.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#elif defined(OS_WINDOWS)
#include <WinSock2.h>
#include <ws2ipdef.h>
#include <WS2tcpip.h>
#else
#error Undefined platform
#endif
#include "log.h"
namespace mlab {
Host::Host(const std::string& hostname) : original_hostname(hostname) {
addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = 0;
hints.ai_protocol = 0;
addrinfo* servinfo;
if (int rv = getaddrinfo(hostname.c_str(), NULL, &hints, &servinfo) != 0)
LOG(FATAL, "Failed to resolve %s: %s", hostname.c_str(), gai_strerror(rv));
for (addrinfo* p = servinfo; p != NULL; p = p->ai_next) {
sockaddr_storage saddr;
std::string address;
switch (p->ai_family) {
case AF_INET: {
sockaddr_in* h = reinterpret_cast<sockaddr_in*>(p->ai_addr);
char buffer[INET_ADDRSTRLEN];
inet_ntop(p->ai_family, &(h->sin_addr), buffer, INET_ADDRSTRLEN);
address.assign(buffer);
sockaddr_in* saddr_in = reinterpret_cast<sockaddr_in*>(&saddr);
memcpy(saddr_in, h, p->ai_addrlen);
ASSERT(saddr_in->sin_family == p->ai_family);
break;
}
case AF_INET6: {
sockaddr_in6* h = reinterpret_cast<sockaddr_in6*>(p->ai_addr);
char buffer[INET6_ADDRSTRLEN];
inet_ntop(p->ai_family, &(h->sin6_addr), buffer, INET6_ADDRSTRLEN);
address.assign(buffer);
sockaddr_in6* saddr_in6 = reinterpret_cast<sockaddr_in6*>(&saddr);
memcpy(saddr_in6, h, p->ai_addrlen);
ASSERT(saddr_in6->sin6_family == p->ai_family);
ASSERT(saddr_in6->sin6_scope_id == 0);
break;
}
default:
LOG(FATAL, "Unexpected family %d", p->ai_family);
break;
}
sockaddr_.push_back(saddr);
if (resolved_ips.insert(address).second)
LOG(VERBOSE, "Resolved: %s", address.c_str());
}
freeaddrinfo(servinfo);
}
} // namespace mlab