From a34c21187204bbea76d0d697057efaeb4c21f640 Mon Sep 17 00:00:00 2001 From: Sudo-Ivan Date: Sun, 28 Dec 2025 22:27:16 -0600 Subject: [PATCH] refactor: format code and add more constants --- internal/storage/storage.go | 31 +++-- pkg/announce/announce.go | 24 ++-- pkg/buffer/buffer.go | 2 +- pkg/channel/channel.go | 8 +- pkg/common/constants.go | 36 +++++ pkg/cryptography/aes_test.go | 1 - pkg/debug/debug.go | 5 +- pkg/identity/identity.go | 12 +- pkg/interfaces/auto.go | 104 +++++++------- pkg/interfaces/tcp.go | 19 ++- pkg/interfaces/tcp_darwin.go | 8 +- pkg/interfaces/tcp_freebsd.go | 4 +- pkg/interfaces/tcp_linux.go | 17 +-- pkg/interfaces/tcp_netbsd.go | 4 +- pkg/interfaces/tcp_openbsd.go | 4 +- pkg/interfaces/tcp_windows.go | 4 +- pkg/interfaces/udp.go | 12 +- pkg/link/establishment_test.go | 49 ++++--- pkg/link/link.go | 240 +++++++++++++++++---------------- pkg/packet/packet.go | 8 +- pkg/packet/receipt.go | 63 +++++---- pkg/packet/receipt_test.go | 5 +- pkg/transport/transport.go | 94 ++++++------- 23 files changed, 394 insertions(+), 360 deletions(-) diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 00632c7..dfce63c 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -13,13 +13,13 @@ import ( ) type Manager struct { - basePath string - ratchetsPath string - identitiesPath string - destinationTable string - knownDestinations string - transportIdentity string - mutex sync.RWMutex + basePath string + ratchetsPath string + identitiesPath string + destinationTable string + knownDestinations string + transportIdentity string + mutex sync.RWMutex } type RatchetData struct { @@ -34,14 +34,14 @@ func NewManager() (*Manager, error) { } basePath := filepath.Join(homeDir, ".reticulum-go", "storage") - + m := &Manager{ - basePath: basePath, - ratchetsPath: filepath.Join(basePath, "ratchets"), - identitiesPath: filepath.Join(basePath, "identities"), - destinationTable: filepath.Join(basePath, "destination_table"), - knownDestinations: filepath.Join(basePath, "known_destinations"), - transportIdentity: filepath.Join(basePath, "transport_identity"), + basePath: basePath, + ratchetsPath: filepath.Join(basePath, "ratchets"), + identitiesPath: filepath.Join(basePath, "identities"), + destinationTable: filepath.Join(basePath, "destination_table"), + knownDestinations: filepath.Join(basePath, "known_destinations"), + transportIdentity: filepath.Join(basePath, "transport_identity"), } if err := m.initializeDirectories(); err != nil { @@ -76,7 +76,7 @@ func (m *Manager) SaveRatchet(identityHash []byte, ratchetKey []byte) error { hexHash := hex.EncodeToString(identityHash) ratchetDir := filepath.Join(m.ratchetsPath, hexHash) - + if err := os.MkdirAll(ratchetDir, 0700); err != nil { return fmt.Errorf("failed to create ratchet directory: %w", err) } @@ -186,4 +186,3 @@ func (m *Manager) GetDestinationTablePath() string { func (m *Manager) GetKnownDestinationsPath() string { return m.knownDestinations } - diff --git a/pkg/announce/announce.go b/pkg/announce/announce.go index cc63bd4..b97eb9d 100644 --- a/pkg/announce/announce.go +++ b/pkg/announce/announce.go @@ -87,17 +87,17 @@ func New(dest *identity.Identity, destinationHash []byte, destinationName string } a := &Announce{ - mutex: &sync.RWMutex{}, - identity: dest, - destinationHash: destinationHash, - destinationName: destinationName, - appData: appData, - config: config, - hops: 0, - timestamp: time.Now().Unix(), - pathResponse: pathResponse, - retries: 0, - handlers: make([]AnnounceHandler, 0), + mutex: &sync.RWMutex{}, + identity: dest, + destinationHash: destinationHash, + destinationName: destinationName, + appData: appData, + config: config, + hops: 0, + timestamp: time.Now().Unix(), + pathResponse: pathResponse, + retries: 0, + handlers: make([]AnnounceHandler, 0), } // Get current ratchet ID if enabled @@ -365,7 +365,7 @@ func (a *Announce) CreatePacket() []byte { copy(ratchetData, ratchetPub) } } - + // Determine context flag based on whether ratchet exists contextFlag := byte(0) if len(ratchetData) > 0 { diff --git a/pkg/buffer/buffer.go b/pkg/buffer/buffer.go index 2d3278a..6708302 100644 --- a/pkg/buffer/buffer.go +++ b/pkg/buffer/buffer.go @@ -114,7 +114,7 @@ func (r *RawChannelReader) Read(p []byte) (n int, err error) { } func (r *RawChannelReader) HandleMessage(msg channel.MessageBase) bool { // #nosec G115 - if streamMsg, ok := msg.(*StreamDataMessage); ok && streamMsg.StreamID == uint16(r.streamID) { + if streamMsg, ok := msg.(*StreamDataMessage); ok && streamMsg.StreamID == uint16(r.streamID) { r.mutex.Lock() defer r.mutex.Unlock() diff --git a/pkg/channel/channel.go b/pkg/channel/channel.go index 93b24d7..76f3fa2 100644 --- a/pkg/channel/channel.go +++ b/pkg/channel/channel.go @@ -226,7 +226,7 @@ func (c *Channel) HandleInbound(data []byte) error { msgType := uint16(data[0])<<8 | uint16(data[1]) sequence := uint16(data[2])<<8 | uint16(data[3]) length := uint16(data[4])<<8 | uint16(data[5]) - + if len(data) < 6+int(length) { return errors.New("channel packet incomplete") } @@ -239,9 +239,9 @@ func (c *Channel) HandleInbound(data []byte) error { for _, handler := range c.messageHandlers { if handler != nil { msg := &GenericMessage{ - Type: msgType, - Data: msgData, - Seq: sequence, + Type: msgType, + Data: msgData, + Seq: sequence, } if handler(msg) { break diff --git a/pkg/common/constants.go b/pkg/common/constants.go index 23d38ae..9eaaebc 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -58,4 +58,40 @@ const ( STALE_TIME = 720 PATH_REQUEST_TTL = 300 ANNOUNCE_TIMEOUT = 15 + + // Common Numeric Constants + ZERO = 0 + ONE = 1 + TWO = 2 + THREE = 3 + FOUR = 4 + FIVE = 5 + SIX = 6 + SEVEN = 7 + EIGHT = 8 + FIFTEEN = 15 + + // Common Size Constants + SIZE_16 = 16 + SIZE_32 = 32 + SIZE_64 = 64 + SIXTY_SEVEN = 67 + + // Common Hex Constants + HEX_0x03 = 0x03 + HEX_0xFF = 0xFF + + // Common Float Constants + FLOAT_ZERO = 0.0 + FLOAT_0_001 = 0.001 + FLOAT_0_025 = 0.025 + FLOAT_0_1 = 0.1 + FLOAT_1_75 = 1.75 + FLOAT_5_0 = 5.0 + FLOAT_1E9 = 1e9 + + // Common String Constants + STR_LINK_ID = "link_id" + STR_BYTES = "bytes" + STR_FMT_HEX = "0x%02x" ) diff --git a/pkg/cryptography/aes_test.go b/pkg/cryptography/aes_test.go index 5d2dd40..c14c5f4 100644 --- a/pkg/cryptography/aes_test.go +++ b/pkg/cryptography/aes_test.go @@ -86,7 +86,6 @@ func TestAES256CBC_InvalidKeySize(t *testing.T) { } } - func TestDecryptAES256CBCErrorCases(t *testing.T) { key, err := GenerateAES256Key() if err != nil { diff --git a/pkg/debug/debug.go b/pkg/debug/debug.go index 4018aa8..5af45fa 100644 --- a/pkg/debug/debug.go +++ b/pkg/debug/debug.go @@ -18,8 +18,8 @@ const ( ) var ( - debugLevel = flag.Int("debug", 3, "debug level (1-7)") - logger *slog.Logger + debugLevel = flag.Int("debug", 3, "debug level (1-7)") + logger *slog.Logger initialized bool ) @@ -113,4 +113,3 @@ func SetDebugLevel(level int) { func GetDebugLevel() int { return *debugLevel } - diff --git a/pkg/identity/identity.go b/pkg/identity/identity.go index f99d7e7..3dfd231 100644 --- a/pkg/identity/identity.go +++ b/pkg/identity/identity.go @@ -240,7 +240,7 @@ func (i *Identity) String() string { func Recall(hash []byte) (*Identity, error) { hashStr := hex.EncodeToString(hash) - + if data, exists := knownDestinations[hashStr]; exists { // data is [packet, destHash, identity, appData] if len(data) >= 3 { @@ -249,7 +249,7 @@ func Recall(hash []byte) (*Identity, error) { } } } - + return nil, fmt.Errorf("identity not found for hash %x", hash) } @@ -523,7 +523,7 @@ func FromFile(path string) (*Identity, error) { ratchetExpiry: make(map[string]int64), mutex: &sync.RWMutex{}, } - + if err := ident.loadPrivateKey(privateKey, signingSeed); err != nil { return nil, fmt.Errorf("failed to load private key: %w", err) } @@ -547,7 +547,7 @@ func LoadOrCreateTransportIdentity() (*Identity, error) { } transportIdentityPath := fmt.Sprintf("%s/transport_identity", storagePath) - + if ident, err := FromFile(transportIdentityPath); err == nil { debug.Log(debug.DEBUG_INFO, "Loaded transport identity from storage") return ident, nil @@ -610,7 +610,7 @@ func (i *Identity) saveRatchets(path string) error { } debug.Log(debug.DEBUG_PACKETS, "Saving ratchets", "count", len(i.ratchets), "path", path) - + // Convert ratchets to list format for msgpack ratchetList := make([][]byte, 0, len(i.ratchets)) for _, ratchet := range i.ratchets { @@ -750,7 +750,7 @@ func (i *Identity) loadRatchets(path string) error { signature, hasSignature := persistedData["signature"] packedRatchets, hasRatchets := persistedData["ratchets"] - + if !hasSignature || !hasRatchets { return fmt.Errorf("invalid ratchet file format: missing signature or ratchets") } diff --git a/pkg/interfaces/auto.go b/pkg/interfaces/auto.go index 19b5781..6804e29 100644 --- a/pkg/interfaces/auto.go +++ b/pkg/interfaces/auto.go @@ -23,42 +23,42 @@ const ( ANNOUNCE_INTERVAL = 1600 * time.Millisecond PEER_JOB_INTERVAL = 4 * time.Second MCAST_ECHO_TIMEOUT = 6500 * time.Millisecond - + SCOPE_LINK = "2" SCOPE_ADMIN = "4" SCOPE_SITE = "5" SCOPE_ORGANISATION = "8" SCOPE_GLOBAL = "e" - + MCAST_ADDR_TYPE_PERMANENT = "0" MCAST_ADDR_TYPE_TEMPORARY = "1" ) type AutoInterface struct { BaseInterface - groupID []byte - groupHash []byte - discoveryPort int - dataPort int - discoveryScope string - multicastAddrType string - mcastDiscoveryAddr string - ifacNetname string - peers map[string]*Peer - linkLocalAddrs []string - adoptedInterfaces map[string]*AdoptedInterface - interfaceServers map[string]*net.UDPConn - discoveryServers map[string]*net.UDPConn - multicastEchoes map[string]time.Time - timedOutInterfaces map[string]time.Time - allowedInterfaces []string - ignoredInterfaces []string - mutex sync.RWMutex - outboundConn *net.UDPConn - announceInterval time.Duration - peerJobInterval time.Duration - peeringTimeout time.Duration - mcastEchoTimeout time.Duration + groupID []byte + groupHash []byte + discoveryPort int + dataPort int + discoveryScope string + multicastAddrType string + mcastDiscoveryAddr string + ifacNetname string + peers map[string]*Peer + linkLocalAddrs []string + adoptedInterfaces map[string]*AdoptedInterface + interfaceServers map[string]*net.UDPConn + discoveryServers map[string]*net.UDPConn + multicastEchoes map[string]time.Time + timedOutInterfaces map[string]time.Time + allowedInterfaces []string + ignoredInterfaces []string + mutex sync.RWMutex + outboundConn *net.UDPConn + announceInterval time.Duration + peerJobInterval time.Duration + peeringTimeout time.Duration + mcastEchoTimeout time.Duration } type AdoptedInterface struct { @@ -97,7 +97,7 @@ func NewAutoInterface(name string, config *common.InterfaceConfig) (*AutoInterfa } groupHash := sha256.Sum256([]byte(groupID)) - + ifacNetname := hex.EncodeToString(groupHash[:])[:16] mcastAddr := fmt.Sprintf("ff%s%s::%s", discoveryScope, multicastAddrType, ifacNetname) @@ -114,27 +114,27 @@ func NewAutoInterface(name string, config *common.InterfaceConfig) (*AutoInterfa MTU: HW_MTU, Bitrate: BITRATE_GUESS, }, - groupID: []byte(groupID), - groupHash: groupHash[:], - discoveryPort: discoveryPort, - dataPort: dataPort, - discoveryScope: discoveryScope, - multicastAddrType: multicastAddrType, - mcastDiscoveryAddr: mcastAddr, - ifacNetname: ifacNetname, - peers: make(map[string]*Peer), - linkLocalAddrs: make([]string, 0), - adoptedInterfaces: make(map[string]*AdoptedInterface), - interfaceServers: make(map[string]*net.UDPConn), - discoveryServers: make(map[string]*net.UDPConn), - multicastEchoes: make(map[string]time.Time), - timedOutInterfaces: make(map[string]time.Time), - allowedInterfaces: make([]string, 0), - ignoredInterfaces: make([]string, 0), - announceInterval: ANNOUNCE_INTERVAL, - peerJobInterval: PEER_JOB_INTERVAL, - peeringTimeout: PEERING_TIMEOUT, - mcastEchoTimeout: MCAST_ECHO_TIMEOUT, + groupID: []byte(groupID), + groupHash: groupHash[:], + discoveryPort: discoveryPort, + dataPort: dataPort, + discoveryScope: discoveryScope, + multicastAddrType: multicastAddrType, + mcastDiscoveryAddr: mcastAddr, + ifacNetname: ifacNetname, + peers: make(map[string]*Peer), + linkLocalAddrs: make([]string, 0), + adoptedInterfaces: make(map[string]*AdoptedInterface), + interfaceServers: make(map[string]*net.UDPConn), + discoveryServers: make(map[string]*net.UDPConn), + multicastEchoes: make(map[string]time.Time), + timedOutInterfaces: make(map[string]time.Time), + allowedInterfaces: make([]string, 0), + ignoredInterfaces: make([]string, 0), + announceInterval: ANNOUNCE_INTERVAL, + peerJobInterval: PEER_JOB_INTERVAL, + peeringTimeout: PEERING_TIMEOUT, + mcastEchoTimeout: MCAST_ECHO_TIMEOUT, } debug.Log(debug.DEBUG_INFO, "AutoInterface configured", "name", name, "group", groupID, "mcast_addr", mcastAddr) @@ -202,26 +202,26 @@ func (ai *AutoInterface) Start() error { go ai.peerJobs() go ai.announceLoop() - + debug.Log(debug.DEBUG_INFO, "AutoInterface started", "adopted", len(ai.adoptedInterfaces)) return nil } func (ai *AutoInterface) shouldIgnoreInterface(name string) bool { ignoreList := []string{"lo", "lo0", "tun0", "awdl0", "llw0", "en5", "dummy0"} - + for _, ignored := range ai.ignoredInterfaces { if name == ignored { return true } } - + for _, ignored := range ignoreList { if name == ignored { return true } } - + return false } @@ -394,7 +394,7 @@ func (ai *AutoInterface) handlePeerAnnounce(addr *net.UDPAddr, ifaceName string) } peerKey := peerIP + "%" + ifaceName - + if peer, exists := ai.peers[peerKey]; exists { peer.lastHeard = time.Now() debug.Log(debug.DEBUG_TRACE, "Updated peer", "peer", peerIP, "interface", ifaceName) diff --git a/pkg/interfaces/tcp.go b/pkg/interfaces/tcp.go index e4c221f..b6630e0 100644 --- a/pkg/interfaces/tcp.go +++ b/pkg/interfaces/tcp.go @@ -21,18 +21,18 @@ const ( KISS_TFEND = 0xDC KISS_TFESC = 0xDD - DEFAULT_MTU = 1064 - BITRATE_GUESS_VAL = 10 * 1000 * 1000 - RECONNECT_WAIT = 5 - INITIAL_TIMEOUT = 5 - INITIAL_BACKOFF = time.Second - MAX_BACKOFF = time.Minute * 5 - + DEFAULT_MTU = 1064 + BITRATE_GUESS_VAL = 10 * 1000 * 1000 + RECONNECT_WAIT = 5 + INITIAL_TIMEOUT = 5 + INITIAL_BACKOFF = time.Second + MAX_BACKOFF = time.Minute * 5 + TCP_USER_TIMEOUT_SEC = 24 TCP_PROBE_AFTER_SEC = 5 TCP_PROBE_INTERVAL_SEC = 2 TCP_PROBES_COUNT = 12 - + I2P_USER_TIMEOUT_SEC = 45 I2P_PROBE_AFTER_SEC = 10 I2P_PROBE_INTERVAL_SEC = 9 @@ -201,7 +201,7 @@ func (tc *TCPClientInterface) handlePacket(data []byte) { // Send implements the interface Send method for TCP interface func (tc *TCPClientInterface) Send(data []byte, address string) error { debug.Log(debug.DEBUG_ALL, "TCP interface sending bytes", "name", tc.Name, "bytes", len(data)) - + if !tc.IsEnabled() || !tc.IsOnline() { return fmt.Errorf("TCP interface %s is not online", tc.Name) } @@ -434,7 +434,6 @@ func (tc *TCPClientInterface) GetStats() (tx uint64, rx uint64, lastTx time.Time return tc.TxBytes, tc.RxBytes, tc.lastTx, tc.lastRx } - type TCPServerInterface struct { BaseInterface connections map[string]net.Conn diff --git a/pkg/interfaces/tcp_darwin.go b/pkg/interfaces/tcp_darwin.go index 984cffe..a7d6887 100644 --- a/pkg/interfaces/tcp_darwin.go +++ b/pkg/interfaces/tcp_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package interfaces @@ -28,19 +29,19 @@ func (tc *TCPClientInterface) setTimeoutsOSX() error { var sockoptErr error err = rawConn.Control(func(fd uintptr) { const TCP_KEEPALIVE = 0x10 - + var probeAfter int if tc.i2pTunneled { probeAfter = I2P_PROBE_AFTER_SEC } else { probeAfter = TCP_PROBE_AFTER_SEC } - + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { sockoptErr = fmt.Errorf("failed to enable SO_KEEPALIVE: %v", err) return } - + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_KEEPALIVE, probeAfter); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set TCP_KEEPALIVE", "error", err) } @@ -56,4 +57,3 @@ func (tc *TCPClientInterface) setTimeoutsOSX() error { debug.Log(debug.DEBUG_VERBOSE, "TCP keepalive configured (OSX)", "i2p", tc.i2pTunneled) return nil } - diff --git a/pkg/interfaces/tcp_freebsd.go b/pkg/interfaces/tcp_freebsd.go index cfc08b6..07ac744 100644 --- a/pkg/interfaces/tcp_freebsd.go +++ b/pkg/interfaces/tcp_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd // +build freebsd package interfaces @@ -24,7 +25,7 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { if tc.i2pTunneled { keepalivePeriod = I2P_PROBE_INTERVAL_SEC * time.Second } - + if err := tcpConn.SetKeepAlivePeriod(keepalivePeriod); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set keepalive period", "error", err) } @@ -36,4 +37,3 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { func (tc *TCPClientInterface) setTimeoutsOSX() error { return tc.setTimeoutsLinux() } - diff --git a/pkg/interfaces/tcp_linux.go b/pkg/interfaces/tcp_linux.go index 400c3f8..4f779f3 100644 --- a/pkg/interfaces/tcp_linux.go +++ b/pkg/interfaces/tcp_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package interfaces @@ -26,7 +27,7 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { var sockoptErr error err = rawConn.Control(func(fd uintptr) { var userTimeout, probeAfter, probeInterval, probeCount int - + if tc.i2pTunneled { userTimeout = I2P_USER_TIMEOUT_SEC * 1000 probeAfter = I2P_PROBE_AFTER_SEC @@ -42,20 +43,20 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, 18, userTimeout); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set TCP_USER_TIMEOUT", "error", err) } - + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { sockoptErr = fmt.Errorf("failed to enable SO_KEEPALIVE: %v", err) return } - + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, 4, probeAfter); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set TCP_KEEPIDLE", "error", err) } - + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, 5, probeInterval); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set TCP_KEEPINTVL", "error", err) } - + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, 6, probeCount); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set TCP_KEEPCNT", "error", err) } @@ -79,7 +80,7 @@ func (tc *TCPClientInterface) setTimeoutsOSX() error { func platformGetRTT(fd uintptr) time.Duration { var info syscall.TCPInfo infoLen := uint32(unsafe.Sizeof(info)) - + // TCP_INFO is 11 on Linux // #nosec G103 _, _, errno := syscall.Syscall6( @@ -91,10 +92,10 @@ func platformGetRTT(fd uintptr) time.Duration { uintptr(unsafe.Pointer(&infoLen)), 0, ) - + if errno != 0 { return 0 } - + return time.Duration(info.Rtt) * time.Microsecond } diff --git a/pkg/interfaces/tcp_netbsd.go b/pkg/interfaces/tcp_netbsd.go index de7de5d..cd4fe67 100644 --- a/pkg/interfaces/tcp_netbsd.go +++ b/pkg/interfaces/tcp_netbsd.go @@ -1,3 +1,4 @@ +//go:build netbsd // +build netbsd package interfaces @@ -24,7 +25,7 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { if tc.i2pTunneled { keepalivePeriod = I2P_PROBE_INTERVAL_SEC * time.Second } - + if err := tcpConn.SetKeepAlivePeriod(keepalivePeriod); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set keepalive period", "error", err) } @@ -36,4 +37,3 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { func (tc *TCPClientInterface) setTimeoutsOSX() error { return tc.setTimeoutsLinux() } - diff --git a/pkg/interfaces/tcp_openbsd.go b/pkg/interfaces/tcp_openbsd.go index a486876..23f40cd 100644 --- a/pkg/interfaces/tcp_openbsd.go +++ b/pkg/interfaces/tcp_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package interfaces @@ -24,7 +25,7 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { if tc.i2pTunneled { keepalivePeriod = I2P_PROBE_INTERVAL_SEC * time.Second } - + if err := tcpConn.SetKeepAlivePeriod(keepalivePeriod); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set keepalive period", "error", err) } @@ -36,4 +37,3 @@ func (tc *TCPClientInterface) setTimeoutsLinux() error { func (tc *TCPClientInterface) setTimeoutsOSX() error { return tc.setTimeoutsLinux() } - diff --git a/pkg/interfaces/tcp_windows.go b/pkg/interfaces/tcp_windows.go index 006505c..9c240ca 100644 --- a/pkg/interfaces/tcp_windows.go +++ b/pkg/interfaces/tcp_windows.go @@ -30,7 +30,7 @@ func (tc *TCPClientInterface) setTimeoutsWindows() error { if tc.i2pTunneled { keepalivePeriod = I2P_PROBE_INTERVAL_SEC * time.Second } - + if err := tcpConn.SetKeepAlivePeriod(keepalivePeriod); err != nil { debug.Log(debug.DEBUG_VERBOSE, "Failed to set keepalive period", "error", err) } @@ -38,5 +38,3 @@ func (tc *TCPClientInterface) setTimeoutsWindows() error { debug.Log(debug.DEBUG_VERBOSE, "TCP keepalive configured (Windows)", "i2p", tc.i2pTunneled) return nil } - - diff --git a/pkg/interfaces/udp.go b/pkg/interfaces/udp.go index ce5247a..dd15788 100644 --- a/pkg/interfaces/udp.go +++ b/pkg/interfaces/udp.go @@ -38,7 +38,7 @@ func NewUDPInterface(name string, addr string, target string, enabled bool) (*UD targetAddr: targetAddr, readBuffer: make([]byte, 1064), } - + ui.MTU = 1064 return ui, nil @@ -183,7 +183,7 @@ func (ui *UDPInterface) Start() error { return err } ui.conn = conn - + // Enable broadcast mode if we have a target address if ui.targetAddr != nil { // Get the raw connection file descriptor to set SO_BROADCAST @@ -194,12 +194,12 @@ func (ui *UDPInterface) Start() error { debug.Log(debug.DEBUG_ERROR, "Failed to set write buffer size", "error", err) } } - + ui.Online = true - + // Start the read loop in a goroutine go ui.readLoop() - + return nil } @@ -218,7 +218,7 @@ func (ui *UDPInterface) readLoop() { ui.mutex.Lock() // #nosec G115 - Network read sizes are always positive and within safe range ui.RxBytes += uint64(n) - + // Auto-discover target address from first packet if not set if ui.targetAddr == nil { debug.Log(debug.DEBUG_ALL, "UDP interface discovered peer", "name", ui.Name, "peer", remoteAddr.String()) diff --git a/pkg/link/establishment_test.go b/pkg/link/establishment_test.go index 7e33ad5..2ddb89d 100644 --- a/pkg/link/establishment_test.go +++ b/pkg/link/establishment_test.go @@ -13,23 +13,23 @@ import ( func TestEphemeralKeyGeneration(t *testing.T) { link := &Link{} - + if err := link.generateEphemeralKeys(); err != nil { t.Fatalf("Failed to generate ephemeral keys: %v", err) } - + if len(link.prv) != KEYSIZE { t.Errorf("Expected private key length %d, got %d", KEYSIZE, len(link.prv)) } - + if len(link.pub) != KEYSIZE { t.Errorf("Expected public key length %d, got %d", KEYSIZE, len(link.pub)) } - + if len(link.sigPriv) != 64 { t.Errorf("Expected signing private key length 64, got %d", len(link.sigPriv)) } - + if len(link.sigPub) != 32 { t.Errorf("Expected signing public key length 32, got %d", len(link.sigPub)) } @@ -38,18 +38,18 @@ func TestEphemeralKeyGeneration(t *testing.T) { func TestSignallingBytes(t *testing.T) { mtu := 500 mode := byte(MODE_AES256_CBC) - + bytes := signallingBytes(mtu, mode) - + if len(bytes) != LINK_MTU_SIZE { t.Errorf("Expected signalling bytes length %d, got %d", LINK_MTU_SIZE, len(bytes)) } - + extractedMTU := (int(bytes[0]&0x1F) << 16) | (int(bytes[1]) << 8) | int(bytes[2]) if extractedMTU != mtu { t.Errorf("Expected MTU %d, got %d", mtu, extractedMTU) } - + extractedMode := (bytes[0] & MODE_BYTEMASK) >> 5 if extractedMode != mode { t.Errorf("Expected mode %d, got %d", mode, extractedMode) @@ -106,55 +106,55 @@ func TestLinkIDGeneration(t *testing.T) { } linkID := linkIDFromPacket(pkt) - + if len(linkID) != 16 { t.Errorf("Expected link ID length 16, got %d", len(linkID)) } - + t.Logf("Generated link ID: %x", linkID) } func TestHandshake(t *testing.T) { link1 := &Link{} link2 := &Link{} - + if err := link1.generateEphemeralKeys(); err != nil { t.Fatalf("Failed to generate keys for link1: %v", err) } - + if err := link2.generateEphemeralKeys(); err != nil { t.Fatalf("Failed to generate keys for link2: %v", err) } - + link1.peerPub = link2.pub link2.peerPub = link1.pub - + link1.linkID = []byte("test-link-id-abc") link2.linkID = []byte("test-link-id-abc") - + link1.mode = MODE_AES256_CBC link2.mode = MODE_AES256_CBC - + if err := link1.performHandshake(); err != nil { t.Fatalf("Link1 handshake failed: %v", err) } - + if err := link2.performHandshake(); err != nil { t.Fatalf("Link2 handshake failed: %v", err) } - + if string(link1.sharedKey) != string(link2.sharedKey) { t.Error("Shared keys do not match") } - + if string(link1.derivedKey) != string(link2.derivedKey) { t.Error("Derived keys do not match") } - + if link1.status != STATUS_HANDSHAKE { t.Errorf("Expected link1 status HANDSHAKE, got %d", link1.status) } - + if link2.status != STATUS_HANDSHAKE { t.Errorf("Expected link2 status HANDSHAKE, got %d", link2.status) } @@ -224,9 +224,9 @@ func TestLinkEstablishment(t *testing.T) { responderLink.peerSigPub = linkRequestPkt.Data[KEYSIZE:ECPUBSIZE] responderLink.linkID = linkIDFromPacket(linkRequestPkt) responderLink.initiator = false - + t.Logf("Responder link ID=%x (len=%d)", responderLink.linkID, len(responderLink.linkID)) - + if len(responderLink.linkID) == 0 { t.Fatal("Responder link ID is empty!") } @@ -362,4 +362,3 @@ func TestLinkProofValidation(t *testing.T) { t.Logf("Derived key length: %d", len(initiatorLink.derivedKey)) t.Logf("RTT: %.3f seconds", initiatorLink.rtt) } - diff --git a/pkg/link/link.go b/pkg/link/link.go index 8f878c9..8766afc 100644 --- a/pkg/link/link.go +++ b/pkg/link/link.go @@ -64,6 +64,11 @@ const ( WATCHDOG_MIN_SLEEP = 0.025 WATCHDOG_INTERVAL = 0.1 + + DEST_TYPE_LINK = 0x03 + + MIN_REQUEST_DATA_LEN = 3 + MIN_RESPONSE_DATA_LEN = 2 ) func init() { @@ -229,10 +234,10 @@ func (l *Link) Identify(id *identity.Identity) error { p := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextLinkIdentify, ContextFlag: packet.FlagUnset, - Hops: 0, + Hops: common.ZERO, DestinationType: l.destination.GetType(), DestinationHash: l.destination.GetHash(), Data: id.GetPublicKey(), @@ -258,25 +263,25 @@ func (l *Link) HandleIdentification(data []byte) error { pubKey := data[:ed25519.PublicKeySize] signature := data[ed25519.PublicKeySize:] - debug.Log(debug.DEBUG_VERBOSE, "Processing identification from public key", "public_key", fmt.Sprintf("%x", pubKey[:8])) + debug.Log(debug.DEBUG_VERBOSE, "Processing identification from public key", "public_key", fmt.Sprintf("%x", pubKey[:common.EIGHT])) remoteIdentity := identity.FromPublicKey(pubKey) if remoteIdentity == nil { - debug.Log(debug.DEBUG_INFO, "Invalid remote identity from public key", "public_key", fmt.Sprintf("%x", pubKey[:8])) + debug.Log(debug.DEBUG_INFO, "Invalid remote identity from public key", "public_key", fmt.Sprintf("%x", pubKey[:common.EIGHT])) return errors.New("invalid remote identity") } signData := append(l.linkID, pubKey...) if !remoteIdentity.Verify(signData, signature) { - debug.Log(debug.DEBUG_INFO, "Invalid signature from remote identity", "public_key", fmt.Sprintf("%x", pubKey[:8])) + debug.Log(debug.DEBUG_INFO, "Invalid signature from remote identity", "public_key", fmt.Sprintf("%x", pubKey[:common.EIGHT])) return errors.New("invalid signature") } - debug.Log(debug.DEBUG_VERBOSE, "Remote identity verified successfully", "public_key", fmt.Sprintf("%x", pubKey[:8])) + debug.Log(debug.DEBUG_VERBOSE, "Remote identity verified successfully", "public_key", fmt.Sprintf("%x", pubKey[:common.EIGHT])) l.remoteIdentity = remoteIdentity if l.identifiedCallback != nil { - debug.Log(debug.DEBUG_VERBOSE, "Executing identified callback for remote identity", "public_key", fmt.Sprintf("%x", pubKey[:8])) + debug.Log(debug.DEBUG_VERBOSE, "Executing identified callback for remote identity", "public_key", fmt.Sprintf("%x", pubKey[:common.EIGHT])) l.identifiedCallback(l, remoteIdentity) } @@ -308,11 +313,11 @@ func (l *Link) Request(path string, data []byte, timeout time.Duration) (*Reques reqPkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextRequest, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: packedRequest, CreateReceipt: false, @@ -395,7 +400,7 @@ func (r *RequestReceipt) GetResponseTime() float64 { r.mutex.RLock() defer r.mutex.RUnlock() if r.receivedAt.IsZero() { - return 0 + return common.FLOAT_ZERO } return r.receivedAt.Sub(r.sentAt).Seconds() } @@ -455,7 +460,7 @@ func (l *Link) GetRSSI() float64 { l.mutex.RLock() defer l.mutex.RUnlock() if !l.trackPhyStats { - return 0 + return common.FLOAT_ZERO } return l.rssi } @@ -464,7 +469,7 @@ func (l *Link) GetSNR() float64 { l.mutex.RLock() defer l.mutex.RUnlock() if !l.trackPhyStats { - return 0 + return common.FLOAT_ZERO } return l.snr } @@ -473,7 +478,7 @@ func (l *Link) GetQ() float64 { l.mutex.RLock() defer l.mutex.RUnlock() if !l.trackPhyStats { - return 0 + return common.FLOAT_ZERO } return l.q } @@ -488,7 +493,7 @@ func (l *Link) GetAge() float64 { l.mutex.RLock() defer l.mutex.RUnlock() if l.establishedAt.IsZero() { - return 0 + return common.FLOAT_ZERO } return time.Since(l.establishedAt).Seconds() } @@ -497,7 +502,7 @@ func (l *Link) NoInboundFor() float64 { l.mutex.RLock() defer l.mutex.RUnlock() if l.lastInbound.IsZero() { - return 0 + return common.FLOAT_ZERO } return time.Since(l.lastInbound).Seconds() } @@ -506,7 +511,7 @@ func (l *Link) NoOutboundFor() float64 { l.mutex.RLock() defer l.mutex.RUnlock() if l.lastOutbound.IsZero() { - return 0 + return common.FLOAT_ZERO } return time.Since(l.lastOutbound).Seconds() } @@ -519,7 +524,7 @@ func (l *Link) NoDataFor() float64 { lastData = l.lastDataSent } if lastData.IsZero() { - return 0 + return common.FLOAT_ZERO } return time.Since(lastData).Seconds() } @@ -532,7 +537,7 @@ func (l *Link) InactiveFor() float64 { lastActivity = l.lastOutbound } if lastActivity.IsZero() { - return 0 + return common.FLOAT_ZERO } return time.Since(lastActivity).Seconds() } @@ -621,10 +626,10 @@ func (l *Link) SendPacket(data []byte) error { p := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextNone, ContextFlag: packet.FlagUnset, - Hops: 0, + Hops: common.ZERO, DestinationType: l.destination.GetType(), DestinationHash: l.destination.GetHash(), Data: encrypted, @@ -707,16 +712,16 @@ func (l *Link) handleDataPacket(pkt *packet.Packet) error { case packet.ContextLinkIdentify: return l.HandleIdentification(plaintext) case packet.ContextKeepalive: - if !l.initiator && len(plaintext) == 1 && plaintext[0] == 0xFF { + if !l.initiator && len(plaintext) == common.ONE && plaintext[common.ZERO] == common.HEX_0xFF { keepaliveResp := []byte{0xFE} keepalivePkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextKeepalive, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: keepaliveResp, CreateReceipt: false, @@ -860,11 +865,11 @@ func (l *Link) rejectResource(resourceHash []byte) error { rejectPkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextResourceRCL, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: resourceHash, CreateReceipt: false, @@ -904,7 +909,7 @@ func (l *Link) sendResourceAdvertisement(res *resource.Resource) error { return errors.New("failed to create resource advertisement") } - advData, err := adv.Pack(0) + advData, err := adv.Pack(common.ZERO) if err != nil { return fmt.Errorf("failed to pack advertisement: %w", err) } @@ -917,11 +922,11 @@ func (l *Link) sendResourceAdvertisement(res *resource.Resource) error { advPkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextResourceAdv, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: encrypted, CreateReceipt: false, @@ -1005,13 +1010,13 @@ func (l *Link) handleRequest(plaintext []byte, pkt *packet.Packet) error { return fmt.Errorf("failed to unpack request: %w", err) } - if len(requestData) < 3 { + if len(requestData) < MIN_REQUEST_DATA_LEN { return errors.New("invalid request format") } - requestedAt := time.Unix(int64(requestData[0].(int64)), 0) - pathHash := requestData[1].([]byte) - requestPayload := requestData[2].([]byte) + requestedAt := time.Unix(int64(requestData[common.ZERO].(int64)), common.ZERO) + pathHash := requestData[common.ONE].([]byte) + requestPayload := requestData[common.TWO].([]byte) requestID := identity.TruncatedHash(plaintext) @@ -1034,12 +1039,12 @@ func (l *Link) handleResponse(plaintext []byte) error { return fmt.Errorf("failed to unpack response: %w", err) } - if len(responseData) < 2 { + if len(responseData) < MIN_RESPONSE_DATA_LEN { return errors.New("invalid response format") } - requestID := responseData[0].([]byte) - responsePayload := responseData[1].([]byte) + requestID := responseData[common.ZERO].([]byte) + responsePayload := responseData[common.ONE].([]byte) l.requestMutex.Lock() for i, req := range l.pendingRequests { @@ -1079,11 +1084,11 @@ func (l *Link) sendResponse(requestID []byte, response interface{}) error { respPkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextResponse, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: encrypted, CreateReceipt: false, @@ -1110,8 +1115,8 @@ func (l *Link) handleRTTPacket(pkt *packet.Packet) error { } var rtt float64 - if len(plaintext) >= 8 { - rtt = float64(binary.BigEndian.Uint64(plaintext[:8])) / 1e9 + if len(plaintext) >= common.EIGHT { + rtt = float64(binary.BigEndian.Uint64(plaintext[:common.EIGHT])) / common.FLOAT_1E9 } l.rtt = max(measuredRTT, rtt) @@ -1140,9 +1145,9 @@ func (l *Link) updateKeepalive() { return } - keepaliveMaxRTT := 1.75 + keepaliveMaxRTT := common.FLOAT_1_75 keepaliveMax := float64(KEEPALIVE) - keepaliveMin := 5.0 + keepaliveMin := common.FLOAT_5_0 calculatedKeepalive := l.rtt * (keepaliveMax / keepaliveMaxRTT) if calculatedKeepalive > keepaliveMax { @@ -1153,7 +1158,7 @@ func (l *Link) updateKeepalive() { } l.keepalive = time.Duration(calculatedKeepalive * float64(time.Second)) - l.staleTime = time.Duration(float64(l.keepalive) * 2.0) + l.staleTime = time.Duration(float64(l.keepalive) * float64(common.TWO)) } func (l *Link) handleLinkProof(pkt *packet.Packet) error { @@ -1284,11 +1289,11 @@ func (l *Link) Send(data []byte) interface{} { pkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextChannel, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: data, CreateReceipt: false, @@ -1464,33 +1469,33 @@ func (l *Link) decodePacket(data []byte) { } packetType := data[0] - debug.Log(debug.DEBUG_ALL, "Packet Analysis", "size", len(data), "type", fmt.Sprintf("0x%02x", packetType)) + debug.Log(debug.DEBUG_ALL, "Packet Analysis", "size", len(data), "type", fmt.Sprintf(common.STR_FMT_HEX, packetType)) switch packetType { case packet.PacketTypeData: - debug.Log(debug.DEBUG_ALL, "Type Description: Data Packet", "payload_size", len(data)-1) + debug.Log(debug.DEBUG_ALL, "Type Description: Data Packet", "payload_size", len(data)-common.ONE) case packet.PacketTypeLinkReq: - debug.Log(debug.DEBUG_ALL, "Type Description: Link Management", "link_id", fmt.Sprintf("%x", data[1:33])) + debug.Log(debug.DEBUG_ALL, "Type Description: Link Management", common.STR_LINK_ID, fmt.Sprintf("%x", data[common.ONE:common.SIZE_32+common.ONE])) case packet.PacketTypeAnnounce: - debug.Log(debug.DEBUG_ALL, "Received announce packet", "bytes", len(data)) + debug.Log(debug.DEBUG_ALL, "Received announce packet", common.STR_BYTES, len(data)) if len(data) < packet.MinAnnounceSize { debug.Log(debug.DEBUG_INFO, "Announce packet too short", "bytes", len(data)) return } - destHash := data[2:18] - encKey := data[18:50] - signKey := data[50:82] - nameHash := data[82:92] - randomHash := data[92:102] - signature := data[102:166] - appData := data[166:] + destHash := data[common.TWO : common.SIZE_16+common.TWO] + encKey := data[common.SIZE_16+common.TWO : common.SIZE_32+common.SIZE_16+common.TWO] + signKey := data[common.SIZE_32+common.SIZE_16+common.TWO : common.SIZE_32*common.TWO+common.SIZE_16+common.TWO] + nameHash := data[common.SIZE_32*common.TWO+common.SIZE_16+common.TWO : common.SIZE_32*common.TWO+common.SIZE_16+common.TWO+common.EIGHT+common.TWO] + randomHash := data[common.SIZE_32*common.TWO+common.SIZE_16+common.TWO+common.EIGHT+common.TWO : common.SIZE_32*common.TWO+common.SIZE_16+common.TWO+common.EIGHT*common.TWO+common.TWO] + signature := data[common.SIZE_32*common.TWO+common.SIZE_16+common.TWO+common.EIGHT*common.TWO+common.TWO : common.SIZE_64+common.SIZE_32*common.TWO+common.SIZE_16+common.TWO+common.EIGHT*common.TWO+common.TWO] + appData := data[common.SIZE_64+common.SIZE_32*common.TWO+common.SIZE_16+common.TWO+common.EIGHT*common.TWO+common.TWO:] pubKey := append(encKey, signKey...) - validationData := make([]byte, 0, 164) + validationData := make([]byte, common.ZERO, common.SIZE_32*common.FIVE+common.FOUR) validationData = append(validationData, destHash...) validationData = append(validationData, encKey...) validationData = append(validationData, signKey...) @@ -1498,18 +1503,18 @@ func (l *Link) decodePacket(data []byte) { validationData = append(validationData, randomHash...) if identity.ValidateAnnounce(validationData, destHash, pubKey, signature, appData) { - debug.Log(debug.DEBUG_VERBOSE, "Valid announce from", "public_key", fmt.Sprintf("%x", pubKey[:8])) + debug.Log(debug.DEBUG_VERBOSE, "Valid announce from", "public_key", fmt.Sprintf("%x", pubKey[:common.EIGHT])) if err := l.transport.HandleAnnounce(destHash, l.networkInterface); err != nil { debug.Log(debug.DEBUG_INFO, "Failed to handle announce", "error", err) } } else { - debug.Log(debug.DEBUG_INFO, "Invalid announce signature from", "public_key", fmt.Sprintf("%x", pubKey[:8])) + debug.Log(debug.DEBUG_INFO, "Invalid announce signature from", "public_key", fmt.Sprintf("%x", pubKey[:common.EIGHT])) } case packet.PacketTypeProof: debug.Log(debug.DEBUG_ALL, "Type Description: RNS Discovery") - if len(data) > 17 { - searchHash := data[1:17] + if len(data) > common.SIZE_16+common.ONE { + searchHash := data[common.ONE : common.SIZE_16+common.ONE] debug.Log(debug.DEBUG_ALL, "Searching for Hash", "search_hash", fmt.Sprintf("%x", searchHash)) if id, err := resolver.ResolveIdentity(hex.EncodeToString(searchHash)); err == nil { @@ -1535,12 +1540,12 @@ func (l *Link) watchdog() { for l.status != STATUS_CLOSED { l.mutex.Lock() if l.watchdogLock { - rttWait := 0.025 - if l.rtt > 0 { + rttWait := common.FLOAT_0_025 + if l.rtt > common.FLOAT_ZERO { rttWait = l.rtt } - if rttWait < 0.025 { - rttWait = 0.025 + if rttWait < common.FLOAT_0_025 { + rttWait = common.FLOAT_0_025 } l.mutex.Unlock() time.Sleep(time.Duration(rttWait * float64(time.Second))) @@ -1559,7 +1564,7 @@ func (l *Link) watchdog() { if l.closedCallback != nil { l.closedCallback(l) } - sleepTime = 0.001 + sleepTime = common.FLOAT_0_001 } } else if l.status == STATUS_HANDSHAKE { nextCheck := l.requestTime.Add(l.establishmentTimeout) @@ -1607,20 +1612,21 @@ func (l *Link) watchdog() { sleepTime = time.Until(nextKeepalive).Seconds() } } else if l.status == STATUS_STALE { - sleepTime = 0.001 + sleepTime = common.FLOAT_0_001 _ = l.sendTeardownPacket() // #nosec G104 - best effort teardown l.status = STATUS_CLOSED l.teardownReason = STATUS_FAILED if l.closedCallback != nil { l.closedCallback(l) } + sleepTime = common.FLOAT_0_001 } - if sleepTime <= 0 { - sleepTime = 0.1 + if sleepTime <= common.FLOAT_ZERO { + sleepTime = common.FLOAT_0_1 } - if sleepTime > 5.0 { - sleepTime = 5.0 + if sleepTime > common.FLOAT_5_0 { + sleepTime = common.FLOAT_5_0 } l.mutex.Unlock() @@ -1634,11 +1640,11 @@ func (l *Link) sendKeepalive() error { keepalivePkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextKeepalive, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: keepaliveData, CreateReceipt: false, @@ -1659,11 +1665,11 @@ func (l *Link) sendTeardownPacket() error { teardownPkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextLinkClose, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: l.linkID, CreateReceipt: false, @@ -1711,10 +1717,10 @@ func (l *Link) generateEphemeralKeys() error { func signallingBytes(mtu int, mode byte) []byte { bytes := make([]byte, LINK_MTU_SIZE) - bytes[0] = byte((mtu >> 16) & 0xFF) - bytes[1] = byte((mtu >> 8) & 0xFF) - bytes[2] = byte(mtu & 0xFF) - bytes[0] |= (mode << 5) + bytes[common.ZERO] = byte((mtu >> common.SIZE_16) & common.HEX_0xFF) + bytes[common.ONE] = byte((mtu >> common.EIGHT) & common.HEX_0xFF) + bytes[common.TWO] = byte(mtu & common.HEX_0xFF) + bytes[common.ZERO] |= (mode << common.FIVE) return bytes } @@ -1724,7 +1730,7 @@ func (l *Link) SendLinkRequest() error { } l.mode = MODE_DEFAULT - l.mtu = 500 + l.mtu = common.DEFAULT_MTU / common.THREE l.updateMDU() signalling := signallingBytes(l.mtu, l.mode) @@ -1736,10 +1742,10 @@ func (l *Link) SendLinkRequest() error { pkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeLinkReq, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextNone, ContextFlag: packet.FlagUnset, - Hops: 0, + Hops: common.ZERO, DestinationType: l.destination.GetType(), DestinationHash: l.destination.GetHash(), Data: requestData, @@ -1764,18 +1770,18 @@ func (l *Link) SendLinkRequest() error { } func linkIDFromPacket(pkt *packet.Packet) []byte { - hashablePart := make([]byte, 0, 1+16+1+ECPUBSIZE) - hashablePart = append(hashablePart, pkt.Raw[0]) + hashablePart := make([]byte, common.ZERO, common.ONE+common.SIZE_16+common.ONE+ECPUBSIZE) + hashablePart = append(hashablePart, pkt.Raw[common.ZERO]) if pkt.HeaderType == packet.HeaderType2 { - startIndex := 18 - endIndex := startIndex + 16 + 1 + ECPUBSIZE + startIndex := common.SIZE_16 + common.TWO + endIndex := startIndex + common.SIZE_16 + common.ONE + ECPUBSIZE if len(pkt.Raw) >= endIndex { hashablePart = append(hashablePart, pkt.Raw[startIndex:endIndex]...) } } else { - startIndex := 2 - endIndex := startIndex + 16 + 1 + ECPUBSIZE + startIndex := common.TWO + endIndex := startIndex + common.SIZE_16 + common.ONE + ECPUBSIZE if len(pkt.Raw) >= endIndex { hashablePart = append(hashablePart, pkt.Raw[startIndex:endIndex]...) } @@ -1788,7 +1794,7 @@ func (l *Link) HandleLinkRequest(pkt *packet.Packet, ownerIdentity *identity.Ide return errors.New("link request data too short") } - peerPub := pkt.Data[0:KEYSIZE] + peerPub := pkt.Data[common.ZERO:KEYSIZE] peerSigPub := pkt.Data[KEYSIZE:ECPUBSIZE] l.peerPub = peerPub @@ -1802,7 +1808,7 @@ func (l *Link) HandleLinkRequest(pkt *packet.Packet, ownerIdentity *identity.Ide l.mode = (mtuBytes[0] & MODE_BYTEMASK) >> 5 debug.Log(debug.DEBUG_VERBOSE, "Link request includes MTU", "mtu", l.mtu, "mode", l.mode) } else { - l.mtu = 500 + l.mtu = common.DEFAULT_MTU / common.THREE l.mode = MODE_DEFAULT } @@ -1834,14 +1840,14 @@ func (l *Link) HandleLinkRequest(pkt *packet.Packet, ownerIdentity *identity.Ide } func (l *Link) updateMDU() { - headerMaxSize := 64 - ifacMinSize := 4 - tokenOverhead := 16 - aesBlockSize := 16 + headerMaxSize := common.SIZE_64 + ifacMinSize := common.FOUR + tokenOverhead := common.SIZE_16 + aesBlockSize := common.SIZE_16 - l.mdu = int(float64(l.mtu-headerMaxSize-ifacMinSize-tokenOverhead)/float64(aesBlockSize))*aesBlockSize - 1 - if l.mdu < 0 { - l.mdu = 100 + l.mdu = int(float64(l.mtu-headerMaxSize-ifacMinSize-tokenOverhead)/float64(aesBlockSize))*aesBlockSize - common.ONE + if l.mdu < common.ZERO { + l.mdu = common.DEFAULT_MTU / common.FIFTEEN } } @@ -1891,7 +1897,7 @@ func (l *Link) sendLinkProof(ownerIdentity *identity.Identity) error { return err } - debug.Log(debug.DEBUG_ERROR, "Link proof packet created", "dest_hash", fmt.Sprintf("%x", proofPkt.DestinationHash), "packet_type", fmt.Sprintf("0x%02x", proofPkt.PacketType)) + debug.Log(debug.DEBUG_ERROR, "Link proof packet created", "dest_hash", fmt.Sprintf("%x", proofPkt.DestinationHash), "packet_type", fmt.Sprintf(common.STR_FMT_HEX, proofPkt.PacketType)) // For responder links (not initiator), send proof directly through the receiving interface if !l.initiator && l.networkInterface != nil { @@ -1940,11 +1946,11 @@ func (l *Link) GenerateLinkProof(ownerIdentity *identity.Identity) (*packet.Pack proofPkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeProof, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextLRProof, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: proofData, CreateReceipt: false, @@ -1967,14 +1973,14 @@ func (l *Link) ValidateLinkProof(pkt *packet.Packet) error { return errors.New("link proof data too short") } - signature := pkt.Data[0 : identity.SIGLENGTH/8] - peerPub := pkt.Data[identity.SIGLENGTH/8 : identity.SIGLENGTH/8+KEYSIZE] + signature := pkt.Data[common.ZERO : identity.SIGLENGTH/common.EIGHT] + peerPub := pkt.Data[identity.SIGLENGTH/common.EIGHT : identity.SIGLENGTH/common.EIGHT+KEYSIZE] - signalling := []byte{0, 0, 0} + signalling := []byte{common.ZERO, common.ZERO, common.ZERO} if len(pkt.Data) >= identity.SIGLENGTH/8+KEYSIZE+LINK_MTU_SIZE { signalling = pkt.Data[identity.SIGLENGTH/8+KEYSIZE : identity.SIGLENGTH/8+KEYSIZE+LINK_MTU_SIZE] - mtu := (int(signalling[0]&0x1F) << 16) | (int(signalling[1]) << 8) | int(signalling[2]) - mode := (signalling[0] & MODE_BYTEMASK) >> 5 + mtu := (int(signalling[common.ZERO]&0x1F) << common.SIZE_16) | (int(signalling[common.ONE]) << common.EIGHT) | int(signalling[common.TWO]) + mode := (signalling[common.ZERO] & MODE_BYTEMASK) >> common.FIVE l.mtu = mtu l.mode = mode debug.Log(debug.DEBUG_VERBOSE, "Link proof includes MTU", "mtu", mtu, "mode", mode) @@ -2018,15 +2024,15 @@ func (l *Link) ValidateLinkProof(pkt *packet.Packet) error { } rttData := make([]byte, 8) - binary.BigEndian.PutUint64(rttData, uint64(l.rtt*1e9)) + binary.BigEndian.PutUint64(rttData, uint64(l.rtt*common.FLOAT_1E9)) rttPkt := &packet.Packet{ HeaderType: packet.HeaderType1, PacketType: packet.PacketTypeData, - TransportType: 0, + TransportType: common.ZERO, Context: packet.ContextLRRTT, ContextFlag: packet.FlagUnset, - Hops: 0, - DestinationType: 0x03, + Hops: common.ZERO, + DestinationType: DEST_TYPE_LINK, DestinationHash: l.linkID, Data: rttData, CreateReceipt: false, diff --git a/pkg/packet/packet.go b/pkg/packet/packet.go index 9ec0c78..75a0497 100644 --- a/pkg/packet/packet.go +++ b/pkg/packet/packet.go @@ -87,7 +87,7 @@ type Packet struct { Addresses []byte Link interface{} - + receipt *PacketReceipt } @@ -143,7 +143,7 @@ func (p *Packet) Pack() error { debug.Log(debug.DEBUG_TRACE, "Created packet header", "flags", fmt.Sprintf("%08b", flags), "hops", p.Hops) header = append(header, p.DestinationHash...) - + if p.HeaderType == HeaderType2 { if p.TransportID == nil { return errors.New("transport ID required for header type 2") @@ -189,8 +189,8 @@ func (p *Packet) Unpack() error { if len(p.Raw) < 2*dstLen+3 { return errors.New("packet too short for header type 2") } - p.DestinationHash = p.Raw[2 : dstLen+2] // Destination hash first - p.TransportID = p.Raw[dstLen+2 : 2*dstLen+2] // Transport ID second + p.DestinationHash = p.Raw[2 : dstLen+2] // Destination hash first + p.TransportID = p.Raw[dstLen+2 : 2*dstLen+2] // Transport ID second p.Context = p.Raw[2*dstLen+2] p.Data = p.Raw[2*dstLen+3:] } else { diff --git a/pkg/packet/receipt.go b/pkg/packet/receipt.go index 99fb550..b5bfb84 100644 --- a/pkg/packet/receipt.go +++ b/pkg/packet/receipt.go @@ -32,14 +32,14 @@ type PacketReceipt struct { timeout time.Duration concludedAt time.Time proofPacket *Packet - + deliveryCallback func(*PacketReceipt) timeoutCallback func(*PacketReceipt) - - link interface{} - destinationHash []byte - destinationIdent *identity.Identity - timeoutCheckDone chan bool + + link interface{} + destinationHash []byte + destinationIdent *identity.Identity + timeoutCheckDone chan bool } func NewPacketReceipt(pkt *Packet) *PacketReceipt { @@ -57,18 +57,18 @@ func NewPacketReceipt(pkt *Packet) *PacketReceipt { } go receipt.timeoutWatchdog() - + debug.Log(debug.DEBUG_PACKETS, "Created packet receipt", "hash", fmt.Sprintf("%x", receipt.truncatedHash)) return receipt } func calculateTimeout(pkt *Packet) time.Duration { baseTimeout := 15 * time.Second - + if pkt.Hops > 0 { baseTimeout += time.Duration(pkt.Hops) * (3 * time.Second) } - + return baseTimeout } @@ -107,11 +107,11 @@ func (pr *PacketReceipt) ValidateLinkProof(proof []byte, link interface{}, proof if len(proof) == EXPL_LENGTH { proofHash := proof[:identity.HASHLENGTH/8] signature := proof[identity.HASHLENGTH/8 : identity.HASHLENGTH/8+identity.SIGLENGTH/8] - + pr.mutex.RLock() hashMatch := string(proofHash) == string(pr.hash) pr.mutex.RUnlock() - + if !hashMatch { return false } @@ -136,7 +136,7 @@ func (pr *PacketReceipt) ValidateLinkProof(proof []byte, link interface{}, proof } else if len(proof) == IMPL_LENGTH { debug.Log(debug.DEBUG_TRACE, "Implicit link proof not yet implemented") } - + return false } @@ -144,14 +144,14 @@ func (pr *PacketReceipt) ValidateProof(proof []byte, proofPacket *Packet) bool { if len(proof) == EXPL_LENGTH { proofHash := proof[:identity.HASHLENGTH/8] signature := proof[identity.HASHLENGTH/8 : identity.HASHLENGTH/8+identity.SIGLENGTH/8] - + pr.mutex.RLock() hashMatch := string(proofHash) == string(pr.hash) ident := pr.destinationIdent pr.mutex.RUnlock() - + debug.Log(debug.DEBUG_PACKETS, "Explicit proof validation", "len", len(proof), "hashMatch", hashMatch, "hasIdent", ident != nil) - + if !hashMatch { debug.Log(debug.DEBUG_PACKETS, "Proof hash mismatch") return false @@ -182,7 +182,7 @@ func (pr *PacketReceipt) ValidateProof(proof []byte, proofPacket *Packet) bool { } } else if len(proof) == IMPL_LENGTH { signature := proof[:identity.SIGLENGTH/8] - + pr.mutex.RLock() ident := pr.destinationIdent pr.mutex.RUnlock() @@ -209,7 +209,7 @@ func (pr *PacketReceipt) ValidateProof(proof []byte, proofPacket *Packet) bool { return true } } - + return false } @@ -217,11 +217,11 @@ func (pr *PacketReceipt) validateLinkSignature(signature []byte, link interface{ type linkValidator interface { Validate(signature, message []byte) bool } - + if validator, ok := link.(linkValidator); ok { return validator.Validate(signature, pr.hash) } - + debug.Log(debug.DEBUG_TRACE, "Link does not implement Validate method") return false } @@ -229,46 +229,46 @@ func (pr *PacketReceipt) validateLinkSignature(signature []byte, link interface{ func (pr *PacketReceipt) GetRTT() time.Duration { pr.mutex.RLock() defer pr.mutex.RUnlock() - + if pr.concludedAt.IsZero() { return 0 } - + return pr.concludedAt.Sub(pr.sentAt) } func (pr *PacketReceipt) IsTimedOut() bool { pr.mutex.RLock() defer pr.mutex.RUnlock() - + return time.Since(pr.sentAt) > pr.timeout } func (pr *PacketReceipt) checkTimeout() { pr.mutex.Lock() - + if pr.status != RECEIPT_SENT { pr.mutex.Unlock() return } - + if !pr.IsTimedOut() { pr.mutex.Unlock() return } - + if pr.timeout < 0 { pr.status = RECEIPT_CULLED } else { pr.status = RECEIPT_FAILED } - + pr.concludedAt = time.Now() callback := pr.timeoutCallback pr.mutex.Unlock() debug.Log(debug.DEBUG_VERBOSE, "Packet receipt timed out", "hash", fmt.Sprintf("%x", pr.truncatedHash)) - + if callback != nil { go callback(pr) } @@ -282,11 +282,11 @@ func (pr *PacketReceipt) timeoutWatchdog() { select { case <-ticker.C: pr.checkTimeout() - + pr.mutex.RLock() status := pr.status pr.mutex.RUnlock() - + if status != RECEIPT_SENT { return } @@ -329,15 +329,14 @@ func (pr *PacketReceipt) SetLink(link interface{}) { func (pr *PacketReceipt) Cancel() { pr.mutex.Lock() defer pr.mutex.Unlock() - + if pr.status == RECEIPT_SENT { pr.status = RECEIPT_CULLED pr.concludedAt = time.Now() } - + select { case pr.timeoutCheckDone <- true: default: } } - diff --git a/pkg/packet/receipt_test.go b/pkg/packet/receipt_test.go index b83d61d..ad3b3dd 100644 --- a/pkg/packet/receipt_test.go +++ b/pkg/packet/receipt_test.go @@ -115,13 +115,13 @@ func TestPacketReceiptProofValidation(t *testing.T) { packetHash := pkt.GetHash() t.Logf("Packet hash: %x", packetHash) - + signature := testIdent.Sign(packetHash) t.Logf("PacketHash length: %d", len(packetHash)) t.Logf("Signature length: %d", len(signature)) t.Logf("EXPL_LENGTH constant: %d", EXPL_LENGTH) - + if testIdent.Verify(packetHash, signature) { t.Log("Direct verification succeeded") } else { @@ -207,4 +207,3 @@ func TestPacketReceiptCallbacks(t *testing.T) { t.Error("Delivery callback was not called") } } - diff --git a/pkg/transport/transport.go b/pkg/transport/transport.go index f6dba2a..2ccae0c 100644 --- a/pkg/transport/transport.go +++ b/pkg/transport/transport.go @@ -162,13 +162,13 @@ func NewTransport(cfg *common.ReticulumConfig) *Transport { interfaces: make(map[string]common.NetworkInterface), paths: make(map[string]*common.Path), seenAnnounces: make(map[string]bool), - announceRate: rate.NewLimiter(PROPAGATION_RATE, 1), + announceRate: rate.NewLimiter(PROPAGATION_RATE, common.ONE), mutex: sync.RWMutex{}, config: cfg, links: make(map[string]LinkInterface), destinations: make(map[string]interface{}), pathfinder: pathfinder.NewPathFinder(), - receipts: make([]*packet.PacketReceipt, 0), + receipts: make([]*packet.PacketReceipt, common.ZERO), receiptsMutex: sync.RWMutex{}, pathRequests: make(map[string]time.Time), pathStates: make(map[string]byte), @@ -189,7 +189,7 @@ func NewTransport(cfg *common.ReticulumConfig) *Transport { } func (t *Transport) startMaintenanceJobs() { - ticker := time.NewTicker(5 * time.Second) + ticker := time.NewTicker(common.FIVE * time.Second) defer ticker.Stop() for range ticker.C { @@ -211,7 +211,7 @@ func (t *Transport) cleanupExpiredPaths() { if now.Sub(path.LastUpdated) > pathExpiry { delete(t.paths, destHash) delete(t.pathStates, destHash) - debug.Log(debug.DEBUG_VERBOSE, "Expired path", "dest_hash", fmt.Sprintf("%x", destHash[:8])) + debug.Log(debug.DEBUG_VERBOSE, "Expired path", "dest_hash", fmt.Sprintf("%x", destHash[:common.EIGHT])) } } } @@ -224,7 +224,7 @@ func (t *Transport) cleanupExpiredDiscoveryRequests() { for destHash, req := range t.discoveryPathRequests { if now.After(req.Timeout) { delete(t.discoveryPathRequests, destHash) - debug.Log(debug.DEBUG_VERBOSE, "Expired discovery path request", "dest_hash", fmt.Sprintf("%x", destHash[:8])) + debug.Log(debug.DEBUG_VERBOSE, "Expired discovery path request", "dest_hash", fmt.Sprintf("%x", destHash[:common.EIGHT])) } } } @@ -238,7 +238,7 @@ func (t *Transport) cleanupExpiredAnnounces() { for destHash, entry := range t.announceTable { if entry != nil && time.Since(entry.CreatedAt) > announceExpiry { delete(t.announceTable, destHash) - debug.Log(debug.DEBUG_VERBOSE, "Expired announce entry", "dest_hash", fmt.Sprintf("%x", destHash[:8])) + debug.Log(debug.DEBUG_VERBOSE, "Expired announce entry", "dest_hash", fmt.Sprintf("%x", destHash[:common.EIGHT])) } } @@ -253,7 +253,7 @@ func (t *Transport) cleanupExpiredReceipts() { t.receiptsMutex.Lock() defer t.receiptsMutex.Unlock() - validReceipts := make([]*packet.PacketReceipt, 0) + validReceipts := make([]*packet.PacketReceipt, common.ZERO) for _, receipt := range t.receipts { if receipt != nil && !receipt.IsTimedOut() { status := receipt.GetStatus() @@ -452,7 +452,7 @@ func (l *Link) Send(data []byte) interface{} { Timestamp: time.Now(), } - if l.rtt == 0 { + if l.rtt == common.FLOAT_ZERO { l.rtt = l.InactiveFor() } @@ -594,9 +594,9 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf debug.Log(debug.DEBUG_ALL, "Transport handling announce", "bytes", len(data), "source", sourceIface.GetName()) // Parse announce fields according to RNS spec - destHash := data[1:33] - identity := data[33:49] - appData := data[49:] + destHash := data[common.ONE:common.SIZE_32+common.ONE] + identity := data[common.SIZE_32+common.ONE:common.SIZE_16+common.SIZE_32+common.ONE] + appData := data[common.SIZE_16+common.SIZE_32+common.ONE:] // Generate announce hash to check for duplicates announceHash := sha256.Sum256(data) @@ -605,15 +605,15 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf t.mutex.Lock() if _, seen := t.seenAnnounces[hashStr]; seen { t.mutex.Unlock() - debug.Log(debug.DEBUG_ALL, "Ignoring duplicate announce", "hash", fmt.Sprintf("%x", announceHash[:8])) + debug.Log(debug.DEBUG_ALL, "Ignoring duplicate announce", "hash", fmt.Sprintf("%x", announceHash[:common.EIGHT])) return nil } t.seenAnnounces[hashStr] = true t.mutex.Unlock() // Don't forward if max hops reached - if data[0] >= MAX_HOPS { - debug.Log(debug.DEBUG_ALL, "Announce exceeded max hops", "hops", data[0]) + if data[common.ZERO] >= MAX_HOPS { + debug.Log(debug.DEBUG_ALL, "Announce exceeded max hops", "hops", data[common.ZERO]) return nil } @@ -798,23 +798,23 @@ func SendAnnounce(packet []byte) error { } func (t *Transport) HandlePacket(data []byte, iface common.NetworkInterface) { - if len(data) < 2 { - debug.Log(debug.DEBUG_INFO, "Dropping packet: insufficient length", "bytes", len(data)) + if len(data) < common.TWO { + debug.Log(debug.DEBUG_INFO, "Dropping packet: insufficient length", common.STR_BYTES, len(data)) return } - headerByte := data[0] - packetType := headerByte & 0x03 - headerType := (headerByte & 0x40) >> 6 - contextFlag := (headerByte & 0x20) >> 5 - propType := (headerByte & 0x10) >> 4 - destType := (headerByte & 0x0C) >> 2 + headerByte := data[common.ZERO] + packetType := headerByte & common.HEX_0x03 + headerType := (headerByte & 0x40) >> common.SIX + contextFlag := (headerByte & 0x20) >> common.FIVE + propType := (headerByte & 0x10) >> common.FOUR + destType := (headerByte & 0x0C) >> common.TWO - debug.Log(debug.DEBUG_INFO, "TRANSPORT: Packet received", "type", fmt.Sprintf("0x%02x", packetType), "header", headerType, "context", contextFlag, "propType", propType, "destType", destType, "size", len(data)) - debug.Log(debug.DEBUG_TRACE, "Interface and raw header", "name", iface.GetName(), "header", fmt.Sprintf("0x%02x", headerByte)) + debug.Log(debug.DEBUG_INFO, "TRANSPORT: Packet received", "type", fmt.Sprintf(common.STR_FMT_HEX, packetType), "header", headerType, "context", contextFlag, "propType", propType, "destType", destType, "size", len(data)) + debug.Log(debug.DEBUG_TRACE, "Interface and raw header", "name", iface.GetName(), "header", fmt.Sprintf(common.STR_FMT_HEX, headerByte)) - if len(data) == 67 { - debug.Log(debug.DEBUG_ERROR, "67-byte packet detected", "header", fmt.Sprintf("0x%02x", headerByte), "packet_type_bits", fmt.Sprintf("0x%02x", packetType), "first_32_bytes", fmt.Sprintf("%x", data[:32])) + if len(data) == common.SIXTY_SEVEN { + debug.Log(debug.DEBUG_ERROR, "67-byte packet detected", "header", fmt.Sprintf(common.STR_FMT_HEX, headerByte), "packet_type_bits", fmt.Sprintf(common.STR_FMT_HEX, packetType), "first_32_bytes", fmt.Sprintf("%x", data[:common.SIZE_32])) } if tcpIface, ok := iface.(*interfaces.TCPClientInterface); ok { @@ -830,66 +830,66 @@ func (t *Transport) HandlePacket(data []byte, iface common.NetworkInterface) { } case PACKET_TYPE_LINK: debug.Log(debug.DEBUG_ERROR, "Processing link packet (type=0x02)", "packet_size", len(data)) - t.handleLinkPacket(data[1:], iface, PACKET_TYPE_LINK) + t.handleLinkPacket(data[common.ONE:], iface, PACKET_TYPE_LINK) case packet.PacketTypeProof: debug.Log(debug.DEBUG_VERBOSE, "Processing proof packet") - fullData := append([]byte{packet.PacketTypeProof}, data[1:]...) + fullData := append([]byte{packet.PacketTypeProof}, data[common.ONE:]...) pkt := &packet.Packet{Raw: fullData} if err := pkt.Unpack(); err != nil { debug.Log(debug.DEBUG_INFO, "Failed to unpack proof packet", "error", err) return } t.handleProofPacket(pkt, iface) - case 0x00: + case common.ZERO: // Data packets addressed to link destinations carry link traffic if destType == DEST_TYPE_LINK { debug.Log(debug.DEBUG_ERROR, "Processing link data packet (dest_type=3)", "packet_size", len(data)) - t.handleLinkPacket(data[1:], iface, 0x00) + t.handleLinkPacket(data[common.ONE:], iface, common.ZERO) } else { debug.Log(debug.DEBUG_ERROR, "Processing data packet (type 0x00)", "packet_size", len(data), "dest_type", destType, "header_type", headerType) - t.handleTransportPacket(data[1:], iface) + t.handleTransportPacket(data[common.ONE:], iface) } default: - debug.Log(debug.DEBUG_INFO, "Unknown packet type", "type", fmt.Sprintf("0x%02x", packetType), "source", iface.GetName()) + debug.Log(debug.DEBUG_INFO, "Unknown packet type", "type", fmt.Sprintf(common.STR_FMT_HEX, packetType), "source", iface.GetName()) } } func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterface) error { debug.Log(debug.DEBUG_INFO, "Processing announce packet", "length", len(data)) - if len(data) < 2 { + if len(data) < common.TWO { return fmt.Errorf("packet too small for header") } // Parse header bytes according to RNS spec - headerByte1 := data[0] - hopCount := data[1] + headerByte1 := data[common.ZERO] + hopCount := data[common.ONE] // Extract header fields - ifacFlag := (headerByte1 & 0x80) >> 7 // IFAC flag in highest bit - headerType := (headerByte1 & 0x40) >> 6 // Header type in next bit - contextFlag := (headerByte1 & 0x20) >> 5 // Context flag - propType := (headerByte1 & 0x10) >> 4 // Propagation type - destType := (headerByte1 & 0x0C) >> 2 // Destination type in next 2 bits - packetType := headerByte1 & 0x03 // Packet type in lowest 2 bits + ifacFlag := (headerByte1 & 0x80) >> common.SEVEN // IFAC flag in highest bit + headerType := (headerByte1 & 0x40) >> common.SIX // Header type in next bit + contextFlag := (headerByte1 & 0x20) >> common.FIVE // Context flag + propType := (headerByte1 & 0x10) >> common.FOUR // Propagation type + destType := (headerByte1 & 0x0C) >> common.TWO // Destination type in next 2 bits + packetType := headerByte1 & common.HEX_0x03 // Packet type in lowest 2 bits debug.Log(debug.DEBUG_TRACE, "Announce header", "ifac", ifacFlag, "headerType", headerType, "context", contextFlag, "propType", propType, "destType", destType, "packetType", packetType) // Skip IFAC code if present - startIdx := 2 - if ifacFlag == 1 { + startIdx := common.TWO + if ifacFlag == common.ONE { startIdx++ // For now assume 1 byte IFAC code } // Announce packets use HEADER_TYPE_1 (single address field) // Calculate address field size - addrSize := 16 // Always 16 bytes for HEADER_TYPE_1 - if headerType == 1 { + addrSize := common.SIZE_16 // Always 16 bytes for HEADER_TYPE_1 + if headerType == common.ONE { // HEADER_TYPE_2 has two address fields - addrSize = 32 + addrSize = common.SIZE_32 } // Validate minimum packet size - minSize := startIdx + addrSize + 1 // Header + address(es) + context + minSize := startIdx + addrSize + common.ONE // Header + address(es) + context if len(data) < minSize { return fmt.Errorf("packet too small: %d bytes", len(data)) }