-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b93db96
commit 150b116
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//go:build with_gvisor | ||
|
||
package tun | ||
|
||
import ( | ||
"github.com/sagernet/gvisor/pkg/tcpip" | ||
"github.com/sagernet/gvisor/pkg/tcpip/header" | ||
"github.com/sagernet/gvisor/pkg/tcpip/stack" | ||
"github.com/sagernet/sing/common/bufio" | ||
N "github.com/sagernet/sing/common/network" | ||
) | ||
|
||
var _ stack.LinkEndpoint = (*LinkEndpointFilter)(nil) | ||
|
||
type LinkEndpointFilter struct { | ||
stack.LinkEndpoint | ||
Writer N.VectorisedWriter | ||
} | ||
|
||
func (w *LinkEndpointFilter) Attach(dispatcher stack.NetworkDispatcher) { | ||
w.LinkEndpoint.Attach(&networkDispatcherFilter{dispatcher, w.Writer}) | ||
} | ||
|
||
var _ stack.NetworkDispatcher = (*networkDispatcherFilter)(nil) | ||
|
||
type networkDispatcherFilter struct { | ||
stack.NetworkDispatcher | ||
writer N.VectorisedWriter | ||
} | ||
|
||
func (w *networkDispatcherFilter) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) { | ||
var network header.Network | ||
if protocol == header.IPv4ProtocolNumber { | ||
if headerPackets, loaded := pkt.Data().PullUp(header.IPv4MinimumSize); loaded { | ||
network = header.IPv4(headerPackets) | ||
} | ||
} else { | ||
if headerPackets, loaded := pkt.Data().PullUp(header.IPv6MinimumSize); loaded { | ||
network = header.IPv6(headerPackets) | ||
} | ||
} | ||
if network == nil { | ||
w.NetworkDispatcher.DeliverNetworkPacket(protocol, pkt) | ||
return | ||
} | ||
destination := AddrFromAddress(network.DestinationAddress()) | ||
if destination.IsMulticast() || !destination.IsGlobalUnicast() { | ||
_, _ = bufio.WriteVectorised(w.writer, pkt.AsSlices()) | ||
return | ||
} | ||
w.NetworkDispatcher.DeliverNetworkPacket(protocol, pkt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters