Lorenzo Colitti | 8bf977d | 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 | 26de1d3 | 2017-11-02 21:01:46 -0700 | [diff] [blame] | 17 | package android.net; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 18 | |
Aaron Huang | 17c660d | 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 | 63dd813 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 21 | |
Aaron Huang | 8745384 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 22 | import android.annotation.IntRange; |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.SystemApi; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 25 | import android.net.util.IpUtils; |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 26 | import android.util.Log; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 27 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 28 | import java.net.InetAddress; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 29 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 30 | /** |
| 31 | * Represents the actual packets that are sent by the |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 32 | * {@link android.net.SocketKeepalive} API. |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 33 | * @hide |
| 34 | */ |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 35 | @SystemApi |
| 36 | public class KeepalivePacketData { |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 37 | private static final String TAG = "KeepalivePacketData"; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 38 | |
| 39 | /** Source IP address */ |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 40 | @NonNull |
Aaron Huang | 8745384 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 41 | private final InetAddress mSrcAddress; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 42 | |
| 43 | /** Destination IP address */ |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 44 | @NonNull |
Aaron Huang | 8745384 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 45 | private final InetAddress mDstAddress; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 46 | |
| 47 | /** Source port */ |
Aaron Huang | 8745384 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 48 | private final int mSrcPort; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 49 | |
| 50 | /** Destination port */ |
Aaron Huang | 8745384 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 51 | private final int mDstPort; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 52 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 53 | /** Packet data. A raw byte string of packet data, not including the link-layer header. */ |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 54 | private final byte[] mPacket; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 55 | |
Roshan Pius | b6361f9 | 2020-02-21 07:37:30 -0800 | [diff] [blame] | 56 | // Note: If you add new fields, please modify the parcelling code in the child classes. |
| 57 | |
| 58 | |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 59 | // This should only be constructed via static factory methods, such as |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 60 | // nattKeepalivePacket. |
| 61 | /** |
| 62 | * A holding class for data necessary to build a keepalive packet. |
| 63 | */ |
Aaron Huang | 8745384 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 64 | protected KeepalivePacketData(@NonNull InetAddress srcAddress, |
| 65 | @IntRange(from = 0, to = 65535) int srcPort, @NonNull InetAddress dstAddress, |
| 66 | @IntRange(from = 0, to = 65535) int dstPort, |
| 67 | @NonNull byte[] data) throws InvalidPacketException { |
| 68 | this.mSrcAddress = srcAddress; |
| 69 | this.mDstAddress = dstAddress; |
| 70 | this.mSrcPort = srcPort; |
| 71 | this.mDstPort = dstPort; |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 72 | this.mPacket = data; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 73 | |
| 74 | // Check we have two IP addresses of the same family. |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 75 | if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName() |
| 76 | .equals(dstAddress.getClass().getName())) { |
| 77 | Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 78 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
| 79 | } |
| 80 | |
| 81 | // Check the ports. |
| 82 | if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) { |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 83 | Log.e(TAG, "Invalid ports in KeepalivePacketData"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 84 | throw new InvalidPacketException(ERROR_INVALID_PORT); |
| 85 | } |
| 86 | } |
| 87 | |
Aaron Huang | 8745384 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 88 | /** Get source IP address. */ |
| 89 | @NonNull |
| 90 | public InetAddress getSrcAddress() { |
| 91 | return mSrcAddress; |
| 92 | } |
| 93 | |
| 94 | /** Get destination IP address. */ |
| 95 | @NonNull |
| 96 | public InetAddress getDstAddress() { |
| 97 | return mDstAddress; |
| 98 | } |
| 99 | |
| 100 | /** Get source port number. */ |
| 101 | public int getSrcPort() { |
| 102 | return mSrcPort; |
| 103 | } |
| 104 | |
| 105 | /** Get destination port number. */ |
| 106 | public int getDstPort() { |
| 107 | return mDstPort; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns a byte array of the given packet data. |
| 112 | */ |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame] | 113 | @NonNull |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 114 | public byte[] getPacket() { |
| 115 | return mPacket.clone(); |
| 116 | } |
| 117 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 118 | } |