update common packages
This commit is contained in:
@@ -1,5 +1,15 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DEFAULT_SHARED_INSTANCE_PORT = 37428
|
||||||
|
DEFAULT_INSTANCE_CONTROL_PORT = 37429
|
||||||
|
DEFAULT_LOG_LEVEL = 20
|
||||||
|
)
|
||||||
|
|
||||||
// ConfigProvider interface for accessing configuration
|
// ConfigProvider interface for accessing configuration
|
||||||
type ConfigProvider interface {
|
type ConfigProvider interface {
|
||||||
GetConfigPath() string
|
GetConfigPath() string
|
||||||
@@ -9,28 +19,55 @@ type ConfigProvider interface {
|
|||||||
|
|
||||||
// InterfaceConfig represents interface configuration
|
// InterfaceConfig represents interface configuration
|
||||||
type InterfaceConfig struct {
|
type InterfaceConfig struct {
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
Type string `toml:"type"`
|
Type string `toml:"type"`
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled"`
|
||||||
Address string `toml:"address"`
|
Address string `toml:"address"`
|
||||||
Port int `toml:"port"`
|
Port int `toml:"port"`
|
||||||
TargetHost string `toml:"target_host"`
|
TargetHost string `toml:"target_host"`
|
||||||
TargetPort int `toml:"target_port"`
|
TargetPort int `toml:"target_port"`
|
||||||
TargetAddress string `toml:"target_address"`
|
TargetAddress string `toml:"target_address"`
|
||||||
Interface string `toml:"interface"`
|
Interface string `toml:"interface"`
|
||||||
KISSFraming bool `toml:"kiss_framing"`
|
KISSFraming bool `toml:"kiss_framing"`
|
||||||
I2PTunneled bool `toml:"i2p_tunneled"`
|
I2PTunneled bool `toml:"i2p_tunneled"`
|
||||||
PreferIPv6 bool `toml:"prefer_ipv6"`
|
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
|
// ReticulumConfig represents the main configuration structure
|
||||||
type ReticulumConfig struct {
|
type ReticulumConfig struct {
|
||||||
ConfigPath string `toml:"-"`
|
ConfigPath string `toml:"-"`
|
||||||
EnableTransport bool `toml:"enable_transport"`
|
EnableTransport bool `toml:"enable_transport"`
|
||||||
ShareInstance bool `toml:"share_instance"`
|
ShareInstance bool `toml:"share_instance"`
|
||||||
SharedInstancePort int `toml:"shared_instance_port"`
|
SharedInstancePort int `toml:"shared_instance_port"`
|
||||||
InstanceControlPort int `toml:"instance_control_port"`
|
InstanceControlPort int `toml:"instance_control_port"`
|
||||||
PanicOnInterfaceErr bool `toml:"panic_on_interface_error"`
|
PanicOnInterfaceErr bool `toml:"panic_on_interface_error"`
|
||||||
LogLevel int `toml:"loglevel"`
|
LogLevel int `toml:"loglevel"`
|
||||||
Interfaces map[string]*InterfaceConfig `toml:"interfaces"`
|
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
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,60 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Interface Types
|
// Interface Types
|
||||||
IF_TYPE_UDP InterfaceType = iota
|
IF_TYPE_NONE InterfaceType = iota
|
||||||
IF_TYPE_TCP
|
IF_TYPE_UDP
|
||||||
IF_TYPE_UNIX
|
IF_TYPE_TCP
|
||||||
|
IF_TYPE_UNIX
|
||||||
|
IF_TYPE_I2P
|
||||||
|
IF_TYPE_BLUETOOTH
|
||||||
|
IF_TYPE_SERIAL
|
||||||
|
|
||||||
// Interface Modes
|
// Interface Modes
|
||||||
IF_MODE_FULL InterfaceMode = iota
|
IF_MODE_FULL InterfaceMode = iota
|
||||||
IF_MODE_POINT
|
IF_MODE_POINT
|
||||||
IF_MODE_GATEWAY
|
IF_MODE_GATEWAY
|
||||||
|
IF_MODE_ACCESS_POINT
|
||||||
|
IF_MODE_ROAMING
|
||||||
|
IF_MODE_BOUNDARY
|
||||||
|
|
||||||
// Transport Modes
|
// Transport Modes
|
||||||
TRANSPORT_MODE_DIRECT TransportMode = iota
|
TRANSPORT_MODE_DIRECT TransportMode = iota
|
||||||
TRANSPORT_MODE_RELAY
|
TRANSPORT_MODE_RELAY
|
||||||
TRANSPORT_MODE_GATEWAY
|
TRANSPORT_MODE_GATEWAY
|
||||||
|
|
||||||
// Path Status
|
// Path Status
|
||||||
PATH_STATUS_UNKNOWN PathStatus = iota
|
PATH_STATUS_UNKNOWN PathStatus = iota
|
||||||
PATH_STATUS_DIRECT
|
PATH_STATUS_DIRECT
|
||||||
PATH_STATUS_RELAY
|
PATH_STATUS_RELAY
|
||||||
PATH_STATUS_FAILED
|
PATH_STATUS_FAILED
|
||||||
|
|
||||||
// Common Constants
|
// Resource Status
|
||||||
DEFAULT_MTU = 1500
|
RESOURCE_STATUS_PENDING = 0x00
|
||||||
MAX_PACKET_SIZE = 65535
|
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
|
||||||
|
)
|
||||||
|
|||||||
@@ -6,38 +6,44 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NetworkInterface combines both low-level and high-level interface requirements
|
// NetworkInterface defines the interface for all network communication methods
|
||||||
type NetworkInterface interface {
|
type NetworkInterface interface {
|
||||||
// Low-level network operations
|
// Core interface operations
|
||||||
Start() error
|
Start() error
|
||||||
Stop() error
|
Stop() error
|
||||||
|
Enable()
|
||||||
|
Disable()
|
||||||
|
Detach()
|
||||||
|
|
||||||
|
// Network operations
|
||||||
Send(data []byte, address string) error
|
Send(data []byte, address string) error
|
||||||
Receive() ([]byte, string, error)
|
GetConn() net.Conn
|
||||||
|
GetMTU() int
|
||||||
|
GetName() string
|
||||||
|
|
||||||
|
// Interface properties
|
||||||
GetType() InterfaceType
|
GetType() InterfaceType
|
||||||
GetMode() InterfaceMode
|
GetMode() InterfaceMode
|
||||||
GetMTU() int
|
IsEnabled() bool
|
||||||
|
IsOnline() bool
|
||||||
// High-level packet operations
|
IsDetached() bool
|
||||||
|
|
||||||
|
// Packet handling
|
||||||
ProcessIncoming([]byte)
|
ProcessIncoming([]byte)
|
||||||
ProcessOutgoing([]byte) error
|
ProcessOutgoing([]byte) error
|
||||||
SendPathRequest([]byte) error
|
SendPathRequest([]byte) error
|
||||||
SendLinkPacket([]byte, []byte, time.Time) error
|
SendLinkPacket([]byte, []byte, time.Time) error
|
||||||
Detach()
|
|
||||||
SetPacketCallback(PacketCallback)
|
SetPacketCallback(PacketCallback)
|
||||||
|
GetPacketCallback() PacketCallback
|
||||||
// Additional required fields
|
|
||||||
GetName() string
|
|
||||||
GetConn() net.Conn
|
|
||||||
IsEnabled() bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BaseInterface provides common implementation
|
// BaseInterface provides common implementation for network interfaces
|
||||||
type BaseInterface struct {
|
type BaseInterface struct {
|
||||||
Name string
|
Name string
|
||||||
Mode InterfaceMode
|
Mode InterfaceMode
|
||||||
Type InterfaceType
|
Type InterfaceType
|
||||||
|
|
||||||
Online bool
|
Online bool
|
||||||
|
Enabled bool
|
||||||
Detached bool
|
Detached bool
|
||||||
|
|
||||||
IN bool
|
IN bool
|
||||||
@@ -53,3 +59,82 @@ type BaseInterface struct {
|
|||||||
Owner interface{}
|
Owner interface{}
|
||||||
PacketCallback PacketCallback
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
type TransportMode byte
|
type TransportMode byte
|
||||||
type PathStatus byte
|
type PathStatus byte
|
||||||
|
|
||||||
// Common structs
|
// Path represents routing information for a destination
|
||||||
type Path struct {
|
type Path struct {
|
||||||
Interface NetworkInterface
|
Interface NetworkInterface
|
||||||
LastSeen time.Time
|
LastSeen time.Time
|
||||||
@@ -20,20 +20,55 @@ type Path struct {
|
|||||||
// Common callbacks
|
// Common callbacks
|
||||||
type ProofRequestedCallback func([]byte, []byte)
|
type ProofRequestedCallback func([]byte, []byte)
|
||||||
type LinkEstablishedCallback func(interface{})
|
type LinkEstablishedCallback func(interface{})
|
||||||
|
type PacketCallback func([]byte, interface{})
|
||||||
|
|
||||||
// Request handler
|
// RequestHandler manages path requests and responses
|
||||||
type RequestHandler struct {
|
type RequestHandler struct {
|
||||||
Path string
|
Path string
|
||||||
ResponseGenerator func(path string, data []byte, requestID []byte, linkID []byte, remoteIdentity interface{}, requestedAt int64) []byte
|
ResponseGenerator func(path string, data []byte, requestID []byte, linkID []byte, remoteIdentity interface{}, requestedAt int64) []byte
|
||||||
AllowMode byte
|
AllowMode byte
|
||||||
AllowedList [][]byte
|
AllowedList [][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Interface types
|
||||||
type InterfaceMode byte
|
type InterfaceMode byte
|
||||||
type InterfaceType byte
|
type InterfaceType byte
|
||||||
|
|
||||||
// PacketCallback defines the function signature for packet handling
|
// RatchetIDReceiver holds ratchet ID information
|
||||||
type PacketCallback func([]byte, interface{})
|
|
||||||
|
|
||||||
type RatchetIDReceiver struct {
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user