module Net.UDP(module Net.UDP,Port(..)) where
import Net.PacketParsing
import Net.Packet(outLen)
import Net.Bits(Word16)
import Net.PortNumber(Port(..))
import Net.Utils(Container(..))
data Packet content = Packet
{ sourcePort, destPort :: !Port,
len :: !Word16,
checksum :: !Word16,
content :: !content }
deriving (Show)
template sp dp c = Packet sp dp 0 0 c
instance Functor Packet where fmap f p = p{content=f (content p)}
instance Container Packet where contents = content
instance Parse a => Parse (Packet a) where
parse = do (sp,dp,len,sum) <- parse
trunc (fromIntegral len8)
Packet sp dp len sum # parse
instance Unparse a => Unparse (Packet a) where
unparse (Packet sp dp l0 s c) = unparse (sp,dp,l,nosum,uc)
where
uc=doUnparse c
l=fromIntegral(8+outLen uc) `asTypeOf` l0
nosum=0::Word16