Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
0135e3eb
提交
0135e3eb
authored
11月 05, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
swarm + net: add explicit listen addresses
上级
f0f202ed
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
138 行增加
和
23 行删除
+138
-23
core.go
core/core.go
+21
-11
interface.go
net/interface.go
+10
-0
net.go
net/net.go
+15
-2
addrs.go
net/swarm/addrs.go
+74
-0
conn.go
net/swarm/conn.go
+13
-3
swarm.go
net/swarm/swarm.go
+3
-5
swarm_test.go
net/swarm/swarm_test.go
+1
-1
dht_test.go
routing/dht/dht_test.go
+1
-1
没有找到文件。
core/core.go
浏览文件 @
0135e3eb
...
...
@@ -123,7 +123,12 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
}
// setup the network
n
.
Network
,
err
=
inet
.
NewIpfsNetwork
(
ctx
,
n
.
Identity
,
n
.
Peerstore
,
muxMap
)
listenAddrs
,
err
:=
listenAddresses
(
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
n
.
Network
,
err
=
inet
.
NewIpfsNetwork
(
ctx
,
listenAddrs
,
n
.
Identity
,
n
.
Peerstore
,
muxMap
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -183,16 +188,6 @@ func initIdentity(cfg *config.Config, peers peer.Peerstore, online bool) (peer.P
return
nil
,
err
}
// address is optional
if
len
(
cfg
.
Addresses
.
Swarm
)
>
0
{
maddr
,
err
:=
ma
.
NewMultiaddr
(
cfg
.
Addresses
.
Swarm
)
if
err
!=
nil
{
return
nil
,
err
}
peer
.
AddAddress
(
maddr
)
}
// when not online, don't need to parse private keys (yet)
if
online
{
skb
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
cfg
.
Identity
.
PrivKey
)
...
...
@@ -233,3 +228,18 @@ func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerst
}
}
}
func
listenAddresses
(
cfg
*
config
.
Config
)
([]
ma
.
Multiaddr
,
error
)
{
var
listen
[]
ma
.
Multiaddr
if
len
(
cfg
.
Addresses
.
Swarm
)
>
0
{
maddr
,
err
:=
ma
.
NewMultiaddr
(
cfg
.
Addresses
.
Swarm
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Failure to parse config.Addresses.Swarm: %s"
,
cfg
.
Addresses
.
Swarm
)
}
listen
=
append
(
listen
,
maddr
)
}
return
listen
,
nil
}
net/interface.go
浏览文件 @
0135e3eb
...
...
@@ -6,6 +6,8 @@ import (
srv
"github.com/jbenet/go-ipfs/net/service"
peer
"github.com/jbenet/go-ipfs/peer"
ctxc
"github.com/jbenet/go-ipfs/util/ctxcloser"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
// Network is the interface IPFS uses for connecting to the world.
...
...
@@ -37,6 +39,14 @@ type Network interface {
// SendMessage sends given Message out
SendMessage
(
msg
.
NetMessage
)
error
// ListenAddresses returns a list of addresses at which this network listens.
ListenAddresses
()
[]
ma
.
Multiaddr
// InterfaceListenAddresses returns a list of addresses at which this network
// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
InterfaceListenAddresses
()
([]
ma
.
Multiaddr
,
error
)
}
// Sender interface for network services.
...
...
net/net.go
浏览文件 @
0135e3eb
...
...
@@ -8,6 +8,7 @@ import (
ctxc
"github.com/jbenet/go-ipfs/util/ctxcloser"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
// IpfsNetwork implements the Network interface,
...
...
@@ -27,7 +28,7 @@ type IpfsNetwork struct {
}
// NewIpfsNetwork is the structure that implements the network interface
func
NewIpfsNetwork
(
ctx
context
.
Context
,
local
peer
.
Peer
,
func
NewIpfsNetwork
(
ctx
context
.
Context
,
l
isten
[]
ma
.
Multiaddr
,
l
ocal
peer
.
Peer
,
peers
peer
.
Peerstore
,
pmap
*
mux
.
ProtocolMap
)
(
*
IpfsNetwork
,
error
)
{
in
:=
&
IpfsNetwork
{
...
...
@@ -37,7 +38,7 @@ func NewIpfsNetwork(ctx context.Context, local peer.Peer,
}
var
err
error
in
.
swarm
,
err
=
swarm
.
NewSwarm
(
ctx
,
local
,
peers
)
in
.
swarm
,
err
=
swarm
.
NewSwarm
(
ctx
,
l
isten
,
l
ocal
,
peers
)
if
err
!=
nil
{
in
.
Close
()
return
nil
,
err
...
...
@@ -96,3 +97,15 @@ func (n *IpfsNetwork) GetPeerList() []peer.Peer {
func
(
n
*
IpfsNetwork
)
GetBandwidthTotals
()
(
in
uint64
,
out
uint64
)
{
return
n
.
muxer
.
GetBandwidthTotals
()
}
// ListenAddresses returns a list of addresses at which this network listens.
func
(
n
*
IpfsNetwork
)
ListenAddresses
()
[]
ma
.
Multiaddr
{
return
n
.
swarm
.
ListenAddresses
()
}
// InterfaceListenAddresses returns a list of addresses at which this network
// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
func
(
n
*
IpfsNetwork
)
InterfaceListenAddresses
()
([]
ma
.
Multiaddr
,
error
)
{
return
n
.
swarm
.
InterfaceListenAddresses
()
}
net/swarm/addrs.go
0 → 100644
浏览文件 @
0135e3eb
package
swarm
import
(
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
// ListenAddresses returns a list of addresses at which this swarm listens.
func
(
s
*
Swarm
)
ListenAddresses
()
[]
ma
.
Multiaddr
{
addrs
:=
make
([]
ma
.
Multiaddr
,
len
(
s
.
listeners
))
for
i
,
l
:=
range
s
.
listeners
{
addrs
[
i
]
=
l
.
Multiaddr
()
}
return
addrs
}
// InterfaceListenAddresses returns a list of addresses at which this swarm
// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
func
(
s
*
Swarm
)
InterfaceListenAddresses
()
([]
ma
.
Multiaddr
,
error
)
{
return
resolveUnspecifiedAddresses
(
s
.
ListenAddresses
())
}
// resolveUnspecifiedAddresses expands unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
func
resolveUnspecifiedAddresses
(
unspecifiedAddrs
[]
ma
.
Multiaddr
)
([]
ma
.
Multiaddr
,
error
)
{
var
outputAddrs
[]
ma
.
Multiaddr
// todo optimize: only fetch these if we have a "any" addr.
ifaceAddrs
,
err
:=
interfaceAddresses
()
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
a
:=
range
unspecifiedAddrs
{
// split address into its components
split
:=
ma
.
Split
(
a
)
// if first component (ip) is not unspecified, use it as is.
if
!
manet
.
IsIPUnspecified
(
split
[
0
])
{
outputAddrs
=
append
(
outputAddrs
)
continue
}
// unspecified? add one address per interface.
for
_
,
ia
:=
range
ifaceAddrs
{
split
[
0
]
=
ia
joined
:=
ma
.
Join
(
split
...
)
outputAddrs
=
append
(
outputAddrs
,
joined
)
}
}
log
.
Info
(
"InterfaceListenAddresses:"
,
outputAddrs
)
return
outputAddrs
,
nil
}
// interfaceAddresses returns a list of addresses associated with local machine
func
interfaceAddresses
()
([]
ma
.
Multiaddr
,
error
)
{
maddrs
,
err
:=
manet
.
InterfaceMultiaddrs
()
if
err
!=
nil
{
return
nil
,
err
}
var
nonLoopback
[]
ma
.
Multiaddr
for
_
,
a
:=
range
maddrs
{
if
!
manet
.
IsIPLoopback
(
a
)
{
nonLoopback
=
append
(
nonLoopback
,
a
)
}
}
return
nonLoopback
,
nil
}
net/swarm/conn.go
浏览文件 @
0135e3eb
...
...
@@ -12,14 +12,14 @@ import (
)
// Open listeners for each network the swarm should listen on
func
(
s
*
Swarm
)
listen
()
error
{
func
(
s
*
Swarm
)
listen
(
addrs
[]
ma
.
Multiaddr
)
error
{
hasErr
:=
false
retErr
:=
&
ListenErr
{
Errors
:
make
([]
error
,
len
(
s
.
local
.
Addresses
()
)),
Errors
:
make
([]
error
,
len
(
addrs
)),
}
// listen on every address
for
i
,
addr
:=
range
s
.
local
.
Addresses
()
{
for
i
,
addr
:=
range
addrs
{
err
:=
s
.
connListen
(
addr
)
if
err
!=
nil
{
hasErr
=
true
...
...
@@ -37,11 +37,21 @@ func (s *Swarm) listen() error {
// Listen for new connections on the given multiaddr
func
(
s
*
Swarm
)
connListen
(
maddr
ma
.
Multiaddr
)
error
{
resolved
,
err
:=
resolveUnspecifiedAddresses
([]
ma
.
Multiaddr
{
maddr
})
if
err
!=
nil
{
return
err
}
list
,
err
:=
conn
.
Listen
(
s
.
Context
(),
maddr
,
s
.
local
,
s
.
peers
)
if
err
!=
nil
{
return
err
}
// add resolved local addresses to peer
for
_
,
addr
:=
range
resolved
{
s
.
local
.
AddAddress
(
addr
)
}
// make sure port can be reused. TOOD this doesn't work...
// if err := setSocketReuse(list); err != nil {
// return err
...
...
net/swarm/swarm.go
浏览文件 @
0135e3eb
...
...
@@ -13,6 +13,7 @@ import (
ctxc
"github.com/jbenet/go-ipfs/util/ctxcloser"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
var
log
=
u
.
Logger
(
"swarm"
)
...
...
@@ -70,7 +71,7 @@ type Swarm struct {
}
// NewSwarm constructs a Swarm, with a Chan.
func
NewSwarm
(
ctx
context
.
Context
,
local
peer
.
Peer
,
ps
peer
.
Peerstore
)
(
*
Swarm
,
error
)
{
func
NewSwarm
(
ctx
context
.
Context
,
l
istenAddrs
[]
ma
.
Multiaddr
,
l
ocal
peer
.
Peer
,
ps
peer
.
Peerstore
)
(
*
Swarm
,
error
)
{
s
:=
&
Swarm
{
Pipe
:
msg
.
NewPipe
(
10
),
conns
:
conn
.
MultiConnMap
{},
...
...
@@ -83,7 +84,7 @@ func NewSwarm(ctx context.Context, local peer.Peer, ps peer.Peerstore) (*Swarm,
s
.
ContextCloser
=
ctxc
.
NewContextCloser
(
ctx
,
s
.
close
)
go
s
.
fanOut
()
return
s
,
s
.
listen
()
return
s
,
s
.
listen
(
listenAddrs
)
}
// close stops a swarm. It's the underlying function called by ContextCloser
...
...
@@ -210,6 +211,3 @@ func (s *Swarm) GetPeerList() []peer.Peer {
s
.
connsLock
.
RUnlock
()
return
out
}
// Temporary to ensure that the Swarm always matches the Network interface as we are changing it
// var _ Network = &Swarm{}
net/swarm/swarm_test.go
浏览文件 @
0135e3eb
...
...
@@ -57,7 +57,7 @@ func makeSwarms(ctx context.Context, t *testing.T, addrs []string) ([]*Swarm, []
for
_
,
addr
:=
range
addrs
{
local
:=
setupPeer
(
t
,
addr
)
peerstore
:=
peer
.
NewPeerstore
()
swarm
,
err
:=
NewSwarm
(
ctx
,
local
,
peerstore
)
swarm
,
err
:=
NewSwarm
(
ctx
,
local
.
Addresses
(),
local
,
peerstore
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
routing/dht/dht_test.go
浏览文件 @
0135e3eb
...
...
@@ -24,7 +24,7 @@ func setupDHT(ctx context.Context, t *testing.T, p peer.Peer) *IpfsDHT {
peerstore
:=
peer
.
NewPeerstore
()
dhts
:=
netservice
.
NewService
(
ctx
,
nil
)
// nil handler for now, need to patch it
net
,
err
:=
inet
.
NewIpfsNetwork
(
ctx
,
p
,
peerstore
,
&
mux
.
ProtocolMap
{
net
,
err
:=
inet
.
NewIpfsNetwork
(
ctx
,
p
.
Addresses
(),
p
,
peerstore
,
&
mux
.
ProtocolMap
{
mux
.
ProtocolID_Routing
:
dhts
,
})
if
err
!=
nil
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论