blob: ffd38e2b8760ef76835513c6775050a42f137a64 [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;
24
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010025/**
Rubin Xu0a537bb2017-07-06 15:04:46 +010026 * 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 Karpinskif77ee4f2016-10-12 16:40:06 +010031 */
32public final class ConnectEvent extends NetworkEvent implements Parcelable {
33
34 /** The destination IP address. */
35 private final String ipAddress;
36
37 /** The destination port number. */
38 private final int port;
39
Michal Karpinskida9d3ad2016-12-05 13:31:40 +000040 /** @hide */
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010041 public ConnectEvent(String ipAddress, int port, String packageName, long timestamp) {
42 super(packageName, timestamp);
43 this.ipAddress = ipAddress;
44 this.port = port;
45 }
46
47 private ConnectEvent(Parcel in) {
48 this.ipAddress = in.readString();
49 this.port = in.readInt();
50 this.packageName = in.readString();
51 this.timestamp = in.readLong();
52 }
53
Pavel Grafova6ea9202017-04-27 14:22:26 +010054 public InetAddress getInetAddress() {
55 try {
56 // ipAddress is already an address, not a host name, no DNS resolution will happen.
57 return InetAddress.getByName(ipAddress);
58 } catch (UnknownHostException e) {
59 // Should never happen as we aren't passing a host name.
60 return InetAddress.getLoopbackAddress();
61 }
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010062 }
63
64 public int getPort() {
65 return port;
66 }
67
68 @Override
69 public String toString() {
70 return String.format("ConnectEvent(%s, %d, %d, %s)", ipAddress, port, timestamp,
71 packageName);
72 }
73
74 public static final Parcelable.Creator<ConnectEvent> CREATOR
75 = new Parcelable.Creator<ConnectEvent>() {
76 @Override
77 public ConnectEvent createFromParcel(Parcel in) {
78 if (in.readInt() != PARCEL_TOKEN_CONNECT_EVENT) {
79 return null;
80 }
81 return new ConnectEvent(in);
82 }
83
84 @Override
85 public ConnectEvent[] newArray(int size) {
86 return new ConnectEvent[size];
87 }
88 };
89
90 @Override
Michal Karpinski0879eb42016-11-25 17:17:35 +000091 public int describeContents() {
92 return 0;
93 }
94
95 @Override
Michal Karpinskif77ee4f2016-10-12 16:40:06 +010096 public void writeToParcel(Parcel out, int flags) {
97 // write parcel token first
98 out.writeInt(PARCEL_TOKEN_CONNECT_EVENT);
99 out.writeString(ipAddress);
100 out.writeInt(port);
101 out.writeString(packageName);
102 out.writeLong(timestamp);
103 }
104}
105