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