diff options
Diffstat (limited to 'tun')
-rw-r--r-- | tun/netstack/tun.go | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index e456bf6..671e865 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -34,6 +34,7 @@ import ( type netTun struct { stack *stack.Stack + nicID tcpip.NICID dispatcher stack.NetworkDispatcher events chan tun.Event incomingPacket chan buffer.VectorisedView @@ -96,14 +97,19 @@ func CreateNetTUN(localAddresses []net.IP, dnsServers []net.IP, mtu int) (tun.De TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol}, HandleLocal: true, } + return CreateNetTUNWithStack(stack.New(opts), 1, localAddresses, dnsServers, mtu) +} + +func CreateNetTUNWithStack(stack *stack.Stack, nicID tcpip.NICID, localAddresses []net.IP, dnsServers []net.IP, mtu int) (Device, *Net, error) { dev := &netTun{ - stack: stack.New(opts), + stack: stack, + nicID: nicID, events: make(chan tun.Event, 10), incomingPacket: make(chan buffer.VectorisedView), dnsServers: dnsServers, mtu: mtu, } - tcpipErr := dev.stack.CreateNIC(1, (*endpoint)(dev)) + tcpipErr := dev.stack.CreateNIC(nicID, (*endpoint)(dev)) if tcpipErr != nil { return nil, nil, fmt.Errorf("CreateNIC: %v", tcpipErr) } @@ -160,7 +166,7 @@ func (tun *netTun) Flush() error { } func (tun *netTun) Close() error { - tun.stack.RemoveNIC(1) + tun.stack.RemoveNIC(tun.nicID) if tun.events != nil { close(tun.events) @@ -193,17 +199,17 @@ func convertToFullAddr(ip net.IP, port int) (tcpip.FullAddress, tcpip.NetworkPro func (net *Net) AddAddress(ip net.IP) *tcpip.Error { if ip4 := ip.To4(); ip4 != nil { - tcpipErr := net.stack.AddAddress(1, ipv4.ProtocolNumber, tcpip.Address(ip4)) + tcpipErr := net.stack.AddAddress(net.nicID, ipv4.ProtocolNumber, tcpip.Address(ip4)) if tcpipErr == nil && !net.hasV4 { net.hasV4 = true - net.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: 1}) + net.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: net.nicID}) } return tcpipErr } else { - tcpipErr := net.stack.AddAddress(1, ipv6.ProtocolNumber, tcpip.Address(ip)) + tcpipErr := net.stack.AddAddress(net.nicID, ipv6.ProtocolNumber, tcpip.Address(ip)) if tcpipErr == nil && !net.hasV6{ net.hasV6 = true - net.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: 1}) + net.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: net.nicID}) } return tcpipErr } |