From fbab2a727320af83c45a9f81bf10c16a1a02d062 Mon Sep 17 00:00:00 2001
From: Jeromy <jeromyj@gmail.com>
Date: Wed, 17 Jun 2015 09:53:12 -0700
Subject: [PATCH] broke filters out into a struct

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
---
 p2p/net/swarm/swarm.go      | 42 ++++++++++++++++++++++++++++---------
 p2p/net/swarm/swarm_dial.go | 18 +---------------
 2 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/p2p/net/swarm/swarm.go b/p2p/net/swarm/swarm.go
index 489d6fec3..814fc7157 100644
--- a/p2p/net/swarm/swarm.go
+++ b/p2p/net/swarm/swarm.go
@@ -16,6 +16,7 @@ import (
 
 	ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
 	ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
+	manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
 	ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
 	pst "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport"
 	psy "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream/transport/yamux"
@@ -52,7 +53,7 @@ type Swarm struct {
 	notifs  map[inet.Notifiee]ps.Notifiee
 
 	// filters for addresses that shouldnt be dialed
-	filters []*net.IPNet
+	Filters *Filters
 
 	cg  ctxgroup.ContextGroup
 	bwc metrics.Reporter
@@ -68,13 +69,14 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
 	}
 
 	s := &Swarm{
-		swarm:  ps.NewSwarm(PSTransport),
-		local:  local,
-		peers:  peers,
-		cg:     ctxgroup.WithContext(ctx),
-		dialT:  DialTimeout,
-		notifs: make(map[inet.Notifiee]ps.Notifiee),
-		bwc:    bwc,
+		swarm:   ps.NewSwarm(PSTransport),
+		local:   local,
+		peers:   peers,
+		cg:      ctxgroup.WithContext(ctx),
+		dialT:   DialTimeout,
+		notifs:  make(map[inet.Notifiee]ps.Notifiee),
+		bwc:     bwc,
+		Filters: new(Filters),
 	}
 
 	// configure Swarm
@@ -88,8 +90,28 @@ func (s *Swarm) teardown() error {
 	return s.swarm.Close()
 }
 
-func (s *Swarm) AddDialFilter(f *net.IPNet) {
-	s.filters = append(s.filters, f)
+type Filters struct {
+	filters []*net.IPNet
+}
+
+func (fs *Filters) AddDialFilter(f *net.IPNet) {
+	fs.filters = append(fs.filters, f)
+}
+
+func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
+	_, addr, err := manet.DialArgs(a)
+	if err != nil {
+		// if we cant parse it, its probably not blocked
+		return false
+	}
+
+	ip := net.ParseIP(addr)
+	for _, ft := range f.filters {
+		if ft.Contains(ip) {
+			return true
+		}
+	}
+	return false
 }
 
 // CtxGroup returns the Context Group of the swarm
diff --git a/p2p/net/swarm/swarm_dial.go b/p2p/net/swarm/swarm_dial.go
index 534f916d1..4fe2bc126 100644
--- a/p2p/net/swarm/swarm_dial.go
+++ b/p2p/net/swarm/swarm_dial.go
@@ -459,29 +459,13 @@ func (s *Swarm) dialAddr(ctx context.Context, d *conn.Dialer, p peer.ID, addr ma
 func (s *Swarm) filterAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
 	var out []ma.Multiaddr
 	for _, a := range addrs {
-		if !s.addrBlocked(a) {
+		if !s.Filters.AddrBlocked(a) {
 			out = append(out, a)
 		}
 	}
 	return out
 }
 
-func (s *Swarm) addrBlocked(a ma.Multiaddr) bool {
-	_, addr, err := manet.DialArgs(a)
-	if err != nil {
-		// if we cant parse it, its probably not blocked
-		return false
-	}
-
-	ip := net.ParseIP(addr)
-	for _, f := range s.filters {
-		if f.Contains(ip) {
-			return true
-		}
-	}
-	return false
-}
-
 // dialConnSetup is the setup logic for a connection from the dial side. it
 // needs to add the Conn to the StreamSwarm, then run newConnSetup
 func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error) {
-- 
GitLab