protocol

package
Version: v0.0.0-...-9720efe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResponseOk = 0x00

	ResponseErr = 0xff

	// MySQL connection state constants
	ConnStateStarted  = 0xf4
	ConnStateFinished = 0xf5
)
View Source
const (
	ComQuit byte = iota + 1

	ComQuery
	ComFieldList

	ComStmtPrepare
	ComStmtExecute

	ComStmtClose
)

Variables

This section is empty.

Functions

func DecodeErrResponse

func DecodeErrResponse(packet []byte) (string, error)

DecodeOkResponse decodes ERR_Packet from server. Part of basic packet structure shown below.

int<3> PacketLength int<1> PacketNumber int<1> PacketType (0xFF) if clientCapabilities & clientProtocol41 {

string<1> SqlStateMarker (#)
string<5> SqlState

} string<EOF> ErrorMessage

func DecodeFieldTypeDouble

func DecodeFieldTypeDouble(r *bytes.Reader) (string, error)

DecodeFieldTypeDouble decodes MYSQL_TYPE_DOUBLE field See https://mariadb.com/kb/en/mariadb/resultset/#field-types

func DecodeFieldTypeLongLong

func DecodeFieldTypeLongLong(r *bytes.Reader) (string, error)

DecodeFieldTypeLongLong decodes MYSQL_TYPE_LONGLONG field See https://mariadb.com/kb/en/mariadb/resultset/#field-types

func DecodeFieldTypeString

func DecodeFieldTypeString(r *bytes.Reader) (string, error)

DecodeFieldTypeString decodes MYSQL_TYPE_VAR_STRING field (length-encoded string) See https://mariadb.com/kb/en/mariadb/resultset/#field-types

func GetPacketType

func GetPacketType(packet []byte) byte

func ProcessHandshake

func ProcessHandshake(client net.Conn, mysql net.Conn) (*HandshakeV10, *HandshakeResponse41, error)

ProcessHandshake handles handshake between server and client. Returns server and client handshake responses

func ProxyPacket

func ProxyPacket(src, dst net.Conn) ([]byte, error)

ProxyPacket ...

func ReadEOFLengthString

func ReadEOFLengthString(data []byte) string

ReadEOFLengthString returns parsed EOF-length string. EOF-length strings are those strings whose length will be calculated by the packet remaining length. See https://mariadb.com/kb/en/mariadb/protocol-data-types/#end-of-file-length-strings

func ReadErrMessage

func ReadErrMessage(errPacket []byte) string

func ReadLenEncodedInteger

func ReadLenEncodedInteger(r *bytes.Reader) (value uint64, offset uint64)

ReadLenEncodedInteger returns parsed length-encoded integer and it's offset. See https://mariadb.com/kb/en/mariadb/protocol-data-types/#length-encoded-integers

func ReadLenEncodedString

func ReadLenEncodedString(r *bytes.Reader) (string, uint64, error)

ReadLenEncodedString returns parsed length-encoded string and it's length. Length-encoded strings are prefixed by a length-encoded integer which describes the length of the string, followed by the string value. See https://mariadb.com/kb/en/mariadb/protocol-data-types/#length-encoded-strings

func ReadNullTerminatedString

func ReadNullTerminatedString(r *bytes.Reader) string

ReadNullTerminatedString reads bytes from reader until 0x00 byte See https://mariadb.com/kb/en/mariadb/protocol-data-types/#null-terminated-strings

func ReadPacket

func ReadPacket(conn net.Conn) ([]byte, error)

ReadPacket ...

func ReadPrepareResponse

func ReadPrepareResponse(conn net.Conn) ([]byte, byte, error)

ReadPrepareResponse reads response from MySQL server for COM_STMT_PREPARE query issued by client. ...

func ReadResponse

func ReadResponse(conn net.Conn, deprecateEof bool) ([]byte, byte, error)

ReadResponse ...

func ReadShowFieldsResponse

func ReadShowFieldsResponse(conn net.Conn) ([]byte, byte, error)

func SkipPacketHeader

func SkipPacketHeader(r *bytes.Reader) error

SkipPacketHeader rewinds reader to packet payload

func WritePacket

func WritePacket(pkt []byte, conn net.Conn) (int, error)

WritePacket ...

Types

type ComStmtExecuteRequest

type ComStmtExecuteRequest struct {
	StatementID        uint32              // ID of prepared statement
	PreparedParameters []PreparedParameter // Slice of prepared parameters
}

ComStmtExecuteRequest represents COM_STMT_EXECUTE request structure.

func DecodeComStmtExecuteRequest

func DecodeComStmtExecuteRequest(packet []byte, paramsCount uint16) (*ComStmtExecuteRequest, error)

DecodeComStmtExecuteRequest decodes COM_STMT_EXECUTE packet sent by MySQL client. Basic packet structure shown below. See https://mariadb.com/kb/en/mariadb/com_stmt_execute/

int<3> PacketLength int<1> PacketNumber int<1> COM_STMT_EXECUTE (0x17) int<4> StatementID int<1> Flags int<4> IterationCount = 1 if (ParamCount > 0) {

byte<(ParamCount + 7) / 8> NullBitmap
byte<1>: SendTypeToServer = 0 or 1
if (SendTypeToServer)
{
	Foreach parameter
	{
		byte<1>: FieldType
		byte<1>: ParameterFlag
	}
}
Foreach parameter
{
	byte<n> BinaryParameterValue
}

}

type ComStmtPrepareOkResponse

type ComStmtPrepareOkResponse struct {
	StatementID   uint32 // ID of prepared statement
	ParametersNum uint16 // Num of prepared parameters
}

ComStmtPrepareOkResponse represents COM_STMT_PREPARE_OK response structure.

func DecodeComStmtPrepareOkResponse

func DecodeComStmtPrepareOkResponse(packet []byte) (*ComStmtPrepareOkResponse, error)

DecodeComStmtPrepareOkResponse decodes COM_STMT_PREPARE_OK response from MySQL server. Basic packet structure shown below. See https://mariadb.com/kb/en/mariadb/com_stmt_prepare/#COM_STMT_PREPARE_OK

int<3> PacketLength int<1> PacketNumber int<1> Command COM_STMT_PREPARE_OK (0x00) int<4> StatementID int<2> NumberOfColumns int<2> NumberOfParameters string<1> <not used> int<2> NumberOfWarnings

type ConnSettings

type ConnSettings struct {
	ClientCapabilities uint32
	ServerCapabilities uint32
	SelectedDb         string
}

...

func (*ConnSettings) DeprecateEOFSet

func (h *ConnSettings) DeprecateEOFSet() bool

...

type ErrResponse

type ErrResponse struct {
	Message string
}

type HandshakeResponse41

type HandshakeResponse41 struct {
	ClientCapabilities uint32
	ClientCharset      byte
}

HandshakeResponse41 represents handshake response packet sent by 4.1+ clients supporting clientProtocol41 capability, if the server announced it in its initial handshake packet. See http://imysql.com/mysql-internal-manual/connection-phase-packets.html#packet-Protocol::HandshakeResponse41

func DecodeHandshakeResponse41

func DecodeHandshakeResponse41(packet []byte) (*HandshakeResponse41, error)

DecodeHandshakeResponse41 decodes handshake response packet send by client. TODO: Add packet struct comment TODO: Add packet length check

type HandshakeV10

type HandshakeV10 struct {
	ProtocolVersion    byte
	ServerVersion      string
	ConnectionID       uint32
	ServerCapabilities uint32
	AuthPlugin         string
}

HandshakeV10 represents sever's initial handshake packet See https://mariadb.com/kb/en/mariadb/1-connecting-connecting/#initial-handshake-packet

func DecodeHandshakeV10

func DecodeHandshakeV10(packet []byte) (*HandshakeV10, error)

DecodeHandshakeV10 decodes initial handshake request from server. Basic packet structure shown below. See http://imysql.com/mysql-internal-manual/connection-phase-packets.html#packet-Protocol::HandshakeV10

int<3> PacketLength int<1> PacketNumber int<1> ProtocolVersion string<NUL> ServerVersion int<4> ConnectionID string<8> AuthPluginDataPart1 (authentication seed) string<1> Reserved (always 0x00) int<2> ServerCapabilities (1st part) int<1> ServerDefaultCollation int<2> StatusFlags int<2> ServerCapabilities (2nd part) if capabilities & clientPluginAuth {

int<1> AuthPluginDataLength

} else {

int<1> 0x00

} string<10> Reserved (all 0x00) if capabilities & clientSecureConnection {

string[$len] AuthPluginDataPart2 ($len=MAX(13, AuthPluginDataLength - 8))

} if capabilities & clientPluginAuth {

string[NUL] AuthPluginName

}

type OkResponse

type OkResponse struct {
	PacketType   byte
	AffectedRows uint64
	LastInsertID uint64
}

OkResponse represents packet sent from the server to the client to signal successful completion of a command https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_ok_packet.html

func DecodeOkResponse

func DecodeOkResponse(packet []byte) (*OkResponse, error)

DecodeOkResponse decodes OK_Packet from server. Part of basic packet structure shown below.

int<3> PacketLength int<1> PacketNumber int<1> PacketType (0x00 or 0xFE) int<lenenc> AffectedRows int<lenenc> LastInsertID ... more ...

type PreparedParameter

type PreparedParameter struct {
	FieldType byte   // Type of prepared parameter. See https://mariadb.com/kb/en/mariadb/resultset/#field-types
	Flag      byte   // Unused
	Value     string // String value of any prepared parameter passed with COM_STMT_EXECUTE request
}

PreparedParameter structure represents single prepared parameter structure for COM_STMT_EXECUTE request.

type QueryRequest

type QueryRequest struct {
	Query string // SQL query value
}

QueryRequest represents COM_QUERY or COM_STMT_PREPARE command sent by client to server.

func DecodeQueryRequest

func DecodeQueryRequest(packet []byte) (*QueryRequest, error)

DecodeQueryRequest decodes COM_QUERY and COM_STMT_PREPARE requests from client. Basic packet structure shown below. See https://mariadb.com/kb/en/mariadb/com_query/ and https://mariadb.com/kb/en/mariadb/com_stmt_prepare/

int<3> PacketLength int<1> PacketNumber int<1> Command COM_QUERY (0x03) or COM_STMT_PREPARE (0x16) string<EOF> SQLStatement

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL