blob: 2b8b7e69dec99c2cde7e6620f4d59ccbdbecbeb2 [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
Aaron Huang17c660d2019-10-02 01:39:46 +080019import static android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS;
20import static android.net.InvalidPacketException.ERROR_INVALID_PORT;
Nathan Harold63dd8132018-02-14 13:09:45 -080021
Aaron Huang17c660d2019-10-02 01:39:46 +080022import android.annotation.NonNull;
23import android.annotation.SystemApi;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090024import android.net.util.IpUtils;
Nathan Harold702247162017-12-06 19:07:32 -080025import android.os.Parcel;
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 */
Aaron Huang17c660d2019-10-02 01:39:46 +080036@SystemApi
37public class KeepalivePacketData {
Nathan Harold702247162017-12-06 19:07:32 -080038 private static final String TAG = "KeepalivePacketData";
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090039
40 /** Source IP address */
Aaron Huang17c660d2019-10-02 01:39:46 +080041 @NonNull
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090042 public final InetAddress srcAddress;
43
44 /** Destination IP address */
Aaron Huang17c660d2019-10-02 01:39:46 +080045 @NonNull
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090046 public final InetAddress dstAddress;
47
48 /** Source port */
49 public final int srcPort;
50
51 /** Destination port */
52 public final int dstPort;
53
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090054 /** Packet data. A raw byte string of packet data, not including the link-layer header. */
Nathan Harold702247162017-12-06 19:07:32 -080055 private final byte[] mPacket;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090056
Nathan Harold702247162017-12-06 19:07:32 -080057 // This should only be constructed via static factory methods, such as
Aaron Huang17c660d2019-10-02 01:39:46 +080058 // nattKeepalivePacket.
59 /**
60 * A holding class for data necessary to build a keepalive packet.
61 */
62 protected KeepalivePacketData(@NonNull InetAddress srcAddress, int srcPort,
63 @NonNull InetAddress dstAddress, int dstPort,
64 @NonNull byte[] data) throws InvalidPacketException {
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090065 this.srcAddress = srcAddress;
66 this.dstAddress = dstAddress;
67 this.srcPort = srcPort;
68 this.dstPort = dstPort;
Nathan Harold702247162017-12-06 19:07:32 -080069 this.mPacket = data;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090070
71 // Check we have two IP addresses of the same family.
Nathan Harold702247162017-12-06 19:07:32 -080072 if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName()
73 .equals(dstAddress.getClass().getName())) {
74 Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090075 throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
76 }
77
78 // Check the ports.
79 if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) {
Nathan Harold702247162017-12-06 19:07:32 -080080 Log.e(TAG, "Invalid ports in KeepalivePacketData");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090081 throw new InvalidPacketException(ERROR_INVALID_PORT);
82 }
83 }
84
Aaron Huang17c660d2019-10-02 01:39:46 +080085 @NonNull
Nathan Harold702247162017-12-06 19:07:32 -080086 public byte[] getPacket() {
87 return mPacket.clone();
88 }
89
Aaron Huang17c660d2019-10-02 01:39:46 +080090 /** @hide */
Nathan Harold702247162017-12-06 19:07:32 -080091 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
Aaron Huang17c660d2019-10-02 01:39:46 +080099 /** @hide */
markchien150e1912018-12-27 22:49:51 +0800100 protected KeepalivePacketData(Parcel in) {
Nathan Harold702247162017-12-06 19:07:32 -0800101 srcAddress = NetworkUtils.numericToInetAddress(in.readString());
102 dstAddress = NetworkUtils.numericToInetAddress(in.readString());
103 srcPort = in.readInt();
104 dstPort = in.readInt();
105 mPacket = in.createByteArray();
106 }
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900107}