blob: 95a8d8f9df59be7529bb81ad1f99b0655f71640d [file] [log] [blame]
alshabibc4901cd2014-09-05 16:50:40 -07001/*******************************************************************************
2 * Copyright 2014 Open Networking Laboratory
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 * Copyright 2011, Big Switch Networks, Inc.
18 * Originally created by David Erickson, Stanford University
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License. You may obtain
22 * a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 * License for the specific language governing permissions and limitations
30 * under the License.
31 **/
32
33package org.onlab.packet;
34
35import java.nio.ByteBuffer;
36
37/**
38 *
39 * @author shudong.zhou@bigswitch.com
40 */
41
42public class TCP extends BasePacket {
43 protected short sourcePort;
44 protected short destinationPort;
45 protected int sequence;
46 protected int acknowledge;
47 protected byte dataOffset;
48 protected short flags;
49 protected short windowSize;
50 protected short checksum;
51 protected short urgentPointer;
52 protected byte[] options;
53
54 /**
55 * @return the sourcePort
56 */
57 public short getSourcePort() {
58 return this.sourcePort;
59 }
60
61 /**
62 * @param sourcePort
63 * the sourcePort to set
64 */
65 public TCP setSourcePort(final short sourcePort) {
66 this.sourcePort = sourcePort;
67 return this;
68 }
69
70 /**
71 * @return the destinationPort
72 */
73 public short getDestinationPort() {
74 return this.destinationPort;
75 }
76
77 /**
78 * @param destinationPort
79 * the destinationPort to set
80 */
81 public TCP setDestinationPort(final short destinationPort) {
82 this.destinationPort = destinationPort;
83 return this;
84 }
85
86 /**
87 * @return the checksum
88 */
89 public short getChecksum() {
90 return this.checksum;
91 }
92
93 public int getSequence() {
94 return this.sequence;
95 }
96
97 public TCP setSequence(final int seq) {
98 this.sequence = seq;
99 return this;
100 }
101
102 public int getAcknowledge() {
103 return this.acknowledge;
104 }
105
106 public TCP setAcknowledge(final int ack) {
107 this.acknowledge = ack;
108 return this;
109 }
110
111 public byte getDataOffset() {
112 return this.dataOffset;
113 }
114
115 public TCP setDataOffset(final byte offset) {
116 this.dataOffset = offset;
117 return this;
118 }
119
120 public short getFlags() {
121 return this.flags;
122 }
123
124 public TCP setFlags(final short flags) {
125 this.flags = flags;
126 return this;
127 }
128
129 public short getWindowSize() {
130 return this.windowSize;
131 }
132
133 public TCP setWindowSize(final short windowSize) {
134 this.windowSize = windowSize;
135 return this;
136 }
137
138 public short getTcpChecksum() {
139 return this.checksum;
140 }
141
142 public TCP setTcpChecksum(final short checksum) {
143 this.checksum = checksum;
144 return this;
145 }
146
147 @Override
148 public void resetChecksum() {
149 this.checksum = 0;
150 super.resetChecksum();
151 }
152
153 public short getUrgentPointer(final short urgentPointer) {
154 return this.urgentPointer;
155 }
156
157 public TCP setUrgentPointer(final short urgentPointer) {
158 this.urgentPointer = urgentPointer;
159 return this;
160 }
161
162 public byte[] getOptions() {
163 return this.options;
164 }
165
166 public TCP setOptions(final byte[] options) {
167 this.options = options;
168 this.dataOffset = (byte) (20 + options.length + 3 >> 2);
169 return this;
170 }
171
172 /**
173 * @param checksum
174 * the checksum to set
175 */
176 public TCP setChecksum(final short checksum) {
177 this.checksum = checksum;
178 return this;
179 }
180
181 /**
182 * Serializes the packet. Will compute and set the following fields if they
183 * are set to specific values at the time serialize is called: -checksum : 0
184 * -length : 0
185 */
186 @Override
187 public byte[] serialize() {
188 int length;
189 if (this.dataOffset == 0) {
190 this.dataOffset = 5; // default header length
191 }
192 length = this.dataOffset << 2;
193 byte[] payloadData = null;
194 if (this.payload != null) {
195 this.payload.setParent(this);
196 payloadData = this.payload.serialize();
197 length += payloadData.length;
198 }
199
200 final byte[] data = new byte[length];
201 final ByteBuffer bb = ByteBuffer.wrap(data);
202
203 bb.putShort(this.sourcePort);
204 bb.putShort(this.destinationPort);
205 bb.putInt(this.sequence);
206 bb.putInt(this.acknowledge);
207 bb.putShort((short) (this.flags | this.dataOffset << 12));
208 bb.putShort(this.windowSize);
209 bb.putShort(this.checksum);
210 bb.putShort(this.urgentPointer);
211 if (this.dataOffset > 5) {
212 int padding;
213 bb.put(this.options);
214 padding = (this.dataOffset << 2) - 20 - this.options.length;
215 for (int i = 0; i < padding; i++) {
216 bb.put((byte) 0);
217 }
218 }
219 if (payloadData != null) {
220 bb.put(payloadData);
221 }
222
223 if (this.parent != null && this.parent instanceof IPv4) {
224 ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_TCP);
225 }
226
227 // compute checksum if needed
228 if (this.checksum == 0) {
229 bb.rewind();
230 int accumulation = 0;
231
232 // compute pseudo header mac
233 if (this.parent != null && this.parent instanceof IPv4) {
234 final IPv4 ipv4 = (IPv4) this.parent;
235 accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff)
236 + (ipv4.getSourceAddress() & 0xffff);
237 accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff)
238 + (ipv4.getDestinationAddress() & 0xffff);
239 accumulation += ipv4.getProtocol() & 0xff;
240 accumulation += length & 0xffff;
241 }
242
243 for (int i = 0; i < length / 2; ++i) {
244 accumulation += 0xffff & bb.getShort();
245 }
246 // pad to an even number of shorts
247 if (length % 2 > 0) {
248 accumulation += (bb.get() & 0xff) << 8;
249 }
250
251 accumulation = (accumulation >> 16 & 0xffff)
252 + (accumulation & 0xffff);
253 this.checksum = (short) (~accumulation & 0xffff);
254 bb.putShort(16, this.checksum);
255 }
256 return data;
257 }
258
259 /*
260 * (non-Javadoc)
261 *
262 * @see java.lang.Object#hashCode()
263 */
264 @Override
265 public int hashCode() {
266 final int prime = 5807;
267 int result = super.hashCode();
268 result = prime * result + this.checksum;
269 result = prime * result + this.destinationPort;
270 result = prime * result + this.sourcePort;
271 return result;
272 }
273
274 /*
275 * (non-Javadoc)
276 *
277 * @see java.lang.Object#equals(java.lang.Object)
278 */
279 @Override
280 public boolean equals(final Object obj) {
281 if (this == obj) {
282 return true;
283 }
284 if (!super.equals(obj)) {
285 return false;
286 }
287 if (!(obj instanceof TCP)) {
288 return false;
289 }
290 final TCP other = (TCP) obj;
291 // May want to compare fields based on the flags set
292 return this.checksum == other.checksum
293 && this.destinationPort == other.destinationPort
294 && this.sourcePort == other.sourcePort
295 && this.sequence == other.sequence
296 && this.acknowledge == other.acknowledge
297 && this.dataOffset == other.dataOffset
298 && this.flags == other.flags
299 && this.windowSize == other.windowSize
300 && this.urgentPointer == other.urgentPointer
301 && (this.dataOffset == 5 || this.options.equals(other.options));
302 }
303
304 @Override
305 public IPacket deserialize(final byte[] data, final int offset,
306 final int length) {
307 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
308 this.sourcePort = bb.getShort();
309 this.destinationPort = bb.getShort();
310 this.sequence = bb.getInt();
311 this.acknowledge = bb.getInt();
312 this.flags = bb.getShort();
313 this.dataOffset = (byte) (this.flags >> 12 & 0xf);
314 this.flags = (short) (this.flags & 0x1ff);
315 this.windowSize = bb.getShort();
316 this.checksum = bb.getShort();
317 this.urgentPointer = bb.getShort();
318 if (this.dataOffset > 5) {
319 int optLength = (this.dataOffset << 2) - 20;
320 if (bb.limit() < bb.position() + optLength) {
321 optLength = bb.limit() - bb.position();
322 }
323 try {
324 this.options = new byte[optLength];
325 bb.get(this.options, 0, optLength);
326 } catch (final IndexOutOfBoundsException e) {
327 this.options = null;
328 }
329 }
330
331 this.payload = new Data();
332 this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
333 - bb.position());
334 this.payload.setParent(this);
335 return this;
336 }
337}