-
Notifications
You must be signed in to change notification settings - Fork 63
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
Closed
Add listen to BSDs #259
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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", | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 duplicatelisten
code.If you duplicate the code, I'd remove the
case "vsock"
block which is hyperkit specific, andListenUnixgram
is also not expected to be used as it's vfkit-specific, so this could error out.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.