mirror of
https://github.com/luxfi/net.git
synced 2026-10-30 15:24:11 +00:00
Replace codec/wrappers.Packer with an in-package packer (binio.go). endpoints/ only needs PackByte/PackShort/PackLong/PackFixedBytes for the gossip-ID preimage and the endpoint signing bytes — a tiny self- contained big-endian writer over a pre-sized slice. Files touched: - endpoints/binio.go (new): packer + ByteLen/ShortLen/IntLen/LongLen - endpoints/claimed_ip_port.go: drop wrappers, use local packer - endpoints/endpoint.go: drop wrappers, use local packer - endpoints/ip_port.go: drop wrappers; remove dead PackIP (no callers) go.mod direct dep on luxfi/codec removed.
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
// Copyright (C) 2019-2026, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package endpoints
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
)
|
|
|
|
// Byte/word lengths for the wire formats this package emits.
|
|
const (
|
|
ByteLen = 1
|
|
ShortLen = 2
|
|
IntLen = 4
|
|
LongLen = 8
|
|
)
|
|
|
|
// ErrInsufficientLength is returned by packer methods when the underlying byte
|
|
// slice can't satisfy the requested read/write.
|
|
var ErrInsufficientLength = errors.New("packer: insufficient length")
|
|
|
|
// packer is a thin big-endian byte-slice writer/reader. Self-contained for the
|
|
// fixed-shape wire layouts in this package (endpoint preimage, gossip ID
|
|
// preimage). No expansion semantics — callers size the slice up front.
|
|
type packer struct {
|
|
Err error
|
|
Bytes []byte
|
|
Offset int
|
|
}
|
|
|
|
// PackByte writes a single byte at the current offset.
|
|
func (p *packer) PackByte(v byte) {
|
|
if p.Err != nil {
|
|
return
|
|
}
|
|
if p.Offset+ByteLen > len(p.Bytes) {
|
|
p.Err = ErrInsufficientLength
|
|
return
|
|
}
|
|
p.Bytes[p.Offset] = v
|
|
p.Offset += ByteLen
|
|
}
|
|
|
|
// PackShort writes a big-endian uint16 at the current offset.
|
|
func (p *packer) PackShort(v uint16) {
|
|
if p.Err != nil {
|
|
return
|
|
}
|
|
if p.Offset+ShortLen > len(p.Bytes) {
|
|
p.Err = ErrInsufficientLength
|
|
return
|
|
}
|
|
binary.BigEndian.PutUint16(p.Bytes[p.Offset:], v)
|
|
p.Offset += ShortLen
|
|
}
|
|
|
|
// PackLong writes a big-endian uint64 at the current offset.
|
|
func (p *packer) PackLong(v uint64) {
|
|
if p.Err != nil {
|
|
return
|
|
}
|
|
if p.Offset+LongLen > len(p.Bytes) {
|
|
p.Err = ErrInsufficientLength
|
|
return
|
|
}
|
|
binary.BigEndian.PutUint64(p.Bytes[p.Offset:], v)
|
|
p.Offset += LongLen
|
|
}
|
|
|
|
// PackFixedBytes copies a fixed-size byte slice at the current offset.
|
|
func (p *packer) PackFixedBytes(b []byte) {
|
|
if p.Err != nil {
|
|
return
|
|
}
|
|
if p.Offset+len(b) > len(p.Bytes) {
|
|
p.Err = ErrInsufficientLength
|
|
return
|
|
}
|
|
copy(p.Bytes[p.Offset:], b)
|
|
p.Offset += len(b)
|
|
}
|