update common packages

This commit is contained in:
Sudo-Ivan
2024-12-31 11:43:22 -06:00
parent 599dd91979
commit 613ceddb0b
4 changed files with 249 additions and 60 deletions

View File

@@ -1,5 +1,15 @@
package common
import (
"fmt"
)
const (
DEFAULT_SHARED_INSTANCE_PORT = 37428
DEFAULT_INSTANCE_CONTROL_PORT = 37429
DEFAULT_LOG_LEVEL = 20
)
// ConfigProvider interface for accessing configuration
type ConfigProvider interface {
GetConfigPath() string
@@ -9,28 +19,55 @@ type ConfigProvider interface {
// InterfaceConfig represents interface configuration
type InterfaceConfig struct {
Name string `toml:"name"`
Type string `toml:"type"`
Enabled bool `toml:"enabled"`
Address string `toml:"address"`
Port int `toml:"port"`
TargetHost string `toml:"target_host"`
TargetPort int `toml:"target_port"`
TargetAddress string `toml:"target_address"`
Interface string `toml:"interface"`
KISSFraming bool `toml:"kiss_framing"`
I2PTunneled bool `toml:"i2p_tunneled"`
PreferIPv6 bool `toml:"prefer_ipv6"`
Name string `toml:"name"`
Type string `toml:"type"`
Enabled bool `toml:"enabled"`
Address string `toml:"address"`
Port int `toml:"port"`
TargetHost string `toml:"target_host"`
TargetPort int `toml:"target_port"`
TargetAddress string `toml:"target_address"`
Interface string `toml:"interface"`
KISSFraming bool `toml:"kiss_framing"`
I2PTunneled bool `toml:"i2p_tunneled"`
PreferIPv6 bool `toml:"prefer_ipv6"`
MaxReconnTries int `toml:"max_reconnect_tries"`
Bitrate int64 `toml:"bitrate"`
MTU int `toml:"mtu"`
}
// ReticulumConfig represents the main configuration structure
type ReticulumConfig struct {
ConfigPath string `toml:"-"`
EnableTransport bool `toml:"enable_transport"`
ConfigPath string `toml:"-"`
EnableTransport bool `toml:"enable_transport"`
ShareInstance bool `toml:"share_instance"`
SharedInstancePort int `toml:"shared_instance_port"`
InstanceControlPort int `toml:"instance_control_port"`
PanicOnInterfaceErr bool `toml:"panic_on_interface_error"`
LogLevel int `toml:"loglevel"`
LogLevel int `toml:"loglevel"`
Interfaces map[string]*InterfaceConfig `toml:"interfaces"`
}
// NewReticulumConfig creates a new ReticulumConfig with default values
func NewReticulumConfig() *ReticulumConfig {
return &ReticulumConfig{
EnableTransport: true,
ShareInstance: false,
SharedInstancePort: DEFAULT_SHARED_INSTANCE_PORT,
InstanceControlPort: DEFAULT_INSTANCE_CONTROL_PORT,
PanicOnInterfaceErr: false,
LogLevel: DEFAULT_LOG_LEVEL,
Interfaces: make(map[string]*InterfaceConfig),
}
}
// Validate checks if the configuration is valid
func (c *ReticulumConfig) Validate() error {
if c.SharedInstancePort < 1 || c.SharedInstancePort > 65535 {
return fmt.Errorf("invalid shared instance port: %d", c.SharedInstancePort)
}
if c.InstanceControlPort < 1 || c.InstanceControlPort > 65535 {
return fmt.Errorf("invalid instance control port: %d", c.InstanceControlPort)
}
return nil
}

View File

@@ -1,28 +1,60 @@
package common
const (
// Interface Types
IF_TYPE_UDP InterfaceType = iota
IF_TYPE_TCP
IF_TYPE_UNIX
// Interface Types
IF_TYPE_NONE InterfaceType = iota
IF_TYPE_UDP
IF_TYPE_TCP
IF_TYPE_UNIX
IF_TYPE_I2P
IF_TYPE_BLUETOOTH
IF_TYPE_SERIAL
// Interface Modes
IF_MODE_FULL InterfaceMode = iota
IF_MODE_POINT
IF_MODE_GATEWAY
// Interface Modes
IF_MODE_FULL InterfaceMode = iota
IF_MODE_POINT
IF_MODE_GATEWAY
IF_MODE_ACCESS_POINT
IF_MODE_ROAMING
IF_MODE_BOUNDARY
// Transport Modes
TRANSPORT_MODE_DIRECT TransportMode = iota
TRANSPORT_MODE_RELAY
TRANSPORT_MODE_GATEWAY
// Transport Modes
TRANSPORT_MODE_DIRECT TransportMode = iota
TRANSPORT_MODE_RELAY
TRANSPORT_MODE_GATEWAY
// Path Status
PATH_STATUS_UNKNOWN PathStatus = iota
PATH_STATUS_DIRECT
PATH_STATUS_RELAY
PATH_STATUS_FAILED
// Path Status
PATH_STATUS_UNKNOWN PathStatus = iota
PATH_STATUS_DIRECT
PATH_STATUS_RELAY
PATH_STATUS_FAILED
// Common Constants
DEFAULT_MTU = 1500
MAX_PACKET_SIZE = 65535
)
// Resource Status
RESOURCE_STATUS_PENDING = 0x00
RESOURCE_STATUS_ACTIVE = 0x01
RESOURCE_STATUS_COMPLETE = 0x02
RESOURCE_STATUS_FAILED = 0x03
RESOURCE_STATUS_CANCELLED = 0x04
// Link Status
LINK_STATUS_PENDING = 0x00
LINK_STATUS_ACTIVE = 0x01
LINK_STATUS_CLOSED = 0x02
LINK_STATUS_FAILED = 0x03
// Direction Constants
IN = 0x01
OUT = 0x02
// Common Constants
DEFAULT_MTU = 1500
MAX_PACKET_SIZE = 65535
BITRATE_MINIMUM = 5
// Timeouts and Intervals
ESTABLISH_TIMEOUT = 6
KEEPALIVE_INTERVAL = 360
STALE_TIME = 720
PATH_REQUEST_TTL = 300
ANNOUNCE_TIMEOUT = 15
)

View File

@@ -6,38 +6,44 @@ import (
"time"
)
// NetworkInterface combines both low-level and high-level interface requirements
// NetworkInterface defines the interface for all network communication methods
type NetworkInterface interface {
// Low-level network operations
// Core interface operations
Start() error
Stop() error
Enable()
Disable()
Detach()
// Network operations
Send(data []byte, address string) error
Receive() ([]byte, string, error)
GetConn() net.Conn
GetMTU() int
GetName() string
// Interface properties
GetType() InterfaceType
GetMode() InterfaceMode
GetMTU() int
// High-level packet operations
IsEnabled() bool
IsOnline() bool
IsDetached() bool
// Packet handling
ProcessIncoming([]byte)
ProcessOutgoing([]byte) error
SendPathRequest([]byte) error
SendLinkPacket([]byte, []byte, time.Time) error
Detach()
SetPacketCallback(PacketCallback)
// Additional required fields
GetName() string
GetConn() net.Conn
IsEnabled() bool
GetPacketCallback() PacketCallback
}
// BaseInterface provides common implementation
// BaseInterface provides common implementation for network interfaces
type BaseInterface struct {
Name string
Mode InterfaceMode
Type InterfaceType
Name string
Mode InterfaceMode
Type InterfaceType
Online bool
Enabled bool
Detached bool
IN bool
@@ -53,3 +59,82 @@ type BaseInterface struct {
Owner interface{}
PacketCallback PacketCallback
}
// NewBaseInterface creates a new BaseInterface instance
func NewBaseInterface(name string, ifaceType InterfaceType, enabled bool) BaseInterface {
return BaseInterface{
Name: name,
Type: ifaceType,
Mode: IF_MODE_FULL,
Enabled: enabled,
MTU: DEFAULT_MTU,
}
}
// Default implementations for BaseInterface
func (i *BaseInterface) GetType() InterfaceType {
return i.Type
}
func (i *BaseInterface) GetMode() InterfaceMode {
return i.Mode
}
func (i *BaseInterface) GetMTU() int {
return i.MTU
}
func (i *BaseInterface) GetName() string {
return i.Name
}
func (i *BaseInterface) IsEnabled() bool {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.Enabled && i.Online && !i.Detached
}
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
}
func (i *BaseInterface) SetPacketCallback(callback PacketCallback) {
i.Mutex.Lock()
defer i.Mutex.Unlock()
i.PacketCallback = callback
}
func (i *BaseInterface) GetPacketCallback() PacketCallback {
i.Mutex.RLock()
defer i.Mutex.RUnlock()
return i.PacketCallback
}
func (i *BaseInterface) Detach() {
i.Mutex.Lock()
defer i.Mutex.Unlock()
i.Detached = true
i.Online = false
}
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
}

View File

@@ -8,7 +8,7 @@ import (
type TransportMode byte
type PathStatus byte
// Common structs
// Path represents routing information for a destination
type Path struct {
Interface NetworkInterface
LastSeen time.Time
@@ -20,20 +20,55 @@ type Path struct {
// Common callbacks
type ProofRequestedCallback func([]byte, []byte)
type LinkEstablishedCallback func(interface{})
type PacketCallback func([]byte, interface{})
// Request handler
// RequestHandler manages path requests and responses
type RequestHandler struct {
Path string
ResponseGenerator func(path string, data []byte, requestID []byte, linkID []byte, remoteIdentity interface{}, requestedAt int64) []byte
AllowMode byte
AllowedList [][]byte
}
// Interface types
type InterfaceMode byte
type InterfaceType byte
// PacketCallback defines the function signature for packet handling
type PacketCallback func([]byte, interface{})
// RatchetIDReceiver holds ratchet ID information
type RatchetIDReceiver struct {
LatestRatchetID []byte
}
LatestRatchetID []byte
}
// NetworkStats holds interface statistics
type NetworkStats struct {
BytesSent uint64
BytesReceived uint64
PacketsSent uint64
PacketsReceived uint64
LastUpdated time.Time
}
// LinkStatus represents the current state of a link
type LinkStatus struct {
Established bool
LastSeen time.Time
RTT time.Duration
Quality float64
Hops uint8
}
// PathRequest represents a path discovery request
type PathRequest struct {
DestinationHash []byte
Tag []byte
TTL int
Recursive bool
}
// PathResponse represents a path discovery response
type PathResponse struct {
DestinationHash []byte
NextHop []byte
Hops uint8
Tag []byte
}