Skip to content

Commit

Permalink
Merge pull request #8 from libp2p/feat/pnet
Browse files Browse the repository at this point in the history
Introduce changes required for Private Netowrk support
  • Loading branch information
whyrusleeping authored Nov 15, 2016
2 parents 02a743f + 94cbca5 commit 2d61be9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
14 changes: 12 additions & 2 deletions p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
addrutil "github.com/libp2p/go-addr-util"
conn "github.com/libp2p/go-libp2p-conn"
ci "github.com/libp2p/go-libp2p-crypto"
ipnet "github.com/libp2p/go-libp2p-interface-pnet"
metrics "github.com/libp2p/go-libp2p-metrics"
mconn "github.com/libp2p/go-libp2p-metrics/conn"
inet "github.com/libp2p/go-libp2p-net"
Expand Down Expand Up @@ -98,11 +99,18 @@ type Swarm struct {
bwc metrics.Reporter

limiter *dialLimiter

protec ipnet.Protector
}

func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr, local peer.ID,
peers pstore.Peerstore, bwc metrics.Reporter) (*Swarm, error) {
return NewSwarmWithProtector(ctx, listenAddrs, local, peers, nil, bwc)
}

// NewSwarm constructs a Swarm, with a Chan.
func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
local peer.ID, peers pstore.Peerstore, bwc metrics.Reporter) (*Swarm, error) {
func NewSwarmWithProtector(ctx context.Context, listenAddrs []ma.Multiaddr, local peer.ID,
peers pstore.Peerstore, protec ipnet.Protector, bwc metrics.Reporter) (*Swarm, error) {

listenAddrs, err := filterAddrs(listenAddrs)
if err != nil {
Expand Down Expand Up @@ -131,7 +139,9 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
fdRateLimit: make(chan struct{}, concurrentFdDials),
Filters: filter.NewFilters(),
dialer: conn.NewDialer(local, peers.PrivKey(local), wrap),
protec: protec,
}
s.dialer.Protector = protec

s.dsync = NewDialSync(s.doDial)
s.limiter = newDialLimiter(s.dialAddr)
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm_listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *Swarm) addListener(tptlist transport.Listener) error {
log.Warning("Listener not given PrivateKey, so WILL NOT SECURE conns.")
}

list, err := conn.WrapTransportListener(s.Context(), tptlist, s.local, sk)
list, err := conn.WrapTransportListenerWithProtector(s.Context(), tptlist, s.local, sk, s.protec)
if err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions p2p/net/swarm/swarm_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/jbenet/goprocess"
ipnet "github.com/libp2p/go-libp2p-interface-pnet"
metrics "github.com/libp2p/go-libp2p-metrics"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
Expand All @@ -17,11 +18,17 @@ import (
// to implement inet.Network.
type Network Swarm

// NewNetwork constructs a new network and starts listening on given addresses.
func NewNetwork(ctx context.Context, listen []ma.Multiaddr, local peer.ID,
peers pstore.Peerstore, bwc metrics.Reporter) (*Network, error) {

s, err := NewSwarm(ctx, listen, local, peers, bwc)
return NewNetworkWithProtector(ctx, listen, local, peers, nil, bwc)
}

// NewNetwork constructs a new network and starts listening on given addresses.
func NewNetworkWithProtector(ctx context.Context, listen []ma.Multiaddr, local peer.ID,
peers pstore.Peerstore, protec ipnet.Protector, bwc metrics.Reporter) (*Network, error) {

s, err := NewSwarmWithProtector(ctx, listen, local, peers, protec, bwc)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2d61be9

Please sign in to comment.