Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1 | /* |
| 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 Harold | 583c95b | 2017-11-02 21:01:46 -0700 | [diff] [blame] | 17 | package android.net; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 18 | |
Aaron Huang | 441e499 | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 19 | import static android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS; |
| 20 | import static android.net.InvalidPacketException.ERROR_INVALID_PORT; |
Nathan Harold | 0990bc8 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 21 | |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 22 | import android.annotation.IntRange; |
Aaron Huang | 441e499 | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.SystemApi; |
Nathan Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 26 | |
Remi NGUYEN VAN | 7f3a751 | 2020-09-24 18:31:55 +0900 | [diff] [blame] | 27 | import com.android.net.module.util.IpUtils; |
| 28 | |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 29 | import java.net.InetAddress; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 30 | |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 31 | /** |
| 32 | * Represents the actual packets that are sent by the |
junyulai | 011b1f1 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 33 | * {@link android.net.SocketKeepalive} API. |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 34 | * @hide |
| 35 | */ |
Aaron Huang | 441e499 | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 36 | @SystemApi |
| 37 | public class KeepalivePacketData { |
Nathan Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 38 | private static final String TAG = "KeepalivePacketData"; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 39 | |
| 40 | /** Source IP address */ |
Aaron Huang | 441e499 | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 41 | @NonNull |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 42 | private final InetAddress mSrcAddress; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 43 | |
| 44 | /** Destination IP address */ |
Aaron Huang | 441e499 | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 45 | @NonNull |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 46 | private final InetAddress mDstAddress; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 47 | |
| 48 | /** Source port */ |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 49 | private final int mSrcPort; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 50 | |
| 51 | /** Destination port */ |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 52 | private final int mDstPort; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 53 | |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 54 | /** Packet data. A raw byte string of packet data, not including the link-layer header. */ |
Nathan Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 55 | private final byte[] mPacket; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 56 | |
Roshan Pius | fae5822 | 2020-02-21 07:37:30 -0800 | [diff] [blame] | 57 | // Note: If you add new fields, please modify the parcelling code in the child classes. |
| 58 | |
| 59 | |
Nathan Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 60 | // This should only be constructed via static factory methods, such as |
Aaron Huang | 441e499 | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 61 | // nattKeepalivePacket. |
| 62 | /** |
| 63 | * A holding class for data necessary to build a keepalive packet. |
| 64 | */ |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 65 | 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 Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 73 | this.mPacket = data; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 74 | |
| 75 | // Check we have two IP addresses of the same family. |
Nathan Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 76 | if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName() |
| 77 | .equals(dstAddress.getClass().getName())) { |
| 78 | Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData"); |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 79 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
| 80 | } |
| 81 | |
| 82 | // Check the ports. |
| 83 | if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) { |
Nathan Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 84 | Log.e(TAG, "Invalid ports in KeepalivePacketData"); |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 85 | throw new InvalidPacketException(ERROR_INVALID_PORT); |
| 86 | } |
| 87 | } |
| 88 | |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 89 | /** 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 Huang | 441e499 | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 114 | @NonNull |
Nathan Harold | 7f8d0be | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 115 | public byte[] getPacket() { |
| 116 | return mPacket.clone(); |
| 117 | } |
| 118 | |
Ling Ma | 4d782d6 | 2022-02-11 11:05:35 -0800 | [diff] [blame] | 119 | @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 Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 128 | } |