hs-GeoIP 0.1.1 → 0.2
raw patch · 2 files changed
+37/−6 lines, 2 files
Files
- hs-GeoIP.cabal +7/−1
- src/Data/Geolocation/GeoIP.hsc +30/−5
hs-GeoIP.cabal view
@@ -1,16 +1,22 @@ Name: hs-GeoIP-Version: 0.1.1+Version: 0.2 Synopsis: Haskell bindings to the MaxMind GeoIPCity database via the C library License: BSD3 License-file: LICENSE Author: Ozgun Ataman Maintainer: ozataman@gmail.com+Homepage: http://github.com/ozataman/hs-GeoIP Category: Data Build-type: Simple Description: This library provides fast, idiomatic Haskell bindings to MaxMind's GeoIPCity IP-based geolocation C API.++ .++ Make sure you have the MaxMind C API installed before installing+ this library, as it depends on the C API. .
src/Data/Geolocation/GeoIP.hsc view
@@ -6,7 +6,7 @@ ( -- * Types- GeoDB+ GeoDB , GeoIPOption , combineOptions , standard@@ -22,6 +22,8 @@ , openGeoDB , geoLocateByIPAddress , geoLocateByIPNum+ , geoStringByIPAddress+ , geoStringByIPNum , mkIpNum ) where @@ -155,17 +157,35 @@ -- | Geo-locate by given IP Adress -- -- > geoLocateByIPAddress db "123.123.123.123"-geoLocateByIPAddress :: GeoDB -> ByteString -> Maybe GeoIPRecord-geoLocateByIPAddress db ip = mkIpNum ip >>= geoLocateByIPNum db +geoLocateByIPAddress :: GeoDB -> ByteString -> IO (Maybe GeoIPRecord)+geoLocateByIPAddress db ip =+ case mkIpNum ip of+ Nothing -> return Nothing+ Just inum -> geoLocateByIPNum db inum +geoStringByIPAddress :: GeoDB -> ByteString -> IO (Maybe ByteString)+geoStringByIPAddress db ip = + case mkIpNum ip of+ Nothing -> return Nothing+ Just inum -> geoStringByIPNum db inum +geoStringByIPNum :: GeoDB -> Integer -> IO (Maybe ByteString)+geoStringByIPNum (GeoDB db) ip =+ withForeignPtr db $ \db' -> do+ ptr <- c_GeoIP_name_by_ipnum db' (fromIntegral ip)+ str <- if nullPtr == ptr+ then return Nothing+ else let x = packCString ptr in x `seq` fmap Just x+ free ptr+ return str+ ------------------------------------------------------------------------------ -- | Geo-locate by given IP number. Call 'mkIpNum' on a 'String' ip address to -- convert to IP number. -- -- > geoLocateByIPNum db 12336939327338-geoLocateByIPNum :: GeoDB -> Integer -> Maybe GeoIPRecord-geoLocateByIPNum (GeoDB db) ip = unsafePerformIO $ do+geoLocateByIPNum :: GeoDB -> Integer -> IO (Maybe GeoIPRecord)+geoLocateByIPNum (GeoDB db) ip = withForeignPtr db $ \db' -> do ptr <- c_GeoIP_record_by_ipnum db' (fromIntegral ip) rec <- peekGeoIPRecord ptr@@ -203,6 +223,11 @@ -> CULong -> IO (Ptr GeoIPRecord) +foreign import ccall safe "GeoIP.h GeoIP_name_by_ipnum"+ c_GeoIP_name_by_ipnum+ :: Ptr GeoIP+ -> CULong+ -> IO CString foreign import ccall safe "GeoIPCity.h &GeoIPRecord_delete" c_GeoIPRecord_delete_funPtr