MySQL  8.0.22
Source Code Documentation
net_serv.cc File Reference

This file is the net layer API for the MySQL client/server protocol. More...

#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <mysql/components/services/log_builtins.h>
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_sys.h"
#include "mysql.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql_async.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "violite.h"
#include "sql/psi_memory_key.h"
#include "mysql_com_server.h"

Functions

void thd_increment_bytes_sent (size_t length)
 
void thd_increment_bytes_received (size_t length)
 
static bool net_write_buff (NET *net, const uchar *packet, size_t len)
 Caching the data in a local buffer before sending it. More...
 
static ucharcompress_packet (NET *net, const uchar *packet, size_t *length)
 Compress and encapsulate a packet into a compressed packet. More...
 
NET_EXTENSIONnet_extension_init ()
 
void net_extension_free (NET *net)
 
static mysql_compress_contextcompress_context (NET *net)
 Returns the appropiate compression_context based on caller. More...
 
bool my_net_init (NET *net, Vio *vio)
 Init with packet info. More...
 
void net_end (NET *net)
 
void net_claim_memory_ownership (NET *net, bool claim)
 
bool net_realloc (NET *net, size_t length)
 Realloc the packet buffer. More...
 
void net_clear (NET *net, bool check_buffer)
 Clear (reinitialize) the NET structure for a new command. More...
 
bool net_flush (NET *net)
 Flush write_buffer if not empty. More...
 
static bool net_should_retry (NET *net, uint *retry_count)
 Whether a I/O operation should be retried later. More...
 
bool my_net_write (NET *net, const uchar *packet, size_t len)
 Write a logical packet with packet header. More...
 
static void reset_packet_write_state (NET *net)
 
static int begin_packet_write_state (NET *net, uchar command, const uchar *packet, size_t packet_len, const uchar *optional_prefix, size_t prefix_len)
 
static net_async_status net_write_vector_nonblocking (NET *net, ssize_t *res)
 
net_async_status net_write_command_nonblocking (NET *net, uchar command, const uchar *prefix, size_t prefix_len, const uchar *packet, size_t packet_len, bool *res)
 Send a command to the server in asynchronous way. More...
 
net_async_status my_net_write_nonblocking (NET *net, const uchar *packet, size_t len, bool *res)
 
bool net_write_command (NET *net, uchar command, const uchar *header, size_t head_len, const uchar *packet, size_t len)
 Send a command to the server. More...
 
static bool net_write_raw_loop (NET *net, const uchar *buf, size_t count)
 Write a determined number of bytes to a network handler. More...
 
bool net_write_packet (NET *net, const uchar *packet, size_t length)
 Write a MySQL protocol packet to the network handler. More...
 
static bool net_read_raw_loop (NET *net, size_t count)
 Read a determined number of bytes from a network handler. More...
 
static bool net_read_packet_header (NET *net)
 Read the header of a packet. More...
 
static ulong net_read_available (NET *net, size_t count)
 
static net_async_status net_read_data_nonblocking (NET *net, size_t count, bool *err_ptr)
 
static net_async_status net_read_packet_header_nonblocking (NET *net, bool *err_ptr)
 
static net_async_status net_read_packet_nonblocking (NET *net, ulong *ret)
 
static void net_read_init_offsets (NET *net, size_t &start_of_packet, size_t &first_packet_offset, uint &multi_byte_packet, size_t &buf_length)
 Initialize the offsets. More...
 
static bool net_read_process_buffer (NET *net, size_t &start_of_packet, size_t &buf_length, uint &multi_byte_packet, size_t &first_packet_offset)
 Read the payload length in the packet, if it is found to multipacket omit the packet header of the subsequent packets. More...
 
static ulong net_read_update_offsets (NET *net, size_t start_of_packet, size_t first_packet_offset, size_t buf_length, uint multi_byte_packet)
 Update the offsets in the NET structure after reading the current packet. More...
 
static net_async_status net_read_compressed_nonblocking (NET *net, ulong *len_ptr)
 
static net_async_status net_read_uncompressed_nonblocking (NET *net, ulong *len_ptr)
 Read the usual packet that is not compressed asynchronously. More...
 
static size_t net_read_packet (NET *net, size_t *complen)
 Read one (variable-length) MySQL protocol packet. More...
 
net_async_status my_net_read_nonblocking (NET *net, ulong *len_ptr)
 
static void net_read_uncompressed_packet (NET *net, size_t &len)
 Reads one packet to net->buff + net->where_b. More...
 
static void net_read_compressed_packet (NET *net, size_t &len)
 
ulong my_net_read (NET *net)
 Read a packet from the client/server and return it without the internal package header. More...
 
void my_net_set_read_timeout (NET *net, uint timeout)
 
void my_net_set_write_timeout (NET *net, uint timeout)
 
void my_net_set_retry_count (NET *net, uint retry_count)
 

Detailed Description

This file is the net layer API for the MySQL client/server protocol.

Write and read of logical packets to/from socket.

Writes are cached into net_buffer_length big packets. Read packets are reallocated dynamicly when reading big packets. Each logical packet has the following pre-info: 3 byte length & 1 byte package-number.

Function Documentation

◆ begin_packet_write_state()

static int begin_packet_write_state ( NET net,
uchar  command,
const uchar packet,
size_t  packet_len,
const uchar optional_prefix,
size_t  prefix_len 
)
static

◆ compress_context()

static mysql_compress_context* compress_context ( NET net)
static

Returns the appropiate compression_context based on caller.

If the caller is server then fetch is fromthe server extension structure.

Parameters
net[in] NET structure
Returns
Return values
mysql_compress_contextstructure pointer

◆ compress_packet()

static uchar * compress_packet ( NET net,
const uchar packet,
size_t *  length 
)
static

Compress and encapsulate a packet into a compressed packet.

Parameters
netNET handler.
packetThe packet to compress.
[in,out]lengthLength of the packet.

See Compressed Packet Header for a description of the header structure.

Returns
Pointer to the (new) compressed packet.

◆ my_net_init()

bool my_net_init ( NET net,
Vio vio 
)

Init with packet info.

◆ my_net_read()

ulong my_net_read ( NET net)

Read a packet from the client/server and return it without the internal package header.

If the packet is the first packet of a multi-packet packet (which is indicated by the length of the packet = 0xffffff) then all sub packets are read and concatenated.

If the packet was compressed, its uncompressed and the length of the uncompressed packet is returned.

Returns
The function returns the length of the found packet or packet_error. net->read_pos points to the read data.

◆ my_net_read_nonblocking()

net_async_status my_net_read_nonblocking ( NET net,
ulong *  len_ptr 
)

◆ my_net_set_read_timeout()

void my_net_set_read_timeout ( NET net,
uint  timeout 
)

◆ my_net_set_retry_count()

void my_net_set_retry_count ( NET net,
uint  retry_count 
)

◆ my_net_set_write_timeout()

void my_net_set_write_timeout ( NET net,
uint  timeout 
)

◆ my_net_write()

bool my_net_write ( NET net,
const uchar packet,
size_t  len 
)

Write a logical packet with packet header.

Format: Packet length (3 bytes), packet number (1 byte) When compression is used, a 3 byte compression length is added.

Note
If compression is used, the original packet is modified!

◆ my_net_write_nonblocking()

net_async_status my_net_write_nonblocking ( NET net,
const uchar packet,
size_t  len,
bool res 
)

◆ net_claim_memory_ownership()

void net_claim_memory_ownership ( NET net,
bool  claim 
)

◆ net_clear()

void net_clear ( NET net,
bool  check_buffer 
)

Clear (reinitialize) the NET structure for a new command.

Remarks
Performs debug checking of the socket buffer to ensure that the protocol sequence is correct.
Parameters
netNET handler
check_bufferWhether to check the socket buffer.

◆ net_end()

void net_end ( NET net)

◆ net_extension_free()

void net_extension_free ( NET net)

◆ net_extension_init()

NET_EXTENSION* net_extension_init ( )

◆ net_flush()

bool net_flush ( NET net)

Flush write_buffer if not empty.

◆ net_read_available()

static ulong net_read_available ( NET net,
size_t  count 
)
static

◆ net_read_compressed_nonblocking()

static net_async_status net_read_compressed_nonblocking ( NET net,
ulong *  len_ptr 
)
static

◆ net_read_compressed_packet()

static void net_read_compressed_packet ( NET net,
size_t &  len 
)
static

◆ net_read_data_nonblocking()

static net_async_status net_read_data_nonblocking ( NET net,
size_t  count,
bool err_ptr 
)
static

◆ net_read_init_offsets()

static void net_read_init_offsets ( NET net,
size_t &  start_of_packet,
size_t &  first_packet_offset,
uint multi_byte_packet,
size_t &  buf_length 
)
static

Initialize the offsets.

If it is first packet then initialize the offsets from the beginning else initialize the offsets after the data last read in the old packet.

Parameters
[in,out]netNET structure.
[in,out]start_of_packetStarting offset of the packet to be read. In multipackets, points to the next header that will be erased.
[in,out]first_packet_offsetStarting offset of the packet to read.
[in,out]multi_byte_packetFlag that indicate if packet is multibyte
[in,out]buf_lengthEnd of readable byte.

◆ net_read_packet()

static size_t net_read_packet ( NET net,
size_t *  complen 
)
static

Read one (variable-length) MySQL protocol packet.

A MySQL packet consists of a header and a payload.

Remarks
Reads one packet to net->buff + net->where_b.
Long packets are handled by my_net_read().
The network buffer is expanded if necessary.
Returns
The length of the packet, or packet_error on error.

◆ net_read_packet_header()

static bool net_read_packet_header ( NET net)
static

Read the header of a packet.

The MySQL protocol packet header consists of the length, in bytes, of the payload (packet data) and a serial number.

Remarks
The encoded length is the length of the packet payload, which does not include the packet header.
The serial number is used to ensure that the packets are received in order. If the packet serial number does not match the expected value, a error is returned.
Parameters
netNET handler.
Returns
true on error, false on success.

◆ net_read_packet_header_nonblocking()

static net_async_status net_read_packet_header_nonblocking ( NET net,
bool err_ptr 
)
static

◆ net_read_packet_nonblocking()

static net_async_status net_read_packet_nonblocking ( NET net,
ulong *  ret 
)
static

◆ net_read_process_buffer()

static bool net_read_process_buffer ( NET net,
size_t &  start_of_packet,
size_t &  buf_length,
uint multi_byte_packet,
size_t &  first_packet_offset 
)
static

Read the payload length in the packet, if it is found to multipacket omit the packet header of the subsequent packets.

Parameters
[in,out]netNET structures
[in,out]start_of_packetIn multipackets, points to the next header that will be erased
[in,out]buf_lengthEnd of readable byte
[in,out]multi_byte_packetFlag that indicate if packet is multibyte
[in,out]first_packet_offsetStarting offset of the packet to read
Returns
Return values
trueThe last packet read
falseOtherwise

◆ net_read_raw_loop()

static bool net_read_raw_loop ( NET net,
size_t  count 
)
static

Read a determined number of bytes from a network handler.

Parameters
netNET handler.
countThe number of bytes to read.
Returns
true on error, false on success.

◆ net_read_uncompressed_nonblocking()

static net_async_status net_read_uncompressed_nonblocking ( NET net,
ulong *  len_ptr 
)
static

Read the usual packet that is not compressed asynchronously.

Parameters
[in,out]netNET structure
[out]len_ptrlength of the packet read
Returns
status of the reads.

◆ net_read_uncompressed_packet()

static void net_read_uncompressed_packet ( NET net,
size_t &  len 
)
static

Reads one packet to net->buff + net->where_b.

If the packet is the first packet of a multi-packet packet (which is indicated by the length of the packet = 0xffffff) then all sub packets are read and concatenated.

Parameters
[in,out]netNET structure
[out]lenlength of the packet read

◆ net_read_update_offsets()

static ulong net_read_update_offsets ( NET net,
size_t  start_of_packet,
size_t  first_packet_offset,
size_t  buf_length,
uint  multi_byte_packet 
)
static

Update the offsets in the NET structure after reading the current packet.

Parameters
[in,out]netNET structure.
[in]start_of_packetIn multipackets, points to the next header that will be erased.
[in]first_packet_offsetStarting offset of the packet to read.
[in]buf_lengthEnd of readable byte.
[in]multi_byte_packetFlag that indicate if packet is multibyte

◆ net_realloc()

bool net_realloc ( NET net,
size_t  length 
)

Realloc the packet buffer.

◆ net_should_retry()

static bool net_should_retry ( NET net,
uint retry_count 
)
static

Whether a I/O operation should be retried later.

Parameters
netNET handler.
retry_countMaximum number of interrupted operations.
Return values
trueOperation should be retried.
falseOperation should not be retried. Fatal error.

◆ net_write_buff()

static bool net_write_buff ( NET net,
const uchar packet,
size_t  len 
)
static

Caching the data in a local buffer before sending it.

Fill up net->buffer and send it to the client when full.

If the rest of the to-be-sent-packet is bigger than buffer, send it in one big block (to avoid copying to internal buffer). If not, copy the rest of the data to the buffer and return without sending data.

Parameters
netNetwork handler
packetPacket to send
lenLength of packet
Note
The cached buffer can be sent as it is with 'net_flush()'. In this code we have to be careful to not send a packet longer than MAX_PACKET_LENGTH to net_write_packet() if we are using the compressed protocol as we store the length of the compressed packet in 3 bytes.
Return values
0ok
1

◆ net_write_command()

bool net_write_command ( NET net,
uchar  command,
const uchar header,
size_t  head_len,
const uchar packet,
size_t  len 
)

Send a command to the server.

The reason for having both header and packet is so that libmysql can easy add a header to a special command (like prepared statements) without having to re-alloc the string.

As the command is part of the first data packet, we have to do some data juggling to put the command in there, without having to create a new packet.

This function will split big packets into sub-packets if needed. (Each sub packet can only be 2^24 bytes)

Parameters
netNET handler
commandCommand in MySQL server (enum enum_server_command)
headerHeader to write after command
head_lenLength of header
packetQuery or parameter to query
lenLength of packet
Return values
0ok
1error

◆ net_write_command_nonblocking()

net_async_status net_write_command_nonblocking ( NET net,
uchar  command,
const uchar prefix,
size_t  prefix_len,
const uchar packet,
size_t  packet_len,
bool res 
)

Send a command to the server in asynchronous way.

This function will first populate all headers in NET::async_write_headers, followed by payload in NET::async_write_vector. Once header and payload is populated in NET, were call net_write_vector_nonblocking to send the packets to server in an asynchronous way.

◆ net_write_packet()

bool net_write_packet ( NET net,
const uchar packet,
size_t  length 
)

Write a MySQL protocol packet to the network handler.

Parameters
netNET handler.
packetThe packet to write.
lengthLength of the packet.
Remarks
The packet might be encapsulated into a compressed packet.
Returns
true on error, false on success.

◆ net_write_raw_loop()

static bool net_write_raw_loop ( NET net,
const uchar buf,
size_t  count 
)
static

Write a determined number of bytes to a network handler.

Parameters
netNET handler.
bufBuffer containing the data to be written.
countThe length, in bytes, of the buffer.
Returns
true on error, false on success.

◆ net_write_vector_nonblocking()

static net_async_status net_write_vector_nonblocking ( NET net,
ssize_t *  res 
)
static

◆ reset_packet_write_state()

static void reset_packet_write_state ( NET net)
static

◆ thd_increment_bytes_received()

void thd_increment_bytes_received ( size_t  length)

◆ thd_increment_bytes_sent()

void thd_increment_bytes_sent ( size_t  length)