From 10533c3e73cdb6f4c4f19e01464782b69ace739e Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 31 Mar 2021 13:55:18 -0700 Subject: all: make conn.Bind.Open return a slice of receive functions Instead of hard-coding exactly two sources from which to receive packets (an IPv4 source and an IPv6 source), allow the conn.Bind to specify a set of sources. Beneficial consequences: * If there's no IPv6 support on a system, conn.Bind.Open can choose not to return a receive function for it, which is simpler than tracking that state in the bind. This simplification removes existing data races from both conn.StdNetBind and bindtest.ChannelBind. * If there are more than two sources on a system, the conn.Bind no longer needs to add a separate muxing layer. Signed-off-by: Josh Bleecher Snyder --- conn/bindtest/bindtest.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'conn/bindtest') diff --git a/conn/bindtest/bindtest.go b/conn/bindtest/bindtest.go index ad8fa05..7d43fb3 100644 --- a/conn/bindtest/bindtest.go +++ b/conn/bindtest/bindtest.go @@ -65,12 +65,14 @@ func (c ChannelEndpoint) DstIP() net.IP { return net.IPv4(127, 0, 0, 1) } func (c ChannelEndpoint) SrcIP() net.IP { return nil } -func (c *ChannelBind) Open(port uint16) (actualPort uint16, err error) { +func (c *ChannelBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) { c.closeSignal = make(chan bool) + fns = append(fns, c.makeReceiveFunc(*c.rx4)) + fns = append(fns, c.makeReceiveFunc(*c.rx6)) if rand.Uint32()&1 == 0 { - return uint16(c.source4), nil + return fns, uint16(c.source4), nil } else { - return uint16(c.source6), nil + return fns, uint16(c.source6), nil } } @@ -87,21 +89,14 @@ func (c *ChannelBind) Close() error { func (c *ChannelBind) SetMark(mark uint32) error { return nil } -func (c *ChannelBind) ReceiveIPv6(b []byte) (n int, ep conn.Endpoint, err error) { - select { - case <-c.closeSignal: - return 0, nil, net.ErrClosed - case rx := <-*c.rx6: - return copy(b, rx), c.target6, nil - } -} - -func (c *ChannelBind) ReceiveIPv4(b []byte) (n int, ep conn.Endpoint, err error) { - select { - case <-c.closeSignal: - return 0, nil, net.ErrClosed - case rx := <-*c.rx4: - return copy(b, rx), c.target4, nil +func (c *ChannelBind) makeReceiveFunc(ch chan []byte) conn.ReceiveFunc { + return func(b []byte) (n int, ep conn.Endpoint, err error) { + select { + case <-c.closeSignal: + return 0, nil, net.ErrClosed + case rx := <-ch: + return copy(b, rx), c.target6, nil + } } } -- cgit v1.2.3