MySQL 9.6.0
Source Code Documentation
hostname_cache.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2025, 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 HOSTNAME_CACHE_INCLUDED
25#define HOSTNAME_CACHE_INCLUDED
26
27#include "my_config.h"
28
29#include <sys/types.h>
30#include <list>
31#include <memory>
32
33#include "my_hostname.h"
34#include "my_inttypes.h"
35#include "mysql_com.h"
36
37#ifdef HAVE_NETINET_IN_H
38#include <netinet/in.h>
39#endif
40
42 public:
44 ~Host_errors() = default;
45
46 void reset();
47 void aggregate(const Host_errors *errors);
48
49 /** Number of connect errors. */
50 ulong m_connect;
51
52 /** Number of host blocked errors. */
54 /** Number of transient errors from getnameinfo(). */
56 /** Number of permanent errors from getnameinfo(). */
58 /** Number of errors from is_hostname_valid(). */
59 ulong m_format;
60 /** Number of transient errors from getaddrinfo(). */
62 /** Number of permanent errors from getaddrinfo(). */
64 /** Number of errors from Forward-Confirmed reverse DNS checks. */
65 ulong m_FCrDNS;
66 /** Number of errors from host grants. */
68 /** Number of errors from missing auth plugin. */
70 /** Number of errors from auth plugin. */
72 /** Number of errors from authentication plugins. */
74 /** Number of errors from proxy user. */
76 /** Number of errors from proxy user acl. */
78 /** Number of errors from authentication. */
80 /** Number of errors from ssl. */
81 ulong m_ssl;
82 /** Number of errors from max user connection. */
84 /** Number of errors from max user connection per hour. */
86 /** Number of errors from the default database. */
88 /** Number of errors from init_connect. */
90 /** Number of errors from the server itself. */
91 ulong m_local;
92 /** Number of account locked errors. */
94 /** Number of temporary account locked errors. */
96
97 bool has_error() const {
98 return (
99 (m_host_blocked != 0) || (m_nameinfo_transient != 0) ||
100 (m_nameinfo_permanent != 0) || (m_format != 0) ||
102 (m_FCrDNS != 0) || (m_host_acl != 0) || (m_no_auth_plugin != 0) ||
103 (m_auth_plugin != 0) || (m_handshake != 0) || (m_proxy_user != 0) ||
104 (m_proxy_user_acl != 0) || (m_authentication != 0) || (m_ssl != 0) ||
106 (m_default_database != 0) || (m_init_connect != 0) || (m_local != 0) ||
108 }
109
111 /* Current (historical) behavior: */
113 }
114
116};
117
118/** Size of IP address string in the hash cache. */
119#define HOST_ENTRY_KEY_SIZE INET6_ADDRSTRLEN
120
121/**
122 An entry in the hostname hash table cache.
123
124 Host name cache does two things:
125 - caches host names to save DNS look ups;
126 - counts errors from IP.
127
128 Host name can be empty (that means DNS look up failed),
129 but errors still are counted.
130*/
132 public:
133 /**
134 Client IP address. This is the key used with the hash table.
135
136 The client IP address is always expressed in IPv6, even when the
137 network IPv6 stack is not present.
138
139 This IP address is never used to connect to a socket.
140 */
142
143 /**
144 One of the host names for the IP address. May be a zero length string.
145 */
147 /** Length in bytes of @c m_hostname. */
149 /** The hostname is validated and used for authorization. */
155 /** Error statistics. */
157
160 m_last_error_seen = now;
161 }
162};
163
164#define RC_OK 0
165#define RC_BLOCKED_HOST 1
166#define RC_LONG_HOSTNAME 2
167int ip_to_hostname(struct sockaddr_storage *ip_storage, const char *ip_string,
168 char **hostname, uint *connect_errors);
169
170void inc_host_errors(const char *ip_string, Host_errors *errors);
171void reset_host_connect_errors(const char *ip_string);
172bool hostname_cache_init(uint size);
174void hostname_cache_refresh(void);
176void hostname_cache_resize(uint size);
179std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_begin();
180std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_end();
181
182#endif /* HOSTNAME_CACHE_INCLUDED */
An entry in the hostname hash table cache.
Definition: hostname_cache.h:131
bool m_host_validated
The hostname is validated and used for authorization.
Definition: hostname_cache.h:150
uint m_hostname_length
Length in bytes of m_hostname.
Definition: hostname_cache.h:148
ulonglong m_last_error_seen
Definition: hostname_cache.h:154
Host_errors m_errors
Error statistics.
Definition: hostname_cache.h:156
ulonglong m_first_error_seen
Definition: hostname_cache.h:153
char ip_key[HOST_ENTRY_KEY_SIZE]
Client IP address.
Definition: hostname_cache.h:141
ulonglong m_first_seen
Definition: hostname_cache.h:151
char m_hostname[HOSTNAME_LENGTH+1]
One of the host names for the IP address.
Definition: hostname_cache.h:146
ulonglong m_last_seen
Definition: hostname_cache.h:152
void set_error_timestamps(ulonglong now)
Definition: hostname_cache.h:158
int ip_to_hostname(struct sockaddr_storage *ip_storage, const char *ip_string, char **hostname, uint *connect_errors)
Resolve IP-address to host name.
Definition: hostname_cache.cc:440
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_begin()
Definition: hostname_cache.cc:229
void reset_host_connect_errors(const char *ip_string)
Definition: hostname_cache.cc:363
void inc_host_errors(const char *ip_string, Host_errors *errors)
Definition: hostname_cache.cc:341
void hostname_cache_free()
Definition: hostname_cache.cc:207
bool hostname_cache_init(uint size)
Definition: hostname_cache.cc:193
#define HOST_ENTRY_KEY_SIZE
Size of IP address string in the hash cache.
Definition: hostname_cache.h:119
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_end()
Definition: hostname_cache.cc:234
void hostname_cache_refresh(void)
Definition: hostname_cache.cc:171
uint hostname_cache_size()
Definition: hostname_cache.cc:179
void hostname_cache_resize(uint size)
Definition: hostname_cache.cc:184
void hostname_cache_unlock()
Definition: hostname_cache.cc:224
void hostname_cache_lock()
Definition: hostname_cache.cc:219
Common definition used by mysys, performance schema and server & client.
static constexpr int HOSTNAME_LENGTH
Definition: my_hostname.h:43
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Common definition between mysql server & client.
size_t size(const char *const c)
Definition: base64.h:46
Definition: hostname_cache.h:41
ulong m_host_blocked
Number of host blocked errors.
Definition: hostname_cache.h:53
ulong m_authentication
Number of errors from authentication.
Definition: hostname_cache.h:79
void clear_connect_errors()
Definition: hostname_cache.h:115
ulong m_handshake
Number of errors from authentication plugins.
Definition: hostname_cache.h:73
ulong m_temporary_account_locked
Number of temporary account locked errors.
Definition: hostname_cache.h:95
ulong m_max_user_connection_per_hour
Number of errors from max user connection per hour.
Definition: hostname_cache.h:85
bool has_error() const
Definition: hostname_cache.h:97
ulong m_account_locked
Number of account locked errors.
Definition: hostname_cache.h:93
ulong m_default_database
Number of errors from the default database.
Definition: hostname_cache.h:87
void reset()
Definition: hostname_cache.cc:112
void sum_connect_errors()
Definition: hostname_cache.h:110
~Host_errors()=default
ulong m_nameinfo_transient
Number of transient errors from getnameinfo().
Definition: hostname_cache.h:55
void aggregate(const Host_errors *errors)
Definition: hostname_cache.cc:138
ulong m_init_connect
Number of errors from init_connect.
Definition: hostname_cache.h:89
ulong m_connect
Number of connect errors.
Definition: hostname_cache.h:50
ulong m_nameinfo_permanent
Number of permanent errors from getnameinfo().
Definition: hostname_cache.h:57
ulong m_FCrDNS
Number of errors from Forward-Confirmed reverse DNS checks.
Definition: hostname_cache.h:65
Host_errors()
Definition: hostname_cache.cc:87
ulong m_max_user_connection
Number of errors from max user connection.
Definition: hostname_cache.h:83
ulong m_no_auth_plugin
Number of errors from missing auth plugin.
Definition: hostname_cache.h:69
ulong m_proxy_user
Number of errors from proxy user.
Definition: hostname_cache.h:75
ulong m_proxy_user_acl
Number of errors from proxy user acl.
Definition: hostname_cache.h:77
ulong m_host_acl
Number of errors from host grants.
Definition: hostname_cache.h:67
ulong m_local
Number of errors from the server itself.
Definition: hostname_cache.h:91
ulong m_addrinfo_permanent
Number of permanent errors from getaddrinfo().
Definition: hostname_cache.h:63
ulong m_addrinfo_transient
Number of transient errors from getaddrinfo().
Definition: hostname_cache.h:61
ulong m_ssl
Number of errors from ssl.
Definition: hostname_cache.h:81
ulong m_format
Number of errors from is_hostname_valid().
Definition: hostname_cache.h:59
ulong m_auth_plugin
Number of errors from auth plugin.
Definition: hostname_cache.h:71