diff options
Diffstat (limited to 'pkg/tcpip/sample/tun_tcp_connect/main.go')
-rw-r--r-- | pkg/tcpip/sample/tun_tcp_connect/main.go | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/pkg/tcpip/sample/tun_tcp_connect/main.go b/pkg/tcpip/sample/tun_tcp_connect/main.go index a1a818a6c..6fbd230aa 100644 --- a/pkg/tcpip/sample/tun_tcp_connect/main.go +++ b/pkg/tcpip/sample/tun_tcp_connect/main.go @@ -60,6 +60,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/link/tun" "gvisor.dev/gvisor/pkg/tcpip/link/tunnel" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" + "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/gre" "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" @@ -95,6 +96,26 @@ func writer(ch chan struct{}, ep tcpip.Endpoint) { } } +func parseIP(s string) tcpip.Address { + addr := tcpip.Address(net.ParseIP(s)) + ip4addr := addr.To4(); if ip4addr != "" { + return ip4addr + } else { + return addr + } +} + +func addAddress(s *stack.Stack, id tcpip.NICID, addr tcpip.Address) *tcpip.Error { + var proto tcpip.NetworkProtocolNumber + if addr.To4() == "" { + proto = ipv6.ProtocolNumber + } else { + proto = ipv4.ProtocolNumber + } + + return s.AddAddress(id, proto, addr) +} + func main() { if len(os.Args) != 8 { log.Fatal("Usage: ", os.Args[0], " <tun-device> <local-ipv4-address> <local-port> <remote-ipv4-address> <remote-port> <local-gre-address> <remote-gre-address>") @@ -110,12 +131,12 @@ func main() { rand.Seed(time.Now().UnixNano()) - addr := tcpip.Address(net.ParseIP(addrName).To4()) - greAddr := tcpip.Address(net.ParseIP(greAddrName).To4()) - greRemoteAddr := tcpip.Address(net.ParseIP(greRemoteAddrName).To4()) + addr := parseIP(addrName) + greAddr := parseIP(greAddrName) + greRemoteAddr := parseIP(greRemoteAddrName) remote := tcpip.FullAddress{ NIC: 0, - Addr: tcpip.Address(net.ParseIP(remoteAddrName).To4()), + Addr: parseIP(remoteAddrName), } log.Printf("local:%v remote:%v", addr, remote) @@ -136,7 +157,7 @@ func main() { // Create the stack with ipv4 and tcp protocols, then add a tun-based // NIC and ipv4 address. s := stack.New(stack.Options{ - NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol}, + NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol}, TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, gre.NewProtocol}, }) @@ -158,7 +179,7 @@ func main() { log.Fatal(err) } - if err := s.AddAddress(1, ipv4.ProtocolNumber, greAddr); err != nil { + if err := addAddress(s, 1, greAddr); err != nil { log.Fatal(err) } @@ -166,14 +187,14 @@ func main() { if err := s.CreateNIC(2, sniffer.New(greEP)); err != nil { log.Fatal(err) } - if err := s.AddAddress(2, ipv4.ProtocolNumber, addr); err != nil { + if err := addAddress(s, 2, addr); err != nil { log.Fatal(err) } // Add default route. subnet, err := tcpip.NewSubnet( - tcpip.Address(net.ParseIP("10.0.0.0").To4()), - tcpip.AddressMask(net.ParseIP("255.255.255.0").To4())) + tcpip.Address(parseIP("10.0.0.0")), + tcpip.AddressMask(parseIP("255.255.255.0"))) if err != nil { panic(err) } @@ -186,6 +207,10 @@ func main() { Destination: header.IPv4EmptySubnet, NIC: 2, }, + { + Destination: header.IPv6EmptySubnet, + NIC: 1, + }, }) log.Printf("Nics enabled 1:%v 2:%v 3:%v", s.CheckNIC(1), s.CheckNIC(2), s.CheckNIC(3)) |