Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func TestConstructFails(t *testing.T) {
"/",
"",
"/p2p/QmxoHT6iViN5xAjoz1VZ553cL31U9F94ht3QvWR1FrEbZY", // sha256 multihash with digest len > 32
"/scion",
"/scion//udp/1234",
"/scion/0-",
"/scion/1234",
}

for _, a := range cases {
Expand Down Expand Up @@ -266,6 +270,10 @@ var good = []string{
"/ip4/127.0.0.1/tcp/0/p2p/12D3KooWCryG7Mon9orvQxcS1rYZjotPgpwoJNHHKcLLfE4Hf5mV/http-path/foo",
"/ip4/127.0.0.1/tcp/443/tls/sni/example.com/http/http-path/foo",
"/memory/4",
"/scion/0-0",
"/scion/1-ff00:0:110",
"/scion/1-ff00:0:110/ip4/1.2.3.4",
"/scion/1-ff00:0:110/ip6/::ffff:127.0.0.1/udp/111",
}

func TestConstructSucceeds(t *testing.T) {
Expand Down Expand Up @@ -542,6 +550,13 @@ func TestEncapsulate(t *testing.T) {
joined = (*first).Encapsulate(rest)
require.True(t, joined.Equal(m))
})

// SCION Multiaddr
m5, _ := NewMultiaddr("/scion/1-ff00:0:110")
e := m5.Encapsulate(m)
if s := e.String(); s != "/scion/1-ff00:0:110/ip4/127.0.0.1/udp/1234" {
t.Error("encapsulate /scion/1-ff00:0:110/ip4/127.0.0.1/udp/1234 failed.", s)
}
}

func TestDecapsulateComment(t *testing.T) {
Expand Down Expand Up @@ -671,6 +686,11 @@ func TestGetValue(t *testing.T) {
a = newMultiaddr(t, "/ip4/0.0.0.0/unix/a/b/c/d") // ending in a path one.
assertValueForProto(t, a, P_IP4, "0.0.0.0")
assertValueForProto(t, a, P_UNIX, "/a/b/c/d")

a = newMultiaddr(t, "/scion/1-ff00:0:110/ip4/127.0.0.1/udp/1234")
assertValueForProto(t, a, P_SCION, "1-ff00:0:110")
assertValueForProto(t, a, P_IP4, "127.0.0.1")
assertValueForProto(t, a, P_UDP, "1234")
}

func FuzzNewMultiaddrBytes(f *testing.F) {
Expand Down Expand Up @@ -758,6 +778,7 @@ func TestRoundTrip(t *testing.T) {
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP",
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP/unix/a/b/c",
"/http-path/tmp%2Fbar",
"/scion/1-ff00:0:110/ip6/::ffff:127.0.0.1/tcp/111",
} {
ma, err := NewMultiaddr(s)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
P_WEBRTC_DIRECT = 280
P_WEBRTC = 281
P_MEMORY = 777
P_SCION = 13639680
)

var (
Expand Down Expand Up @@ -290,6 +291,14 @@ var (
Size: 64,
Transcoder: TranscoderMemory,
}
protoSCION = Protocol{
Name: "scion",
Code: P_SCION,
VCode: CodeToVarint(P_SCION),
Size: LengthPrefixedVarSize,
Path: false,
Transcoder: TranscoderSCION,
}
)

func init() {
Expand Down Expand Up @@ -332,6 +341,7 @@ func init() {
protoWebRTCDirect,
protoWebRTC,
protoMemory,
protoSCION,
} {
if err := AddProtocol(p); err != nil {
panic(err)
Expand Down
29 changes: 29 additions & 0 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,32 @@ func memoryValidate(b []byte) error {

return nil
}
var TranscoderSCION = NewTranscoderFromFunctions(scionStB, scionBtS, scionVal)

func scionVal(b []byte) error {
// SCION IA is 16 bit ISD and 48 bit AS numbers separated by "-"
// ISD numbers formatted as decimal, AS numbering similar to IPv6
// E.g.: "0-0" or "1234-ff00:0:110"
if len(b) < 3 {
return fmt.Errorf("byte slice too short: %d", len(b))
}
if minus := bytes.IndexByte(b, '-'); minus < 0 {
return errors.New("scion addresses must contain '-'")
}
return nil
}

func scionStB(s string) ([]byte, error) {
b := []byte(s)
if err := scionVal(b); err != nil {
return nil, err
}
return b, nil
}

func scionBtS(b []byte) (string, error) {
if err := scionVal(b); err != nil {
return "", err
}
return string(b), nil
}