From 2ffd12b3e1ad877b04c5881251e9a03727d015cc Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 7 Sep 2025 02:21:48 -0500 Subject: [PATCH] Add Send method to TCPClientInterface and better logging in UDPInterface --- pkg/interfaces/tcp.go | 23 ++++++++++++++++++++++- pkg/interfaces/udp.go | 13 +++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/interfaces/tcp.go b/pkg/interfaces/tcp.go index 70293d1..97c3464 100644 --- a/pkg/interfaces/tcp.go +++ b/pkg/interfaces/tcp.go @@ -238,6 +238,23 @@ func (tc *TCPClientInterface) handlePacket(data []byte) { } } +// Send implements the interface Send method for TCP interface +func (tc *TCPClientInterface) Send(data []byte, address string) error { + log.Printf("[DEBUG-7] TCP interface %s: Sending %d bytes", tc.Name, len(data)) + + if !tc.IsEnabled() || !tc.IsOnline() { + return fmt.Errorf("TCP interface %s is not online", tc.Name) + } + + // For TCP interface, we need to prepend a packet type byte for announce packets + // RNS TCP protocol expects: [packet_type][data] + frame := make([]byte, 0, len(data)+1) + frame = append(frame, 0x01) // Announce packet type + frame = append(frame, data...) + + return tc.ProcessOutgoing(frame) +} + func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error { if !tc.Online { return fmt.Errorf("interface offline") @@ -257,8 +274,12 @@ func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error { // Update TX stats before sending tc.UpdateStats(uint64(len(frame)), false) - + + log.Printf("[DEBUG-7] TCP interface %s: Writing %d bytes to network", tc.Name, len(frame)) _, err := tc.conn.Write(frame) + if err != nil { + log.Printf("[DEBUG-1] TCP interface %s: Write failed: %v", tc.Name, err) + } return err } diff --git a/pkg/interfaces/udp.go b/pkg/interfaces/udp.go index bee510d..578b6eb 100644 --- a/pkg/interfaces/udp.go +++ b/pkg/interfaces/udp.go @@ -2,6 +2,7 @@ package interfaces import ( "fmt" + "log" "net" "sync" @@ -75,6 +76,8 @@ func (ui *UDPInterface) Detach() { } func (ui *UDPInterface) Send(data []byte, addr string) error { + log.Printf("[DEBUG-7] UDP interface %s: Sending %d bytes", ui.Name, len(data)) + if !ui.IsEnabled() { return fmt.Errorf("interface not enabled") } @@ -83,7 +86,17 @@ func (ui *UDPInterface) Send(data []byte, addr string) error { return fmt.Errorf("no target address configured") } + // Update TX stats before sending + ui.mutex.Lock() + ui.TxBytes += uint64(len(data)) + ui.mutex.Unlock() + _, err := ui.conn.WriteTo(data, ui.targetAddr) + if err != nil { + log.Printf("[DEBUG-1] UDP interface %s: Write failed: %v", ui.Name, err) + } else { + log.Printf("[DEBUG-7] UDP interface %s: Sent %d bytes successfully", ui.Name, len(data)) + } return err }