Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
20a20c9e
提交
20a20c9e
authored
10月 06, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
updated multiaddr
上级
f3ae0e8e
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
302 行增加
和
184 行删除
+302
-184
Godeps.json
Godeps/Godeps.json
+2
-2
LICENSE
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/LICENSE
+21
-0
README.md
...s/_workspace/src/github.com/jbenet/go-multiaddr/README.md
+20
-18
doc.go
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/doc.go
+36
-0
index.go
...ps/_workspace/src/github.com/jbenet/go-multiaddr/index.go
+0
-117
interface.go
...workspace/src/github.com/jbenet/go-multiaddr/interface.go
+42
-0
multiaddr.go
...workspace/src/github.com/jbenet/go-multiaddr/multiaddr.go
+110
-0
multiaddr_test.go
...pace/src/github.com/jbenet/go-multiaddr/multiaddr_test.go
+7
-39
net.go
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net.go
+38
-2
net_test.go
..._workspace/src/github.com/jbenet/go-multiaddr/net_test.go
+26
-6
没有找到文件。
Godeps/Godeps.json
浏览文件 @
20a20c9e
...
...
@@ -80,8 +80,8 @@
},
{
"ImportPath"
:
"github.com/jbenet/go-multiaddr"
,
"Comment"
:
"0.1.2-
3-g74443fc
"
,
"Rev"
:
"
74443fca319c4c2f5e9968b8e268c30a4a74dc64
"
"Comment"
:
"0.1.2-
9-g1ec9436
"
,
"Rev"
:
"
1ec9436b1d642f4f04c0d9e21a0719cda3d659ee
"
},
{
"ImportPath"
:
"github.com/jbenet/go-multihash"
,
...
...
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/LICENSE
0 → 100644
浏览文件 @
20a20c9e
The MIT License (MIT)
Copyright (c) 2014 Juan Batiz-Benet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/README.md
浏览文件 @
20a20c9e
...
...
@@ -7,18 +7,20 @@
### Simple
```
go
import
"github.com/jbenet/go-multiaddr"
import
ma
"github.com/jbenet/go-multiaddr"
m
:=
multiaddr
.
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
// <Multiaddr /ip4/127.0.0.1/udp/1234>
m
.
buffer
// <Buffer >
m
.
String
()
// /ip4/127.0.0.1/udp/1234
// construct from a string (err signals parse failure)
m1
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
// construct with Buffer
m
=
multiaddr
.
Multiaddr
{
Bytes
:
m
.
Bytes
}
// <Multiaddr /ip4/127.0.0.1/udp/1234>
// construct from bytes (err signals parse failure)
m2
,
err
:=
ma
.
NewMultiaddrBytes
(
m1
.
Bytes
())
// true
strings
.
Equal
(
m1
.
String
(),
"/ip4/127.0.0.1/udp/1234"
)
strings
.
Equal
(
m1
.
String
(),
m2
.
String
())
bytes
.
Equal
(
m1
.
Bytes
(),
m2
.
Bytes
())
m1
.
Equal
(
m2
)
m2
.
Equal
(
m1
)
```
### Protocols
...
...
@@ -36,7 +38,7 @@ addr.Protocols()
```
go
// handles the stupid url version too
m
=
m
ultiaddr
.
NewUrl
(
"udp4://127.0.0.1:1234"
)
m
=
m
a
.
NewUrl
(
"udp4://127.0.0.1:1234"
)
// <Multiaddr /ip4/127.0.0.1/udp/1234>
m
.
Url
(
buf
)
// udp4://127.0.0.1:1234
...
...
@@ -45,9 +47,9 @@ m.Url(buf)
### En/decapsulate
```
go
m
.
Encapsulate
(
m
.
NewMultiaddr
(
"/sctp/5678"
))
m
.
Encapsulate
(
m
a
.
NewMultiaddr
(
"/sctp/5678"
))
// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
m
.
Decapsulate
(
m
.
NewMultiaddr
(
"/udp"
))
// up to + inc last occurrence of subaddr
m
.
Decapsulate
(
m
a
.
NewMultiaddr
(
"/udp"
))
// up to + inc last occurrence of subaddr
// <Multiaddr /ip4/127.0.0.1>
```
...
...
@@ -56,11 +58,11 @@ m.Decapsulate(m.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
Multiaddr allows expressing tunnels very nicely.
```
js
printer
:
=
multiaddr
.
NewMultiaddr
(
"/ip4/192.168.0.13/tcp/80"
)
proxy
:
=
multiaddr
.
NewMultiaddr
(
"/ip4/10.20.30.40/tcp/443"
)
printer
,
_
:
=
ma
.
NewMultiaddr
(
"/ip4/192.168.0.13/tcp/80"
)
proxy
,
_
:
=
ma
.
NewMultiaddr
(
"/ip4/10.20.30.40/tcp/443"
)
printerOverProxy
:
=
proxy
.
Encapsulate
(
printer
)
//
<Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>
//
/ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80
proxyAgain
:
=
printerOverProxy
.
Decapsulate
(
multiaddr
.
NewMultiaddr
(
"/ip4"
)
)
//
<Multiaddr /ip4/10.20.30.40/tcp/443>
proxyAgain
:
=
printerOverProxy
.
Decapsulate
(
printer
)
//
/ip4/10.20.30.40/tcp/443
```
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/doc.go
0 → 100644
浏览文件 @
20a20c9e
/*
Package multiaddr provides an implementation of the Multiaddr network
address format. Multiaddr emphasizes explicitness, self-description, and
portability. It allows applications to treat addresses as opaque tokens,
and to avoid making assumptions about the address representation (e.g. length).
Learn more at https://github.com/jbenet/multiaddr
Basic Use:
import (
"bytes"
"strings"
ma "github.com/jbenet/go-multiaddr"
)
// construct from a string (err signals parse failure)
m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
// construct from bytes (err signals parse failure)
m2, err := ma.NewMultiaddrBytes(m1.Bytes())
// true
strings.Equal(m1.String(), "/ip4/127.0.0.1/udp/1234")
strings.Equal(m1.String(), m2.String())
bytes.Equal(m1.Bytes(), m2.Bytes())
m1.Equal(m2)
m2.Equal(m1)
// tunneling (en/decap)
printer, _ := ma.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
proxy, _ := ma.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
printerOverProxy := proxy.Encapsulate(printer)
proxyAgain := printerOverProxy.Decapsulate(printer)
*/
package
multiaddr
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/index.go
deleted
100644 → 0
浏览文件 @
f3ae0e8e
package
multiaddr
import
(
"bytes"
"fmt"
"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
:=
stringToBytes
(
s
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
Multiaddr
{
Bytes
:
b
},
nil
}
// Equal tests whether two multiaddrs are equal
func
(
m
*
Multiaddr
)
Equal
(
m2
*
Multiaddr
)
bool
{
return
bytes
.
Equal
(
m
.
Bytes
,
m2
.
Bytes
)
}
// String returns the string representation of a Multiaddr
func
(
m
*
Multiaddr
)
String
()
(
string
,
error
)
{
return
bytesToString
(
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.
defer
func
()
{
if
e
:=
recover
();
e
!=
nil
{
ret
=
nil
err
=
e
.
(
error
)
}
}()
ps
:=
[]
*
Protocol
{}
b
:=
m
.
Bytes
[
:
]
for
len
(
b
)
>
0
{
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
if
p
==
nil
{
return
nil
,
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
}
ps
=
append
(
ps
,
p
)
b
=
b
[
1
+
(
p
.
Size
/
8
)
:
]
}
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
{
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
])
}
// 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
)
}
str
,
err
:=
m
.
String
()
if
err
!=
nil
{
return
""
,
""
,
err
}
parts
:=
strings
.
Split
(
str
,
"/"
)[
1
:
]
network
:=
parts
[
2
]
host
:=
strings
.
Join
([]
string
{
parts
[
1
],
parts
[
3
]},
":"
)
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
{
return
false
}
if
p
[
0
]
.
Code
!=
P_IP4
&&
p
[
0
]
.
Code
!=
P_IP6
{
return
false
}
if
p
[
1
]
.
Code
!=
P_TCP
&&
p
[
1
]
.
Code
!=
P_UDP
{
return
false
}
return
true
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/interface.go
0 → 100644
浏览文件 @
20a20c9e
package
multiaddr
/*
Multiaddr is a cross-protocol, cross-platform format for representing
internet addresses. It emphasizes explicitness and self-description.
Learn more here: https://github.com/jbenet/multiaddr
Multiaddrs have both a binary and string representation.
import ma "github.com/jbenet/go-multiaddr"
addr, err := ma.NewMultiaddr("/ip4/1.2.3.4/tcp/80")
// err non-nil when parsing failed.
*/
type
Multiaddr
interface
{
// Equal returns whether two Multiaddrs are exactly equal
Equal
(
Multiaddr
)
bool
// Bytes returns the []byte representation of this Multiaddr
Bytes
()
[]
byte
// String returns the string representation of this Multiaddr
// (may panic if internal state is corrupted)
String
()
string
// Protocols returns the list of Protocols this Multiaddr includes
// will panic if protocol code incorrect (and bytes accessed incorrectly)
Protocols
()
[]
*
Protocol
// Encapsulate wraps this Multiaddr around another. For example:
//
// /ip4/1.2.3.4 encapsulate /tcp/80 = /ip4/1.2.3.4/tcp/80
//
Encapsulate
(
Multiaddr
)
Multiaddr
// Decapsultate removes a Multiaddr wrapping. For example:
//
// /ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = /tcp/80
//
Decapsulate
(
Multiaddr
)
Multiaddr
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr.go
0 → 100644
浏览文件 @
20a20c9e
package
multiaddr
import
(
"bytes"
"fmt"
"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
:=
stringToBytes
(
s
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
multiaddr
{
bytes
:
b
},
nil
}
// NewMultiaddrBytes initializes a Multiaddr from a byte representation.
// It validates it as an input string.
func
NewMultiaddrBytes
(
b
[]
byte
)
(
Multiaddr
,
error
)
{
s
,
err
:=
bytesToString
(
b
)
if
err
!=
nil
{
return
nil
,
err
}
return
NewMultiaddr
(
s
)
}
// Equal tests whether two multiaddrs are equal
func
(
m
*
multiaddr
)
Equal
(
m2
Multiaddr
)
bool
{
return
bytes
.
Equal
(
m
.
bytes
,
m2
.
Bytes
())
}
// Bytes returns the []byte representation of this Multiaddr
func
(
m
*
multiaddr
)
Bytes
()
[]
byte
{
// consider returning copy to prevent changing underneath us?
cpy
:=
make
([]
byte
,
len
(
m
.
bytes
))
copy
(
cpy
,
m
.
bytes
)
return
cpy
}
// String returns the string representation of a Multiaddr
func
(
m
*
multiaddr
)
String
()
string
{
s
,
err
:=
bytesToString
(
m
.
bytes
)
if
err
!=
nil
{
panic
(
"multiaddr failed to convert back to string. corrupted?"
)
}
return
s
}
// Protocols returns the list of protocols this Multiaddr has.
// will panic in case we access bytes incorrectly.
func
(
m
*
multiaddr
)
Protocols
()
[]
*
Protocol
{
// panic handler, in case we try accessing bytes incorrectly.
defer
func
()
{
if
e
:=
recover
();
e
!=
nil
{
err
:=
e
.
(
error
)
panic
(
"Multiaddr.Protocols error: "
+
err
.
Error
())
}
}()
ps
:=
[]
*
Protocol
{}
b
:=
m
.
bytes
[
:
]
for
len
(
b
)
>
0
{
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
if
p
==
nil
{
// 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
]))
}
ps
=
append
(
ps
,
p
)
b
=
b
[
1
+
(
p
.
Size
/
8
)
:
]
}
return
ps
}
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func
(
m
*
multiaddr
)
Encapsulate
(
o
Multiaddr
)
Multiaddr
{
mb
:=
m
.
bytes
ob
:=
o
.
Bytes
()
var
b
bytes
.
Buffer
b
.
Write
(
mb
)
b
.
Write
(
ob
)
return
&
multiaddr
{
bytes
:
b
.
Bytes
()}
}
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
func
(
m
*
multiaddr
)
Decapsulate
(
o
Multiaddr
)
Multiaddr
{
s1
:=
m
.
String
()
s2
:=
o
.
String
()
i
:=
strings
.
LastIndex
(
s1
,
s2
)
if
i
<
0
{
// if multiaddr not contained, returns a copy.
cpy
:=
make
([]
byte
,
len
(
m
.
bytes
))
copy
(
cpy
,
m
.
bytes
)
return
&
multiaddr
{
bytes
:
cpy
}
}
ma
,
err
:=
NewMultiaddr
(
s1
[
:
i
])
if
err
!=
nil
{
panic
(
"Multiaddr.Decapsulate incorrect byte boundaries."
)
}
return
ma
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr_test.go
浏览文件 @
20a20c9e
...
...
@@ -6,7 +6,7 @@ import (
"testing"
)
func
newMultiaddr
(
t
*
testing
.
T
,
a
string
)
*
Multiaddr
{
func
newMultiaddr
(
t
*
testing
.
T
,
a
string
)
Multiaddr
{
m
,
err
:=
NewMultiaddr
(
a
)
if
err
!=
nil
{
t
.
Error
(
err
)
...
...
@@ -88,11 +88,7 @@ func TestProtocols(t *testing.T) {
t
.
Error
(
"failed to construct"
,
"/ip4/127.0.0.1/udp/1234"
)
}
ps
,
err
:=
m
.
Protocols
()
if
err
!=
nil
{
t
.
Error
(
"failed to get protocols"
,
"/ip4/127.0.0.1/udp/1234"
)
}
ps
:=
m
.
Protocols
()
if
ps
[
0
]
!=
ProtocolWithName
(
"ip4"
)
{
t
.
Error
(
ps
[
0
],
ProtocolWithName
(
"ip4"
))
t
.
Error
(
"failed to get ip4 protocol"
)
...
...
@@ -117,47 +113,19 @@ func TestEncapsulate(t *testing.T) {
}
b
:=
m
.
Encapsulate
(
m2
)
if
s
,
_
:=
b
.
String
();
s
!=
"/ip4/127.0.0.1/udp/1234/udp/5678"
{
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"
{
c
:=
b
.
Decapsulate
(
m3
)
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
!=
""
{
d
:=
c
.
Decapsulate
(
m4
)
if
s
:=
d
.
String
();
s
!=
""
{
t
.
Error
(
"decapsulate /ip4 failed."
,
"/"
,
s
)
}
}
func
TestDialArgs
(
t
*
testing
.
T
)
{
m
,
err
:=
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
if
err
!=
nil
{
t
.
Fatal
(
"failed to construct"
,
"/ip4/127.0.0.1/udp/1234"
)
}
nw
,
host
,
err
:=
m
.
DialArgs
()
if
err
!=
nil
{
t
.
Fatal
(
"failed to get dial args"
,
"/ip4/127.0.0.1/udp/1234"
,
err
)
}
if
nw
!=
"udp"
{
t
.
Error
(
"failed to get udp network Dial Arg"
)
}
if
host
!=
"127.0.0.1:1234"
{
t
.
Error
(
"failed to get host:port Dial Arg"
)
}
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net.go
浏览文件 @
20a20c9e
...
...
@@ -3,12 +3,13 @@ package multiaddr
import
(
"fmt"
"net"
"strings"
)
var
errIncorrectNetAddr
=
fmt
.
Errorf
(
"incorrect network addr conversion"
)
// FromNetAddr converts a net.Addr type to a Multiaddr.
func
FromNetAddr
(
a
net
.
Addr
)
(
*
Multiaddr
,
error
)
{
func
FromNetAddr
(
a
net
.
Addr
)
(
Multiaddr
,
error
)
{
switch
a
.
Network
()
{
case
"tcp"
,
"tcp4"
,
"tcp6"
:
ac
,
ok
:=
a
.
(
*
net
.
TCPAddr
)
...
...
@@ -65,7 +66,7 @@ func FromNetAddr(a net.Addr) (*Multiaddr, error) {
}
// FromIP converts a net.IP type to a Multiaddr.
func
FromIP
(
ip
net
.
IP
)
(
*
Multiaddr
,
error
)
{
func
FromIP
(
ip
net
.
IP
)
(
Multiaddr
,
error
)
{
switch
{
case
ip
.
To4
()
!=
nil
:
return
NewMultiaddr
(
"/ip4/"
+
ip
.
String
())
...
...
@@ -75,3 +76,38 @@ func FromIP(ip net.IP) (*Multiaddr, error) {
return
nil
,
errIncorrectNetAddr
}
}
// DialArgs is a convenience function returning arguments for use in net.Dial
func
DialArgs
(
m
Multiaddr
)
(
string
,
string
,
error
)
{
if
!
IsThinWaist
(
m
)
{
return
""
,
""
,
fmt
.
Errorf
(
"%s is not a 'thin waist' address"
,
m
)
}
str
:=
m
.
String
()
parts
:=
strings
.
Split
(
str
,
"/"
)[
1
:
]
network
:=
parts
[
2
]
var
host
string
switch
parts
[
0
]
{
case
"ip4"
:
host
=
strings
.
Join
([]
string
{
parts
[
1
],
parts
[
3
]},
":"
)
case
"ip6"
:
host
=
fmt
.
Sprintf
(
"[%s]:%s"
,
parts
[
1
],
parts
[
3
])
}
return
network
,
host
,
nil
}
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
// This means: /{IP4, IP6}/{TCP, UDP}
func
IsThinWaist
(
m
Multiaddr
)
bool
{
p
:=
m
.
Protocols
()
if
p
[
0
]
.
Code
!=
P_IP4
&&
p
[
0
]
.
Code
!=
P_IP6
{
return
false
}
if
p
[
1
]
.
Code
!=
P_TCP
&&
p
[
1
]
.
Code
!=
P_UDP
{
return
false
}
return
true
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net_test.go
浏览文件 @
20a20c9e
...
...
@@ -5,7 +5,7 @@ import (
"testing"
)
type
GenFunc
func
()
(
*
Multiaddr
,
error
)
type
GenFunc
func
()
(
Multiaddr
,
error
)
func
testConvert
(
t
*
testing
.
T
,
s
string
,
gen
GenFunc
)
{
m
,
err
:=
gen
()
...
...
@@ -13,25 +13,25 @@ func testConvert(t *testing.T, s string, gen GenFunc) {
t
.
Fatal
(
"failed to generate."
)
}
if
s2
,
_
:=
m
.
String
();
err
!=
nil
||
s2
!=
s
{
if
s2
:=
m
.
String
();
err
!=
nil
||
s2
!=
s
{
t
.
Fatal
(
"failed to convert: "
+
s
+
" != "
+
s2
)
}
}
func
TestFromIP4
(
t
*
testing
.
T
)
{
testConvert
(
t
,
"/ip4/10.20.30.40"
,
func
()
(
*
Multiaddr
,
error
)
{
testConvert
(
t
,
"/ip4/10.20.30.40"
,
func
()
(
Multiaddr
,
error
)
{
return
FromIP
(
net
.
ParseIP
(
"10.20.30.40"
))
})
}
func
TestFromIP6
(
t
*
testing
.
T
)
{
testConvert
(
t
,
"/ip6/2001:4860:0:2001::68"
,
func
()
(
*
Multiaddr
,
error
)
{
testConvert
(
t
,
"/ip6/2001:4860:0:2001::68"
,
func
()
(
Multiaddr
,
error
)
{
return
FromIP
(
net
.
ParseIP
(
"2001:4860:0:2001::68"
))
})
}
func
TestFromTCP
(
t
*
testing
.
T
)
{
testConvert
(
t
,
"/ip4/10.20.30.40/tcp/1234"
,
func
()
(
*
Multiaddr
,
error
)
{
testConvert
(
t
,
"/ip4/10.20.30.40/tcp/1234"
,
func
()
(
Multiaddr
,
error
)
{
return
FromNetAddr
(
&
net
.
TCPAddr
{
IP
:
net
.
ParseIP
(
"10.20.30.40"
),
Port
:
1234
,
...
...
@@ -40,10 +40,30 @@ func TestFromTCP(t *testing.T) {
}
func
TestFromUDP
(
t
*
testing
.
T
)
{
testConvert
(
t
,
"/ip4/10.20.30.40/udp/1234"
,
func
()
(
*
Multiaddr
,
error
)
{
testConvert
(
t
,
"/ip4/10.20.30.40/udp/1234"
,
func
()
(
Multiaddr
,
error
)
{
return
FromNetAddr
(
&
net
.
UDPAddr
{
IP
:
net
.
ParseIP
(
"10.20.30.40"
),
Port
:
1234
,
})
})
}
func
TestDialArgs
(
t
*
testing
.
T
)
{
m
,
err
:=
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
if
err
!=
nil
{
t
.
Fatal
(
"failed to construct"
,
"/ip4/127.0.0.1/udp/1234"
)
}
nw
,
host
,
err
:=
DialArgs
(
m
)
if
err
!=
nil
{
t
.
Fatal
(
"failed to get dial args"
,
"/ip4/127.0.0.1/udp/1234"
,
err
)
}
if
nw
!=
"udp"
{
t
.
Error
(
"failed to get udp network Dial Arg"
)
}
if
host
!=
"127.0.0.1:1234"
{
t
.
Error
(
"failed to get host:port Dial Arg"
)
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论