blob: e21cb44f72d8442a616e181e8c0b291ec1e6b73e [file] [log] [blame]
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001/*
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 Harold26de1d32017-11-02 21:01:46 -070017package android.net;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090018
Aaron Huang17c660d2019-10-02 01:39:46 +080019import static android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS;
20import static android.net.InvalidPacketException.ERROR_INVALID_PORT;
Nathan Harold63dd8132018-02-14 13:09:45 -080021
Aaron Huang87453842020-03-18 19:24:31 +080022import android.annotation.IntRange;
Aaron Huang17c660d2019-10-02 01:39:46 +080023import android.annotation.NonNull;
24import android.annotation.SystemApi;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090025import android.net.util.IpUtils;
Nathan Harold702247162017-12-06 19:07:32 -080026import android.util.Log;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090027
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090028import java.net.InetAddress;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090029
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090030/**
31 * Represents the actual packets that are sent by the
junyulai06835112019-01-03 18:50:15 +080032 * {@link android.net.SocketKeepalive} API.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090033 * @hide
34 */
Aaron Huang17c660d2019-10-02 01:39:46 +080035@SystemApi
36public class KeepalivePacketData {
Nathan Harold702247162017-12-06 19:07:32 -080037 private static final String TAG = "KeepalivePacketData";
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090038
39 /** Source IP address */
Aaron Huang17c660d2019-10-02 01:39:46 +080040 @NonNull
Aaron Huang87453842020-03-18 19:24:31 +080041 private final InetAddress mSrcAddress;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090042
43 /** Destination IP address */
Aaron Huang17c660d2019-10-02 01:39:46 +080044 @NonNull
Aaron Huang87453842020-03-18 19:24:31 +080045 private final InetAddress mDstAddress;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090046
47 /** Source port */
Aaron Huang87453842020-03-18 19:24:31 +080048 private final int mSrcPort;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090049
50 /** Destination port */
Aaron Huang87453842020-03-18 19:24:31 +080051 private final int mDstPort;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090052
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090053 /** Packet data. A raw byte string of packet data, not including the link-layer header. */
Nathan Harold702247162017-12-06 19:07:32 -080054 private final byte[] mPacket;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090055
Roshan Piusb6361f92020-02-21 07:37:30 -080056 // Note: If you add new fields, please modify the parcelling code in the child classes.
57
58
Nathan Harold702247162017-12-06 19:07:32 -080059 // This should only be constructed via static factory methods, such as
Aaron Huang17c660d2019-10-02 01:39:46 +080060 // nattKeepalivePacket.
61 /**
62 * A holding class for data necessary to build a keepalive packet.
63 */
Aaron Huang87453842020-03-18 19:24:31 +080064 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 Harold702247162017-12-06 19:07:32 -080072 this.mPacket = data;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090073
74 // Check we have two IP addresses of the same family.
Nathan Harold702247162017-12-06 19:07:32 -080075 if (srcAddress == null || dstAddress == null || !srcAddress.getClass().getName()
76 .equals(dstAddress.getClass().getName())) {
77 Log.e(TAG, "Invalid or mismatched InetAddresses in KeepalivePacketData");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090078 throw new InvalidPacketException(ERROR_INVALID_IP_ADDRESS);
79 }
80
81 // Check the ports.
82 if (!IpUtils.isValidUdpOrTcpPort(srcPort) || !IpUtils.isValidUdpOrTcpPort(dstPort)) {
Nathan Harold702247162017-12-06 19:07:32 -080083 Log.e(TAG, "Invalid ports in KeepalivePacketData");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090084 throw new InvalidPacketException(ERROR_INVALID_PORT);
85 }
86 }
87
Aaron Huang87453842020-03-18 19:24:31 +080088 /** 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 Huang17c660d2019-10-02 01:39:46 +0800113 @NonNull
Nathan Harold702247162017-12-06 19:07:32 -0800114 public byte[] getPacket() {
115 return mPacket.clone();
116 }
117
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900118}