func (gi *GeoIP) CityByIPNum(ipnum uint32) string { cGir := C.GeoIP_record_by_ipnum(gi.gi, C.ulong(ipnum)) if cGir == nil { return "" } // this call frees all the CStrings in cGir defer C.GeoIPRecord_delete(cGir) return latin1toUTF8([]byte(C.GoString(cGir.city))) }
func (gi *GeoIP) RecordByIPNum(ipnum uint32) (gir *GeoIPRecord) { cGir := C.GeoIP_record_by_ipnum(gi.gi, C.ulong(ipnum)) if cGir == nil { return nil } // this call frees all the CStrings in cGir defer C.GeoIPRecord_delete(cGir) gir = new(GeoIPRecord) gir.CountryCode = C.GoString(cGir.country_code) gir.CountryCode3 = C.GoString(cGir.country_code3) gir.CountryName = C.GoString(cGir.country_name) gir.Region = C.GoString(cGir.region) gir.City = latin1toUTF8([]byte(C.GoString(cGir.city))) gir.PostalCode = C.GoString(cGir.postal_code) gir.Latitude = float64(cGir.latitude) gir.Longitude = float64(cGir.longitude) gir.AreaCode = int(cGir.area_code) gir.ContinentCode = C.GoString(cGir.continent_code) return }
func (self *GeoIP) RecordByIP(ip net.IP) *GeoIPRecord { if len(ip) == 4 { ipaddr := IPv4ToInt(ip) record := C.GeoIP_record_by_ipnum(self.GeoIP, C.ulong(ipaddr)) defer C.GeoIPRecord_delete(record) return parseGeoIPRecord(record) } else if len(ip) == 16 { //having weird problems with cgo and in6_addr, maybe the //typedef is messing it up? addr_buf := C.CString(ip.String()) defer C.free(unsafe.Pointer(addr_buf)) record := C.GeoIP_record_by_name_v6(self.GeoIP, addr_buf) if record != nil { defer C.GeoIPRecord_delete(record) return parseGeoIPRecord(record) } return nil } return nil }