Пример #1
0
// Returns the "City Record" for an IP address. Requires the GeoCity(Lite)
// database - http://www.maxmind.com/en/city
func (gi *GeoIP) GetRecord(ip string) *GeoIPRecord {
	if gi.db == nil {
		return nil
	}

	cip := C.CString(ip)
	defer C.free(unsafe.Pointer(cip))

	gi.mu.Lock()
	record := C.GeoIP_record_by_addr(gi.db, cip)
	gi.mu.Unlock()

	if record == nil {
		return nil
	}
	// defer C.free(unsafe.Pointer(record))
	defer C.GeoIPRecord_delete(record)
	rec := new(GeoIPRecord)
	rec.CountryCode = C.GoString(record.country_code)
	rec.CountryCode3 = C.GoString(record.country_code3)
	rec.CountryName = C.GoString(record.country_name)
	rec.Region = C.GoString(record.region)
	rec.City = C.GoString(record.city)
	rec.PostalCode = C.GoString(record.postal_code)
	rec.Latitude = float32(record.latitude)
	rec.Longitude = float32(record.longitude)
	rec.AreaCode = int(record.area_code)
	rec.CharSet = int(record.charset)
	rec.ContinentCode = C.GoString(record.continent_code)

	return rec
}
Пример #2
0
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)))
}
Пример #3
0
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
}
Пример #4
0
// Same as GetRecord() but for IPv6.
func (gi *GeoIP) GetRecordV6(ip string) *GeoIPRecord {
	if gi.db == nil {
		return nil
	}

	cip := C.CString(ip)
	defer C.free(unsafe.Pointer(cip))

	gi.mu.Lock()
	record := C.GeoIP_record_by_addr_v6(gi.db, cip)
	gi.mu.Unlock()

	if record == nil {
		return nil
	}
	defer C.GeoIPRecord_delete(record)

	return populateGeoIPRecords(gi.db.databaseType, record)
}
Пример #5
0
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
}
Пример #6
0
// Returns the "City Record" for an IP address. Requires the GeoCity(Lite)
// database - http://www.maxmind.com/en/city
func (gi *GeoIP) GetRecord(ip string) *GeoIPRecord {
	if gi.db == nil {
		return nil
	}

	cip := C.CString(ip)
	defer C.free(unsafe.Pointer(cip))

	gi.mu.Lock()
	record := C.GeoIP_record_by_addr(gi.db, cip)
	gi.mu.Unlock()

	if record == nil {
		return nil
	}
	// defer C.free(unsafe.Pointer(record))
	defer C.GeoIPRecord_delete(record)
	rec := new(GeoIPRecord)
	rec.CountryCode = C.GoString(record.country_code)
	rec.CountryCode3 = C.GoString(record.country_code3)
	rec.CountryName = C.GoString(record.country_name)
	rec.Region = C.GoString(record.region)
	rec.City = C.GoString(record.city)
	rec.PostalCode = C.GoString(record.postal_code)
	rec.Latitude = float32(record.latitude)
	rec.Longitude = float32(record.longitude)
	rec.CharSet = int(record.charset)
	rec.ContinentCode = C.GoString(record.continent_code)

	if gi.db.databaseType != C.GEOIP_CITY_EDITION_REV0 {
		/* DIRTY HACK BELOW:
		   The GeoIPRecord struct in GeoIPCity.h contains an int32 union of metro_code and dma_code.
		   The union is unnamed, so cgo names it anon0 and assumes it's a 4-byte array.
		*/
		union_int := (*int32)(unsafe.Pointer(&record.anon0))
		rec.MetroCode = int(*union_int)
		rec.AreaCode = int(record.area_code)
	}

	return rec
}
Пример #7
0
func (gi *GeoIP) RecordByIPv6(ip net.IP) (gir *GeoIPRecord) {
	cip := checkedCString(ip.String())
	defer C.free(unsafe.Pointer(cip))
	cGir := C.GeoIP_record_by_addr_v6(gi.gi, cip)
	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
}