blob: 58d0e05be6fde052770429c0204a46bb07d51283 [file] [log] [blame]
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07001/*
2 * Copyright (C) 2018 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.net;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.net.InetAddress;
23import java.net.InetSocketAddress;
24import java.net.UnknownHostException;
25
26/**
27 * Describe a network connection including local and remote address/port of a connection and the
28 * transport protocol.
29 *
30 * @hide
31 */
32public final class ConnectionInfo implements Parcelable {
33 public final int protocol;
34 public final InetSocketAddress local;
35 public final InetSocketAddress remote;
36
37 @Override
38 public int describeContents() {
39 return 0;
40 }
41
42 public ConnectionInfo(int protocol, InetSocketAddress local, InetSocketAddress remote) {
43 this.protocol = protocol;
44 this.local = local;
45 this.remote = remote;
46 }
47
48 @Override
49 public void writeToParcel(Parcel out, int flags) {
50 out.writeInt(protocol);
51 out.writeByteArray(local.getAddress().getAddress());
52 out.writeInt(local.getPort());
53 out.writeByteArray(remote.getAddress().getAddress());
54 out.writeInt(remote.getPort());
55 }
56
57 public static final Creator<ConnectionInfo> CREATOR = new Creator<ConnectionInfo>() {
58 public ConnectionInfo createFromParcel(Parcel in) {
59 int protocol = in.readInt();
60 InetAddress localAddress;
61 try {
62 localAddress = InetAddress.getByAddress(in.createByteArray());
63 } catch (UnknownHostException e) {
64 throw new IllegalArgumentException("Invalid InetAddress");
65 }
66 int localPort = in.readInt();
67 InetAddress remoteAddress;
68 try {
69 remoteAddress = InetAddress.getByAddress(in.createByteArray());
70 } catch (UnknownHostException e) {
71 throw new IllegalArgumentException("Invalid InetAddress");
72 }
73 int remotePort = in.readInt();
74 InetSocketAddress local = new InetSocketAddress(localAddress, localPort);
75 InetSocketAddress remote = new InetSocketAddress(remoteAddress, remotePort);
76 return new ConnectionInfo(protocol, local, remote);
77 }
78
79 public ConnectionInfo[] newArray(int size) {
80 return new ConnectionInfo[size];
81 }
82 };
83}