{-# LINE 1 "src/System/Socket/Internal/AddressInfo.hsc" #-}
{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables,
{-# LINE 2 "src/System/Socket/Internal/AddressInfo.hsc" #-}
            StandaloneDeriving, FlexibleContexts, TypeFamilies,
            GeneralizedNewtypeDeriving #-}
module System.Socket.Internal.AddressInfo (
    AddressInfo (..)
  , GetAddressInfo (..)
  , GetNameInfo (..)
  , AddressInfoException (..)
  --, gaiStrerror
  , eaiAgain
  , eaiBadFlags
  , eaiFail
  , eaiFamily
  , eaiMemory
  , eaiNoName
  , eaiSocketType
  , eaiService
  , eaiSystem
  , AddressInfoFlags (..)
  , aiAddressConfig
  , aiAll
  , aiCanonicalName
  , aiNumericHost
  , aiNumericService
  , aiPassive
  , aiV4Mapped
  , NameInfoFlags (..)
  , niNameRequired
  , niDatagram
  , niNoFullyQualifiedDomainName
  , niNumericHost
  , niNumericService
  ) where

import Control.Exception
import Control.Monad

import Data.Bits
import Data.Monoid
import Data.Typeable
import qualified Data.ByteString as BS

import Foreign.Ptr
import Foreign.Storable
import Foreign.C.Types
import Foreign.C.String
import Foreign.Marshal.Alloc

import System.IO.Unsafe

import System.Socket.Family
import System.Socket.Family.Inet
import System.Socket.Family.Inet6
import System.Socket.Type
import System.Socket.Protocol
import System.Socket.Internal.Platform


{-# LINE 59 "src/System/Socket/Internal/AddressInfo.hsc" #-}

{-# LINE 60 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-------------------------------------------------------------------------------
-- AddressInfo
-------------------------------------------------------------------------------

data AddressInfo f t p
   = AddressInfo
     { addressInfoFlags :: AddressInfoFlags
     , socketAddress    :: SocketAddress f
     , canonicalName    :: Maybe BS.ByteString
     }

deriving instance (Eq   (SocketAddress f)) => Eq   (AddressInfo f t p)
deriving instance (Show (SocketAddress f)) => Show (AddressInfo f t p)

-------------------------------------------------------------------------------
-- AddressInfoException
-------------------------------------------------------------------------------

-- | Contains the error code that can be matched against. Use `show`
--   to get a human readable explanation of the error.
newtype AddressInfoException
      = AddressInfoException CInt
   deriving (Eq, Typeable)

instance Show AddressInfoException where
  show e = "AddressInfoException \"" ++ gaiStrerror e ++ "\""

instance Exception AddressInfoException

-- | A wrapper around @gai_strerror@.
gaiStrerror :: AddressInfoException -> String
gaiStrerror (AddressInfoException e) =
  unsafePerformIO $ do
    msgPtr <- c_gai_strerror e
    peekCString msgPtr

-- | > AddressInfoException "Temporary failure in name resolution"
eaiAgain    :: AddressInfoException
eaiAgain     = AddressInfoException (-3)
{-# LINE 100 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "Bad value for ai_flags"
eaiBadFlags :: AddressInfoException
eaiBadFlags  = AddressInfoException (-1)
{-# LINE 104 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "Non-recoverable failure in name resolution"
eaiFail     :: AddressInfoException
eaiFail      = AddressInfoException (-4)
{-# LINE 108 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "ai_family not supported"
eaiFamily   :: AddressInfoException
eaiFamily    = AddressInfoException (-6)
{-# LINE 112 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "Memory allocation failure"
eaiMemory   :: AddressInfoException
eaiMemory    = AddressInfoException (-10)
{-# LINE 116 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "No such host is known"
eaiNoName   :: AddressInfoException
eaiNoName    = AddressInfoException (-2)
{-# LINE 120 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "Servname not supported for ai_socktype"
eaiService  :: AddressInfoException
eaiService   = AddressInfoException (-8)
{-# LINE 124 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "ai_socktype not supported"
eaiSocketType :: AddressInfoException
eaiSocketType  = AddressInfoException (-7)
{-# LINE 128 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | > AddressInfoException "System error"
eaiSystem   :: AddressInfoException
eaiSystem    = AddressInfoException (-11)
{-# LINE 132 "src/System/Socket/Internal/AddressInfo.hsc" #-}


-- | Use the `Data.Monoid.Monoid` instance to combine several flags:
--
--   > mconcat [aiAddressConfig, aiV4Mapped]
newtype AddressInfoFlags
      = AddressInfoFlags CInt
      deriving (Eq, Show, Bits)

instance Monoid AddressInfoFlags where
  mempty
    = AddressInfoFlags 0
  mappend (AddressInfoFlags a) (AddressInfoFlags b)
    = AddressInfoFlags (a .|. b)

-- | @AI_ADDRCONFIG@:
aiAddressConfig  :: AddressInfoFlags
aiAddressConfig   = AddressInfoFlags (32)
{-# LINE 150 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @AI_ALL@: Return both IPv4 (as mapped `SocketAddressInet6`) and IPv6 addresses when
-- `aiV4Mapped` is set independent of whether IPv6 addresses exist for this
--  name.
aiAll         :: AddressInfoFlags
aiAll          = AddressInfoFlags (16)
{-# LINE 156 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @AI_CANONNAME@:
aiCanonicalName   :: AddressInfoFlags
aiCanonicalName    = AddressInfoFlags (2)
{-# LINE 160 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @AI_NUMERICHOST@:
aiNumericHost :: AddressInfoFlags
aiNumericHost  = AddressInfoFlags (4)
{-# LINE 164 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @AI_NUMERICSERV@:
aiNumericService :: AddressInfoFlags
aiNumericService  = AddressInfoFlags (1024)
{-# LINE 168 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @AI_PASSIVE@:
aiPassive     :: AddressInfoFlags
aiPassive      = AddressInfoFlags (1)
{-# LINE 172 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @AI_V4MAPPED@: Return mapped IPv4 addresses if no IPv6 addresses could be found
--   or if `aiAll` flag is set.
aiV4Mapped    :: AddressInfoFlags
aiV4Mapped     = AddressInfoFlags (8)
{-# LINE 177 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | Use the `Data.Monoid.Monoid` instance to combine several flags:
--
--   > mconcat [niNameRequired, niNoFullyQualifiedDomainName]
newtype NameInfoFlags
      = NameInfoFlags CInt
      deriving (Eq, Show, Bits)

instance Monoid NameInfoFlags where
  mempty
    = NameInfoFlags 0
  mappend (NameInfoFlags a) (NameInfoFlags b)
    = NameInfoFlags (a .|. b)

-- | @NI_NAMEREQD@: Throw an exception if the hostname cannot be determined.
niNameRequired     :: NameInfoFlags
niNameRequired      = NameInfoFlags (8)
{-# LINE 194 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @NI_DGRAM@: Service is datagram based (i.e. `System.Socket.Protocol.UDP.UDP`) rather than stream based (i.e. `System.Socket.Protocol.TCP.TCP`).
niDatagram        :: NameInfoFlags
niDatagram         = NameInfoFlags (16)
{-# LINE 198 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @NI_NOFQDN@: Return only the hostname part of the fully qualified domain name for local hosts.
niNoFullyQualifiedDomainName       :: NameInfoFlags
niNoFullyQualifiedDomainName        = NameInfoFlags (4)
{-# LINE 202 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @NI_NUMERICHOST@: Return the numeric form of the host address.
niNumericHost  :: NameInfoFlags
niNumericHost   = NameInfoFlags (1)
{-# LINE 206 "src/System/Socket/Internal/AddressInfo.hsc" #-}

-- | @NI_NUMERICSERV@: Return the numeric form of the service address.
niNumericService  :: NameInfoFlags
niNumericService   = NameInfoFlags (2)
{-# LINE 210 "src/System/Socket/Internal/AddressInfo.hsc" #-}

class (Family f) => GetAddressInfo f where
  -- | Maps names to addresses (i.e. by DNS lookup).
--
--   The operation throws `AddressInfoException`s.
--
--   Contrary to the underlying @getaddrinfo@ operation this wrapper is
--   typesafe and thus only returns records that match the address, type
--   and protocol encoded in the type. This is the price we have to pay
--   for typesafe sockets and extensibility.
--
--   If you need different types of records, you need to start several
--   queries. If you want to connect to both IPv4 and IPV6 addresses use
--   `aiV4Mapped` and use IPv6-sockets.
--
--   > > getAddressInfo (Just "www.haskell.org") (Just "80") aiV4Mapped :: IO [AddressInfo Inet6 Stream TCP]
--   > [AddressInfo {
--   >    addressInfoFlags = AddressInfoFlags 8,
--   >    socketAddress    = SocketAddressInet6 {address = 2400:cb00:2048:0001:0000:0000:6ca2:cc3c, port = 80, flowInfo = mempty, scopeId = 0},
--   >    canonicalName    = Nothing }]
--   > > getAddressInfo (Just "darcs.haskell.org") Nothing aiV4Mapped :: IO [AddressInfo Inet6 Stream TCP]
--   > [AddressInfo {
--   >    addressInfoFlags = AddressInfoFlags 8, 
--   >    socketAddress    = SocketAddressInet6 {address = 0000:0000:0000:0000:0000:ffff:17fd:e1ad, port = 0, flowInfo = mempty, scopeId = 0},
--   >    canonicalName    = Nothing }]
--   > > getAddressInfo (Just "darcs.haskell.org") Nothing mempty :: IO [AddressInfo Inet6 Stream TCP]
--   > *** Exception: AddressInfoException "Name or service not known"
  getAddressInfo :: (Type t, Protocol p) => Maybe BS.ByteString -> Maybe BS.ByteString -> AddressInfoFlags -> IO [AddressInfo f t p]

instance GetAddressInfo Inet where
  getAddressInfo = getAddressInfo'

instance GetAddressInfo Inet6 where
  getAddressInfo = getAddressInfo'

getAddressInfo' :: forall f t p. (Family f, Type t, Protocol p) => Maybe BS.ByteString -> Maybe BS.ByteString -> AddressInfoFlags -> IO [AddressInfo f t p]
getAddressInfo' mnode mservice (AddressInfoFlags flags) = do
  alloca $ \resultPtrPtr-> do
    poke resultPtrPtr nullPtr
    allocaBytes ((48)) $ \addrInfoPtr-> do
{-# LINE 250 "src/System/Socket/Internal/AddressInfo.hsc" #-}
      -- properly initialize the struct
      c_memset addrInfoPtr 0 (48)
{-# LINE 252 "src/System/Socket/Internal/AddressInfo.hsc" #-}
      poke (ai_flags addrInfoPtr) flags
      poke (ai_family addrInfoPtr) (familyNumber (undefined :: f))
      poke (ai_socktype addrInfoPtr) (typeNumber (undefined :: t))
      poke (ai_protocol addrInfoPtr) (protocolNumber (undefined :: p))
      fnode $ \nodePtr-> do
        fservice $ \servicePtr->
          bracket
            (c_getaddrinfo nodePtr servicePtr addrInfoPtr resultPtrPtr)
            (\_-> do resultPtr <- peek resultPtrPtr
                     when (resultPtr /= nullPtr) (c_freeaddrinfo resultPtr)
            )
            (\e-> if e == 0 then do
                    resultPtr <- peek resultPtrPtr
                    peekAddressInfos resultPtr
                  else do
                    throwIO (AddressInfoException e)
            )
  where
    ai_flags     = ((\hsc_ptr -> hsc_ptr `plusPtr` 0))     :: Ptr (AddressInfo a t p) -> Ptr CInt
{-# LINE 271 "src/System/Socket/Internal/AddressInfo.hsc" #-}
    ai_family    = ((\hsc_ptr -> hsc_ptr `plusPtr` 4))    :: Ptr (AddressInfo a t p) -> Ptr CInt
{-# LINE 272 "src/System/Socket/Internal/AddressInfo.hsc" #-}
    ai_socktype  = ((\hsc_ptr -> hsc_ptr `plusPtr` 8))  :: Ptr (AddressInfo a t p) -> Ptr CInt
{-# LINE 273 "src/System/Socket/Internal/AddressInfo.hsc" #-}
    ai_protocol  = ((\hsc_ptr -> hsc_ptr `plusPtr` 12))  :: Ptr (AddressInfo a t p) -> Ptr CInt
{-# LINE 274 "src/System/Socket/Internal/AddressInfo.hsc" #-}
    ai_addr      = ((\hsc_ptr -> hsc_ptr `plusPtr` 24))      :: Ptr (AddressInfo a t p) -> Ptr (Ptr a)
{-# LINE 275 "src/System/Socket/Internal/AddressInfo.hsc" #-}
    ai_canonname = ((\hsc_ptr -> hsc_ptr `plusPtr` 32)) :: Ptr (AddressInfo a t p) -> Ptr CString
{-# LINE 276 "src/System/Socket/Internal/AddressInfo.hsc" #-}
    ai_next      = ((\hsc_ptr -> hsc_ptr `plusPtr` 40))      :: Ptr (AddressInfo a t p) -> Ptr (Ptr (AddressInfo a t p))
{-# LINE 277 "src/System/Socket/Internal/AddressInfo.hsc" #-}
    fnode = case mnode of
      Just node    -> BS.useAsCString node
      Nothing      -> \f-> f nullPtr
    fservice = case mservice of
      Just service -> BS.useAsCString service
      Nothing      -> \f-> f nullPtr
    peekAddressInfos ptr = 
      if ptr == nullPtr
        then return []
        else do
          flag  <- peek (ai_flags ptr)
          addr  <- peek (ai_addr ptr) >>= peek
          cname <- do cnPtr <- peek (ai_canonname ptr)
                      if cnPtr == nullPtr
                        then return Nothing
                        else BS.packCString cnPtr >>= return . Just
          as    <- peek (ai_next ptr) >>= peekAddressInfos
          return ((AddressInfo (AddressInfoFlags flag) addr cname):as)

-- | Maps addresss to readable host- and service names.
--
--   The operation throws `AddressInfoException`s.
--
--   > > getNameInfo (SocketAddressInet loopback 80) mempty
--   > ("localhost.localdomain","http")
class (Family f) => GetNameInfo f where
  getNameInfo :: SocketAddress f -> NameInfoFlags -> IO (BS.ByteString, BS.ByteString)

instance GetNameInfo Inet where
  getNameInfo = getNameInfo'

instance GetNameInfo Inet6 where
  getNameInfo = getNameInfo'

getNameInfo' :: Storable a => a -> NameInfoFlags -> IO (BS.ByteString, BS.ByteString)
getNameInfo' addr (NameInfoFlags flags) =
  alloca $ \addrPtr->
    allocaBytes (1025) $ \hostPtr->
{-# LINE 315 "src/System/Socket/Internal/AddressInfo.hsc" #-}
      allocaBytes (32) $ \servPtr-> do
{-# LINE 316 "src/System/Socket/Internal/AddressInfo.hsc" #-}
        poke addrPtr addr
        e <- c_getnameinfo addrPtr (fromIntegral $ sizeOf addr)
                           hostPtr (1025)
{-# LINE 319 "src/System/Socket/Internal/AddressInfo.hsc" #-}
                           servPtr (32)
{-# LINE 320 "src/System/Socket/Internal/AddressInfo.hsc" #-}
                           flags
        if e == 0 then do
          host <- BS.packCString hostPtr
          serv <- BS.packCString servPtr
          return (host,serv)
        else do
          throwIO (AddressInfoException e)