refactor(agent/stream): update header size calculation and field types

- Adjusted headerSize calculation to reflect the correct size based on field definitions.
- Changed HostLength and PortLength types from uint8 to byte.
- Updated PROTOCOL.md to reflect the new header size and structure.
This commit is contained in:
yusing
2026-01-07 22:33:24 +08:00
parent 5048326c20
commit 63f96b8d76
2 changed files with 6 additions and 9 deletions

View File

@@ -12,9 +12,8 @@ The on-wire header is a fixed-size binary blob:
- `PortLength` (1 byte)
- `Port` (5 bytes, NUL padded)
- `Checksum` (4 bytes, big-endian CRC32)
- `Padding` (14 bytes)
Total: `headerSize = 8 + 1 + 255 + 1 + 5 + 4 + 14 = 288` bytes.
Total: `headerSize = 8 + 1 + 255 + 1 + 5 + 4 = 273` bytes.
Checksum is `crc32.ChecksumIEEE(header[0:headerSize-4])`.

View File

@@ -15,7 +15,7 @@ const (
portSize = 5
checksumSize = 4 // crc32 checksum
headerSize = 288
headerSize = versionSize + 1 + hostSize + 1 + portSize + checksumSize
)
var version = [versionSize]byte{'0', '.', '1', '.', '0', 0, 0, 0}
@@ -25,15 +25,13 @@ var ErrInvalidHeader = errors.New("invalid header")
type StreamRequestHeader struct {
Version [versionSize]byte
HostLength uint8
HostLength byte
Host [hostSize]byte
PortLength uint8
PortLength byte
Port [portSize]byte
Checksum [checksumSize]byte
_ [14]byte // padding to make the header size match the size of the struct
}
func init() {
@@ -51,9 +49,9 @@ func NewStreamRequestHeader(host, port string) (*StreamRequestHeader, error) {
}
header := &StreamRequestHeader{}
copy(header.Version[:], version[:])
header.HostLength = uint8(len(host))
header.HostLength = byte(len(host))
copy(header.Host[:], host)
header.PortLength = uint8(len(port))
header.PortLength = byte(len(port))
copy(header.Port[:], port)
header.updateChecksum()
return header, nil