Example #1
0
func BuildGeoIP(base string, cacheSize uint16) *GeoIP {
	cbase := C.CString(base)
	gi := C.GeoIP_open(cbase, C.GEOIP_INDEX_CACHE|C.GEOIP_CHECK_CACHE)
	C.free(unsafe.Pointer(cbase))
	if gi == nil {
		return nil
	}
	return &GeoIP{gi}
}
Example #2
0
func GeoIP_Open(base string) *GeoIP {
	cbase := C.CString(base)
	gi := C.GeoIP_open(cbase, C.GEOIP_STANDARD|C.GEOIP_CHECK_CACHE)
	C.free(unsafe.Pointer(cbase))
	if gi == nil {
		return nil
	}
	return &GeoIP{gi}
}
Example #3
0
//Open a database located a filename using flags
func Open(filename string, flags GeoIPOptions) (*GeoIP, error) {
	base := C.CString(filename)
	defer C.free(unsafe.Pointer(base))
	db := C.GeoIP_open(base, C.int(flags))
	if db == nil {
		return nil, errors.New("Cannot create GeoIP object")
	}
	geoIP := &GeoIP{db}
	return geoIP, nil
}
Example #4
0
func Open(filename string) (gi *GeoIP, err error) {
	cFilename := checkedCString(filename)
	defer C.free(unsafe.Pointer(cFilename))
	gi = new(GeoIP)
	gi.gi = C.GeoIP_open(cFilename, C.GEOIP_MEMORY_CACHE)
	if gi.gi == nil {
		err = errors.New("GeoIP_open failed")
		return
	}
	gi.edition = gi.DatabaseEdition()
	return
}
Example #5
0
// Opens a GeoIP database by filename, all formats supported by libgeoip are
// supported though there are only functions to access some of the databases in this API.
// The database is opened in MEMORY_CACHE mode, if you need to optimize for memory
// instead of performance you should change this.
// If you don't pass a filename, it will try opening the database from
// a list of common paths.
func Open(files ...string) (*GeoIP, error) {
	if len(files) == 0 {
		files = []string{
			"/usr/share/GeoIP/GeoIP.dat",       // Linux default
			"/usr/share/local/GeoIP/GeoIP.dat", // source install?
			"/usr/local/share/GeoIP/GeoIP.dat", // FreeBSD
			"/opt/local/share/GeoIP/GeoIP.dat", // MacPorts
			"/usr/share/GeoIP/GeoIP.dat",       // ArchLinux
		}
	}

	g := &GeoIP{}
	runtime.SetFinalizer(g, (*GeoIP).free)

	var err error

	for _, file := range files {

		// libgeoip prints errors if it can't open the file, so check first
		if _, err := os.Stat(file); err != nil {
			if os.IsExist(err) {
				log.Println(err)
			}
			continue
		}

		cbase := C.CString(file)
		defer C.free(unsafe.Pointer(cbase))

		g.db, err = C.GeoIP_open(cbase, C.GEOIP_MEMORY_CACHE)
		if g.db != nil && err != nil {
			break
		}
	}
	if err != nil {
		return nil, fmt.Errorf("Error opening GeoIP database (%s): %s", files, err)
	}

	if g.db == nil {
		return nil, fmt.Errorf("Didn't open GeoIP database (%s)", files)
	}

	C.GeoIP_set_charset(g.db, C.GEOIP_CHARSET_UTF8)
	return g, nil
}
Example #6
0
// Open opens a GeoIP database, all formats supported by libgeoip are supported though
// there are only functions to access the country information in this API.
// The database is opened in MEMORY_CACHE mode, if you need to optimize for memory
// instead of performance you should change this.
// If you don't pass a filename, it will try opening the database from
// a list of common paths.
func Open(files ...string) *GeoIP {
	if len(files) == 0 {
		files = []string{
			"/usr/share/GeoIP/GeoIP.dat",       // Linux default
			"/usr/share/local/GeoIP/GeoIP.dat", // source install?
			"/usr/local/share/GeoIP/GeoIP.dat", // FreeBSD
			"/opt/local/share/GeoIP/GeoIP.dat", // MacPorts
		}
	}

	g := &GeoIP{}

	var err error

	for _, file := range files {

		// libgeoip prints errors if it can't open the file, so check first
		if _, err := os.Stat(file); err != nil {
			if os.IsExist(err) {
				log.Println(err)
			}
			continue
		}

		cbase := C.CString(file)
		g.db, err = C.GeoIP_open(cbase, C.GEOIP_MEMORY_CACHE)
		C.free(unsafe.Pointer(cbase))
		if g.db != nil && err != nil {
			break
		}
	}
	if err != nil && !Quiet {
		log.Println("Error opening GeoIP database", files, err)
	}

	if g.db != nil {
		C.GeoIP_set_charset(g.db, C.GEOIP_CHARSET_UTF8)
		return g
	}

	return nil
}