Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ozataman/hs-GeoIP
base: 990c1f36a2
...
head fork: ozataman/hs-GeoIP
compare: 013b4e963c
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Mar 28, 2012
@davean davean Now with the ability to query Org, ISP, and NetSpeed databases. 38a5e2f
Commits on May 13, 2012
@ozataman Merge pull request #1 from davean/master
Add API for Org, ISP, and Netspeed datasets + get rid of unsafePerformIO
013b4e9
Showing with 31 additions and 6 deletions.
  1. +1 −1  hs-GeoIP.cabal
  2. +30 −5 src/Data/Geolocation/GeoIP.hsc
View
2  hs-GeoIP.cabal
@@ -1,5 +1,5 @@
Name: hs-GeoIP
-Version: 0.1.1
+Version: 0.1.3
Synopsis: Haskell bindings to the MaxMind GeoIPCity database via the C library
License: BSD3
License-file: LICENSE
View
35 src/Data/Geolocation/GeoIP.hsc
@@ -22,6 +22,8 @@ module Data.Geolocation.GeoIP
, openGeoDB
, geoLocateByIPAddress
, geoLocateByIPNum
+ , geoStringByIPAddress
+ , geoStringByIPNum
, mkIpNum
) where
@@ -155,17 +157,35 @@ openGeoDB ops dbname = withCString dbname $ \dbname' -> do
-- | 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 @@ foreign import ccall safe "GeoIPCity.h GeoIP_record_by_ipnum"
-> 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

No commit comments for this range

Something went wrong with that request. Please try again.