MySQL 8.0.39
Source Code Documentation
viosocket.cc File Reference
#include "my_config.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_macros.h"
#include "mysys_err.h"
#include "template_utils.h"
#include "vio/vio_priv.h"
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/ioctl.h>
#include "mysql/psi/mysql_socket.h"

Macros

#define VIO_DONTWAIT   0
 
#define VIO_UNBUFFERED_READ_MIN_SIZE   2048
 
#define SOCKET_PRINTF_FORMAT   "%d"
 
#define POLLRDHUP   0
 Set of event flags grouped by operations. More...
 
#define MY_POLL_SET_IN   (POLLIN | POLLPRI)
 
#define MY_POLL_SET_OUT   (POLLOUT)
 
#define MY_POLL_SET_ERR   (POLLERR | POLLHUP | POLLNVAL)
 

Functions

int vio_errno (Vio *vio)
 
int vio_socket_io_wait (Vio *vio, enum enum_vio_io_event event)
 Attempt to wait for an I/O event on a socket. More...
 
size_t vio_read (Vio *vio, uchar *buf, size_t size)
 
size_t vio_read_buff (Vio *vio, uchar *buf, size_t size)
 
bool vio_buff_has_data (Vio *vio)
 
size_t vio_write (Vio *vio, const uchar *buf, size_t size)
 
int vio_set_blocking (Vio *vio, bool status)
 
int vio_set_blocking_flag (Vio *vio, bool status)
 
bool vio_is_blocking (Vio *vio)
 
int vio_socket_timeout (Vio *vio, uint which, bool old_mode)
 
int vio_fastsend (Vio *vio)
 
int vio_keepalive (Vio *vio, bool set_keep_alive)
 
bool vio_should_retry (Vio *vio)
 Indicate whether a I/O operation must be retried later. More...
 
bool vio_was_timeout (Vio *vio)
 Indicate whether a I/O operation timed out. More...
 
static void vio_wait_until_woken (Vio *vio)
 
int vio_shutdown (Vio *vio)
 
void vio_description (Vio *vio, char *buf)
 
enum enum_vio_type vio_type (const Vio *vio)
 
my_socket vio_fd (Vio *vio)
 
static void vio_get_normalized_ip (const struct sockaddr *src, size_t src_length, struct sockaddr *dst, size_t *dst_length)
 Convert a sock-address (AF_INET or AF_INET6) into the "normalized" form, which is the IPv4 form for IPv4-mapped or IPv4-compatible IPv6 addresses. More...
 
bool vio_get_normalized_ip_string (const struct sockaddr *addr, size_t addr_length, char *ip_string, size_t ip_string_size)
 Return the normalized IP address string for a sock-address. More...
 
bool vio_peer_addr (Vio *vio, char *ip_buffer, uint16 *port, size_t ip_buffer_size)
 Return IP address and port of a VIO client socket. More...
 
static bool socket_peek_read (Vio *vio, uint *bytes)
 Retrieve the amount of data that can be read from a socket. More...
 
int vio_io_wait (Vio *vio, enum enum_vio_io_event event, int timeout)
 Wait for an I/O event on a VIO socket. More...
 
bool vio_socket_connect (Vio *vio, struct sockaddr *addr, socklen_t len, bool nonblocking, int timeout, bool *connect_done)
 Connect to a peer address. More...
 
bool vio_is_connected (Vio *vio)
 Determine if the endpoint of a connection is still available. More...
 
ssize_t vio_pending (Vio *vio)
 Number of bytes in the read or socket buffer. More...
 
bool vio_is_no_name_error (int err_code)
 Checks if the error code, returned by vio_getnameinfo(), means it was the "No-name" error. More...
 
int vio_getnameinfo (const struct sockaddr *sa, char *hostname, size_t hostname_size, char *port, size_t port_size, int flags)
 This is a wrapper for the system getnameinfo(), because different OS differ in the getnameinfo() implementation: More...
 

Macro Definition Documentation

◆ MY_POLL_SET_ERR

#define MY_POLL_SET_ERR   (POLLERR | POLLHUP | POLLNVAL)

◆ MY_POLL_SET_IN

#define MY_POLL_SET_IN   (POLLIN | POLLPRI)

◆ MY_POLL_SET_OUT

#define MY_POLL_SET_OUT   (POLLOUT)

◆ POLLRDHUP

#define POLLRDHUP   0

Set of event flags grouped by operations.

◆ SOCKET_PRINTF_FORMAT

#define SOCKET_PRINTF_FORMAT   "%d"

◆ VIO_DONTWAIT

#define VIO_DONTWAIT   0

◆ VIO_UNBUFFERED_READ_MIN_SIZE

#define VIO_UNBUFFERED_READ_MIN_SIZE   2048

Function Documentation

◆ socket_peek_read()

static bool socket_peek_read ( Vio vio,
uint bytes 
)
static

Retrieve the amount of data that can be read from a socket.

Parameters
vioA VIO object.
[out]bytesThe amount of bytes available.
Return values
falseSuccess.
trueFailure.

◆ vio_buff_has_data()

bool vio_buff_has_data ( Vio vio)

◆ vio_description()

void vio_description ( Vio vio,
char *  buf 
)

◆ vio_errno()

int vio_errno ( Vio vio)

◆ vio_fastsend()

int vio_fastsend ( Vio vio)

◆ vio_fd()

my_socket vio_fd ( Vio vio)

◆ vio_get_normalized_ip()

static void vio_get_normalized_ip ( const struct sockaddr src,
size_t  src_length,
struct sockaddr dst,
size_t *  dst_length 
)
static

Convert a sock-address (AF_INET or AF_INET6) into the "normalized" form, which is the IPv4 form for IPv4-mapped or IPv4-compatible IPv6 addresses.

Note
Background: when IPv4 and IPv6 are used simultaneously, IPv4 addresses may be written in a form of IPv4-mapped or IPv4-compatible IPv6 addresses. That means, one address (a.b.c.d) can be written in three forms:
  • IPv4: a.b.c.d;
  • IPv4-compatible IPv6:
    ::a.b.c.d
    ;
  • IPv4-mapped IPv4:
    ::ffff:a.b.c.d
    ;

Having three forms of one address makes it a little difficult to compare addresses with each other (the IPv4-compatible IPv6-address of foo.bar will be different from the IPv4-mapped IPv6-address of foo.bar).

Note
This function can be made public when it's needed.
Parameters
[in]srcsource IP address (AF_INET or AF_INET6).
[in]src_lengthlength of the src.
[out]dsta buffer to store normalized IP address (sockaddr_storage).
[out]dst_lengthactual length of the normalized IP address.

◆ vio_get_normalized_ip_string()

bool vio_get_normalized_ip_string ( const struct sockaddr addr,
size_t  addr_length,
char *  ip_string,
size_t  ip_string_size 
)

Return the normalized IP address string for a sock-address.

The idea is to return an IPv4-address for an IPv4-mapped and IPv4-compatible IPv6 address.

The function writes the normalized IP address to the given buffer. The buffer should have enough space, otherwise error flag is returned. The system constant INET6_ADDRSTRLEN can be used to reserve buffers of the right size.

Parameters
[in]addrsockaddr object (AF_INET or AF_INET6).
[in]addr_lengthlength of the addr.
[out]ip_stringbuffer to write normalized IP address.
[in]ip_string_sizesize of the ip_string.
Returns
Error status.
Return values
truein case of error (the ip_string buffer is not enough).
falseon success.

◆ vio_getnameinfo()

int vio_getnameinfo ( const struct sockaddr sa,
char *  hostname,
size_t  hostname_size,
char *  port,
size_t  port_size,
int  flags 
)

This is a wrapper for the system getnameinfo(), because different OS differ in the getnameinfo() implementation:

  • Solaris 10 requires that the 2nd argument (salen) must match the actual size of the struct sockaddr_storage passed to it;
  • Mac OS X has sockaddr_in::sin_len and sockaddr_in6::sin6_len and requires them to be filled.

◆ vio_io_wait()

int vio_io_wait ( Vio vio,
enum enum_vio_io_event  event,
int  timeout 
)

Wait for an I/O event on a VIO socket.

Parameters
vioVIO object representing a connected socket.
eventThe type of I/O event to wait for.
timeoutInterval (in milliseconds) to wait for an I/O event. A negative timeout value means an infinite timeout.
Remarks
sock_errno is set to SOCKET_ETIMEDOUT on timeout.
Returns
A three-state value which indicates the operation status.
Return values
-1Failure, socket_errno indicates the error.
0The wait has timed out.
1The requested I/O event has occurred.

◆ vio_is_blocking()

bool vio_is_blocking ( Vio vio)

◆ vio_is_connected()

bool vio_is_connected ( Vio vio)

Determine if the endpoint of a connection is still available.

Remarks
The socket is assumed to be disconnected if an EOF condition is encountered.
Parameters
vioThe VIO object.
Return values
trueEOF condition not found.
falseEOF condition is signaled.

◆ vio_is_no_name_error()

bool vio_is_no_name_error ( int  err_code)

Checks if the error code, returned by vio_getnameinfo(), means it was the "No-name" error.

Windows-specific note: getnameinfo() returns WSANO_DATA instead of EAI_NODATA or EAI_NONAME when no reverse mapping is available at the host (i.e. Windows can't get hostname by IP-address). This error should be treated as EAI_NONAME.

Returns
if the error code is actually EAI_NONAME.
Return values
trueif the error code is EAI_NONAME.
falseotherwise.

◆ vio_keepalive()

int vio_keepalive ( Vio vio,
bool  set_keep_alive 
)

◆ vio_peer_addr()

bool vio_peer_addr ( Vio vio,
char *  ip_buffer,
uint16 port,
size_t  ip_buffer_size 
)

Return IP address and port of a VIO client socket.

The function returns an IPv4 address if IPv6 support is disabled.

The function returns an IPv4 address if the client socket is associated with an IPv4-compatible or IPv4-mapped IPv6 address. Otherwise, the native IPv6 address is returned.

◆ vio_pending()

ssize_t vio_pending ( Vio vio)

Number of bytes in the read or socket buffer.

Remarks
An EOF condition might count as one readable byte.
Returns
number of bytes in one of the buffers or < 0 if error.

◆ vio_read()

size_t vio_read ( Vio vio,
uchar buf,
size_t  size 
)

◆ vio_read_buff()

size_t vio_read_buff ( Vio vio,
uchar buf,
size_t  size 
)

◆ vio_set_blocking()

int vio_set_blocking ( Vio vio,
bool  status 
)

◆ vio_set_blocking_flag()

int vio_set_blocking_flag ( Vio vio,
bool  status 
)

◆ vio_should_retry()

bool vio_should_retry ( Vio vio)

Indicate whether a I/O operation must be retried later.

Parameters
vioA VIO object
Returns
Whether a I/O operation should be deferred.
Return values
trueTemporary failure, retry operation.
falseIndeterminate failure.

◆ vio_shutdown()

int vio_shutdown ( Vio vio)

◆ vio_socket_connect()

bool vio_socket_connect ( Vio vio,
struct sockaddr addr,
socklen_t  len,
bool  nonblocking,
int  timeout,
bool *  connect_done 
)

Connect to a peer address.

Parameters
vioA VIO object.
addrSocket address containing the peer address.
lenLength of socket address.
nonblockingflag to represent if socket is blocking or nonblocking
timeoutInterval (in milliseconds) to wait until a connection is established.
[out]connect_doneIndication if connect actually completed or not. If set to true this means there's no need to wait for connect to complete anymore.
Return values
falseA connection was successfully established.
trueA fatal error. See socket_errno.

◆ vio_socket_io_wait()

int vio_socket_io_wait ( Vio vio,
enum enum_vio_io_event  event 
)

Attempt to wait for an I/O event on a socket.

Parameters
vioVIO object representing a connected socket.
eventThe type of I/O event (read or write) to wait for.
Returns
Return value is -1 on failure, 0 on success.

◆ vio_socket_timeout()

int vio_socket_timeout ( Vio vio,
uint  which,
bool  old_mode 
)

◆ vio_type()

enum enum_vio_type vio_type ( const Vio vio)

◆ vio_wait_until_woken()

static void vio_wait_until_woken ( Vio vio)
static

◆ vio_was_timeout()

bool vio_was_timeout ( Vio vio)

Indicate whether a I/O operation timed out.

Parameters
vioA VIO object
Returns
Whether a I/O operation timed out.
Return values
trueOperation timed out.
falseNot a timeout failure.

◆ vio_write()

size_t vio_write ( Vio vio,
const uchar buf,
size_t  size 
)