1 module Network.DNS.Message
15 import Data.Binary.Get
16 import Data.Binary.Put
24 , msgQuestions :: ![Question]
25 , msgAnswers :: ![ResourceRecord]
26 , msgAuthorities :: ![ResourceRecord]
27 , msgAdditionals :: ![ResourceRecord]
32 hdMessageID :: !Word16
33 , hdMessageType :: !MessageType
35 , hdIsAuthoritativeAnswer :: !Bool
36 , hdIsTruncated :: !Bool
37 , hdIsRecursionDesired :: !Bool
38 , hdIsRecursionAvailable :: !Bool
39 , hdResponseCode :: !ResponseCode
41 -- These fields are supressed in this data structure:
71 , qType :: !RecordType
72 , qClass :: !RecordClass
76 type DomainName = [[Word8]]
83 | AnyClass -- Only for queries
89 , rrType :: !RecordType
90 , rrClass :: !RecordClass
121 instance Binary Header where
122 put h = do putWord16be $ hdMessageID h
123 let qr = fromIntegral $ fromEnum $ hdMessageType h
124 op = fromIntegral $ fromEnum $ hdOpcode h
125 aa = boolToNum $ hdIsAuthoritativeAnswer h
126 tc = boolToNum $ hdIsTruncated h
127 rd = boolToNum $ hdIsRecursionDesired h
128 ra = boolToNum $ hdIsRecursionAvailable h
129 rc = fromIntegral $ fromEnum $ hdResponseCode h
130 flags = ((qr `shiftL` 15) .&. 0x01) .|.
131 ((op `shiftL` 11) .&. 0x0F) .|.
132 ((aa `shiftL` 10) .&. 0x01) .|.
133 ((tc `shiftL` 9) .&. 0x01) .|.
134 ((rd `shiftL` 8) .&. 0x01) .|.
135 ((ra `shiftL` 7) .&. 0x01) .|.
136 ((rc `shiftL` 0) .&. 0x0F)
139 boolToNum :: Num a => Bool -> a
143 get = do mID <- getWord16be
145 let qr = toEnum $ fromIntegral ((flags `shiftR` 15) .&. 0x01)
146 op = toEnum $ fromIntegral ((flags `shiftR` 11) .&. 0x0F)
147 aa = testBit flags 10
151 rc = toEnum $ fromIntegral (flags .&. 0x0F)
156 , hdIsAuthoritativeAnswer = aa
158 , hdIsRecursionDesired = rd
159 , hdIsRecursionAvailable = ra
160 , hdResponseCode = rc
164 instance Enum MessageType where
166 fromEnum Response = 1
172 instance Enum Opcode where
173 fromEnum StandardQuery = 0
174 fromEnum InverseQuery = 1
175 fromEnum ServerStatusRequest = 2
177 toEnum 0 = StandardQuery
178 toEnum 1 = InverseQuery
179 toEnum 2 = ServerStatusRequest
182 instance Enum ResponseCode where
184 fromEnum FormatError = 1
185 fromEnum ServerFailure = 2
186 fromEnum NameError = 3
187 fromEnum NotImplemented = 4
191 toEnum 1 = FormatError
192 toEnum 2 = ServerFailure
194 toEnum 4 = NotImplemented
198 instance Enum RecordType where
218 fromEnum AnyType = 255
242 instance Enum RecordClass
247 fromEnum AnyClass = 255
253 toEnum 255 = AnyClass