Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
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
20 changes: 18 additions & 2 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net"
"path/filepath"
"runtime"
"strings"

ma "github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -192,6 +194,10 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
}
return network, "[" + ip + "]" + ":" + port, nil
case "unix":
if runtime.GOOS == "windows" {
// convert /c:/... to c:\...
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
}
return network, ip, nil
default:
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
Expand Down Expand Up @@ -263,6 +269,16 @@ func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) {
if !ok {
return nil, errIncorrectNetAddr
}
cleaned := filepath.Clean(ac.Name)
return ma.NewComponent("unix", cleaned)

path := ac.Name
if runtime.GOOS == "windows" {
// Convert c:\foobar\... to c:/foobar/...
path = filepath.ToSlash(path)
}
if len(path) == 0 || path[0] != '/' {
// convert "" and "c:/..." to "/..."
path = "/" + path
}

return ma.NewComponent("unix", path)
}
19 changes: 19 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manet

import (
"net"
"runtime"
"testing"

ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -64,6 +65,24 @@ func TestFromIP4(t *testing.T) {
})
}

func TestFromUnix(t *testing.T) {
path := "/C:/foo/bar"
if runtime.GOOS == "windows" {
path = `C:\foo\bar`
}
testConvert(t, "/unix/C:/foo/bar", func() (ma.Multiaddr, error) {
return FromNetAddr(&net.UnixAddr{Name: path, Net: "unix"})
})
}

func TestToUnix(t *testing.T) {
path := "/C:/foo/bar"
if runtime.GOOS == "windows" {
path = `C:\foo\bar`
}
testToNetAddr(t, "/unix/C:/foo/bar", "unix", path)
}

func TestFromIP6(t *testing.T) {
testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) {
return FromNetAddr(&net.IPAddr{IP: net.ParseIP("2001:4860:0:2001::68")})
Expand Down