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/dnssd/minimal_mdns/responders/ReplyFilter.h>
21 :
22 : #include "Parser.h"
23 : #include "Query.h"
24 :
25 : namespace mdns {
26 : namespace Minimal {
27 :
28 : /// A reply filter implementation based on a received/parsed query.
29 : class QueryReplyFilter : public ReplyFilter
30 : {
31 : public:
32 270 : QueryReplyFilter(const QueryData & queryData) : mQueryData(queryData) {}
33 :
34 1853 : bool Accept(QType qType, QClass qClass, FullQName qname) override
35 : {
36 1853 : if (!AcceptableQueryType(qType))
37 : {
38 443 : return false;
39 : }
40 :
41 1410 : if (!AcceptableQueryClass(qClass))
42 : {
43 0 : return false;
44 : }
45 :
46 1410 : return AcceptablePath(qname);
47 : }
48 :
49 : /// Ignore qname matches during Accept calls (if set to true, only qtype and qclass are matched).
50 : ///
51 : /// Ignoring qname is useful when sending related data replies: cliens often query for PTR
52 : /// records however to avoid chattyness, servers generally send all of PTR, SRV (pointed to by PTR)
53 : /// and A/AAAA (pointed to by SRV).
54 135 : QueryReplyFilter & SetIgnoreNameMatch(bool ignore)
55 : {
56 135 : mIgnoreNameMatch = ignore;
57 135 : return *this;
58 : }
59 :
60 135 : QueryReplyFilter & SetSendingAdditionalItems(bool additional)
61 : {
62 135 : mSendingAdditionalItems = additional;
63 135 : return *this;
64 : }
65 :
66 : private:
67 1853 : bool AcceptableQueryType(QType qType)
68 : {
69 1853 : if (mSendingAdditionalItems)
70 : {
71 368 : return true;
72 : }
73 :
74 1485 : return ((mQueryData.GetType() == QType::ANY) || (mQueryData.GetType() == qType));
75 : }
76 :
77 1410 : bool AcceptableQueryClass(QClass qClass)
78 : {
79 1410 : return ((mQueryData.GetClass() == QClass::ANY) || (mQueryData.GetClass() == qClass));
80 : }
81 :
82 1410 : bool AcceptablePath(FullQName qname)
83 : {
84 1410 : if (mIgnoreNameMatch || mQueryData.IsAnnounceBroadcast())
85 : {
86 1012 : return true;
87 : }
88 :
89 398 : return (mQueryData.GetName() == qname);
90 : }
91 :
92 : const QueryData & mQueryData;
93 : bool mIgnoreNameMatch = false;
94 : bool mSendingAdditionalItems = false;
95 : };
96 :
97 : } // namespace Minimal
98 : } // namespace mdns
|