Documentation ¶
Overview ¶
Package vnet provides a virtual network layer for pion
Index ¶
- Constants
- type Chunk
- type ChunkFilter
- type DelayFilter
- type Dialer
- type EndpointDependencyType
- type Interface
- type InterfaceBase
- type LossFilter
- type NATMode
- type NATType
- type NIC
- type Net
- func (n *Net) CreateDialer(dialer *net.Dialer) Dialer
- func (n *Net) Dial(network, address string) (net.Conn, error)
- func (n *Net) DialUDP(network string, laddr, raddr *net.UDPAddr) (UDPPacketConn, error)
- func (n *Net) InterfaceByName(name string) (*Interface, error)
- func (n *Net) Interfaces() ([]*Interface, error)
- func (n *Net) IsVirtual() bool
- func (n *Net) ListenPacket(network string, address string) (net.PacketConn, error)
- func (n *Net) ListenUDP(network string, locAddr *net.UDPAddr) (UDPPacketConn, error)
- func (n *Net) ResolveUDPAddr(network, address string) (*net.UDPAddr, error)
- type NetConfig
- type Router
- func (r *Router) AddChildRouter(router *Router) error
- func (r *Router) AddChunkFilter(filter ChunkFilter)
- func (r *Router) AddHost(hostName string, ipAddr string) error
- func (r *Router) AddNet(nic NIC) error
- func (r *Router) AddRouter(router *Router) error
- func (r *Router) Start() error
- func (r *Router) Stop() error
- type RouterConfig
- type TBFOption
- type TokenBucketFilter
- type UDPConn
- func (c *UDPConn) Close() error
- func (c *UDPConn) LocalAddr() net.Addr
- func (c *UDPConn) Read(b []byte) (int, error)
- func (c *UDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
- func (c *UDPConn) RemoteAddr() net.Addr
- func (c *UDPConn) SetDeadline(t time.Time) error
- func (c *UDPConn) SetReadDeadline(t time.Time) error
- func (c *UDPConn) SetWriteDeadline(t time.Time) error
- func (c *UDPConn) Write(b []byte) (int, error)
- func (c *UDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error)
- type UDPPacketConn
- type UDPProxy
Constants ¶
const ( // Bit is a single bit Bit = 1 // KBit is a kilobit KBit = 1000 * Bit // MBit is a Megabit MBit = 1000 * KBit )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chunk ¶
type Chunk interface { SourceAddr() net.Addr DestinationAddr() net.Addr UserData() []byte Tag() string Clone() Chunk Network() string // returns "udp" or "tcp" String() string // contains filtered or unexported methods }
Chunk represents a packet passed around in the vnet
type ChunkFilter ¶ added in v0.8.7
ChunkFilter is a handler users can add to filter chunks. If the filter returns false, the packet will be dropped.
type DelayFilter ¶ added in v0.13.0
type DelayFilter struct { NIC // contains filtered or unexported fields }
DelayFilter delays outgoing packets by the given delay. Run must be called before any packets will be forwarded.
func NewDelayFilter ¶ added in v0.13.0
func NewDelayFilter(nic NIC, delay time.Duration) (*DelayFilter, error)
NewDelayFilter creates a new DelayFilter with the given nic and delay.
func (*DelayFilter) Run ¶ added in v0.13.0
func (f *DelayFilter) Run(ctx context.Context)
Run starts forwarding of packets. Packets will be forwarded if they spent >delay time in the internal queue. Must be called before any packet will be forwarded.
type Dialer ¶
Dialer is identical to net.Dialer excepts that its methods (Dial, DialContext) are overridden to use virtual network. Use vnet.CreateDialer() to create an instance of this Dialer.
type EndpointDependencyType ¶
type EndpointDependencyType uint8
EndpointDependencyType defines a type of behavioral dependendency on the remote endpoint's IP address or port number. This is used for the two kinds of behaviors:
- Port mapping behavior
- Filtering behavior
See: https://tools.ietf.org/html/rfc4787
const ( // EndpointIndependent means the behavior is independent of the endpoint's address or port EndpointIndependent EndpointDependencyType = iota // EndpointAddrDependent means the behavior is dependent on the endpoint's address EndpointAddrDependent // EndpointAddrPortDependent means the behavior is dependent on the endpoint's address and port EndpointAddrPortDependent )
type Interface ¶
type Interface struct { InterfaceBase // contains filtered or unexported fields }
Interface ...
type LossFilter ¶ added in v0.13.0
type LossFilter struct { NIC // contains filtered or unexported fields }
LossFilter is a wrapper around NICs, that drops some of the packets passed to onInboundChunk
func NewLossFilter ¶ added in v0.13.0
func NewLossFilter(nic NIC, chance int) (*LossFilter, error)
NewLossFilter creates a new LossFilter that drops every packet with a probability of chance/100. Every packet that is not dropped is passed on to the given NIC.
type NATMode ¶ added in v0.8.9
type NATMode uint8
NATMode defines basic behavior of the NAT
const ( // NATModeNormal means the NAT behaves as a standard NAPT (RFC 2663). NATModeNormal NATMode = iota // NATModeNAT1To1 exhibits 1:1 DNAT where the external IP address is statically mapped to // a specific local IP address with port number is preserved always between them. // When this mode is selected, MappingBehavior, FilteringBehavior, PortPreservation and // MappingLifeTime of NATType are ignored. NATModeNAT1To1 )
type NATType ¶
type NATType struct { Mode NATMode MappingBehavior EndpointDependencyType FilteringBehavior EndpointDependencyType Hairpining bool // Not implemented yet PortPreservation bool // Not implemented yet MappingLifeTime time.Duration }
NATType has a set of parameters that define the behavior of NAT.
type NIC ¶
type NIC interface {
// contains filtered or unexported methods
}
NIC is a nework inerface controller that interfaces Router
type Net ¶
type Net struct {
// contains filtered or unexported fields
}
Net represents a local network stack euivalent to a set of layers from NIC up to the transport (UDP / TCP) layer.
func NewNet ¶
NewNet creates an instance of Net. If config is nil, the virtual network is disabled. (uses corresponding net.Xxxx() operations. By design, it always have lo0 and eth0 interfaces. The lo0 has the address 127.0.0.1 assigned by default. IP address for eth0 will be assigned when this Net is added to a router.
func (*Net) CreateDialer ¶
CreateDialer creates an instance of vnet.Dialer
func (*Net) InterfaceByName ¶
InterfaceByName returns the interface specified by name.
func (*Net) Interfaces ¶
Interfaces returns a list of the system's network interfaces.
func (*Net) ListenPacket ¶
ListenPacket announces on the local network address.
type NetConfig ¶
type NetConfig struct { // StaticIPs is an array of static IP addresses to be assigned for this Net. // If no static IP address is given, the router will automatically assign // an IP address. StaticIPs []string // StaticIP is deprecated. Use StaticIPs. StaticIP string }
NetConfig is a bag of configuration parameters passed to NewNet().
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router ...
func (*Router) AddChildRouter ¶ added in v0.13.0
AddChildRouter is like AddRouter, but does not add the child routers NIC to the parent. This has to be done manually by calling AddNet, which allows to use a wrapper around the subrouters NIC. AddNet MUST be called before AddChildRouter.
func (*Router) AddChunkFilter ¶ added in v0.8.7
func (r *Router) AddChunkFilter(filter ChunkFilter)
AddChunkFilter adds a filter for chunks traversing this router. You may add more than one filter. The filters are called in the order of this method call. If a chunk is dropped by a filter, subsequent filter will not receive the chunk.
func (*Router) AddHost ¶
AddHost adds a mapping of hostname and an IP address to the local resolver.
type RouterConfig ¶
type RouterConfig struct { // Name of router. If not specified, a unique name will be assigned. Name string // CIDR notation, like "192.0.2.0/24" CIDR string // StaticIPs is an array of static IP addresses to be assigned for this router. // If no static IP address is given, the router will automatically assign // an IP address. // This will be ignored if this router is the root. StaticIPs []string // StaticIP is deprecated. Use StaticIPs. StaticIP string // Internal queue size QueueSize int // Effective only when this router has a parent router NATType *NATType // Minimum Delay MinDelay time.Duration // Max Jitter MaxJitter time.Duration // Logger factory LoggerFactory logging.LoggerFactory }
RouterConfig ...
type TBFOption ¶ added in v0.13.0
type TBFOption func(*TokenBucketFilter) TBFOption
TBFOption is the option type to configure a TokenBucketFilter
func TBFMaxBurst ¶ added in v0.13.0
TBFMaxBurst sets the bucket size of the token bucket filter. This is the maximum size that can instantly leave the filter, if the bucket is full.
func TBFQueueSizeInBytes ¶ added in v0.13.0
TBFQueueSizeInBytes sets the max number of bytes waiting in the queue. Can only be set in constructor before using the TBF.
type TokenBucketFilter ¶ added in v0.13.0
type TokenBucketFilter struct { NIC // contains filtered or unexported fields }
TokenBucketFilter implements a token bucket rate limit algorithm.
func NewTokenBucketFilter ¶ added in v0.13.0
func NewTokenBucketFilter(n NIC, opts ...TBFOption) (*TokenBucketFilter, error)
NewTokenBucketFilter creates and starts a new TokenBucketFilter
func (*TokenBucketFilter) Close ¶ added in v0.13.0
func (t *TokenBucketFilter) Close() error
Close closes and stops the token bucket filter queue
func (*TokenBucketFilter) Set ¶ added in v0.13.0
func (t *TokenBucketFilter) Set(opts ...TBFOption) (previous TBFOption)
Set updates a setting on the token bucket filter
type UDPConn ¶
type UDPConn struct {
// contains filtered or unexported fields
}
UDPConn is the implementation of the Conn and PacketConn interfaces for UDP network connections. comatible with net.PacketConn and net.Conn
func (*UDPConn) Close ¶
Close closes the connection. Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
func (*UDPConn) Read ¶
Read reads data from the connection. Read can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.
func (*UDPConn) ReadFrom ¶
ReadFrom reads a packet from the connection, copying the payload into p. It returns the number of bytes copied into p and the return address that was on the packet. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Callers should always process the n > 0 bytes returned before considering the error err. ReadFrom can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.
func (*UDPConn) RemoteAddr ¶
RemoteAddr returns the remote network address.
func (*UDPConn) SetDeadline ¶
SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
A deadline is an absolute time after which I/O operations fail with a timeout (see type Error) instead of blocking. The deadline applies to all future and pending I/O, not just the immediately following call to ReadFrom or WriteTo. After a deadline has been exceeded, the connection can be refreshed by setting a deadline in the future.
An idle timeout can be implemented by repeatedly extending the deadline after successful ReadFrom or WriteTo calls.
A zero value for t means I/O operations will not time out.
func (*UDPConn) SetReadDeadline ¶
SetReadDeadline sets the deadline for future ReadFrom calls and any currently-blocked ReadFrom call. A zero value for t means ReadFrom will not time out.
func (*UDPConn) SetWriteDeadline ¶
SetWriteDeadline sets the deadline for future WriteTo calls and any currently-blocked WriteTo call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means WriteTo will not time out.
func (*UDPConn) Write ¶
Write writes data to the connection. Write can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline.
type UDPPacketConn ¶
type UDPPacketConn interface { net.PacketConn Read(b []byte) (int, error) RemoteAddr() net.Addr Write(b []byte) (int, error) }
UDPPacketConn is packet-oriented connection for UDP.
type UDPProxy ¶ added in v0.12.3
type UDPProxy struct {
// contains filtered or unexported fields
}
UDPProxy is a proxy between real server(net.UDPConn) and vnet.UDPConn.
High level design:
.............................................. : Virtual Network (vnet) : : : +-------+ * 1 +----+ +--------+ : | :App |------------>|:Net|--o<-----|:Router | ............................. +-------+ +----+ | | : UDPProxy : : | | +----+ +---------+ +---------+ +--------+ : | |--->o--|:Net|-->o-| vnet. |-->o-| net. |--->-| :Real | : | | +----+ | UDPConn | | UDPConn | | Server | : | | : +---------+ +---------+ +--------+ : | | ............................: : +--------+ : ...............................................
func NewProxy ¶ added in v0.12.3
NewProxy create a proxy, the router for this proxy belongs/bind to. If need to proxy for please create a new proxy for each router. For all addresses we proxy, we will create a vnet.Net in this router and proxy all packets.