Documentation
¶
Overview ¶
Package codec 提供 GTP 消息包的编解码能力。
这个包负责组装和解析 GTP 消息头与消息体,并在编码和解码过程中协调 压缩、认证、加密等可选模块。
Index ¶
- Variables
- type Authentication
- type Compression
- type Decoder
- func (d *Decoder) Decode(data []byte, validation IValidation) (gtp.MsgPacket, int, error)
- func (d *Decoder) SetAuthentication(authentication IAuthentication) *Decoder
- func (d *Decoder) SetCompression(compression ICompression, maxUncompressedSize int) *Decoder
- func (d *Decoder) SetEncryption(encryption IEncryption) *Decoder
- func (d *Decoder) SetMaxPacketSize(maxPacketSize int) *Decoder
- type Encoder
- func (e *Encoder) Encode(flags gtp.Flags, msg gtp.ReadableMsg) (binaryutil.Bytes, error)
- func (e *Encoder) SetAuthentication(authentication IAuthentication) *Encoder
- func (e *Encoder) SetCompression(compression ICompression, compressionThreshold int) *Encoder
- func (e *Encoder) SetEncryption(encryption IEncryption) *Encoder
- type Encryption
- type FetchNonce
- type IAuthentication
- type ICompression
- type IEncryption
- type IValidation
Constants ¶
This section is empty.
Variables ¶
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) )
var ( // ErrAuthenticate 是 GTP 消息认证错误的根错误。 ErrAuthenticate = errors.New("gtp-authenticate") // ErrInvalidMAC 表示消息认证码校验失败。 ErrInvalidMAC = fmt.Errorf("%w: invalid MAC", ErrAuthenticate) )
var ( // ErrCompress 是 GTP 消息压缩和解压错误的根错误。 ErrCompress = errors.New("gtp-compress") )
var ( // ErrEncode 是 GTP 消息包编码错误的根错误。 ErrEncode = errors.New("gtp-encode") )
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) 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) 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
SetMaxPacketSize 设置完整消息包字节上限;小于等于零时不限制。
type Encoder ¶
type Encoder struct {
Encryption IEncryption // 可选的加密模块。
Authentication IAuthentication // 可选的消息认证模块;仅在启用加密时使用。
Compression ICompression // 可选的压缩模块。
CompressionThreshold int // 启用压缩的字节阈值;小于等于零时禁用压缩。
}
Encoder 按“压缩、认证、加密”的顺序编码 GTP 消息包。
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 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 来源。