LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
system_utils.hpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2018 Max Mazurov (fox.cpp) <fox.cpp [at] disroot [dot] org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <tuple>
26 #include <libwire/error.hpp>
27 #include <libwire/address.hpp>
30 
31 #if defined(LIBWIRE_POSIX)
32 # include <sys/socket.h>
33 # include <netinet/in.h>
34 #endif
35 #if defined(LIBWIRE_WINDOWS)
36 # include <winsock2.h>
37 # include <ws2tcpip.h>
38 #endif
39 
46 namespace libwire::internal_ {
47  endpoint sockaddr_to_endpoint(sockaddr_storage in);
48  sockaddr_storage endpoint_to_sockaddr(const endpoint& in);
49 
53  template<typename Func, typename... Args>
54  auto error_wrapper(const Func& func, std::error_code& ec, Args&&... args) {
55  ec = std::error_code();
56  decltype(func(std::forward<Args>(args)...)) res;
57  do {
58  res = func(std::forward<Args>(args)...);
59  if (res < 0) {
60  ec = last_system_error();
61  }
62  } while (ec == error::interrupted);
63  return res;
64  }
65 } // namespace libwire::internal_
This file defines universal address structure for both IPv4 and IPv6 network addresses.
System call interrupted by signal (POSIX-specific).
Definition: error.hpp:160
This header defines set of enumerations for platform-independent error code handling.