Commits

Anonymous committed 12901b3

dnsServiceGetVersion

Comments (0)

Files changed (2)

-module Network.DNSSD where
+{-# LANGUAGE ForeignFunctionInterface #-}
+module Network.DNSSD
+    ( dnsServiceGetVersion
+    ) where
+import Data.Word
+import Foreign.C.Types (CUInt, CInt (..), CChar)
+import Foreign.Marshal.Alloc (alloca)
+import Foreign.C.String (withCString)
+import Foreign.Storable (peek, sizeOf, poke)
+import Network.DNSSD.Error
+import Foreign.Ptr (castPtr, Ptr)
 
+#define _DNS_SD_LIBDISPATCH 0
+#include <dns_sd.h>
 
+
+dnsServicePropertyDaemonVersion :: String
+dnsServicePropertyDaemonVersion =  "DaemonVersion"
+
+
+dnsServiceGetProperty :: String ->  IO (DNSServiceError, CUInt , CUInt)
+dnsServiceGetProperty s =   
+    withCString s $ \cs ->
+    alloca        $ \prop ->
+    alloca        $ \size -> do
+    poke size (fromIntegral $ sizeOf prop :: CUInt)
+    res  <- {# call  unsafe DNSServiceGetProperty as ^ #} cs (castPtr prop) size
+    val <- peek prop
+    rsize <- peek size
+    return (unmarshalError res , val , rsize)
+
+dnsServiceGetVersion :: IO (Either DNSServiceError Word32)
+dnsServiceGetVersion = do
+    (err , ver , _) <- dnsServiceGetProperty dnsServicePropertyDaemonVersion
+    if err /= DNSServiceErr_NoError
+        then return $ Right $ fromIntegral ver
+        else return $ Left err
 - DONE provide three types of service contexts
 - DONE change type of Register call
 - DONE implement record functions
-- representation of TXT records
-- implement all TXT record functions
-- update service implementation with missing TXT marshaling/unmarshaling
+- DONE representation of TXT records
+- DONE implement all TXT record functions
+- DONE update service implementation with missing TXT marshaling/unmarshaling
 - implement special functions 
 - think about having record type in RecordRef
 - read carefully comments in dns_sd.h (especially on flags) and probably change the API (kDNSServiceFlagsShareConnection is a prominent example)
-- decide on using Either
+- DONE decide on using Either
 - make getVersion instead of getProperty
 - implement record manipulation functions and RegisterRecord
 - visibility between modules and public API