internet 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  1. // <experimental/internet> -*- C++ -*-
  2. // Copyright (C) 2015-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/internet
  21. * This is a TS C++ Library header.
  22. * @ingroup networking-ts
  23. */
  24. #ifndef _GLIBCXX_EXPERIMENTAL_INTERNET
  25. #define _GLIBCXX_EXPERIMENTAL_INTERNET
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201402L
  28. #include <experimental/netfwd>
  29. #include <experimental/io_context>
  30. #include <experimental/bits/net.h>
  31. #include <array>
  32. #include <forward_list>
  33. #include <sstream>
  34. #include <cstdint>
  35. #include <experimental/string_view>
  36. #ifdef _GLIBCXX_HAVE_UNISTD_H
  37. # include <unistd.h>
  38. #endif
  39. #ifdef _GLIBCXX_HAVE_SYS_SOCKET_H
  40. # include <sys/socket.h> // AF_INET, AF_INET6, SOCK_DGRAM, SOCK_STREAM
  41. #endif
  42. #ifdef _GLIBCXX_HAVE_ARPA_INET_H
  43. # include <arpa/inet.h> // inet_ntop
  44. #endif
  45. #ifdef _GLIBCXX_HAVE_NETINET_IN_H
  46. # include <netinet/in.h> // IPPROTO_IP
  47. #endif
  48. #ifdef _GLIBCXX_HAVE_NETINET_TCP_H
  49. # include <netinet/tcp.h> // TCP_NODELAY
  50. #endif
  51. #ifdef _GLIBCXX_HAVE_NETDB_H
  52. # include <netdb.h> // getaddrinfo etc.
  53. #endif
  54. namespace std _GLIBCXX_VISIBILITY(default)
  55. {
  56. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  57. namespace experimental
  58. {
  59. namespace net
  60. {
  61. inline namespace v1
  62. {
  63. namespace ip
  64. {
  65. /** @addtogroup networking-ts
  66. * @{
  67. */
  68. #ifdef _GLIBCXX_HAVE_NETDB_H
  69. /** Error codes for resolver errors.
  70. * @{
  71. */
  72. enum class resolver_errc : int {
  73. host_not_found = EAI_NONAME,
  74. host_not_found_try_again = EAI_AGAIN,
  75. service_not_found = EAI_SERVICE
  76. };
  77. /// Error category for resolver errors.
  78. inline const error_category& resolver_category() noexcept // TODO non-inline
  79. {
  80. struct __cat : error_category
  81. {
  82. const char* name() const noexcept { return "resolver"; }
  83. std::string message(int __e) const { return ::gai_strerror(__e); }
  84. virtual void __message(int) { } // TODO dual ABI XXX
  85. };
  86. static __cat __c;
  87. return __c;
  88. }
  89. error_code make_error_code(resolver_errc __e) noexcept
  90. { return error_code(static_cast<int>(__e), resolver_category()); }
  91. error_condition make_error_condition(resolver_errc __e) noexcept
  92. { return error_condition(static_cast<int>(__e), resolver_category()); }
  93. /// @}
  94. #endif
  95. using port_type = uint_least16_t; ///< Type used for port numbers.
  96. using scope_id_type = uint_least32_t; ///< Type used for IPv6 scope IDs.
  97. /// Convenience alias for constraining allocators for strings.
  98. template<typename _Alloc>
  99. using __string_with
  100. = enable_if_t<std::is_same<typename _Alloc::value_type, char>::value,
  101. std::basic_string<char, std::char_traits<char>, _Alloc>>;
  102. /** Tag indicating conversion between IPv4 and IPv4-mapped IPv6 addresses.
  103. * @{
  104. */
  105. struct v4_mapped_t {};
  106. constexpr v4_mapped_t v4_mapped;
  107. /// @}
  108. /// An IPv4 address.
  109. class address_v4
  110. {
  111. public:
  112. // types:
  113. using uint_type = uint_least32_t;
  114. struct bytes_type : array<unsigned char, 4>
  115. {
  116. template<typename... _Tp>
  117. explicit constexpr
  118. bytes_type(_Tp... __tp)
  119. : array<unsigned char, 4>{{static_cast<unsigned char>(__tp)...}}
  120. {
  121. #if UCHAR_MAX > 0xFF
  122. for (auto __b : *this)
  123. if (__b > 0xFF)
  124. __throw_out_of_range("invalid address_v4::bytes_type value");
  125. #endif
  126. }
  127. };
  128. // constructors:
  129. constexpr address_v4() noexcept : _M_addr(0) { }
  130. constexpr address_v4(const address_v4& a) noexcept = default;
  131. constexpr
  132. address_v4(const bytes_type& __b)
  133. : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3])
  134. { }
  135. explicit constexpr
  136. address_v4(uint_type __val) : _M_addr(_S_hton_32(__val))
  137. {
  138. #if UINT_LEAST32_MAX > 0xFFFFFFFF
  139. if (__val > 0xFFFFFFFF)
  140. __throw_out_of_range("invalid address_v4::uint_type value");
  141. #endif
  142. }
  143. // assignment:
  144. address_v4& operator=(const address_v4& a) noexcept = default;
  145. // members:
  146. constexpr bool is_unspecified() const noexcept { return to_uint() == 0; }
  147. constexpr bool
  148. is_loopback() const noexcept
  149. { return (to_uint() & 0xFF000000) == 0x7F000000; }
  150. constexpr bool
  151. is_multicast() const noexcept
  152. { return (to_uint() & 0xF0000000) == 0xE0000000; }
  153. constexpr bytes_type
  154. to_bytes() const noexcept
  155. {
  156. return bytes_type{
  157. (_M_addr >> 24) & 0xFF,
  158. (_M_addr >> 16) & 0xFF,
  159. (_M_addr >> 8) & 0xFF,
  160. _M_addr & 0xFF
  161. };
  162. }
  163. constexpr uint_type
  164. to_uint() const noexcept { return _S_ntoh_32(_M_addr); }
  165. #ifdef _GLIBCXX_HAVE_ARPA_INET_H
  166. template<typename _Allocator = allocator<char>>
  167. __string_with<_Allocator>
  168. to_string(const _Allocator& __a = _Allocator()) const
  169. {
  170. __string_with<_Allocator> __str(__a);
  171. __str.resize(INET6_ADDRSTRLEN);
  172. if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size()))
  173. __str.erase(__str.find('\0'));
  174. else
  175. __str.resize(0);
  176. return __str;
  177. }
  178. #endif
  179. // static members:
  180. static constexpr address_v4 any() noexcept { return address_v4{}; }
  181. static constexpr
  182. address_v4 loopback() noexcept { return address_v4{0x7F000001}; }
  183. static constexpr
  184. address_v4 broadcast() noexcept { return address_v4{0xFFFFFFFF}; }
  185. private:
  186. template<typename _InternetProtocol>
  187. friend class basic_endpoint;
  188. friend address_v4 make_address_v4(const char*, error_code&) noexcept;
  189. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  190. static constexpr uint16_t _S_hton_16(uint16_t __h) { return __h; }
  191. static constexpr uint16_t _S_ntoh_16(uint16_t __n) { return __n; }
  192. static constexpr uint32_t _S_hton_32(uint32_t __h) { return __h; }
  193. static constexpr uint32_t _S_ntoh_32(uint32_t __n) { return __n; }
  194. #else
  195. static constexpr uint16_t
  196. _S_hton_16(uint16_t __h) { return __builtin_bswap16(__h); }
  197. static constexpr uint16_t
  198. _S_ntoh_16(uint16_t __n) { return __builtin_bswap16(__n); }
  199. static constexpr uint32_t
  200. _S_hton_32(uint32_t __h) { return __builtin_bswap32(__h); }
  201. static constexpr uint32_t
  202. _S_ntoh_32(uint32_t __n) { return __builtin_bswap32(__n); }
  203. #endif
  204. in_addr_t _M_addr; // network byte order
  205. };
  206. /// An IPv6 address.
  207. class address_v6
  208. {
  209. public:
  210. // types:
  211. struct bytes_type : array<unsigned char, 16>
  212. {
  213. template<typename... _Tp> explicit constexpr bytes_type(_Tp... __t)
  214. : array<unsigned char, 16>{{static_cast<unsigned char>(__t)...}} { }
  215. };
  216. // constructors:
  217. constexpr address_v6() noexcept : _M_bytes(), _M_scope_id() { }
  218. constexpr address_v6(const address_v6& __a) noexcept = default;
  219. constexpr
  220. address_v6(const bytes_type& __bytes, scope_id_type __scope = 0)
  221. : _M_bytes(__bytes), _M_scope_id(__scope)
  222. { }
  223. // assignment:
  224. address_v6& operator=(const address_v6& __a) noexcept = default;
  225. // members:
  226. void scope_id(scope_id_type __id) noexcept { _M_scope_id = __id; }
  227. constexpr scope_id_type scope_id() const noexcept { return _M_scope_id; }
  228. constexpr bool
  229. is_unspecified() const noexcept
  230. {
  231. for (int __i = 0; __i < 16; ++__i)
  232. if (_M_bytes[__i] != 0x00)
  233. return false;
  234. return _M_scope_id == 0;
  235. }
  236. constexpr bool
  237. is_loopback() const noexcept
  238. {
  239. for (int __i = 0; __i < 15; ++__i)
  240. if (_M_bytes[__i] != 0x00)
  241. return false;
  242. return _M_bytes[15] == 0x01 && _M_scope_id == 0;
  243. }
  244. constexpr bool
  245. is_multicast() const noexcept { return _M_bytes[0] == 0xFF; }
  246. constexpr bool
  247. is_link_local() const noexcept
  248. { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0x80; }
  249. constexpr bool
  250. is_site_local() const noexcept
  251. { return _M_bytes[0] == 0xFE && (_M_bytes[1] & 0xC0) == 0xC0; }
  252. constexpr bool
  253. is_v4_mapped() const noexcept
  254. {
  255. const bytes_type& __b = _M_bytes;
  256. return __b[0] == 0 && __b[1] == 0 && __b[ 2] == 0 && __b[ 3] == 0
  257. && __b[4] == 0 && __b[5] == 0 && __b[ 6] == 0 && __b[ 7] == 0
  258. && __b[8] == 0 && __b[9] == 0 && __b[10] == 0xFF && __b[11] == 0xFF;
  259. }
  260. constexpr bool
  261. is_multicast_node_local() const noexcept
  262. { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x01; }
  263. constexpr bool
  264. is_multicast_link_local() const noexcept
  265. { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x02; }
  266. constexpr bool
  267. is_multicast_site_local() const noexcept
  268. { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x05; }
  269. constexpr bool
  270. is_multicast_org_local() const noexcept
  271. { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x08; }
  272. constexpr bool
  273. is_multicast_global() const noexcept
  274. { return is_multicast() && (_M_bytes[1] & 0x0F) == 0x0b; }
  275. constexpr bytes_type to_bytes() const noexcept { return _M_bytes; }
  276. #ifdef _GLIBCXX_HAVE_ARPA_INET_H
  277. template<typename _Allocator = allocator<char>>
  278. __string_with<_Allocator>
  279. to_string(const _Allocator& __a = _Allocator()) const
  280. {
  281. __string_with<_Allocator> __str(__a);
  282. __str.resize(INET6_ADDRSTRLEN + (_M_scope_id ? 11 : 0));
  283. char* const __p = &__str.front();
  284. if (inet_ntop(AF_INET6, &_M_bytes, __p, __str.size()))
  285. {
  286. auto __end = __str.find('\0');
  287. if (unsigned long __scope = _M_scope_id)
  288. {
  289. __end +=
  290. #if _GLIBCXX_USE_C99_STDIO
  291. __builtin_snprintf(__p + __end, __str.size() - __end,
  292. "%%%lu", __scope);
  293. #else
  294. __builtin_sprintf(__p + __end, "%%%lu", __scope);
  295. #endif
  296. }
  297. __str.erase(__end);
  298. }
  299. else
  300. __str.resize(0);
  301. return __str;
  302. }
  303. #endif
  304. // static members:
  305. static constexpr address_v6
  306. any() noexcept
  307. {
  308. return {};
  309. }
  310. static constexpr address_v6
  311. loopback() noexcept
  312. {
  313. return {bytes_type{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
  314. }
  315. private:
  316. template<typename _InternetProtocol>
  317. friend class basic_endpoint;
  318. friend constexpr bool
  319. operator==(const address_v6&, const address_v6&) noexcept;
  320. friend constexpr bool
  321. operator< (const address_v6&, const address_v6&) noexcept;
  322. bytes_type _M_bytes;
  323. scope_id_type _M_scope_id;
  324. };
  325. /// Exception type thrown on misuse of IPv4 addresses as IPv6 or vice versa.
  326. class bad_address_cast : public bad_cast
  327. {
  328. public:
  329. bad_address_cast() { }
  330. const char* what() const noexcept { return "bad address cast"; }
  331. };
  332. /// An IPv4 or IPv6 address.
  333. class address
  334. {
  335. public:
  336. // constructors:
  337. constexpr address() noexcept : _M_v4(), _M_is_v4(true) { }
  338. constexpr
  339. address(const address& __a) noexcept : _M_uninit(), _M_is_v4(__a._M_is_v4)
  340. {
  341. if (_M_is_v4)
  342. ::new (std::addressof(_M_v4)) address_v4(__a.to_v4());
  343. else
  344. ::new (std::addressof(_M_v6)) address_v6(__a.to_v6());
  345. }
  346. constexpr
  347. address(const address_v4& __a) noexcept : _M_v4(__a), _M_is_v4(true) { }
  348. constexpr
  349. address(const address_v6& __a) noexcept : _M_v6(__a), _M_is_v4(false) { }
  350. // assignment:
  351. address&
  352. operator=(const address& __a) noexcept
  353. {
  354. if (__a._M_is_v4)
  355. *this = __a.to_v4();
  356. else
  357. *this = __a.to_v6();
  358. return *this;
  359. }
  360. address&
  361. operator=(const address_v4& __a) noexcept
  362. {
  363. ::new (std::addressof(_M_v4)) address_v4(__a);
  364. _M_is_v4 = true;
  365. return *this;
  366. }
  367. address&
  368. operator=(const address_v6& __a) noexcept
  369. {
  370. ::new (std::addressof(_M_v6)) address_v6(__a);
  371. _M_is_v4 = false;
  372. return *this;
  373. }
  374. // members:
  375. constexpr bool is_v4() const noexcept { return _M_is_v4; }
  376. constexpr bool is_v6() const noexcept { return !_M_is_v4; }
  377. constexpr address_v4
  378. to_v4() const
  379. {
  380. if (!is_v4())
  381. _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
  382. return _M_v4;
  383. }
  384. constexpr address_v6
  385. to_v6() const
  386. {
  387. if (!is_v6())
  388. _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
  389. return _M_v6;
  390. }
  391. constexpr bool
  392. is_unspecified() const noexcept
  393. { return _M_is_v4 ? _M_v4.is_unspecified() : _M_v6.is_unspecified(); }
  394. constexpr bool
  395. is_loopback() const noexcept
  396. { return _M_is_v4 ? _M_v4.is_loopback() : _M_v6.is_loopback(); }
  397. constexpr bool
  398. is_multicast() const noexcept
  399. { return _M_is_v4 ? _M_v4.is_multicast() : _M_v6.is_multicast(); }
  400. template<typename _Allocator = allocator<char>>
  401. __string_with<_Allocator>
  402. to_string(const _Allocator& __a = _Allocator()) const
  403. {
  404. if (_M_is_v4)
  405. return to_v4().to_string(__a);
  406. return to_v6().to_string(__a);
  407. }
  408. private:
  409. template<typename _InternetProtocol>
  410. friend class basic_endpoint;
  411. friend constexpr bool
  412. operator==(const address&, const address&) noexcept;
  413. friend constexpr bool
  414. operator<(const address&, const address&) noexcept;
  415. union {
  416. address_v4 _M_v4;
  417. address_v6 _M_v6;
  418. bool _M_uninit;
  419. };
  420. bool _M_is_v4;
  421. };
  422. /** ip::address_v4 comparisons
  423. * @{
  424. */
  425. constexpr bool
  426. operator==(const address_v4& __a, const address_v4& __b) noexcept
  427. { return __a.to_uint() == __b.to_uint(); }
  428. constexpr bool
  429. operator!=(const address_v4& __a, const address_v4& __b) noexcept
  430. { return !(__a == __b); }
  431. constexpr bool
  432. operator< (const address_v4& __a, const address_v4& __b) noexcept
  433. { return __a.to_uint() < __b.to_uint(); }
  434. constexpr bool
  435. operator> (const address_v4& __a, const address_v4& __b) noexcept
  436. { return __b < __a; }
  437. constexpr bool
  438. operator<=(const address_v4& __a, const address_v4& __b) noexcept
  439. { return !(__b < __a); }
  440. constexpr bool
  441. operator>=(const address_v4& __a, const address_v4& __b) noexcept
  442. { return !(__a < __b); }
  443. /// @}
  444. /** ip::address_v6 comparisons
  445. * @{
  446. */
  447. constexpr bool
  448. operator==(const address_v6& __a, const address_v6& __b) noexcept
  449. {
  450. const auto& __aa = __a._M_bytes;
  451. const auto& __bb = __b._M_bytes;
  452. int __i = 0;
  453. for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
  454. ;
  455. return __i == 16 ? __a.scope_id() == __b.scope_id() : false;
  456. }
  457. constexpr bool
  458. operator!=(const address_v6& __a, const address_v6& __b) noexcept
  459. { return !(__a == __b); }
  460. constexpr bool
  461. operator< (const address_v6& __a, const address_v6& __b) noexcept
  462. {
  463. const auto& __aa = __a._M_bytes;
  464. const auto& __bb = __b._M_bytes;
  465. int __i = 0;
  466. for (; __i < 16 && __aa[__i] == __bb[__i]; ++__i)
  467. ;
  468. return __i == 16 ? __a.scope_id() < __b.scope_id() : __aa[__i] < __bb[__i];
  469. }
  470. constexpr bool
  471. operator> (const address_v6& __a, const address_v6& __b) noexcept
  472. { return __b < __a; }
  473. constexpr bool
  474. operator<=(const address_v6& __a, const address_v6& __b) noexcept
  475. { return !(__b < __a); }
  476. constexpr bool
  477. operator>=(const address_v6& __a, const address_v6& __b) noexcept
  478. { return !(__a < __b); }
  479. /// @}
  480. /** ip::address comparisons
  481. * @{
  482. */
  483. constexpr bool
  484. operator==(const address& __a, const address& __b) noexcept
  485. {
  486. if (__a.is_v4())
  487. return __b.is_v4() ? __a._M_v4 == __b._M_v4 : false;
  488. return __b.is_v4() ? false : __a._M_v6 == __b._M_v6;
  489. }
  490. constexpr bool
  491. operator!=(const address& __a, const address& __b) noexcept
  492. { return !(__a == __b); }
  493. constexpr bool
  494. operator< (const address& __a, const address& __b) noexcept
  495. {
  496. if (__a.is_v4())
  497. return __b.is_v4() ? __a._M_v4 < __b._M_v4 : true;
  498. return __b.is_v4() ? false : __a._M_v6 < __b._M_v6;
  499. }
  500. constexpr bool
  501. operator> (const address& __a, const address& __b) noexcept
  502. { return __b < __a; }
  503. constexpr bool
  504. operator<=(const address& __a, const address& __b) noexcept
  505. { return !(__b < __a); }
  506. constexpr bool
  507. operator>=(const address& __a, const address& __b) noexcept
  508. { return !(__a < __b); }
  509. /// @}
  510. /** ip::address_v4 creation
  511. * @{
  512. */
  513. constexpr address_v4
  514. make_address_v4(const address_v4::bytes_type& __b)
  515. { return address_v4{__b}; }
  516. constexpr address_v4
  517. make_address_v4(address_v4::uint_type __val)
  518. { return address_v4{__val}; }
  519. constexpr address_v4
  520. make_address_v4(v4_mapped_t, const address_v6& __a)
  521. {
  522. if (!__a.is_v4_mapped())
  523. _GLIBCXX_THROW_OR_ABORT(bad_address_cast());
  524. const auto __v6b = __a.to_bytes();
  525. return address_v4::bytes_type(__v6b[12], __v6b[13], __v6b[14], __v6b[15]);
  526. }
  527. inline address_v4
  528. make_address_v4(const char* __str, error_code& __ec) noexcept
  529. {
  530. address_v4 __a;
  531. const int __res = ::inet_pton(AF_INET, __str, &__a._M_addr);
  532. if (__res == 1)
  533. {
  534. __ec.clear();
  535. return __a;
  536. }
  537. if (__res == 0)
  538. __ec = std::make_error_code(std::errc::invalid_argument);
  539. else
  540. __ec.assign(errno, generic_category());
  541. return {};
  542. }
  543. inline address_v4
  544. make_address_v4(const char* __str)
  545. { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
  546. inline address_v4
  547. make_address_v4(const string& __str, error_code& __ec) noexcept
  548. { return make_address_v4(__str.c_str(), __ec); }
  549. inline address_v4
  550. make_address_v4(const string& __str)
  551. { return make_address_v4(__str.c_str()); }
  552. inline address_v4
  553. make_address_v4(string_view __str, error_code& __ec) noexcept
  554. {
  555. char __buf[INET_ADDRSTRLEN];
  556. auto __len = __str.copy(__buf, sizeof(__buf));
  557. if (__len == sizeof(__buf))
  558. {
  559. __ec = std::make_error_code(std::errc::invalid_argument);
  560. return {};
  561. }
  562. __ec.clear();
  563. __buf[__len] = '\0';
  564. return make_address_v4(__buf, __ec);
  565. }
  566. inline address_v4
  567. make_address_v4(string_view __str)
  568. { return make_address_v4(__str, __throw_on_error{"make_address_v4"}); }
  569. /// @}
  570. /** ip::address_v6 creation
  571. * @{
  572. */
  573. constexpr address_v6
  574. make_address_v6(const address_v6::bytes_type& __b, scope_id_type __scope = 0)
  575. { return address_v6{__b, __scope}; }
  576. constexpr address_v6
  577. make_address_v6(v4_mapped_t, const address_v4& __a) noexcept
  578. {
  579. const address_v4::bytes_type __v4b = __a.to_bytes();
  580. address_v6::bytes_type __v6b(0, 0, 0, 0, 0, 0, 0, 0,
  581. 0, 0, 0xFF, 0xFF,
  582. __v4b[0], __v4b[1], __v4b[2], __v4b[3]);
  583. return address_v6(__v6b);
  584. }
  585. inline address_v6
  586. __make_address_v6(const char* __addr, const char* __scope, error_code& __ec)
  587. {
  588. address_v6::bytes_type __b;
  589. int __res = ::inet_pton(AF_INET6, __addr, __b.data());
  590. if (__res == 1)
  591. {
  592. __ec.clear();
  593. if (!__scope)
  594. {
  595. return { __b };
  596. }
  597. char* __eptr;
  598. unsigned long __val = std::strtoul(__scope, &__eptr, 10);
  599. if (__eptr != __scope && !*__eptr
  600. && __val <= numeric_limits<scope_id_type>::max())
  601. {
  602. return { __b, static_cast<scope_id_type>(__val) };
  603. }
  604. __ec = std::make_error_code(std::errc::invalid_argument);
  605. }
  606. else if (__res == 0)
  607. __ec = std::make_error_code(std::errc::invalid_argument);
  608. else
  609. __ec.assign(errno, generic_category());
  610. return {};
  611. }
  612. inline address_v6
  613. make_address_v6(const char* __str, error_code& __ec) noexcept
  614. {
  615. auto __p = __builtin_strchr(__str, '%');
  616. if (__p == nullptr)
  617. return __make_address_v6(__str, nullptr, __ec);
  618. char __buf[64];
  619. char* __out = __buf;
  620. bool __skip_leading_zero = true;
  621. while (__str < __p && __out < std::end(__buf))
  622. {
  623. if (!__skip_leading_zero || *__str != '0')
  624. {
  625. if (*__str == ':' || *__str == '.')
  626. __skip_leading_zero = true;
  627. else
  628. __skip_leading_zero = false;
  629. *__out = *__str;
  630. }
  631. __str++;
  632. }
  633. if (__out == std::end(__buf))
  634. {
  635. __ec = std::make_error_code(std::errc::invalid_argument);
  636. return {};
  637. }
  638. else
  639. {
  640. *__out = '\0';
  641. return __make_address_v6(__buf, __p + 1, __ec);
  642. }
  643. }
  644. inline address_v6
  645. make_address_v6(const char* __str)
  646. { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
  647. inline address_v6
  648. make_address_v6(const string& __str, error_code& __ec) noexcept
  649. {
  650. auto __pos = __str.find('%');
  651. if (__pos == string::npos)
  652. return __make_address_v6(__str.c_str(), nullptr, __ec);
  653. char __buf[64];
  654. char* __out = __buf;
  655. bool __skip_leading_zero = true;
  656. size_t __n = 0;
  657. while (__n < __pos && __out < std::end(__buf))
  658. {
  659. if (!__skip_leading_zero || __str[__n] != '0')
  660. {
  661. if (__str[__n] == ':' || __str[__n] == '.')
  662. __skip_leading_zero = true;
  663. else
  664. __skip_leading_zero = false;
  665. *__out = __str[__n];
  666. }
  667. __n++;
  668. }
  669. if (__out == std::end(__buf))
  670. {
  671. __ec = std::make_error_code(std::errc::invalid_argument);
  672. return {};
  673. }
  674. else
  675. {
  676. *__out = '\0';
  677. return __make_address_v6(__buf, __str.c_str() + __pos + 1, __ec);
  678. }
  679. }
  680. inline address_v6
  681. make_address_v6(const string& __str)
  682. { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
  683. inline address_v6
  684. make_address_v6(string_view __str, error_code& __ec) noexcept
  685. {
  686. char __buf[64];
  687. char* __out = __buf;
  688. char* __scope = nullptr;
  689. bool __skip_leading_zero = true;
  690. size_t __n = 0;
  691. while (__n < __str.length() && __out < std::end(__buf))
  692. {
  693. if (__str[__n] == '%')
  694. {
  695. if (__scope)
  696. __out = std::end(__buf);
  697. else
  698. {
  699. *__out = '\0';
  700. __scope = ++__out;
  701. __skip_leading_zero = true;
  702. }
  703. }
  704. else if (!__skip_leading_zero || __str[__n] != '0')
  705. {
  706. if (__str[__n] == ':' || __str[__n] == '.')
  707. __skip_leading_zero = true;
  708. else
  709. __skip_leading_zero = false;
  710. *__out = __str[__n];
  711. __out++;
  712. }
  713. __n++;
  714. }
  715. if (__out == std::end(__buf))
  716. {
  717. __ec = std::make_error_code(std::errc::invalid_argument);
  718. return {};
  719. }
  720. else
  721. {
  722. *__out = '\0';
  723. return __make_address_v6(__buf, __scope, __ec);
  724. }
  725. }
  726. inline address_v6
  727. make_address_v6(string_view __str)
  728. { return make_address_v6(__str, __throw_on_error{"make_address_v6"}); }
  729. /// @}
  730. /** ip::address creation
  731. * @{
  732. */
  733. inline address
  734. make_address(const char* __str, error_code& __ec) noexcept
  735. {
  736. address __a;
  737. address_v6 __v6a = make_address_v6(__str, __ec);
  738. if (!__ec)
  739. __a = __v6a;
  740. else
  741. {
  742. address_v4 __v4a = make_address_v4(__str, __ec);
  743. if (!__ec)
  744. __a = __v4a;
  745. }
  746. return __a;
  747. }
  748. inline address
  749. make_address(const char* __str)
  750. { return make_address(__str, __throw_on_error{"make_address"}); }
  751. inline address
  752. make_address(const string& __str, error_code& __ec) noexcept; // TODO
  753. inline address
  754. make_address(const string& __str)
  755. { return make_address(__str, __throw_on_error{"make_address"}); }
  756. inline address
  757. make_address(string_view __str, error_code& __ec) noexcept
  758. {
  759. if (__str.rfind('\0') != string_view::npos)
  760. return make_address(__str.data(), __ec);
  761. return make_address(__str.to_string(), __ec); // TODO don't allocate
  762. }
  763. inline address
  764. make_address(string_view __str)
  765. { return make_address(__str, __throw_on_error{"make_address"}); }
  766. /// @}
  767. /// ip::address I/O
  768. template<typename _CharT, typename _Traits>
  769. inline basic_ostream<_CharT, _Traits>&
  770. operator<<(basic_ostream<_CharT, _Traits>& __os, const address& __a)
  771. { return __os << __a.to_string(); }
  772. /// ip::address_v4 I/O
  773. template<typename _CharT, typename _Traits>
  774. inline basic_ostream<_CharT, _Traits>&
  775. operator<<(basic_ostream<_CharT, _Traits>& __os, const address_v4& __a)
  776. { return __os << __a.to_string(); }
  777. /// ip::address_v6 I/O
  778. template<typename _CharT, typename _Traits>
  779. inline basic_ostream<_CharT, _Traits>&
  780. operator<<(basic_ostream<_CharT, _Traits>& __os, const address_v6& __a)
  781. { return __os << __a.to_string(); }
  782. template<typename> class basic_address_iterator; // not defined
  783. template<> class basic_address_iterator<address_v4>
  784. {
  785. public:
  786. // types:
  787. using value_type = address_v4;
  788. using difference_type = ptrdiff_t;
  789. using pointer = const address_v4*;
  790. using reference = const address_v4&;
  791. using iterator_category = input_iterator_tag;
  792. // constructors:
  793. basic_address_iterator(const address_v4& __a) noexcept
  794. : _M_address(__a) { }
  795. // members:
  796. reference operator*() const noexcept { return _M_address; }
  797. pointer operator->() const noexcept { return &_M_address; }
  798. basic_address_iterator&
  799. operator++() noexcept
  800. {
  801. _M_address = value_type(_M_address.to_uint() + 1);
  802. return *this;
  803. }
  804. basic_address_iterator operator++(int) noexcept
  805. {
  806. auto __tmp = *this;
  807. ++*this;
  808. return __tmp;
  809. }
  810. basic_address_iterator& operator--() noexcept
  811. {
  812. _M_address = value_type(_M_address.to_uint() - 1);
  813. return *this;
  814. }
  815. basic_address_iterator
  816. operator--(int) noexcept
  817. {
  818. auto __tmp = *this;
  819. --*this;
  820. return __tmp;
  821. }
  822. bool
  823. operator==(const basic_address_iterator& __rhs) const noexcept
  824. { return _M_address == __rhs._M_address; }
  825. bool
  826. operator!=(const basic_address_iterator& __rhs) const noexcept
  827. { return _M_address != __rhs._M_address; }
  828. private:
  829. address_v4 _M_address;
  830. };
  831. using address_v4_iterator = basic_address_iterator<address_v4>;
  832. template<> class basic_address_iterator<address_v6>
  833. {
  834. public:
  835. // types:
  836. using value_type = address_v6;
  837. using difference_type = ptrdiff_t;
  838. using pointer = const address_v6*;
  839. using reference = const address_v6&;
  840. using iterator_category = input_iterator_tag;
  841. // constructors:
  842. basic_address_iterator(const address_v6& __a) noexcept
  843. : _M_address(__a) { }
  844. // members:
  845. reference operator*() const noexcept { return _M_address; }
  846. pointer operator->() const noexcept { return &_M_address; }
  847. basic_address_iterator&
  848. operator++() noexcept; // TODO
  849. basic_address_iterator
  850. operator++(int) noexcept
  851. {
  852. auto __tmp = *this;
  853. ++*this;
  854. return __tmp;
  855. }
  856. basic_address_iterator&
  857. operator--() noexcept; // TODO
  858. basic_address_iterator
  859. operator--(int) noexcept
  860. {
  861. auto __tmp = *this;
  862. --*this;
  863. return __tmp;
  864. }
  865. bool
  866. operator==(const basic_address_iterator& __rhs) const noexcept
  867. { return _M_address == __rhs._M_address; }
  868. bool
  869. operator!=(const basic_address_iterator& __rhs) const noexcept
  870. { return _M_address != __rhs._M_address; }
  871. private:
  872. address_v6 _M_address;
  873. };
  874. using address_v6_iterator = basic_address_iterator<address_v6>;
  875. template<typename> class basic_address_range; // not defined
  876. /** An IPv6 address range.
  877. * @{
  878. */
  879. template<> class basic_address_range<address_v4>
  880. {
  881. public:
  882. // types:
  883. using iterator = basic_address_iterator<address_v4>;
  884. // constructors:
  885. basic_address_range() noexcept : _M_begin({}), _M_end({}) { }
  886. basic_address_range(const address_v4& __first,
  887. const address_v4& __last) noexcept
  888. : _M_begin(__first), _M_end(__last) { }
  889. // members:
  890. iterator begin() const noexcept { return _M_begin; }
  891. iterator end() const noexcept { return _M_end; }
  892. _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
  893. size_t
  894. size() const noexcept { return _M_end->to_uint() - _M_begin->to_uint(); }
  895. iterator
  896. find(const address_v4& __addr) const noexcept
  897. {
  898. if (*_M_begin <= __addr && __addr < *_M_end)
  899. return iterator{__addr};
  900. return end();
  901. }
  902. private:
  903. iterator _M_begin;
  904. iterator _M_end;
  905. };
  906. using address_v4_range = basic_address_range<address_v4>;
  907. /// @}
  908. /** An IPv6 address range.
  909. * @{
  910. */
  911. template<> class basic_address_range<address_v6>
  912. {
  913. public:
  914. // types:
  915. using iterator = basic_address_iterator<address_v6>;
  916. // constructors:
  917. basic_address_range() noexcept : _M_begin({}), _M_end({}) { }
  918. basic_address_range(const address_v6& __first,
  919. const address_v6& __last) noexcept
  920. : _M_begin(__first), _M_end(__last) { }
  921. // members:
  922. iterator begin() const noexcept { return _M_begin; }
  923. iterator end() const noexcept { return _M_end; }
  924. _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_begin == _M_end; }
  925. iterator
  926. find(const address_v6& __addr) const noexcept
  927. {
  928. if (*_M_begin <= __addr && __addr < *_M_end)
  929. return iterator{__addr};
  930. return end();
  931. }
  932. private:
  933. iterator _M_begin;
  934. iterator _M_end;
  935. };
  936. using address_v6_range = basic_address_range<address_v6>;
  937. /// @}
  938. bool
  939. operator==(const network_v4& __a, const network_v4& __b) noexcept;
  940. bool
  941. operator==(const network_v6& __a, const network_v6& __b) noexcept;
  942. /// An IPv4 network address.
  943. class network_v4
  944. {
  945. public:
  946. // constructors:
  947. constexpr network_v4() noexcept : _M_addr(), _M_prefix_len(0) { }
  948. constexpr
  949. network_v4(const address_v4& __addr, int __prefix_len)
  950. : _M_addr(__addr), _M_prefix_len(__prefix_len)
  951. {
  952. if (_M_prefix_len < 0 || _M_prefix_len > 32)
  953. __throw_out_of_range("network_v4: invalid prefix length");
  954. }
  955. constexpr
  956. network_v4(const address_v4& __addr, const address_v4& __mask)
  957. : _M_addr(__addr), _M_prefix_len(__builtin_popcount(__mask.to_uint()))
  958. {
  959. if (_M_prefix_len != 0)
  960. {
  961. address_v4::uint_type __mask_uint = __mask.to_uint();
  962. if (__builtin_ctz(__mask_uint) != (32 - _M_prefix_len))
  963. __throw_invalid_argument("network_v4: invalid mask");
  964. if ((__mask_uint & 0x80000000) == 0)
  965. __throw_invalid_argument("network_v4: invalid mask");
  966. }
  967. }
  968. // members:
  969. constexpr address_v4 address() const noexcept { return _M_addr; }
  970. constexpr int prefix_length() const noexcept { return _M_prefix_len; }
  971. constexpr address_v4
  972. netmask() const noexcept
  973. {
  974. address_v4::uint_type __val = address_v4::broadcast().to_uint();
  975. __val >>= (32 - _M_prefix_len);
  976. __val <<= (32 - _M_prefix_len);
  977. return address_v4{__val};
  978. }
  979. constexpr address_v4
  980. network() const noexcept
  981. { return address_v4{_M_addr.to_uint() & netmask().to_uint()}; }
  982. constexpr address_v4
  983. broadcast() const noexcept
  984. { return address_v4{_M_addr.to_uint() | ~netmask().to_uint()}; }
  985. address_v4_range
  986. hosts() const noexcept
  987. {
  988. if (is_host())
  989. return { address(), *++address_v4_iterator(address()) };
  990. return { network(), broadcast() };
  991. }
  992. constexpr network_v4
  993. canonical() const noexcept
  994. { return network_v4(network(), prefix_length()); }
  995. constexpr bool is_host() const noexcept { return _M_prefix_len == 32; }
  996. constexpr bool
  997. is_subnet_of(const network_v4& __other) const noexcept
  998. {
  999. if (__other.prefix_length() < prefix_length())
  1000. {
  1001. network_v4 __net(address(), __other.prefix_length());
  1002. return __net.canonical() == __other.canonical();
  1003. }
  1004. return false;
  1005. }
  1006. template<typename _Allocator = allocator<char>>
  1007. __string_with<_Allocator>
  1008. to_string(const _Allocator& __a = _Allocator()) const
  1009. {
  1010. return address().to_string(__a) + '/'
  1011. + std::to_string(prefix_length());
  1012. }
  1013. private:
  1014. address_v4 _M_addr;
  1015. int _M_prefix_len;
  1016. };
  1017. /// An IPv6 network address.
  1018. class network_v6
  1019. {
  1020. public:
  1021. // constructors:
  1022. constexpr network_v6() noexcept : _M_addr(), _M_prefix_len(0) { }
  1023. constexpr
  1024. network_v6(const address_v6& __addr, int __prefix_len)
  1025. : _M_addr(__addr), _M_prefix_len(__prefix_len)
  1026. {
  1027. if (_M_prefix_len < 0 || _M_prefix_len > 128)
  1028. __throw_out_of_range("network_v6: invalid prefix length");
  1029. }
  1030. // members:
  1031. constexpr address_v6 address() const noexcept { return _M_addr; }
  1032. constexpr int prefix_length() const noexcept { return _M_prefix_len; }
  1033. constexpr address_v6 network() const noexcept; // TODO
  1034. address_v6_range
  1035. hosts() const noexcept
  1036. {
  1037. if (is_host())
  1038. return { address(), *++address_v6_iterator(address()) };
  1039. return {}; // { network(), XXX broadcast() XXX }; // TODO
  1040. }
  1041. constexpr network_v6
  1042. canonical() const noexcept
  1043. { return network_v6{network(), prefix_length()}; }
  1044. constexpr bool is_host() const noexcept { return _M_prefix_len == 128; }
  1045. constexpr bool
  1046. is_subnet_of(const network_v6& __other) const noexcept
  1047. {
  1048. if (__other.prefix_length() < prefix_length())
  1049. {
  1050. network_v6 __net(address(), __other.prefix_length());
  1051. return __net.canonical() == __other.canonical();
  1052. }
  1053. return false;
  1054. }
  1055. template<typename _Allocator = allocator<char>>
  1056. __string_with<_Allocator>
  1057. to_string(const _Allocator& __a = _Allocator()) const
  1058. {
  1059. return address().to_string(__a) + '/'
  1060. + std::to_string(prefix_length());
  1061. }
  1062. private:
  1063. address_v6 _M_addr;
  1064. int _M_prefix_len;
  1065. };
  1066. /** ip::network_v4 comparisons
  1067. * @{
  1068. */
  1069. inline bool
  1070. operator==(const network_v4& __a, const network_v4& __b) noexcept
  1071. {
  1072. return __a.address() == __b.address()
  1073. && __a.prefix_length() == __b.prefix_length();
  1074. }
  1075. inline bool
  1076. operator!=(const network_v4& __a, const network_v4& __b) noexcept
  1077. { return !(__a == __b); }
  1078. /// @}
  1079. /** ip::network_v6 comparisons
  1080. * @{
  1081. */
  1082. inline bool
  1083. operator==(const network_v6& __a, const network_v6& __b) noexcept
  1084. {
  1085. return __a.address() == __b.address()
  1086. && __a.prefix_length() == __b.prefix_length();
  1087. }
  1088. inline bool
  1089. operator!=(const network_v6& __a, const network_v6& __b) noexcept
  1090. { return !(__a == __b); }
  1091. /// @}
  1092. /** ip::network_v4 creation
  1093. * @{
  1094. */
  1095. inline network_v4
  1096. make_network_v4(const address_v4& __a, int __prefix_len)
  1097. { return network_v4{__a, __prefix_len}; }
  1098. network_v4
  1099. make_network_v4(const address_v4& __a, const address_v4& __mask)
  1100. { return network_v4{ __a, __mask }; }
  1101. network_v4 make_network_v4(const char*, error_code&) noexcept; // TODO
  1102. inline network_v4
  1103. make_network_v4(const char* __str)
  1104. { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
  1105. network_v4 make_network_v4(const string&, error_code&) noexcept; // TODO
  1106. inline network_v4
  1107. make_network_v4(const string& __str)
  1108. { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
  1109. network_v4 make_network_v4(string_view, error_code&) noexcept; // TODO
  1110. inline network_v4
  1111. make_network_v4(string_view __str)
  1112. { return make_network_v4(__str, __throw_on_error{"make_network_v4"}); }
  1113. /// @}
  1114. /** ip::network_v6 creation
  1115. * @{
  1116. */
  1117. inline network_v6
  1118. make_network_v6(const address_v6& __a, int __prefix_len)
  1119. { return network_v6{__a, __prefix_len}; }
  1120. network_v6 make_network_v6(const char*, error_code&) noexcept; // TODO
  1121. inline network_v6
  1122. make_network_v6(const char* __str)
  1123. { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
  1124. network_v6 make_network_v6(const string&, error_code&) noexcept; // TODO
  1125. inline network_v6
  1126. make_network_v6(const string& __str)
  1127. { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
  1128. network_v6 make_network_v6(string_view, error_code&) noexcept; // TODO
  1129. inline network_v6
  1130. make_network_v6(string_view __str)
  1131. { return make_network_v6(__str, __throw_on_error{"make_network_v6"}); }
  1132. /// @}
  1133. /// ip::network_v4 I/O
  1134. template<typename _CharT, typename _Traits>
  1135. inline basic_ostream<_CharT, _Traits>&
  1136. operator<<(basic_ostream<_CharT, _Traits>& __os, const network_v4& __net)
  1137. { return __os << __net.to_string(); }
  1138. /// ip::network_v6 I/O
  1139. template<typename _CharT, typename _Traits>
  1140. inline basic_ostream<_CharT, _Traits>&
  1141. operator<<(basic_ostream<_CharT, _Traits>& __os, const network_v6& __net)
  1142. { return __os << __net.to_string(); }
  1143. /// An IP endpoint.
  1144. template<typename _InternetProtocol>
  1145. class basic_endpoint
  1146. {
  1147. public:
  1148. // types:
  1149. using protocol_type = _InternetProtocol;
  1150. // constructors:
  1151. constexpr
  1152. basic_endpoint() noexcept : _M_data()
  1153. { _M_data._M_v4.sin_family = protocol_type::v4().family(); }
  1154. constexpr
  1155. basic_endpoint(const protocol_type& __proto,
  1156. port_type __port_num) noexcept
  1157. : _M_data()
  1158. {
  1159. __glibcxx_assert(__proto == protocol_type::v4()
  1160. || __proto == protocol_type::v6());
  1161. _M_data._M_v4.sin_family = __proto.family();
  1162. _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
  1163. }
  1164. constexpr
  1165. basic_endpoint(const ip::address& __addr,
  1166. port_type __port_num) noexcept
  1167. : _M_data()
  1168. {
  1169. if (__addr.is_v4())
  1170. {
  1171. _M_data._M_v4.sin_family = protocol_type::v4().family();
  1172. _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num);
  1173. _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
  1174. }
  1175. else
  1176. {
  1177. _M_data._M_v6 = {};
  1178. _M_data._M_v6.sin6_family = protocol_type::v6().family();
  1179. _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num);
  1180. __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
  1181. __addr._M_v6._M_bytes.data(), 16);
  1182. _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id;
  1183. }
  1184. }
  1185. // members:
  1186. constexpr protocol_type protocol() const noexcept
  1187. {
  1188. return _M_data._M_v4.sin_family == AF_INET6
  1189. ? protocol_type::v6() : protocol_type::v4();
  1190. }
  1191. constexpr ip::address
  1192. address() const noexcept
  1193. {
  1194. ip::address __addr;
  1195. if (protocol().family() == AF_INET6)
  1196. {
  1197. __builtin_memcpy(&__addr._M_v6._M_bytes,
  1198. _M_data._M_v6.sin6_addr.s6_addr, 16);
  1199. __addr._M_is_v4 = false;
  1200. }
  1201. else
  1202. {
  1203. __builtin_memcpy(&__addr._M_v4._M_addr,
  1204. &_M_data._M_v4.sin_addr.s_addr, 4);
  1205. }
  1206. return __addr;
  1207. }
  1208. void
  1209. address(const ip::address& __addr) noexcept
  1210. {
  1211. if (__addr.is_v6())
  1212. {
  1213. _M_data._M_v6 = {};
  1214. _M_data._M_v6.sin6_family = protocol_type::v6().family();
  1215. __builtin_memcpy(_M_data._M_v6.sin6_addr.s6_addr,
  1216. __addr._M_v6._M_bytes.data(), 16);
  1217. _M_data._M_v6.sin6_scope_id = __addr._M_v6._M_scope_id;
  1218. }
  1219. else
  1220. {
  1221. _M_data._M_v4.sin_family = protocol_type::v4().family();
  1222. _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr;
  1223. }
  1224. }
  1225. constexpr port_type
  1226. port() const noexcept
  1227. { return address_v4::_S_ntoh_16(_M_data._M_v4.sin_port); }
  1228. void
  1229. port(port_type __port_num) noexcept
  1230. { _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); }
  1231. void* data() noexcept { return &_M_data; }
  1232. const void* data() const noexcept { return &_M_data; }
  1233. constexpr size_t size() const noexcept
  1234. {
  1235. return protocol().family() == AF_INET6
  1236. ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
  1237. }
  1238. void
  1239. resize(size_t __s)
  1240. {
  1241. if ((protocol().family() == AF_INET6 && __s != sizeof(sockaddr_in6))
  1242. || (protocol().family() == AF_INET && __s != sizeof(sockaddr_in)))
  1243. __throw_length_error("net::ip::basic_endpoint::resize");
  1244. }
  1245. constexpr size_t capacity() const noexcept { return sizeof(_M_data); }
  1246. private:
  1247. union
  1248. {
  1249. sockaddr_in _M_v4;
  1250. sockaddr_in6 _M_v6;
  1251. } _M_data;
  1252. };
  1253. /** basic_endpoint comparisons
  1254. * @{
  1255. */
  1256. template<typename _InternetProtocol>
  1257. inline bool
  1258. operator==(const basic_endpoint<_InternetProtocol>& __a,
  1259. const basic_endpoint<_InternetProtocol>& __b)
  1260. { return __a.address() == __b.address() && __a.port() == __b.port(); }
  1261. template<typename _InternetProtocol>
  1262. inline bool
  1263. operator!=(const basic_endpoint<_InternetProtocol>& __a,
  1264. const basic_endpoint<_InternetProtocol>& __b)
  1265. { return !(__a == __b); }
  1266. template<typename _InternetProtocol>
  1267. inline bool
  1268. operator< (const basic_endpoint<_InternetProtocol>& __a,
  1269. const basic_endpoint<_InternetProtocol>& __b)
  1270. {
  1271. return __a.address() < __b.address()
  1272. || (!(__b.address() < __a.address()) && __a.port() < __b.port());
  1273. }
  1274. template<typename _InternetProtocol>
  1275. inline bool
  1276. operator> (const basic_endpoint<_InternetProtocol>& __a,
  1277. const basic_endpoint<_InternetProtocol>& __b)
  1278. { return __b < __a; }
  1279. template<typename _InternetProtocol>
  1280. inline bool
  1281. operator<=(const basic_endpoint<_InternetProtocol>& __a,
  1282. const basic_endpoint<_InternetProtocol>& __b)
  1283. { return !(__b < __a); }
  1284. template<typename _InternetProtocol>
  1285. inline bool
  1286. operator>=(const basic_endpoint<_InternetProtocol>& __a,
  1287. const basic_endpoint<_InternetProtocol>& __b)
  1288. { return !(__a < __b); }
  1289. /// @}
  1290. /// basic_endpoint I/O
  1291. template<typename _CharT, typename _Traits, typename _InternetProtocol>
  1292. inline basic_ostream<_CharT, _Traits>&
  1293. operator<<(basic_ostream<_CharT, _Traits>& __os,
  1294. const basic_endpoint<_InternetProtocol>& __ep)
  1295. {
  1296. basic_ostringstream<_CharT, _Traits> __ss;
  1297. if (__ep.protocol()
  1298. == basic_endpoint<_InternetProtocol>::protocol_type::v6())
  1299. __ss << '[' << __ep.address() << ']';
  1300. else
  1301. __ss << __ep.address();
  1302. __ss << ':' << __ep.port();
  1303. __os << __ss.str();
  1304. return __os;
  1305. }
  1306. /** Type representing a single result of name/address resolution.
  1307. * @{
  1308. */
  1309. template<typename _InternetProtocol>
  1310. class basic_resolver_entry
  1311. {
  1312. public:
  1313. // types:
  1314. using protocol_type = _InternetProtocol;
  1315. using endpoint_type = typename _InternetProtocol::endpoint;
  1316. // constructors:
  1317. basic_resolver_entry() { }
  1318. basic_resolver_entry(const endpoint_type& __ep,
  1319. string_view __h, string_view __s)
  1320. : _M_ep(__ep), _M_host(__h), _M_svc(__s) { }
  1321. // members:
  1322. endpoint_type endpoint() const { return _M_ep; }
  1323. operator endpoint_type() const { return _M_ep; }
  1324. template<typename _Allocator = allocator<char>>
  1325. __string_with<_Allocator>
  1326. host_name(const _Allocator& __a = _Allocator()) const
  1327. { return { _M_host, __a }; }
  1328. template<typename _Allocator = allocator<char>>
  1329. __string_with<_Allocator>
  1330. service_name(const _Allocator& __a = _Allocator()) const
  1331. { return { _M_svc, __a }; }
  1332. private:
  1333. basic_endpoint<_InternetProtocol> _M_ep;
  1334. string _M_host;
  1335. string _M_svc;
  1336. };
  1337. template<typename _InternetProtocol>
  1338. inline bool
  1339. operator==(const basic_resolver_entry<_InternetProtocol>& __a,
  1340. const basic_resolver_entry<_InternetProtocol>& __b)
  1341. {
  1342. return __a.endpoint() == __b.endpoint()
  1343. && __a.host_name() == __b.host_name()
  1344. && __a.service_name() == __b.service_name();
  1345. }
  1346. template<typename _InternetProtocol>
  1347. inline bool
  1348. operator!=(const basic_resolver_entry<_InternetProtocol>& __a,
  1349. const basic_resolver_entry<_InternetProtocol>& __b)
  1350. { return !(__a == __b); }
  1351. /// @}
  1352. /** Base class defining flags for name/address resolution.
  1353. * @{
  1354. */
  1355. class resolver_base
  1356. {
  1357. public:
  1358. enum flags : int
  1359. {
  1360. __flags_passive = AI_PASSIVE,
  1361. __flags_canonical_name = AI_CANONNAME,
  1362. __flags_numeric_host = AI_NUMERICHOST,
  1363. #ifdef AI_NUMERICSERV
  1364. __flags_numeric_service = AI_NUMERICSERV,
  1365. #endif
  1366. __flags_v4_mapped = AI_V4MAPPED,
  1367. __flags_all_matching = AI_ALL,
  1368. __flags_address_configured = AI_ADDRCONFIG
  1369. };
  1370. static constexpr flags passive = __flags_passive;
  1371. static constexpr flags canonical_name = __flags_canonical_name;
  1372. static constexpr flags numeric_host = __flags_numeric_host;
  1373. #ifdef AI_NUMERICSERV
  1374. static constexpr flags numeric_service = __flags_numeric_service;
  1375. #endif
  1376. static constexpr flags v4_mapped = __flags_v4_mapped;
  1377. static constexpr flags all_matching = __flags_all_matching;
  1378. static constexpr flags address_configured = __flags_address_configured;
  1379. protected:
  1380. resolver_base() = default;
  1381. ~resolver_base() = default;
  1382. };
  1383. constexpr resolver_base::flags
  1384. operator&(resolver_base::flags __f1, resolver_base::flags __f2)
  1385. { return resolver_base::flags( int(__f1) & int(__f2) ); }
  1386. constexpr resolver_base::flags
  1387. operator|(resolver_base::flags __f1, resolver_base::flags __f2)
  1388. { return resolver_base::flags( int(__f1) | int(__f2) ); }
  1389. constexpr resolver_base::flags
  1390. operator^(resolver_base::flags __f1, resolver_base::flags __f2)
  1391. { return resolver_base::flags( int(__f1) ^ int(__f2) ); }
  1392. constexpr resolver_base::flags
  1393. operator~(resolver_base::flags __f)
  1394. { return resolver_base::flags( ~int(__f) ); }
  1395. inline resolver_base::flags&
  1396. operator&=(resolver_base::flags& __f1, resolver_base::flags __f2)
  1397. { return __f1 = (__f1 & __f2); }
  1398. inline resolver_base::flags&
  1399. operator|=(resolver_base::flags& __f1, resolver_base::flags __f2)
  1400. { return __f1 = (__f1 | __f2); }
  1401. inline resolver_base::flags&
  1402. operator^=(resolver_base::flags& __f1, resolver_base::flags __f2)
  1403. { return __f1 = (__f1 ^ __f2); }
  1404. // TODO define resolver_base::flags static constants for C++14 mode
  1405. /// @}
  1406. /** Container for results of name/address resolution.
  1407. * @{
  1408. */
  1409. template<typename _InternetProtocol>
  1410. class basic_resolver_results
  1411. {
  1412. public:
  1413. // types:
  1414. using protocol_type = _InternetProtocol;
  1415. using endpoint_type = typename protocol_type::endpoint;
  1416. using value_type = basic_resolver_entry<protocol_type>;
  1417. using const_reference = const value_type&;
  1418. using reference = value_type&;
  1419. using const_iterator = typename forward_list<value_type>::const_iterator;
  1420. using iterator = const_iterator;
  1421. using difference_type = ptrdiff_t;
  1422. using size_type = size_t;
  1423. // construct / copy / destroy:
  1424. basic_resolver_results() = default;
  1425. basic_resolver_results(const basic_resolver_results&) = default;
  1426. basic_resolver_results(basic_resolver_results&&) noexcept = default;
  1427. basic_resolver_results&
  1428. operator=(const basic_resolver_results&) = default;
  1429. basic_resolver_results&
  1430. operator=(basic_resolver_results&&) = default;
  1431. ~basic_resolver_results() = default;
  1432. // size:
  1433. size_type size() const noexcept { return _M_size; }
  1434. size_type max_size() const noexcept { return _M_results.max_size(); }
  1435. _GLIBCXX_NODISCARD bool
  1436. empty() const noexcept { return _M_results.empty(); }
  1437. // element access:
  1438. const_iterator begin() const { return _M_results.begin(); }
  1439. const_iterator end() const { return _M_results.end(); }
  1440. const_iterator cbegin() const { return _M_results.begin(); }
  1441. const_iterator cend() const { return _M_results.end(); }
  1442. // swap:
  1443. void
  1444. swap(basic_resolver_results& __that) noexcept
  1445. { _M_results.swap(__that._M_results); }
  1446. private:
  1447. friend class basic_resolver<protocol_type>;
  1448. basic_resolver_results(string_view, string_view, resolver_base::flags,
  1449. error_code&, protocol_type* = nullptr);
  1450. basic_resolver_results(const endpoint_type&, error_code&);
  1451. forward_list<value_type> _M_results;
  1452. size_t _M_size = 0;
  1453. };
  1454. template<typename _InternetProtocol>
  1455. inline bool
  1456. operator==(const basic_resolver_results<_InternetProtocol>& __a,
  1457. const basic_resolver_results<_InternetProtocol>& __b)
  1458. {
  1459. return __a.size() == __b.size()
  1460. && std::equal(__a.begin(), __a.end(), __b.begin());
  1461. }
  1462. template<typename _InternetProtocol>
  1463. inline bool
  1464. operator!=(const basic_resolver_results<_InternetProtocol>& __a,
  1465. const basic_resolver_results<_InternetProtocol>& __b)
  1466. { return !(__a == __b); }
  1467. /// @}
  1468. /// Perform name/address resolution.
  1469. template<typename _InternetProtocol>
  1470. class basic_resolver : public resolver_base
  1471. {
  1472. public:
  1473. // types:
  1474. using executor_type = io_context::executor_type;
  1475. using protocol_type = _InternetProtocol;
  1476. using endpoint_type = typename _InternetProtocol::endpoint;
  1477. using results_type = basic_resolver_results<_InternetProtocol>;
  1478. // construct / copy / destroy:
  1479. explicit basic_resolver(io_context& __ctx) : _M_ctx(&__ctx) { }
  1480. basic_resolver(const basic_resolver&) = delete;
  1481. basic_resolver(basic_resolver&& __rhs) noexcept
  1482. : _M_ctx(__rhs._M_ctx)
  1483. { } // TODO move state/tasks etc.
  1484. ~basic_resolver() { cancel(); }
  1485. basic_resolver& operator=(const basic_resolver&) = delete;
  1486. basic_resolver& operator=(basic_resolver&& __rhs)
  1487. {
  1488. cancel();
  1489. _M_ctx = __rhs._M_ctx;
  1490. // TODO move state/tasks etc.
  1491. return *this;
  1492. }
  1493. // basic_resolver operations:
  1494. executor_type get_executor() noexcept { return _M_ctx->get_executor(); }
  1495. void cancel() { } // TODO
  1496. results_type
  1497. resolve(string_view __host_name, string_view __service_name)
  1498. {
  1499. return resolve(__host_name, __service_name, resolver_base::flags(),
  1500. __throw_on_error{"basic_resolver::resolve"});
  1501. }
  1502. results_type
  1503. resolve(string_view __host_name, string_view __service_name,
  1504. error_code& __ec)
  1505. {
  1506. return resolve(__host_name, __service_name, resolver_base::flags(),
  1507. __ec);
  1508. }
  1509. results_type
  1510. resolve(string_view __host_name, string_view __service_name, flags __f)
  1511. {
  1512. return resolve(__host_name, __service_name, __f,
  1513. __throw_on_error{"basic_resolver::resolve"});
  1514. }
  1515. results_type
  1516. resolve(string_view __host_name, string_view __service_name, flags __f,
  1517. error_code& __ec)
  1518. { return {__host_name, __service_name, __f, __ec}; }
  1519. template<typename _CompletionToken>
  1520. __deduced_t<_CompletionToken, void(error_code, results_type)>
  1521. async_resolve(string_view __host_name, string_view __service_name,
  1522. _CompletionToken&& __token)
  1523. {
  1524. return async_resolve(__host_name, __service_name,
  1525. resolver_base::flags(),
  1526. forward<_CompletionToken>(__token));
  1527. }
  1528. template<typename _CompletionToken>
  1529. __deduced_t<_CompletionToken, void(error_code, results_type)>
  1530. async_resolve(string_view __host_name, string_view __service_name,
  1531. flags __f, _CompletionToken&& __token); // TODO
  1532. results_type
  1533. resolve(const protocol_type& __protocol,
  1534. string_view __host_name, string_view __service_name)
  1535. {
  1536. return resolve(__protocol, __host_name, __service_name,
  1537. resolver_base::flags(),
  1538. __throw_on_error{"basic_resolver::resolve"});
  1539. }
  1540. results_type
  1541. resolve(const protocol_type& __protocol,
  1542. string_view __host_name, string_view __service_name,
  1543. error_code& __ec)
  1544. {
  1545. return resolve(__protocol, __host_name, __service_name,
  1546. resolver_base::flags(), __ec);
  1547. }
  1548. results_type
  1549. resolve(const protocol_type& __protocol,
  1550. string_view __host_name, string_view __service_name, flags __f)
  1551. {
  1552. return resolve(__protocol, __host_name, __service_name, __f,
  1553. __throw_on_error{"basic_resolver::resolve"});
  1554. }
  1555. results_type
  1556. resolve(const protocol_type& __protocol,
  1557. string_view __host_name, string_view __service_name,
  1558. flags __f, error_code& __ec)
  1559. { return {__host_name, __service_name, __f, __ec, &__protocol}; }
  1560. template<typename _CompletionToken>
  1561. __deduced_t<_CompletionToken, void(error_code, results_type)>
  1562. async_resolve(const protocol_type& __protocol,
  1563. string_view __host_name, string_view __service_name,
  1564. _CompletionToken&& __token)
  1565. {
  1566. return async_resolve(__protocol, __host_name, __service_name,
  1567. resolver_base::flags(),
  1568. forward<_CompletionToken>(__token));
  1569. }
  1570. template<typename _CompletionToken>
  1571. __deduced_t<_CompletionToken, void(error_code, results_type)>
  1572. async_resolve(const protocol_type& __protocol,
  1573. string_view __host_name, string_view __service_name,
  1574. flags __f, _CompletionToken&& __token); // TODO
  1575. results_type
  1576. resolve(const endpoint_type& __ep)
  1577. { return resolve(__ep, __throw_on_error{"basic_resolver::resolve"}); }
  1578. results_type
  1579. resolve(const endpoint_type& __ep, error_code& __ec)
  1580. { return { __ep, __ec }; }
  1581. template<typename _CompletionToken> // TODO
  1582. __deduced_t<_CompletionToken, void(error_code, results_type)>
  1583. async_resolve(const endpoint_type& __ep, _CompletionToken&& __token);
  1584. private:
  1585. io_context* _M_ctx;
  1586. };
  1587. /// Private constructor to synchronously resolve host and service names.
  1588. template<typename _InternetProtocol>
  1589. basic_resolver_results<_InternetProtocol>::
  1590. basic_resolver_results(string_view __host_name, string_view __service_name,
  1591. resolver_base::flags __f, error_code& __ec,
  1592. protocol_type* __protocol)
  1593. {
  1594. #ifdef _GLIBCXX_HAVE_NETDB_H
  1595. string __host;
  1596. const char* __h = __host_name.data()
  1597. ? (__host = __host_name.to_string()).c_str()
  1598. : nullptr;
  1599. string __svc;
  1600. const char* __s = __service_name.data()
  1601. ? (__svc = __service_name.to_string()).c_str()
  1602. : nullptr;
  1603. ::addrinfo __hints{ };
  1604. __hints.ai_flags = static_cast<int>(__f);
  1605. if (__protocol)
  1606. {
  1607. __hints.ai_family = __protocol->family();
  1608. __hints.ai_socktype = __protocol->type();
  1609. __hints.ai_protocol = __protocol->protocol();
  1610. }
  1611. else
  1612. {
  1613. auto __p = endpoint_type{}.protocol();
  1614. __hints.ai_family = AF_UNSPEC;
  1615. __hints.ai_socktype = __p.type();
  1616. __hints.ai_protocol = __p.protocol();
  1617. }
  1618. struct __scoped_addrinfo
  1619. {
  1620. ~__scoped_addrinfo() { if (_M_p) ::freeaddrinfo(_M_p); }
  1621. ::addrinfo* _M_p = nullptr;
  1622. } __sai;
  1623. if (int __err = ::getaddrinfo(__h, __s, &__hints, &__sai._M_p))
  1624. {
  1625. __ec.assign(__err, resolver_category());
  1626. return;
  1627. }
  1628. __ec.clear();
  1629. endpoint_type __ep;
  1630. auto __tail = _M_results.before_begin();
  1631. for (auto __ai = __sai._M_p; __ai != nullptr; __ai = __ai->ai_next)
  1632. {
  1633. if (__ai->ai_family == AF_INET || __ai->ai_family == AF_INET6)
  1634. {
  1635. if (__ai->ai_addrlen <= __ep.capacity())
  1636. __builtin_memcpy(__ep.data(), __ai->ai_addr, __ai->ai_addrlen);
  1637. __ep.resize(__ai->ai_addrlen);
  1638. __tail = _M_results.emplace_after(__tail, __ep, __host, __svc);
  1639. _M_size++;
  1640. }
  1641. }
  1642. #else
  1643. __ec = std::make_error_code(errc::operation_not_supported);
  1644. #endif
  1645. }
  1646. /// Private constructor to synchronously resolve an endpoint.
  1647. template<typename _InternetProtocol>
  1648. basic_resolver_results<_InternetProtocol>::
  1649. basic_resolver_results(const endpoint_type& __ep, error_code& __ec)
  1650. {
  1651. #ifdef _GLIBCXX_HAVE_NETDB_H
  1652. char __host_name[256];
  1653. char __service_name[128];
  1654. int __flags = 0;
  1655. if (__ep.protocol().type() == SOCK_DGRAM)
  1656. __flags |= NI_DGRAM;
  1657. auto __sa = static_cast<const sockaddr*>(__ep.data());
  1658. int __err = ::getnameinfo(__sa, __ep.size(),
  1659. __host_name, sizeof(__host_name),
  1660. __service_name, sizeof(__service_name),
  1661. __flags);
  1662. if (__err)
  1663. {
  1664. __flags |= NI_NUMERICSERV;
  1665. __err = ::getnameinfo(__sa, __ep.size(),
  1666. __host_name, sizeof(__host_name),
  1667. __service_name, sizeof(__service_name),
  1668. __flags);
  1669. }
  1670. if (__err)
  1671. __ec.assign(__err, resolver_category());
  1672. else
  1673. {
  1674. __ec.clear();
  1675. _M_results.emplace_front(__ep, __host_name, __service_name);
  1676. _M_size = 1;
  1677. }
  1678. #else
  1679. __ec = std::make_error_code(errc::operation_not_supported);
  1680. #endif
  1681. }
  1682. /** The name of the local host.
  1683. * @{
  1684. */
  1685. template<typename _Allocator>
  1686. __string_with<_Allocator>
  1687. host_name(const _Allocator& __a, error_code& __ec)
  1688. {
  1689. #ifdef HOST_NAME_MAX
  1690. constexpr size_t __maxlen = HOST_NAME_MAX;
  1691. #else
  1692. constexpr size_t __maxlen = 256;
  1693. #endif
  1694. char __buf[__maxlen + 1];
  1695. if (::gethostname(__buf, __maxlen) == -1)
  1696. __ec.assign(errno, generic_category());
  1697. __buf[__maxlen] = '\0';
  1698. return { __buf, __a };
  1699. }
  1700. template<typename _Allocator>
  1701. inline __string_with<_Allocator>
  1702. host_name(const _Allocator& __a)
  1703. { return host_name(__a, __throw_on_error{"host_name"}); }
  1704. inline string
  1705. host_name(error_code& __ec)
  1706. { return host_name(std::allocator<char>{}, __ec); }
  1707. inline string
  1708. host_name()
  1709. { return host_name(std::allocator<char>{}, __throw_on_error{"host_name"}); }
  1710. /// @}
  1711. /// The TCP byte-stream protocol.
  1712. class tcp
  1713. {
  1714. public:
  1715. // types:
  1716. using endpoint = basic_endpoint<tcp>; ///< A TCP endpoint.
  1717. using resolver = basic_resolver<tcp>; ///< A TCP resolver.
  1718. using socket = basic_stream_socket<tcp>; ///< A TCP socket.
  1719. using acceptor = basic_socket_acceptor<tcp>; ///< A TCP acceptor.
  1720. using iostream = basic_socket_iostream<tcp>; /// A TCP iostream.
  1721. #ifdef _GLIBCXX_HAVE_NETINET_TCP_H
  1722. /// Disable coalescing of small segments (i.e. the Nagle algorithm).
  1723. struct no_delay : __sockopt_crtp<no_delay, bool>
  1724. {
  1725. using __sockopt_crtp::__sockopt_crtp;
  1726. static const int _S_level = IPPROTO_TCP;
  1727. static const int _S_name = TCP_NODELAY;
  1728. };
  1729. #endif
  1730. // static members:
  1731. /// A protocol object representing IPv4 TCP.
  1732. static constexpr tcp v4() noexcept { return tcp(AF_INET); }
  1733. /// A protocol object representing IPv6 TCP.
  1734. static constexpr tcp v6() noexcept { return tcp(AF_INET6); }
  1735. tcp() = delete;
  1736. constexpr int family() const noexcept { return _M_family; }
  1737. constexpr int type() const noexcept { return SOCK_STREAM; }
  1738. constexpr int protocol() const noexcept { return IPPROTO_TCP; }
  1739. private:
  1740. constexpr explicit tcp(int __family) : _M_family(__family) { }
  1741. int _M_family;
  1742. };
  1743. /** tcp comparisons
  1744. * @{
  1745. */
  1746. inline bool
  1747. operator==(const tcp& __a, const tcp& __b)
  1748. { return __a.family() == __b.family(); }
  1749. inline bool
  1750. operator!=(const tcp& __a, const tcp& __b)
  1751. { return !(__a == __b); }
  1752. /// @}
  1753. /// The UDP datagram protocol.
  1754. class udp
  1755. {
  1756. public:
  1757. // types:
  1758. using endpoint = basic_endpoint<udp>;
  1759. using resolver = basic_resolver<udp>;
  1760. using socket = basic_datagram_socket<udp>;
  1761. // static members:
  1762. static constexpr udp v4() noexcept { return udp(AF_INET); }
  1763. static constexpr udp v6() noexcept { return udp(AF_INET6); }
  1764. udp() = delete;
  1765. constexpr int family() const noexcept { return _M_family; }
  1766. constexpr int type() const noexcept { return SOCK_DGRAM; }
  1767. constexpr int protocol() const noexcept { return IPPROTO_UDP; }
  1768. private:
  1769. constexpr explicit udp(int __family) : _M_family(__family) { }
  1770. int _M_family;
  1771. };
  1772. /** udp comparisons
  1773. * @{
  1774. */
  1775. bool
  1776. operator==(const udp& __a, const udp& __b)
  1777. { return __a.family() == __b.family(); }
  1778. inline bool
  1779. operator!=(const udp& __a, const udp& __b)
  1780. { return !(__a == __b); }
  1781. /// @}
  1782. /// Restrict a socket created for an IPv6 protocol to IPv6 only.
  1783. struct v6_only : __sockopt_crtp<v6_only, bool>
  1784. {
  1785. using __sockopt_crtp::__sockopt_crtp;
  1786. static const int _S_level = IPPROTO_IPV6;
  1787. static const int _S_name = IPV6_V6ONLY;
  1788. };
  1789. namespace unicast
  1790. {
  1791. /// Set the default number of hops (TTL) for outbound datagrams.
  1792. struct hops : __sockopt_crtp<hops>
  1793. {
  1794. using __sockopt_crtp::__sockopt_crtp;
  1795. template<typename _Protocol>
  1796. int
  1797. level(const _Protocol& __p) const noexcept
  1798. { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
  1799. template<typename _Protocol>
  1800. int
  1801. name(const _Protocol& __p) const noexcept
  1802. { return __p.family() == AF_INET6 ? IPV6_UNICAST_HOPS : IP_TTL; }
  1803. };
  1804. } // namespace unicast
  1805. namespace multicast
  1806. {
  1807. /// Request that a socket joins a multicast group.
  1808. struct join_group
  1809. {
  1810. explicit
  1811. join_group(const address&);
  1812. explicit
  1813. join_group(const address_v4&, const address_v4& = address_v4::any());
  1814. explicit
  1815. join_group(const address_v6&, unsigned int = 0);
  1816. template<typename _Protocol>
  1817. int
  1818. level(const _Protocol& __p) const noexcept
  1819. { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
  1820. template<typename _Protocol>
  1821. int
  1822. name(const _Protocol& __p) const noexcept
  1823. {
  1824. return __p.family() == AF_INET6
  1825. ? IPV6_JOIN_GROUP : IP_ADD_MEMBERSHIP;
  1826. }
  1827. template<typename _Protocol>
  1828. void*
  1829. data(const _Protocol&) noexcept
  1830. { return std::addressof(_M_value); }
  1831. template<typename _Protocol>
  1832. const void*
  1833. data(const _Protocol&) const noexcept
  1834. { return std::addressof(_M_value); }
  1835. template<typename _Protocol>
  1836. size_t
  1837. size(const _Protocol& __p) const noexcept
  1838. {
  1839. return __p.family() == AF_INET6
  1840. ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
  1841. }
  1842. template<typename _Protocol>
  1843. void
  1844. resize(const _Protocol& __p, size_t __s)
  1845. {
  1846. if (__s != size(__p))
  1847. __throw_length_error("invalid value for socket option resize");
  1848. }
  1849. protected:
  1850. union
  1851. {
  1852. ipv6_mreq _M_v6;
  1853. ip_mreq _M_v4;
  1854. } _M_value;
  1855. };
  1856. /// Request that a socket leaves a multicast group.
  1857. struct leave_group
  1858. {
  1859. explicit
  1860. leave_group(const address&);
  1861. explicit
  1862. leave_group(const address_v4&, const address_v4& = address_v4::any());
  1863. explicit
  1864. leave_group(const address_v6&, unsigned int = 0);
  1865. template<typename _Protocol>
  1866. int
  1867. level(const _Protocol& __p) const noexcept
  1868. { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
  1869. template<typename _Protocol>
  1870. int
  1871. name(const _Protocol& __p) const noexcept
  1872. {
  1873. return __p.family() == AF_INET6
  1874. ? IPV6_LEAVE_GROUP : IP_DROP_MEMBERSHIP;
  1875. }
  1876. template<typename _Protocol>
  1877. void*
  1878. data(const _Protocol&) noexcept
  1879. { return std::addressof(_M_value); }
  1880. template<typename _Protocol>
  1881. const void*
  1882. data(const _Protocol&) const noexcept
  1883. { return std::addressof(_M_value); }
  1884. template<typename _Protocol>
  1885. size_t
  1886. size(const _Protocol& __p) const noexcept
  1887. {
  1888. return __p.family() == AF_INET6
  1889. ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
  1890. }
  1891. template<typename _Protocol>
  1892. void
  1893. resize(const _Protocol& __p, size_t __s)
  1894. {
  1895. if (__s != size(__p))
  1896. __throw_length_error("invalid value for socket option resize");
  1897. }
  1898. protected:
  1899. union
  1900. {
  1901. ipv6_mreq _M_v6;
  1902. ip_mreq _M_v4;
  1903. } _M_value;
  1904. };
  1905. /// Specify the network interface for outgoing multicast datagrams.
  1906. class outbound_interface
  1907. {
  1908. explicit
  1909. outbound_interface(const address_v4&);
  1910. explicit
  1911. outbound_interface(unsigned int);
  1912. template<typename _Protocol>
  1913. int
  1914. level(const _Protocol& __p) const noexcept
  1915. { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
  1916. template<typename _Protocol>
  1917. int
  1918. name(const _Protocol& __p) const noexcept
  1919. {
  1920. return __p.family() == AF_INET6
  1921. ? IPV6_MULTICAST_IF : IP_MULTICAST_IF;
  1922. }
  1923. template<typename _Protocol>
  1924. const void*
  1925. data(const _Protocol&) const noexcept
  1926. { return std::addressof(_M_value); }
  1927. template<typename _Protocol>
  1928. size_t
  1929. size(const _Protocol& __p) const noexcept
  1930. {
  1931. return __p.family() == AF_INET6
  1932. ? sizeof(_M_value._M_v6) : sizeof(_M_value._M_v4);
  1933. }
  1934. protected:
  1935. union {
  1936. unsigned _M_v6;
  1937. in_addr _M_v4;
  1938. } _M_value;
  1939. };
  1940. /// Set the default number of hops (TTL) for outbound datagrams.
  1941. struct hops : __sockopt_crtp<hops>
  1942. {
  1943. using __sockopt_crtp::__sockopt_crtp;
  1944. template<typename _Protocol>
  1945. int
  1946. level(const _Protocol& __p) const noexcept
  1947. { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
  1948. template<typename _Protocol>
  1949. int
  1950. name(const _Protocol& __p) const noexcept
  1951. {
  1952. return __p.family() == AF_INET6
  1953. ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL;
  1954. }
  1955. };
  1956. /// Set whether datagrams are delivered back to the local application.
  1957. struct enable_loopback : __sockopt_crtp<enable_loopback>
  1958. {
  1959. using __sockopt_crtp::__sockopt_crtp;
  1960. template<typename _Protocol>
  1961. int
  1962. level(const _Protocol& __p) const noexcept
  1963. { return __p.family() == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP; }
  1964. template<typename _Protocol>
  1965. int
  1966. name(const _Protocol& __p) const noexcept
  1967. {
  1968. return __p.family() == AF_INET6
  1969. ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP;
  1970. }
  1971. };
  1972. } // namespace multicast
  1973. /// @}
  1974. } // namespace ip
  1975. } // namespace v1
  1976. } // namespace net
  1977. } // namespace experimental
  1978. template<>
  1979. struct is_error_condition_enum<experimental::net::v1::ip::resolver_errc>
  1980. : public true_type {};
  1981. // hash support
  1982. template<typename _Tp> struct hash;
  1983. template<>
  1984. struct hash<experimental::net::v1::ip::address>
  1985. : __hash_base<size_t, experimental::net::v1::ip::address>
  1986. {
  1987. size_t
  1988. operator()(const experimental::net::v1::ip::address& __a) const
  1989. {
  1990. if (__a.is_v4())
  1991. return _Hash_impl::hash(__a.to_v4());
  1992. else
  1993. return _Hash_impl::hash(__a.to_v6());
  1994. }
  1995. };
  1996. template<>
  1997. struct hash<experimental::net::v1::ip::address_v4>
  1998. : __hash_base<size_t, experimental::net::v1::ip::address_v4>
  1999. {
  2000. size_t
  2001. operator()(const experimental::net::v1::ip::address_v4& __a) const
  2002. { return _Hash_impl::hash(__a.to_bytes()); }
  2003. };
  2004. template<> struct hash<experimental::net::v1::ip::address_v6>
  2005. : __hash_base<size_t, experimental::net::v1::ip::address_v6>
  2006. {
  2007. size_t
  2008. operator()(const experimental::net::v1::ip::address_v6& __a) const
  2009. { return _Hash_impl::hash(__a.to_bytes()); }
  2010. };
  2011. _GLIBCXX_END_NAMESPACE_VERSION
  2012. } // namespace std
  2013. #endif // C++14
  2014. #endif // _GLIBCXX_EXPERIMENTAL_INTERNET