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/conn.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'conn/conn.go') diff --git a/conn/conn.go b/conn/conn.go index 6fd232f..3c7fcd0 100644 --- a/conn/conn.go +++ b/conn/conn.go @@ -12,6 +12,11 @@ import ( "strings" ) +// A ReceiveFunc receives a single inbound packet from the network. +// It writes the data into b. n is the length of the packet. +// ep is the remote endpoint. +type ReceiveFunc func(b []byte) (n int, ep Endpoint, err error) + // A Bind listens on a port for both IPv6 and IPv4 UDP traffic. // // A Bind interface may also be a PeekLookAtSocketFd or BindSocketToInterface, @@ -19,23 +24,17 @@ import ( type Bind interface { // Open puts the Bind into a listening state on a given port and reports the actual // port that it bound to. Passing zero results in a random selection. - Open(port uint16) (actualPort uint16, err error) + // fns is the set of functions that will be called to receive packets. + Open(port uint16) (fns []ReceiveFunc, actualPort uint16, err error) // Close closes the Bind listener. + // All fns returned by Open must return net.ErrClosed after a call to Close. Close() error // SetMark sets the mark for each packet sent through this Bind. // This mark is passed to the kernel as the socket option SO_MARK. SetMark(mark uint32) error - // ReceiveIPv6 reads an IPv6 UDP packet into b. It reports the number of bytes read, - // n, the packet source address ep, and any error. - ReceiveIPv6(b []byte) (n int, ep Endpoint, err error) - - // ReceiveIPv4 reads an IPv4 UDP packet into b. It reports the number of bytes read, - // n, the packet source address ep, and any error. - ReceiveIPv4(b []byte) (n int, ep Endpoint, err error) - // Send writes a packet b to address ep. Send(b []byte, ep Endpoint) error -- cgit v1.2.3