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
24 public class TCP extends Packet {
26 public static final String SRCPORT = "SourcePort";
27 public static final String DESTPORT = "DestinationPort";
28 public static final String SEQNUMBER = "SequenceNumber";
29 public static final String ACKNUMBER = "AcknowledgementNumber";
30 public static final String DATAOFFSET = "DataOffset";
31 public static final String RESERVED = "Reserved";
32 public static final String HEADERLENFLAGS = "HeaderLenFlags";
33 public static final String WINDOWSIZE = "WindowSize";
34 public static final String CHECKSUM = "Checksum";
35 public static final String URGENTPOINTER = "UrgentPointer";
37 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
38 private static final long serialVersionUID = 1L;
40 put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
41 put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
42 put(SEQNUMBER, new ImmutablePair<Integer, Integer>(32, 32));
43 put(ACKNUMBER, new ImmutablePair<Integer, Integer>(64, 32));
44 put(DATAOFFSET, new ImmutablePair<Integer, Integer>(96, 4));
45 put(RESERVED, new ImmutablePair<Integer, Integer>(100, 3));
46 put(HEADERLENFLAGS, new ImmutablePair<Integer, Integer>(103, 9));
47 put(WINDOWSIZE, new ImmutablePair<Integer, Integer>(112, 16));
48 put(CHECKSUM, new ImmutablePair<Integer, Integer>(128, 16));
49 put(URGENTPOINTER, new ImmutablePair<Integer, Integer>(144, 16));
53 private final Map<String, byte[]> fieldValues;
56 * Default constructor that sets all the header fields to zero
60 fieldValues = new HashMap<String, byte[]>();
61 hdrFieldCoordMap = fieldCoordinates;
62 hdrFieldsMap = fieldValues;
63 /* Setting all remaining header field values to
64 * default value of 0. These maybe changed as needed
66 setSourcePort((short) 0);
67 setDestinationPort((short) 0);
70 setDataOffset((byte) 0);
71 setReserved((byte) 0);
72 setWindowSize((short) 0);
73 setUrgentPointer((short) 0);
74 setChecksum((short) 0);
78 * Constructor that sets the access level for the packet and
79 * sets all the header fields to zero.
81 public TCP(boolean writeAccess) {
83 fieldValues = new HashMap<String, byte[]>();
84 hdrFieldCoordMap = fieldCoordinates;
85 hdrFieldsMap = fieldValues;
86 /* Setting all remaining header field values to
87 * default value of 0. These maybe changed as needed
89 setSourcePort((short) 0);
90 setDestinationPort((short) 0);
93 setDataOffset((byte) 0);
94 setReserved((byte) 0);
95 setWindowSize((short) 0);
96 setUrgentPointer((short) 0);
97 setChecksum((short) 0);
102 * Stores the value read from data stream
104 public void setHeaderField(String headerField, byte[] readValue) {
105 hdrFieldsMap.put(headerField, readValue);
109 * Sets the TCP source port for the current TCP object instance
110 * @param short tcpSourcePort
113 public TCP setSourcePort(short tcpSourcePort) {
114 byte[] sourcePort = BitBufferHelper.toByteArray(tcpSourcePort);
115 fieldValues.put(SRCPORT, sourcePort);
120 * Sets the TCP destination port for the current TCP object instance
121 * @param short tcpDestinationPort
124 public TCP setDestinationPort(short tcpDestinationPort) {
125 byte[] destinationPort = BitBufferHelper
126 .toByteArray(tcpDestinationPort);
127 fieldValues.put(DESTPORT, destinationPort);
132 * Sets the TCP sequence number for the current TCP object instance
133 * @param int tcpSequenceNumber
136 public TCP setSequenceNumber(int tcpSequenceNumber) {
137 byte[] sequenceNumber = BitBufferHelper.toByteArray(tcpSequenceNumber);
138 fieldValues.put(SEQNUMBER, sequenceNumber);
143 * Sets the TCP data offset for the current TCP object instance
144 * @param byte tcpDataOffset
147 public TCP setDataOffset(byte tcpDataOffset) {
148 byte[] offset = BitBufferHelper.toByteArray(tcpDataOffset);
149 fieldValues.put("DataOffset", offset);
154 * Sets the TCP reserved bits for the current TCP object instance
155 * @param byte tcpReserved
158 public TCP setReserved(byte tcpReserved) {
159 byte[] reserved = BitBufferHelper.toByteArray(tcpReserved);
160 fieldValues.put("Reserved", reserved);
165 * Sets the TCP Ack number for the current TCP object instance
166 * @param int tcpAckNumber
169 public TCP setAckNumber(int tcpAckNumber) {
170 byte[] ackNumber = BitBufferHelper.toByteArray(tcpAckNumber);
171 fieldValues.put(ACKNUMBER, ackNumber);
176 * Sets the TCP flags for the current TCP object instance
177 * @param short tcpFlags
180 public TCP setHeaderLenFlags(short tcpFlags) {
181 byte[] headerLenFlags = BitBufferHelper.toByteArray(tcpFlags);
182 fieldValues.put(HEADERLENFLAGS, headerLenFlags);
187 * Sets the TCP window size for the current TCP object instance
188 * @param short tcpWsize
191 public TCP setWindowSize(short tcpWsize) {
192 byte[] wsize = BitBufferHelper.toByteArray(tcpWsize);
193 fieldValues.put(WINDOWSIZE, wsize);
198 * Sets the TCP checksum for the current TCP object instance
199 * @param short tcpChecksum
202 public TCP setChecksum(short tcpChecksum) {
203 byte[] checksum = BitBufferHelper.toByteArray(tcpChecksum);
204 fieldValues.put(CHECKSUM, checksum);
209 * Sets the TCP Urgent Pointer for the current TCP object instance
210 * @param short tcpUrgentPointer
213 public TCP setUrgentPointer(short tcpUrgentPointer) {
214 byte[] urgentPointer = BitBufferHelper.toByteArray(tcpUrgentPointer);
215 fieldValues.put(URGENTPOINTER, urgentPointer);
220 * Gets the stored source port value of TCP header
221 * @return the sourcePort
223 public short getSourcePort() {
224 return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
228 * Gets the stored destination port value of TCP header
229 * @return the destinationPort
231 public short getDestinationPort() {
232 return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
236 * Get the stored checksum value of the TCP header
237 * @return short - the checksum
239 public short getChecksum() {
240 return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
244 public void populateMatch(Match match) {
245 match.setField(MatchType.TP_SRC, this.getSourcePort());
246 match.setField(MatchType.TP_DST, this.getDestinationPort());