13 #ifndef __STOUT_POSIX_NET_HPP__ 14 #define __STOUT_POSIX_NET_HPP__ 31 memset(&addr, 0,
sizeof(addr));
32 addr.ai_socktype = socktype;
33 addr.ai_family = family;
34 addr.ai_flags |=
flags;
47 struct sockaddr_storage storage;
48 memset(&storage, 0,
sizeof(storage));
52 struct sockaddr_in addr;
53 memset(&addr, 0,
sizeof(addr));
54 addr.sin_family = AF_INET;
55 addr.sin_addr = ip.
in().get();
58 memcpy(&storage, &addr,
sizeof(addr));
62 struct sockaddr_in6 addr;
63 memset(&addr, 0,
sizeof(addr));
64 addr.sin6_family = AF_INET6;
65 addr.sin6_addr = ip.
in6().get();
68 memcpy(&storage, &addr,
sizeof(addr));
79 if (ip.
family() == AF_INET) {
80 length =
sizeof(
struct sockaddr_in);
81 }
else if (ip.
family() == AF_INET6) {
82 length =
sizeof(
struct sockaddr_in6);
87 int error = getnameinfo(
88 (
struct sockaddr*) &storage,
97 return Error(std::string(gai_strerror(error)));
100 return std::string(hostname);
109 struct addrinfo* result =
nullptr;
111 int error = getaddrinfo(hostname.c_str(),
nullptr, &hints, &result);
114 return Error(gai_strerror(error));
117 if (result->ai_addr ==
nullptr) {
118 freeaddrinfo(result);
119 return Error(
"No addresses found");
125 freeaddrinfo(result);
126 return Error(
"Unsupported family type");
129 freeaddrinfo(result);
137 struct ifaddrs* ifaddr =
nullptr;
138 if (getifaddrs(&ifaddr) == -1) {
142 std::set<std::string> names;
143 for (
struct ifaddrs* ifa = ifaddr; ifa !=
nullptr; ifa = ifa->ifa_next) {
144 if (ifa->ifa_name !=
nullptr) {
145 names.insert(ifa->ifa_name);
158 if (gethostname(host,
sizeof(host)) < 0) {
162 struct addrinfo hints =
createAddrInfo(SOCK_STREAM, AF_UNSPEC, AI_CANONNAME);
163 struct addrinfo* result =
nullptr;
165 int error = getaddrinfo(host,
nullptr, &hints, &result);
168 return Error(gai_strerror(error));
171 std::string
hostname = result->ai_canonname;
172 freeaddrinfo(result);
181 if (sethostname(hostname.c_str(), hostname.size()) != 0) {
190 #endif // __STOUT_POSIX_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
T & get()&
Definition: try.hpp:80
Try< struct in_addr > in() const
Definition: ip.hpp:124
Definition: errorbase.hpp:50
Try< std::string > getHostname(const IP &ip)
Definition: net.hpp:45
#define MAXHOSTNAMELEN
Definition: windows.hpp:162
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
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