diff --git a/pkg/interfaces/auto.go b/pkg/interfaces/auto.go index 3253050..bdb2385 100644 --- a/pkg/interfaces/auto.go +++ b/pkg/interfaces/auto.go @@ -44,21 +44,19 @@ type Peer struct { } func NewAutoInterface(name string, config *common.InterfaceConfig) (*AutoInterface, error) { - base := &BaseInterface{ - Name: name, - Mode: common.IF_MODE_FULL, - Type: common.IF_TYPE_AUTO, - Online: false, - Enabled: config.Enabled, - Detached: false, - IN: false, - OUT: false, - MTU: common.DEFAULT_MTU, - Bitrate: BITRATE_MINIMUM, - } - ai := &AutoInterface{ - BaseInterface: *base, + BaseInterface: BaseInterface{ + Name: name, + Mode: common.IF_MODE_FULL, + Type: common.IF_TYPE_AUTO, + Online: false, + Enabled: config.Enabled, + Detached: false, + IN: false, + OUT: false, + MTU: common.DEFAULT_MTU, + Bitrate: BITRATE_MINIMUM, + }, discoveryPort: DEFAULT_DISCOVERY_PORT, dataPort: DEFAULT_DATA_PORT, discoveryScope: SCOPE_LINK, @@ -165,13 +163,13 @@ func (ai *AutoInterface) startDataListener(iface *net.Interface) error { func (ai *AutoInterface) handleDiscovery(conn *net.UDPConn, ifaceName string) { buf := make([]byte, 1024) for { - n, remoteAddr, err := conn.ReadFromUDP(buf) + _, remoteAddr, err := conn.ReadFromUDP(buf) if err != nil { log.Printf("Discovery read error: %v", err) continue } - ai.handlePeerAnnounce(remoteAddr, buf[:n], ifaceName) + ai.handlePeerAnnounce(remoteAddr, ifaceName) } } @@ -192,7 +190,7 @@ func (ai *AutoInterface) handleData(conn *net.UDPConn) { } } -func (ai *AutoInterface) handlePeerAnnounce(addr *net.UDPAddr, data []byte, ifaceName string) { +func (ai *AutoInterface) handlePeerAnnounce(addr *net.UDPAddr, ifaceName string) { ai.mutex.Lock() defer ai.mutex.Unlock() diff --git a/pkg/interfaces/auto_test.go b/pkg/interfaces/auto_test.go index 0d8cfcd..5a2ecf8 100644 --- a/pkg/interfaces/auto_test.go +++ b/pkg/interfaces/auto_test.go @@ -97,7 +97,7 @@ func (m *mockAutoInterface) Stop() error { } // mockHandlePeerAnnounce is a test-only method that doesn't handle its own locking -func (m *mockAutoInterface) mockHandlePeerAnnounce(addr *net.UDPAddr, data []byte, ifaceName string) { +func (m *mockAutoInterface) mockHandlePeerAnnounce(addr *net.UDPAddr, ifaceName string) { peerAddr := addr.IP.String() + "%" + addr.Zone for _, localAddr := range m.linkLocalAddrs { @@ -174,7 +174,7 @@ func TestAutoInterfacePeerManagement(t *testing.T) { t.Run("AddPeer1", func(t *testing.T) { ai.mutex.Lock() - ai.mockHandlePeerAnnounce(peer1Addr, []byte("announce1"), "eth0") + ai.mockHandlePeerAnnounce(peer1Addr, "eth0") ai.mutex.Unlock() // Give a small amount of time for the peer to be processed @@ -202,7 +202,7 @@ func TestAutoInterfacePeerManagement(t *testing.T) { t.Run("AddPeer2", func(t *testing.T) { ai.mutex.Lock() - ai.mockHandlePeerAnnounce(peer2Addr, []byte("announce2"), "eth0") + ai.mockHandlePeerAnnounce(peer2Addr, "eth0") ai.mutex.Unlock() // Give a small amount of time for the peer to be processed @@ -223,7 +223,7 @@ func TestAutoInterfacePeerManagement(t *testing.T) { t.Run("IgnoreLocalAnnounce", func(t *testing.T) { ai.mutex.Lock() - ai.mockHandlePeerAnnounce(localAddr, []byte("local_announce"), "eth0") + ai.mockHandlePeerAnnounce(localAddr, "eth0") ai.mutex.Unlock() // Give a small amount of time for the peer to be processed @@ -252,7 +252,7 @@ func TestAutoInterfacePeerManagement(t *testing.T) { } ai.mutex.Lock() - ai.mockHandlePeerAnnounce(peer1Addr, []byte("announce1_again"), "eth0") + ai.mockHandlePeerAnnounce(peer1Addr, "eth0") ai.mutex.Unlock() // Give a small amount of time for the peer to be processed diff --git a/pkg/interfaces/tcp.go b/pkg/interfaces/tcp.go index 39eee19..97a1ac8 100644 --- a/pkg/interfaces/tcp.go +++ b/pkg/interfaces/tcp.go @@ -68,7 +68,7 @@ func NewTCPClientInterface(name string, targetHost string, targetPort int, kissF } if enabled { - addr := fmt.Sprintf("%s:%d", targetHost, targetPort) + addr := net.JoinHostPort(targetHost, fmt.Sprintf("%d", targetPort)) conn, err := net.Dial("tcp", addr) if err != nil { return nil, err @@ -95,7 +95,7 @@ func (tc *TCPClientInterface) Start() error { return nil } - addr := fmt.Sprintf("%s:%d", tc.targetAddr, tc.targetPort) + addr := net.JoinHostPort(tc.targetAddr, fmt.Sprintf("%d", tc.targetPort)) conn, err := net.Dial("tcp", addr) if err != nil { return err @@ -346,7 +346,7 @@ func (tc *TCPClientInterface) reconnect() { for retries < tc.maxReconnectTries { tc.teardown() - addr := fmt.Sprintf("%s:%d", tc.targetAddr, tc.targetPort) + addr := net.JoinHostPort(tc.targetAddr, fmt.Sprintf("%d", tc.targetPort)) conn, err := net.Dial("tcp", addr) if err == nil { diff --git a/pkg/interfaces/udp.go b/pkg/interfaces/udp.go index e923764..5879d00 100644 --- a/pkg/interfaces/udp.go +++ b/pkg/interfaces/udp.go @@ -2,7 +2,6 @@ package interfaces import ( "fmt" - "log" "net" "sync" @@ -173,32 +172,24 @@ func (ui *UDPInterface) Start() error { return nil } +/* func (ui *UDPInterface) readLoop() { buffer := make([]byte, ui.MTU) for { - if ui.IsDetached() { - return - } - - n, addr, err := ui.conn.ReadFromUDP(buffer) + n, _, err := ui.conn.ReadFromUDP(buffer) if err != nil { - if !ui.IsDetached() { - log.Printf("UDP read error: %v", err) + if ui.Online { + log.Printf("Error reading from UDP interface %s: %v", ui.Name, err) + ui.Stop() // Consider if stopping is the right action or just log and continue } return } - - ui.mutex.Lock() - ui.RxBytes += uint64(n) - ui.mutex.Unlock() - - log.Printf("Received %d bytes from %s", n, addr.String()) - - if callback := ui.GetPacketCallback(); callback != nil { - callback(buffer[:n], ui) + if ui.packetCallback != nil { + ui.packetCallback(buffer[:n], ui) } } } +*/ func (ui *UDPInterface) IsEnabled() bool { ui.mutex.RLock()