Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
17f4666d
Commit
17f4666d
authored
Jan 09, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Protocols now value
parent
ed277d56
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
27 additions
and
27 deletions
+27
-27
README.md
README.md
+3
-3
codec.go
codec.go
+5
-5
interface.go
interface.go
+1
-1
multiaddr.go
multiaddr.go
+3
-3
multiaddr_test.go
multiaddr_test.go
+2
-2
protocols.go
protocols.go
+13
-13
No files found.
README.md
View file @
17f4666d
...
...
@@ -28,9 +28,9 @@ m2.Equal(m1)
```
go
// get the multiaddr protocol description objects
addr
.
Protocols
()
// []
*
Protocol{
//
&
Protocol{ Code: 4, Name: 'ip4', Size: 32},
//
&
Protocol{ Code: 17, Name: 'udp', Size: 16},
// []Protocol{
// Protocol{ Code: 4, Name: 'ip4', Size: 32},
// Protocol{ Code: 17, Name: 'udp', Size: 16},
// }
```
...
...
codec.go
View file @
17f4666d
...
...
@@ -25,7 +25,7 @@ func stringToBytes(s string) ([]byte, error) {
for
len
(
sp
)
>
0
{
p
:=
ProtocolWithName
(
sp
[
0
])
if
p
==
nil
{
if
p
.
Code
==
0
{
return
nil
,
fmt
.
Errorf
(
"no protocol with name %s"
,
sp
[
0
])
}
b
=
append
(
b
,
CodeToVarint
(
p
.
Code
)
...
)
...
...
@@ -62,7 +62,7 @@ func bytesToString(b []byte) (ret string, err error) {
code
,
n
:=
ReadVarintCode
(
b
)
b
=
b
[
n
:
]
p
:=
ProtocolWithCode
(
code
)
if
p
==
nil
{
if
p
.
Code
==
0
{
return
""
,
fmt
.
Errorf
(
"no protocol with code %d"
,
code
)
}
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
p
.
Name
},
""
)
...
...
@@ -92,7 +92,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) {
for
len
(
b
)
>
0
{
code
,
n
:=
ReadVarintCode
(
b
)
p
:=
ProtocolWithCode
(
code
)
if
p
==
nil
{
if
p
.
Code
==
0
{
return
[][]
byte
{},
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
}
...
...
@@ -104,7 +104,7 @@ func bytesSplit(b []byte) (ret [][]byte, err error) {
return
ret
,
nil
}
func
addressStringToBytes
(
p
*
Protocol
,
s
string
)
([]
byte
,
error
)
{
func
addressStringToBytes
(
p
Protocol
,
s
string
)
([]
byte
,
error
)
{
switch
p
.
Code
{
case
P_IP4
:
// ipv4
...
...
@@ -138,7 +138,7 @@ func addressStringToBytes(p *Protocol, s string) ([]byte, error) {
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
{
switch
p
.
Code
{
// ipv4,6
...
...
interface.go
View file @
17f4666d
...
...
@@ -26,7 +26,7 @@ type Multiaddr interface {
// Protocols returns the list of Protocols this Multiaddr includes
// will panic if protocol code incorrect (and bytes accessed incorrectly)
Protocols
()
[]
*
Protocol
Protocols
()
[]
Protocol
// Encapsulate wraps this Multiaddr around another. For example:
//
...
...
multiaddr.go
View file @
17f4666d
...
...
@@ -54,7 +54,7 @@ func (m *multiaddr) String() string {
// Protocols returns the list of protocols this Multiaddr has.
// will panic in case we access bytes incorrectly.
func
(
m
*
multiaddr
)
Protocols
()
[]
*
Protocol
{
func
(
m
*
multiaddr
)
Protocols
()
[]
Protocol
{
// panic handler, in case we try accessing bytes incorrectly.
defer
func
()
{
...
...
@@ -64,12 +64,12 @@ func (m *multiaddr) Protocols() []*Protocol {
}
}()
ps
:=
[]
*
Protocol
{}
ps
:=
[]
Protocol
{}
b
:=
m
.
bytes
[
:
]
for
len
(
b
)
>
0
{
code
,
n
:=
ReadVarintCode
(
b
)
p
:=
ProtocolWithCode
(
code
)
if
p
==
nil
{
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
]))
...
...
multiaddr_test.go
View file @
17f4666d
...
...
@@ -204,12 +204,12 @@ func TestProtocols(t *testing.T) {
}
ps
:=
m
.
Protocols
()
if
ps
[
0
]
!=
ProtocolWithName
(
"ip4"
)
{
if
ps
[
0
]
.
Code
!=
ProtocolWithName
(
"ip4"
)
.
Code
{
t
.
Error
(
ps
[
0
],
ProtocolWithName
(
"ip4"
))
t
.
Error
(
"failed to get ip4 protocol"
)
}
if
ps
[
1
]
!=
ProtocolWithName
(
"udp"
)
{
if
ps
[
1
]
.
Code
!=
ProtocolWithName
(
"udp"
)
.
Code
{
t
.
Error
(
ps
[
1
],
ProtocolWithName
(
"udp"
))
t
.
Error
(
"failed to get udp protocol"
)
}
...
...
protocols.go
View file @
17f4666d
...
...
@@ -28,38 +28,38 @@ const (
)
// Protocols is the list of multiaddr protocols supported by this module.
var
Protocols
=
[]
*
Protocol
{
&
Protocol
{
P_IP4
,
32
,
"ip4"
,
CodeToVarint
(
P_IP4
)},
&
Protocol
{
P_TCP
,
16
,
"tcp"
,
CodeToVarint
(
P_TCP
)},
&
Protocol
{
P_UDP
,
16
,
"udp"
,
CodeToVarint
(
P_UDP
)},
&
Protocol
{
P_DCCP
,
16
,
"dccp"
,
CodeToVarint
(
P_DCCP
)},
&
Protocol
{
P_IP6
,
128
,
"ip6"
,
CodeToVarint
(
P_IP6
)},
var
Protocols
=
[]
Protocol
{
Protocol
{
P_IP4
,
32
,
"ip4"
,
CodeToVarint
(
P_IP4
)},
Protocol
{
P_TCP
,
16
,
"tcp"
,
CodeToVarint
(
P_TCP
)},
Protocol
{
P_UDP
,
16
,
"udp"
,
CodeToVarint
(
P_UDP
)},
Protocol
{
P_DCCP
,
16
,
"dccp"
,
CodeToVarint
(
P_DCCP
)},
Protocol
{
P_IP6
,
128
,
"ip6"
,
CodeToVarint
(
P_IP6
)},
// these require varint:
&
Protocol
{
P_SCTP
,
16
,
"sctp"
,
CodeToVarint
(
P_SCTP
)},
&
Protocol
{
P_UTP
,
0
,
"utp"
,
CodeToVarint
(
P_UTP
)},
&
Protocol
{
P_UDT
,
0
,
"udt"
,
CodeToVarint
(
P_UDT
)},
Protocol
{
P_SCTP
,
16
,
"sctp"
,
CodeToVarint
(
P_SCTP
)},
Protocol
{
P_UTP
,
0
,
"utp"
,
CodeToVarint
(
P_UTP
)},
Protocol
{
P_UDT
,
0
,
"udt"
,
CodeToVarint
(
P_UDT
)},
// {480, 0, "http"},
// {443, 0, "https"},
}
// ProtocolWithName returns the Protocol description with given string name.
func
ProtocolWithName
(
s
string
)
*
Protocol
{
func
ProtocolWithName
(
s
string
)
Protocol
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Name
==
s
{
return
p
}
}
return
nil
return
Protocol
{}
}
// ProtocolWithCode returns the Protocol description with given protocol code.
func
ProtocolWithCode
(
c
int
)
*
Protocol
{
func
ProtocolWithCode
(
c
int
)
Protocol
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Code
==
c
{
return
p
}
}
return
nil
return
Protocol
{}
}
// CodeToVarint converts an integer to a varint-encoded []byte
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment