module System.Win32.DHCP.SEARCH_INFO
( SEARCH_INFO_TYPE
, SEARCH_INFO (..)
, withSearchInfo
) where
import Data.Monoid ((<>))
import qualified Data.Text as T
import Foreign
import Foreign.C.Types
import System.Win32.Types
import Data.Ip
import Data.Mac
import System.Win32.DHCP.CLIENT_UID
type SEARCH_INFO_TYPE = CInt
data SEARCH_INFO
= ClientIpAddress !Ip
| ClientHardwareAddress !Mac
| ClientName !String
instance Show SEARCH_INFO where
show (ClientIpAddress ip) = T.unpack $ "ClientIpAddress " <> showIp ip
show (ClientHardwareAddress mac) = T.unpack $ "ClientHardwareAddress " <> showMac ":" mac
show (ClientName name) = "ClientName " ++ name
siTypeOf :: SEARCH_INFO -> SEARCH_INFO_TYPE
siTypeOf (ClientIpAddress _) = 0
siTypeOf (ClientHardwareAddress _) = 1
siTypeOf (ClientName _) = 2
withSearchInfo :: SEARCH_INFO -> (Ptr SEARCH_INFO -> IO r) -> IO r
withSearchInfo si f = allocaBytes 12 $ \ptr -> do
let pX = ptr `plusPtr` 4
poke (castPtr ptr) $ siTypeOf si
case si of
ClientIpAddress x -> poke (castPtr pX) x >> f ptr
ClientHardwareAddress m -> withMac m $ \pm -> copyBytes (castPtr pX) pm 8 >> f ptr
ClientName str -> withTString str $ \pstr -> copyBytes (castPtr pX) pstr 4 >> f ptr