added http, https, and ipfs protocols

This commit is contained in:
Juan Batiz-Benet 2015-01-17 21:12:54 -08:00
parent 0d7b54ba43
commit f529ec7884
4 changed files with 76 additions and 33 deletions

View File

@ -6,6 +6,8 @@ import (
"net" "net"
"strconv" "strconv"
"strings" "strings"
mh "github.com/jbenet/go-multihash"
) )
func stringToBytes(s string) ([]byte, error) { func stringToBytes(s string) ([]byte, error) {
@ -31,17 +33,19 @@ func stringToBytes(s string) ([]byte, error) {
b = append(b, CodeToVarint(p.Code)...) b = append(b, CodeToVarint(p.Code)...)
sp = sp[1:] sp = sp[1:]
if p.Size > 0 { if p.Size == 0 { // no length.
if len(sp) < 1 { continue
return nil, fmt.Errorf("protocol requires address, none given: %s", p.Name)
}
a, err := addressStringToBytes(p, sp[0])
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err)
}
b = append(b, a...)
sp = sp[1:]
} }
if len(sp) < 1 {
return nil, fmt.Errorf("protocol requires address, none given: %s", p.Name)
}
a, err := addressStringToBytes(p, sp[0])
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err)
}
b = append(b, a...)
sp = sp[1:]
} }
return b, nil return b, nil
} }
@ -67,13 +71,18 @@ func bytesToString(b []byte) (ret string, err error) {
} }
s = strings.Join([]string{s, "/", p.Name}, "") s = strings.Join([]string{s, "/", p.Name}, "")
if p.Size > 0 { if p.Size == 0 {
a := addressBytesToString(p, b[:(p.Size/8)]) continue
if len(a) > 0 {
s = strings.Join([]string{s, "/", a}, "")
}
b = b[(p.Size / 8):]
} }
a, err := addressBytesToString(p, b[:(p.Size/8)])
if err != nil {
return "", err
}
if len(a) > 0 {
s = strings.Join([]string{s, "/", a}, "")
}
b = b[(p.Size / 8):]
} }
return s, nil return s, nil
@ -133,23 +142,39 @@ func addressStringToBytes(p Protocol, s string) ([]byte, error) {
b := make([]byte, 2) b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(i)) binary.BigEndian.PutUint16(b, uint16(i))
return b, nil return b, nil
case P_IPFS: // ipfs
// the address is a multihash string representation
m, err := mh.FromB58String(s)
if err != nil {
return nil, fmt.Errorf("failed to parse ipfs addr: %s %s", s, err)
}
return []byte(m), nil
} }
return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name) return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name)
} }
func addressBytesToString(p Protocol, b []byte) string { func addressBytesToString(p Protocol, b []byte) (string, error) {
switch p.Code { switch p.Code {
// ipv4,6 // ipv4,6
case P_IP4, P_IP6: case P_IP4, P_IP6:
return net.IP(b).String() return net.IP(b).String(), nil
// tcp udp dccp sctp // tcp udp dccp sctp
case P_TCP, P_UDP, P_DCCP, P_SCTP: case P_TCP, P_UDP, P_DCCP, P_SCTP:
i := binary.BigEndian.Uint16(b) i := binary.BigEndian.Uint16(b)
return strconv.Itoa(int(i)) return strconv.Itoa(int(i)), nil
case P_IPFS: // ipfs
// the address is a multihash string representation
m, err := mh.Cast(b)
if err != nil {
return "", err
}
return m.B58String(), nil
} }
return "" return "", fmt.Errorf("unknown protocol")
} }

View File

@ -32,11 +32,13 @@ func TestConstructFails(t *testing.T) {
"/ip4/127.0.0.1/udp", "/ip4/127.0.0.1/udp",
"/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa", "/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa",
"/ip4/127.0.0.1/tcp", "/ip4/127.0.0.1/tcp",
"/ip4/127.0.0.1/ipfs",
"/ip4/127.0.0.1/ipfs/tcp",
} }
for _, a := range cases { for _, a := range cases {
if _, err := NewMultiaddr(a); err == nil { if _, err := NewMultiaddr(a); err == nil {
t.Errorf("should have failed: %s", a) t.Errorf("should have failed: %s - %s", a, err)
} }
} }
} }
@ -55,18 +57,24 @@ func TestConstructSucceeds(t *testing.T) {
"/sctp/1234", "/sctp/1234",
"/udp/65535", "/udp/65535",
"/tcp/65535", "/tcp/65535",
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/udp/1234/sctp/1234", "/udp/1234/sctp/1234",
"/udp/1234/udt", "/udp/1234/udt",
"/udp/1234/utp", "/udp/1234/utp",
"/tcp/1234/http",
"/tcp/1234/https",
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/ip4/127.0.0.1/udp/1234", "/ip4/127.0.0.1/udp/1234",
"/ip4/127.0.0.1/udp/0", "/ip4/127.0.0.1/udp/0",
"/ip4/127.0.0.1/tcp/1234", "/ip4/127.0.0.1/tcp/1234",
"/ip4/127.0.0.1/tcp/1234/", "/ip4/127.0.0.1/tcp/1234/",
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
} }
for _, a := range cases { for _, a := range cases {
if _, err := NewMultiaddr(a); err != nil { if _, err := NewMultiaddr(a); err != nil {
t.Errorf("should have succeeded: %s", a) t.Errorf("should have succeeded: %s -- %s", a, err)
} }
} }
} }

View File

@ -7,5 +7,6 @@ code size name
132 16 sctp 132 16 sctp
301 0 udt 301 0 udt
302 0 utp 302 0 utp
421 V ipfs
480 0 http 480 0 http
443 0 https 443 0 https

1 code size name
7 132 16 sctp
8 301 0 udt
9 302 0 utp
10 421 V ipfs
11 480 0 http
12 443 0 https

View File

@ -9,7 +9,7 @@ import (
// Protocol is a Multiaddr protocol description structure. // Protocol is a Multiaddr protocol description structure.
type Protocol struct { type Protocol struct {
Code int Code int
Size int Size int // a size of -1 indicates a length-prefixed variable size
Name string Name string
VCode []byte VCode []byte
} }
@ -19,14 +19,22 @@ type Protocol struct {
// 2. ensuring errors in the csv don't screw up code. // 2. ensuring errors in the csv don't screw up code.
// 3. changing a number has to happen in two places. // 3. changing a number has to happen in two places.
const ( const (
P_IP4 = 4 P_IP4 = 4
P_TCP = 6 P_TCP = 6
P_UDP = 17 P_UDP = 17
P_DCCP = 33 P_DCCP = 33
P_IP6 = 41 P_IP6 = 41
P_SCTP = 132 P_SCTP = 132
P_UTP = 301 P_UTP = 301
P_UDT = 302 P_UDT = 302
P_IPFS = 421
P_HTTP = 480
P_HTTPS = 443
)
// These are special sizes
const (
LengthPrefixedVarSize = -1
) )
// Protocols is the list of multiaddr protocols supported by this module. // Protocols is the list of multiaddr protocols supported by this module.
@ -40,8 +48,9 @@ var Protocols = []Protocol{
Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)}, Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)}, Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)}, Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
// {480, 0, "http"}, Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP)},
// {443, 0, "https"}, Protocol{P_HTTPS, 0, "https", CodeToVarint(P_HTTPS)},
Protocol{P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS)},
} }
// ProtocolWithName returns the Protocol description with given string name. // ProtocolWithName returns the Protocol description with given string name.