blob: ddb3a6a72fb404455aeaa2edf2729a8e0fb2ebce [file] [log] [blame]
Remi NGUYEN VAN11511b32020-11-10 15:29:01 +09001/*
2 * Copyright (C) 2020 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 */
16package android.net;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.annotation.SystemApi;
21import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.net.InetAddress;
25import java.util.Objects;
26
27/**
28 * Represents the actual tcp keep alive packets which will be used for hardware offload.
29 * @hide
30 */
31@SystemApi
32public final class TcpKeepalivePacketData extends KeepalivePacketData implements Parcelable {
33 private static final String TAG = "TcpKeepalivePacketData";
34
35 /** TCP sequence number. */
36 public final int tcpSeq;
37
38 /** TCP ACK number. */
39 public final int tcpAck;
40
41 /** TCP RCV window. */
42 public final int tcpWindow;
43
44 /** TCP RCV window scale. */
45 public final int tcpWindowScale;
46
47 /** IP TOS. */
48 public final int ipTos;
49
50 /** IP TTL. */
51 public final int ipTtl;
52
53 public TcpKeepalivePacketData(@NonNull final InetAddress srcAddress, int srcPort,
54 @NonNull final InetAddress dstAddress, int dstPort, @NonNull final byte[] data,
55 int tcpSeq, int tcpAck, int tcpWindow, int tcpWindowScale, int ipTos, int ipTtl)
56 throws InvalidPacketException {
57 super(srcAddress, srcPort, dstAddress, dstPort, data);
58 this.tcpSeq = tcpSeq;
59 this.tcpAck = tcpAck;
60 this.tcpWindow = tcpWindow;
61 this.tcpWindowScale = tcpWindowScale;
62 this.ipTos = ipTos;
63 this.ipTtl = ipTtl;
64 }
65
66 @Override
67 public boolean equals(@Nullable final Object o) {
68 if (!(o instanceof TcpKeepalivePacketData)) return false;
69 final TcpKeepalivePacketData other = (TcpKeepalivePacketData) o;
70 final InetAddress srcAddress = getSrcAddress();
71 final InetAddress dstAddress = getDstAddress();
72 return srcAddress.equals(other.getSrcAddress())
73 && dstAddress.equals(other.getDstAddress())
74 && getSrcPort() == other.getSrcPort()
75 && getDstPort() == other.getDstPort()
76 && this.tcpAck == other.tcpAck
77 && this.tcpSeq == other.tcpSeq
78 && this.tcpWindow == other.tcpWindow
79 && this.tcpWindowScale == other.tcpWindowScale
80 && this.ipTos == other.ipTos
81 && this.ipTtl == other.ipTtl;
82 }
83
84 @Override
85 public int hashCode() {
86 return Objects.hash(getSrcAddress(), getDstAddress(), getSrcPort(), getDstPort(),
87 tcpAck, tcpSeq, tcpWindow, tcpWindowScale, ipTos, ipTtl);
88 }
89
90 /**
91 * Parcelable Implementation.
92 * Note that this object implements parcelable (and needs to keep doing this as it inherits
93 * from a class that does), but should usually be parceled as a stable parcelable using
94 * the toStableParcelable() and fromStableParcelable() methods.
95 */
96 @Override
97 public int describeContents() {
98 return 0;
99 }
100
101 /** Write to parcel. */
102 @Override
103 public void writeToParcel(@NonNull Parcel out, int flags) {
104 out.writeString(getSrcAddress().getHostAddress());
105 out.writeString(getDstAddress().getHostAddress());
106 out.writeInt(getSrcPort());
107 out.writeInt(getDstPort());
108 out.writeByteArray(getPacket());
109 out.writeInt(tcpSeq);
110 out.writeInt(tcpAck);
111 out.writeInt(tcpWindow);
112 out.writeInt(tcpWindowScale);
113 out.writeInt(ipTos);
114 out.writeInt(ipTtl);
115 }
116
117 private static TcpKeepalivePacketData readFromParcel(Parcel in) throws InvalidPacketException {
118 InetAddress srcAddress = InetAddresses.parseNumericAddress(in.readString());
119 InetAddress dstAddress = InetAddresses.parseNumericAddress(in.readString());
120 int srcPort = in.readInt();
121 int dstPort = in.readInt();
122 byte[] packet = in.createByteArray();
123 int tcpSeq = in.readInt();
124 int tcpAck = in.readInt();
125 int tcpWnd = in.readInt();
126 int tcpWndScale = in.readInt();
127 int ipTos = in.readInt();
128 int ipTtl = in.readInt();
129 return new TcpKeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, packet, tcpSeq,
130 tcpAck, tcpWnd, tcpWndScale, ipTos, ipTtl);
131 }
132
133 /** Parcelable Creator. */
134 public static final @NonNull Parcelable.Creator<TcpKeepalivePacketData> CREATOR =
135 new Parcelable.Creator<TcpKeepalivePacketData>() {
136 public TcpKeepalivePacketData createFromParcel(Parcel in) {
137 try {
138 return readFromParcel(in);
139 } catch (InvalidPacketException e) {
140 throw new IllegalArgumentException(
141 "Invalid TCP keepalive data: " + e.getError());
142 }
143 }
144
145 public TcpKeepalivePacketData[] newArray(int size) {
146 return new TcpKeepalivePacketData[size];
147 }
148 };
149
150 @Override
151 public String toString() {
152 return "saddr: " + getSrcAddress()
153 + " daddr: " + getDstAddress()
154 + " sport: " + getSrcPort()
155 + " dport: " + getDstPort()
156 + " seq: " + tcpSeq
157 + " ack: " + tcpAck
158 + " window: " + tcpWindow
159 + " windowScale: " + tcpWindowScale
160 + " tos: " + ipTos
161 + " ttl: " + ipTtl;
162 }
163}