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
b9067889
Commit
b9067889
authored
Sep 11, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go lint + unexporting some funcs
parent
6ee313e6
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
12 deletions
+24
-12
codec.go
codec.go
+6
-6
index.go
index.go
+12
-3
multiaddr_test.go
multiaddr_test.go
+2
-2
protocols.go
protocols.go
+4
-1
No files found.
codec.go
View file @
b9067889
...
...
@@ -8,7 +8,7 @@ import (
"strings"
)
func
S
tringToBytes
(
s
string
)
([]
byte
,
error
)
{
func
s
tringToBytes
(
s
string
)
([]
byte
,
error
)
{
b
:=
[]
byte
{}
sp
:=
strings
.
Split
(
s
,
"/"
)
...
...
@@ -26,7 +26,7 @@ func StringToBytes(s string) ([]byte, error) {
}
b
=
append
(
b
,
byte
(
p
.
Code
))
a
:=
A
ddressStringToBytes
(
p
,
sp
[
1
])
a
:=
a
ddressStringToBytes
(
p
,
sp
[
1
])
b
=
append
(
b
,
a
...
)
sp
=
sp
[
2
:
]
...
...
@@ -34,7 +34,7 @@ func StringToBytes(s string) ([]byte, error) {
return
b
,
nil
}
func
B
ytesToString
(
b
[]
byte
)
(
ret
string
,
err
error
)
{
func
b
ytesToString
(
b
[]
byte
)
(
ret
string
,
err
error
)
{
// panic handler, in case we try accessing bytes incorrectly.
defer
func
()
{
if
e
:=
recover
();
e
!=
nil
{
...
...
@@ -53,7 +53,7 @@ func BytesToString(b []byte) (ret string, err error) {
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
p
.
Name
},
""
)
b
=
b
[
1
:
]
a
:=
A
ddressBytesToString
(
p
,
b
[
:
(
p
.
Size
/
8
)])
a
:=
a
ddressBytesToString
(
p
,
b
[
:
(
p
.
Size
/
8
)])
if
len
(
a
)
>
0
{
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
a
},
""
)
}
...
...
@@ -63,7 +63,7 @@ func BytesToString(b []byte) (ret string, err error) {
return
s
,
nil
}
func
A
ddressStringToBytes
(
p
*
Protocol
,
s
string
)
[]
byte
{
func
a
ddressStringToBytes
(
p
*
Protocol
,
s
string
)
[]
byte
{
switch
p
.
Code
{
// ipv4,6
...
...
@@ -83,7 +83,7 @@ func AddressStringToBytes(p *Protocol, s string) []byte {
return
[]
byte
{}
}
func
A
ddressBytesToString
(
p
*
Protocol
,
b
[]
byte
)
string
{
func
a
ddressBytesToString
(
p
*
Protocol
,
b
[]
byte
)
string
{
switch
p
.
Code
{
// ipv4,6
...
...
index.go
View file @
b9067889
...
...
@@ -5,22 +5,26 @@ import (
"strings"
)
// Multiaddr is the data structure representing a multiaddr
type
Multiaddr
struct
{
Bytes
[]
byte
}
// NewMultiaddr parses and validates an input string, returning a *Multiaddr
func
NewMultiaddr
(
s
string
)
(
*
Multiaddr
,
error
)
{
b
,
err
:=
S
tringToBytes
(
s
)
b
,
err
:=
s
tringToBytes
(
s
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
Multiaddr
{
Bytes
:
b
},
nil
}
// String returns the string representation of a Multiaddr
func
(
m
*
Multiaddr
)
String
()
(
string
,
error
)
{
return
B
ytesToString
(
m
.
Bytes
)
return
b
ytesToString
(
m
.
Bytes
)
}
// Protocols returns the list of protocols this Multiaddr has.
func
(
m
*
Multiaddr
)
Protocols
()
(
ret
[]
*
Protocol
,
err
error
)
{
// panic handler, in case we try accessing bytes incorrectly.
...
...
@@ -44,12 +48,14 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
return
ps
,
nil
}
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func
(
m
*
Multiaddr
)
Encapsulate
(
o
*
Multiaddr
)
*
Multiaddr
{
b
:=
make
([]
byte
,
len
(
m
.
Bytes
)
+
len
(
o
.
Bytes
))
b
=
append
(
m
.
Bytes
,
o
.
Bytes
...
)
return
&
Multiaddr
{
Bytes
:
b
}
}
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
func
(
m
*
Multiaddr
)
Decapsulate
(
o
*
Multiaddr
)
(
*
Multiaddr
,
error
)
{
s1
,
err
:=
m
.
String
()
if
err
!=
nil
{
...
...
@@ -68,9 +74,10 @@ func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error) {
return
NewMultiaddr
(
s1
[
:
i
])
}
// DialArgs is a convenience function returning arguments for use in net.Dial
func
(
m
*
Multiaddr
)
DialArgs
()
(
string
,
string
,
error
)
{
if
!
m
.
IsThinWaist
()
{
return
""
,
""
,
fmt
.
Errorf
(
"%s is not a 'thin waist' address
.
"
,
m
)
return
""
,
""
,
fmt
.
Errorf
(
"%s is not a 'thin waist' address"
,
m
)
}
str
,
err
:=
m
.
String
()
...
...
@@ -84,6 +91,8 @@ func (m *Multiaddr) DialArgs() (string, string, error) {
return
network
,
host
,
nil
}
// IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols.
// This means: /{IP4, IP6}/{TCP, UDP}
func
(
m
*
Multiaddr
)
IsThinWaist
()
bool
{
p
,
err
:=
m
.
Protocols
()
if
err
!=
nil
{
...
...
multiaddr_test.go
View file @
b9067889
...
...
@@ -14,7 +14,7 @@ func TestStringToBytes(t *testing.T) {
t
.
Error
(
"failed to decode hex"
,
h
)
}
b2
,
err
:=
S
tringToBytes
(
s
)
b2
,
err
:=
s
tringToBytes
(
s
)
if
err
!=
nil
{
t
.
Error
(
"failed to convert"
,
s
)
}
...
...
@@ -35,7 +35,7 @@ func TestBytesToString(t *testing.T) {
t
.
Error
(
"failed to decode hex"
,
h
)
}
s2
,
err
:=
B
ytesToString
(
b
)
s2
,
err
:=
b
ytesToString
(
b
)
if
err
!=
nil
{
t
.
Error
(
"failed to convert"
,
b
)
}
...
...
protocols.go
View file @
b9067889
package
multiaddr
// Protocol is a Multiaddr protocol description structure.
type
Protocol
struct
{
Code
int
Size
int
...
...
@@ -10,7 +11,6 @@ type Protocol struct {
// 1. avoid parsing the csv
// 2. ensuring errors in the csv don't screw up code.
// 3. changing a number has to happen in two places.
const
(
P_IP4
=
4
P_TCP
=
6
...
...
@@ -20,6 +20,7 @@ const (
P_SCTP
=
132
)
// Protocols is the list of multiaddr protocols supported by this module.
var
Protocols
=
[]
*
Protocol
{
&
Protocol
{
P_IP4
,
32
,
"ip4"
},
&
Protocol
{
P_TCP
,
16
,
"tcp"
},
...
...
@@ -32,6 +33,7 @@ var Protocols = []*Protocol{
// {443, 0, "https"},
}
// ProtocolWithName returns the Protocol description with given string name.
func
ProtocolWithName
(
s
string
)
*
Protocol
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Name
==
s
{
...
...
@@ -41,6 +43,7 @@ func ProtocolWithName(s string) *Protocol {
return
nil
}
// ProtocolWithCode returns the Protocol description with given protocol code.
func
ProtocolWithCode
(
c
int
)
*
Protocol
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Code
==
c
{
...
...
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