/*
 * Copyright (c) 2010 Hemanth Narra
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Author: Hemanth Narra <hemanth@ittc.ku.com>
 *
 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
 * ResiliNets Research Group  https://resilinets.org/
 * Information and Telecommunication Technology Center (ITTC)
 * and Department of Electrical Engineering and Computer Science
 * The University of Kansas Lawrence, KS USA.
 *
 * Work supported in part by NSF FIND (Future Internet Design) Program
 * under grant CNS-0626918 (Postmodern Internet Architecture),
 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
 * US Department of Defense (DoD), and ITTC at The University of Kansas.
 */
#include "dsdv-packet.h"

#include "ns3/address-utils.h"
#include "ns3/packet.h"

namespace ns3
{
namespace dsdv
{

NS_OBJECT_ENSURE_REGISTERED(DsdvHeader);

DsdvHeader::DsdvHeader(Ipv4Address dst, uint32_t hopCount, uint32_t dstSeqNo)
    : m_dst(dst),
      m_hopCount(hopCount),
      m_dstSeqNo(dstSeqNo)
{
}

DsdvHeader::~DsdvHeader()
{
}

TypeId
DsdvHeader::GetTypeId()
{
    static TypeId tid = TypeId("ns3::dsdv::DsdvHeader")
                            .SetParent<Header>()
                            .SetGroupName("Dsdv")
                            .AddConstructor<DsdvHeader>();
    return tid;
}

TypeId
DsdvHeader::GetInstanceTypeId() const
{
    return GetTypeId();
}

uint32_t
DsdvHeader::GetSerializedSize() const
{
    return 12;
}

void
DsdvHeader::Serialize(Buffer::Iterator i) const
{
    WriteTo(i, m_dst);
    i.WriteHtonU32(m_hopCount);
    i.WriteHtonU32(m_dstSeqNo);
}

uint32_t
DsdvHeader::Deserialize(Buffer::Iterator start)
{
    Buffer::Iterator i = start;

    ReadFrom(i, m_dst);
    m_hopCount = i.ReadNtohU32();
    m_dstSeqNo = i.ReadNtohU32();

    uint32_t dist = i.GetDistanceFrom(start);
    NS_ASSERT(dist == GetSerializedSize());
    return dist;
}

void
DsdvHeader::Print(std::ostream& os) const
{
    os << "DestinationIpv4: " << m_dst << " Hopcount: " << m_hopCount
       << " SequenceNumber: " << m_dstSeqNo;
}
} // namespace dsdv
} // namespace ns3
