Browse Source

Avoid manipulating IPs as strings when making the /24 IP target

Ask Bjørn Hansen 11 years ago
parent
commit
fc3fd3d74c
2 changed files with 7 additions and 4 deletions
  1. 6 3
      targeting.go
  2. 1 1
      targeting_test.go

+ 6 - 3
targeting.go

@@ -39,9 +39,12 @@ func (t TargetOptions) GetTargets(ip net.IP) ([]string, int) {
 	if t&TargetIP > 0 {
 	if t&TargetIP > 0 {
 		ipStr := ip.String()
 		ipStr := ip.String()
 		targets = append(targets, "["+ipStr+"]")
 		targets = append(targets, "["+ipStr+"]")
-		dotIndex := strings.LastIndex(ipStr, ".")
-		if dotIndex > 0 {
-			targets = append(targets, "["+ipStr[:dotIndex]+".0]")
+		ip4 := ip.To4()
+		if ip4 != nil {
+			if ip4[3] != 0 {
+				ip4[3] = 0
+				targets = append(targets, "["+ip4.String()+"]")
+			}
 		}
 		}
 	}
 	}
 
 

+ 1 - 1
targeting_test.go

@@ -34,8 +34,8 @@ func (s *TargetingSuite) TestTargetParse(c *C) {
 	str = tgt.String()
 	str = tgt.String()
 	c.Check(str, Equals, "@ continent country asn")
 	c.Check(str, Equals, "@ continent country asn")
 }
 }
-func (s *TargetingSuite) TestGetTargets(c *C) {
 
 
+func (s *TargetingSuite) TestGetTargets(c *C) {
 	ip := net.ParseIP("207.171.1.1")
 	ip := net.ParseIP("207.171.1.1")
 
 
 	geoIP.setupGeoIPCity()
 	geoIP.setupGeoIPCity()