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 | |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 19 | import static android.net.SocketKeepalive.ERROR_INVALID_IP_ADDRESS; |
| 20 | import static android.net.SocketKeepalive.ERROR_INVALID_PORT; |
Nathan Harold | 63dd813 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 21 | |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 22 | import android.net.SocketKeepalive.InvalidPacketException; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 23 | import android.net.util.IpUtils; |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
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 | */ |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 36 | public class KeepalivePacketData implements Parcelable { |
| 37 | private static final String TAG = "KeepalivePacketData"; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 38 | |
| 39 | /** Source IP address */ |
| 40 | public final InetAddress srcAddress; |
| 41 | |
| 42 | /** Destination IP address */ |
| 43 | public final InetAddress dstAddress; |
| 44 | |
| 45 | /** Source port */ |
| 46 | public final int srcPort; |
| 47 | |
| 48 | /** Destination port */ |
| 49 | public final int dstPort; |
| 50 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 51 | /** 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] | 52 | private final byte[] mPacket; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 53 | |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 54 | protected static final int IPV4_HEADER_LENGTH = 20; |
| 55 | protected static final int UDP_HEADER_LENGTH = 8; |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 56 | |
| 57 | // This should only be constructed via static factory methods, such as |
| 58 | // nattKeepalivePacket |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 59 | protected KeepalivePacketData(InetAddress srcAddress, int srcPort, |
| 60 | InetAddress dstAddress, int dstPort, byte[] data) throws InvalidPacketException { |
| 61 | this.srcAddress = srcAddress; |
| 62 | this.dstAddress = dstAddress; |
| 63 | this.srcPort = srcPort; |
| 64 | this.dstPort = dstPort; |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 65 | this.mPacket = data; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 66 | |
| 67 | // Check we have two IP addresses of the same family. |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 68 | if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName() |
| 69 | .equals(dstAddress.getClass().getName())) { |
| 70 | Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 71 | throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS); |
| 72 | } |
| 73 | |
| 74 | // Check the ports. |
| 75 | if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) { |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 76 | Log.e(TAG, "Invalid ports in KeepalivePacketData"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 77 | throw new InvalidPacketException(ERROR_INVALID_PORT); |
| 78 | } |
| 79 | } |
| 80 | |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 81 | public byte[] getPacket() { |
| 82 | return mPacket.clone(); |
| 83 | } |
| 84 | |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 85 | /* Parcelable Implementation */ |
| 86 | public int describeContents() { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /** Write to parcel */ |
| 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 | |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 99 | protected KeepalivePacketData(Parcel in) { |
Nathan Harold | 70224716 | 2017-12-06 19:07:32 -0800 | [diff] [blame] | 100 | srcAddress = NetworkUtils.numericToInetAddress(in.readString()); |
| 101 | dstAddress = NetworkUtils.numericToInetAddress(in.readString()); |
| 102 | srcPort = in.readInt(); |
| 103 | dstPort = in.readInt(); |
| 104 | mPacket = in.createByteArray(); |
| 105 | } |
| 106 | |
| 107 | /** Parcelable Creator */ |
| 108 | public static final Parcelable.Creator<KeepalivePacketData> CREATOR = |
| 109 | new Parcelable.Creator<KeepalivePacketData>() { |
| 110 | public KeepalivePacketData createFromParcel(Parcel in) { |
| 111 | return new KeepalivePacketData(in); |
| 112 | } |
| 113 | |
| 114 | public KeepalivePacketData[] newArray(int size) { |
| 115 | return new KeepalivePacketData[size]; |
| 116 | } |
| 117 | }; |
| 118 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 119 | } |