Skip to content

Commit

Permalink
correctly format ip4-in-6 addresses
Browse files Browse the repository at this point in the history
fixes part one of #77
  • Loading branch information
Stebalien committed Sep 30, 2018
1 parent a80cfc3 commit 41068aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ func TestBinaryRepresentation(t *testing.T) {
func TestRoundTrip(t *testing.T) {
for _, s := range []string{
"/unix/a/b/c/d",
"/ip6/::ffff:127.0.0.1/tcp/111",
"/ip4/127.0.0.1/tcp/123",
"/ip4/127.0.0.1/udp/123",
"/ip4/127.0.0.1/udp/123/ip6/::",
Expand Down
15 changes: 12 additions & 3 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (t twrp) ValidateBytes(b []byte) error {
return t.validbyte(b)
}

var TranscoderIP4 = NewTranscoderFromFunctions(ip4StB, ipBtS, nil)
var TranscoderIP6 = NewTranscoderFromFunctions(ip6StB, ipBtS, nil)
var TranscoderIP4 = NewTranscoderFromFunctions(ip4StB, ip4BtS, nil)
var TranscoderIP6 = NewTranscoderFromFunctions(ip6StB, ip6BtS, nil)
var TranscoderIP6Zone = NewTranscoderFromFunctions(ip6zoneStB, ip6zoneBtS, ip6zoneVal)

func ip4StB(s string) ([]byte, error) {
Expand Down Expand Up @@ -88,7 +88,16 @@ func ip6StB(s string) ([]byte, error) {
return i, nil
}

func ipBtS(b []byte) (string, error) {
func ip6BtS(b []byte) (string, error) {
ip := net.IP(b)
if ip4 := ip.To4(); ip4 != nil {
// Go fails to prepend the `::ffff:` part.
return "::ffff:" + ip4.String(), nil
}
return ip.String(), nil
}

func ip4BtS(b []byte) (string, error) {
return net.IP(b).String(), nil
}

Expand Down

0 comments on commit 41068aa

Please sign in to comment.