Skip to content

Commit

Permalink
Merge pull request #262 from cfergeau/bsd
Browse files Browse the repository at this point in the history
Refactor transport.Listen code
  • Loading branch information
openshift-merge-robot authored Aug 24, 2023
2 parents 89946ed + 4dbb0ed commit 21e3438
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 55 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ image:

.PHONY: cross
cross: $(TOOLS_BINDIR)/makefat
GOARCH=amd64 GOOS=freebsd go build $(LDFLAGS) -o bin/gvproxy-freebsd-amd64 ./cmd/gvproxy
GOARCH=amd64 GOOS=windows go build $(LDFLAGS) -o bin/gvproxy-windows.exe ./cmd/gvproxy
GOARCH=amd64 GOOS=linux go build $(LDFLAGS) -o bin/gvproxy-linux-amd64 ./cmd/gvproxy
GOARCH=arm64 GOOS=linux go build $(LDFLAGS) -o bin/gvproxy-linux-arm64 ./cmd/gvproxy
Expand Down
26 changes: 26 additions & 0 deletions pkg/transport/listen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package transport

import (
"errors"
"net"
"net/url"
)

func defaultListenURL(url *url.URL) (net.Listener, error) {
switch url.Scheme {
case "unix":
return net.Listen(url.Scheme, url.Path)
case "tcp":
return net.Listen("tcp", url.Host)
default:
return nil, errors.New("unexpected scheme")
}
}

func Listen(endpoint string) (net.Listener, error) {
parsed, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
return listenURL(parsed)
}
12 changes: 2 additions & 10 deletions pkg/transport/listen_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import (

const DefaultURL = "vsock://null:1024/vm_directory"

func Listen(endpoint string) (net.Listener, error) {
parsed, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
func listenURL(parsed *url.URL) (net.Listener, error) {
switch parsed.Scheme {
case "vsock":
port, err := strconv.Atoi(parsed.Port())
Expand All @@ -31,12 +27,8 @@ func Listen(endpoint string) (net.Listener, error) {
Name: path,
Net: "unix",
})
case "unix":
return net.Listen("unix", parsed.Path)
case "tcp":
return net.Listen("tcp", parsed.Host)
default:
return nil, errors.New("unexpected scheme")
return defaultListenURL(parsed)
}
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/transport/listen_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !darwin && !linux && !windows
// +build !darwin,!linux,!windows

package transport

import (
"net"
"net/url"
)

func listenURL(url *url.URL) (net.Listener, error) {
return defaultListenURL(url)
}
27 changes: 3 additions & 24 deletions pkg/transport/listen_linux.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package transport

import (
"errors"
"net"
"net/url"
"strconv"
Expand All @@ -11,37 +10,17 @@ import (

const DefaultURL = "vsock://:1024"

func Listen(endpoint string) (net.Listener, error) {
parsed, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
func listenURL(parsed *url.URL) (net.Listener, error) {
switch parsed.Scheme {
case "vsock":
port, err := strconv.Atoi(parsed.Port())
if err != nil {
return nil, err
}
return mdlayhervsock.Listen(uint32(port), nil)
case "unix", "unixpacket":
case "unixpacket":
return net.Listen(parsed.Scheme, parsed.Path)
case "tcp":
return net.Listen("tcp", parsed.Host)
default:
return nil, errors.New("unexpected scheme")
}
}

func ListenUnixgram(endpoint string) (*net.UnixConn, error) {
parsed, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
if parsed.Scheme != "unixgram" {
return nil, errors.New("unexpected scheme")
return defaultListenURL(parsed)
}
return net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: parsed.Path,
Net: "unixgram",
})
}
21 changes: 2 additions & 19 deletions pkg/transport/listen_windows.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package transport

import (
"errors"
"net"
"net/url"

Expand All @@ -10,11 +9,7 @@ import (

const DefaultURL = "vsock://00000400-FACB-11E6-BD58-64006A7986D3"

func Listen(endpoint string) (net.Listener, error) {
parsed, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
func listenURL(parsed *url.URL) (net.Listener, error) {
switch parsed.Scheme {
case "vsock":
svcid, err := hvsock.GUIDFromString(parsed.Hostname())
Expand All @@ -25,19 +20,7 @@ func Listen(endpoint string) (net.Listener, error) {
VMID: hvsock.GUIDWildcard,
ServiceID: svcid,
})
case "unix":
return net.Listen(parsed.Scheme, parsed.Path)
case "tcp":
return net.Listen("tcp", parsed.Host)
default:
return nil, errors.New("unexpected scheme")
return defaultListenURL(parsed)
}
}

func ListenUnixgram(endpoint string) (net.Conn, error) {
return nil, errors.New("unsupported 'unixgram' scheme")
}

func AcceptVfkit(listeningConn net.Conn) (net.Conn, error) {
return nil, errors.New("vfkit is unsupported on Windows")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !windows
// +build !windows
//go:build darwin
// +build darwin

package transport

Expand Down
17 changes: 17 additions & 0 deletions pkg/transport/unixgram_nondarwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !darwin
// +build !darwin

package transport

import (
"errors"
"net"
)

func ListenUnixgram(_ string) (net.Conn, error) {
return nil, errors.New("unsupported 'unixgram' scheme")
}

func AcceptVfkit(_ net.Conn) (net.Conn, error) {
return nil, errors.New("vfkit is unsupported on this platform")
}

0 comments on commit 21e3438

Please sign in to comment.