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
3855a29d
Commit
3855a29d
authored
Jul 04, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
en/decapsulation
parent
e20c5b9d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
10 deletions
+72
-10
README.md
README.md
+6
-6
codec.go
codec.go
+1
-1
index.go
index.go
+26
-1
multiaddr_test.go
multiaddr_test.go
+39
-2
No files found.
README.md
View file @
3855a29d
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
```
go
```
go
import
"github.com/jbenet/go-multiaddr"
import
"github.com/jbenet/go-multiaddr"
m
:=
multiaddr
.
New
String
(
"/ip4/127.0.0.1/udp/1234"
)
m
:=
multiaddr
.
New
Multiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
// <Multiaddr /ip4/127.0.0.1/udp/1234>
// <Multiaddr /ip4/127.0.0.1/udp/1234>
m
.
buffer
m
.
buffer
// <Buffer >
// <Buffer >
...
@@ -45,9 +45,9 @@ m.Url(buf)
...
@@ -45,9 +45,9 @@ m.Url(buf)
### En/decapsulate
### En/decapsulate
```
go
```
go
m
.
Encapsulate
(
m
.
New
String
(
"/sctp/5678"
))
m
.
Encapsulate
(
m
.
New
Multiaddr
(
"/sctp/5678"
))
// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
m
.
Decapsulate
(
m
.
New
String
(
"/udp"
))
// up to + inc last occurrence of subaddr
m
.
Decapsulate
(
m
.
New
Multiaddr
(
"/udp"
))
// up to + inc last occurrence of subaddr
// <Multiaddr /ip4/127.0.0.1>
// <Multiaddr /ip4/127.0.0.1>
```
```
...
@@ -56,11 +56,11 @@ m.Decapsulate(m.NewString("/udp")) // up to + inc last occurrence of subaddr
...
@@ -56,11 +56,11 @@ m.Decapsulate(m.NewString("/udp")) // up to + inc last occurrence of subaddr
Multiaddr allows expressing tunnels very nicely.
Multiaddr allows expressing tunnels very nicely.
```
js
```
js
printer
:
=
multiaddr
.
New
String
(
"
/ip4/192.168.0.13/tcp/80
"
)
printer
:
=
multiaddr
.
New
Multiaddr
(
"
/ip4/192.168.0.13/tcp/80
"
)
proxy
:
=
multiaddr
.
New
String
(
"
/ip4/10.20.30.40/tcp/443
"
)
proxy
:
=
multiaddr
.
New
Multiaddr
(
"
/ip4/10.20.30.40/tcp/443
"
)
printerOverProxy
:
=
proxy
.
Encapsulate
(
printer
)
printerOverProxy
:
=
proxy
.
Encapsulate
(
printer
)
// <Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>
// <Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>
proxyAgain
:
=
printerOverProxy
.
Decapsulate
(
multiaddr
.
New
String
(
"
/ip4
"
))
proxyAgain
:
=
printerOverProxy
.
Decapsulate
(
multiaddr
.
New
Multiaddr
(
"
/ip4
"
))
// <Multiaddr /ip4/10.20.30.40/tcp/443>
// <Multiaddr /ip4/10.20.30.40/tcp/443>
```
```
codec.go
View file @
3855a29d
...
@@ -50,7 +50,7 @@ func BytesToString(b []byte) (ret string, err error) {
...
@@ -50,7 +50,7 @@ func BytesToString(b []byte) (ret string, err error) {
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
p
.
Name
},
""
)
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
p
.
Name
},
""
)
b
=
b
[
1
:
]
b
=
b
[
1
:
]
a
:=
AddressBytesToString
(
p
,
b
)
a
:=
AddressBytesToString
(
p
,
b
[
:
(
p
.
Size
/
8
)]
)
if
len
(
a
)
>
0
{
if
len
(
a
)
>
0
{
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
a
},
""
)
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
a
},
""
)
}
}
...
...
index.go
View file @
3855a29d
...
@@ -2,13 +2,14 @@ package multiaddr
...
@@ -2,13 +2,14 @@ package multiaddr
import
(
import
(
"fmt"
"fmt"
"strings"
)
)
type
Multiaddr
struct
{
type
Multiaddr
struct
{
Bytes
[]
byte
Bytes
[]
byte
}
}
func
New
String
(
s
string
)
(
*
Multiaddr
,
error
)
{
func
New
Multiaddr
(
s
string
)
(
*
Multiaddr
,
error
)
{
b
,
err
:=
StringToBytes
(
s
)
b
,
err
:=
StringToBytes
(
s
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -42,3 +43,27 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
...
@@ -42,3 +43,27 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
}
}
return
ps
,
nil
return
ps
,
nil
}
}
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
}
}
func
(
m
*
Multiaddr
)
Decapsulate
(
o
*
Multiaddr
)
(
*
Multiaddr
,
error
)
{
s1
,
err
:=
m
.
String
()
if
err
!=
nil
{
return
nil
,
err
}
s2
,
err
:=
o
.
String
()
if
err
!=
nil
{
return
nil
,
err
}
i
:=
strings
.
LastIndex
(
s1
,
s2
)
if
i
<
0
{
return
nil
,
fmt
.
Errorf
(
"%s not contained in %s"
,
s2
,
s1
)
}
return
NewMultiaddr
(
s1
[
:
i
])
}
multiaddr_test.go
View file @
3855a29d
...
@@ -41,7 +41,7 @@ func TestBytesToString(t *testing.T) {
...
@@ -41,7 +41,7 @@ func TestBytesToString(t *testing.T) {
t
.
Error
(
"failed to convert"
,
b
)
t
.
Error
(
"failed to convert"
,
b
)
}
}
if
s1
=
=
s2
{
if
s1
!
=
s2
{
t
.
Error
(
"failed to convert"
,
b
,
"to"
,
s1
,
"got"
,
s2
)
t
.
Error
(
"failed to convert"
,
b
,
"to"
,
s1
,
"got"
,
s2
)
}
}
}
}
...
@@ -51,7 +51,7 @@ func TestBytesToString(t *testing.T) {
...
@@ -51,7 +51,7 @@ func TestBytesToString(t *testing.T) {
func
TestProtocols
(
t
*
testing
.
T
)
{
func
TestProtocols
(
t
*
testing
.
T
)
{
m
,
err
:=
New
String
(
"/ip4/127.0.0.1/udp/1234"
)
m
,
err
:=
New
Multiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Error
(
"failed to construct"
,
"/ip4/127.0.0.1/udp/1234"
)
t
.
Error
(
"failed to construct"
,
"/ip4/127.0.0.1/udp/1234"
)
}
}
...
@@ -72,3 +72,40 @@ func TestProtocols(t *testing.T) {
...
@@ -72,3 +72,40 @@ func TestProtocols(t *testing.T) {
}
}
}
}
func
TestEncapsulate
(
t
*
testing
.
T
)
{
m
,
err
:=
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
m2
,
err
:=
NewMultiaddr
(
"/udp/5678"
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
b
:=
m
.
Encapsulate
(
m2
)
if
s
,
_
:=
b
.
String
();
s
!=
"/ip4/127.0.0.1/udp/1234/udp/5678"
{
t
.
Error
(
"encapsulate /ip4/127.0.0.1/udp/1234/udp/5678 failed."
,
s
)
}
m3
,
_
:=
NewMultiaddr
(
"/udp/5678"
)
c
,
err
:=
b
.
Decapsulate
(
m3
)
if
err
!=
nil
{
t
.
Error
(
"decapsulate /udp failed."
,
err
)
}
if
s
,
_
:=
c
.
String
();
s
!=
"/ip4/127.0.0.1/udp/1234"
{
t
.
Error
(
"decapsulate /udp failed."
,
"/ip4/127.0.0.1/udp/1234"
,
s
)
}
m4
,
_
:=
NewMultiaddr
(
"/ip4/127.0.0.1"
)
d
,
err
:=
c
.
Decapsulate
(
m4
)
if
err
!=
nil
{
t
.
Error
(
"decapsulate /ip4 failed."
,
err
)
}
if
s
,
_
:=
d
.
String
();
s
!=
""
{
t
.
Error
(
"decapsulate /ip4 failed."
,
"/"
,
s
)
}
}
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