MySQL 8.0.39
Source Code Documentation
hostname_cache.cc File Reference

Get hostname for an IP address. More...

#include "sql/hostname_cache.h"
#include "my_config.h"
#include "map_helpers.h"
#include "mutex_lock.h"
#include "my_loglevel.h"
#include "my_psi_config.h"
#include "my_systime.h"
#include "mysql/components/services/bits/mysql_mutex_bits.h"
#include "mysql/components/services/bits/psi_mutex_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "thr_mutex.h"
#include <netdb.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <list>
#include <memory>
#include <new>
#include <string>
#include <unordered_map>
#include <utility>
#include "m_ctype.h"
#include "m_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_sys.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_mysql_alloc.h"
#include "mysqld_error.h"
#include "sql/log.h"
#include "sql/mysqld.h"
#include "sql/psi_memory_key.h"
#include "violite.h"
#include <arpa/inet.h>

Functions

void hostname_cache_refresh ()
 
uint hostname_cache_size ()
 
void hostname_cache_resize (uint size)
 
bool hostname_cache_init (uint size)
 
void hostname_cache_free ()
 
void hostname_cache_lock ()
 
void hostname_cache_unlock ()
 
list< unique_ptr< Host_entry > >::iterator hostname_cache_begin ()
 
list< unique_ptr< Host_entry > >::iterator hostname_cache_end ()
 
static Host_entryhostname_cache_search (const char *ip_string)
 
static void add_hostname_impl (const char *ip_string, const char *hostname, bool validated, Host_errors *errors, ulonglong now)
 
static void add_hostname (const char *ip_string, const char *hostname, bool validated, Host_errors *errors)
 
void inc_host_errors (const char *ip_string, Host_errors *errors)
 
void reset_host_connect_errors (const char *ip_string)
 
static bool is_ip_loopback (const struct sockaddr *ip)
 
static bool is_hostname_valid (const char *hostname)
 
int ip_to_hostname (struct sockaddr_storage *ip_storage, const char *ip_string, char **hostname, uint *connect_errors)
 Resolve IP-address to host name. More...
 

Variables

PSI_mutex_key key_hash_filo_lock
 
static size_t hostname_cache_max_size
 
static list< unique_ptr< Host_entry > > * hostname_cache_lru
 
static malloc_unordered_map< string, list< unique_ptr< Host_entry > >::iterator > * hostname_cache_by_ip
 
static mysql_mutex_t hostname_cache_mutex
 
static bool hostname_cache_mutex_inited = false
 

Detailed Description

Get hostname for an IP address.

Hostnames are checked with reverse name lookup and checked that they doesn't resemble an IP address.

Function Documentation

◆ add_hostname()

static void add_hostname ( const char *  ip_string,
const char *  hostname,
bool  validated,
Host_errors errors 
)
static

◆ add_hostname_impl()

static void add_hostname_impl ( const char *  ip_string,
const char *  hostname,
bool  validated,
Host_errors errors,
ulonglong  now 
)
static

◆ hostname_cache_begin()

list< unique_ptr< Host_entry > >::iterator hostname_cache_begin ( )

◆ hostname_cache_end()

list< unique_ptr< Host_entry > >::iterator hostname_cache_end ( )

◆ hostname_cache_free()

void hostname_cache_free ( )

◆ hostname_cache_init()

bool hostname_cache_init ( uint  size)

◆ hostname_cache_lock()

void hostname_cache_lock ( )

◆ hostname_cache_refresh()

void hostname_cache_refresh ( void  )

◆ hostname_cache_resize()

void hostname_cache_resize ( uint  size)

◆ hostname_cache_search()

static Host_entry * hostname_cache_search ( const char *  ip_string)
inlinestatic

◆ hostname_cache_size()

uint hostname_cache_size ( )

◆ hostname_cache_unlock()

void hostname_cache_unlock ( )

◆ inc_host_errors()

void inc_host_errors ( const char *  ip_string,
Host_errors errors 
)

◆ ip_to_hostname()

int ip_to_hostname ( struct sockaddr_storage *  ip_storage,
const char *  ip_string,
char **  hostname,
uint connect_errors 
)

Resolve IP-address to host name.

This function does the following things:

  • resolves IP-address;
  • employs Forward Confirmed Reverse DNS technique to validate IP-address;
  • returns host name if IP-address is validated;
  • set value to out-variable connect_errors – this variable represents the number of connection errors from the specified IP-address.
  • update the host_cache statistics

NOTE: connect_errors are counted (are supported) only for the clients where IP-address can be resolved and FCrDNS check is passed.

Parameters
[in]ip_storageIP address (sockaddr). Must be set.
[in]ip_stringIP address (string). Must be set.
[out]hostnameHostname if IP-address is valid.
[out]connect_errorsRepresents number of connection errors.
Returns
Error status
Return values
0Success
RC_BLOCKED_HOSTThe host is blocked.
RC_LONG_HOSTNAMEThe hostname is longer than HOSTNAME_LENGTH.

The function does not set/report MySQL server error in case of failure. It's caller's responsibility to handle failures of this function properly.

◆ is_hostname_valid()

static bool is_hostname_valid ( const char *  hostname)
inlinestatic

◆ is_ip_loopback()

static bool is_ip_loopback ( const struct sockaddr ip)
inlinestatic

◆ reset_host_connect_errors()

void reset_host_connect_errors ( const char *  ip_string)

Variable Documentation

◆ hostname_cache_by_ip

malloc_unordered_map<string, list<unique_ptr<Host_entry> >::iterator>* hostname_cache_by_ip
static

◆ hostname_cache_lru

list<unique_ptr<Host_entry> >* hostname_cache_lru
static

◆ hostname_cache_max_size

size_t hostname_cache_max_size
static

◆ hostname_cache_mutex

mysql_mutex_t hostname_cache_mutex
static

◆ hostname_cache_mutex_inited

bool hostname_cache_mutex_inited = false
static

◆ key_hash_filo_lock

PSI_mutex_key key_hash_filo_lock
extern