blob: f47cc5cd631d4fc5248cc7a8ce17a93ecace1888 [file] [log] [blame]
Lorenzo Colitti0b798a82015-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 Harold583c95b2017-11-02 21:01:46 -070017package android.net;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090018
Aaron Huang441e4992019-10-02 01:39:46 +080019import static android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS;
20import static android.net.InvalidPacketException.ERROR_INVALID_PORT;
Nathan Harold0990bc82018-02-14 13:09:45 -080021
Aaron Huang005f9d12020-03-18 19:24:31 +080022import android.annotation.IntRange;
Aaron Huang441e4992019-10-02 01:39:46 +080023import android.annotation.NonNull;
24import android.annotation.SystemApi;
Nathan Harold7f8d0be2017-12-06 19:07:32 -080025import android.util.Log;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090026
Remi NGUYEN VAN7f3a7512020-09-24 18:31:55 +090027import com.android.net.module.util.IpUtils;
28
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090029import java.net.InetAddress;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090030
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090031/**
32 * Represents the actual packets that are sent by the
junyulai011b1f12019-01-03 18:50:15 +080033 * {@link android.net.SocketKeepalive} API.
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090034 * @hide
35 */
Aaron Huang441e4992019-10-02 01:39:46 +080036@SystemApi
37public class KeepalivePacketData {
Nathan Harold7f8d0be2017-12-06 19:07:32 -080038 private static final String TAG = "KeepalivePacketData";
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090039
40 /** Source IP address */
Aaron Huang441e4992019-10-02 01:39:46 +080041 @NonNull
Aaron Huang005f9d12020-03-18 19:24:31 +080042 private final InetAddress mSrcAddress;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090043
44 /** Destination IP address */
Aaron Huang441e4992019-10-02 01:39:46 +080045 @NonNull
Aaron Huang005f9d12020-03-18 19:24:31 +080046 private final InetAddress mDstAddress;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090047
48 /** Source port */
Aaron Huang005f9d12020-03-18 19:24:31 +080049 private final int mSrcPort;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090050
51 /** Destination port */
Aaron Huang005f9d12020-03-18 19:24:31 +080052 private final int mDstPort;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090053
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090054 /** Packet data. A raw byte string of packet data, not including the link-layer header. */
Nathan Harold7f8d0be2017-12-06 19:07:32 -080055 private final byte[] mPacket;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090056
Roshan Piusfae58222020-02-21 07:37:30 -080057 // Note: If you add new fields, please modify the parcelling code in the child classes.
58
59
Nathan Harold7f8d0be2017-12-06 19:07:32 -080060 // This should only be constructed via static factory methods, such as
Aaron Huang441e4992019-10-02 01:39:46 +080061 // nattKeepalivePacket.
62 /**
63 * A holding class for data necessary to build a keepalive packet.
64 */
Aaron Huang005f9d12020-03-18 19:24:31 +080065 protected KeepalivePacketData(@NonNull InetAddress srcAddress,
66 @IntRange(from = 0, to = 65535) int srcPort, @NonNull InetAddress dstAddress,
67 @IntRange(from = 0, to = 65535) int dstPort,
68 @NonNull byte[] data) throws InvalidPacketException {
69 this.mSrcAddress = srcAddress;
70 this.mDstAddress = dstAddress;
71 this.mSrcPort = srcPort;
72 this.mDstPort = dstPort;
Nathan Harold7f8d0be2017-12-06 19:07:32 -080073 this.mPacket = data;
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090074
75 // Check we have two IP addresses of the same family.
Nathan Harold7f8d0be2017-12-06 19:07:32 -080076 if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName()
77 .equals(dstAddress.getClass().getName())) {
78 Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData");
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090079 throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
80 }
81
82 // Check the ports.
83 if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) {
Nathan Harold7f8d0be2017-12-06 19:07:32 -080084 Log.e(TAG, "Invalid ports in KeepalivePacketData");
Lorenzo Colitti0b798a82015-06-15 14:29:22 +090085 throw new InvalidPacketException(ERROR_INVALID_PORT);
86 }
87 }
88
Aaron Huang005f9d12020-03-18 19:24:31 +080089 /** Get source IP address. */
90 @NonNull
91 public InetAddress getSrcAddress() {
92 return mSrcAddress;
93 }
94
95 /** Get destination IP address. */
96 @NonNull
97 public InetAddress getDstAddress() {
98 return mDstAddress;
99 }
100
101 /** Get source port number. */
102 public int getSrcPort() {
103 return mSrcPort;
104 }
105
106 /** Get destination port number. */
107 public int getDstPort() {
108 return mDstPort;
109 }
110
111 /**
112 * Returns a byte array of the given packet data.
113 */
Aaron Huang441e4992019-10-02 01:39:46 +0800114 @NonNull
Nathan Harold7f8d0be2017-12-06 19:07:32 -0800115 public byte[] getPacket() {
116 return mPacket.clone();
117 }
118
Ling Ma4d782d62022-02-11 11:05:35 -0800119 @Override
120 public String toString() {
121 return "KeepalivePacketData[srcAddress=" + mSrcAddress
122 + ", dstAddress=" + mDstAddress
123 + ", srcPort=" + mSrcPort
124 + ", dstPort=" + mDstPort
125 + ", packet.length=" + mPacket.length
126 + ']';
127 }
Lorenzo Colitti0b798a82015-06-15 14:29:22 +0900128}