Skip to content

Commit

Permalink
refactor(netxlite): use *Netx for creating UDP sockets
Browse files Browse the repository at this point in the history
This diff is similar to 07a048c
but here we use *Netx to create UDP sockets.

While there, recognize that UDP code needs its own files
and should not live inside quic{,_test}.go.

Part of ooni/probe#2531
  • Loading branch information
bassosimone committed Sep 12, 2023
1 parent 07a048c commit bd6ec0b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
6 changes: 0 additions & 6 deletions internal/netxlite/netx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ func (netx *Netx) maybeCustomUnderlyingNetwork() *MaybeCustomUnderlyingNetwork {
return &MaybeCustomUnderlyingNetwork{netx.Underlying}
}

// NewUDPListener is like [netxlite.NewUDPListener] but the constructed [model.UDPListener]
// uses the [model.UnderlyingNetwork] configured inside the [Netx] structure.
func (n *Netx) NewUDPListener() model.UDPListener {
return &udpListenerErrWrapper{&udpListenerStdlib{provider: n.maybeCustomUnderlyingNetwork()}}
}

// NewQUICDialerWithResolver is like [netxlite.NewQUICDialerWithResolver] but the constructed
// [model.QUICDialer] uses the [model.UnderlyingNetwork] configured inside the [Netx] structure.
func (n *Netx) NewQUICDialerWithResolver(listener model.UDPListener, logger model.DebugLogger,
Expand Down
19 changes: 0 additions & 19 deletions internal/netxlite/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,6 @@ import (
"github.com/quic-go/quic-go"
)

// NewUDPListener creates a new UDPListener using the standard
// library to create listening UDP sockets.
func NewUDPListener() model.UDPListener {
return &udpListenerErrWrapper{&udpListenerStdlib{}}
}

// udpListenerStdlib is a UDPListener using the standard library.
type udpListenerStdlib struct {
// provider is the OPTIONAL nil-safe [model.UnderlyingNetwork] provider.
provider *MaybeCustomUnderlyingNetwork
}

var _ model.UDPListener = &udpListenerStdlib{}

// Listen implements UDPListener.Listen.
func (qls *udpListenerStdlib) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
return qls.provider.Get().ListenUDP("udp", addr)
}

// NewQUICDialerWithResolver is the WrapDialer equivalent for QUIC where
// we return a composed QUICDialer modified by optional wrappers.
//
Expand Down
6 changes: 0 additions & 6 deletions internal/netxlite/quic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ import (
"github.com/quic-go/quic-go"
)

func TestNewUDPListener(t *testing.T) {
ql := NewUDPListener()
qew := ql.(*udpListenerErrWrapper)
_ = qew.UDPListener.(*udpListenerStdlib)
}

type extensionQUICDialerFirst struct {
model.QUICDialer
}
Expand Down
33 changes: 33 additions & 0 deletions internal/netxlite/udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package netxlite

import (
"net"

"github.com/ooni/probe-cli/v3/internal/model"
)

// NewUDPListener creates a new UDPListener using the underlying
// [*Netx] structure to create listening UDP sockets.
func (netx *Netx) NewUDPListener() model.UDPListener {
return &udpListenerErrWrapper{&udpListenerStdlib{provider: netx.maybeCustomUnderlyingNetwork()}}
}

// NewUDPListener is equivalent to creating an empty [*Netx]
// and calling its NewUDPListener method.
func NewUDPListener() model.UDPListener {
netx := &Netx{Underlying: nil}
return netx.NewUDPListener()
}

// udpListenerStdlib is a UDPListener using the standard library.
type udpListenerStdlib struct {
// provider is the OPTIONAL nil-safe [model.UnderlyingNetwork] provider.
provider *MaybeCustomUnderlyingNetwork
}

var _ model.UDPListener = &udpListenerStdlib{}

// Listen implements UDPListener.Listen.
func (qls *udpListenerStdlib) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
return qls.provider.Get().ListenUDP("udp", addr)
}
9 changes: 9 additions & 0 deletions internal/netxlite/udp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package netxlite

import "testing"

func TestNewUDPListener(t *testing.T) {
ql := NewUDPListener()
qew := ql.(*udpListenerErrWrapper)
_ = qew.UDPListener.(*udpListenerStdlib)
}

0 comments on commit bd6ec0b

Please sign in to comment.