MySQL  8.0.16
Source Code Documentation
hostname.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef HOSTNAME_INCLUDED
24 #define HOSTNAME_INCLUDED
25 
26 #include "my_config.h"
27 
28 #include <sys/types.h>
29 #include <list>
30 #include <memory>
31 
32 #include "my_inttypes.h"
33 #include "mysql_com.h"
34 
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38 
39 struct Host_errors {
40  public:
41  Host_errors();
42  ~Host_errors();
43 
44  void reset();
45  void aggregate(const Host_errors *errors);
46 
47  /** Number of connect errors. */
49 
50  /** Number of host blocked errors. */
52  /** Number of transient errors from getnameinfo(). */
54  /** Number of permanent errors from getnameinfo(). */
56  /** Number of errors from is_hostname_valid(). */
58  /** Number of transient errors from getaddrinfo(). */
60  /** Number of permanent errors from getaddrinfo(). */
62  /** Number of errors from Forward-Confirmed reverse DNS checks. */
64  /** Number of errors from host grants. */
66  /** Number of errors from missing auth plugin. */
68  /** Number of errors from auth plugin. */
70  /** Number of errors from authentication plugins. */
72  /** Number of errors from proxy user. */
74  /** Number of errors from proxy user acl. */
76  /** Number of errors from authentication. */
78  /** Number of errors from ssl. */
80  /** Number of errors from max user connection. */
82  /** Number of errors from max user connection per hour. */
84  /** Number of errors from the default database. */
86  /** Number of errors from init_connect. */
88  /** Number of errors from the server itself. */
90 
91  bool has_error() const {
92  return (
93  (m_host_blocked != 0) || (m_nameinfo_transient != 0) ||
94  (m_nameinfo_permanent != 0) || (m_format != 0) ||
95  (m_addrinfo_transient != 0) || (m_addrinfo_permanent != 0) ||
96  (m_FCrDNS != 0) || (m_host_acl != 0) || (m_no_auth_plugin != 0) ||
97  (m_auth_plugin != 0) || (m_handshake != 0) || (m_proxy_user != 0) ||
98  (m_proxy_user_acl != 0) || (m_authentication != 0) || (m_ssl != 0) ||
100  (m_default_database != 0) || (m_init_connect != 0) || (m_local != 0));
101  }
102 
104  /* Current (historical) behavior: */
106  }
107 
109 };
110 
111 /** Size of IP address string in the hash cache. */
112 #define HOST_ENTRY_KEY_SIZE INET6_ADDRSTRLEN
113 
114 /**
115  An entry in the hostname hash table cache.
116 
117  Host name cache does two things:
118  - caches host names to save DNS look ups;
119  - counts errors from IP.
120 
121  Host name can be empty (that means DNS look up failed),
122  but errors still are counted.
123 */
124 class Host_entry {
125  public:
126  /**
127  Client IP address. This is the key used with the hash table.
128 
129  The client IP address is always expressed in IPv6, even when the
130  network IPv6 stack is not present.
131 
132  This IP address is never used to connect to a socket.
133  */
135 
136  /**
137  One of the host names for the IP address. May be a zero length string.
138  */
140  /** Length in bytes of @c m_hostname. */
142  /** The hostname is validated and used for authorization. */
148  /** Error statistics. */
150 
152  if (m_first_error_seen == 0) m_first_error_seen = now;
153  m_last_error_seen = now;
154  }
155 };
156 
157 #define RC_OK 0
158 #define RC_BLOCKED_HOST 1
159 int ip_to_hostname(struct sockaddr_storage *ip_storage, const char *ip_string,
160  char **hostname, uint *connect_errors);
161 
162 void inc_host_errors(const char *ip_string, Host_errors *errors);
163 void reset_host_connect_errors(const char *ip_string);
164 bool hostname_cache_init(uint size);
165 void hostname_cache_free();
166 void hostname_cache_refresh(void);
168 void hostname_cache_resize(uint size);
169 void hostname_cache_lock();
170 void hostname_cache_unlock();
171 std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_begin();
172 std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_end();
173 
174 #endif /* HOSTNAME_INCLUDED */
ulong m_local
Number of errors from the server itself.
Definition: hostname.h:89
ulong m_max_user_connection
Number of errors from max user connection.
Definition: hostname.h:81
unsigned long long int ulonglong
Definition: my_inttypes.h:69
ulong m_nameinfo_permanent
Number of permanent errors from getnameinfo().
Definition: hostname.h:55
char ip_key[HOST_ENTRY_KEY_SIZE]
Client IP address.
Definition: hostname.h:134
char m_hostname[HOSTNAME_LENGTH+1]
One of the host names for the IP address.
Definition: hostname.h:139
ulonglong m_first_error_seen
Definition: hostname.h:146
Host_errors m_errors
Error statistics.
Definition: hostname.h:149
Some integer typedefs for easier portability.
void aggregate(const Host_errors *errors)
Definition: hostname.cc:139
ulong m_addrinfo_transient
Number of transient errors from getaddrinfo().
Definition: hostname.h:59
void clear_connect_errors()
Definition: hostname.h:108
ulong m_proxy_user
Number of errors from proxy user.
Definition: hostname.h:73
ulong m_host_acl
Number of errors from host grants.
Definition: hostname.h:65
uint hostname_cache_size()
Definition: hostname.cc:178
ulong m_FCrDNS
Number of errors from Forward-Confirmed reverse DNS checks.
Definition: hostname.h:63
bool has_error() const
Definition: hostname.h:91
Common definition between mysql server & client.
ulong m_ssl
Number of errors from ssl.
Definition: hostname.h:79
ulong m_format
Number of errors from is_hostname_valid().
Definition: hostname.h:57
void sum_connect_errors()
Definition: hostname.h:103
ulonglong m_last_seen
Definition: hostname.h:145
ulong m_init_connect
Number of errors from init_connect.
Definition: hostname.h:87
#define HOST_ENTRY_KEY_SIZE
Size of IP address string in the hash cache.
Definition: hostname.h:112
void hostname_cache_refresh(void)
Definition: hostname.cc:170
An entry in the hostname hash table cache.
Definition: hostname.h:124
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_begin()
Definition: hostname.cc:228
ulong m_max_user_connection_per_hour
Number of errors from max user connection per hour.
Definition: hostname.h:83
void inc_host_errors(const char *ip_string, Host_errors *errors)
Definition: hostname.cc:345
unsigned int uint
Definition: uca-dump.cc:29
ulong m_default_database
Number of errors from the default database.
Definition: hostname.h:85
#define HOSTNAME_LENGTH
Definition: mysql_com.h:54
void reset()
Definition: hostname.cc:115
ulong m_nameinfo_transient
Number of transient errors from getnameinfo().
Definition: hostname.h:53
ulong m_host_blocked
Number of host blocked errors.
Definition: hostname.h:51
void reset_host_connect_errors(const char *ip_string)
Definition: hostname.cc:367
bool m_host_validated
The hostname is validated and used for authorization.
Definition: hostname.h:143
ulonglong m_first_seen
Definition: hostname.h:144
ulong m_authentication
Number of errors from authentication.
Definition: hostname.h:77
void set_error_timestamps(ulonglong now)
Definition: hostname.h:151
Host_errors()
Definition: hostname.cc:90
void hostname_cache_free()
Definition: hostname.cc:206
ulong m_connect
Number of connect errors.
Definition: hostname.h:48
~Host_errors()
Definition: hostname.cc:113
ulong m_handshake
Number of errors from authentication plugins.
Definition: hostname.h:71
void hostname_cache_unlock()
Definition: hostname.cc:223
ulonglong m_last_error_seen
Definition: hostname.h:147
ulong m_addrinfo_permanent
Number of permanent errors from getaddrinfo().
Definition: hostname.h:61
ulong m_no_auth_plugin
Number of errors from missing auth plugin.
Definition: hostname.h:67
void hostname_cache_resize(uint size)
Definition: hostname.cc:183
void hostname_cache_lock()
Definition: hostname.cc:218
ulong m_proxy_user_acl
Number of errors from proxy user acl.
Definition: hostname.h:75
Definition: hostname.h:39
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.cc:441
unsigned long ulong
Definition: my_inttypes.h:46
ulong m_auth_plugin
Number of errors from auth plugin.
Definition: hostname.h:69
uint m_hostname_length
Length in bytes of m_hostname.
Definition: hostname.h:141
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_end()
Definition: hostname.cc:233
bool hostname_cache_init(uint size)
Definition: hostname.cc:192