@cryptotaxi247 / kubo / commits / e49e610b0

p2p/net/filter: Guard with a mutex

Callers assume this is safe to call whenever, let's make it so. License: MIT Signed-off-by: Tommi Virtanen <tv@eagain.net>

Tommi Virtanen committed Sep 1, 2015 at 16:49 UTC e49e610b07a8f0cbd443eb6ec9a3db05686beee1
1 file changed +10
p2p/net/filter/filter.go
+10
@@ -3,12 +3,14 @@ package filter
3 import (
4 "net"
5 "strings"
6 + "sync"
7
8 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9 manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
10 )
11
12 type Filters struct {
13 + mu sync.RWMutex
14 filters map[string]*net.IPNet
15 }
16
@@ -19,6 +21,8 @@ func NewFilters() *Filters {
21 }
22
23 func (fs *Filters) AddDialFilter(f *net.IPNet) {
24 + fs.mu.Lock()
25 + defer fs.mu.Unlock()
26 fs.filters[f.String()] = f
27 }
28
@@ -31,6 +35,8 @@ func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
35
36 ipstr := strings.Split(addr, ":")[0]
37 ip := net.ParseIP(ipstr)
38 + f.mu.RLock()
39 + defer f.mu.RUnlock()
40 for _, ft := range f.filters {
41 if ft.Contains(ip) {
42 return true
@@ -41,6 +47,8 @@ func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
47
48 func (f *Filters) Filters() []*net.IPNet {
49 var out []*net.IPNet
50 + f.mu.RLock()
51 + defer f.mu.RUnlock()
52 for _, ff := range f.filters {
53 out = append(out, ff)
54 }
@@ -48,5 +56,7 @@ func (f *Filters) Filters() []*net.IPNet {
56 }
57
58 func (f *Filters) Remove(ff *net.IPNet) {
59 + f.mu.Lock()
60 + defer f.mu.Unlock()
61 delete(f.filters, ff.String())
62 }