13 #ifndef __STOUT_WINDOWS_NET_HPP__ 14 #define __STOUT_WINDOWS_NET_HPP__ 32 struct addrinfoW addr;
33 memset(&addr, 0,
sizeof(addr));
34 addr.ai_socktype = socktype;
35 addr.ai_family = family;
36 addr.ai_flags |=
flags;
55 struct sockaddr_storage storage;
56 memset(&storage, 0,
sizeof(storage));
60 struct sockaddr_in addr;
61 memset(&addr, 0,
sizeof(addr));
62 addr.sin_family = AF_INET;
63 addr.sin_addr = ip.
in().get();
66 memcpy(&storage, &addr,
sizeof(addr));
70 struct sockaddr_in6 addr;
71 memset(&addr, 0,
sizeof(addr));
72 addr.sin6_family = AF_INET6;
73 addr.sin6_addr = ip.
in6().get();
76 memcpy(&storage, &addr,
sizeof(addr));
87 if (ip.
family() == AF_INET) {
88 length =
sizeof(
struct sockaddr_in);
89 }
else if (ip.
family() == AF_INET6) {
90 length =
sizeof(
struct sockaddr_in6);
95 int error = GetNameInfoW(
96 (
struct sockaddr*) &storage,
108 return stringify(std::wstring(hostname));
117 struct addrinfoW* result =
nullptr;
120 GetAddrInfoW(wide_stringify(hostname).data(),
nullptr, &hints, &result);
126 if (result->ai_addr ==
nullptr) {
127 FreeAddrInfoW(result);
128 return Error(
"No addresses found");
134 FreeAddrInfoW(result);
135 return Error(
"Unsupported family type");
138 FreeAddrInfoW(result);
156 if (GetAdaptersInfo(
nullptr, &size) != ERROR_BUFFER_OVERFLOW) {
157 return WindowsError(
"Calling GetAdaptersInfo returned unexpected result");
160 std::set<std::string> names;
161 std::vector<IP_ADAPTER_INFO> adapter_info(size /
sizeof(IP_ADAPTER_INFO));
162 result = GetAdaptersInfo(
163 static_cast<PIP_ADAPTER_INFO>(adapter_info.data()),
166 if (result != NO_ERROR) {
170 foreach (
const IP_ADAPTER_INFO& ip_adapter, adapter_info) {
171 names.insert(ip_adapter.AdapterName);
187 if (::SetComputerNameW(wide_stringify(hostname).data()) == 0) {
196 #endif // __STOUT_WINDOWS_NET_HPP__ Try< Nothing > setHostname(const std::string &hostname)
Definition: net.hpp:179
Definition: nothing.hpp:16
Definition: errorbase.hpp:36
#define ABORT(...)
Definition: abort.hpp:40
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
T & get()&
Definition: try.hpp:80
Try< struct in_addr > in() const
Definition: ip.hpp:124
Definition: error.hpp:108
Try< std::string > getHostname(const IP &ip)
Definition: net.hpp:45
#define MAXHOSTNAMELEN
Definition: windows.hpp:162
Try< std::string > nodename()
Definition: os.hpp:56
Try< std::set< std::string > > links()
Definition: net.hpp:135
struct addrinfo createAddrInfo(int socktype, int family, int flags)
Definition: net.hpp:28
Try< std::string > hostname()
Definition: net.hpp:154
#define flags
Definition: decoder.hpp:18
Error GaiError(int error)
Definition: net.hpp:42
bool isError() const
Definition: try.hpp:78
Try< struct in6_addr > in6() const
Definition: ip.hpp:134
std::string error(const std::string &msg, uint32_t code)
int family() const
Definition: ip.hpp:118
Try< IP > getIP(const std::string &hostname, int family=AF_UNSPEC)
Definition: net.hpp:106
std::string stringify(int flags)
static Try< IP > create(const struct sockaddr_storage &_storage)
Definition: ip.hpp:454