MySQL 8.0.40
Source Code Documentation
internet.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_NET_TS_INTERNET_H_
27#define MYSQL_HARNESS_NET_TS_INTERNET_H_
28
29#include <algorithm>
30#include <array>
31#include <bitset>
32#include <cstddef> // ptrdiff_t
33#include <cstring> // memset
34#include <forward_list>
35#include <sstream>
36#include <stdexcept> // length_error
37#include <system_error>
38
39#ifdef _WIN32
40#include <WS2tcpip.h> // inet_ntop
41#include <WinSock2.h>
42#include <Windows.h>
43#else
44#include <arpa/inet.h> // inet_ntop
45#include <netdb.h> // getaddrinfo
46#include <netinet/in.h> // in_addr_t
47#include <netinet/ip6.h> // in6_addr_t
48#include <netinet/tcp.h> // TCP_NODELAY
49#include <sys/ioctl.h> // ioctl
50#endif
51
55#include "mysql/harness/stdx/bit.h" // byteswap
57#include "mysql/harness/stdx/type_traits.h" // endian
58
59namespace net {
60namespace ip {
61
62/**
63 * convert an integer from host-endianness into network endianness.
64 *
65 * constexpr version of htons()/htonl()
66 */
67template <class T>
68constexpr T host_to_network(const T t) noexcept {
69 // no need to swap, host has network byte-order
71
72 return stdx::byteswap(t);
73}
74
75/**
76 * convert an integer from network-endianness into host endianness.
77 *
78 * constexpr version of ntohs()/ntohl()
79 */
80template <class T>
81constexpr T network_to_host(const T t) noexcept {
82 // no need to swap, host has network byte-order
84
85 return stdx::byteswap(t);
86}
87
89
90using port_type = uint_least16_t;
91using scope_id_type = uint_least32_t;
92
94 public:
95 using uint_type = uint_least32_t;
96 struct bytes_type : std::array<unsigned char, 4> {
97 template <class... T>
98 explicit constexpr bytes_type(T... t)
99 : array<unsigned char, 4>{{static_cast<unsigned char>(t)...}} {}
100 };
101
102 // create from int in host-byte-order
103 constexpr address_v4() noexcept : addr_{} {}
104 explicit constexpr address_v4(uint_type val)
105 : addr_{
106 (val >> 24) & 0xff,
107 (val >> 16) & 0xff,
108 (val >> 8) & 0xff,
109 (val >> 0) & 0xff,
110 } {}
111
112 // create from bytes in network-byte-order
113 constexpr address_v4(const bytes_type &b) : addr_{b} {}
114
115 constexpr bool is_unspecified() const noexcept { return to_uint() == 0; }
116 constexpr bool is_loopback() const noexcept {
117 return (to_uint() & 0xff000000) == 0x7f000000;
118 }
119 constexpr bool is_multicast() const noexcept {
120 return (to_uint() & 0xf0000000) == 0xe0000000;
121 }
122
123 template <class Allocator = std::allocator<char>>
124 std::basic_string<char, std::char_traits<char>, Allocator> to_string(
125 const Allocator &a = Allocator()) const {
126 std::basic_string<char, std::char_traits<char>, Allocator> out(a);
127 out.resize(INET_ADDRSTRLEN);
128
129 if (nullptr != ::inet_ntop(AF_INET, &addr_, &out.front(), out.size())) {
130 out.erase(out.find('\0'));
131 } else {
132 // it failed. return empty instead
133 out.resize(0);
134 }
135
136 return out;
137 }
138
139 // network byte order
140 constexpr bytes_type to_bytes() const noexcept { return addr_; }
141
142 // host byte-order
143 constexpr uint_type to_uint() const noexcept {
144 return (addr_[0] << 24) | (addr_[1] << 16) | (addr_[2] << 8) | addr_[3];
145 }
146
147 static constexpr address_v4 any() noexcept { return address_v4{}; }
148 static constexpr address_v4 loopback() noexcept {
149 return address_v4{0x7f000001};
150 }
151 static constexpr address_v4 broadcast() noexcept {
152 return address_v4{0xffffffff};
153 }
154
155 private:
156 bytes_type addr_; // network byte order
157};
158
159// 21.5.5 [internet.address.v4.comparisons]
160constexpr bool operator==(const address_v4 &a, const address_v4 &b) noexcept {
161 return a.to_uint() == b.to_uint();
162}
163constexpr bool operator!=(const address_v4 &a, const address_v4 &b) noexcept {
164 return !(a == b);
165}
166constexpr bool operator<(const address_v4 &a, const address_v4 &b) noexcept {
167 return a.to_uint() < b.to_uint();
168}
169constexpr bool operator>(const address_v4 &a, const address_v4 &b) noexcept {
170 return b < a;
171}
172constexpr bool operator<=(const address_v4 &a, const address_v4 &b) noexcept {
173 return !(b < a);
174}
175constexpr bool operator>=(const address_v4 &a, const address_v4 &b) noexcept {
176 return !(a < b);
177}
178
179/**
180 * IPv6 address with scope_id.
181 */
183 public:
184 struct bytes_type : std::array<unsigned char, 16> {
185 // create an array, from parameters.
186 template <class... T>
187 explicit constexpr bytes_type(T... t)
188 : array<unsigned char, 16>{{static_cast<unsigned char>(t)...}} {}
189 };
190
191 constexpr address_v6() noexcept : bytes_{}, scope_id_{} {}
192 constexpr address_v6(const bytes_type &bytes,
193 scope_id_type scope_id = 0) noexcept
194 : bytes_{bytes}, scope_id_{scope_id} {}
195
196 // address of IPv6 any
197 static constexpr address_v6 any() { return {}; }
198
199 // address of IPv6 loopback
200 static constexpr address_v6 loopback() {
201 return bytes_type{
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
204 };
205 }
206
207 constexpr bool is_unspecified() const noexcept;
208 constexpr bool is_loopback() const noexcept;
209 constexpr bool is_multicast() const noexcept { return bytes_[0] == 0xff; }
210 constexpr bool is_link_local() const noexcept {
211 return (bytes_[0] == 0xfe) && (bytes_[1] & 0xc0) == 0x80;
212 }
213 constexpr bool is_site_local() const noexcept {
214 return (bytes_[0] == 0xfe) && (bytes_[1] & 0xc0) == 0xc0;
215 }
216 constexpr bool is_v4_mapped() const noexcept {
217 return bytes_[0] == 0 && bytes_[1] == 0 && bytes_[2] == 0 &&
218 bytes_[3] == 0 && bytes_[4] == 0 && bytes_[5] == 0 &&
219 bytes_[6] == 0 && bytes_[7] == 0 && bytes_[8] == 0 &&
220 bytes_[9] == 0 && bytes_[10] == 0xff && bytes_[11] == 0xff;
221 }
222 constexpr bool is_multicast_node_local() const noexcept {
223 return is_multicast() && (bytes_[1] & 0x0f) == 0x01;
224 }
225 constexpr bool is_multicast_link_local() const noexcept {
226 return is_multicast() && (bytes_[1] & 0x0f) == 0x02;
227 }
228 constexpr bool is_multicast_site_local() const noexcept {
229 return is_multicast() && (bytes_[1] & 0x0f) == 0x05;
230 }
231 constexpr bool is_multicast_org_local() const noexcept {
232 return is_multicast() && (bytes_[1] & 0x0f) == 0x08;
233 }
234 constexpr bool is_multicast_global() const noexcept {
235 return is_multicast() && (bytes_[1] & 0x0f) == 0x0e;
236 }
237
238 constexpr bytes_type to_bytes() const noexcept { return bytes_; }
239
240 constexpr scope_id_type scope_id() const noexcept { return scope_id_; }
241
242 /**
243 * convert an address_v6 into a string.
244 *
245 * @returns empty string on error.
246 */
247 template <class Allocator = std::allocator<char>>
248 std::basic_string<char, std::char_traits<char>, Allocator> to_string(
249 const Allocator &a = Allocator()) const {
250 std::basic_string<char, std::char_traits<char>, Allocator> out(a);
251 out.resize(INET6_ADDRSTRLEN);
252
253 if (nullptr !=
254 ::inet_ntop(AF_INET6, bytes_.data(), &out.front(), out.size())) {
255 out.erase(out.find('\0')); // we don't want the trailing \0
256 if (scope_id() != 0) {
257 out.append("%");
258 out += std::to_string(scope_id());
259 }
260 } else {
261 // it failed. return empty instead
262 out.resize(0);
263 }
264
265 return out;
266 }
267
268 private:
271};
272
273class bad_address_cast : public std::bad_cast {
274 public:
275 bad_address_cast() noexcept = default;
276};
277
278// 21.6.5 [internet.address.v6.comparisons]
279constexpr bool operator==(const address_v6 &a, const address_v6 &b) noexcept {
280 auto const aa = a.to_bytes();
281 auto const bb = b.to_bytes();
282
283 size_t ndx{0};
284
285 for (; ndx != aa.size() && aa[ndx] == bb[ndx]; ++ndx)
286 ;
287
288 return (ndx == aa.size()) ? a.scope_id() == b.scope_id() : false;
289}
290constexpr bool operator!=(const address_v6 &a, const address_v6 &b) noexcept {
291 return !(a == b);
292}
293constexpr bool operator<(const address_v6 &a, const address_v6 &b) noexcept {
294 auto const aa = a.to_bytes();
295 auto const bb = b.to_bytes();
296
297 size_t ndx{0};
298
299 for (; ndx != aa.size() && aa[ndx] == bb[ndx]; ++ndx)
300 ;
301
302 return (ndx == aa.size()) ? a.scope_id() < b.scope_id() : aa[ndx] < bb[ndx];
303}
304constexpr bool operator>(const address_v6 &a, const address_v6 &b) noexcept {
305 return b < a;
306}
307constexpr bool operator<=(const address_v6 &a, const address_v6 &b) noexcept {
308 return !(b < a);
309}
310constexpr bool operator>=(const address_v6 &a, const address_v6 &b) noexcept {
311 return !(a < b);
312}
313
314constexpr bool address_v6::is_unspecified() const noexcept {
315 return *this == any();
316}
317constexpr bool address_v6::is_loopback() const noexcept {
318 return *this == loopback();
319}
320
321class address {
322 public:
323 constexpr address() noexcept : v4_{}, is_v4_{true} {}
324 constexpr address(const address_v4 &a) noexcept : v4_{a}, is_v4_{true} {}
325 constexpr address(const address_v6 &a) noexcept : v6_{a}, is_v4_{false} {}
326
327 constexpr bool is_v4() const noexcept { return is_v4_; }
328 constexpr bool is_v6() const noexcept { return !is_v4_; }
329
330 /**
331 * get address_v4 part.
332 *
333 * @throws bad_address_cast if !is_v4()
334 */
335 constexpr address_v4 to_v4() const {
336 return is_v4() ? v4_ : throw bad_address_cast();
337 }
338 /**
339 * get address_v6 part.
340 *
341 * @throws bad_address_cast if !is_v6()
342 */
343 constexpr address_v6 to_v6() const {
344 return is_v6() ? v6_ : throw bad_address_cast();
345 }
346 constexpr bool is_unspecified() const noexcept {
347 return is_v4() ? v4_.is_unspecified() : v6_.is_unspecified();
348 }
349 constexpr bool is_loopback() const noexcept {
350 return is_v4() ? v4_.is_loopback() : v6_.is_loopback();
351 }
352 constexpr bool is_multicast() const noexcept {
353 return is_v4() ? v4_.is_multicast() : v6_.is_multicast();
354 }
355
356 /**
357 * convert an address into a string.
358 *
359 * @returns empty string on error.
360 */
361 template <class Allocator = std::allocator<char>>
362 std::basic_string<char, std::char_traits<char>, Allocator> to_string(
363 const Allocator &a = Allocator()) const {
364 return is_v4() ? v4_.to_string(a) : v6_.to_string(a);
365 }
366
367 private:
368 union {
371 };
372
373 bool is_v4_;
374};
375
376/**
377 * make address_v6 from a string.
378 *
379 * @param str address string.
380 *
381 * @returns a address_v6 on success, std::error_code otherwise
382 */
384 const char *str) {
385 address_v6::bytes_type ipv6_addr;
386
387 scope_id_type scope_id{0};
388 int inet_pton_res;
389 // parse the scope_id separately as inet_pton() doesn't know about it.
390 //
391 // only numeric IDs though. For named scope-ids like "lo", getifaddrs() is
392 // needed
393 if (const char *percent = strchr(str, '%')) {
394 const char *after_percent = percent + 1;
395 // empty and numerics with leading -|+ are invalid
396 if (*after_percent == '\0' || *after_percent == '-' ||
397 *after_percent == '+') {
399 make_error_code(std::errc::invalid_argument));
400 }
401
402 char *err{nullptr};
403 scope_id = ::strtoul(after_percent, &err, 10);
404 if (*err != '\0') {
406 make_error_code(std::errc::invalid_argument));
407 }
408
409 std::string before_percent(str, percent);
410
411 inet_pton_res = ::inet_pton(AF_INET6, before_percent.c_str(), &ipv6_addr);
412 } else {
413 inet_pton_res = ::inet_pton(AF_INET6, str, &ipv6_addr);
414 }
415 if (inet_pton_res == 1) {
416 return {std::in_place, ipv6_addr, scope_id};
417 } else if (inet_pton_res == 0) {
418 // parse failed
419 return stdx::make_unexpected(make_error_code(std::errc::invalid_argument));
420 } else {
422 }
423}
424
425/**
426 * make address_v4 from a string.
427 *
428 * @param str address string.
429 *
430 * @returns a address_v4 on success, std::error_code otherwise
431 */
433 const char *str) {
434 address_v4::bytes_type ipv4_addr;
435
436 int res = ::inet_pton(AF_INET, str, &ipv4_addr);
437 if (res == 1) {
438 return {ipv4_addr};
439 } else if (res == 0) {
440 // parse failed
441 return stdx::make_unexpected(make_error_code(std::errc::invalid_argument));
442 } else {
444 }
445}
446
447/**
448 * make address from a c-string.
449 *
450 * @param str address string.
451 *
452 * @returns a address4 on success, std::error_code otherwise
453 */
455 auto v6_res = make_address_v6(str);
456 if (v6_res) {
457 return address{*v6_res};
458 } else {
459 auto v4_res = make_address_v4(str);
460
461 if (v4_res) return address{*v4_res};
462 return stdx::make_unexpected(v4_res.error());
463 }
464}
465
466/**
467 * make address from a string.
468 *
469 * @param str address string.
470 *
471 * @returns a address4 on success, std::error_code otherwise
472 */
474 const std::string &str) {
475 return make_address(str.c_str());
476}
477
478template <class CharT, class Traits>
479std::basic_ostream<CharT, Traits> &operator<<(
480 std::basic_ostream<CharT, Traits> &os, const address &addr) {
481 os << addr.to_string().c_str();
482
483 return os;
484}
485
486constexpr bool operator==(const address &a, const address &b) noexcept {
487 if (a.is_v4() && b.is_v4()) return a.to_v4() == b.to_v4();
488 if (a.is_v6() && b.is_v6()) return a.to_v6() == b.to_v6();
489
490 return false;
491}
492
493constexpr bool operator!=(const address &a, const address &b) noexcept {
494 return !(a == b);
495}
496
497constexpr bool operator<(const address &a, const address &b) noexcept {
498 // v4 is "smaller" than v6
499 if (a.is_v4() && !b.is_v4()) return true;
500 if (!a.is_v4() && b.is_v4()) return false;
501
502 // both are of the same type
503 if (a.is_v4()) return a.to_v4() < b.to_v4();
504 return a.to_v6() < b.to_v6();
505}
506
507/**
508 * endpoint of IPv4/IPv6 based connection.
509 *
510 *
511 */
512template <typename InternetProtocol>
514 public:
515 using protocol_type = InternetProtocol;
516 using endpoint_type = typename protocol_type::endpoint;
517
519
520 basic_resolver_entry(const endpoint_type &ep, std::string host_name,
521 std::string service_name)
522 : ep_{ep},
523 host_name_{std::move(host_name)},
524 service_name_{std::move(service_name)} {}
525
526 endpoint_type endpoint() const { return ep_; }
527
528 std::string host_name() const { return host_name_; }
529 std::string service_name() const { return service_name_; }
530
531 private:
533 std::string host_name_;
534 std::string service_name_;
535};
536
537// forward-decl for the the friendship
538template <typename InternetProtocol>
539class basic_resolver;
540
541template <typename InternetProtocol>
543 public:
544 using protocol_type = InternetProtocol;
545 using endpoint_type = typename protocol_type::endpoint;
549 using const_iterator = typename std::forward_list<value_type>::const_iterator;
551 using difference_type = ptrdiff_t;
552 using size_type = size_t;
553
555
556 size_type size() const noexcept { return size_; }
557 size_type max_size() const noexcept { return results_.max_size(); }
558 bool empty() const noexcept { return results_.empty(); }
559
560 const_iterator begin() const { return results_.begin(); }
561 const_iterator end() const { return results_.end(); }
562 const_iterator cbegin() const { return results_.cbegin(); }
563 const_iterator cend() const { return results_.cend(); }
564
565 private:
566 friend class basic_resolver<protocol_type>;
567
568 basic_resolver_results(std::unique_ptr<addrinfo, void (*)(addrinfo *)> ainfo,
569 const std::string &host_name,
570 const std::string &service_name) {
571 endpoint_type ep;
572
573 auto tail = results_.before_begin();
574 for (const auto *cur = ainfo.get(); cur != nullptr; cur = cur->ai_next) {
575 std::memcpy(ep.data(), cur->ai_addr, cur->ai_addrlen);
576
577 tail = results_.emplace_after(tail, ep, host_name, service_name);
578 ++size_;
579 }
580 }
581
582 basic_resolver_results(const endpoint_type &ep, const std::string &host_name,
583 const std::string &service_name) {
584 auto tail = results_.before_begin();
585
586 tail = results_.emplace_after(tail, ep, host_name, service_name);
587 ++size_;
588 }
589
590 std::forward_list<value_type> results_;
591 size_t size_{0};
592};
593
595 public:
596 using flags = std::bitset<32>;
597
598 static constexpr flags passive = AI_PASSIVE;
599 static constexpr flags canonical_name = AI_CANONNAME;
600 static constexpr flags numeric_host = AI_NUMERICHOST;
601 static constexpr flags numeric_service = AI_NUMERICSERV;
602 static constexpr flags v4_mapped = AI_V4MAPPED;
603 static constexpr flags all_matching = AI_ALL;
604 static constexpr flags address_configured = AI_ADDRCONFIG;
605};
606
607template <typename InternetProtocol>
609 public:
611 using protocol_type = InternetProtocol;
612 using endpoint_type = typename InternetProtocol::endpoint;
615
616 explicit basic_resolver(io_context &io_ctx) : io_ctx_{io_ctx} {}
617
619 const std::string &host_name, const std::string &service_name, flags f) {
620 const auto proto = endpoint_type{}.protocol();
621
622 ::addrinfo hints{};
623 hints.ai_family = AF_UNSPEC;
624 hints.ai_socktype = proto.type();
625 hints.ai_protocol = proto.protocol();
626
627 // posix ulong, windows int
628 hints.ai_flags = static_cast<decltype(hints.ai_flags)>(f.to_ulong());
629
630 auto res = io_ctx_.socket_service()->getaddrinfo(
631 host_name.empty() ? nullptr : host_name.c_str(),
632 service_name.empty() ? nullptr : service_name.c_str(), &hints);
633
634 if (!res) return stdx::make_unexpected(res.error());
635
636 return results_type{std::move(res.value()), host_name, service_name};
637 }
639 const std::string &host_name, const std::string &service_name) {
640 return resolve(host_name, service_name, flags{});
641 }
642
644 std::array<char, NI_MAXHOST> host_name;
645 std::array<char, NI_MAXSERV> service_name;
646
647 int flags{0};
648
649 if (endpoint_type().protocol().type() == SOCK_DGRAM) {
650 flags |= NI_DGRAM;
651 }
652
653 const auto nameinfo_res = net::impl::resolver::getnameinfo(
654 static_cast<const struct sockaddr *>(ep.data()), ep.size(),
655 host_name.data(), host_name.size(), service_name.data(),
656 service_name.size(), flags);
657 if (!nameinfo_res) {
658 return stdx::make_unexpected(nameinfo_res.error());
659 }
660
661 // find \0 char in array. If \0 isn't found, end of array.
662 const auto name_end = std::find(host_name.begin(), host_name.end(), '\0');
663 const auto serv_end =
664 std::find(service_name.begin(), service_name.end(), '\0');
665
666 return results_type{
667 ep,
668 std::string{host_name.begin(), name_end},
669 std::string{service_name.begin(), serv_end},
670 };
671 }
672
673 private:
675};
676
677template <typename InternetProtocol>
679 public:
680 using protocol_type = InternetProtocol;
681 using size_type = size_t;
682
683 /**
684 * default constructor.
685 *
686 * protocol() is v4()
687 */
688 constexpr basic_endpoint() : data_{} {
689 data_.v4.sin_family = protocol_type::v4().family();
690 }
691
692 /**
693 * construct from protocol and port-number.
694 *
695 * basic_endpoint(v4(), 80).
696 */
697 constexpr basic_endpoint(const protocol_type &proto,
698 port_type port_num) noexcept
699 : data_{} {
700 // win32 has short for .sin_family, unix has int
701 data_.v4.sin_family = decltype(data_.v4.sin_family)(proto.family());
702 data_.v4.sin_port = host_to_network(port_num);
703 }
704
705 /**
706 * construct from address and port-number.
707 */
708 constexpr basic_endpoint(const ip::address &addr,
709 port_type port_num) noexcept {
710 if (addr.is_v4()) {
711 data_.v4.sin_family = protocol_type::v4().family();
712 data_.v4.sin_port = host_to_network(port_num);
713 {
714 auto addr_b = addr.to_v4().to_bytes();
715
716 // cast, as .s_addr is an int and incrementing it would step way too far
717 std::copy(addr_b.begin(), addr_b.end(),
718 reinterpret_cast<unsigned char *>(&data_.v4.sin_addr.s_addr));
719 }
720 } else {
721 data_.v6.sin6_family = protocol_type::v6().family();
722 data_.v6.sin6_port = host_to_network(port_num);
723 {
724 auto addr_b = addr.to_v6().to_bytes();
725 std::copy(addr_b.begin(), addr_b.end(), data_.v6.sin6_addr.s6_addr);
726 }
727 data_.v6.sin6_scope_id = addr.to_v6().scope_id();
728 }
729 }
730
731 /**
732 * get protocol of the endpoint.
733 */
734 constexpr protocol_type protocol() const noexcept {
735 return data_.v4.sin_family == AF_INET ? protocol_type::v4()
736 : protocol_type::v6();
737 }
738
739 /**
740 * get address of the endpoint.
741 */
742 constexpr ip::address address() const noexcept {
743 if (protocol().family() == protocol_type::v4().family())
744 return address_v4(network_to_host(data_.v4.sin_addr.s_addr));
745
747
748 std::copy(data_.v6.sin6_addr.s6_addr, data_.v6.sin6_addr.s6_addr + 16,
749 v6b.begin());
750 return address_v6(v6b);
751 }
752
753 /**
754 * get port of the endpoint.
755 */
756 constexpr port_type port() const noexcept {
757 return data_.v4.sin_family == AF_INET ? network_to_host(data_.v4.sin_port)
758 : network_to_host(data_.v6.sin6_port);
759 }
760
761 /**
762 * const pointer to the underlying sockaddr.
763 */
764 const void *data() const noexcept { return &data_; }
765
766 /**
767 * pointer to the underlying sockaddr.
768 */
769 void *data() noexcept { return &data_; }
770
771 /**
772 * size of the underlying sockaddr.
773 */
774 constexpr size_type size() const noexcept {
775 return data_.v4.sin_family == AF_INET ? sizeof(sockaddr_in)
776 : sizeof(sockaddr_in6);
777 }
778
779 /**
780 * get capacity of the underlying sockaddr.
781 */
782 constexpr size_t capacity() const noexcept { return sizeof(data_); }
783
784 /**
785 * set the size of valid data of the underlying sockaddr.
786 *
787 * ~~~{.cpp}
788 * basic_endpoint<tcp> ep;
789 *
790 * if (addrlen < ep.capacity()) {
791 * memcpy(ep.data(), addr, addrlen);
792 * ep.resize(addrlen);
793 * }
794 * ~~~
795 *
796 * @param n new size
797 *
798 * @throws std::length_error if n > capacity
799 */
801 if (n > capacity()) throw std::length_error("n > capacity()");
802 }
803
804 private:
805 union {
806 sockaddr_in v4;
807 sockaddr_in6 v6;
808 } data_;
809};
810
811template <class CharT, class Traits, class InternetProtocol>
812std::basic_ostream<CharT, Traits> &operator<<(
813 std::basic_ostream<CharT, Traits> &os,
814 const basic_endpoint<InternetProtocol> &ep) {
815 std::basic_ostringstream<CharT, Traits> ss;
816 if (ep.protocol() == basic_endpoint<InternetProtocol>::protocol_type::v6()) {
817 ss << "[" << ep.address() << "]";
818 } else {
819 ss << ep.address();
820 }
821 ss << ":" << ep.port();
822
823 os << ss.str();
824
825 return os;
826}
827
828// 21.13.3 basic_endpoint comparison
829
830template <class InternetProtocol>
832 const basic_endpoint<InternetProtocol> &b) noexcept {
833 return a.port() == b.port() && a.address() == b.address();
834}
835
836template <class InternetProtocol>
838 const basic_endpoint<InternetProtocol> &b) noexcept {
839 return !(a == b);
840}
841
842// 21.9 [internet.address.iter]
843template <class Address>
845
846template <>
848 public:
850 using difference_type = ptrdiff_t;
851 using pointer = const value_type *;
852 using reference = const value_type &;
853 using iterator_category = std::input_iterator_tag;
854
855 basic_address_iterator(const value_type &a) noexcept : addr_{a} {}
856
857 reference operator*() const noexcept { return addr_; }
858 pointer operator->() const noexcept { return std::addressof(addr_); }
860 // increment
861 addr_ = value_type{addr_.to_uint() + 1};
862 return *this;
863 }
865 auto tmp = *this;
866 ++*this;
867 return tmp;
868 }
870 // increment
871 addr_ = value_type{addr_.to_uint() - 1};
872 return *this;
873 }
875 auto tmp = *this;
876 --*this;
877 return tmp;
878 }
879
880 bool operator==(const basic_address_iterator &rhs) const noexcept {
881 return addr_ == rhs.addr_;
882 }
883
884 bool operator!=(const basic_address_iterator &rhs) const noexcept {
885 return addr_ != rhs.addr_;
886 }
887
888 private:
890};
891
892template <>
894 public:
896 using difference_type = ptrdiff_t;
897 using pointer = const value_type *;
898 using reference = const value_type &;
899 using iterator_category = std::input_iterator_tag;
900
901 basic_address_iterator(const value_type &a) noexcept : addr_{a} {}
902
903 reference operator*() const noexcept { return addr_; }
904 pointer operator->() const noexcept { return std::addressof(addr_); }
906 basic_address_iterator operator++(int) noexcept {
907 auto tmp = *this;
908 ++*this;
909 return tmp;
910 }
912 // increment the bytes
913
914 basic_address_iterator operator--(int) noexcept {
915 auto tmp = *this;
916 --*this;
917 return tmp;
918 }
919
920 private:
922};
923
926
927// 21.10 [internet.address.range]
928//
929template <class Address>
931
934
935template <>
937 public:
940
941 basic_address_range() noexcept : begin_({}), end_({}) {}
942 basic_address_range(const value_type &first, const value_type &last) noexcept
943 : begin_{first}, end_{last} {}
944
945 iterator begin() const noexcept { return begin_; }
946 iterator end() const noexcept { return end_; }
947
948 bool empty() const noexcept { return begin_ == end_; }
949 size_t size() const noexcept { return end_->to_uint() - begin_->to_uint(); }
950
951 iterator find(const value_type &addr) const noexcept {
952 if (*begin_ <= addr && addr < *end_) return iterator{addr};
953
954 return end();
955 }
956
957 private:
960};
961
962template <>
964 public:
966};
967
968// 21.11 [internet.network.v4]
969
971 public:
972 constexpr network_v4() noexcept = default;
973 constexpr network_v4(const address_v4 &addr, int prefix_len)
974 : addr_{addr}, prefix_len_{prefix_len} {}
975 constexpr network_v4(const address_v4 &addr, const address_v4 &mask)
976 : addr_{addr} {
977 auto m = mask.to_uint();
978 uint32_t t{1U << 31U};
979
980 size_t sh{0};
981 for (; sh < 32; ++sh) {
982 if ((m & t) == 0) break;
983
984 t >>= 1U;
985 }
986
987 // TODO(jkneschk): check the remainder is all zero
988
989 prefix_len_ = sh;
990 }
991
992 // 21.11.2, mmembers
993 constexpr address_v4 address() const noexcept { return addr_; }
994 constexpr int prefix_length() const noexcept { return prefix_len_; }
995 constexpr address_v4 netmask() const noexcept {
996 address_v4::uint_type v{0xffffffff};
997
998 v <<= 32U - prefix_len_;
999
1000 return address_v4{v};
1001 }
1002 constexpr address_v4 network() const noexcept {
1003 return address_v4{address().to_uint() & netmask().to_uint()};
1004 }
1005 constexpr address_v4 broadcast() const noexcept {
1006 auto mask = netmask().to_uint();
1007 address_v4::uint_type v{0xffffffff};
1008 return address_v4{(address().to_uint() & mask) | (~(v & mask) & v)};
1009 }
1010 address_v4_range hosts() const noexcept;
1011 constexpr network_v4 canonical() const noexcept {
1012 return {network(), prefix_length()};
1013 }
1014 constexpr bool is_host() const noexcept { return prefix_length() == 32; }
1015 constexpr bool is_subnet_of(const network_v4 &other) const noexcept;
1016
1017 template <class Allocator = std::allocator<char>>
1018 std::basic_string<char, std::char_traits<char>, Allocator> to_string(
1019 const Allocator &a = Allocator()) const {
1020 return this->address().to_string(a) + "/" + std::to_string(prefix_length());
1021 }
1022
1023 private:
1024 address_v4 addr_{};
1025 int prefix_len_{0};
1026};
1027
1028// 21.11.3 network_v4 comp
1029constexpr bool operator==(const network_v4 &a, const network_v4 &b) noexcept {
1030 return a.address() == b.address() && a.prefix_length() == b.prefix_length();
1031}
1032
1034 const network_v4 &other) const noexcept {
1035 return other.prefix_length() < prefix_length() &&
1036 (network_v4(this->address(), other.prefix_length()).canonical() ==
1037 other.canonical());
1038}
1039
1040// 21.11.4 network_v4 creation
1041//
1042// 21.11.5 network_v4 io
1043template <class CharT, class Traits>
1044std::basic_ostream<CharT, Traits> &operator<<(
1045 std::basic_ostream<CharT, Traits> &os, const network_v4 &net) {
1046 os << net.to_string().c_str();
1047
1048 return os;
1049}
1050
1051// 21.12 [internet.network.v6]
1052
1054 public:
1055 constexpr network_v6() noexcept = default;
1056 constexpr network_v6(const address_v6 &addr, int prefix_len)
1057 : addr_{addr}, prefix_len_{prefix_len} {}
1058
1059 // 21.11.2, mmembers
1060 constexpr address_v6 address() const noexcept { return addr_; }
1061 constexpr int prefix_length() const noexcept { return prefix_len_; }
1062 constexpr address_v6 network() const noexcept {
1063 const auto address_bytes = address().to_bytes();
1064 address_v6::bytes_type netmask_bytes{
1065 networkbits(address_bytes, prefix_len_, 0),
1066 networkbits(address_bytes, prefix_len_, 1),
1067 networkbits(address_bytes, prefix_len_, 2),
1068 networkbits(address_bytes, prefix_len_, 3),
1069 networkbits(address_bytes, prefix_len_, 4),
1070 networkbits(address_bytes, prefix_len_, 5),
1071 networkbits(address_bytes, prefix_len_, 6),
1072 networkbits(address_bytes, prefix_len_, 7),
1073 networkbits(address_bytes, prefix_len_, 8),
1074 networkbits(address_bytes, prefix_len_, 9),
1075 networkbits(address_bytes, prefix_len_, 10),
1076 networkbits(address_bytes, prefix_len_, 11),
1077 networkbits(address_bytes, prefix_len_, 12),
1078 networkbits(address_bytes, prefix_len_, 13),
1079 networkbits(address_bytes, prefix_len_, 14),
1080 networkbits(address_bytes, prefix_len_, 15),
1081 };
1082
1083 return address_v6{netmask_bytes};
1084 }
1085 address_v6_range hosts() const noexcept;
1086 constexpr network_v6 canonical() const noexcept {
1087 return {network(), prefix_length()};
1088 }
1089 constexpr bool is_host() const noexcept { return prefix_length() == 128; }
1090 constexpr bool is_subnet_of(const network_v6 &other) const noexcept;
1091
1092 template <class Allocator = std::allocator<char>>
1093 std::basic_string<char, std::char_traits<char>, Allocator> to_string(
1094 const Allocator &a = Allocator()) const {
1095 return this->address().to_string(a) + "/" + std::to_string(prefix_length());
1096 }
1097
1098 private:
1099 constexpr unsigned char networkbits(
1100 const address_v6::bytes_type &address_bytes, int prefix_len,
1101 int ndx) const {
1102 return address_bytes[ndx] & leftmostbits(ndx, prefix_len);
1103 }
1104
1105 constexpr uint8_t leftmostbits(int ndx, int prefix_len) const {
1106 int bitndx = ndx * 8;
1107
1108 if (bitndx > prefix_len) return 0;
1109 prefix_len -= bitndx;
1110 if (prefix_len >= 8) return 0xff;
1111
1112 return 0xff << (8 - prefix_len);
1113 }
1114
1115 address_v6 addr_{};
1116 int prefix_len_{0};
1117};
1118
1119// 21.12.3 network_v6 comp
1120constexpr bool operator==(const network_v6 &a, const network_v6 &b) noexcept {
1121 return a.address() == b.address() && a.prefix_length() == b.prefix_length();
1122}
1123
1125 const network_v6 &other) const noexcept {
1126 return other.prefix_length() < prefix_length() &&
1127 (network_v6(this->address(), other.prefix_length()).canonical() ==
1128 other.canonical());
1129}
1130
1131// 21.12.4 network_v6 creation
1132//
1133// 21.12.5 network_v6 io
1134template <class CharT, class Traits>
1135std::basic_ostream<CharT, Traits> &operator<<(
1136 std::basic_ostream<CharT, Traits> &os, const network_v6 &net) {
1137 os << net.to_string().c_str();
1138
1139 return os;
1140}
1141
1142/**
1143 * TCP protocol.
1144 *
1145 * - endpoint
1146 * - socket options
1147 *
1148 * ~~~
1149 * net::ip::tcp::no_delay opt;
1150 * if (0 == setsockopt(sock, opt.level(), opt.name(), opt.data(), opt.size())) {
1151 * opt.resize();
1152 * }
1153 * ~~~
1154 */
1155class tcp {
1156 public:
1161
1162#ifdef TCP_CONGESTION
1163 // linux, freebsd, solaris
1164 // using congestion = socket_option::string<IPPROTO_TCP, TCP_CONGESTION>;
1165#endif
1166#ifdef TCP_CORK
1167 // linux, solaris
1169#endif
1170#ifdef TCP_DEFER_ACCEPT
1171 // linux
1173#endif
1174#ifdef TCP_EXPEDITED_1122
1175 // windows xp
1176 using expedited_rfc1122 =
1178#endif
1179#ifdef TCP_FASTOPEN
1180 // linux, windows 10 build 1607, freebsd 10
1181 // freebsd 12 -> struct tcp_fastopen
1183#endif
1184#ifdef TCP_FASTOPEN_CONNECT
1185 // linux 4.11
1186 using fast_open_connect =
1188#endif
1189#ifdef TCP_INFO
1190 // linux, freebsd, solaris
1191 //
1192 // windows has GetPerTcpConnectionEStats
1193 // using info = socket_option::tcp_info<IPPROTO_TCP, TCP_INFO>;
1194#endif
1195#ifdef TCP_KEEPINIT
1196 // freebsd
1198#endif
1199#ifdef TCP_KEEPCNT
1200 // linux, windows 10 build 1703, freebsd
1202#endif
1203#ifdef TCP_KEEPIDLE
1204 // linux, windows 10 build 1709, freebsd
1206#endif
1207#ifdef TCP_KEEPINTVL
1208 // linux, windows 10 build 1709, freebsd
1210#endif
1211#ifdef TCP_LINGER2
1212 // linux
1214#endif
1215#ifdef TCP_MAXRT
1216 // windows vista
1218#endif
1219#ifdef TCP_MAXSEG
1220 // linux, freebsd, solaris, macosx
1222#endif
1223#ifdef TCP_MD5SIG
1224 // linux, freebsd
1225 // using md5sig = socket_option::md5sig<IPPROTO_TCP, TCP_MD5SIG>;
1226#endif
1227#ifdef TCP_NODELAY
1228 // all
1230#endif
1231#ifdef TCP_NOOPT
1232 // freebsd, macosx
1234#endif
1235#ifdef TCP_NOPUSH
1236 // freebsd, macosx
1238#endif
1239#ifdef TCP_QUICKACK
1240 // linux
1242#endif
1243#ifdef TCP_SYNCNT
1244 // linux
1246#endif
1247#ifdef TCP_USER_TIMEOUT
1248 // linux
1250#endif
1251#ifdef TCP_WINDOW_CLAMP
1252 // linux
1254#endif
1255#ifdef TCP_TIMESTAMPS
1256 // windows vista
1258#endif
1259#ifdef TCP_NOTSENT_LOWAT
1260 // linux, macosx
1262#endif
1263
1264 // ## linux
1265 //
1266 // TCP_CC_INFO - tcp_cc_info
1267 // TCP_COOKIE_TRANSACTIONS - struct tcp_cookie_transactions
1268 // TCP_FASTOPEN_CONNECT
1269 // TCP_MD5SIG_EXT
1270 // TCP_QUEUE_SEQ
1271 // TCP_REPAIR
1272 // TCP_REPAIR_QUEUE
1273 // TCP_REPAIR_OPTIONS - struct tcp_repair_opt
1274 // TCP_REPAIR_WINDOW - struct tcp_repair_window
1275 // TCP_THIN_LINEAR_TIMEOUTS
1276 // TCP_THIN_DUPACK
1277 // TCP_TIMESTAMP
1278 // TCP_SAVE_SYN
1279 // TCP_SAVED_SYN
1280 // TCP_ULP
1281
1282 // ## solaris
1283 //
1284 // TCP_CONN_ABORT_THRESHOLD
1285 // TCP_ABORT_THRESHOLD
1286 // TCP_RTO_MIN
1287 // TCP_RTO_MAX
1288 // TCP_RTO_INITIAL
1289 // TCP_INIT_CWND
1290 //
1291 // ## macosx
1292 //
1293 // TCP_KEEPALIVE
1294 // TCP_CONNECTIONTIMEOUT
1295 // TCP_SENDMOREACKS
1296 // TCP_ENABLE_ENC
1297 // TCP_CONNECTION_INFO
1298
1299 static constexpr tcp v4() noexcept { return tcp{AF_INET}; }
1300 static constexpr tcp v6() noexcept { return tcp{AF_INET6}; }
1301
1302 constexpr int family() const noexcept { return family_; }
1303 constexpr int type() const noexcept { return SOCK_STREAM; }
1304 constexpr int protocol() const noexcept { return IPPROTO_TCP; }
1305
1306 // tcp() = delete;
1307
1308 private:
1309 constexpr explicit tcp(int family) : family_{family} {}
1310
1312};
1313
1314constexpr bool operator==(const tcp &a, const tcp &b) noexcept {
1315 return a.family() == b.family() && a.type() == b.type();
1316}
1317
1318constexpr bool operator!=(const tcp &a, const tcp &b) noexcept {
1319 return !(a == b);
1320}
1321
1322// 21.20 internet.udp
1323//
1324class udp {
1325 public:
1329
1330 static constexpr udp v4() noexcept { return udp{AF_INET}; }
1331 static constexpr udp v6() noexcept { return udp{AF_INET6}; }
1332
1333 constexpr int family() const noexcept { return family_; }
1334 constexpr int type() const noexcept { return SOCK_DGRAM; }
1335 constexpr int protocol() const noexcept { return IPPROTO_UDP; }
1336
1337 // udp() = delete;
1338
1339 private:
1340 constexpr explicit udp(int family) : family_{family} {}
1341
1343};
1344
1345// 21.20.1 internet.udp.comparisons
1346
1347constexpr bool operator==(const udp &a, const udp &b) noexcept {
1348 return a.family() == b.family() && a.type() == b.type();
1349}
1350
1351constexpr bool operator!=(const udp &a, const udp &b) noexcept {
1352 return !(a == b);
1353}
1354
1355} // namespace ip
1356
1357} // namespace net
1358
1359#endif
Definition: socket.h:971
Definition: socket.h:1412
Definition: socket.h:1144
Definition: io_context.h:990
Definition: io_context.h:61
Definition: internet.h:93
bytes_type addr_
Definition: internet.h:156
static constexpr address_v4 any() noexcept
Definition: internet.h:147
constexpr bool is_loopback() const noexcept
Definition: internet.h:116
constexpr address_v4(uint_type val)
Definition: internet.h:104
constexpr uint_type to_uint() const noexcept
Definition: internet.h:143
constexpr address_v4(const bytes_type &b)
Definition: internet.h:113
constexpr address_v4() noexcept
Definition: internet.h:103
uint_least32_t uint_type
Definition: internet.h:95
constexpr bool is_unspecified() const noexcept
Definition: internet.h:115
static constexpr address_v4 loopback() noexcept
Definition: internet.h:148
constexpr bool is_multicast() const noexcept
Definition: internet.h:119
std::basic_string< char, std::char_traits< char >, Allocator > to_string(const Allocator &a=Allocator()) const
Definition: internet.h:124
constexpr bytes_type to_bytes() const noexcept
Definition: internet.h:140
static constexpr address_v4 broadcast() noexcept
Definition: internet.h:151
IPv6 address with scope_id.
Definition: internet.h:182
constexpr bool is_multicast_site_local() const noexcept
Definition: internet.h:228
constexpr bool is_multicast_org_local() const noexcept
Definition: internet.h:231
constexpr bool is_multicast_link_local() const noexcept
Definition: internet.h:225
constexpr address_v6(const bytes_type &bytes, scope_id_type scope_id=0) noexcept
Definition: internet.h:192
constexpr bool is_v4_mapped() const noexcept
Definition: internet.h:216
constexpr address_v6() noexcept
Definition: internet.h:191
constexpr scope_id_type scope_id() const noexcept
Definition: internet.h:240
std::basic_string< char, std::char_traits< char >, Allocator > to_string(const Allocator &a=Allocator()) const
convert an address_v6 into a string.
Definition: internet.h:248
static constexpr address_v6 loopback()
Definition: internet.h:200
constexpr bool is_multicast() const noexcept
Definition: internet.h:209
constexpr bool is_loopback() const noexcept
Definition: internet.h:317
constexpr bytes_type to_bytes() const noexcept
Definition: internet.h:238
static constexpr address_v6 any()
Definition: internet.h:197
constexpr bool is_site_local() const noexcept
Definition: internet.h:213
constexpr bool is_unspecified() const noexcept
Definition: internet.h:314
constexpr bool is_multicast_node_local() const noexcept
Definition: internet.h:222
constexpr bool is_multicast_global() const noexcept
Definition: internet.h:234
bytes_type bytes_
Definition: internet.h:269
constexpr bool is_link_local() const noexcept
Definition: internet.h:210
scope_id_type scope_id_
Definition: internet.h:270
Definition: internet.h:321
constexpr address_v4 to_v4() const
get address_v4 part.
Definition: internet.h:335
constexpr bool is_v6() const noexcept
Definition: internet.h:328
constexpr bool is_multicast() const noexcept
Definition: internet.h:352
constexpr bool is_v4() const noexcept
Definition: internet.h:327
bool is_v4_
Definition: internet.h:373
address_v6 v6_
Definition: internet.h:370
constexpr address(const address_v4 &a) noexcept
Definition: internet.h:324
constexpr address() noexcept
Definition: internet.h:323
constexpr address_v6 to_v6() const
get address_v6 part.
Definition: internet.h:343
std::basic_string< char, std::char_traits< char >, Allocator > to_string(const Allocator &a=Allocator()) const
convert an address into a string.
Definition: internet.h:362
constexpr bool is_unspecified() const noexcept
Definition: internet.h:346
constexpr bool is_loopback() const noexcept
Definition: internet.h:349
address_v4 v4_
Definition: internet.h:369
constexpr address(const address_v6 &a) noexcept
Definition: internet.h:325
Definition: internet.h:273
bad_address_cast() noexcept=default
basic_address_iterator operator--(int) noexcept
Definition: internet.h:874
basic_address_iterator & operator++() noexcept
Definition: internet.h:859
basic_address_iterator & operator--() noexcept
Definition: internet.h:869
basic_address_iterator(const value_type &a) noexcept
Definition: internet.h:855
std::input_iterator_tag iterator_category
Definition: internet.h:853
ptrdiff_t difference_type
Definition: internet.h:850
bool operator==(const basic_address_iterator &rhs) const noexcept
Definition: internet.h:880
bool operator!=(const basic_address_iterator &rhs) const noexcept
Definition: internet.h:884
basic_address_iterator operator++(int) noexcept
Definition: internet.h:864
reference operator*() const noexcept
Definition: internet.h:857
pointer operator->() const noexcept
Definition: internet.h:858
value_type addr_
Definition: internet.h:889
pointer operator->() const noexcept
Definition: internet.h:904
value_type addr_
Definition: internet.h:921
basic_address_iterator & operator++() noexcept
reference operator*() const noexcept
Definition: internet.h:903
basic_address_iterator(const value_type &a) noexcept
Definition: internet.h:901
basic_address_iterator & operator--() noexcept
std::input_iterator_tag iterator_category
Definition: internet.h:899
ptrdiff_t difference_type
Definition: internet.h:896
Definition: internet.h:844
iterator find(const value_type &addr) const noexcept
Definition: internet.h:951
basic_address_range(const value_type &first, const value_type &last) noexcept
Definition: internet.h:942
basic_address_range() noexcept
Definition: internet.h:941
size_t size() const noexcept
Definition: internet.h:949
bool empty() const noexcept
Definition: internet.h:948
iterator end_
Definition: internet.h:959
iterator begin_
Definition: internet.h:958
iterator begin() const noexcept
Definition: internet.h:945
iterator end() const noexcept
Definition: internet.h:946
Definition: internet.h:930
Definition: internet.h:678
const void * data() const noexcept
const pointer to the underlying sockaddr.
Definition: internet.h:764
size_t size_type
Definition: internet.h:681
constexpr size_type size() const noexcept
size of the underlying sockaddr.
Definition: internet.h:774
constexpr basic_endpoint(const protocol_type &proto, port_type port_num) noexcept
construct from protocol and port-number.
Definition: internet.h:697
void resize(size_type n)
set the size of valid data of the underlying sockaddr.
Definition: internet.h:800
void * data() noexcept
pointer to the underlying sockaddr.
Definition: internet.h:769
constexpr size_t capacity() const noexcept
get capacity of the underlying sockaddr.
Definition: internet.h:782
constexpr basic_endpoint(const ip::address &addr, port_type port_num) noexcept
construct from address and port-number.
Definition: internet.h:708
constexpr protocol_type protocol() const noexcept
get protocol of the endpoint.
Definition: internet.h:734
InternetProtocol protocol_type
Definition: internet.h:680
sockaddr_in v4
Definition: internet.h:806
constexpr basic_endpoint()
default constructor.
Definition: internet.h:688
sockaddr_in6 v6
Definition: internet.h:807
constexpr ip::address address() const noexcept
get address of the endpoint.
Definition: internet.h:742
constexpr port_type port() const noexcept
get port of the endpoint.
Definition: internet.h:756
endpoint of IPv4/IPv6 based connection.
Definition: internet.h:513
basic_resolver_entry(const endpoint_type &ep, std::string host_name, std::string service_name)
Definition: internet.h:520
std::string host_name() const
Definition: internet.h:528
basic_resolver_entry()=default
endpoint_type ep_
Definition: internet.h:532
typename protocol_type::endpoint endpoint_type
Definition: internet.h:516
endpoint_type endpoint() const
Definition: internet.h:526
std::string host_name_
Definition: internet.h:533
std::string service_name_
Definition: internet.h:534
InternetProtocol protocol_type
Definition: internet.h:515
std::string service_name() const
Definition: internet.h:529
Definition: internet.h:542
const_iterator iterator
Definition: internet.h:550
typename protocol_type::endpoint endpoint_type
Definition: internet.h:545
InternetProtocol protocol_type
Definition: internet.h:544
size_type max_size() const noexcept
Definition: internet.h:557
const_iterator cbegin() const
Definition: internet.h:562
basic_resolver_results(std::unique_ptr< addrinfo, void(*)(addrinfo *)> ainfo, const std::string &host_name, const std::string &service_name)
Definition: internet.h:568
ptrdiff_t difference_type
Definition: internet.h:551
std::forward_list< value_type > results_
Definition: internet.h:590
basic_resolver_results(const endpoint_type &ep, const std::string &host_name, const std::string &service_name)
Definition: internet.h:582
typename std::forward_list< value_type >::const_iterator const_iterator
Definition: internet.h:549
bool empty() const noexcept
Definition: internet.h:558
const_iterator begin() const
Definition: internet.h:560
const_iterator end() const
Definition: internet.h:561
size_type size() const noexcept
Definition: internet.h:556
size_t size_type
Definition: internet.h:552
Definition: internet.h:608
stdx::expected< results_type, error_type > resolve(const endpoint_type &ep)
Definition: internet.h:643
typename InternetProtocol::endpoint endpoint_type
Definition: internet.h:612
InternetProtocol protocol_type
Definition: internet.h:611
impl::socket::error_type error_type
Definition: internet.h:614
stdx::expected< results_type, error_type > resolve(const std::string &host_name, const std::string &service_name)
Definition: internet.h:638
basic_resolver(io_context &io_ctx)
Definition: internet.h:616
io_context & io_ctx_
Definition: internet.h:674
stdx::expected< results_type, error_type > resolve(const std::string &host_name, const std::string &service_name, flags f)
Definition: internet.h:618
Definition: internet.h:970
constexpr bool is_subnet_of(const network_v4 &other) const noexcept
Definition: internet.h:1033
constexpr address_v4 network() const noexcept
Definition: internet.h:1002
constexpr int prefix_length() const noexcept
Definition: internet.h:994
constexpr network_v4(const address_v4 &addr, const address_v4 &mask)
Definition: internet.h:975
constexpr address_v4 broadcast() const noexcept
Definition: internet.h:1005
constexpr address_v4 netmask() const noexcept
Definition: internet.h:995
address_v4_range hosts() const noexcept
constexpr network_v4 canonical() const noexcept
Definition: internet.h:1011
constexpr network_v4() noexcept=default
std::basic_string< char, std::char_traits< char >, Allocator > to_string(const Allocator &a=Allocator()) const
Definition: internet.h:1018
constexpr bool is_host() const noexcept
Definition: internet.h:1014
constexpr address_v4 address() const noexcept
Definition: internet.h:993
Definition: internet.h:1053
address_v6_range hosts() const noexcept
constexpr unsigned char networkbits(const address_v6::bytes_type &address_bytes, int prefix_len, int ndx) const
Definition: internet.h:1099
constexpr address_v6 network() const noexcept
Definition: internet.h:1062
std::basic_string< char, std::char_traits< char >, Allocator > to_string(const Allocator &a=Allocator()) const
Definition: internet.h:1093
constexpr bool is_host() const noexcept
Definition: internet.h:1089
constexpr network_v6 canonical() const noexcept
Definition: internet.h:1086
constexpr uint8_t leftmostbits(int ndx, int prefix_len) const
Definition: internet.h:1105
constexpr int prefix_length() const noexcept
Definition: internet.h:1061
constexpr bool is_subnet_of(const network_v6 &other) const noexcept
Definition: internet.h:1124
constexpr network_v6() noexcept=default
constexpr address_v6 address() const noexcept
Definition: internet.h:1060
Definition: internet.h:594
std::bitset< 32 > flags
Definition: internet.h:596
TCP protocol.
Definition: internet.h:1155
static constexpr tcp v4() noexcept
Definition: internet.h:1299
constexpr int protocol() const noexcept
Definition: internet.h:1304
int family_
Definition: internet.h:1311
constexpr int family() const noexcept
Definition: internet.h:1302
constexpr int type() const noexcept
Definition: internet.h:1303
constexpr tcp(int family)
Definition: internet.h:1309
static constexpr tcp v6() noexcept
Definition: internet.h:1300
Definition: internet.h:1324
int family_
Definition: internet.h:1342
constexpr int protocol() const noexcept
Definition: internet.h:1335
constexpr int family() const noexcept
Definition: internet.h:1333
constexpr int type() const noexcept
Definition: internet.h:1334
static constexpr udp v4() noexcept
Definition: internet.h:1330
constexpr udp(int family)
Definition: internet.h:1340
static constexpr udp v6() noexcept
Definition: internet.h:1331
base-class of socket options.
Definition: socket.h:77
Definition: expected.h:944
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
const byte * find(const Pages *pages, const page_id_t &page_id) noexcept
Find a doublewrite copy of a page.
Definition: buf0dblwr.cc:3563
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:910
stdx::expected< void, std::error_code > getnameinfo(const struct sockaddr *saddr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
Definition: resolver.h:173
std::error_code last_error_code()
get last std::error_code for socket-errors.
Definition: socket_error.h:107
std::error_code error_type
Definition: socket_constants.h:55
constexpr bool operator>=(const address_v4 &a, const address_v4 &b) noexcept
Definition: internet.h:175
stdx::expected< address, std::error_code > make_address(const char *str)
make address from a c-string.
Definition: internet.h:454
stdx::expected< address_v6, std::error_code > make_address_v6(const char *str)
make address_v6 from a string.
Definition: internet.h:383
constexpr bool operator==(const address_v4 &a, const address_v4 &b) noexcept
Definition: internet.h:160
constexpr T host_to_network(const T t) noexcept
convert an integer from host-endianness into network endianness.
Definition: internet.h:68
constexpr bool operator<(const address_v4 &a, const address_v4 &b) noexcept
Definition: internet.h:166
stdx::expected< address_v4, std::error_code > make_address_v4(const char *str)
make address_v4 from a string.
Definition: internet.h:432
uint_least32_t scope_id_type
Definition: internet.h:91
constexpr bool operator<=(const address_v4 &a, const address_v4 &b) noexcept
Definition: internet.h:172
constexpr T network_to_host(const T t) noexcept
convert an integer from network-endianness into host endianness.
Definition: internet.h:81
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const address &addr)
Definition: internet.h:479
constexpr bool operator>(const address_v4 &a, const address_v4 &b) noexcept
Definition: internet.h:169
uint_least16_t port_type
Definition: internet.h:90
constexpr bool operator!=(const address_v4 &a, const address_v4 &b) noexcept
Definition: internet.h:163
std::error_code make_error_code(resolver_errc ec)
Definition: resolver.h:135
Definition: buffer.h:45
Definition: gcs_xcom_synode.h:64
constexpr auto make_unexpected(E &&e) -> unexpected< std::decay_t< E > >
Definition: expected.h:125
constexpr std::enable_if_t< std::is_integral< IntegerType >::value, IntegerType > byteswap(IntegerType t) noexcept
Definition: bit.h:142
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2439
required string type
Definition: replication_group_member_actions.proto:34
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
Definition: internet.h:96
constexpr bytes_type(T... t)
Definition: internet.h:98
Definition: internet.h:184
constexpr bytes_type(T... t)
Definition: internet.h:187
#define IPPROTO_TCP
Definition: task_os.h:87
int n
Definition: xcom_base.cc:509