DwmDns-0.1.0
DwmDnsMessageHeader.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/DwmDns/tags/DwmDns-0.1.0/classes/include/DwmDnsMessageHeader.hh 10136 $
3 // @(#) $Id: DwmDnsMessageHeader.hh 10136 2018-01-28 06:01:09Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2000, 2016, 2018
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMDNSMESSAGEHEADER_HH_
43 #define _DWMDNSMESSAGEHEADER_HH_
44 
45 #include <cstdint>
46 
47 namespace Dwm {
48 
49  namespace Dns {
50 
51  //------------------------------------------------------------------------
54  //------------------------------------------------------------------------
56  {
57  public:
58  //----------------------------------------------------------------------
60  //----------------------------------------------------------------------
61  MessageHeader();
62 
63  //----------------------------------------------------------------------
65  //----------------------------------------------------------------------
66  bool operator == (const MessageHeader & messageHeader) const;
67 
68  //----------------------------------------------------------------------
70  //----------------------------------------------------------------------
71  void Clear();
72 
73  //----------------------------------------------------------------------
75  //----------------------------------------------------------------------
76  uint16_t Id() const;
77 
78  //----------------------------------------------------------------------
80  //----------------------------------------------------------------------
81  uint16_t Id(uint16_t id);
82 
83  //----------------------------------------------------------------------
86  //----------------------------------------------------------------------
87  bool IsResponse() const;
88 
89  //----------------------------------------------------------------------
92  //----------------------------------------------------------------------
93  bool IsResponse(bool isResponse);
94 
95  //----------------------------------------------------------------------
97  //----------------------------------------------------------------------
98  uint8_t OpCode() const;
99 
100  //----------------------------------------------------------------------
102  //----------------------------------------------------------------------
103  uint8_t OpCode(uint8_t opcode);
104 
105  //----------------------------------------------------------------------
108  //----------------------------------------------------------------------
109  bool IsAuthoritativeAnswer() const;
110 
111  //----------------------------------------------------------------------
114  //----------------------------------------------------------------------
115  bool IsAuthoritativeAnswer(bool isAuthoritative);
116 
117  //----------------------------------------------------------------------
119  //----------------------------------------------------------------------
120  bool IsTruncated() const;
121 
122  //----------------------------------------------------------------------
125  //----------------------------------------------------------------------
126  bool IsTruncated(bool isTruncated);
127 
128  //----------------------------------------------------------------------
130  //----------------------------------------------------------------------
131  bool RecursionDesired() const;
132 
133  //----------------------------------------------------------------------
136  //----------------------------------------------------------------------
137  bool RecursionDesired(bool recursionDesired);
138 
139  //----------------------------------------------------------------------
141  //----------------------------------------------------------------------
142  bool RecursionAvailable() const;
143 
144  //----------------------------------------------------------------------
146  //----------------------------------------------------------------------
147  bool RecursionAvailable(bool recursionAvailable);
148 
149  uint8_t Z() const;
150  uint8_t Z(uint8_t z);
151 
152  //----------------------------------------------------------------------
154  //----------------------------------------------------------------------
155  uint8_t ResponseCode() const;
156 
157  //----------------------------------------------------------------------
159  //----------------------------------------------------------------------
160  uint8_t ResponseCode(uint8_t responseCode);
161 
162  //----------------------------------------------------------------------
164  //----------------------------------------------------------------------
165  uint16_t QuestionCount() const;
166 
167  //----------------------------------------------------------------------
169  //----------------------------------------------------------------------
170  uint16_t QuestionCount(uint16_t questionCount);
171 
172  //----------------------------------------------------------------------
175  //----------------------------------------------------------------------
176  uint16_t AnswerCount() const;
177 
178  //----------------------------------------------------------------------
181  //----------------------------------------------------------------------
182  uint16_t AnswerCount(uint16_t answerCount);
183 
184  //----------------------------------------------------------------------
187  //----------------------------------------------------------------------
188  uint16_t AuthorityCount() const;
189 
190  //----------------------------------------------------------------------
193  //----------------------------------------------------------------------
194  uint16_t AuthorityCount(uint16_t authorityCount);
195 
196  //----------------------------------------------------------------------
199  //----------------------------------------------------------------------
200  uint16_t AdditionalCount() const;
201 
202  //----------------------------------------------------------------------
205  //----------------------------------------------------------------------
206  uint16_t AdditionalCount(uint16_t additionalCount);
207 
208  //----------------------------------------------------------------------
215  //----------------------------------------------------------------------
216  uint8_t *Encode(uint8_t *pkt, uint8_t *ptr, uint16_t pktlen) const;
217 
218  //----------------------------------------------------------------------
223  //----------------------------------------------------------------------
224  const uint8_t *Decode(const uint8_t *pkt, const uint8_t *ptr,
225  uint16_t pktlen);
226 
227  private:
228  typedef struct __attribute__((packed)) {
229  uint16_t id;
230  uint16_t flags;
231  uint16_t questionCount;
232  uint16_t answerCount;
233  uint16_t authorityCount;
234  uint16_t additionalCount;
235  } data_t;
236 
237  data_t _data;
238 
239  static void ToHostByteOrder(data_t & data);
240  static void ToNetworkByteOrder(data_t & data);
241  };
242 
243  } // namespace Dns
244 
245 } // namespace Dwm
246 
247 #endif // _DWMDNSMESSAGEHEADER_HH_
uint8_t OpCode() const
Returns the OPCODE.
uint16_t AnswerCount() const
Returns the answer count, i.e.
void Clear()
Clears the message header.
uint16_t AuthorityCount() const
Returns the authority count, i.e.
bool RecursionDesired() const
Returns true if recursion is desired.
const uint8_t * Decode(const uint8_t *pkt, const uint8_t *ptr, uint16_t pktlen)
Decodes the encoded header starting at ptr inside a buffer pkt of length pktlen.
uint16_t AdditionalCount() const
Returns the additional count, i.e.
uint8_t * Encode(uint8_t *pkt, uint8_t *ptr, uint16_t pktlen) const
Encodes the header for transport.
bool IsTruncated() const
Returns true if the message was truncated.
MessageHeader()
constructor
uint8_t ResponseCode() const
Returns the response code.
Definition: DwmDnsEtcHosts.hh:60
bool IsAuthoritativeAnswer() const
Returns true if the message is authoritative.
bool RecursionAvailable() const
Returns the recursion available bit in the header.
Encapsulates a DNS message header.
Definition: DwmDnsMessageHeader.hh:55
uint16_t Id() const
Returns the message ID.
bool operator==(const MessageHeader &messageHeader) const
Equal-to operator. Mostly here for unit tests.
bool IsResponse() const
Returns true if the message is a response, false if the message is a query.
uint16_t QuestionCount() const
Returns the question count.