Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <lib/support/BufferWriter.h>
21 :
22 : #include <lib/dnssd/minimal_mdns/core/Constants.h>
23 : #include <lib/dnssd/minimal_mdns/core/QName.h>
24 : #include <lib/dnssd/minimal_mdns/core/RecordWriter.h>
25 :
26 : namespace mdns {
27 : namespace Minimal {
28 :
29 : /// Represents a MDNS Query: QName and flags
30 : class Query
31 : {
32 : public:
33 0 : Query(FullQName name) : mQName(name) {}
34 :
35 : bool IsAnswerViaUnicast() const { return mAnswerViaUnicast; }
36 0 : Query & SetAnswerViaUnicast(bool value)
37 : {
38 0 : mAnswerViaUnicast = value;
39 0 : return *this;
40 : }
41 :
42 : QType GetType() const { return mType; }
43 0 : Query & SetType(QType value)
44 : {
45 0 : mType = value;
46 0 : return *this;
47 : }
48 :
49 : QClass GetClass() const { return mClass; }
50 0 : Query & SetClass(QClass value)
51 : {
52 0 : mClass = value;
53 0 : return *this;
54 : }
55 :
56 : const FullQName & GetName() const { return mQName; }
57 :
58 : /// Append the query to the specified buffer
59 : ///
60 : /// @param hdr will be updated with a query count
61 : /// @param out where to write the query data
62 0 : bool Append(HeaderRef & hdr, RecordWriter & out) const
63 : {
64 : // Questions can only be appended before any other data is added
65 0 : if ((hdr.GetAdditionalCount() != 0) || (hdr.GetAnswerCount() != 0) || (hdr.GetAuthorityCount() != 0))
66 : {
67 0 : return false;
68 : }
69 :
70 0 : out.WriteQName(mQName)
71 0 : .Put16(static_cast<uint16_t>(mType))
72 0 : .Put16(static_cast<uint16_t>(static_cast<uint16_t>(mClass) | (mAnswerViaUnicast ? kQClassUnicastAnswerFlag : 0)));
73 :
74 0 : if (!out.Fit())
75 : {
76 0 : return false;
77 : }
78 :
79 0 : hdr.SetQueryCount(static_cast<uint16_t>(hdr.GetQueryCount() + 1));
80 0 : return true;
81 : }
82 :
83 : private:
84 : const FullQName mQName;
85 : QType mType = QType::ANY;
86 : QClass mClass = QClass::IN;
87 : bool mAnswerViaUnicast = true;
88 : };
89 :
90 : } // namespace Minimal
91 :
92 : } // namespace mdns
|