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