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