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