Skip to content

Commit

Permalink
Merge pull request #1632 from ipfs/p2p-filter-race
Browse files Browse the repository at this point in the history
p2p/net/filter: Guard with a mutex
  • Loading branch information
jbenet committed Sep 2, 2015
2 parents b4e7603 + a8ca3bc commit 4a66fb4
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions p2p/net/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package filter
import (
"net"
"strings"
"sync"

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"
)

type Filters struct {
mu sync.RWMutex
filters map[string]*net.IPNet
}

Expand All @@ -19,6 +21,8 @@ func NewFilters() *Filters {
}

func (fs *Filters) AddDialFilter(f *net.IPNet) {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.filters[f.String()] = f
}

Expand All @@ -31,6 +35,8 @@ func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {

ipstr := strings.Split(addr, ":")[0]
ip := net.ParseIP(ipstr)
f.mu.RLock()
defer f.mu.RUnlock()
for _, ft := range f.filters {
if ft.Contains(ip) {
return true
Expand All @@ -41,12 +47,16 @@ func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {

func (f *Filters) Filters() []*net.IPNet {
var out []*net.IPNet
f.mu.RLock()
defer f.mu.RUnlock()
for _, ff := range f.filters {
out = append(out, ff)
}
return out
}

func (f *Filters) Remove(ff *net.IPNet) {
f.mu.Lock()
defer f.mu.Unlock()
delete(f.filters, ff.String())
}

0 comments on commit 4a66fb4

Please sign in to comment.