codec

package
v0.3.68 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: LGPL-2.1 Imports: 12 Imported by: 1

Documentation

Overview

Package codec 提供 GTP 消息包的编解码能力。

这个包负责组装和解析 GTP 消息头与消息体,并在编码和解码过程中协调 压缩、认证、加密等可选模块。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDecode 是 GTP 消息包解码错误的根错误。
	ErrDecode = errors.New("gtp-decode")
	// ErrUnableToPeekLength 表示输入不足以读取消息包长度。
	ErrUnableToPeekLength = fmt.Errorf("%w: %w, unable to peek length", ErrDecode, io.ErrShortBuffer)
	// ErrPacketTooLarge 表示消息包长度超过配置上限。
	ErrPacketTooLarge = fmt.Errorf("%w: packet too large", ErrDecode)
)
View Source
var (
	// ErrAuthenticate 是 GTP 消息认证错误的根错误。
	ErrAuthenticate = errors.New("gtp-authenticate")
	// ErrInvalidMAC 表示消息认证码校验失败。
	ErrInvalidMAC = fmt.Errorf("%w: invalid MAC", ErrAuthenticate)
)
View Source
var (
	// ErrCompress 是 GTP 消息压缩和解压错误的根错误。
	ErrCompress = errors.New("gtp-compress")
)
View Source
var (
	// ErrEncode 是 GTP 消息包编码错误的根错误。
	ErrEncode = errors.New("gtp-encode")
)
View Source
var (
	// ErrEncrypt 是 GTP 消息加密和解密错误的根错误。
	ErrEncrypt = errors.New("gtp-encrypt")
)

Functions

This section is empty.

Types

type Authentication added in v0.3.63

type Authentication struct {
	HMAC hash.Hash // 消息认证使用的 HMAC。
	// contains filtered or unexported fields
}

Authentication 使用可复用的 HMAC 状态认证消息,不支持并发调用。

func (*Authentication) Auth added in v0.3.63

func (m *Authentication) Auth(msgId gtp.MsgId, flags gtp.Flags, msgBuf []byte) ([]byte, error)

Auth 验证 MsgSigned 中的认证码,并返回引用 msgBuf 的原始消息体。

func (*Authentication) Sign added in v0.3.63

func (m *Authentication) Sign(msgId gtp.MsgId, flags gtp.Flags, msgBuf []byte) (binaryutil.Bytes, error)

Sign 计算消息认证码并返回池化的 MsgSigned 编码。

func (*Authentication) SizeOfAddition added in v0.3.63

func (m *Authentication) SizeOfAddition(msgLen int) (int, error)

SizeOfAddition 返回 MsgSigned 编码相对原消息体增加的字节数。

type Compression added in v0.3.35

type Compression struct {
	CompressionStream method.CompressionStream // 压缩算法适配器。
}

Compression 通过 CompressionStream 压缩和还原消息体。

func (*Compression) Compress added in v0.3.35

func (c *Compression) Compress(src []byte) (binaryutil.Bytes, bool, error)

Compress 压缩 src;空输入或压缩无收益时返回 compressed=false。

func (*Compression) Uncompress added in v0.3.35

func (c *Compression) Uncompress(src []byte, max int) (binaryutil.Bytes, error)

Uncompress 解码 MsgCompressed 并将数据还原到池化缓冲区;max 必须容纳原始大小。

type Decoder

type Decoder struct {
	MsgCreator          gtp.IMsgCreator // 用于构造消息体的消息构建器。
	Encryption          IEncryption     // 可选的解密模块。
	Authentication      IAuthentication // 可选的消息认证模块。
	Compression         ICompression    // 可选的解压模块。
	MaxUncompressedSize int             // 解压后负载的字节上限,用于防御压缩炸弹。
	MaxPacketSize       int             // 完整消息包的字节上限;小于等于零时不限制。
}

Decoder 按“解密、认证、解压”的顺序还原 GTP 消息包。

func NewDecoder added in v0.3.35

func NewDecoder(msgCreator gtp.IMsgCreator) *Decoder

NewDecoder 创建使用指定消息构建器的解码器;构建器不得为 nil。

func (*Decoder) Decode

func (d *Decoder) Decode(data []byte, validation IValidation) (gtp.MsgPacket, int, error)

Decode 从 data 起始位置解码一个完整消息包,并返回消费字节数。 输入不足时消费字节数表示所需包长;成功返回的消息体不引用 data。

func (*Decoder) SetAuthentication added in v0.3.63

func (d *Decoder) SetAuthentication(authentication IAuthentication) *Decoder

SetAuthentication 设置消息认证模块;应在开始并发解码前完成配置。

func (*Decoder) SetCompression added in v0.3.35

func (d *Decoder) SetCompression(compression ICompression, maxUncompressedSize int) *Decoder

SetCompression 设置解压模块和解压后负载字节上限。

func (*Decoder) SetEncryption added in v0.3.35

func (d *Decoder) SetEncryption(encryption IEncryption) *Decoder

SetEncryption 设置解密模块;应在开始并发解码前完成配置。

func (*Decoder) SetMaxPacketSize added in v0.3.68

func (d *Decoder) SetMaxPacketSize(maxPacketSize int) *Decoder

SetMaxPacketSize 设置完整消息包字节上限;小于等于零时不限制。

type Encoder

type Encoder struct {
	Encryption           IEncryption     // 可选的加密模块。
	Authentication       IAuthentication // 可选的消息认证模块;仅在启用加密时使用。
	Compression          ICompression    // 可选的压缩模块。
	CompressionThreshold int             // 启用压缩的字节阈值;小于等于零时禁用压缩。
}

Encoder 按“压缩、认证、加密”的顺序编码 GTP 消息包。

func NewEncoder added in v0.3.35

func NewEncoder() *Encoder

NewEncoder 创建未配置加密、认证和压缩模块的消息包编码器。

func (*Encoder) Encode

func (e *Encoder) Encode(flags gtp.Flags, msg gtp.ReadableMsg) (binaryutil.Bytes, error)

Encode 编码消息包并返回池化字节缓冲区;调用方使用完后必须调用 Release。

func (*Encoder) SetAuthentication added in v0.3.63

func (e *Encoder) SetAuthentication(authentication IAuthentication) *Encoder

SetAuthentication 设置消息认证模块;应在开始并发编码前完成配置。

func (*Encoder) SetCompression added in v0.3.35

func (e *Encoder) SetCompression(compression ICompression, compressionThreshold int) *Encoder

SetCompression 设置压缩模块和启用压缩的字节阈值。

func (*Encoder) SetEncryption added in v0.3.35

func (e *Encoder) SetEncryption(encryption IEncryption) *Encoder

SetEncryption 设置加密模块;应在开始并发编码前完成配置。

type Encryption added in v0.3.35

type Encryption struct {
	Cipher     method.Cipher  // 加密或解密变换。
	Padding    method.Padding // 密码要求时使用的填充方案。
	FetchNonce FetchNonce     // 密码要求时为每次变换提供 nonce。
}

Encryption 组合对称密码、可选填充和 nonce 来源。

func (*Encryption) SizeOfAddition added in v0.3.35

func (e *Encryption) SizeOfAddition(msgLen int) (int, error)

SizeOfAddition 返回当前密码变换最多增加的字节数。

func (*Encryption) Transforming added in v0.3.35

func (e *Encryption) Transforming(dst, src []byte) (binaryutil.Bytes, error)

Transforming 准备输入、获取 nonce、执行密码变换并按需填充或去除填充。

type FetchNonce

type FetchNonce = generic.FuncPair0[[]byte, error]

FetchNonce 为每次加密或解密变换提供 nonce。

type IAuthentication added in v0.3.63

type IAuthentication interface {
	// Sign 包装消息体和认证码;返回的池化缓冲区由调用方释放。
	Sign(msgId gtp.MsgId, flags gtp.Flags, msgBuf []byte) (signedBuf binaryutil.Bytes, err error)
	// Auth 验证认证码并返回包装中的原始消息体。
	Auth(msgId gtp.MsgId, flags gtp.Flags, msgBuf []byte) (authBuf []byte, err error)
	// SizeOfAddition 返回认证包装相对原消息体增加的字节数。
	SizeOfAddition(msgLen int) (size int, err error)
}

IAuthentication 为消息类型、标志位和消息体生成并验证认证码。

func NewAuthentication added in v0.3.63

func NewAuthentication(hmac hash.Hash) IAuthentication

NewAuthentication 创建使用指定 HMAC 的消息认证模块;hmac 不得为 nil。

type ICompression added in v0.3.35

type ICompression interface {
	// Compress 仅在编码结果更小时返回压缩数据;池化结果由调用方释放。
	Compress(src []byte) (compressedBuf binaryutil.Bytes, compressed bool, err error)
	// Uncompress 在 max 字节上限内还原数据;池化结果由调用方释放。
	Uncompress(src []byte, max int) (uncompressedBuf binaryutil.Bytes, err error)
}

ICompression 压缩和还原 GTP 消息体。

func NewCompression added in v0.3.35

func NewCompression(cs method.CompressionStream) ICompression

NewCompression 创建使用指定压缩流的模块;压缩流不得为 nil。

type IEncryption added in v0.3.35

type IEncryption interface {
	// Transforming 将 src 变换到 dst 或新池化缓冲区;结果由调用方释放。
	Transforming(dst, src []byte) (transformedBuf binaryutil.Bytes, err error)
	// SizeOfAddition 返回变换结果相对输入可能增加的字节数。
	SizeOfAddition(msgLen int) (size int, err error)
}

IEncryption 对 GTP 消息体执行加密或解密变换。

func NewEncryption added in v0.3.35

func NewEncryption(cipher method.Cipher, padding method.Padding, fetchNonce FetchNonce) IEncryption

NewEncryption 创建加密或解密模块,并校验密码所需的填充器和 nonce 来源。

type IValidation added in v0.3.35

type IValidation interface {
	// Validate 校验原始消息包;返回错误会中止解码。
	Validate(msgHead gtp.MsgHead, msgBuf []byte) error
}

IValidation 在解密和解压前校验原始消息头及消息体。

Jump to

Keyboard shortcuts

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