• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.net.apf;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 
21 import com.android.net.module.util.HexDump;
22 
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Objects;
26 
27 /**
28  * Represents a rule for offloading mDNS service.
29  *
30  * @hide
31  */
32 public class MdnsOffloadRule {
33 
34     /**
35      * The payload data to be sent in the mDNS offload reply.
36      * If the payload is empty, the APF must let the query through so that host can respond.
37      */
38     @Nullable
39     public final byte[] mOffloadPayload;
40 
41     @NonNull
42     public final List<Matcher> mMatchers;
43 
44     @NonNull
45     public final String mFullServiceName;
46 
47     /**
48      * Construct an mDNS offload rule.
49      */
MdnsOffloadRule(@onNull String fullServiceName, @NonNull List<Matcher> matchers, @Nullable byte[] offloadPayload)50     public MdnsOffloadRule(@NonNull String fullServiceName, @NonNull List<Matcher> matchers,
51             @Nullable byte[] offloadPayload) {
52         mFullServiceName = fullServiceName;
53         mMatchers = matchers;
54         mOffloadPayload = offloadPayload;
55     }
56 
57     @Override
equals(Object o)58     public boolean equals(Object o) {
59         if (this == o) return true;
60         if (!(o instanceof MdnsOffloadRule that)) return false;
61         return Arrays.equals(mOffloadPayload, that.mOffloadPayload)
62                 && Objects.equals(mMatchers, that.mMatchers);
63     }
64 
65     @Override
hashCode()66     public int hashCode() {
67         int result = Objects.hash(mMatchers);
68         result = 31 * result + Arrays.hashCode(mOffloadPayload);
69         return result;
70     }
71 
72     @Override
toString()73     public String toString() {
74         return "MdnsOffloadRule{" + "mOffloadPayload="
75                 + ((mOffloadPayload == null) ? "(null)" : HexDump.toHexString(mOffloadPayload))
76                 + ", mMatchers=" + mMatchers + '}';
77     }
78 
79     /**
80      * The matcher class.
81      * <p>
82      * A matcher encapsulates the following information:
83      *   mQnames: The QNAME(s) (query names) to match in the mDNS query.
84      *   mQtype: The QTYPE (query type) to match in the mDNS query.
85      */
86     public static class Matcher {
87         /**
88          * The QNAME(s) from the mDNS query that this rule matches.
89          */
90         public final byte[] mQnames;
91         /**
92          * The QTYPE from the mDNS query that this rule matches.
93          */
94         public final int[] mQtypes;
95 
96         /**
97          * Creates a new Matcher.
98          */
Matcher(byte[] qnames, int[] qtypes)99         public Matcher(byte[] qnames, int[] qtypes) {
100             mQnames = qnames;
101             mQtypes = qtypes;
102         }
103 
104         @Override
equals(Object o)105         public boolean equals(Object o) {
106             if (!(o instanceof Matcher matcher)) return false;
107             return Objects.deepEquals(mQnames, matcher.mQnames) && Objects.deepEquals(
108                     mQtypes, matcher.mQtypes);
109         }
110 
111         @Override
hashCode()112         public int hashCode() {
113             return Objects.hash(Arrays.hashCode(mQnames), Arrays.hashCode(mQtypes));
114         }
115 
116         @Override
toString()117         public String toString() {
118             return "Matcher{" + "mQnames=" + HexDump.toHexString(mQnames) + ", mQtypes="
119                     + Arrays.toString(mQtypes) + '}';
120         }
121     }
122 
123 }
124