minato_protocol_frontend (minato v1.2.0)

View Source

Encoders for the messages a client sends to a PostgreSQL server.

Internal to minato. The supported surface is minato_protocol.

Shape

Every message but three is a type byte, a 32 bit length and a payload. The length counts itself and the payload but not the type byte, which is why every encoder here adds four to the payload size.

StartupMessage, SSLRequest and CancelRequest carry no type byte, because they are sent before the connection has agreed what version of the protocol it is speaking. A server tells them apart by the 32 bit code that follows the length: 196608 for protocol 3.0, 80877103 to ask for TLS, and 80877102 to cancel a query on another connection.

Payloads

Strings are null terminated. Counted sequences carry a 16 bit count first. Parameter values in Bind carry a 32 bit length each, and a length of -1 is NULL, which is why null is a parameter value rather than an empty binary. The SASL data in SASLInitialResponse is written the same way, and -1 there means the mechanism sent no data at all.

Format codes are 0 for text and 1 for binary. Bind takes them as lists, and the protocol reads the length of those lists: an empty list means every value is text, a single element applies to every value, and any other length has to match the number of values.

Lengths are checked, not truncated

A length is 32 bits on the wire and a count is 16, and a number that does not fit would be written modulo the field: a parameter over 4 GiB would announce a short message, and the server would read the rest of it as messages of its own. So nothing is ever written modulo anything. A message longer than 1,073,741,823 bytes, which is the longest PostgreSQL itself accepts, raises error({minato_protocol, #{reason := message_too_long}}), and a Parse or Bind with more than 65,535 parameters or format codes raises too_many_values. Both are raised while encoding, so nothing of the message has been sent. A parameter is part of its Bind, so the same bound covers it.

A string cannot end early

Strings are null terminated, so a null byte inside one ends it where the caller did not mean it to. In a StartupMessage that is worse than a truncated name: the bytes after it are read as the next parameter, so a database name taken from outside could carry an options of its own. A startup parameter whose name or value holds a null byte, and a name that is empty, raise error({minato_protocol, #{message := startup}}) naming the parameter and not its value.

The other strings are a statement's text and the names of statements and portals, in Query, Parse, Bind, Describe, Execute and Close. One that holds a null byte raises error({minato_protocol, #{message := Message, reason := null_byte, data := Field}}), where Field is query, name, portal or statement, before anything is sent. A parameter value is length prefixed and may hold any byte.

What is not enforced

An encoder writes the message it is given. It does not check that StartupMessage names a user, that a Bind has as many parameters as the statement was parsed with, or that a portal exists. Those belong to whatever drives the exchange, which knows the session; this module only knows bytes.

Summary

Functions

Encode one frontend message.

Functions

encode/1

-spec encode(minato_protocol:frontend()) -> iodata().

Encode one frontend message.