Files
Archive/mieru/pkg/protocol/underlay.go
T
2024-10-02 15:52:17 +02:00

135 lines
4.0 KiB
Go

// Copyright (C) 2023 mieru authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package protocol
import (
"context"
"net"
"github.com/enfein/mieru/v3/pkg/metrics"
"github.com/enfein/mieru/v3/pkg/util"
)
var (
UnderlayMaxConn = metrics.RegisterMetric("underlay", "MaxConn", metrics.GAUGE)
UnderlayActiveOpens = metrics.RegisterMetric("underlay", "ActiveOpens", metrics.COUNTER)
UnderlayPassiveOpens = metrics.RegisterMetric("underlay", "PassiveOpens", metrics.COUNTER)
UnderlayCurrEstablished = metrics.RegisterMetric("underlay", "CurrEstablished", metrics.GAUGE)
UnderlayMalformedUDP = metrics.RegisterMetric("underlay", "UnderlayMalformedUDP", metrics.COUNTER)
UnderlayUnsolicitedUDP = metrics.RegisterMetric("underlay", "UnsolicitedUDP", metrics.COUNTER)
)
// UnderlayProperties defines network properties of a underlay.
type UnderlayProperties interface {
// Layer 2 MTU of this network connection.
MTU() int
// The IP version used to establish the underlay.
IPVersion() util.IPVersion
// The transport protocol used to implement the underlay.
TransportProtocol() util.TransportProtocol
// LocalAddr implements net.Conn interface.
LocalAddr() net.Addr
// RemoteAddr implements net.Conn interface.
RemoteAddr() net.Addr
}
// Underlay contains methods implemented by a underlay network connection.
type Underlay interface {
// Accept incoming sessions.
net.Listener
// Store basic network properties.
UnderlayProperties
// Add a session to the underlay connection.
// Optionally, the remote network address can be specified for the session.
// The session is ready to use when this returns.
AddSession(*Session, net.Addr) error
// Remove a session from the underlay connection.
// The session is destroyed when this returns.
RemoveSession(*Session) error
// Returns the number of sessions.
NSessions() int
// Returns detailed information of all the sessions.
Sessions() []SessionInfo
// Run event loop.
// The underlay needs to be closed when this returns.
RunEventLoop(context.Context) error
// Return the schedule controller.
Scheduler() *ScheduleController
// Indicate the underlay is closed.
Done() chan struct{}
}
// underlayDescriptor implements UnderlayProperties.
type underlayDescriptor struct {
mtu int
ipVersion util.IPVersion
transportProtocol util.TransportProtocol
localAddr net.Addr
remoteAddr net.Addr
}
var _ UnderlayProperties = &underlayDescriptor{}
func (d *underlayDescriptor) MTU() int {
return d.mtu
}
func (d *underlayDescriptor) IPVersion() util.IPVersion {
return d.ipVersion
}
func (d *underlayDescriptor) TransportProtocol() util.TransportProtocol {
return d.transportProtocol
}
func (d *underlayDescriptor) LocalAddr() net.Addr {
return d.localAddr
}
func (d *underlayDescriptor) RemoteAddr() net.Addr {
return d.remoteAddr
}
// NewUnderlayProperties creates a new instance of UnderlayProperties.
func NewUnderlayProperties(mtu int, ipVersion util.IPVersion, transportProtocol util.TransportProtocol, localAddr net.Addr, remoteAddr net.Addr) UnderlayProperties {
d := &underlayDescriptor{
mtu: mtu,
ipVersion: ipVersion,
transportProtocol: transportProtocol,
localAddr: localAddr,
remoteAddr: remoteAddr,
}
if localAddr == nil {
d.localAddr = util.NilNetAddr()
}
if remoteAddr == nil {
d.remoteAddr = util.NilNetAddr()
}
return d
}