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 | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.SystemApi; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 24 | import android.net.util.IpUtils; |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 25 | import android.os.Parcel; |
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 | * |
| 34 | * @hide |
| 35 | */ |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 36 | @SystemApi |
| 37 | public class KeepalivePacketData { |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 38 | private static final String TAG = "KeepalivePacketData"; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 39 | |
| 40 | /** Source IP address */ |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 41 | @NonNull |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 42 | public final InetAddress srcAddress; |
| 43 | |
| 44 | /** Destination IP address */ |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 45 | @NonNull |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 46 | public final InetAddress dstAddress; |
| 47 | |
| 48 | /** Source port */ |
| 49 | public final int srcPort; |
| 50 | |
| 51 | /** Destination port */ |
| 52 | public final int dstPort; |
| 53 | |
Lorenzo Colitti | 8bf977d | 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 | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 55 | private final byte[] mPacket; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 56 | |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 57 | // This should only be constructed via static factory methods, such as |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 58 | // 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 Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 65 | this.srcAddress = srcAddress; |
| 66 | this.dstAddress = dstAddress; |
| 67 | this.srcPort = srcPort; |
| 68 | this.dstPort = dstPort; |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 69 | this.mPacket = data; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 70 | |
| 71 | // Check we have two IP addresses of the same family. |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 72 | if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName() |
| 73 | .equals(dstAddress.getClass().getName())) { |
| 74 | Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 75 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
| 76 | } |
| 77 | |
| 78 | // Check the ports. |
| 79 | if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) { |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 80 | Log.e(TAG, "Invalid ports in KeepalivePacketData"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 81 | throw new InvalidPacketException(ERROR_INVALID_PORT); |
| 82 | } |
| 83 | } |
| 84 | |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 85 | @NonNull |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 86 | public byte[] getPacket() { |
| 87 | return mPacket.clone(); |
| 88 | } |
| 89 | |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 90 | /** @hide */ |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 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 | |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 99 | /** @hide */ |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 100 | protected KeepalivePacketData(Parcel in) { |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 101 | 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 Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 107 | } |