uvcc
libuv C++ bindings
netstruct.hpp
1 
2 #ifndef UVCC_NETSTRUCT__HPP
3 #define UVCC_NETSTRUCT__HPP
4 
5 #include <cstring> // memset()
6 #include <cstdlib> // atoi()
7 #include <uv.h>
8 
9 #ifdef _WIN32
10 #include <Winsock2.h> // sockaddr_storage sockaddr_in sockaddr_in6
11 #include <Ws2tcpip.h> // addrinfo AI_ADDRCONFIG
12 #else
13 #include <sys/socket.h> // sockaddr_storage
14 #include <netinet/in.h> // sockaddr_in sockaddr_in6
15 #include <netdb.h> // addrinfo AI_ADDRCONFIG
16 #endif
17 
18 
19 namespace uv
20 {
21 /*! \defgroup doxy_group__netstruct Network-related structures initialization
22  \ingroup doxy_group__utility
23  \sa Linux: [ip(7):`sockaddr_in`](http://man7.org/linux/man-pages/man7/ip.7.html),
24  [ipv6(7):`sockaddr_in6`](http://man7.org/linux/man-pages/man7/ipv6.7.html),
25  [socket(7):`sockaddr_storage`](http://man7.org/linux/man-pages/man7/socket.7.html).\n
26  Windows: [`sockaddr_in`](https://msdn.microsoft.com/en-us/library/ms740496(v=vs.85).aspx),
27  [`sockaddr_in6`](https://msdn.microsoft.com/en-us/library/ms740496(v=vs.85).aspx),
28  [`sockaddr_storage`](https://msdn.microsoft.com/en-us/library/ms740504(v=vs.85).aspx). */
29 //! \{
30 
31 //! \name sockaddr_in
32 //! \{
33 
34 /*! \brief Initialize a `sockaddr_in` structure. */
35 inline int init(::sockaddr_in &_sin)
36 {
37  std::memset(&_sin, 0, sizeof(_sin));
38  _sin.sin_family = AF_INET;
39  return 0;
40 }
41 /*! \brief Initialize a `sockaddr_in` structure from strings containing an IPv4 address and (optionally) a port.
42  \sa libuv API documentation: [`uv_ip4_addr()`](http://docs.libuv.org/en/v1.x/misc.html#c.uv_ip4_addr),
43  [`uv_inet_pton()`](http://docs.libuv.org/en/v1.x/misc.html#c.uv_inet_pton).
44  \sa Linux: [`inet_pton()`](http://man7.org/linux/man-pages/man3/inet_pton.3.html).\n
45  Windows: [`RtlIpv4StringToAddressEx()`](https://msdn.microsoft.com/en-us/library/aa814459(v=vs.85).aspx),
46  [`InetPton()`](https://msdn.microsoft.com/en-us/library/cc805844(v=vs.85).aspx). */
47 inline int init(::sockaddr_in &_sin, const char *_ip, const char *_port = nullptr)
48 {
49  int pnum = _port ? std::atoi(_port) : 0;
50  return ::uv_ip4_addr(_ip, pnum, &_sin);
51 }
52 
53 //! \}
54 
55 
56 //! \name sockaddr_in6
57 //! \{
58 
59 /*! \brief Initialize a `sockaddr_in6` structure. */
60 inline int init(::sockaddr_in6 &_sin6)
61 {
62  std::memset(&_sin6, 0, sizeof(_sin6));
63  _sin6.sin6_family = AF_INET6;
64  return 0;
65 }
66 /*! \brief Initialize a `sockaddr_in6` structure from strings containing an IPv6 address and (optionally) a port.
67  \sa libuv API documentation: [`uv_ip6_addr()`](http://docs.libuv.org/en/v1.x/misc.html#c.uv_ip6_addr),
68  [`uv_inet_pton()`](http://docs.libuv.org/en/v1.x/misc.html#c.uv_inet_pton).
69  \sa Linux: [`inet_pton()`](http://man7.org/linux/man-pages/man3/inet_pton.3.html).\n
70  Windows: [`RtlIpv6StringToAddressEx()`](https://msdn.microsoft.com/en-us/library/aa814463(v=vs.85).aspx),
71  [`InetPton()`](https://msdn.microsoft.com/en-us/library/cc805844(v=vs.85).aspx). */
72 inline int init(::sockaddr_in6 &_sin6, const char *_ip, const char *_port = nullptr)
73 {
74  int pnum = _port ? std::atoi(_port) : 0;
75  return ::uv_ip6_addr(_ip, pnum, &_sin6);
76 }
77 
78 //! \}
79 
80 
81 //! \name sockaddr_storage
82 //! \{
83 
84 inline int init(::sockaddr_storage &_ss, decltype(::sockaddr::sa_family) _family = AF_UNSPEC)
85 {
86  std::memset(&_ss, 0, sizeof(_ss));
87  _ss.ss_family = _family;
88  return 0;
89 }
90 
91 inline int init(::sockaddr_storage &_ss, const ::sockaddr &_sa)
92 {
93  std::memset(&_ss, 0, sizeof(_ss));
94  switch (_sa.sa_family)
95  {
96  case AF_INET:
97  reinterpret_cast< ::sockaddr_in& >(_ss) = reinterpret_cast< const ::sockaddr_in& >(_sa);
98  break;
99  case AF_INET6:
100  reinterpret_cast< ::sockaddr_in6& >(_ss) = reinterpret_cast< const ::sockaddr_in6& >(_sa);
101  break;
102  default:
103  return UV_EAFNOSUPPORT;
104  }
105  return 0;
106 }
107 
108 inline int init(::sockaddr_storage &_ss, const char *_ip, const char *_port = nullptr)
109 {
110  std::memset(&_ss, 0, sizeof(_ss));
111  return (
112  init(reinterpret_cast< ::sockaddr_in& >(_ss), _ip, _port) == 0
113  or
114  init(reinterpret_cast< ::sockaddr_in6& >(_ss), _ip, _port) == 0
115  ) ? 0 : UV_EINVAL;
116 }
117 
118 //! \}
119 
120 
121 //! \name addrinfo
122 //! \{
123 
124 /*! \brief Initialize an `addrinfo` structure for to be used as a hints argument in `uv::getaddrinfo` request.
125  \details
126  The `_family` argument might be AF_UNSPEC or AF_INET or AF_INET6 or (Windows only) AF_NETBIOS.
127 
128  The `_socktype` argument might be SOCK_DGRAM or SOCK_STREAM.
129 
130  The `_flags` argument can be a combination of the following values:
131  - AI_ADDRCONFIG
132  - AI_ALL
133  - AI_CANONNAME
134  - AI_NUMERICHOST
135  - AI_NUMERICSERV
136  - AI_PASSIVE
137  - AI_V4MAPPED
138  .
139  Linux only - extensions for Internationalized Domain Names:
140  - AI_CANONIDN
141  - AI_IDN
142  - AI_IDN_ALLOW_UNASSIGNED
143  - AI_IDN_USE_STD3_ASCII_RULES
144  .
145  Windows only:
146  - AI_DISABLE_IDN_ENCODING
147  - AI_FILESERVER
148  - AI_FLAGS
149  - AI_FQDN
150  - AI_NON_AUTHORITATIVE
151  - AI_RETURN_PREFERRED_NAMES
152  - AI_SECURE
153  .
154  \sa Linux: [`getaddrinfo()`](http://man7.org/linux/man-pages/man3/getaddrinfo.3.html).\n
155  Windows: [`addrinfo`](https://msdn.microsoft.com/en-us/library/ms737530(v=vs.85).aspx),
156  [`getaddrinfo()`](https://msdn.microsoft.com/en-us/library/ms738520(v=vs.85).aspx),
157  [`GetAddrInfoEx()`](https://msdn.microsoft.com/en-us/library/ms738518(v=vs.85).aspx). */
158 inline int init(::addrinfo &_ai,
159  decltype(::addrinfo::ai_family) _family = AF_UNSPEC,
160  decltype(::addrinfo::ai_socktype) _socktype = 0,
161  decltype(::addrinfo::ai_flags) _flags = AI_ADDRCONFIG
162 )
163 {
164  std::memset(&_ai, 0, sizeof(_ai));
165  _ai.ai_family = _family;
166  _ai.ai_socktype = _socktype;
167  _ai.ai_flags = _flags;
168  return 0;
169 }
170 
171 //! \}
172 
173 
174 //! \}
175 }
176 
177 #endif
Namespace for all uvcc definitions.
Definition: buffer.hpp:17
int init(::addrinfo &_ai, decltype(::addrinfo::ai_family) _family=AF_UNSPEC, decltype(::addrinfo::ai_socktype) _socktype=0, decltype(::addrinfo::ai_flags) _flags=AI_ADDRCONFIG)
Initialize an addrinfo structure for to be used as a hints argument in uv::getaddrinfo request...
Definition: netstruct.hpp:158
int init(::sockaddr_in &_sin, const char *_ip, const char *_port=nullptr)
Initialize a sockaddr_in structure from strings containing an IPv4 address and (optionally) a port...
Definition: netstruct.hpp:47
int init(::sockaddr_in6 &_sin6, const char *_ip, const char *_port=nullptr)
Initialize a sockaddr_in6 structure from strings containing an IPv6 address and (optionally) a port...
Definition: netstruct.hpp:72
int init(::sockaddr_in6 &_sin6)
Initialize a sockaddr_in6 structure.
Definition: netstruct.hpp:60
int init(::sockaddr_in &_sin)
Initialize a sockaddr_in structure.
Definition: netstruct.hpp:35