module System.Linux.Netlink.GeNetlink
where
import Data.List (intersperse)
import Data.Serialize.Get
import Data.Serialize.Put
import Data.Word (Word8)
import System.Linux.Netlink hiding (makeSocket)
data GenlHeader = GenlHeader
{
genlCmd :: Word8
, genlVersion :: Word8
} deriving (Eq)
instance Convertable GenlHeader where
getPut = putGeHeader
getGet _ = getGenlHeader
data GenlData a = GenlData
{
genlDataHeader :: GenlHeader
, genlDataData :: a
} deriving (Eq)
instance Convertable a => Convertable (GenlData a) where
getPut (GenlData h a) = putGeHeader h >> getPut a
getGet t = do
hdr <- getGenlHeader
dat <- getGet t
return $GenlData hdr dat
type GenlPacket a = Packet (GenlData a)
instance Show GenlHeader where
show (GenlHeader cmd ver) =
"Header: Cmd = " ++ show cmd ++ ", Version: " ++ show ver ++ "\n"
instance Show a => Show (GenlData a) where
show (GenlData hdr content) =
show hdr ++ show content
instance Show (GenlData NoData) where
show (GenlData hdr _) =
show hdr
instance Show a => Show (GenlPacket a) where
showList xs = ((concat . intersperse "===\n" . map show $xs) ++)
show (Packet _ cus attrs) =
"GenlPacket: " ++ show cus ++ "\n" ++
"Attrs: \n" ++ showNLAttrs attrs
show p = showPacket p
getGenlHeader :: Get GenlHeader
getGenlHeader = do
cmd <- getWord8
version <- getWord8
_ <- getWord16host
return $GenlHeader cmd version
putGeHeader :: GenlHeader -> Put
putGeHeader gehdr = do
putWord8 $ genlCmd gehdr
putWord8 $ genlVersion gehdr
putWord16host 0
makeSocket :: IO NetlinkSocket
makeSocket = makeSocketGeneric 16