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