Files
unison/src/internet-stack/tcp-header.cc
2008-07-01 10:52:11 -07:00

269 lines
5.8 KiB
C++

/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 Georgia Tech Research Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Raj Bhattacharjea <raj.b@gatech.edu>
*/
#include <stdint.h>
#include <iostream>
#include "tcp-socket-impl.h"
#include "tcp-header.h"
#include "ns3/buffer.h"
namespace ns3 {
NS_OBJECT_ENSURE_REGISTERED (TcpHeader);
TcpHeader::TcpHeader ()
: m_sourcePort (0),
m_destinationPort (0),
m_sequenceNumber (0),
m_ackNumber (0),
m_length (5),
m_flags (0),
m_windowSize (0xffff),
m_urgentPointer (0),
m_initialChecksum(0),
m_checksum (0),
m_calcChecksum(false),
m_goodChecksum(true)
{}
TcpHeader::~TcpHeader ()
{}
void
TcpHeader::EnableChecksums (void)
{
m_calcChecksum = true;
}
void TcpHeader::SetPayloadSize(uint16_t payloadSize)
{
m_payloadSize = payloadSize;
}
void TcpHeader::SetSourcePort (uint16_t port)
{
m_sourcePort = port;
}
void TcpHeader::SetDestinationPort (uint16_t port)
{
m_destinationPort = port;
}
void TcpHeader::SetSequenceNumber (SequenceNumber sequenceNumber)
{
m_sequenceNumber = sequenceNumber;
}
void TcpHeader::SetAckNumber (SequenceNumber ackNumber)
{
m_ackNumber = ackNumber;
}
void TcpHeader::SetLength (uint8_t length)
{
m_length = length;
}
void TcpHeader::SetFlags (uint8_t flags)
{
m_flags = flags;
}
void TcpHeader::SetWindowSize (uint16_t windowSize)
{
m_windowSize = windowSize;
}
void TcpHeader::SetChecksum (uint16_t checksum)
{
m_checksum = checksum;
}
void TcpHeader::SetUrgentPointer (uint16_t urgentPointer)
{
m_urgentPointer = urgentPointer;
}
uint16_t TcpHeader::GetSourcePort () const
{
return m_sourcePort;
}
uint16_t TcpHeader::GetDestinationPort () const
{
return m_destinationPort;
}
SequenceNumber TcpHeader::GetSequenceNumber () const
{
return m_sequenceNumber;
}
SequenceNumber TcpHeader::GetAckNumber () const
{
return m_ackNumber;
}
uint8_t TcpHeader::GetLength () const
{
return m_length;
}
uint8_t TcpHeader::GetFlags () const
{
return m_flags;
}
uint16_t TcpHeader::GetWindowSize () const
{
return m_windowSize;
}
uint16_t TcpHeader::GetChecksum () const
{
return m_checksum;
}
uint16_t TcpHeader::GetUrgentPointer () const
{
return m_urgentPointer;
}
void
TcpHeader::InitializeChecksum (Ipv4Address source,
Ipv4Address destination,
uint8_t protocol)
{
Buffer buf = Buffer(12);
uint8_t tmp[4];
Buffer::Iterator it;
uint16_t tcpLength = m_payloadSize + GetSerializedSize();
buf.AddAtStart(12);
it = buf.Begin();
source.Serialize(tmp);
it.Write(tmp, 4); /* source IP address */
destination.Serialize(tmp);
it.Write(tmp, 4); /* destination IP address */
it.WriteU8(0); /* protocol */
it.WriteU8(protocol); /* protocol */
it.WriteU8(tcpLength >> 8); /* length */
it.WriteU8(tcpLength & 0xff); /* length */
it = buf.Begin();
/* we don't CompleteChecksum ( ~ ) now */
m_initialChecksum = ~(it.CalculateIpChecksum(12));
}
bool
TcpHeader::IsChecksumOk (void) const
{
return m_goodChecksum;
}
TypeId
TcpHeader::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpHeader")
.SetParent<Header> ()
.AddConstructor<TcpHeader> ()
;
return tid;
}
TypeId
TcpHeader::GetInstanceTypeId (void) const
{
return GetTypeId ();
}
void TcpHeader::Print (std::ostream &os) const
{
os << m_sourcePort << " > " << m_destinationPort;
if(m_flags!=0)
{
os<<" [";
if((m_flags & FIN) != 0)
{
os<<" FIN ";
}
if((m_flags & SYN) != 0)
{
os<<" SYN ";
}
if((m_flags & RST) != 0)
{
os<<" RST ";
}
if((m_flags & PSH) != 0)
{
os<<" PSH ";
}
if((m_flags & ACK) != 0)
{
os<<" ACK ";
}
if((m_flags & URG) != 0)
{
os<<" URG ";
}
os<<"]";
}
os<<" Seq="<<m_sequenceNumber<<" Ack="<<m_ackNumber<<" Win="<<m_windowSize;
}
uint32_t TcpHeader::GetSerializedSize (void) const
{
return 4*m_length;
}
void TcpHeader::Serialize (Buffer::Iterator start) const
{
Buffer::Iterator i = start;
uint16_t tcpLength = m_payloadSize + GetSerializedSize();
i.WriteHtonU16 (m_sourcePort);
i.WriteHtonU16 (m_destinationPort);
i.WriteHtonU32 (m_sequenceNumber);
i.WriteHtonU32 (m_ackNumber);
i.WriteHtonU16 (m_length << 12 | m_flags); //reserved bits are all zero
i.WriteHtonU16 (m_windowSize);
i.WriteHtonU16 (0);
i.WriteHtonU16 (m_urgentPointer);
if(m_calcChecksum)
{
i = start;
uint16_t checksum = i.CalculateIpChecksum(tcpLength, m_initialChecksum);
i = start;
i.Next(16);
i.WriteU16(checksum);
}
}
uint32_t TcpHeader::Deserialize (Buffer::Iterator start)
{
Buffer::Iterator i = start;
m_sourcePort = i.ReadNtohU16 ();
m_destinationPort = i.ReadNtohU16 ();
m_sequenceNumber = i.ReadNtohU32 ();
m_ackNumber = i.ReadNtohU32 ();
uint16_t field = i.ReadNtohU16 ();
m_flags = field & 0x3F;
m_length = field>>12;
m_windowSize = i.ReadNtohU16 ();
m_checksum = i.ReadU16 ();
m_urgentPointer = i.ReadNtohU16 ();
if(m_calcChecksum)
{
i = start;
uint16_t checksum = i.CalculateIpChecksum(m_payloadSize + GetSerializedSize(), m_initialChecksum);
m_goodChecksum = (checksum == 0);
}
return GetSerializedSize ();
}
}; // namespace ns3