2 * Copyright (c) 2013 - 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.vpnservice.mdsalutil.packet;
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.opendaylight.controller.liblldp.BitBufferHelper;
18 import org.opendaylight.controller.liblldp.Packet;
21 * Class that represents the TCP segment objects
23 public class TCP extends Packet {
25 public static final String SRCPORT = "SourcePort";
26 public static final String DESTPORT = "DestinationPort";
27 public static final String SEQNUMBER = "SequenceNumber";
28 public static final String ACKNUMBER = "AcknowledgementNumber";
29 public static final String DATAOFFSET = "DataOffset";
30 public static final String RESERVED = "Reserved";
31 public static final String HEADERLENFLAGS = "HeaderLenFlags";
32 public static final String WINDOWSIZE = "WindowSize";
33 public static final String CHECKSUM = "Checksum";
34 public static final String URGENTPOINTER = "UrgentPointer";
36 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
37 private static final long serialVersionUID = 1L;
39 put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
40 put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
41 put(SEQNUMBER, new ImmutablePair<Integer, Integer>(32, 32));
42 put(ACKNUMBER, new ImmutablePair<Integer, Integer>(64, 32));
43 put(DATAOFFSET, new ImmutablePair<Integer, Integer>(96, 4));
44 put(RESERVED, new ImmutablePair<Integer, Integer>(100, 3));
45 put(HEADERLENFLAGS, new ImmutablePair<Integer, Integer>(103, 9));
46 put(WINDOWSIZE, new ImmutablePair<Integer, Integer>(112, 16));
47 put(CHECKSUM, new ImmutablePair<Integer, Integer>(128, 16));
48 put(URGENTPOINTER, new ImmutablePair<Integer, Integer>(144, 16));
52 private final Map<String, byte[]> fieldValues;
55 * Default constructor that sets all the header fields to zero
59 fieldValues = new HashMap<String, byte[]>();
60 hdrFieldCoordMap = fieldCoordinates;
61 hdrFieldsMap = fieldValues;
62 /* Setting all remaining header field values to
63 * default value of 0. These maybe changed as needed
65 setSourcePort((short) 0);
66 setDestinationPort((short) 0);
69 setDataOffset((byte) 0);
70 setReserved((byte) 0);
71 setWindowSize((short) 0);
72 setUrgentPointer((short) 0);
73 setChecksum((short) 0);
77 * Constructor that sets the access level for the packet and
78 * sets all the header fields to zero.
79 * @param writeAccess - boolean
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
103 * @param headerField - String
104 * @param readValue - byte[]
106 public void setHeaderField(String headerField, byte[] readValue) {
107 hdrFieldsMap.put(headerField, readValue);
111 * Sets the TCP source port for the current TCP object instance
112 * @param tcpSourcePort short
115 public TCP setSourcePort(short tcpSourcePort) {
116 byte[] sourcePort = BitBufferHelper.toByteArray(tcpSourcePort);
117 fieldValues.put(SRCPORT, sourcePort);
122 * Sets the TCP destination port for the current TCP object instance
123 * @param tcpDestinationPort short
126 public TCP setDestinationPort(short tcpDestinationPort) {
127 byte[] destinationPort = BitBufferHelper
128 .toByteArray(tcpDestinationPort);
129 fieldValues.put(DESTPORT, destinationPort);
134 * Sets the TCP sequence number for the current TCP object instance
135 * @param tcpSequenceNumber - int
138 public TCP setSequenceNumber(int tcpSequenceNumber) {
139 byte[] sequenceNumber = BitBufferHelper.toByteArray(tcpSequenceNumber);
140 fieldValues.put(SEQNUMBER, sequenceNumber);
145 * Sets the TCP data offset for the current TCP object instance
146 * @param tcpDataOffset - byte
149 public TCP setDataOffset(byte tcpDataOffset) {
150 byte[] offset = BitBufferHelper.toByteArray(tcpDataOffset);
151 fieldValues.put("DataOffset", offset);
156 * Sets the TCP reserved bits for the current TCP object instance
157 * @param tcpReserved byte
160 public TCP setReserved(byte tcpReserved) {
161 byte[] reserved = BitBufferHelper.toByteArray(tcpReserved);
162 fieldValues.put("Reserved", reserved);
167 * Sets the TCP Ack number for the current TCP object instance
168 * @param tcpAckNumber int
171 public TCP setAckNumber(int tcpAckNumber) {
172 byte[] ackNumber = BitBufferHelper.toByteArray(tcpAckNumber);
173 fieldValues.put(ACKNUMBER, ackNumber);
178 * Sets the TCP flags for the current TCP object instance
179 * @param tcpFlags short
182 public TCP setHeaderLenFlags(short tcpFlags) {
183 byte[] headerLenFlags = BitBufferHelper.toByteArray(tcpFlags);
184 fieldValues.put(HEADERLENFLAGS, headerLenFlags);
189 * Sets the TCP window size for the current TCP object instance
190 * @param tcpWsize short
193 public TCP setWindowSize(short tcpWsize) {
194 byte[] wsize = BitBufferHelper.toByteArray(tcpWsize);
195 fieldValues.put(WINDOWSIZE, wsize);
200 * Sets the TCP checksum for the current TCP object instance
201 * @param tcpChecksum short
204 public TCP setChecksum(short tcpChecksum) {
205 byte[] checksum = BitBufferHelper.toByteArray(tcpChecksum);
206 fieldValues.put(CHECKSUM, checksum);
211 * Sets the TCP Urgent Pointer for the current TCP object instance
212 * @param tcpUrgentPointer short
215 public TCP setUrgentPointer(short tcpUrgentPointer) {
216 byte[] urgentPointer = BitBufferHelper.toByteArray(tcpUrgentPointer);
217 fieldValues.put(URGENTPOINTER, urgentPointer);
222 * Gets the stored source port value of TCP header
223 * @return the sourcePort
225 public short getSourcePort() {
226 return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
230 * Gets the stored destination port value of TCP header
231 * @return the destinationPort
233 public short getDestinationPort() {
234 return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
238 * Get the stored checksum value of the TCP header
239 * @return short - the checksum
241 public short getChecksum() {
242 return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));