blob: aade93407a0f4f0bdf59910b59215b52f20ef2e4 [file] [log] [blame]
Michal Karpinskif77ee4f2016-10-12 16:40:06 +01001/*
2 * Copyright (C) 2016 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
17package android.app.admin;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
Pavel Grafova6ea9202017-04-27 14:22:26 +010022import java.net.InetAddress;
23import java.net.UnknownHostException;
Pavel Grafovf09a1662017-04-28 19:09:38 +010024import java.util.ArrayList;
25import java.util.Collections;
26import java.util.List;
Pavel Grafova6ea9202017-04-27 14:22:26 +010027
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010028/**
Rubin Xu0a537bb2017-07-06 15:04:46 +010029 * A class that represents a DNS lookup event initiated through the standard network stack.
30 *
31 * <p>It contains information about the originating app as well as the DNS hostname and resolved
32 * IP addresses.
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010033 */
34public final class DnsEvent extends NetworkEvent implements Parcelable {
35
36 /** The hostname that was looked up. */
Naomi Musgrave3b501192017-10-27 14:52:44 +010037 private final String mHostname;
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010038
39 /** Contains (possibly a subset of) the IP addresses returned. */
Naomi Musgrave3b501192017-10-27 14:52:44 +010040 private final String[] mIpAddresses;
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010041
42 /**
43 * The number of IP addresses returned from the DNS lookup event. May be different from the
44 * length of ipAddresses if there were too many addresses to log.
45 */
Naomi Musgrave3b501192017-10-27 14:52:44 +010046 private final int mIpAddressesCount;
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010047
Michal Karpinskida9d3ad2016-12-05 13:31:40 +000048 /** @hide */
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010049 public DnsEvent(String hostname, String[] ipAddresses, int ipAddressesCount,
50 String packageName, long timestamp) {
51 super(packageName, timestamp);
Naomi Musgrave3b501192017-10-27 14:52:44 +010052 this.mHostname = hostname;
53 this.mIpAddresses = ipAddresses;
54 this.mIpAddressesCount = ipAddressesCount;
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010055 }
56
57 private DnsEvent(Parcel in) {
Naomi Musgrave3b501192017-10-27 14:52:44 +010058 this.mHostname = in.readString();
59 this.mIpAddresses = in.createStringArray();
60 this.mIpAddressesCount = in.readInt();
61 this.mPackageName = in.readString();
62 this.mTimestamp = in.readLong();
63 this.mId = in.readLong();
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010064 }
65
66 /** Returns the hostname that was looked up. */
67 public String getHostname() {
Naomi Musgrave3b501192017-10-27 14:52:44 +010068 return mHostname;
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010069 }
70
71 /** Returns (possibly a subset of) the IP addresses returned. */
Pavel Grafovf09a1662017-04-28 19:09:38 +010072 public List<InetAddress> getInetAddresses() {
Naomi Musgrave3b501192017-10-27 14:52:44 +010073 if (mIpAddresses == null || mIpAddresses.length == 0) {
Pavel Grafovf09a1662017-04-28 19:09:38 +010074 return Collections.emptyList();
75 }
Naomi Musgrave3b501192017-10-27 14:52:44 +010076 final List<InetAddress> inetAddresses = new ArrayList<>(mIpAddresses.length);
77 for (final String ipAddress : mIpAddresses) {
Pavel Grafova6ea9202017-04-27 14:22:26 +010078 try {
79 // ipAddress is already an address, not a host name, no DNS resolution will happen.
Pavel Grafovf09a1662017-04-28 19:09:38 +010080 inetAddresses.add(InetAddress.getByName(ipAddress));
Pavel Grafova6ea9202017-04-27 14:22:26 +010081 } catch (UnknownHostException e) {
82 // Should never happen as we aren't passing a host name.
Pavel Grafova6ea9202017-04-27 14:22:26 +010083 }
84 }
85 return inetAddresses;
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010086 }
87
88 /**
89 * Returns the number of IP addresses returned from the DNS lookup event. May be different from
Pavel Grafovf09a1662017-04-28 19:09:38 +010090 * the length of the list returned by {@link #getInetAddresses()} if there were too many
Pavel Grafova6ea9202017-04-27 14:22:26 +010091 * addresses to log.
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010092 */
Pavel Grafova6ea9202017-04-27 14:22:26 +010093 public int getTotalResolvedAddressCount() {
Naomi Musgrave3b501192017-10-27 14:52:44 +010094 return mIpAddressesCount;
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010095 }
96
97 @Override
98 public String toString() {
Naomi Musgrave10ae7482018-01-12 17:21:38 +000099 return String.format("DnsEvent(%d, %s, %s, %d, %d, %s)", mId, mHostname,
Naomi Musgrave3b501192017-10-27 14:52:44 +0100100 (mIpAddresses == null) ? "NONE" : String.join(" ", mIpAddresses),
101 mIpAddressesCount, mTimestamp, mPackageName);
Michal Karpinskif77ee4f2016-10-12 16:40:06 +0100102 }
103
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700104 public static final @android.annotation.NonNull Parcelable.Creator<DnsEvent> CREATOR
Michal Karpinskif77ee4f2016-10-12 16:40:06 +0100105 = new Parcelable.Creator<DnsEvent>() {
106 @Override
107 public DnsEvent createFromParcel(Parcel in) {
108 if (in.readInt() != PARCEL_TOKEN_DNS_EVENT) {
109 return null;
110 }
111 return new DnsEvent(in);
112 }
113
114 @Override
115 public DnsEvent[] newArray(int size) {
116 return new DnsEvent[size];
117 }
118 };
119
120 @Override
Michal Karpinski0879eb42016-11-25 17:17:35 +0000121 public int describeContents() {
122 return 0;
123 }
124
125 @Override
Michal Karpinskif77ee4f2016-10-12 16:40:06 +0100126 public void writeToParcel(Parcel out, int flags) {
127 // write parcel token first
128 out.writeInt(PARCEL_TOKEN_DNS_EVENT);
Naomi Musgrave3b501192017-10-27 14:52:44 +0100129 out.writeString(mHostname);
130 out.writeStringArray(mIpAddresses);
131 out.writeInt(mIpAddressesCount);
132 out.writeString(mPackageName);
133 out.writeLong(mTimestamp);
134 out.writeLong(mId);
Michal Karpinskif77ee4f2016-10-12 16:40:06 +0100135 }
136}