Skip to content

Add listen to BSDs #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions pkg/transport/listen_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//go:build freebsd || netbsd || openbsd || dragonfly
// +build freebsd netbsd openbsd dragonfly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

darwin.go can be merged here too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it in different files just in case there was something BSD-y required that's not present in Darwin. I can't try all BSDs tho.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it would be easy enough to duplicate this file when BSD-specific support would be needed? I'd prefer to only have //go:build darwin || dragonfly || freebsd || netbsd || openbsd for now, instead of adding more duplicate listen code.
If you duplicate the code, I'd remove the case "vsock" block which is hyperkit specific, and ListenUnixgram is also not expected to be used as it's vfkit-specific, so this could error out.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good reason to have two, one for darwin and one for bsd

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the BSD files are different from the darwin files, yes, this is a good reason for having 2 files. As the PR is right now, the files are the same.


package transport

import (
"errors"
"fmt"
"net"
"net/url"
"os"
"path"
"strconv"
)

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
}
switch parsed.Scheme {
case "vsock":
port, err := strconv.Atoi(parsed.Port())
if err != nil {
return nil, err
}
path := path.Join(parsed.Path, fmt.Sprintf("00000002.%08x", port))
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
return nil, err
}
return net.ListenUnix("unix", &net.UnixAddr{
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")
}
}

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 net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: parsed.Path,
Net: "unixgram",
})
}