Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / src / core / net / dns_headers.hpp
1 /*
2  *  Copyright (c) 2017, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /**
30  * @file
31  *   This file includes definitions for generating and processing DNS headers.
32  */
33
34 #ifndef DNS_HEADER_HPP_
35 #define DNS_HEADER_HPP_
36
37 #include "openthread-core-config.h"
38
39 #include "common/encoding.hpp"
40 #include "common/message.hpp"
41
42 namespace ot {
43
44 /**
45  * @namespace ot::Dns
46  * @brief
47  *   This namespace includes definitions for DNS.
48  *
49  */
50 namespace Dns {
51
52 using ot::Encoding::BigEndian::HostSwap16;
53 using ot::Encoding::BigEndian::HostSwap32;
54
55 /**
56  * @addtogroup core-dns
57  *
58  * @brief
59  *   This module includes definitions for DNS.
60  *
61  * @{
62  *
63  */
64
65 /**
66  * This class implements DNS header generation and parsing.
67  *
68  */
69 OT_TOOL_PACKED_BEGIN
70 class Header
71 {
72 public:
73     /**
74      * Default constructor for DNS Header.
75      *
76      */
77     Header(void) { memset(this, 0, sizeof(*this)); }
78
79     /**
80      * This method returns the Message ID.
81      *
82      * @returns The Message ID value.
83      *
84      */
85     uint16_t GetMessageId(void) const { return HostSwap16(mMessageId); }
86
87     /**
88      * This method sets the Message ID.
89      *
90      * @param[in]  aMessageId The Message ID value.
91      *
92      */
93     void SetMessageId(uint16_t aMessageId) { mMessageId = HostSwap16(aMessageId); }
94
95     /**
96      * Defines types of DNS message.
97      *
98      */
99     enum Type
100     {
101         kTypeQuery    = 0,
102         kTypeResponse = 1,
103     };
104
105     /**
106      * This method returns the type of the message.
107      *
108      * @returns The type of the message.
109      */
110     Type GetType(void) const { return static_cast<Type>((mFlags[0] & kQrFlagMask) >> kQrFlagOffset); }
111
112     /**
113      * This method sets the type of the message.
114      *
115      * @param[in]  aType The type of the message.
116      *
117      */
118     void SetType(Type aType)
119     {
120         mFlags[0] &= ~kQrFlagMask;
121         mFlags[0] |= static_cast<uint8_t>(aType) << kQrFlagOffset;
122     }
123
124     /**
125      * Defines types of query.
126      *
127      */
128     enum QueryType
129     {
130         kQueryTypeStandard = 0,
131         kQueryTypeInverse  = 1,
132         kQueryTypeStatus   = 2,
133         kQueryTypeNotify   = 4,
134         kQueryTypeUpdate   = 5
135     };
136
137     /**
138      * This method returns the type of the query.
139      *
140      * @returns The type of the query.
141      */
142     QueryType GetQueryType(void) const { return static_cast<QueryType>((mFlags[0] & kOpCodeMask) >> kOpCodeOffset); }
143
144     /**
145      * This method sets the type of the query.
146      *
147      * @param[in]  aType The type of the query.
148      *
149      */
150     void SetQueryType(QueryType aType)
151     {
152         mFlags[0] &= ~kOpCodeMask;
153         mFlags[0] |= static_cast<uint8_t>(aType) << kOpCodeOffset;
154     }
155
156     /**
157      * This method specifies in response message if the responding name server is an
158      * authority for the domain name in question section.
159      *
160      * @returns True if Authoritative Answer flag (AA) is set in the header, false otherwise.
161      */
162     bool IsAuthoritativeAnswerFlagSet(void) const { return (mFlags[0] & kAaFlagMask) == kAaFlagMask; }
163
164     /**
165      * This method clears the Authoritative Answer flag (AA) in the header.
166      *
167      */
168     void ClearAuthoritativeAnswerFlag(void) { mFlags[0] &= ~kAaFlagMask; }
169
170     /**
171      * This method sets the Authoritative Answer flag (AA) in the header.
172      *
173      */
174     void SetAuthoritativeAnswerFlag(void) { mFlags[0] |= kAaFlagMask; }
175
176     /**
177      * This method specifies if message is truncated.
178      *
179      * @returns True if Truncation flag (TC) is set in the header, false otherwise.
180      */
181     bool IsTruncationFlagSet(void) const { return (mFlags[0] & kTcFlagMask) == kTcFlagMask; }
182
183     /**
184      * This method clears the Truncation flag (TC) in the header.
185      *
186      */
187     void ClearTruncationFlag(void) { mFlags[0] &= ~kTcFlagMask; }
188
189     /**
190      * This method sets the Truncation flag (TC) in the header.
191      *
192      */
193     void SetTruncationFlag(void) { mFlags[0] |= kTcFlagMask; }
194
195     /**
196      * This method specifies if resolver wants to direct the name server to pursue
197      * the query recursively.
198      *
199      * @returns True if Recursion Desired flag (RD) is set in the header, false otherwise.
200      */
201     bool IsRecursionDesiredFlagSet(void) const { return (mFlags[0] & kRdFlagMask) == kRdFlagMask; }
202
203     /**
204      * This method clears the Recursion Desired flag (RD) in the header.
205      *
206      */
207     void ClearRecursionDesiredFlag(void) { mFlags[0] &= ~kRdFlagMask; }
208
209     /**
210      * This method sets the Recursion Desired flag (RD) in the header.
211      *
212      */
213     void SetRecursionDesiredFlag(void) { mFlags[0] |= kRdFlagMask; }
214
215     /**
216      * This method denotes whether recursive query support is available in the name server.
217      *
218      * @returns True if Recursion Available flag (RA) is set in the header, false otherwise.
219      */
220     bool IsRecursionAvailableFlagSet(void) const { return (mFlags[1] & kRaFlagMask) == kRaFlagMask; }
221
222     /**
223      * This method clears the Recursion Available flag (RA) in the header.
224      *
225      */
226     void ClearRecursionAvailableFlag(void) { mFlags[1] &= ~kRaFlagMask; }
227
228     /**
229      * This method sets the Recursion Available flag (RA) in the header.
230      *
231      */
232     void SetRecursionAvailableFlag(void) { mFlags[1] |= kRaFlagMask; }
233
234     /**
235      * Defines response codes.
236      *
237      */
238     enum Response
239     {
240         kResponseSuccess        = 0,
241         kResponseFormatError    = 1,
242         kResponseServerFailure  = 2,
243         kResponseNameError      = 3,
244         kResponseNotImplemented = 4,
245         kResponseRefused        = 5,
246         kResponseNotAuth        = 9,
247         kResponseNotZone        = 10,
248         kResponseBadName        = 20,
249         kResponseBadAlg         = 21,
250         kResponseBadTruncation  = 22,
251     };
252
253     /**
254      * This method returns the response code.
255      *
256      * @returns The response code from the header.
257      */
258     Response GetResponseCode(void) const { return static_cast<Response>((mFlags[1] & kRCodeMask) >> kRCodeOffset); }
259
260     /**
261      * This method sets the response code.
262      *
263      * @param[in]  aResponse The type of the response.
264      *
265      */
266     void SetResponseCode(Response aResponse)
267     {
268         mFlags[1] &= ~kRCodeMask;
269         mFlags[1] |= static_cast<uint8_t>(aResponse) << kRCodeOffset;
270     }
271
272     /**
273      * This method returns the number of entries in question section.
274      *
275      * @returns The number of entries in question section.
276      *
277      */
278     uint16_t GetQuestionCount(void) const { return HostSwap16(mQdCount); }
279
280     /**
281      * This method sets the number of entries in question section.
282      *
283      * @param[in]  aCount The number of entries in question section.
284      *
285      */
286     void SetQuestionCount(uint16_t aCount) { mQdCount = HostSwap16(aCount); }
287
288     /**
289      * This method returns the number of entries in answer section.
290      *
291      * @returns The number of entries in answer section.
292      *
293      */
294     uint16_t GetAnswerCount(void) const { return HostSwap16(mAnCount); }
295
296     /**
297      * This method sets the number of entries in answer section.
298      *
299      * @param[in]  aCount The number of entries in answer section.
300      *
301      */
302     void SetAnswerCount(uint16_t aCount) { mAnCount = HostSwap16(aCount); }
303
304     /**
305      * This method returns the number of entries in authority records section.
306      *
307      * @returns The number of entries in authority records section.
308      *
309      */
310     uint16_t GetAuthorityRecordsCount(void) const { return HostSwap16(mNsCount); }
311
312     /**
313      * This method sets the number of entries in authority records section.
314      *
315      * @param[in]  aCount The number of entries in authority records section.
316      *
317      */
318     void SetAuthorityRecordsCount(uint16_t aCount) { mNsCount = HostSwap16(aCount); }
319
320     /**
321      * This method returns the number of entries in additional records section.
322      *
323      * @returns The number of entries in additional records section.
324      *
325      */
326     uint16_t GetAdditionalRecordsCount(void) const { return HostSwap16(mArCount); }
327
328     /**
329      * This method sets the number of entries in additional records section.
330      *
331      * @param[in]  aCount The number of entries in additional records section.
332      *
333      */
334     void SetAdditionalRecordsCount(uint16_t aCount) { mArCount = HostSwap16(aCount); }
335
336 private:
337     /**
338      * Protocol Constants (RFC 1035).
339      *
340      */
341     enum
342     {
343         kQrFlagOffset = 7,                     ///< QR Flag offset.
344         kQrFlagMask   = 0x01 << kQrFlagOffset, ///< QR Flag mask.
345         kOpCodeOffset = 3,                     ///< OpCode field offset.
346         kOpCodeMask   = 0x0f << kOpCodeOffset, ///< OpCode field mask.
347         kAaFlagOffset = 2,                     ///< AA Flag offset.
348         kAaFlagMask   = 0x01 << kAaFlagOffset, ///< AA Flag mask.
349         kTcFlagOffset = 1,                     ///< TC Flag offset.
350         kTcFlagMask   = 0x01 << kTcFlagOffset, ///< TC Flag mask.
351         kRdFlagOffset = 0,                     ///< RD Flag offset.
352         kRdFlagMask   = 0x01 << kRdFlagOffset, ///< RD Flag mask.
353
354         kRaFlagOffset = 7,                     ///< RA Flag offset.
355         kRaFlagMask   = 0x01 << kRaFlagOffset, ///< RA Flag mask.
356         kRCodeOffset  = 0,                     ///< RCODE field offset.
357         kRCodeMask    = 0x0f << kRCodeOffset,  ///< RCODE field mask.
358     };
359
360     uint16_t
361              mMessageId; ///< A message identifier that is used by the requester to match up replies to outstanding queries.
362     uint8_t  mFlags[2]; ///< DNS header flags.
363     uint16_t mQdCount;  ///< A number specifying the number of entries in the question section.
364     uint16_t mAnCount;  ///< A number specifying the number of entries in the answer section.
365     uint16_t mNsCount;  ///< A number specifying the number of entries in the authority records section.
366     uint16_t mArCount;  ///< A number specifying the number of entries in the additional records section.
367
368 } OT_TOOL_PACKED_END;
369
370 /**
371  * This class implements Resource Record body format (RR).
372  *
373  */
374 OT_TOOL_PACKED_BEGIN
375 class ResourceRecord
376 {
377 public:
378     /**
379      * This method returns the type of the resource record.
380      *
381      * @returns The type of the resource record.
382      */
383     uint16_t GetType(void) const { return HostSwap16(mType); }
384
385     /**
386      * This method sets the type of the resource record.
387      *
388      * @param[in]  aType The type of the resource record.
389      *
390      */
391     void SetType(uint16_t aType) { mType = HostSwap16(aType); }
392
393     /**
394      * This method returns the class of the resource record.
395      *
396      * @returns The class of the resource record.
397      */
398     uint16_t GetClass(void) const { return HostSwap16(mClass); }
399
400     /**
401      * This method sets the class of the resource record.
402      *
403      * @param[in]  aClass The class of the resource record.
404      *
405      */
406     void SetClass(uint16_t aClass) { mClass = HostSwap16(aClass); }
407
408     /**
409      * This method returns the time to live field of the resource record.
410      *
411      * @returns The time to live field of the resource record.
412      */
413     uint32_t GetTtl(void) const { return HostSwap32(mTtl); }
414
415     /**
416      * This method sets the time to live field of the resource record.
417      *
418      * @param[in]  aTtl The time to live field of the resource record.
419      *
420      */
421     void SetTtl(uint32_t aTtl) { mTtl = HostSwap32(aTtl); }
422
423     /**
424      * This method returns the length of the resource record.
425      *
426      * @returns The length of the resource record.
427      */
428     uint16_t GetLength(void) const { return HostSwap16(mLength); }
429
430     /**
431      * This method sets the length of the resource record.
432      *
433      * @param[in]  aLength The length of the resource record.
434      *
435      */
436     void SetLength(uint16_t aLength) { mLength = HostSwap16(aLength); }
437
438 private:
439     uint16_t mType;   ///< The type of the data in RDATA section.
440     uint16_t mClass;  ///< The class of the data in RDATA section.
441     uint32_t mTtl;    ///< Specifies the maximum time that the resource record may be cached.
442     uint16_t mLength; ///< The length of RDATA section in bytes.
443
444 } OT_TOOL_PACKED_END;
445
446 /**
447  * This class implements Resource Record body format of AAAA type.
448  *
449  */
450 OT_TOOL_PACKED_BEGIN
451 class ResourceRecordAaaa : public ResourceRecord
452 {
453 public:
454     enum
455     {
456         kType   = 0x1C, ///< AAAA Resource Record type.
457         kClass  = 0x01, ///< The value of the Internet class.
458         kLength = 16,   ///< Size of the AAAA Resource Record type.
459     };
460
461     /**
462      * This method initializes the AAAA Resource Record.
463      *
464      */
465     void Init(void)
466     {
467         ResourceRecord::SetType(kType);
468         ResourceRecord::SetClass(kClass);
469         ResourceRecord::SetTtl(0);
470         ResourceRecord::SetLength(kLength);
471         memset(&mAddress, 0, sizeof(mAddress));
472     }
473
474     /**
475      * This method sets the IPv6 address of the resource record.
476      *
477      * @param[in]  aAddress The IPv6 address of the resource record.
478      *
479      */
480     void SetAddress(otIp6Address &aAddress) { mAddress = aAddress; }
481
482     /**
483      * This method returns the reference to IPv6 address of the resource record.
484      *
485      * @returns The reference to IPv6 address of the resource record.
486      */
487     otIp6Address &GetAddress(void) { return mAddress; }
488
489 private:
490     otIp6Address mAddress; ///< IPv6 Address of AAAA Resource Record.
491
492 } OT_TOOL_PACKED_END;
493
494 /**
495  * This class implements Question format.
496  *
497  */
498 OT_TOOL_PACKED_BEGIN
499 class Question
500 {
501 public:
502     /**
503      * Constructor for Question.
504      *
505      */
506     Question(uint16_t aType, uint16_t aClass)
507     {
508         SetType(aType);
509         SetClass(aClass);
510     };
511
512     /**
513      * This method returns the type of the question.
514      *
515      * @returns The type of the question.
516      */
517     uint16_t GetType(void) const { return HostSwap16(mType); }
518
519     /**
520      * This method sets the type of the question.
521      *
522      * @param[in]  aType The type of the question.
523      *
524      */
525     void SetType(uint16_t aType) { mType = HostSwap16(aType); }
526
527     /**
528      * This method returns the class of the question.
529      *
530      * @returns The class of the question.
531      */
532     uint16_t GetClass(void) const { return HostSwap16(mClass); }
533
534     /**
535      * This method sets the class of the question.
536      *
537      * @param[in]  aClass The class of the question.
538      *
539      */
540     void SetClass(uint16_t aClass) { mClass = HostSwap16(aClass); }
541
542 private:
543     uint16_t mType;  ///< The type of the data in question section.
544     uint16_t mClass; ///< The class of the data in question section.
545
546 } OT_TOOL_PACKED_END;
547
548 /**
549  * This class implements Question format of AAAA type.
550  *
551  */
552 class QuestionAaaa : public Question
553 {
554 public:
555     enum
556     {
557         kType  = 0x1C, ///< AAAA Resource Record type.
558         kClass = 0x01, ///< The value of the Internet class.
559     };
560
561     /**
562      * Default constructor for AAAA Question.
563      *
564      */
565     QuestionAaaa(void)
566         : Question(kType, kClass)
567     {
568     }
569
570     /**
571      * This method appends request data to the message.
572      *
573      * @param[in]  aMessage  A reference to the message.
574      *
575      * @retval OT_ERROR_NONE     Successfully appended the bytes.
576      * @retval OT_ERROR_NO_BUFS  Insufficient available buffers to grow the message.
577      *
578      */
579     otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); }
580 };
581
582 /**
583  * @}
584  *
585  */
586
587 } // namespace Dns
588 } // namespace ot
589
590 #endif // DNS_HEADER_HPP_