Browse Source

fix masking bug

IP.Mask() returns a new IP value, rather than modifying its target in place
tags/v1.0.0-rc1
Shivaram Lingamneni 5 years ago
parent
commit
eb8f0e50df
2 changed files with 10 additions and 29 deletions
  1. 8
    12
      irc/connection_limits/limiter.go
  2. 2
    17
      irc/connection_limits/throttler.go

+ 8
- 12
irc/connection_limits/limiter.go View File

@@ -42,17 +42,14 @@ type Limiter struct {
42 42
 	exemptedNets []net.IPNet
43 43
 }
44 44
 
45
-// maskAddr masks the given IPv4/6 address with our cidr limit masks.
46
-func (cl *Limiter) maskAddr(addr net.IP) net.IP {
47
-	if addr.To4() == nil {
48
-		// IPv6 addr
49
-		addr = addr.Mask(cl.ipv6Mask)
45
+// addrToKey canonicalizes `addr` to a string key.
46
+func addrToKey(addr net.IP, v4Mask net.IPMask, v6Mask net.IPMask) string {
47
+	if addr.To4() != nil {
48
+		addr = addr.Mask(v4Mask) // IP.Mask() handles the 4-in-6 mapping for us
50 49
 	} else {
51
-		// IPv4 addr
52
-		addr = addr.Mask(cl.ipv4Mask)
50
+		addr = addr.Mask(v6Mask)
53 51
 	}
54
-
55
-	return addr
52
+	return addr.String()
56 53
 }
57 54
 
58 55
 // AddClient adds a client to our population if possible. If we can't, throws an error instead.
@@ -72,8 +69,7 @@ func (cl *Limiter) AddClient(addr net.IP, force bool) error {
72 69
 	}
73 70
 
74 71
 	// check population
75
-	cl.maskAddr(addr)
76
-	addrString := addr.String()
72
+	addrString := addrToKey(addr, cl.ipv4Mask, cl.ipv6Mask)
77 73
 
78 74
 	if cl.population[addrString]+1 > cl.subnetLimit && !force {
79 75
 		return errTooManyClients
@@ -93,7 +89,7 @@ func (cl *Limiter) RemoveClient(addr net.IP) {
93 89
 		return
94 90
 	}
95 91
 
96
-	addrString := addr.String()
92
+	addrString := addrToKey(addr, cl.ipv4Mask, cl.ipv6Mask)
97 93
 	cl.population[addrString] = cl.population[addrString] - 1
98 94
 
99 95
 	// safety limiter

+ 2
- 17
irc/connection_limits/throttler.go View File

@@ -88,19 +88,6 @@ type Throttler struct {
88 88
 	exemptedNets []net.IPNet
89 89
 }
90 90
 
91
-// maskAddr masks the given IPv4/6 address with our cidr limit masks.
92
-func (ct *Throttler) maskAddr(addr net.IP) net.IP {
93
-	if addr.To4() == nil {
94
-		// IPv6 addr
95
-		addr = addr.Mask(ct.ipv6Mask)
96
-	} else {
97
-		// IPv4 addr
98
-		addr = addr.Mask(ct.ipv4Mask)
99
-	}
100
-
101
-	return addr
102
-}
103
-
104 91
 // ResetFor removes any existing count for the given address.
105 92
 func (ct *Throttler) ResetFor(addr net.IP) {
106 93
 	ct.Lock()
@@ -111,8 +98,7 @@ func (ct *Throttler) ResetFor(addr net.IP) {
111 98
 	}
112 99
 
113 100
 	// remove
114
-	ct.maskAddr(addr)
115
-	addrString := addr.String()
101
+	addrString := addrToKey(addr, ct.ipv4Mask, ct.ipv6Mask)
116 102
 	delete(ct.population, addrString)
117 103
 }
118 104
 
@@ -131,8 +117,7 @@ func (ct *Throttler) AddClient(addr net.IP) error {
131 117
 	}
132 118
 
133 119
 	// check throttle
134
-	ct.maskAddr(addr)
135
-	addrString := addr.String()
120
+	addrString := addrToKey(addr, ct.ipv4Mask, ct.ipv6Mask)
136 121
 
137 122
 	details := ct.population[addrString] // retrieve mutable throttle state from the map
138 123
 	// add in constant state to process the limiting operation

Loading…
Cancel
Save