00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007 Georgia Tech Research Corporation 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Raj Bhattacharjea <raj.b@gatech.edu> 00019 */ 00020 00021 #ifndef TCP_HEADER_H 00022 #define TCP_HEADER_H 00023 00024 #include <stdint.h> 00025 #include "ns3/header.h" 00026 #include "ns3/buffer.h" 00027 #include "ns3/tcp-socket-factory.h" 00028 #include "ns3/ipv4-address.h" 00029 #include "ns3/sequence-number.h" 00030 00031 namespace ns3 { 00032 00033 /** 00034 * \ingroup tcp 00035 * \brief Header for the Transmission Control Protocol 00036 * 00037 * This class has fields corresponding to those in a network TCP header 00038 * (port numbers, sequence and acknowledgement numbers, flags, etc) as well 00039 * as methods for serialization to and deserialization from a byte buffer. 00040 */ 00041 00042 class TcpHeader : public Header 00043 { 00044 public: 00045 TcpHeader (); 00046 virtual ~TcpHeader (); 00047 00048 /** 00049 * \brief Enable checksum calculation for TCP (XXX currently has no effect) 00050 */ 00051 void EnableChecksums (void); 00052 //Setters 00053 /** 00054 * \param port The source port for this TcpHeader 00055 */ 00056 void SetSourcePort (uint16_t port); 00057 /** 00058 * \param port the destination port for this TcpHeader 00059 */ 00060 void SetDestinationPort (uint16_t port); 00061 /** 00062 * \param sequenceNumber the sequence number for this TcpHeader 00063 */ 00064 void SetSequenceNumber (SequenceNumber sequenceNumber); 00065 /** 00066 * \param ackNumber the ACK number for this TcpHeader 00067 */ 00068 void SetAckNumber (SequenceNumber ackNumber); 00069 /** 00070 * \param length the length of this TcpHeader 00071 */ 00072 void SetLength (uint8_t length); 00073 /** 00074 * \param flags the flags for this TcpHeader 00075 */ 00076 void SetFlags (uint8_t flags); 00077 /** 00078 * \param windowSize the window size for this TcpHeader 00079 */ 00080 void SetWindowSize (uint16_t windowSize); 00081 /** 00082 * \param urgentPointer the urgent pointer for this TcpHeader 00083 */ 00084 void SetUrgentPointer (uint16_t urgentPointer); 00085 00086 00087 //Getters 00088 /** 00089 * \return The source port for this TcpHeader 00090 */ 00091 uint16_t GetSourcePort () const; 00092 /** 00093 * \return the destination port for this TcpHeader 00094 */ 00095 uint16_t GetDestinationPort () const; 00096 /** 00097 * \return the sequence number for this TcpHeader 00098 */ 00099 SequenceNumber GetSequenceNumber () const; 00100 /** 00101 * \return the ACK number for this TcpHeader 00102 */ 00103 SequenceNumber GetAckNumber () const; 00104 /** 00105 * \return the length of this TcpHeader 00106 */ 00107 uint8_t GetLength () const; 00108 /** 00109 * \return the flags for this TcpHeader 00110 */ 00111 uint8_t GetFlags () const; 00112 /** 00113 * \return the window size for this TcpHeader 00114 */ 00115 uint16_t GetWindowSize () const; 00116 /** 00117 * \return the urgent pointer for this TcpHeader 00118 */ 00119 uint16_t GetUrgentPointer () const; 00120 00121 /** 00122 * \param source the ip source to use in the underlying 00123 * ip packet. 00124 * \param destination the ip destination to use in the 00125 * underlying ip packet. 00126 * \param protocol the protocol number to use in the underlying 00127 * ip packet. 00128 * 00129 * If you want to use tcp checksums, you should call this 00130 * method prior to adding the header to a packet. 00131 */ 00132 void InitializeChecksum (Ipv4Address source, 00133 Ipv4Address destination, 00134 uint8_t protocol); 00135 00136 typedef enum { NONE = 0, FIN = 1, SYN = 2, RST = 4, PSH = 8, ACK = 16, 00137 URG = 32} Flags_t; 00138 00139 static TypeId GetTypeId (void); 00140 virtual TypeId GetInstanceTypeId (void) const; 00141 virtual void Print (std::ostream &os) const; 00142 virtual uint32_t GetSerializedSize (void) const; 00143 virtual void Serialize (Buffer::Iterator start) const; 00144 virtual uint32_t Deserialize (Buffer::Iterator start); 00145 00146 /** 00147 * \brief Is the TCP checksum correct ? 00148 * \returns true if the checksum is correct, false otherwise. 00149 */ 00150 bool IsChecksumOk (void) const; 00151 00152 private: 00153 uint16_t CalculateHeaderChecksum (uint16_t size) const; 00154 uint16_t m_sourcePort; 00155 uint16_t m_destinationPort; 00156 uint32_t m_sequenceNumber; 00157 uint32_t m_ackNumber; 00158 uint8_t m_length; // really a uint4_t 00159 uint8_t m_flags; // really a uint6_t 00160 uint16_t m_windowSize; 00161 uint16_t m_urgentPointer; 00162 00163 Ipv4Address m_source; 00164 Ipv4Address m_destination; 00165 uint8_t m_protocol; 00166 00167 uint16_t m_initialChecksum; 00168 bool m_calcChecksum; 00169 bool m_goodChecksum; 00170 }; 00171 00172 }; // namespace ns3 00173 00174 #endif /* TCP_HEADER */