Documentation
¶
Overview ¶
Package proxyproto builds and decodes PROXY protocol v1 and v2 headers, which carry the original client and destination addresses of a proxied connection to the backend, and provides a Conn that reports those addresses for a connection whose peer sends one, with a Listener that wraps the connections it accepts.
Config.Datagram is zero for streams. DatagramFirst looks only at the first datagram. If it has no header, Required rejects the connection and Optional passes the flow through unchanged. DatagramEach skips datagrams with missing, truncated or malformed headers. DatagramAuto passes datagrams without a complete v1 or v2 signature through unchanged and skips those with a complete signature and a truncated or malformed header. In both per-datagram modes, ReadDatagram returns the header with its payload and DroppedDatagrams counts skips. Ignore passes all traffic through unchanged.
A header lets its sender choose the reported client address, so accept one only from trusted peers, such as with TrustPrefixes:
ln := &proxyproto.Listener{
Listener: inner,
PolicyFor: proxyproto.TrustPrefixes(netip.MustParsePrefix("10.0.0.0/8")),
}
To send a header, build it with NewHeader or HeaderFromConn and encode it with Header.AppendBinary. HeaderOf finds a connection-wide header of a Conn beneath other wrappers. In DatagramEach and DatagramAuto it returns nil without reading, and the connection reports the transport addresses.
Decoding is lenient towards non-standard senders wherever that cannot change who a connection is attributed to: version 1 lines may end in a bare LF, use runs of spaces, any letter case, UDP4 and UDP6 tokens, address families that differ from the token, and padded ports. Binary headers must carry version 2. Unknown commands, protocols and families fall back to the connection's own addresses. In a datagram, a v2 PROXY command must declare enough room for its address family. DatagramEach and DatagramAuto also drop packets with truncated TLVs. Other reads keep the whole TLVs that precede a truncation. Decoded headers drop NOOP padding and keep every other TLV in no more memory than the header took. The package depends only on the standard library.
Settings: the zero value of a field selects its default, stated in the field's documentation; for timeouts and limits it is a safe bound unless the field says otherwise. A negative timeout means no timeout, as for http.Server.IdleTimeout. Other negative values are an error unless the field gives them a meaning.
Index ¶
- Constants
- Variables
- func TrustPrefixes(p ...netip.Prefix) func(peer net.Addr) Policy
- type Config
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) CloseWrite() error
- func (c *Conn) DroppedDatagrams() uint64
- func (c *Conn) Header() (*Header, error)
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) NetConn() net.Conn
- func (c *Conn) NetxOwned() netutil.Owned
- func (c *Conn) Read(b []byte) (int, error)
- func (c *Conn) ReadDatagram(b []byte) (n int, h *Header, err error)
- func (c *Conn) ReadFrom(r io.Reader) (int64, error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) Write(b []byte) (int, error)
- func (c *Conn) WriteTo(w io.Writer) (int64, error)
- type DatagramMode
- type Header
- type Listener
- type Policy
- type SSL
- type SSLClient
- type TLVType
- type TLVs
- type Version
Examples ¶
Constants ¶
const DefaultHeaderTimeout = 10 * time.Second
DefaultHeaderTimeout bounds how long a Conn waits for its header when the HeaderTimeout of its Config is 0.
Variables ¶
var ( // ErrNoHeader is returned by Decode for data that does not start with a // PROXY protocol header, and by a Conn whose policy requires one. ErrNoHeader = errors.New("proxyproto: no PROXY protocol header") // ErrMalformed reports an invalid header that starts with a PROXY // protocol signature. ErrMalformed = errors.New("proxyproto: malformed PROXY protocol header") // ErrChecksum reports a version 2 header whose CRC32C field does not // match its checksum, or that has more than one CRC32C field or one // whose value is not 4 bytes. It matches ErrMalformed. ErrChecksum = fmt.Errorf("%w: CRC32C checksum mismatch", ErrMalformed) )
Functions ¶
func TrustPrefixes ¶
TrustPrefixes returns a function for Listener.PolicyFor under which peers whose IP address is in one of the prefixes must send a header and other peers are passed through without one, as Required and Ignore do. An IPv4 address and its IPv4-mapped IPv6 form (::ffff:a.b.c.d) are the same peer, for addresses and prefixes alike, so ::ffff:10.0.0.0/104 is 10.0.0.0/8, and an IPv6 prefix that holds the IPv4-mapped form of an IPv4 peer, such as ::/0, holds the peer. Zones are ignored. Peers without an IP address, such as those of a Unix socket, are passed through.
Types ¶
type Config ¶
type Config struct {
// HeaderTimeout bounds the wait for a stream or DatagramFirst header; 0 means
// DefaultHeaderTimeout (10s), and a negative value means no timeout,
// as for http.Server.ReadHeaderTimeout. DatagramEach and DatagramAuto
// use the connection's read deadline.
HeaderTimeout time.Duration
// Policy tells whether the peer sends a header. The zero value is
// Required. With DatagramEach or DatagramAuto, the mode determines
// how Required and Optional handle missing headers. Ignore always
// passes datagrams through unchanged.
Policy Policy
// Datagram selects framing for connections that return one datagram per
// Read. Zero selects a stream. DatagramFirst checks only the first
// datagram and uses Policy when the header is absent. DatagramEach
// skips datagrams without a header. DatagramAuto passes them through
// unchanged. In all three modes, TCP header addresses become *net.UDPAddr
// because version 1 UDP senders use TCP4 and TCP6.
Datagram DatagramMode
}
Config configures a Conn.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a connection whose peer sends a PROXY protocol header, from NewConn or a Listener. For streams and DatagramFirst, it reads the header the first time it is read, its addresses are queried, or Header is called. Avoid querying those addresses in an accept loop, since that waits for the header. In DatagramEach and DatagramAuto, ReadDatagram returns each datagram's header with its payload.
Writes do not wait for the header: Write, ReadFrom and CloseWrite go straight to the connection beneath. Under Required and Optional, a server that speaks first, or one that answers what it cannot use, as an HTTP server answers 400, therefore writes to a peer that may have sent no header or a malformed one. When that matters, check the header before writing.
For streams and DatagramFirst, the header must arrive within HeaderTimeout. The read that reaches it returns the timeout. Later reads return a non-timeout error matching ErrNoHeader so that loops retrying timeouts do not spin. A connection that cannot set deadlines is closed when the header timeout expires. A read deadline set by the caller that expires sooner also ends a header read started by Read or WriteTo, or their wait for one started by another call, which then returns the timeout; the next call resumes the header. On an Optional stream, the header timeout or end of stream passes through bytes that are only the start of a signature. In DatagramFirst, I/O errors during the header read are returned even under Optional.
func NewConn ¶
NewConn wraps c and reads PROXY protocol headers as configured by cfg. Headers are read lazily. Closing the Conn closes c. With the Ignore policy it passes c through and reports no header. For a nil c, its I/O methods return net.ErrClosed, its addresses are nil and Close does nothing.
Example ¶
Read the header of a connection accepted elsewhere.
package main
import (
"fmt"
"net"
"github.com/djylb/netx/proxyproto"
)
func main() {
server, client := net.Pipe()
go func() {
h, _ := proxyproto.NewHeader(proxyproto.V1,
&net.TCPAddr{IP: net.ParseIP("203.0.113.7"), Port: 40000},
&net.TCPAddr{IP: net.ParseIP("198.51.100.1"), Port: 443})
_, _ = h.WriteTo(client)
_, _ = client.Write([]byte("hello"))
_ = client.Close()
}()
c := proxyproto.NewConn(server, proxyproto.Config{})
defer func() { _ = c.Close() }()
buf := make([]byte, 5)
n, err := c.Read(buf)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(c.RemoteAddr(), string(buf[:n]))
}
Output: 203.0.113.7:40000 hello
func (*Conn) CloseWrite ¶
CloseWrite shuts down the writing side of the connection when it supports that, and otherwise returns an error matching errors.ErrUnsupported.
func (*Conn) DroppedDatagrams ¶
DroppedDatagrams returns the cumulative number of packets skipped while reading in DatagramEach or DatagramAuto. It is safe to call concurrently with reads.
func (*Conn) Header ¶
Header returns the connection's PROXY protocol header, reading it first if needed. It returns nil without an error when an Optional connection had no header, under Ignore, or in DatagramEach and DatagramAuto. Use ReadDatagram for per-datagram headers.
func (*Conn) LocalAddr ¶
LocalAddr returns the destination address from the header, or the local address when the header has none, with the address type of RemoteAddr. In DatagramEach and DatagramAuto it always returns the transport's local address without reading.
func (*Conn) NetConn ¶
NetConn returns the connection beneath, as *tls.Conn's NetConn does, for unwrapping helpers. Reading it directly bypasses the header handling.
func (*Conn) Read ¶
Read reads from the stream after the header. On a datagram connection it returns one datagram, truncated to b like a UDP socket. In DatagramEach and DatagramAuto it skips rejected datagrams and discards the metadata of delivered datagrams. Use ReadDatagram to obtain that metadata.
func (*Conn) ReadDatagram ¶
ReadDatagram reads one datagram, truncating its payload to fit b, and returns its header in DatagramEach and DatagramAuto. A header-only packet in these modes returns zero bytes and its header. With DatagramAuto or Ignore, a datagram passed through unchanged has a nil header. In DatagramFirst, the returned header applies to the entire connection. Headers can be retained across reads. For a stream, ReadDatagram returns errors.ErrUnsupported.
func (*Conn) ReadFrom ¶
ReadFrom copies r to the connection with the ReadFrom of the connection beneath, so that a *net.TCPConn can splice the data. Writes do not wait for the header.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the source address from the header, or the peer address when the header has none. A stream reports a UDP address as a *net.TCPAddr, so that it has the address type of its listener; a header of AF_UNIX addresses gives a *net.UnixAddr, so check the type rather than assert *net.TCPAddr when peers may send them. In DatagramEach and DatagramAuto it always returns the peer address without reading.
func (*Conn) SetDeadline ¶
SetDeadline sets the read and write deadlines. During a header read the read deadline takes effect as described for Conn.
func (*Conn) SetReadDeadline ¶
SetReadDeadline sets the read deadline. During a header read it takes effect as described for Conn.
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline.
type DatagramMode ¶
type DatagramMode uint8
DatagramMode selects how a Conn reads headers on a connection that returns one datagram per Read. Its zero value selects stream framing.
const ( // DatagramFirst reads the header only from the first datagram. A // header-only first datagram is consumed. If it has no header, // Required rejects the connection and Optional passes all datagrams // through unchanged. DatagramFirst DatagramMode = iota + 1 // DatagramEach requires a header on each datagram. Reads skip and count // datagrams whose header is missing, truncated or malformed. DatagramEach // DatagramAuto checks each datagram for a complete v1 or v2 signature. // Reads pass datagrams without one through unchanged, and skip and count // datagrams with a signature followed by a truncated or malformed header. DatagramAuto )
type Header ¶
type Header struct {
// Source and Destination are the client and server addresses of the
// proxied connection: *net.TCPAddr, *net.UDPAddr or *net.UnixAddr. They
// are nil for a Local header and for unspecified or unknown families, in
// which case the connection's own addresses apply.
Source net.Addr
Destination net.Addr
// TLVs holds the version 2 type-length-value fields.
TLVs TLVs
// Version is V1 for the text format and V2 for the binary format.
Version Version
// Local marks a version 2 LOCAL command, which a proxy sends for its own
// connections such as health checks, and a version 1 UNKNOWN header.
// Unknown version 2 commands and version 1 protocols are decoded as
// Local. The connection's own addresses apply.
Local bool
}
Header is a PROXY protocol header. Build one to send with NewHeader or HeaderFromConn, or as a literal for a Local header.
func Decode ¶
Decode decodes the PROXY protocol header at the start of b and returns it with its length in bytes. TLV values are copied, so the header does not alias b.
Decode accepts the deviations of non-standard senders described in the package documentation. It returns ErrNoHeader if b does not start with a header, io.ErrUnexpectedEOF if b holds only the start of one, so that a caller sniffing a stream can read more and try again, and an error wrapping ErrMalformed for a header that cannot be decoded. To read the header of a connection, use NewConn, which keeps the bytes that follow it.
Example ¶
package main
import (
"fmt"
"github.com/djylb/netx/proxyproto"
)
func main() {
data := []byte("PROXY TCP4 203.0.113.7 198.51.100.1 40000 443\r\nGET / HTTP/1.1\r\n")
h, n, err := proxyproto.Decode(data)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(h.Source, h.Destination)
fmt.Printf("%q\n", data[n:n+5])
}
Output: 203.0.113.7:40000 198.51.100.1:443 "GET /"
func HeaderFromConn ¶
HeaderFromConn returns a header of version v for c, from its remote address to its local address, as NewHeader does.
func HeaderOf ¶
HeaderOf returns the header of the first Conn in the chain of c and the connections that NetConn() net.Conn methods, such as *tls.Conn's, return beneath it, reading the header first if needed, as Conn.Header does. It returns nil and no error in DatagramEach and DatagramAuto, or when there is no Conn, such as for a connection a Listener passed through under Ignore.
Example ¶
Serve HTTPS behind TLS-passthrough load balancers that send PROXY protocol headers with the host name the client asked for, and reach the header from the *tls.Conn of each request.
package main
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"github.com/djylb/netx/proxyproto"
)
func main() {
ln, err := net.Listen("tcp", ":8443")
if err != nil {
fmt.Println(err)
return
}
config := &tls.Config{} // with the server's certificates
srv := &http.Server{
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
if h, err := proxyproto.HeaderOf(c); err == nil && h != nil {
if v, ok := h.TLVs.Get(proxyproto.TLVAuthority); ok {
fmt.Printf("%s asked the load balancer for %s\n", h.Source, v)
}
}
return ctx
},
}
_ = srv.Serve(tls.NewListener(&proxyproto.Listener{Listener: ln}, config))
}
Output:
func NewHeader ¶
NewHeader returns a header of version v for a connection from src to dst.
Both addresses are *net.TCPAddr, both *net.UDPAddr or, for version 2, both *net.UnixAddr of the same kind (stream or "unixgram"), and src must have an IP address. A nil dst, or one with an unspecified IP address, stands for an unknown destination: it is sent as the zero address of src's family (0.0.0.0 or ::), with dst's port if any; a nil Unix dst is unnamed. An IPv4 and an IPv6 address are sent as IPv6 with the IPv4 side in its IPv4-mapped form (::ffff:a.b.c.d). NewHeader returns an error for other pairs, such as a nil src, address types of other packages, or a TCP and a UDP address. The header holds copies of the addresses.
When the pair cannot be sent and the backend should still get a header, send one without addresses explicitly: Header{Version: v} is PROXY UNKNOWN in version 1 and a PROXY command with AF_UNSPEC in version 2, which can still carry TLVs, so that the backend uses the connection's own addresses. For a proxy's own connections, such as health checks, use Header{Version: v, Local: true}, the LOCAL command.
Example ¶
package main
import (
"bytes"
"fmt"
"net"
"github.com/djylb/netx/proxyproto"
)
func main() {
client := &net.TCPAddr{IP: net.ParseIP("192.0.2.10"), Port: 51234}
target := &net.TCPAddr{IP: net.ParseIP("198.51.100.20"), Port: 443}
for _, v := range []proxyproto.Version{proxyproto.V1, proxyproto.V2} {
h, err := proxyproto.NewHeader(v, client, target)
if err != nil {
fmt.Println(err)
return
}
var b bytes.Buffer
if _, err := h.WriteTo(&b); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%d %q\n", b.Len(), b.Bytes()[:12])
}
}
Output: 47 "PROXY TCP4 1" 28 "\r\n\r\n\x00\r\nQUIT\n"
func (*Header) AppendBinary ¶
AppendBinary appends the encoded header to b.
A Local header is encoded without addresses, as "PROXY UNKNOWN" (version 1) or a LOCAL command (version 2). Otherwise Source and Destination must be as NewHeader returns them, without its rewriting of the destination: both *net.TCPAddr or both *net.UDPAddr with IP addresses, or, for version 2, both *net.UnixAddr of the same kind with names of at most 108 bytes. Both nil, as decoded from a header of an unknown family, is encoded as "PROXY UNKNOWN" or a PROXY command of the AF_UNSPEC family. Version 1 has no UDP token, so UDP pairs use the TCP4 and TCP6 tokens. AppendBinary fails for other addresses, for TLVs in version 1, and in version 2 for TLVs that are not a sequence of whole fields, for more than one CRC32C field or one whose value is not 4 bytes, and for an address and TLV block of more than 65535 bytes. On failure b is returned unchanged. A CRC32C field gets the checksum of the encoded header as its value; see TLVs.AppendCRC32C.
func (*Header) UDPAddrs ¶
UDPAddrs returns Source and Destination with *net.TCPAddr converted to *net.UDPAddr, for a header sent ahead of UDP traffic. Version 1 has no UDP token, so UDP senders use TCP4 and TCP6, and some use a version 2 STREAM transport. Other addresses are returned unchanged.
func (*Header) WriteTo ¶
WriteTo writes the encoded header to w with one Write, so that it goes out in one segment ahead of the proxied stream or as a datagram of its own. It fails as AppendBinary does, without writing, and with io.ErrShortWrite for a Write that writes less without an error.
Example ¶
Forward clients to a backend that expects a PROXY protocol header telling it who each client is.
package main
import (
"context"
"fmt"
"net"
"github.com/djylb/netx"
"github.com/djylb/netx/proxyproto"
)
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Println(err)
return
}
for {
client, err := ln.Accept()
if err != nil {
return
}
go func() {
defer func() { _ = client.Close() }()
backend, err := net.Dial("tcp", "backend.example:8080")
if err != nil {
return
}
defer func() { _ = backend.Close() }()
h, err := proxyproto.HeaderFromConn(proxyproto.V2, client)
if err != nil {
return
}
if _, err := h.WriteTo(backend); err != nil {
return
}
_, _, _ = netx.Relay(context.Background(), client, backend)
}()
}
}
Output:
type Listener ¶
type Listener struct {
net.Listener
// PolicyFor, when set, decides for each accepted connection, by its peer
// address, whether a header is expected, in place of Config.Policy.
PolicyFor func(peer net.Addr) Policy
// Config configures each accepted Conn. Its Policy applies to every
// peer unless PolicyFor is set; the zero value is Required.
Config Config
}
Listener wraps a net.Listener whose peers, such as load balancers, send a PROXY protocol header. Connections with a connection-wide header use its addresses for RemoteAddr and LocalAddr.
Only trusted peers should be allowed to send a header, since it lets them choose the reported client address; restrict them with PolicyFor, such as to TrustPrefixes. As described for Conn, writes to an accepted connection do not wait for its header to be validated.
Example ¶
Serve HTTP behind load balancers in 10.0.0.0/8 that send PROXY protocol headers, and to other peers directly.
package main
import (
"fmt"
"net"
"net/http"
"net/netip"
"github.com/djylb/netx/proxyproto"
)
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Println(err)
return
}
pl := &proxyproto.Listener{
Listener: ln,
PolicyFor: proxyproto.TrustPrefixes(netip.MustParsePrefix("10.0.0.0/8")),
}
_ = http.Serve(pl, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "client:", r.RemoteAddr) // from the header
}))
}
Output:
Example (Datagram) ¶
Serve UDP behind a proxy that sends a PROXY protocol header in the first datagram of each flow, as some UDP proxies do with version 1 TCP4 and TCP6 tokens.
package main
import (
"fmt"
"net"
"github.com/djylb/netx/proxyproto"
"github.com/djylb/netx/udpflow"
)
func main() {
pc, err := net.ListenPacket("udp", ":19132")
if err != nil {
fmt.Println(err)
return
}
pl := &proxyproto.Listener{Listener: udpflow.NewPacketListener(pc), Config: proxyproto.Config{Datagram: proxyproto.DatagramFirst}}
defer func() { _ = pl.Close() }()
for {
c, err := pl.Accept()
if err != nil {
return
}
go func() {
defer func() { _ = c.Close() }()
buf := make([]byte, 1500)
for {
n, err := c.Read(buf) // one datagram, header removed
if err != nil {
return
}
// c.RemoteAddr() is the client's *net.UDPAddr from the header.
_, _ = c.Write(buf[:n])
}
}()
}
}
Output:
type Policy ¶
type Policy uint8
Policy tells a Conn whether its peer sends a PROXY protocol header. In DatagramEach and DatagramAuto, the mode determines how Required and Optional handle missing headers.
const ( // Required rejects connections that do not start with a header: their // first read returns ErrNoHeader, or io.EOF for one that ends before // sending anything. It is the zero value. Required Policy = iota // Optional uses a header when the connection starts with one and // otherwise passes the stream through unchanged, including when it // stops, by the header timeout or its end, after at most the start of a // signature, as a peer that waits for the server to speak first does. Optional // Ignore passes the connection through without looking for a header. Ignore )
type SSL ¶
type SSL struct {
// TLVs holds the sub-fields, such as TLVSSLVersion and TLVSSLCN. Those
// of a decoded field share the header's memory.
TLVs TLVs
// Verify is zero when the client sent a certificate that the proxy
// verified, and otherwise not zero, such as an X.509 verification
// result code.
Verify uint32
// Client holds the client flags.
Client SSLClient
}
SSL is the value of an SSL field (PP2_TYPE_SSL): what the proxy that ended the client's TLS connection tells of it. Read it with TLVs.SSL and send it with TLVs.AppendSSL. Like the addresses, it is to be believed only from trusted proxies.
func (SSL) Cipher ¶
Cipher returns the cipher suite of the client's connection, such as "ECDHE-RSA-AES128-GCM-SHA256", or "" if the proxy did not tell it.
func (SSL) CommonName ¶
CommonName returns the Common Name of the subject of the client's certificate, or "" if the proxy did not tell it.
func (SSL) KeyAlg ¶
KeyAlg returns the algorithm of the key of the client's certificate, such as "RSA2048", or "" if the proxy did not tell it.
func (SSL) SigAlg ¶
SigAlg returns the algorithm that signed the client's certificate, such as "SHA256", or "" if the proxy did not tell it.
type SSLClient ¶
type SSLClient uint8
SSLClient holds the client flags of an SSL field (PP2_CLIENT_*).
type TLVType ¶
type TLVType byte
TLVType is the type of a version 2 type-length-value field.
const ( TLVALPN TLVType = 0x01 // application protocol, as in TLS ALPN TLVAuthority TLVType = 0x02 // host name the client asked for, as in TLS SNI TLVCRC32C TLVType = 0x03 // CRC32c checksum of the header; see TLVs.AppendCRC32C TLVNoop TLVType = 0x04 // padding TLVUniqueID TLVType = 0x05 // opaque connection identifier TLVSSL TLVType = 0x20 // TLS information of the client connection; see SSL TLVNetNS TLVType = 0x30 // network namespace name )
TLV types defined by the PROXY protocol specification.
const ( TLVSSLVersion TLVType = 0x21 // TLS version, such as "TLSv1.3" TLVSSLCN TLVType = 0x22 // Common Name of the client certificate's subject TLVSSLCipher TLVType = 0x23 // cipher suite, such as "ECDHE-RSA-AES128-GCM-SHA256" TLVSSLSigAlg TLVType = 0x24 // signature algorithm of the client certificate TLVSSLKeyAlg TLVType = 0x25 // key algorithm of the client certificate, such as "RSA2048" )
Types of the sub-fields of an SSL field (PP2_SUBTYPE_SSL_*); see SSL.
const ( TLVCustomMin TLVType = 0xE0 TLVCustomMax TLVType = 0xEF TLVExperimentalMin TLVType = 0xF0 TLVExperimentalMax TLVType = 0xF7 )
TLV type ranges that the specification leaves to applications: custom types for an application's own fields, such as who a connection belongs to, and experimental ones. A receiver must accept fields only from peers it trusts, as for the addresses; see Listener.PolicyFor and TrustPrefixes.
func (TLVType) IsExperimental ¶
IsExperimental reports whether t is in the range for experimental fields.
type TLVs ¶
type TLVs []byte
TLVs holds version 2 type-length-value fields in their wire encoding: a type byte, a 2-byte big-endian length and the value, for each in header order. Build them with Append; a decoded header keeps a copy of its fields without NOOP padding and ends them at the first truncated one, so it takes no more memory than the header did however many fields it has.
func (TLVs) All ¶
All returns the fields in order, without NOOP padding, until the first truncated one. The values share f's memory.
func (TLVs) Append ¶
Append appends a field of type t and value v. It fails for a value longer than 65535 bytes, leaving the fields unchanged.
func (TLVs) AppendCRC32C ¶
AppendCRC32C appends a CRC32C field, whose value AppendBinary fills in with the checksum of the encoded header, as the receiver then verifies. The specification computes it over the whole header, so append it once, in any place among the fields.
func (TLVs) AppendSSL ¶
AppendSSL appends an SSL field with the value s. It fails, leaving the fields unchanged, for sub-fields that are not a sequence of whole fields or a value longer than 65535 bytes.