Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/SagerNet/sing-tun into meta
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 26, 2023
2 parents d78d1d3 + 8adce0e commit 4e9d1ad
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 54 deletions.
12 changes: 10 additions & 2 deletions monitor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tun

import (
"os"
"runtime"
"sync"

"github.com/sagernet/netlink"
Expand All @@ -20,12 +21,19 @@ type networkUpdateMonitor struct {
}

func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
return &networkUpdateMonitor{
monitor := &networkUpdateMonitor{
routeUpdate: make(chan netlink.RouteUpdate, 2),
linkUpdate: make(chan netlink.LinkUpdate, 2),
close: make(chan struct{}),
logger: logger,
}, nil
}
if runtime.GOOS == "android" {
_, err := netlink.LinkList()
if err != nil {
return nil, err
}
}
return monitor, nil
}

func (m *networkUpdateMonitor) Start() error {
Expand Down
3 changes: 1 addition & 2 deletions stack_gvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"net/netip"
"time"
"unsafe"

"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/canceler"
Expand Down Expand Up @@ -130,7 +129,7 @@ func (t *GVisor) Start() error {
endpoint.Abort()
return
}
gConn := &gUDPConn{UDPConn: udpConn, stack: ipStack, packet: (*gRequest)(unsafe.Pointer(request)).pkt.IncRef()}
gConn := &gUDPConn{UDPConn: udpConn}
go func() {
var metadata M.Metadata
metadata.Source = M.SocksaddrFromNet(lAddr)
Expand Down
52 changes: 4 additions & 48 deletions stack_gvisor_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ type UDPForwarder struct {
udpNat *udpnat.Service[netip.AddrPort]

// cache
cacheProto tcpip.NetworkProtocolNumber
cacheID stack.TransportEndpointID
cachePacket stack.PacketBufferPtr
cacheProto tcpip.NetworkProtocolNumber
cacheID stack.TransportEndpointID
}

func NewUDPForwarder(ctx context.Context, stack *stack.Stack, handler Handler, udpTimeout int64) *UDPForwarder {
Expand All @@ -59,7 +58,6 @@ func (f *UDPForwarder) HandlePacket(id stack.TransportEndpointID, pkt stack.Pack
sBuffer.Write(view.AsSlice())
})
f.cacheID = id
f.cachePacket = pkt
f.udpNat.NewPacket(
f.ctx,
upstreamMetadata.Source.AddrPort(),
Expand All @@ -76,30 +74,18 @@ func (f *UDPForwarder) newUDPConn(natConn N.PacketConn) N.PacketWriter {
source: f.cacheID.RemoteAddress,
sourcePort: f.cacheID.RemotePort,
sourceNetwork: f.cacheProto,
packet: f.cachePacket.IncRef(),
}
}

type UDPBackWriter struct {
access sync.Mutex
access sync.Mutex
stack *stack.Stack
source tcpip.Address
sourcePort uint16
sourceNetwork tcpip.NetworkProtocolNumber
packet stack.PacketBufferPtr
}

func (w *UDPBackWriter) Close() error {
w.access.Lock()
defer w.access.Unlock()
if w.packet == nil {
return os.ErrClosed
}
w.packet.DecRef()
w.packet = nil
return nil
}

func (w *UDPBackWriter) WritePacket(packetBuffer *buf.Buffer, destination M.Socksaddr) error {
if !destination.IsIP() {
return E.Cause(os.ErrInvalid, "invalid destination")
Expand Down Expand Up @@ -164,16 +150,6 @@ func (w *UDPBackWriter) WritePacket(packetBuffer *buf.Buffer, destination M.Sock
return nil
}

func (w *UDPBackWriter) HandshakeFailure(err error) error {
if w.packet == nil {
return os.ErrClosed
}
err = gWriteUnreachable(w.stack, w.packet, err)
w.packet.DecRef()
w.packet = nil
return err
}

type gRequest struct {
stack *stack.Stack
id stack.TransportEndpointID
Expand All @@ -182,9 +158,6 @@ type gRequest struct {

type gUDPConn struct {
*gonet.UDPConn
access sync.Mutex
stack *stack.Stack
packet stack.PacketBufferPtr
}

func (c *gUDPConn) Read(b []byte) (n int, err error) {
Expand All @@ -206,27 +179,10 @@ func (c *gUDPConn) Write(b []byte) (n int, err error) {
}

func (c *gUDPConn) Close() error {
c.access.Lock()
defer c.access.Unlock()
if c.packet == nil {
return os.ErrClosed
}
c.packet.DecRef()
c.packet = nil
return c.UDPConn.Close()
}

func (c *gUDPConn) HandshakeFailure(err error) error {
if c.packet == nil {
return os.ErrClosed
}
err = gWriteUnreachable(c.stack, c.packet, err)
c.packet.DecRef()
c.packet = nil
return err
}

func gWriteUnreachable(gStack *stack.Stack, packet stack.PacketBufferPtr, err error) error {
func gWriteUnreachable(gStack *stack.Stack, packet stack.PacketBufferPtr, err error) (retErr error) {
if errors.Is(err, syscall.ENETUNREACH) {
if packet.NetworkProtocolNumber == header.IPv4ProtocolNumber {
return gWriteUnreachable4(gStack, packet, stack.RejectIPv4WithICMPNetUnreachable)
Expand Down
3 changes: 1 addition & 2 deletions stack_mixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package tun

import (
"time"
"unsafe"

"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/bufio"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (m *Mixed) Start() error {
endpoint.Abort()
return
}
gConn := &gUDPConn{UDPConn: udpConn, stack: ipStack, packet: (*gRequest)(unsafe.Pointer(request)).pkt.IncRef()}
gConn := &gUDPConn{UDPConn: udpConn}
go func() {
var metadata M.Metadata
metadata.Source = M.SocksaddrFromNet(lAddr)
Expand Down

0 comments on commit 4e9d1ad

Please sign in to comment.