mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-21 12:28:39 +00:00
add flag to demo hard-vs-easy quickly
Change-Id: I48945c0b84e7e863b71dd6bdf97cd41b3b1e81bb Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
38aa2687ac
commit
ba5ada52ad
@ -159,12 +159,13 @@ func (n *easyNAT) PickOutgoingSrc(src, dst netip.AddrPort, at time.Time) (wanSrc
|
||||
// position and looping back around to the start.
|
||||
start := rand.N(uint16(32 << 10))
|
||||
for off := range uint16(32 << 10) {
|
||||
port := 32<<10 + (start + off)
|
||||
port := 32<<10 + (start+off)%(32<<10)
|
||||
if _, ok := n.in[port]; !ok {
|
||||
wanAddr := netip.AddrPortFrom(n.wanIP, port)
|
||||
// Found a free port.
|
||||
mak.Set(&n.out, src, portMappingAndTime{port: port, at: at})
|
||||
mak.Set(&n.in, port, lanAddrAndTime{lanAddr: src, at: at})
|
||||
return netip.AddrPortFrom(n.wanIP, port)
|
||||
return wanAddr
|
||||
}
|
||||
}
|
||||
return netip.AddrPort{} // failed to allocate a mapping; TODO: fire an alert?
|
||||
|
@ -45,6 +45,7 @@ import (
|
||||
|
||||
var (
|
||||
listen = flag.String("listen", "/tmp/qemu.sock", "path to listen on")
|
||||
hard = flag.Bool("hard", false, "use hard NAT")
|
||||
)
|
||||
|
||||
const nicID = 1
|
||||
@ -68,6 +69,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Hard-coded world shape for me.
|
||||
|
||||
net1 := &network{
|
||||
s: s,
|
||||
mac: MAC{0x52, 0x54, 0x00, 0x01, 0x01, 0x01},
|
||||
@ -81,7 +83,7 @@ func main() {
|
||||
}
|
||||
|
||||
s.nodes[node1.mac] = node1
|
||||
net1.SetNATTable(&hardNAT{wanIP: net1.wanIP})
|
||||
net1.InitNAT(*hard)
|
||||
net2 := &network{
|
||||
s: s,
|
||||
mac: MAC{0x52, 0x54, 0x00, 0x01, 0x01, 0x2},
|
||||
@ -94,7 +96,7 @@ func main() {
|
||||
lanIP: netip.MustParseAddr("10.2.0.102"),
|
||||
}
|
||||
s.nodes[node2.mac] = node2
|
||||
net2.SetNATTable(&oneToOneNAT{wanIP: net2.wanIP, lanIP: node2.lanIP})
|
||||
net2.InitNAT(*hard)
|
||||
|
||||
if err := s.checkWorld(); err != nil {
|
||||
log.Fatalf("checkWorld: %v", err)
|
||||
@ -197,6 +199,14 @@ func (s *Server) checkWorld() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *network) InitNAT(useHardNAT bool) {
|
||||
if useHardNAT {
|
||||
n.SetNATTable(&hardNAT{wanIP: n.wanIP})
|
||||
} else {
|
||||
n.SetNATTable(&easyNAT{wanIP: n.wanIP})
|
||||
}
|
||||
}
|
||||
|
||||
func (n *network) SetNATTable(nt NATTable) {
|
||||
n.natMu.Lock()
|
||||
defer n.natMu.Unlock()
|
||||
@ -676,7 +686,7 @@ func (n *network) HandleUDPPacket(p UDPPacket) {
|
||||
}
|
||||
node, ok := n.nodesByIP[dst.Addr()]
|
||||
if !ok {
|
||||
log.Printf("no node for dest IP %v", dst.Addr())
|
||||
log.Printf("no node for dest IP %v in UDP packet %v=>%v", dst.Addr(), p.Src, p.Dst)
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user