blob: 18726f748d301961bd2747a14e87524a921853a1 [file] [log] [blame]
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001/*
2 * Copyright (C) 2015 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
Nathan Harold26de1d32017-11-02 21:01:46 -070017package android.net;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090018
junyulai06835112019-01-03 18:50:15 +080019import static android.net.SocketKeepalive.ERROR_INVALID_IP_ADDRESS;
20import static android.net.SocketKeepalive.ERROR_INVALID_PORT;
Nathan Harold63dd8132018-02-14 13:09:45 -080021
junyulai06835112019-01-03 18:50:15 +080022import android.net.SocketKeepalive.InvalidPacketException;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090023import android.net.util.IpUtils;
Nathan Harold702247162017-12-06 19:07:32 -080024import android.os.Parcel;
25import android.os.Parcelable;
Nathan Harold702247162017-12-06 19:07:32 -080026import android.util.Log;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090027
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090028import java.net.InetAddress;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090029
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090030/**
31 * Represents the actual packets that are sent by the
junyulai06835112019-01-03 18:50:15 +080032 * {@link android.net.SocketKeepalive} API.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090033 *
34 * @hide
35 */
Nathan Harold702247162017-12-06 19:07:32 -080036public class KeepalivePacketData implements Parcelable {
37 private static final String TAG = "KeepalivePacketData";
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090038
39 /** Source IP address */
40 public final InetAddress srcAddress;
41
42 /** Destination IP address */
43 public final InetAddress dstAddress;
44
45 /** Source port */
46 public final int srcPort;
47
48 /** Destination port */
49 public final int dstPort;
50
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090051 /** Packet data. A raw byte string of packet data, not including the link-layer header. */
Nathan Harold702247162017-12-06 19:07:32 -080052 private final byte[] mPacket;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090053
junyulai06835112019-01-03 18:50:15 +080054 protected static final int IPV4_HEADER_LENGTH = 20;
55 protected static final int UDP_HEADER_LENGTH = 8;
Nathan Harold702247162017-12-06 19:07:32 -080056
57 // This should only be constructed via static factory methods, such as
58 // nattKeepalivePacket
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090059 protected KeepalivePacketData(InetAddress srcAddress, int srcPort,
60 InetAddress dstAddress, int dstPort, byte[] data) throws InvalidPacketException {
61 this.srcAddress = srcAddress;
62 this.dstAddress = dstAddress;
63 this.srcPort = srcPort;
64 this.dstPort = dstPort;
Nathan Harold702247162017-12-06 19:07:32 -080065 this.mPacket = data;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090066
67 // Check we have two IP addresses of the same family.
Nathan Harold702247162017-12-06 19:07:32 -080068 if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName()
69 .equals(dstAddress.getClass().getName())) {
70 Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090071 throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
72 }
73
74 // Check the ports.
75 if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) {
Nathan Harold702247162017-12-06 19:07:32 -080076 Log.e(TAG, "Invalid ports in KeepalivePacketData");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090077 throw new InvalidPacketException(ERROR_INVALID_PORT);
78 }
79 }
80
Nathan Harold702247162017-12-06 19:07:32 -080081 public byte[] getPacket() {
82 return mPacket.clone();
83 }
84
Nathan Harold702247162017-12-06 19:07:32 -080085 /* Parcelable Implementation */
86 public int describeContents() {
87 return 0;
88 }
89
90 /** Write to parcel */
91 public void writeToParcel(Parcel out, int flags) {
92 out.writeString(srcAddress.getHostAddress());
93 out.writeString(dstAddress.getHostAddress());
94 out.writeInt(srcPort);
95 out.writeInt(dstPort);
96 out.writeByteArray(mPacket);
97 }
98
markchien150e1912018-12-27 22:49:51 +080099 protected KeepalivePacketData(Parcel in) {
Nathan Harold702247162017-12-06 19:07:32 -0800100 srcAddress = NetworkUtils.numericToInetAddress(in.readString());
101 dstAddress = NetworkUtils.numericToInetAddress(in.readString());
102 srcPort = in.readInt();
103 dstPort = in.readInt();
104 mPacket = in.createByteArray();
105 }
106
107 /** Parcelable Creator */
108 public static final Parcelable.Creator<KeepalivePacketData> CREATOR =
109 new Parcelable.Creator<KeepalivePacketData>() {
110 public KeepalivePacketData createFromParcel(Parcel in) {
111 return new KeepalivePacketData(in);
112 }
113
114 public KeepalivePacketData[] newArray(int size) {
115 return new KeepalivePacketData[size];
116 }
117 };
118
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900119}