MySQL 8.4.13
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 std::string name, std::vector<std::pair<sa_family_t, std::string>> &ips);
128
129/**
130 This function translates hostname to all possible IP addresses.
131
132 @param[in] name The hostname to translate.
133 @param[out] ip The IP addresses after translation.
134
135 @return false on success, true otherwise.
136 */
137bool resolve_ip_addr_from_hostname(std::string name,
138 std::vector<std::string> &ip);
139
140/**
141 Converts the peer address of a connected socket to a string.
142
143 @param[in] fd Connected socket file descriptor.
144 @param[out] out String representation of the peer address.
145
146 @return false on success, true otherwise.
147 */
148bool sock_descriptor_to_string(int fd, std::string &out);
149
150/**
151 Converts an address in string format (X.X.X.X/XX) into network octet format
152
153 @param[in] addr IP address in X.X.X.X format
154 @param[in] mask Network mask associated with the address
155 @param[out] out_pair IP address and netmask, in binary format (network byte
156 order)
157
158 @return false on success, true otherwise.
159 */
160bool get_address_for_allowlist(std::string addr, std::string mask,
161 std::pair<std::vector<unsigned char>,
162 std::vector<unsigned char>> &out_pair);
163
164/**
165 @class Gcs_ip_allowlist_entry
166 @brief Base abstract class for the allowlist entries.
167
168 This is the base class for the Allowlist entries. Any derived class must
169 implement its two abstract methods:
170 - init_value();
171 - get_value();
172 */
174 public:
175 /**
176 Constructor
177
178 @param[in] addr IP address or hostname of this entry
179 @param[in] mask Network mask of this entry.
180 */
181 Gcs_ip_allowlist_entry(std::string addr, std::string mask);
182
183 virtual ~Gcs_ip_allowlist_entry() = default;
184
185 /**
186 Entry initialization.
187
188 If one needs to initialize internal values, it should be done in this
189 method.
190
191 @return false on success, true otherwise
192 */
193 virtual bool init_value() = 0;
194
195 /**
196 Virtual Method that implements value retrieval for this entry.
197
198 The returned value must be a list of std::pairs that contains both the
199 address and the mask in network octet value. This is in list format because
200 in the case of allowlist names, we can have multiple value for the same
201 entry
202
203 @return an std::vector of std::pair with ip and mask in network octet form
204 */
205 virtual std::vector<
206 std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
207 *get_value() = 0;
208
209 /** Getters */
210 std::string get_addr() const { return m_addr; }
211 std::string get_mask() const { return m_mask; }
212
213 private:
214 std::string m_addr;
215 std::string m_mask;
216};
217
220 const Gcs_ip_allowlist_entry *rhs) const {
221 // Check if addresses are different in content
222 if (lhs->get_addr() != rhs->get_addr()) { // Then compare only the
223 // addresses
224 return lhs->get_addr() < rhs->get_addr();
225 } else { // If addresses are equal, then compare the masks to untie.
226 return lhs->get_mask() < rhs->get_mask();
227 }
228 }
229};
230
231/**
232 @class Gcs_ip_allowlist_entry_ip
233 @brief Implementation of Gcs_ip_allowlist_entry to use with
234 raw IP addresses in format X.X.X.X/XX
235 */
237 public:
238 Gcs_ip_allowlist_entry_ip(std::string addr, std::string mask);
239
240 public:
241 bool init_value() override;
242 std::vector<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
243 *get_value() override;
244
245 private:
246 std::pair<std::vector<unsigned char>, std::vector<unsigned char>> m_value;
247};
248
249/**
250 @class Gcs_ip_allowlist_entry_hostname
251 @brief Implementation of Gcs_ip_allowlist_entry to use with
252 hostnames
253 */
255 public:
256 Gcs_ip_allowlist_entry_hostname(std::string addr, std::string mask);
257 Gcs_ip_allowlist_entry_hostname(std::string addr);
258
259 public:
260 bool init_value() override;
261 std::vector<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
262 *get_value() override;
263};
264
266 public:
267 static const std::string DEFAULT_ALLOWLIST;
268
269 private:
271 private:
272 /**
273 * @brief When true, it is locked. When false it is not.
274 */
275 std::atomic_flag &m_guard;
276
277 public:
278 Atomic_lock_guard(std::atomic_flag &guard) : m_guard(guard) {
279 // keep trying until it is unlocked, then lock
280 while (m_guard.test_and_set()) {
281 std::this_thread::yield();
282 }
283 }
284
286 };
287
288 private:
289 /*
290 The IP allowlist. It is a list of tuples Hexadecimal IP number
291 and subnet mask also in Hexadecimal. E.g.: 192.168.1.2/24 or 127.0.0.1/32.
292
293 This is for optimization purposes, so that we don't calculate the
294 values each time we want to check.
295 */
296 std::set<Gcs_ip_allowlist_entry *, Gcs_ip_allowlist_entry_pointer_comparator>
298
299 /**
300 This is the list that originally submitted to be parsed and to configure
301 the allowlist.
302 */
303 std::string m_original_list;
304
305 public:
307 m_atomic_guard.clear();
308 }
309 virtual ~Gcs_ip_allowlist();
310
311 /**
312 This member function shall be used to configure the allowlist.
313
314 @param the_list The list with IP addresses. This list is a comma separated
315 list formatted only with IP addresses and/or in the form of
316 a subnet range, e.g., IP/netbits.
317 @return true if the configuration failed, false otherwise.
318 */
319 bool configure(const std::string &the_list);
320
321 /**
322 This member function shall be used to validate the list that is used as
323 input to the configure member function.
324
325 @param the_list The list with IP addresses. This list is a comma separated
326 list formatted only with IP addresses and/or in the form of
327 a subnet range, e.g., IP/netbits.
328
329 @return true if the configuration failed, false otherwise.
330 */
331 bool is_valid(const std::string &the_list);
332
333 /**
334 This member function SHALL return true if the given IP is to be blocked,
335 false otherwise.
336
337 @param ip_addr a string representation of an IPv4 address.
338 @param xcom_config the latest XCom configuration.
339
340 @return true if the ip should be blocked, false otherwise.
341 */
342 bool shall_block(const std::string &ip_addr,
343 site_def const *xcom_config = nullptr);
344
345 /**
346 This member function SHALL return true if the IP of the given file
347 descriptor is to be blocked, false otherwise.
348
349 @param fd the file descriptor of the accepted socket to check.
350 @param xcom_config the latest XCom configuration.
351
352 @return true if the ip should be blocked, false otherwise.
353 */
354 bool shall_block(int fd, site_def const *xcom_config = nullptr);
355
356 /**
357 This member function gets the textual representation of the list as
358 provided to the configure member function.
359 */
360 const std::string get_configured_ip_allowlist() {
361 // lock the list
363 return m_original_list;
364 }
365
366 /**
367 A string representation of the internal list of IP addresses. Can have
368 more addresses than those submitted through the configure member
369 function, since there are addresses that are implicitly added when
370 configuring the list.
371 */
372 std::string to_string() const;
373
374 private:
375 bool do_check_block(struct sockaddr_storage *sa,
376 site_def const *xcom_config) const;
378 std::vector<unsigned char> const &incoming_octets) const;
379 bool do_check_block_xcom(std::vector<unsigned char> const &incoming_octets,
380 site_def const *xcom_config) const;
381 bool add_address(std::string addr, std::string mask);
382
383 /**
384 @brief Clears the contents of this Allowlist object.
385
386 It deletes all entries and clears the internal set.
387 */
388 void clear();
389
390 /**
391 * @brief An atomic lock to guard the ip allowlist.
392 */
393 std::atomic_flag m_atomic_guard;
394
395 private:
398};
399
400#endif /* GCS_XCOM_NETWORKING_H */
Definition: gcs_xcom_networking.h:270
Atomic_lock_guard(std::atomic_flag &guard)
Definition: gcs_xcom_networking.h:278
std::atomic_flag & m_guard
When true, it is locked.
Definition: gcs_xcom_networking.h:275
~Atomic_lock_guard()
Definition: gcs_xcom_networking.h:285
Implementation of Gcs_ip_allowlist_entry to use with hostnames.
Definition: gcs_xcom_networking.h:254
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:236
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:246
Base abstract class for the allowlist entries.
Definition: gcs_xcom_networking.h:173
virtual ~Gcs_ip_allowlist_entry()=default
std::string m_mask
Definition: gcs_xcom_networking.h:215
std::string m_addr
Definition: gcs_xcom_networking.h:214
virtual bool init_value()=0
Entry initialization.
std::string get_mask() const
Definition: gcs_xcom_networking.h:211
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:210
Gcs_ip_allowlist_entry(std::string addr, std::string mask)
Constructor.
Definition: gcs_xcom_networking.cc:492
Definition: gcs_xcom_networking.h:265
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:297
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:360
bool do_check_block(struct sockaddr_storage *sa, site_def const *xcom_config) const
Definition: gcs_xcom_networking.cc:901
Gcs_ip_allowlist & operator=(Gcs_ip_allowlist const &)
Gcs_ip_allowlist()
Definition: gcs_xcom_networking.h:306
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:987
static const std::string DEFAULT_ALLOWLIST
Definition: gcs_xcom_networking.h:267
bool do_check_block_allowlist(std::vector< unsigned char > const &incoming_octets) const
Definition: gcs_xcom_networking.cc:796
virtual ~Gcs_ip_allowlist()
Definition: gcs_xcom_networking.cc:771
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:303
bool add_address(std::string addr, std::string mask)
Definition: gcs_xcom_networking.cc:773
std::atomic_flag m_atomic_guard
An atomic lock to guard the ip allowlist.
Definition: gcs_xcom_networking.h:393
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 resolve_ip_addr_from_hostname(std::string name, std::vector< std::string > &ip)
This function translates hostname to all possible IP addresses.
Definition: gcs_xcom_networking.cc:258
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:93
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:209
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(std::string addr, 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_all_ip_addr_from_hostname(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:300
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:2879
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
case opt name
Definition: sslopt-case.h:29
Definition: gcs_xcom_networking.h:218
bool operator()(const Gcs_ip_allowlist_entry *lhs, const Gcs_ip_allowlist_entry *rhs) const
Definition: gcs_xcom_networking.h:219
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