<commit_before>||| DNSSD bridges to <dns_sd.h>, the C-language DNS Service Discovery API.
module DNSSD

%include C "dnssd_bridge.c"

public
data ResourceRecordType =
  A
  | AAAA

private
resourceRecordType : ResourceRecordType -> Int
resourceRecordType rrType =
  case rrType of
    A =>
      1

    AAAA =>
      28

public
data ResourceRecordClass = IN

private
resourceRecordClass : ResourceRecordClass -> Int
resourceRecordClass rrClass =
  case rrClass of
    IN =>
      1

private
kDNSServiceFlagsTimeout : Int
kDNSServiceFlagsTimeout            = 0x10000

private
kDNSServiceFlagsValidate : Int
kDNSServiceFlagsValidate               = 0x200000

public
record ResourceRecord where
  constructor mkResourceRecord
  fullname : String
  rrType : ResourceRecordType
  rrClass : ResourceRecordClass
  timeToLive : Int


private
synchronouslyQueryRecord : String -> ResourceRecordType -> ResourceRecordClass
  -> IO Ptr
synchronouslyQueryRecord fullName rrType rrClass =
  foreign FFI_C
  "synchronouslyQueryRecord"
  (String -> Int -> Int -> IO Ptr)
  fullName (resourceRecordType rrType) (resourceRecordClass rrClass)


private
queryResultIsError : Ptr -> IO Bool
queryResultIsError result = do
  intVal <- foreign FFI_C
    "queryResultIsError"
    (Ptr -> IO Int)
    result
  return $ intVal /= 0


private
queryResultError : Ptr -> IO Int
queryResultError result =
  foreign FFI_C
  "queryResultError"
  (Ptr -> IO Int)
  result


||| Synchronously queries for a record on all interfaces.
abstract
serviceQueryRecord : String -> ResourceRecordType -> ResourceRecordClass
  -> IO $ Either String (List ResourceRecord)
serviceQueryRecord fullName rrType rrClass = do
  queryResult <- synchronouslyQueryRecord fullName rrType rrClass
  isError <- queryResultIsError queryResult
  if isError
  then return $ Left $ "error " ++ show !(queryResultError queryResult)
  else return $ Left "serviceQueryRecord: WIP"<commit_msg>Stub out result collection and marshaling<commit_after>||| DNSSD bridges to <dns_sd.h>, the C-language DNS Service Discovery API.
module DNSSD

%include C "dnssd_bridge.c"

public
data ResourceRecordType =
  A
  | AAAA

private
resourceRecordType : ResourceRecordType -> Int
resourceRecordType rrType =
  case rrType of
    A =>
      1

    AAAA =>
      28

public
data ResourceRecordClass = IN

private
resourceRecordClass : ResourceRecordClass -> Int
resourceRecordClass rrClass =
  case rrClass of
    IN =>
      1

private
kDNSServiceFlagsTimeout : Int
kDNSServiceFlagsTimeout            = 0x10000

private
kDNSServiceFlagsValidate : Int
kDNSServiceFlagsValidate               = 0x200000

public
record ResourceRecord where
  constructor mkResourceRecord
  fullname : String
  rrType : ResourceRecordType
  rrClass : ResourceRecordClass
  timeToLive : Int


private
synchronouslyQueryRecord : String -> ResourceRecordType -> ResourceRecordClass
  -> IO Ptr
synchronouslyQueryRecord fullName rrType rrClass =
  foreign FFI_C
  "synchronouslyQueryRecord"
  (String -> Int -> Int -> IO Ptr)
  fullName (resourceRecordType rrType) (resourceRecordClass rrClass)


private
queryResultIsError : Ptr -> IO Bool
queryResultIsError result = do
  intVal <- foreign FFI_C
    "queryResultIsError"
    (Ptr -> IO Int)
    result
  return $ intVal /= 0


private
queryResultError : Ptr -> IO Int
queryResultError result =
  foreign FFI_C
  "queryResultError"
  (Ptr -> IO Int)
  result


private
queryResultRecordList : Ptr -> IO Ptr
queryResultRecordList result =
  foreign FFI_C
  "queryResultRecordList"
  (Ptr -> IO Ptr)
  result


private
resultRecordToResourceRecord : Ptr -> IO $ ResourceRecord
resultRecordToResourceRecord headResult =
  return $ mkResourceRecord "WIP" A IN 0


private
extractResultList : Ptr -> IO $ List Ptr
extractResultList queryResult =
  return $ List.Nil


||| Synchronously queries for a record on all interfaces.
abstract
serviceQueryRecord : String -> ResourceRecordType -> ResourceRecordClass
  -> IO $ Either String (List ResourceRecord)
serviceQueryRecord fullName rrType rrClass = do
  queryResult <- synchronouslyQueryRecord fullName rrType rrClass
  isError <- queryResultIsError queryResult
  if isError
  then return $ Left $ "error " ++ show !(queryResultError queryResult)
  else do
    results <- extractResultList queryResult
    records <- sequence $ map resultRecordToResourceRecord results
    return $ Right records
{'fraction_non_alphanumeric': 0.06084776663628077, 'fraction_numerical': 0.008432087511394713, 'mean_word_length': 3.993174061433447, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5274034', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1276, 'n_words': 399}