From 3ed2c6774270ff022a623fe99399f241094c85ef Mon Sep 17 00:00:00 2001 From: Sudo-Ivan Date: Tue, 31 Dec 2024 11:22:37 -0600 Subject: [PATCH] interface: add interface mods, types and more --- pkg/interfaces/interface.go | 169 ++++++++++++++++++++++-------------- pkg/interfaces/tcp.go | 67 ++++++++------ pkg/interfaces/udp.go | 47 +++++----- 3 files changed, 167 insertions(+), 116 deletions(-) diff --git a/pkg/interfaces/interface.go b/pkg/interfaces/interface.go index 715bab0..f58193d 100644 --- a/pkg/interfaces/interface.go +++ b/pkg/interfaces/interface.go @@ -13,13 +13,23 @@ import ( const ( BITRATE_MINIMUM = 5 // Minimum required bitrate in bits/sec MODE_FULL = 0x01 + + // Interface modes + MODE_GATEWAY = 0x02 + MODE_ACCESS_POINT = 0x03 + MODE_ROAMING = 0x04 + MODE_BOUNDARY = 0x05 + + // Interface types + TYPE_UDP = 0x01 + TYPE_TCP = 0x02 ) type Interface interface { GetName() string GetType() common.InterfaceType GetMode() common.InterfaceMode - IsOnline() bool + IsOnline() bool IsDetached() bool IsEnabled() bool Detach() @@ -28,43 +38,65 @@ type Interface interface { Send(data []byte, addr string) error SetPacketCallback(common.PacketCallback) GetPacketCallback() common.PacketCallback + ProcessIncoming([]byte) + ProcessOutgoing([]byte) error + SendPathRequest([]byte) error + SendLinkPacket([]byte, []byte, time.Time) error + Start() error + Stop() error + GetMTU() int + GetConn() net.Conn } type BaseInterface struct { - common.BaseInterface - name string - mode common.InterfaceMode - ifType common.InterfaceType - online bool - enabled bool - detached bool - mtu int + Name string + Mode common.InterfaceMode + Type common.InterfaceType + Online bool + Enabled bool + Detached bool + IN bool + OUT bool + MTU int + Bitrate int64 + TxBytes uint64 + RxBytes uint64 + mutex sync.RWMutex packetCallback common.PacketCallback } func NewBaseInterface(name string, ifType common.InterfaceType, enabled bool) BaseInterface { return BaseInterface{ - name: name, - mode: common.IF_MODE_FULL, - ifType: ifType, - online: false, - enabled: enabled, - detached: false, - mtu: common.DEFAULT_MTU, + Name: name, + Mode: common.IF_MODE_FULL, + Type: ifType, + Online: false, + Enabled: enabled, + Detached: false, + IN: false, + OUT: false, + MTU: common.DEFAULT_MTU, + Bitrate: BITRATE_MINIMUM, } } func (i *BaseInterface) SetPacketCallback(callback common.PacketCallback) { - i.Mutex.Lock() - defer i.Mutex.Unlock() - i.PacketCallback = callback + i.mutex.Lock() + defer i.mutex.Unlock() + i.packetCallback = callback +} + +func (i *BaseInterface) GetPacketCallback() common.PacketCallback { + i.mutex.RLock() + defer i.mutex.RUnlock() + return i.packetCallback } func (i *BaseInterface) ProcessIncoming(data []byte) { - i.Mutex.RLock() - callback := i.PacketCallback - i.Mutex.RUnlock() + i.mutex.RLock() + callback := i.packetCallback + i.mutex.RUnlock() if callback != nil { callback(data, i) @@ -74,24 +106,20 @@ func (i *BaseInterface) ProcessIncoming(data []byte) { } func (i *BaseInterface) ProcessOutgoing(data []byte) error { + if !i.Online || i.Detached { + return fmt.Errorf("interface offline or detached") + } i.TxBytes += uint64(len(data)) return nil } -func (i *BaseInterface) Detach() { - i.Mutex.Lock() - defer i.Mutex.Unlock() - i.Detached = true - i.Online = false -} - func (i *BaseInterface) SendPathRequest(packet []byte) error { if !i.Online || i.Detached { return fmt.Errorf("interface offline or detached") } - frame := make([]byte, 0, len(packet)+2) - frame = append(frame, 0x01) + frame := make([]byte, 0, len(packet)+1) + frame = append(frame, 0x01) // Path request type frame = append(frame, packet...) return i.ProcessOutgoing(frame) @@ -103,32 +131,46 @@ func (i *BaseInterface) SendLinkPacket(dest []byte, data []byte, timestamp time. } frame := make([]byte, 0, len(dest)+len(data)+9) - frame = append(frame, 0x02) + frame = append(frame, 0x02) // Link packet type frame = append(frame, dest...) ts := make([]byte, 8) binary.BigEndian.PutUint64(ts, uint64(timestamp.Unix())) frame = append(frame, ts...) - frame = append(frame, data...) return i.ProcessOutgoing(frame) } -func (i *BaseInterface) Start() error { - return nil +func (i *BaseInterface) Detach() { + i.mutex.Lock() + defer i.mutex.Unlock() + i.Detached = true + i.Online = false } -func (i *BaseInterface) Stop() error { - return nil +func (i *BaseInterface) IsEnabled() bool { + i.mutex.RLock() + defer i.mutex.RUnlock() + return i.Enabled && i.Online && !i.Detached } -func (i *BaseInterface) Send(data []byte, address string) error { - return i.ProcessOutgoing(data) +func (i *BaseInterface) Enable() { + i.mutex.Lock() + defer i.mutex.Unlock() + i.Enabled = true + i.Online = true } -func (i *BaseInterface) Receive() ([]byte, string, error) { - return nil, "", nil +func (i *BaseInterface) Disable() { + i.mutex.Lock() + defer i.mutex.Unlock() + i.Enabled = false + i.Online = false +} + +func (i *BaseInterface) GetName() string { + return i.Name } func (i *BaseInterface) GetType() common.InterfaceType { @@ -143,30 +185,31 @@ func (i *BaseInterface) GetMTU() int { return i.MTU } -func (i *BaseInterface) GetName() string { - return i.Name +func (i *BaseInterface) IsOnline() bool { + i.mutex.RLock() + defer i.mutex.RUnlock() + return i.Online +} + +func (i *BaseInterface) IsDetached() bool { + i.mutex.RLock() + defer i.mutex.RUnlock() + return i.Detached +} + +// Default implementations that should be overridden by specific interfaces +func (i *BaseInterface) Start() error { + return nil +} + +func (i *BaseInterface) Stop() error { + return nil +} + +func (i *BaseInterface) Send(data []byte, address string) error { + return i.ProcessOutgoing(data) } func (i *BaseInterface) GetConn() net.Conn { return nil } - -func (i *BaseInterface) IsEnabled() bool { - i.mutex.RLock() - defer i.mutex.RUnlock() - return i.enabled && i.online && !i.detached -} - -func (i *BaseInterface) Enable() { - i.mutex.Lock() - defer i.mutex.Unlock() - i.enabled = true - i.online = true -} - -func (i *BaseInterface) Disable() { - i.mutex.Lock() - defer i.mutex.Unlock() - i.enabled = false - i.online = false -} diff --git a/pkg/interfaces/tcp.go b/pkg/interfaces/tcp.go index 267abec..be7e392 100644 --- a/pkg/interfaces/tcp.go +++ b/pkg/interfaces/tcp.go @@ -41,9 +41,7 @@ type TCPClientInterface struct { maxReconnectTries int packetBuffer []byte packetType byte - packetCallback common.PacketCallback mutex sync.RWMutex - detached bool enabled bool } @@ -65,7 +63,7 @@ func NewTCPClient(name string, targetHost string, targetPort int, kissFraming bo return nil, err } tc.conn = conn - tc.online = true + tc.Online = true } return tc, nil @@ -75,12 +73,12 @@ func (tc *TCPClientInterface) Start() error { tc.mutex.Lock() defer tc.mutex.Unlock() - if !tc.enabled { + if !tc.Enabled { return fmt.Errorf("interface not enabled") } if tc.conn != nil { - tc.online = true + tc.Online = true return nil } @@ -90,7 +88,7 @@ func (tc *TCPClientInterface) Start() error { return err } tc.conn = conn - tc.online = true + tc.Online = true return nil } @@ -254,7 +252,7 @@ func (tc *TCPClientInterface) SetPacketCallback(cb common.PacketCallback) { func (tc *TCPClientInterface) IsEnabled() bool { tc.mutex.RLock() defer tc.mutex.RUnlock() - return tc.enabled && tc.online && !tc.detached + return tc.enabled && tc.Online && !tc.Detached } func (tc *TCPClientInterface) GetName() string { @@ -270,13 +268,13 @@ func (tc *TCPClientInterface) GetPacketCallback() common.PacketCallback { func (tc *TCPClientInterface) IsDetached() bool { tc.mutex.RLock() defer tc.mutex.RUnlock() - return tc.detached + return tc.Detached } func (tc *TCPClientInterface) IsOnline() bool { tc.mutex.RLock() defer tc.mutex.RUnlock() - return tc.online + return tc.Online } func (tc *TCPClientInterface) reconnect() { @@ -297,7 +295,7 @@ func (tc *TCPClientInterface) reconnect() { if err == nil { tc.mutex.Lock() tc.conn = conn - tc.online = true + tc.Online = true tc.neverConnected = false tc.reconnecting = false tc.mutex.Unlock() @@ -325,13 +323,13 @@ func (tc *TCPClientInterface) reconnect() { func (tc *TCPClientInterface) Enable() { tc.mutex.Lock() defer tc.mutex.Unlock() - tc.online = true + tc.Online = true } func (tc *TCPClientInterface) Disable() { tc.mutex.Lock() defer tc.mutex.Unlock() - tc.online = false + tc.Online = false } type TCPServerInterface struct { @@ -344,18 +342,17 @@ type TCPServerInterface struct { kissFraming bool i2pTunneled bool packetCallback common.PacketCallback - detached bool } func NewTCPServer(name string, bindAddr string, bindPort int, kissFraming bool, i2pTunneled bool, preferIPv6 bool) (*TCPServerInterface, error) { ts := &TCPServerInterface{ BaseInterface: BaseInterface{ - name: name, - mode: common.IF_MODE_FULL, - ifType: common.IF_TYPE_TCP, - online: false, - mtu: common.DEFAULT_MTU, - detached: false, + Name: name, + Mode: common.IF_MODE_FULL, + Type: common.IF_TYPE_TCP, + Online: false, + MTU: common.DEFAULT_MTU, + Detached: false, }, connections: make(map[string]net.Conn), bindAddr: bindAddr, @@ -377,7 +374,7 @@ func (ts *TCPServerInterface) String() string { addr = "0.0.0.0" } } - return fmt.Sprintf("TCPServerInterface[%s/%s:%d]", ts.name, addr, ts.bindPort) + return fmt.Sprintf("TCPServerInterface[%s/%s:%d]", ts.Name, addr, ts.bindPort) } func (ts *TCPServerInterface) SetPacketCallback(callback common.PacketCallback) { @@ -393,33 +390,51 @@ func (ts *TCPServerInterface) GetPacketCallback() common.PacketCallback { } func (ts *TCPServerInterface) IsEnabled() bool { - return ts.online + ts.mutex.RLock() + defer ts.mutex.RUnlock() + return ts.BaseInterface.Enabled && ts.BaseInterface.Online && !ts.BaseInterface.Detached } func (ts *TCPServerInterface) GetName() string { - return ts.name + return ts.Name } func (ts *TCPServerInterface) IsDetached() bool { ts.mutex.RLock() defer ts.mutex.RUnlock() - return ts.detached + return ts.BaseInterface.Detached } func (ts *TCPServerInterface) IsOnline() bool { ts.mutex.RLock() defer ts.mutex.RUnlock() - return ts.online + return ts.Online } func (ts *TCPServerInterface) Enable() { ts.mutex.Lock() defer ts.mutex.Unlock() - ts.online = true + ts.Online = true } func (ts *TCPServerInterface) Disable() { ts.mutex.Lock() defer ts.mutex.Unlock() - ts.online = false + ts.Online = false +} + +func (ts *TCPServerInterface) Start() error { + ts.mutex.Lock() + defer ts.mutex.Unlock() + + ts.Online = true + return nil +} + +func (ts *TCPServerInterface) Stop() error { + ts.mutex.Lock() + defer ts.mutex.Unlock() + + ts.Online = false + return nil } diff --git a/pkg/interfaces/udp.go b/pkg/interfaces/udp.go index e6cb3f8..e923764 100644 --- a/pkg/interfaces/udp.go +++ b/pkg/interfaces/udp.go @@ -16,11 +16,6 @@ type UDPInterface struct { targetAddr *net.UDPAddr mutex sync.RWMutex readBuffer []byte - txBytes uint64 - rxBytes uint64 - mtu int - bitrate int - enabled bool } func NewUDPInterface(name string, addr string, target string, enabled bool) (*UDPInterface, error) { @@ -39,44 +34,42 @@ func NewUDPInterface(name string, addr string, target string, enabled bool) (*UD ui := &UDPInterface{ BaseInterface: NewBaseInterface(name, common.IF_TYPE_UDP, enabled), - addr: udpAddr, - targetAddr: targetAddr, - readBuffer: make([]byte, common.DEFAULT_MTU), - mtu: common.DEFAULT_MTU, - enabled: enabled, + addr: udpAddr, + targetAddr: targetAddr, + readBuffer: make([]byte, common.DEFAULT_MTU), } return ui, nil } func (ui *UDPInterface) GetName() string { - return ui.name + return ui.Name } func (ui *UDPInterface) GetType() common.InterfaceType { - return ui.ifType + return ui.Type } func (ui *UDPInterface) GetMode() common.InterfaceMode { - return ui.mode + return ui.Mode } func (ui *UDPInterface) IsOnline() bool { ui.mutex.RLock() defer ui.mutex.RUnlock() - return ui.online + return ui.Online } func (ui *UDPInterface) IsDetached() bool { ui.mutex.RLock() defer ui.mutex.RUnlock() - return ui.detached + return ui.Detached } func (ui *UDPInterface) Detach() { ui.mutex.Lock() defer ui.mutex.Unlock() - ui.detached = true + ui.Detached = true if ui.conn != nil { ui.conn.Close() } @@ -128,7 +121,7 @@ func (ui *UDPInterface) ProcessOutgoing(data []byte) error { } ui.mutex.Lock() - ui.txBytes += uint64(len(data)) + ui.TxBytes += uint64(len(data)) ui.mutex.Unlock() return nil @@ -141,33 +134,33 @@ func (ui *UDPInterface) GetConn() net.Conn { func (ui *UDPInterface) GetTxBytes() uint64 { ui.mutex.RLock() defer ui.mutex.RUnlock() - return ui.txBytes + return ui.TxBytes } func (ui *UDPInterface) GetRxBytes() uint64 { ui.mutex.RLock() defer ui.mutex.RUnlock() - return ui.rxBytes + return ui.RxBytes } func (ui *UDPInterface) GetMTU() int { - return ui.mtu + return ui.MTU } func (ui *UDPInterface) GetBitrate() int { - return ui.bitrate + return int(ui.Bitrate) } func (ui *UDPInterface) Enable() { ui.mutex.Lock() defer ui.mutex.Unlock() - ui.online = true + ui.Online = true } func (ui *UDPInterface) Disable() { ui.mutex.Lock() defer ui.mutex.Unlock() - ui.online = false + ui.Online = false } func (ui *UDPInterface) Start() error { @@ -176,12 +169,12 @@ func (ui *UDPInterface) Start() error { return err } ui.conn = conn - ui.online = true + ui.Online = true return nil } func (ui *UDPInterface) readLoop() { - buffer := make([]byte, ui.mtu) + buffer := make([]byte, ui.MTU) for { if ui.IsDetached() { return @@ -196,7 +189,7 @@ func (ui *UDPInterface) readLoop() { } ui.mutex.Lock() - ui.rxBytes += uint64(n) + ui.RxBytes += uint64(n) ui.mutex.Unlock() log.Printf("Received %d bytes from %s", n, addr.String()) @@ -210,5 +203,5 @@ func (ui *UDPInterface) readLoop() { func (ui *UDPInterface) IsEnabled() bool { ui.mutex.RLock() defer ui.mutex.RUnlock() - return ui.enabled && ui.online && !ui.detached + return ui.Enabled && ui.Online && !ui.Detached }