Skip to content
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

[WIP] add the "unknown" multiaddr #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"strings"

multibase "github.com/multiformats/go-multibase"
)

func stringToBytes(s string) ([]byte, error) {
Expand All @@ -22,6 +24,18 @@ func stringToBytes(s string) ([]byte, error) {
sp = sp[1:]

for len(sp) > 0 {
if sp[0] == "unknown" {
if len(sp) < 2 {
return nil, fmt.Errorf("unknown protocol requires an argument")
}
_, bts, err := multibase.Decode(sp[1])
if err != nil {
return nil, err
}
b.Write(bts)
sp = sp[2:]
continue
}
p := ProtocolWithName(sp[0])
if p.Code == 0 {
return nil, fmt.Errorf("no protocol with name %s", sp[0])
Expand Down Expand Up @@ -70,7 +84,7 @@ func validateBytes(b []byte) (err error) {
b = b[n:]
p := ProtocolWithCode(code)
if p.Code == 0 {
return fmt.Errorf("no protocol with code %d", code)
return nil
}

if p.Size == 0 {
Expand Down Expand Up @@ -108,11 +122,16 @@ func bytesToString(b []byte) (ret string, err error) {
return "", err
}

b = b[n:]
p := ProtocolWithCode(code)
if p.Code == 0 {
return "", fmt.Errorf("no protocol with code %d", code)
encoded, err := multibase.Encode(multibase.Base32, b)
if err != nil {
panic(err)
}
return s + "/unknown/" + encoded, nil
}
b = b[n:]

s += "/" + p.Name

if p.Size == 0 {
Expand Down Expand Up @@ -173,8 +192,10 @@ func bytesSplit(b []byte) ([][]byte, error) {
}

p := ProtocolWithCode(code)
// Unknown protocol. Finish.
if p.Code == 0 {
return nil, fmt.Errorf("no protocol with code %d", b[0])
ret = append(ret, b)
break
}

n2, size, err := sizeForAddr(p, b[n:])
Expand Down
13 changes: 9 additions & 4 deletions multiaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func (m *multiaddr) String() string {
}

// Protocols returns the list of protocols this Multiaddr has.
// will panic in case we access bytes incorrectly.
//
// Will end with an "unknown" protocol if we hit any protocol codes that we
// don't understand.
func (m *multiaddr) Protocols() []Protocol {
ps := make([]Protocol, 0, 8)
b := m.bytes
Expand All @@ -79,9 +81,12 @@ func (m *multiaddr) Protocols() []Protocol {

p := ProtocolWithCode(code)
if p.Code == 0 {
// this is a panic (and not returning err) because this should've been
// caught on constructing the Multiaddr
panic(fmt.Errorf("no protocol with code %d", b[0]))
ps = append(ps, Protocol{
Name: "unknown",
Size: -1,
Code: -1,
})
return ps
}
ps = append(ps, p)
b = b[n:]
Expand Down
2 changes: 2 additions & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Protocol struct {
Name string

// Code is the protocol's multicodec (a normal, non-varint number).
//
// -1 is reserved for the special "unknown" protocol.

Choose a reason for hiding this comment

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

This comment seems to be inconsistent with the rest of the code. e.g.:

ps = append(ps, Protocol{
  Name: "unknown",
  Size: -1,
  Code: 0,
})

IMO, -1 for unknown protocol fells better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I changed my mind multiple times half-way through. I agree.

Code int

// VCode is a precomputed varint encoded version of Code.
Expand Down
2 changes: 2 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package multiaddr
import "fmt"

// Split returns the sub-address portions of a multiaddr.
//
// Split stops when it hits a protocol that it doesn't understand.
func Split(m Multiaddr) []Multiaddr {
split, err := bytesSplit(m.Bytes())
if err != nil {
Expand Down