Skip to content

Commit

Permalink
Avoid net.UnixConn type assertions
Browse files Browse the repository at this point in the history
Use `net.DialUnix` rather than `net.Dial` to avoid type assertion.

Extract netutil to house a new `DialUnix` that hides the messier
interface presented by the standard library.

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
  • Loading branch information
tamird committed Nov 19, 2024
1 parent 1f0113c commit c1c8174
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/lima-vm/lima/pkg/hostagent/events"
"github.com/lima-vm/lima/pkg/identifierutil"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/netutil"
"github.com/lima-vm/lima/pkg/networks"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/portfwd"
Expand Down Expand Up @@ -631,8 +632,7 @@ func (a *HostAgent) createConnection(ctx context.Context) (net.Conn, error) {
conn, err := a.driver.GuestAgentConn(ctx)
// default to forwarded sock
if conn == nil && err == nil {
var d net.Dialer
conn, err = d.DialContext(ctx, "unix", filepath.Join(a.instDir, filenames.GuestAgentSock))
conn, err = netutil.DialUnix("unix", filepath.Join(a.instDir, filenames.GuestAgentSock))
}
return conn, err
}
Expand Down
14 changes: 5 additions & 9 deletions pkg/hostagent/port_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/lima-vm/lima/pkg/bicopy"
"github.com/lima-vm/lima/pkg/netutil"
"github.com/lima-vm/lima/pkg/portfwd"
"github.com/lima-vm/sshocker/pkg/ssh"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -43,7 +44,7 @@ func forwardTCP(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
if verb == verbCancel {
plf, ok := pseudoLoopbackForwarders[local]
if ok {
localUnix := plf.unixAddr.Name
localUnix := plf.unixSock
_ = plf.Close()
delete(pseudoLoopbackForwarders, local)
if err := forwardSSH(ctx, sshConfig, port, localUnix, remote, verb, false); err != nil {
Expand Down Expand Up @@ -87,16 +88,11 @@ var pseudoLoopbackForwarders = make(map[string]*pseudoLoopbackForwarder)

type pseudoLoopbackForwarder struct {
ln *net.TCPListener
unixAddr *net.UnixAddr
unixSock string
onClose func() error
}

func newPseudoLoopbackForwarder(localPort int, unixSock string) (*pseudoLoopbackForwarder, error) {
unixAddr, err := net.ResolveUnixAddr("unix", unixSock)
if err != nil {
return nil, err
}

// use "tcp" network to listen on both "tcp4" and "tcp6"
lnAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("0.0.0.0:%d", localPort))
if err != nil {
Expand All @@ -109,7 +105,7 @@ func newPseudoLoopbackForwarder(localPort int, unixSock string) (*pseudoLoopback

plf := &pseudoLoopbackForwarder{
ln: ln,
unixAddr: unixAddr,
unixSock: unixSock,
}

return plf, nil
Expand Down Expand Up @@ -144,7 +140,7 @@ func (plf *pseudoLoopbackForwarder) Serve() error {

func (plf *pseudoLoopbackForwarder) forward(ac *net.TCPConn) error {
defer ac.Close()
unixConn, err := net.DialUnix("unix", nil, plf.unixAddr)
unixConn, err := netutil.DialUnix("unix", plf.unixSock)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/httpclientutil/httpclientutil_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net"
"net/http"
"os"

"github.com/lima-vm/lima/pkg/netutil"
)

// NewHTTPClientWithSocketPath creates a client.
Expand All @@ -18,8 +20,7 @@ func NewHTTPClientWithSocketPath(socketPath string) (*http.Client, error) {
hc := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {

Check failure on line 22 in pkg/httpclientutil/httpclientutil_others.go

View workflow job for this annotation

GitHub Actions / Lints

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
var d net.Dialer
return d.DialContext(ctx, "unix", socketPath)
return netutil.DialUnix("unix", socketPath)
},
},
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/httpclientutil/httpclientutil_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"net/http"
"os"

"github.com/lima-vm/lima/pkg/netutil"
)

// NewHTTPClientWithSocketPath creates a client.
Expand All @@ -17,8 +19,7 @@ func NewHTTPClientWithSocketPath(socketPath string) (*http.Client, error) {
hc := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "unix", socketPath)
return netutil.DialUnix("unix", socketPath)
},
},
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/netutil/netutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package netutil

import "net"

func DialUnix(network, address string) (*net.UnixConn, error) {
addr, err := net.ResolveUnixAddr(network, address)
if err != nil {
return nil, err
}
return net.DialUnix(network, nil, addr)
}
3 changes: 2 additions & 1 deletion pkg/networks/usernet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/httpclientutil"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/netutil"
"github.com/lima-vm/lima/pkg/networks/usernet/dnshosts"
)

Expand Down Expand Up @@ -140,7 +141,7 @@ func create(sock string, subnet net.IP, base string) *Client {
client := &http.Client{
Transport: &http.Transport{
DialContext: func(context.Context, string, string) (net.Conn, error) {
return net.Dial("unix", sock)
return netutil.DialUnix("unix", sock)
},
},
}
Expand Down
13 changes: 4 additions & 9 deletions pkg/qemu/qemu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/digitalocean/go-qemu/qmp/raw"
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/netutil"
"github.com/lima-vm/lima/pkg/networks/usernet"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
Expand Down Expand Up @@ -396,10 +397,8 @@ func (l *LimaQemuDriver) ListSnapshots(_ context.Context) (string, error) {
return List(qCfg, l.Instance.Status == store.StatusRunning)
}

func (l *LimaQemuDriver) GuestAgentConn(ctx context.Context) (net.Conn, error) {
var d net.Dialer
dialContext, err := d.DialContext(ctx, "unix", filepath.Join(l.Instance.Dir, filenames.GuestAgentSock))
return dialContext, err
func (l *LimaQemuDriver) GuestAgentConn(context.Context) (net.Conn, error) {
return netutil.DialUnix("unix", filepath.Join(l.Instance.Dir, filenames.GuestAgentSock))
}

type qArgTemplateApplier struct {
Expand All @@ -417,11 +416,7 @@ func (a *qArgTemplateApplier) applyTemplate(qArg string) (string, error) {
if !ok {
return "", fmt.Errorf("non-string argument %+v", v)
}
addr := &net.UnixAddr{
Net: "unix",
Name: s,
}
conn, err := net.DialUnix("unix", nil, addr)
conn, err := netutil.DialUnix("unix", s)
if err != nil {
return "", err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/vz/network_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
"time"

"github.com/balajiv113/fd"
"github.com/lima-vm/lima/pkg/netutil"

"github.com/sirupsen/logrus"
)

func PassFDToUnix(unixSock string) (*os.File, error) {
unixConn, err := net.Dial("unix", unixSock)
unixConn, err := netutil.DialUnix("unix", unixSock)
if err != nil {
return nil, err
}
Expand All @@ -27,7 +28,7 @@ func PassFDToUnix(unixSock string) (*os.File, error) {
if err != nil {
return nil, err
}
err = fd.Put(unixConn.(*net.UnixConn), server)
err = fd.Put(unixConn, server)
if err != nil {
return nil, err
}
Expand All @@ -37,7 +38,7 @@ func PassFDToUnix(unixSock string) (*os.File, error) {
// DialQemu support connecting to QEMU supported network stack via unix socket.
// Returns os.File, connected dgram connection to be used for vz.
func DialQemu(unixSock string) (*os.File, error) {
unixConn, err := net.Dial("unix", unixSock)
unixConn, err := netutil.DialUnix("unix", unixSock)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c1c8174

Please sign in to comment.