MySQL 8.2.0
Source Code Documentation
tcp_address.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 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_TCPADDRESS_INCLUDED
26#define MYSQL_HARNESS_TCPADDRESS_INCLUDED
27
28#include <cstdint>
29#include <string>
30#include <system_error>
31
32#include "harness_export.h"
33
35
36namespace mysql_harness {
37
38/** @brief Defines an IP address with port number */
39class HARNESS_EXPORT TCPAddress {
40 public:
41 TCPAddress() = default;
42
43 TCPAddress(std::string address, uint16_t tcp_port)
44 : addr_(std::move(address)), port_(tcp_port) {}
45
46 TCPAddress(const TCPAddress &other) = default;
47 TCPAddress(TCPAddress &&other) = default;
48 TCPAddress &operator=(const TCPAddress &other) = default;
49 TCPAddress &operator=(TCPAddress &&other) = default;
50
51 std::string address() const { return addr_; }
52
53 uint16_t port() const { return port_; }
54
55 void port(uint16_t p) { port_ = p; }
56
57 /** @brief Returns the address as a string
58 *
59 * Returns the address as a string.
60 *
61 * @return instance of std::string
62 */
63 std::string str() const;
64
65 /** @brief Compares two addresses for equality
66 *
67 */
68 friend bool operator==(const TCPAddress &left, const TCPAddress &right) {
69 return (left.addr_ == right.addr_) && (left.port_ == right.port_);
70 }
71
72 /**
73 * @brief Function for performing comparison of TCPAddresses
74 */
75 friend bool operator<(const TCPAddress &left, const TCPAddress &right) {
76 if (left.addr_ < right.addr_)
77 return true;
78 else if (left.addr_ > right.addr_)
79 return false;
80 return left.port_ < right.port_;
81 }
82
83 private:
84 /** @brief Network name IP */
85 std::string addr_;
86
87 /** @brief TCP port */
88 uint16_t port_{};
89};
90
91/**
92 * create TCPAddress from endpoint string.
93 *
94 * - [::1]:1234
95 * - ::1
96 * - 10.0.1.1
97 * - 10.0.1.1:1234
98 * - example.org:1234
99 */
101 const std::string &endpoint);
102
103} // namespace mysql_harness
104
105#endif // MYSQL_HARNESS_TCPADDRESS_INCLUDED
Defines an IP address with port number
Definition: tcp_address.h:39
uint16_t port_
TCP port.
Definition: tcp_address.h:88
friend bool operator==(const TCPAddress &left, const TCPAddress &right)
Compares two addresses for equality.
Definition: tcp_address.h:68
TCPAddress(std::string address, uint16_t tcp_port)
Definition: tcp_address.h:43
std::string addr_
Network name IP.
Definition: tcp_address.h:85
friend bool operator<(const TCPAddress &left, const TCPAddress &right)
Function for performing comparison of TCPAddresses.
Definition: tcp_address.h:75
uint16_t port() const
Definition: tcp_address.h:53
TCPAddress & operator=(TCPAddress &&other)=default
TCPAddress & operator=(const TCPAddress &other)=default
TCPAddress(const TCPAddress &other)=default
TCPAddress(TCPAddress &&other)=default
void port(uint16_t p)
Definition: tcp_address.h:55
std::string address() const
Definition: tcp_address.h:51
Definition: expected.h:943
const char * p
Definition: ctype-mb.cc:1234
static uint tcp_port
Definition: mysqladmin.cc:74
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1085
Definition: common.h:41
HARNESS_EXPORT stdx::expected< TCPAddress, std::error_code > make_tcp_address(const std::string &endpoint)
create TCPAddress from endpoint string.
Definition: tcp_address.cc:139
Definition: varlen_sort.h:183