3 * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.packet;
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
18 import org.opendaylight.controller.sal.match.Match;
19 import org.opendaylight.controller.sal.match.MatchType;
22 * Class that represents the TCP segment objects
25 public class TCP extends Packet {
27 public static final String SRCPORT = "SourcePort";
28 public static final String DESTPORT = "DestinationPort";
29 public static final String SEQNUMBER = "SequenceNumber";
30 public static final String ACKNUMBER = "AcknowledgementNumber";
31 public static final String DATAOFFSET = "DataOffset";
32 public static final String RESERVED = "Reserved";
33 public static final String HEADERLENFLAGS = "HeaderLenFlags";
34 public static final String WINDOWSIZE = "WindowSize";
35 public static final String CHECKSUM = "Checksum";
36 public static final String URGENTPOINTER = "UrgentPointer";
38 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
39 private static final long serialVersionUID = 1L;
41 put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
42 put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
43 put(SEQNUMBER, new ImmutablePair<Integer, Integer>(32, 32));
44 put(ACKNUMBER, new ImmutablePair<Integer, Integer>(64, 32));
45 put(DATAOFFSET, new ImmutablePair<Integer, Integer>(96, 4));
46 put(RESERVED, new ImmutablePair<Integer, Integer>(100, 3));
47 put(HEADERLENFLAGS, new ImmutablePair<Integer, Integer>(103, 9));
48 put(WINDOWSIZE, new ImmutablePair<Integer, Integer>(112, 16));
49 put(CHECKSUM, new ImmutablePair<Integer, Integer>(128, 16));
50 put(URGENTPOINTER, new ImmutablePair<Integer, Integer>(144, 16));
54 private final Map<String, byte[]> fieldValues;
57 * Default constructor that sets all the header fields to zero
61 fieldValues = new HashMap<String, byte[]>();
62 hdrFieldCoordMap = fieldCoordinates;
63 hdrFieldsMap = fieldValues;
64 /* Setting all remaining header field values to
65 * default value of 0. These maybe changed as needed
67 setSourcePort((short) 0);
68 setDestinationPort((short) 0);
71 setDataOffset((byte) 0);
72 setReserved((byte) 0);
73 setWindowSize((short) 0);
74 setUrgentPointer((short) 0);
75 setChecksum((short) 0);
79 * Constructor that sets the access level for the packet and
80 * sets all the header fields to zero.
82 public TCP(boolean writeAccess) {
84 fieldValues = new HashMap<String, byte[]>();
85 hdrFieldCoordMap = fieldCoordinates;
86 hdrFieldsMap = fieldValues;
87 /* Setting all remaining header field values to
88 * default value of 0. These maybe changed as needed
90 setSourcePort((short) 0);
91 setDestinationPort((short) 0);
94 setDataOffset((byte) 0);
95 setReserved((byte) 0);
96 setWindowSize((short) 0);
97 setUrgentPointer((short) 0);
98 setChecksum((short) 0);
103 * Stores the value read from data stream
105 public void setHeaderField(String headerField, byte[] readValue) {
106 hdrFieldsMap.put(headerField, readValue);
110 * Sets the TCP source port for the current TCP object instance
111 * @param short tcpSourcePort
114 public TCP setSourcePort(short tcpSourcePort) {
115 byte[] sourcePort = BitBufferHelper.toByteArray(tcpSourcePort);
116 fieldValues.put(SRCPORT, sourcePort);
121 * Sets the TCP destination port for the current TCP object instance
122 * @param short tcpDestinationPort
125 public TCP setDestinationPort(short tcpDestinationPort) {
126 byte[] destinationPort = BitBufferHelper
127 .toByteArray(tcpDestinationPort);
128 fieldValues.put(DESTPORT, destinationPort);
133 * Sets the TCP sequence number for the current TCP object instance
134 * @param int tcpSequenceNumber
137 public TCP setSequenceNumber(int tcpSequenceNumber) {
138 byte[] sequenceNumber = BitBufferHelper.toByteArray(tcpSequenceNumber);
139 fieldValues.put(SEQNUMBER, sequenceNumber);
144 * Sets the TCP data offset for the current TCP object instance
145 * @param byte tcpDataOffset
148 public TCP setDataOffset(byte tcpDataOffset) {
149 byte[] offset = BitBufferHelper.toByteArray(tcpDataOffset);
150 fieldValues.put("DataOffset", offset);
155 * Sets the TCP reserved bits for the current TCP object instance
156 * @param byte tcpReserved
159 public TCP setReserved(byte tcpReserved) {
160 byte[] reserved = BitBufferHelper.toByteArray(tcpReserved);
161 fieldValues.put("Reserved", reserved);
166 * Sets the TCP Ack number for the current TCP object instance
167 * @param int tcpAckNumber
170 public TCP setAckNumber(int tcpAckNumber) {
171 byte[] ackNumber = BitBufferHelper.toByteArray(tcpAckNumber);
172 fieldValues.put(ACKNUMBER, ackNumber);
177 * Sets the TCP flags for the current TCP object instance
178 * @param short tcpFlags
181 public TCP setHeaderLenFlags(short tcpFlags) {
182 byte[] headerLenFlags = BitBufferHelper.toByteArray(tcpFlags);
183 fieldValues.put(HEADERLENFLAGS, headerLenFlags);
188 * Sets the TCP window size for the current TCP object instance
189 * @param short tcpWsize
192 public TCP setWindowSize(short tcpWsize) {
193 byte[] wsize = BitBufferHelper.toByteArray(tcpWsize);
194 fieldValues.put(WINDOWSIZE, wsize);
199 * Sets the TCP checksum for the current TCP object instance
200 * @param short tcpChecksum
203 public TCP setChecksum(short tcpChecksum) {
204 byte[] checksum = BitBufferHelper.toByteArray(tcpChecksum);
205 fieldValues.put(CHECKSUM, checksum);
210 * Sets the TCP Urgent Pointer for the current TCP object instance
211 * @param short tcpUrgentPointer
214 public TCP setUrgentPointer(short tcpUrgentPointer) {
215 byte[] urgentPointer = BitBufferHelper.toByteArray(tcpUrgentPointer);
216 fieldValues.put(URGENTPOINTER, urgentPointer);
221 * Gets the stored source port value of TCP header
222 * @return the sourcePort
224 public short getSourcePort() {
225 return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
229 * Gets the stored destination port value of TCP header
230 * @return the destinationPort
232 public short getDestinationPort() {
233 return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
237 * Get the stored checksum value of the TCP header
238 * @return short - the checksum
240 public short getChecksum() {
241 return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
245 public void populateMatch(Match match) {
246 match.setField(MatchType.TP_SRC, this.getSourcePort());
247 match.setField(MatchType.TP_DST, this.getDestinationPort());