Skip to content

Commit

Permalink
p2p/net/filter: Guard with a mutex
Browse files Browse the repository at this point in the history
Callers assume this is safe to call whenever, let's make it so.

License: MIT
Signed-off-by: Tommi Virtanen <tv@eagain.net>
  • Loading branch information
tv42 authored and whyrusleeping committed Sep 11, 2015
1 parent bcf98bd commit 47d6836
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 47d6836

Please sign in to comment.