Implemented byte decoder for onion addresses. Corrected onion bit size.

This commit is contained in:
Ian Preston 2016-04-11 01:17:18 +01:00
parent f3dff105e4
commit e32e9cd0f0
4 changed files with 10 additions and 3 deletions

View File

@ -276,6 +276,12 @@ func addressBytesToString(p Protocol, b []byte) (string, error) {
return "", err
}
return m.B58String(), nil
case P_ONION:
addr := strings.ToLower(base32.StdEncoding.EncodeToString(b[0:10]))
port := binary.BigEndian.Uint16(b[10:12])
return addr + ":"+ strconv.Itoa(int(port)), nil
default:
return "", fmt.Errorf("unknown protocol")
}

View File

@ -166,7 +166,7 @@ func TestBytesToString(t *testing.T) {
s2, err := bytesToString(b)
if err != nil {
t.Error("failed to convert", b)
t.Error("failed to convert", b, err)
}
if s1 != s2 {
@ -177,6 +177,7 @@ func TestBytesToString(t *testing.T) {
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1")
testString("/onion/aaimaq4ygg2iegci:80", "bc030010c0439831b48218480050")
}
func TestBytesSplitAndJoin(t *testing.T) {

View File

@ -10,4 +10,4 @@ code size name
421 V ipfs
480 0 http
443 0 https
444 10 onion
444 96 onion
1 code size name
10 421 V ipfs
11 480 0 http
12 443 0 https
13 444 10 96 onion

View File

@ -47,7 +47,7 @@ var Protocols = []Protocol{
Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)},
// these require varint:
Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
Protocol{P_ONION, 80, "onion", CodeToVarint(P_ONION)},
Protocol{P_ONION, 96, "onion", CodeToVarint(P_ONION)},
Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP)},