00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ns3/assert.h"
00022 #include "ns3/address-utils.h"
00023 #include "arp-header.h"
00024
00025 namespace ns3 {
00026
00027 NS_OBJECT_ENSURE_REGISTERED (ArpHeader);
00028
00029 void
00030 ArpHeader::SetRequest (Address sourceHardwareAddress,
00031 Ipv4Address sourceProtocolAddress,
00032 Address destinationHardwareAddress,
00033 Ipv4Address destinationProtocolAddress)
00034 {
00035 m_type = ARP_TYPE_REQUEST;
00036 m_macSource = sourceHardwareAddress;
00037 m_macDest = destinationHardwareAddress;
00038 m_ipv4Source = sourceProtocolAddress;
00039 m_ipv4Dest = destinationProtocolAddress;
00040 }
00041 void
00042 ArpHeader::SetReply (Address sourceHardwareAddress,
00043 Ipv4Address sourceProtocolAddress,
00044 Address destinationHardwareAddress,
00045 Ipv4Address destinationProtocolAddress)
00046 {
00047 m_type = ARP_TYPE_REPLY;
00048 m_macSource = sourceHardwareAddress;
00049 m_macDest = destinationHardwareAddress;
00050 m_ipv4Source = sourceProtocolAddress;
00051 m_ipv4Dest = destinationProtocolAddress;
00052 }
00053 bool
00054 ArpHeader::IsRequest (void) const
00055 {
00056 return (m_type == ARP_TYPE_REQUEST)?true:false;
00057 }
00058 bool
00059 ArpHeader::IsReply (void) const
00060 {
00061 return (m_type == ARP_TYPE_REPLY)?true:false;
00062 }
00063 Address
00064 ArpHeader::GetSourceHardwareAddress (void)
00065 {
00066 return m_macSource;
00067 }
00068 Address
00069 ArpHeader::GetDestinationHardwareAddress (void)
00070 {
00071 return m_macDest;
00072 }
00073 Ipv4Address
00074 ArpHeader::GetSourceIpv4Address (void)
00075 {
00076 return m_ipv4Source;
00077 }
00078 Ipv4Address
00079 ArpHeader::GetDestinationIpv4Address (void)
00080 {
00081 return m_ipv4Dest;
00082 }
00083
00084
00085 TypeId
00086 ArpHeader::GetTypeId (void)
00087 {
00088 static TypeId tid = TypeId ("ns3::ArpHeader")
00089 .SetParent<Header> ()
00090 .AddConstructor<ArpHeader> ()
00091 ;
00092 return tid;
00093 }
00094 TypeId
00095 ArpHeader::GetInstanceTypeId (void) const
00096 {
00097 return GetTypeId ();
00098 }
00099 void
00100 ArpHeader::Print (std::ostream &os) const
00101 {
00102 if (IsRequest ())
00103 {
00104 os << "request "
00105 << "source mac: " << m_macSource << " "
00106 << "source ipv4: " << m_ipv4Source << " "
00107 << "dest ipv4: " << m_ipv4Dest
00108 ;
00109 }
00110 else
00111 {
00112 NS_ASSERT (IsReply ());
00113 os << "reply "
00114 << "source mac: " << m_macSource << " "
00115 << "source ipv4: " << m_ipv4Source << " "
00116 << "dest mac: " << m_macDest << " "
00117 << "dest ipv4: " <<m_ipv4Dest
00118 ;
00119 }
00120 }
00121 uint32_t
00122 ArpHeader::GetSerializedSize (void) const
00123 {
00124
00125 return 28;
00126 }
00127
00128 void
00129 ArpHeader::Serialize (Buffer::Iterator start) const
00130 {
00131 Buffer::Iterator i = start;
00132 NS_ASSERT (m_macSource.GetLength () == m_macDest.GetLength ());
00133
00134
00135 i.WriteHtonU16 (0x0001);
00136
00137 i.WriteHtonU16 (0x0800);
00138 i.WriteU8 (m_macSource.GetLength ());
00139 i.WriteU8 (4);
00140 i.WriteHtonU16 (m_type);
00141 WriteTo (i, m_macSource);
00142 WriteTo (i, m_ipv4Source);
00143 WriteTo (i, m_macDest);
00144 WriteTo (i, m_ipv4Dest);
00145 }
00146 uint32_t
00147 ArpHeader::Deserialize (Buffer::Iterator start)
00148 {
00149 Buffer::Iterator i = start;
00150 i.Next (2+2);
00151 uint32_t hardwareAddressLen = i.ReadU8 ();
00152 i.Next (1);
00153 m_type = i.ReadNtohU16 ();
00154 ReadFrom (i, m_macSource, hardwareAddressLen);
00155 ReadFrom (i, m_ipv4Source);
00156 ReadFrom (i, m_macDest, hardwareAddressLen);
00157 ReadFrom (i, m_ipv4Dest);
00158 return GetSerializedSize ();
00159 }
00160
00161 };