MySQL 26.10.0
Source Code Documentation
gcs_xcom_networking.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
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, version 2.0, 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#ifndef GCS_XCOM_NETWORKING_H
25#define GCS_XCOM_NETWORKING_H
26
27#include <map>
28#include <string>
29#include <vector>
30
33
34#ifdef _WIN32
35typedef u_short sa_family_t;
36#endif
37
38/**
39 * @brief Interface to decouple XCom sock_probe implementation to allow
40 * unit testing.
41 */
43 public:
45 virtual ~Gcs_sock_probe_interface() = default;
46
47 virtual int init_sock_probe(sock_probe *s) = 0;
48 virtual int number_of_interfaces(sock_probe *s) = 0;
50 struct sockaddr **out) = 0;
52 struct sockaddr **out) = 0;
53 virtual char *get_if_name(sock_probe *s, int count) = 0;
54 virtual bool_t is_if_running(sock_probe *s, int count) = 0;
55 virtual void close_sock_probe(sock_probe *s) = 0;
56
59 default; // move constructor must be explicit because copy is
61 default;
63 default; // move assignment must be explicit because move is
64};
65
66/**
67 * @brief Implementation of @class Gcs_sock_probe_interface
68 */
70 public:
72 ~Gcs_sock_probe_interface_impl() override = default;
73
74 int init_sock_probe(sock_probe *s) override;
75 int number_of_interfaces(sock_probe *s) override;
77 struct sockaddr **out) override;
79 struct sockaddr **out) override;
80 char *get_if_name(sock_probe *s, int count) override;
81 bool_t is_if_running(sock_probe *s, int count) override;
82 void close_sock_probe(sock_probe *s) override;
83
86 default; // move constructor must be explicit because copy is
88 const Gcs_sock_probe_interface_impl &) = default;
90 default; // move assignment must be explicit because move is
91};
92
93/**
94 This function gets all network addresses on this host and their
95 subnet masks as a string.
96
97 @param[out] sock_probe Socket probe interface
98 @param[out] out maps IP addresses to subnetmasks
99 @param filter_out_inactive If set to true, only active interfaces will be
100 added to out
101 @return false on success, true otherwise.
102 */
104 std::map<std::string, int> &out,
105 bool filter_out_inactive = false);
106
107/**
108 This function gets all private network addresses and their
109 subnet masks as a string. IPv4 only. (SOCK_STREAM only)
110 @param[out] out maps IP addresses to subnetmasks
111 @param filter_out_inactive If set to true, only active interfaces will be added
112 to out
113 @return false on success, true otherwise.
114 */
115bool get_local_private_addresses(std::map<std::string, int> &out,
116 bool filter_out_inactive = false);
117
118/**
119 This function translates hostnames to all possible IP addresses.
120
121 @param[in] name The hostname to translate.
122 @param[out] ips The IP addresses after translation.
123
124 @return false on success, true otherwise.
125 */
127 const std::string &name,
128 std::vector<std::pair<sa_family_t, std::string>> &ips);
129
130/**
131 This function translates hostname to all possible IP addresses.
132
133 @param[in] name The hostname to translate.
134 @param[out] ip The IP addresses after translation.
135
136 @return false on success, true otherwise.
137 */
138bool resolve_ip_addr_from_hostname(const std::string &name,
139 std::vector<std::string> &ip);
140
141/**
142 Converts the peer address of a connected socket to a string.
143
144 @param[in] fd Connected socket file descriptor.
145 @param[out] out String representation of the peer address.
146
147 @return false on success, true otherwise.
148 */
149bool sock_descriptor_to_string(int fd, std::string &out);
150
151/**
152 Converts an address in string format (X.X.X.X/XX) into network octet format
153
154 @param[in] addr IP address in X.X.X.X format
155 @param[in] mask Network mask associated with the address
156 @param[out] out_pair IP address and netmask, in binary format (network byte
157 order)
158
159 @return false on success, true otherwise.
160 */
161bool get_address_for_allowlist(const std::string &addr, const std::string &mask,
162 std::pair<std::vector<unsigned char>,
163 std::vector<unsigned char>> &out_pair);
164
165/**
166 @class Gcs_ip_allowlist_entry
167 @brief Base abstract class for the allowlist entries.
168
169 This is the base class for the Allowlist entries. Any derived class must
170 implement its two abstract methods:
171 - init_value();
172 - get_value();
173 */
175 public:
176 /**
177 Constructor
178
179 @param[in] addr IP address or hostname of this entry
180 @param[in] mask Network mask of this entry.
181 */
182 Gcs_ip_allowlist_entry(std::string addr, std::string mask);
183
184 virtual ~Gcs_ip_allowlist_entry() = default;
185
186 /**
187 Entry initialization.
188
189 If one needs to initialize internal values, it should be done in this
190 method.
191
192 @return false on success, true otherwise
193 */
194 virtual bool init_value() = 0;
195
196 /**
197 Virtual Method that implements value retrieval for this entry.
198
199 The returned value must be a list of std::pairs that contains both the
200 address and the mask in network octet value. This is in list format because
201 in the case of allowlist names, we can have multiple value for the same
202 entry
203
204 @return an std::vector of std::pair with ip and mask in network octet form
205 */
206 virtual std::vector<
207 std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
208 *get_value() = 0;
209
210 /** Getters */
211 std::string get_addr() const { return m_addr; }
212 std::string get_mask() const { return m_mask; }
213
214 private:
215 std::string m_addr;
216 std::string m_mask;
217};
218
221 const Gcs_ip_allowlist_entry *rhs) const {
222 // Check if addresses are different in content
223 if (lhs->get_addr() != rhs->get_addr()) { // Then compare only the
224 // addresses
225 return lhs->get_addr() < rhs->get_addr();
226 } else { // If addresses are equal, then compare the masks to untie.
227 return lhs->get_mask() < rhs->get_mask();
228 }
229 }
230};
231
232/**
233 @class Gcs_ip_allowlist_entry_ip
234 @brief Implementation of Gcs_ip_allowlist_entry to use with
235 raw IP addresses in format X.X.X.X/XX
236 */
238 public:
239 Gcs_ip_allowlist_entry_ip(std::string addr, std::string mask);
240
241 public:
242 bool init_value() override;
243 std::vector<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
244 *get_value() override;
245
246 private:
247 std::pair<std::vector<unsigned char>, std::vector<unsigned char>> m_value;
248};
249
250/**
251 @class Gcs_ip_allowlist_entry_hostname
252 @brief Implementation of Gcs_ip_allowlist_entry to use with
253 hostnames
254 */
256 public:
257 Gcs_ip_allowlist_entry_hostname(std::string addr, std::string mask);
258 Gcs_ip_allowlist_entry_hostname(std::string addr);
259
260 public:
261 bool init_value() override;
262 std::vector<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
263 *get_value() override;
264};
265
267 public:
268 static const std::string DEFAULT_ALLOWLIST;
269
270 private:
272 private:
273 /**
274 * @brief When true, it is locked. When false it is not.
275 */
276 std::atomic_flag &m_guard;
277
278 public:
279 Atomic_lock_guard(std::atomic_flag &guard) : m_guard(guard) {
280 // keep trying until it is unlocked, then lock
281 while (m_guard.test_and_set()) {
282 std::this_thread::yield();
283 }
284 }
285
287 };
288
289 private:
290 /*
291 The IP allowlist. It is a list of tuples Hexadecimal IP number
292 and subnet mask also in Hexadecimal. E.g.: 192.168.1.2/24 or 127.0.0.1/32.
293
294 This is for optimization purposes, so that we don't calculate the
295 values each time we want to check.
296 */
297 std::set<Gcs_ip_allowlist_entry *, Gcs_ip_allowlist_entry_pointer_comparator>
299
300 /**
301 This is the list that originally submitted to be parsed and to configure
302 the allowlist.
303 */
304 std::string m_original_list;
305
306 public:
308 m_atomic_guard.clear();
309 }
310 virtual ~Gcs_ip_allowlist();
311
312 /**
313 This member function shall be used to configure the allowlist.
314
315 @param the_list The list with IP addresses. This list is a comma separated
316 list formatted only with IP addresses and/or in the form of
317 a subnet range, e.g., IP/netbits.
318 @return true if the configuration failed, false otherwise.
319 */
320 bool configure(const std::string &the_list);
321
322 /**
323 This member function shall be used to validate the list that is used as
324 input to the configure member function.
325
326 @param the_list The list with IP addresses. This list is a comma separated
327 list formatted only with IP addresses and/or in the form of
328 a subnet range, e.g., IP/netbits.
329
330 @return true if the configuration failed, false otherwise.
331 */
332 bool is_valid(const std::string &the_list);
333
334 /**
335 This member function SHALL return true if the given IP is to be blocked,
336 false otherwise.
337
338 @param ip_addr a string representation of an IPv4 address.
339 @param xcom_config the latest XCom configuration.
340
341 @return true if the ip should be blocked, false otherwise.
342 */
343 bool shall_block(const std::string &ip_addr,
344 site_def const *xcom_config = nullptr);
345
346 /**
347 This member function SHALL return true if the IP of the given file
348 descriptor is to be blocked, false otherwise.
349
350 @param fd the file descriptor of the accepted socket to check.
351 @param xcom_config the latest XCom configuration.
352
353 @return true if the ip should be blocked, false otherwise.
354 */
355 bool shall_block(int fd, site_def const *xcom_config = nullptr);
356
357 /**
358 This member function gets the textual representation of the list as
359 provided to the configure member function.
360 */
361 const std::string get_configured_ip_allowlist() {
362 // lock the list
364 return m_original_list;
365 }
366
367 /**
368 A string representation of the internal list of IP addresses. Can have
369 more addresses than those submitted through the configure member
370 function, since there are addresses that are implicitly added when
371 configuring the list.
372 */
373 std::string to_string() const;
374
375 private:
376 bool do_check_block(struct sockaddr_storage *sa,
377 site_def const *xcom_config) const;
379 std::vector<unsigned char> const &incoming_octets) const;
380 bool do_check_block_xcom(std::vector<unsigned char> const &incoming_octets,
381 site_def const *xcom_config) const;
382 bool add_address(const std::string &addr, const std::string &mask);
383
384 /**
385 @brief Clears the contents of this Allowlist object.
386
387 It deletes all entries and clears the internal set.
388 */
389 void clear();
390
391 /**
392 * @brief An atomic lock to guard the ip allowlist.
393 */
394 std::atomic_flag m_atomic_guard;
395
396 private:
399};
400
401#endif /* GCS_XCOM_NETWORKING_H */
Definition: gcs_xcom_networking.h:271
Atomic_lock_guard(std::atomic_flag &guard)
Definition: gcs_xcom_networking.h:279
std::atomic_flag & m_guard
When true, it is locked.
Definition: gcs_xcom_networking.h:276
~Atomic_lock_guard()
Definition: gcs_xcom_networking.h:286
Implementation of Gcs_ip_allowlist_entry to use with hostnames.
Definition: gcs_xcom_networking.h:255
Gcs_ip_allowlist_entry_hostname(std::string addr, std::string mask)
Definition: gcs_xcom_networking.cc:513
std::vector< std::pair< std::vector< unsigned char >, std::vector< unsigned char > > > * get_value() override
Virtual Method that implements value retrieval for this entry.
Definition: gcs_xcom_networking.cc:524
bool init_value() override
Entry initialization.
Definition: gcs_xcom_networking.cc:521
Implementation of Gcs_ip_allowlist_entry to use with raw IP addresses in format X....
Definition: gcs_xcom_networking.h:237
bool init_value() override
Entry initialization.
Definition: gcs_xcom_networking.cc:500
Gcs_ip_allowlist_entry_ip(std::string addr, std::string mask)
Definition: gcs_xcom_networking.cc:496
std::vector< std::pair< std::vector< unsigned char >, std::vector< unsigned char > > > * get_value() override
Virtual Method that implements value retrieval for this entry.
Definition: gcs_xcom_networking.cc:507
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > m_value
Definition: gcs_xcom_networking.h:247
Base abstract class for the allowlist entries.
Definition: gcs_xcom_networking.h:174
virtual ~Gcs_ip_allowlist_entry()=default
std::string m_mask
Definition: gcs_xcom_networking.h:216
std::string m_addr
Definition: gcs_xcom_networking.h:215
virtual bool init_value()=0
Entry initialization.
std::string get_mask() const
Definition: gcs_xcom_networking.h:212
virtual std::vector< std::pair< std::vector< unsigned char >, std::vector< unsigned char > > > * get_value()=0
Virtual Method that implements value retrieval for this entry.
std::string get_addr() const
Getters.
Definition: gcs_xcom_networking.h:211
Gcs_ip_allowlist_entry(std::string addr, std::string mask)
Constructor.
Definition: gcs_xcom_networking.cc:492
Definition: gcs_xcom_networking.h:266
bool do_check_block_xcom(std::vector< unsigned char > const &incoming_octets, site_def const *xcom_config) const
Definition: gcs_xcom_networking.cc:833
std::set< Gcs_ip_allowlist_entry *, Gcs_ip_allowlist_entry_pointer_comparator > m_ip_allowlist
Definition: gcs_xcom_networking.h:298
std::string to_string() const
A string representation of the internal list of IP addresses.
Definition: gcs_xcom_networking.cc:574
Gcs_ip_allowlist(Gcs_ip_allowlist const &)
const std::string get_configured_ip_allowlist()
This member function gets the textual representation of the list as provided to the configure member ...
Definition: gcs_xcom_networking.h:361
bool do_check_block(struct sockaddr_storage *sa, site_def const *xcom_config) const
Definition: gcs_xcom_networking.cc:902
Gcs_ip_allowlist & operator=(Gcs_ip_allowlist const &)
Gcs_ip_allowlist()
Definition: gcs_xcom_networking.h:307
bool shall_block(const std::string &ip_addr, site_def const *xcom_config=nullptr)
This member function SHALL return true if the given IP is to be blocked, false otherwise.
Definition: gcs_xcom_networking.cc:988
static const std::string DEFAULT_ALLOWLIST
Definition: gcs_xcom_networking.h:268
bool do_check_block_allowlist(std::vector< unsigned char > const &incoming_octets) const
Definition: gcs_xcom_networking.cc:796
bool add_address(const std::string &addr, const std::string &mask)
Definition: gcs_xcom_networking.cc:772
virtual ~Gcs_ip_allowlist()
Definition: gcs_xcom_networking.cc:770
void clear()
Clears the contents of this Allowlist object.
Definition: gcs_xcom_networking.cc:762
bool configure(const std::string &the_list)
This member function shall be used to configure the allowlist.
Definition: gcs_xcom_networking.cc:642
std::string m_original_list
This is the list that originally submitted to be parsed and to configure the allowlist.
Definition: gcs_xcom_networking.h:304
std::atomic_flag m_atomic_guard
An atomic lock to guard the ip allowlist.
Definition: gcs_xcom_networking.h:394
bool is_valid(const std::string &the_list)
This member function shall be used to validate the list that is used as input to the configure member...
Definition: gcs_xcom_networking.cc:588
Definition: gcs_xcom_networking.h:69
Gcs_sock_probe_interface_impl()
Definition: gcs_xcom_networking.h:71
Gcs_sock_probe_interface_impl & operator=(Gcs_sock_probe_interface_impl &&)=default
void close_sock_probe(sock_probe *s) override
Definition: gcs_xcom_networking.cc:475
Gcs_sock_probe_interface_impl(Gcs_sock_probe_interface_impl &)=default
void get_sockaddr_netmask(sock_probe *s, int count, struct sockaddr **out) override
Definition: gcs_xcom_networking.cc:467
void get_sockaddr_address(sock_probe *s, int count, struct sockaddr **out) override
Definition: gcs_xcom_networking.cc:462
~Gcs_sock_probe_interface_impl() override=default
int number_of_interfaces(sock_probe *s) override
Definition: gcs_xcom_networking.cc:458
Gcs_sock_probe_interface_impl & operator=(const Gcs_sock_probe_interface_impl &)=default
char * get_if_name(sock_probe *s, int count) override
Definition: gcs_xcom_networking.cc:471
Gcs_sock_probe_interface_impl(Gcs_sock_probe_interface_impl &&)=default
bool_t is_if_running(sock_probe *s, int count) override
Definition: gcs_xcom_networking.cc:479
int init_sock_probe(sock_probe *s) override
Definition: gcs_xcom_networking.cc:454
Interface to decouple XCom sock_probe implementation to allow unit testing.
Definition: gcs_xcom_networking.h:42
Gcs_sock_probe_interface(Gcs_sock_probe_interface &)=default
virtual int init_sock_probe(sock_probe *s)=0
virtual char * get_if_name(sock_probe *s, int count)=0
Gcs_sock_probe_interface & operator=(const Gcs_sock_probe_interface &)=default
Gcs_sock_probe_interface & operator=(Gcs_sock_probe_interface &&)=default
virtual int number_of_interfaces(sock_probe *s)=0
virtual void close_sock_probe(sock_probe *s)=0
Gcs_sock_probe_interface(Gcs_sock_probe_interface &&)=default
virtual ~Gcs_sock_probe_interface()=default
virtual void get_sockaddr_address(sock_probe *s, int count, struct sockaddr **out)=0
virtual bool_t is_if_running(sock_probe *s, int count)=0
Gcs_sock_probe_interface()=default
virtual void get_sockaddr_netmask(sock_probe *s, int count, struct sockaddr **out)=0
bool get_local_addresses(Gcs_sock_probe_interface &sock_probe, std::map< std::string, int > &out, bool filter_out_inactive=false)
This function gets all network addresses on this host and their subnet masks as a string.
Definition: gcs_xcom_networking.cc:95
bool resolve_all_ip_addr_from_hostname(const std::string &name, std::vector< std::pair< sa_family_t, std::string > > &ips)
This function translates hostnames to all possible IP addresses.
Definition: gcs_xcom_networking.cc:302
bool get_local_private_addresses(std::map< std::string, int > &out, bool filter_out_inactive=false)
This function gets all private network addresses and their subnet masks as a string.
Definition: gcs_xcom_networking.cc:211
bool sock_descriptor_to_string(int fd, std::string &out)
Converts the peer address of a connected socket to a string.
Definition: gcs_xcom_networking.cc:423
bool get_address_for_allowlist(const std::string &addr, const std::string &mask, std::pair< std::vector< unsigned char >, std::vector< unsigned char > > &out_pair)
Converts an address in string format (X.X.X.X/XX) into network octet format.
Definition: gcs_xcom_networking.cc:709
bool resolve_ip_addr_from_hostname(const std::string &name, std::vector< std::string > &ip)
This function translates hostname to all possible IP addresses.
Definition: gcs_xcom_networking.cc:260
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
static int count
Definition: myisam_ftdump.cc:45
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2724
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
case opt name
Definition: sslopt-case.h:29
Definition: gcs_xcom_networking.h:219
bool operator()(const Gcs_ip_allowlist_entry *lhs, const Gcs_ip_allowlist_entry *rhs) const
Definition: gcs_xcom_networking.h:220
Definition: site_struct.h:43
Definition: sock_probe_ix.h:54
__u_short u_short
Definition: types.h:72
int bool_t
Definition: types.h:35