src/node/arp-header.cc
changeset 524 082ffdd8fbd7
parent 523 eb380b33ae24
child 525 4a6c58f0e327
equal deleted inserted replaced
523:eb380b33ae24 524:082ffdd8fbd7
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2005 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 
       
    22 #include "ns3/assert.h"
       
    23 #include "arp-header.h"
       
    24 #include "header-utils.h"
       
    25 
       
    26 namespace ns3 {
       
    27 
       
    28 ArpHeader::~ArpHeader ()
       
    29 {}
       
    30 
       
    31 void 
       
    32 ArpHeader::SetRequest (MacAddress sourceHardwareAddress,
       
    33                        Ipv4Address sourceProtocolAddress,
       
    34                        MacAddress destinationHardwareAddress,
       
    35                        Ipv4Address destinationProtocolAddress)
       
    36 {
       
    37   m_type = ARP_TYPE_REQUEST;
       
    38   m_macSource = sourceHardwareAddress;
       
    39   m_macDest = destinationHardwareAddress;
       
    40   m_ipv4Source = sourceProtocolAddress;
       
    41   m_ipv4Dest = destinationProtocolAddress;
       
    42 }
       
    43 void 
       
    44 ArpHeader::SetReply (MacAddress sourceHardwareAddress,
       
    45                      Ipv4Address sourceProtocolAddress,
       
    46                      MacAddress destinationHardwareAddress,
       
    47                      Ipv4Address destinationProtocolAddress)
       
    48 {
       
    49   m_type = ARP_TYPE_REPLY;
       
    50   m_macSource = sourceHardwareAddress;
       
    51   m_macDest = destinationHardwareAddress;
       
    52   m_ipv4Source = sourceProtocolAddress;
       
    53   m_ipv4Dest = destinationProtocolAddress;
       
    54 }
       
    55 bool 
       
    56 ArpHeader::IsRequest (void) const
       
    57 {
       
    58   return (m_type == ARP_TYPE_REQUEST)?true:false;
       
    59 }
       
    60 bool 
       
    61 ArpHeader::IsReply (void) const
       
    62 {
       
    63   return (m_type == ARP_TYPE_REPLY)?true:false;
       
    64 }
       
    65 MacAddress 
       
    66 ArpHeader::GetSourceHardwareAddress (void)
       
    67 {
       
    68   return m_macSource;
       
    69 }
       
    70 MacAddress 
       
    71 ArpHeader::GetDestinationHardwareAddress (void)
       
    72 {
       
    73   return m_macDest;
       
    74 }
       
    75 Ipv4Address 
       
    76 ArpHeader::GetSourceIpv4Address (void)
       
    77 {
       
    78   return m_ipv4Source;
       
    79 }
       
    80 Ipv4Address 
       
    81 ArpHeader::GetDestinationIpv4Address (void)
       
    82 {
       
    83   return m_ipv4Dest;
       
    84 }
       
    85 
       
    86 
       
    87 void 
       
    88 ArpHeader::PrintTo (std::ostream &os) const
       
    89 {
       
    90   os << "(arp)";
       
    91   if (IsRequest ()) 
       
    92     {
       
    93       os << " source mac: " << m_macSource
       
    94           << " source ipv4: " << m_ipv4Source
       
    95           << " dest ipv4: " << m_ipv4Dest;
       
    96     } 
       
    97   else 
       
    98     {
       
    99       NS_ASSERT (IsReply ());
       
   100       os << " source mac: " << m_macSource
       
   101           << " source ipv4: " << m_ipv4Source
       
   102           << " dest mac: " << m_macDest
       
   103           << " dest ipv4: " <<m_ipv4Dest;
       
   104     }
       
   105 }
       
   106 uint32_t 
       
   107 ArpHeader::GetSerializedSize (void) const
       
   108 {
       
   109   /* this is the size of an ARP payload. */
       
   110   return 28;
       
   111 }
       
   112 
       
   113 void
       
   114 ArpHeader::SerializeTo (Buffer::Iterator start) const
       
   115 {
       
   116   Buffer::Iterator i = start;
       
   117   NS_ASSERT (m_macSource.GetLength () == m_macDest.GetLength ());
       
   118 
       
   119   /* ethernet */
       
   120   i.WriteHtonU16 (0x0001);
       
   121   /* ipv4 */
       
   122   i.WriteHtonU16 (0x0800);
       
   123   i.WriteU8 (m_macSource.GetLength ());
       
   124   i.WriteU8 (4);
       
   125   i.WriteHtonU16 (m_type);
       
   126   WriteTo (i, m_macSource);
       
   127   WriteTo (i, m_ipv4Source);
       
   128   WriteTo (i, m_macDest);
       
   129   WriteTo (i, m_ipv4Dest);
       
   130 }
       
   131 uint32_t
       
   132 ArpHeader::DeserializeFrom (Buffer::Iterator start)
       
   133 {
       
   134   Buffer::Iterator i = start;
       
   135   i.Next (2+2);
       
   136   uint32_t hardwareAddressLen = i.ReadU8 ();
       
   137   i.Next (1);
       
   138   m_type = i.ReadNtohU16 ();
       
   139   ReadFrom (i, m_macSource, hardwareAddressLen);
       
   140   ReadFrom (i, m_ipv4Source);
       
   141   ReadFrom (i, m_macDest, hardwareAddressLen);
       
   142   ReadFrom (i, m_ipv4Dest);
       
   143   return GetSerializedSize ();
       
   144 }
       
   145 
       
   146 }; // namespace ns3