Browse Source

Work with latest geoip; use netmask from GeoIP in replies

Ask Bjørn Hansen 12 years ago
parent
commit
f6b607ff50
2 changed files with 11 additions and 5 deletions
  1. 4 3
      geoip.go
  2. 7 2
      serve.go

+ 4 - 3
geoip.go

@@ -7,9 +7,10 @@ import (
 
 func setupGeoIP() *geoip.GeoIP {
 
-	gi := geoip.Open()
-	if gi == nil {
-		log.Printf("Could not open GeoIP database\n")
+	gi, err := geoip.Open()
+	if gi == nil || err != nil {
+		log.Printf("Could not open GeoIP database: %s\n", err)
+		return nil
 	}
 	return gi
 }

+ 7 - 2
serve.go

@@ -58,11 +58,13 @@ func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
 	}
 
 	var country string
+	var netmask int
 	if geoIP != nil {
 		if len(ip) == 0 { // no edns subnet
 			ip, _, _ = net.SplitHostPort(w.RemoteAddr().String())
 		}
-		country = strings.ToLower(geoIP.GetCountry(ip))
+		country, netmask = geoIP.GetCountry(ip)
+		country = strings.ToLower(country)
 		logPrintln("Country:", ip, country)
 	}
 
@@ -76,7 +78,10 @@ func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
 	// TODO: set scope to 0 if there are no alternate responses
 	if edns != nil {
 		if edns.Family != 0 {
-			edns.SourceScope = 16
+			if netmask < 16 {
+				netmask = 16
+			}
+			edns.SourceScope = uint8(netmask)
 			m.Extra = append(m.Extra, opt_rr)
 		}
 	}