A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
arp-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/assert.h"
22 #include "ns3/address-utils.h"
23 #include "arp-header.h"
24 
25 namespace ns3 {
26 
27 NS_OBJECT_ENSURE_REGISTERED (ArpHeader);
28 
29 void
30 ArpHeader::SetRequest (Address sourceHardwareAddress,
31  Ipv4Address sourceProtocolAddress,
32  Address destinationHardwareAddress,
33  Ipv4Address destinationProtocolAddress)
34 {
36  m_macSource = sourceHardwareAddress;
37  m_macDest = destinationHardwareAddress;
38  m_ipv4Source = sourceProtocolAddress;
39  m_ipv4Dest = destinationProtocolAddress;
40 }
41 void
42 ArpHeader::SetReply (Address sourceHardwareAddress,
43  Ipv4Address sourceProtocolAddress,
44  Address destinationHardwareAddress,
45  Ipv4Address destinationProtocolAddress)
46 {
48  m_macSource = sourceHardwareAddress;
49  m_macDest = destinationHardwareAddress;
50  m_ipv4Source = sourceProtocolAddress;
51  m_ipv4Dest = destinationProtocolAddress;
52 }
53 bool
55 {
56  return (m_type == ARP_TYPE_REQUEST) ? true : false;
57 }
58 bool
59 ArpHeader::IsReply (void) const
60 {
61  return (m_type == ARP_TYPE_REPLY) ? true : false;
62 }
63 Address
65 {
66  return m_macSource;
67 }
68 Address
70 {
71  return m_macDest;
72 }
75 {
76  return m_ipv4Source;
77 }
80 {
81  return m_ipv4Dest;
82 }
83 
84 
85 TypeId
87 {
88  static TypeId tid = TypeId ("ns3::ArpHeader")
89  .SetParent<Header> ()
90  .AddConstructor<ArpHeader> ()
91  ;
92  return tid;
93 }
94 TypeId
96 {
97  return GetTypeId ();
98 }
99 void
100 ArpHeader::Print (std::ostream &os) const
101 {
102  if (IsRequest ())
103  {
104  os << "request "
105  << "source mac: " << m_macSource << " "
106  << "source ipv4: " << m_ipv4Source << " "
107  << "dest ipv4: " << m_ipv4Dest
108  ;
109  }
110  else
111  {
112  NS_ASSERT (IsReply ());
113  os << "reply "
114  << "source mac: " << m_macSource << " "
115  << "source ipv4: " << m_ipv4Source << " "
116  << "dest mac: " << m_macDest << " "
117  << "dest ipv4: " <<m_ipv4Dest
118  ;
119  }
120 }
121 uint32_t
123 {
124  NS_ASSERT((m_macSource.GetLength () == 6) || (m_macSource.GetLength () == 8));
126 
127  uint32_t length = 16; // Length minus two hardware addresses
128  length += m_macSource.GetLength () * 2;
129 
130  return length;
131 }
132 
133 void
135 {
138 
139  /* ethernet */
140  i.WriteHtonU16 (0x0001);
141  /* ipv4 */
142  i.WriteHtonU16 (0x0800);
144  i.WriteU8 (4);
145  i.WriteHtonU16 (m_type);
146  WriteTo (i, m_macSource);
147  WriteTo (i, m_ipv4Source);
148  WriteTo (i, m_macDest);
149  WriteTo (i, m_ipv4Dest);
150 }
151 
152 uint32_t
154 {
156  i.Next (2); // Skip HRD
157  uint32_t protocolType = i.ReadNtohU16 (); // Read PRO
158  uint32_t hardwareAddressLen = i.ReadU8 (); // Read HLN
159  uint32_t protocolAddressLen = i.ReadU8 (); // Read PLN
160 
161  //
162  // It is implicit here that we have a protocol type of 0x800 (IP).
163  // It is also implicit here that we are using Ipv4 (PLN == 4).
164  // If this isn't the case, we need to return an error since we don't want to
165  // be too fragile if we get connected to real networks.
166  //
167  if (protocolType != 0x800 || protocolAddressLen != 4)
168  {
169  return 0;
170  }
171 
172  m_type = i.ReadNtohU16 (); // Read OP
173  ReadFrom (i, m_macSource, hardwareAddressLen); // Read SHA (size HLN)
174  ReadFrom (i, m_ipv4Source); // Read SPA (size PLN == 4)
175  ReadFrom (i, m_macDest, hardwareAddressLen); // Read THA (size HLN)
176  ReadFrom (i, m_ipv4Dest); // Read TPA (size PLN == 4)
177  return GetSerializedSize ();
178 }
179 
180 } // namespace ns3