1 module Network.DNS.Message
10 import Data.Binary.Put
17 hdMessageID :: !Word16
19 , hdIsTruncated :: !Bool
20 , hdIsRecursionDesired :: !Bool
23 hdMessageID :: !Word16
25 , hdIsAuthoritativeAnswer :: !Bool
26 , hdIsTruncated :: !Bool
27 , hdIsRecursionDesired :: !Bool
28 , hdIsRecursionAvailable :: !Bool
29 , hdResponseCode :: !ResponseCode
46 hdIsResponse :: Header -> Bool
47 hdIsResponse (QueryHeader _ _ _ _ ) = False
48 hdIsResponse (ResponseHeader _ _ _ _ _ _ _) = True
50 instance Binary Header where
51 put h = do putWord16be $ hdMessageID h
52 let qr = boolToNum $ hdIsResponse h
53 op = fromIntegral $ fromEnum $ hdOpcode h
54 aa = if hdIsResponse h then
55 boolToNum $ hdIsAuthoritativeAnswer h
58 tc = boolToNum $ hdIsTruncated h
59 rd = boolToNum $ hdIsRecursionDesired h
60 ra = if hdIsResponse h then
61 boolToNum $ hdIsRecursionAvailable h
64 rc = if hdIsResponse h then
65 fromIntegral $ fromEnum $ hdResponseCode h
68 flags = ((qr `shiftL` 15) .&. 0x01) .|.
69 ((op `shiftL` 11) .&. 0x0F) .|.
70 ((aa `shiftL` 10) .&. 0x01) .|.
71 ((tc `shiftL` 9) .&. 0x01) .|.
72 ((rd `shiftL` 8) .&. 0x01) .|.
73 ((ra `shiftL` 7) .&. 0x01) .|.
74 ((rc `shiftL` 0) .&. 0x0F)
77 boolToNum :: Num a => Bool -> a
81 get = do mID <- getWord16be
83 let qr = testBit flags 15
84 op = toEnum $ fromIntegral ((flags `shiftR` 11) .&. 0x0F)
89 rc = toEnum $ fromIntegral (flags .&. 0x0F)
94 , hdIsAuthoritativeAnswer = aa
96 , hdIsRecursionDesired = rd
97 , hdIsRecursionAvailable = ra
105 , hdIsRecursionDesired = rd
109 instance Enum Opcode where
110 fromEnum StandardQuery = 0
111 fromEnum InverseQuery = 1
112 fromEnum ServerStatusRequest = 2
114 toEnum 0 = StandardQuery
115 toEnum 1 = InverseQuery
116 toEnum 2 = ServerStatusRequest
119 instance Bounded Opcode where
120 minBound = StandardQuery
121 maxBound = ServerStatusRequest
123 instance Enum ResponseCode where
125 fromEnum FormatError = 1
126 fromEnum ServerFailure = 2
127 fromEnum NameError = 3
128 fromEnum NotImplemented = 4
132 toEnum 1 = FormatError
133 toEnum 2 = ServerFailure
135 toEnum 4 = NotImplemented
139 instance Bounded ResponseCode where