protocol

package
v1.7.11 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrorSeverityFatal   string = "FATAL"
	ErrorSeverityPanic   string = "PANIC"
	ErrorSeverityWarning string = "WARNING"
	ErrorSeverityNotice  string = "NOTICE"
	ErrorSeverityDebug   string = "DEBUG"
	ErrorSeverityInfo    string = "INFO"
	ErrorSeverityLog     string = "LOG"
)

PG Error Severity Levels

View Source
const (
	ErrorFieldSeverity      byte = 'S'
	ErrorFieldCode          byte = 'C'
	ErrorFieldMessage       byte = 'M'
	ErrorFieldMessageDetail byte = 'D'
	ErrorFieldMessageHint   byte = 'H'
)

PG Error Message Field Identifiers

View Source
const (
	PGMessageLengthOffsetStartup int = 0
	PGMessageLengthOffset        int = 1
)

PostgreSQL message length offset constants.

View Source
const (
	ProtocolVersion int32 = 196608
	SSLRequestCode  int32 = 80877103

	/* SSL Responses */
	SSLAllowed    byte = 'S'
	SSLNotAllowed byte = 'N'
)

PostgreSQL Protocol Version/Code constants

View Source
const (
	AuthenticationMessageType  byte = 'R'
	ErrorMessageType           byte = 'E'
	EmptyQueryMessageType      byte = 'I'
	DescribeMessageType        byte = 'D'
	RowDescriptionMessageType  byte = 'T'
	DataRowMessageType         byte = 'D'
	QueryMessageType           byte = 'Q'
	CommandCompleteMessageType byte = 'C'
	TerminateMessageType       byte = 'X'
	NoticeMessageType          byte = 'N'
	PasswordMessageType        byte = 'p'
	ReadyForQueryMessageType   byte = 'Z'
)

PostgreSQL Message Type constants.

View Source
const (
	AuthenticationOk          int32 = 0
	AuthenticationKerberosV5  int32 = 2
	AuthenticationClearText   int32 = 3
	AuthenticationMD5         int32 = 5
	AuthenticationSCM         int32 = 6
	AuthenticationGSS         int32 = 7
	AuthenticationGSSContinue int32 = 8
	AuthenticationSSPI        int32 = 9
)

PostgreSQL Authentication Method constants.

View Source
const (
	// ErrorCodeInternalError indicates an unspecified internal error.
	ErrorCodeInternalError = "XX000"
)

Variables

This section is empty.

Functions

func CreateAuthenticationOKMessage

func CreateAuthenticationOKMessage() []byte

CreateAuthenticationOKMessage creates a Postgresql message which indicates successful authentication.

func CreateStartupMessage

func CreateStartupMessage(
	version int32,
	username string,
	database string,
	options map[string]string,
) []byte

CreateStartupMessage creates a PG startup message. This message is used to startup all connections with a PG backend.

func HandleAuthenticationRequest

func HandleAuthenticationRequest(username string, password string, connection net.Conn) (err error)

HandleAuthenticationRequest sends credentials to the server and reports whether they were accepted or not.

func NewError added in v1.3.0

func NewError(data []byte) error

NewError constructs a protocol error from an error packet. This is done, instead of passing the error through to the client as is, to give Secretless the ability to modify the error contents.

NewError parses the error packet and populates the protocol error with the field types as detailed in the protocol documentation: https://www.postgresql.org/docs/9.1/protocol-error-fields.html. If parsing fails NewError returns an "unparseable error from postgres server" error.

NOTE: We have made a conscious choice to propagate a subset of the error field types. We focus on those that are always present and, by our assessment, hold the most salient information.

func ParseStartupMessage

func ParseStartupMessage(message []byte) (version int32, options map[string]string, err error)

ParseStartupMessage parses the pg startup message.

func ReadMessage

func ReadMessage(client io.Reader) (messageType byte, message []byte, err error)

ReadMessage accepts an incoming message. The first byte is the message type, the second int32 is the message length, and the rest of the bytes are the message body.

func ReadStartupMessage

func ReadStartupMessage(client io.Reader) ([]byte, error)

ReadStartupMessage reads the startup message. The startup message is the same as a regular message except it does not begin with a message type byte.

Types

type Error

type Error struct {
	Severity string
	Code     string
	Message  string
	Detail   string
	Hint     string
}

Error is a Postgresql processing error.

func (*Error) Error

func (e *Error) Error() string

func (*Error) GetPacket

func (e *Error) GetPacket() []byte

GetPacket formats an Error into a protocol message.

type MessageBuffer

type MessageBuffer struct {
	// contains filtered or unexported fields
}

MessageBuffer is a variable-sized byte buffer used to read and write PostgreSQL Frontend and Backend messages.

A separate instance of a MessageBuffer should be use for reading and writing.

func NewMessageBuffer

func NewMessageBuffer(message []byte) *MessageBuffer

NewMessageBuffer creates and intializes a new MessageBuffer using message as its initial contents.

func (*MessageBuffer) Bytes

func (message *MessageBuffer) Bytes() []byte

Bytes gets the contents of the message buffer. This function is only useful after 'Write' operations as the underlying implementation will return the 'unread' portion of the buffer.

func (*MessageBuffer) ReadByte

func (message *MessageBuffer) ReadByte() (byte, error)

ReadByte reads a byte from the message buffer.

This function will read and return the next available byte from the message buffer.

func (*MessageBuffer) ReadInt32

func (message *MessageBuffer) ReadInt32() (value int32, err error)

ReadInt32 reads an int32 from the message buffer.

This function will read the next 4 available bytes from the message buffer and return them as an int32.

panic on error.

func (*MessageBuffer) ReadString

func (message *MessageBuffer) ReadString() (string, error)

ReadString reads a string from the message buffer.

This function will read and return the next Null terminated string from the message buffer.

func (*MessageBuffer) ResetLength

func (message *MessageBuffer) ResetLength(offset int)

ResetLength will reset the message length for the message.

offset should be one of the PGMessageLengthOffset* constants.

func (*MessageBuffer) WriteByte

func (message *MessageBuffer) WriteByte(value byte) error

WriteByte will write the specified byte to the message buffer.

func (*MessageBuffer) WriteInt32

func (message *MessageBuffer) WriteInt32(value int32) (err error)

WriteInt32 will write a 4 byte int32 to the message buffer.

func (*MessageBuffer) WriteString

func (message *MessageBuffer) WriteString(value string) (int, error)

WriteString will write a NULL terminated string to the buffer. It is assumed that the incoming string has *NOT* been NULL terminated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL