Files
zeekay f8e36531a8 codec: rip github.com/luxfi/codec from leaf-misc (Wave 1D)
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.
2026-06-05 11:56:48 -07:00

93 lines
1.9 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package endpoints
import (
"errors"
"fmt"
"net"
"strconv"
)
const nullStr = "null"
var (
errMissingQuotes = errors.New("first and last characters should be quotes")
errBadIP = errors.New("bad ip format")
)
type IPDesc IPPort
func (ipDesc IPDesc) String() string {
return IPPort(ipDesc).String()
}
func (ipDesc IPDesc) MarshalJSON() ([]byte, error) {
return []byte(`"` + ipDesc.String() + `"`), nil
}
func (ipDesc *IPDesc) UnmarshalJSON(b []byte) error {
str := string(b)
if str == nullStr { // If "null", do nothing
return nil
} else if len(str) < 2 {
return errMissingQuotes
}
lastIndex := len(str) - 1
if str[0] != '"' || str[lastIndex] != '"' {
return errMissingQuotes
}
ipPort, err := ToIPPort(str[1:lastIndex])
if err != nil {
return fmt.Errorf("couldn't decode to IPPort: %w", err)
}
*ipDesc = IPDesc(ipPort)
return nil
}
// An IP and a port.
type IPPort struct {
IP net.IP `json:"ip"`
Port uint16 `json:"port"`
}
func (ipPort IPPort) Equal(other IPPort) bool {
return ipPort.Port == other.Port && ipPort.IP.Equal(other.IP)
}
func (ipPort IPPort) String() string {
return net.JoinHostPort(ipPort.IP.String(), strconv.FormatUint(uint64(ipPort.Port), 10))
}
// IsZero returns if the IP or port is zeroed out
func (ipPort IPPort) IsZero() bool {
ip := ipPort.IP
return ipPort.Port == 0 ||
len(ip) == 0 ||
ip.Equal(net.IPv4zero) ||
ip.Equal(net.IPv6zero)
}
func ToIPPort(str string) (IPPort, error) {
host, portStr, err := net.SplitHostPort(str)
if err != nil {
return IPPort{}, errBadIP
}
port, err := strconv.ParseUint(portStr, 10 /*=base*/, 16 /*=size*/)
if err != nil {
return IPPort{}, err
}
ip := net.ParseIP(host)
if ip == nil {
return IPPort{}, errBadIP
}
return IPPort{
IP: ip,
Port: uint16(port),
}, nil
}