Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app.admin; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | |
Pavel Grafov | a6ea920 | 2017-04-27 14:22:26 +0100 | [diff] [blame] | 22 | import java.net.InetAddress; |
| 23 | import java.net.UnknownHostException; |
| 24 | |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 25 | /** |
Rubin Xu | 0a537bb | 2017-07-06 15:04:46 +0100 | [diff] [blame] | 26 | * A class that represents a TCP connect event initiated through the standard network stack. |
| 27 | * |
| 28 | * <p>It contains information about the originating app as well as the remote TCP endpoint. |
| 29 | * |
| 30 | * <p>Support both IPv4 and IPv6 connections. |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 31 | */ |
| 32 | public final class ConnectEvent extends NetworkEvent implements Parcelable { |
| 33 | |
| 34 | /** The destination IP address. */ |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 35 | private final String mIpAddress; |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 36 | |
| 37 | /** The destination port number. */ |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 38 | private final int mPort; |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 39 | |
Michal Karpinski | da9d3ad | 2016-12-05 13:31:40 +0000 | [diff] [blame] | 40 | /** @hide */ |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 41 | public ConnectEvent(String ipAddress, int port, String packageName, long timestamp) { |
| 42 | super(packageName, timestamp); |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 43 | this.mIpAddress = ipAddress; |
| 44 | this.mPort = port; |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | private ConnectEvent(Parcel in) { |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 48 | this.mIpAddress = in.readString(); |
| 49 | this.mPort = in.readInt(); |
| 50 | this.mPackageName = in.readString(); |
| 51 | this.mTimestamp = in.readLong(); |
| 52 | this.mId = in.readLong(); |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Pavel Grafov | a6ea920 | 2017-04-27 14:22:26 +0100 | [diff] [blame] | 55 | public InetAddress getInetAddress() { |
| 56 | try { |
| 57 | // ipAddress is already an address, not a host name, no DNS resolution will happen. |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 58 | return InetAddress.getByName(mIpAddress); |
Pavel Grafov | a6ea920 | 2017-04-27 14:22:26 +0100 | [diff] [blame] | 59 | } catch (UnknownHostException e) { |
| 60 | // Should never happen as we aren't passing a host name. |
| 61 | return InetAddress.getLoopbackAddress(); |
| 62 | } |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public int getPort() { |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 66 | return mPort; |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
Naomi Musgrave | 10ae748 | 2018-01-12 17:21:38 +0000 | [diff] [blame] | 71 | return String.format("ConnectEvent(%d, %s, %d, %d, %s)", mId, mIpAddress, mPort, mTimestamp, |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 72 | mPackageName); |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 75 | public static final @android.annotation.NonNull Parcelable.Creator<ConnectEvent> CREATOR |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 76 | = new Parcelable.Creator<ConnectEvent>() { |
| 77 | @Override |
| 78 | public ConnectEvent createFromParcel(Parcel in) { |
| 79 | if (in.readInt() != PARCEL_TOKEN_CONNECT_EVENT) { |
| 80 | return null; |
| 81 | } |
| 82 | return new ConnectEvent(in); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public ConnectEvent[] newArray(int size) { |
| 87 | return new ConnectEvent[size]; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | @Override |
Michal Karpinski | 0879eb4 | 2016-11-25 17:17:35 +0000 | [diff] [blame] | 92 | public int describeContents() { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | @Override |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 97 | public void writeToParcel(Parcel out, int flags) { |
| 98 | // write parcel token first |
| 99 | out.writeInt(PARCEL_TOKEN_CONNECT_EVENT); |
Naomi Musgrave | 3b50119 | 2017-10-27 14:52:44 +0100 | [diff] [blame] | 100 | out.writeString(mIpAddress); |
| 101 | out.writeInt(mPort); |
| 102 | out.writeString(mPackageName); |
| 103 | out.writeLong(mTimestamp); |
| 104 | out.writeLong(mId); |
Michal Karpinski | f77ee4f | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 105 | } |
| 106 | } |