author | Josh Pelkey <jpelkey@gatech.edu> |
Fri, 13 May 2011 14:55:24 -0400 | |
changeset 7176 | 9f2663992e99 |
parent 6834 | 036f9a0b9899 |
child 7385 | 10beb0e53130 |
permissions | -rw-r--r-- |
242 | 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 |
||
286
57e6a2006962
convert use of <cassert> to "ns3/assert.h"
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
258
diff
changeset
|
21 |
#include "ns3/assert.h" |
976
e82bac1816ce
ethernet Header and Trailer classes
Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
parents:
902
diff
changeset
|
22 |
#include "ns3/address-utils.h" |
242 | 23 |
#include "arp-header.h" |
24 |
||
1236
cfa5e0b39281
rework the NS_XX_ENSURE_REGISTERED macros and make sure we typecheck the input to TraceContext::Add and TraceContext::Get methods
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1234
diff
changeset
|
25 |
namespace ns3 { |
1152
1d06449f0a98
macros to ensure proper initialization
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1151
diff
changeset
|
26 |
|
2643
2a3324f4dabe
define a TypeId for each Header/Trailer.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1236
diff
changeset
|
27 |
NS_OBJECT_ENSURE_REGISTERED (ArpHeader); |
2a3324f4dabe
define a TypeId for each Header/Trailer.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1236
diff
changeset
|
28 |
|
242 | 29 |
void |
1161
bb72baff8b26
replace MacAddress by Address
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
902
diff
changeset
|
30 |
ArpHeader::SetRequest (Address sourceHardwareAddress, |
242 | 31 |
Ipv4Address sourceProtocolAddress, |
1161
bb72baff8b26
replace MacAddress by Address
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
902
diff
changeset
|
32 |
Address destinationHardwareAddress, |
242 | 33 |
Ipv4Address destinationProtocolAddress) |
34 |
{ |
|
35 |
m_type = ARP_TYPE_REQUEST; |
|
36 |
m_macSource = sourceHardwareAddress; |
|
37 |
m_macDest = destinationHardwareAddress; |
|
38 |
m_ipv4Source = sourceProtocolAddress; |
|
39 |
m_ipv4Dest = destinationProtocolAddress; |
|
40 |
} |
|
41 |
void |
|
1161
bb72baff8b26
replace MacAddress by Address
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
902
diff
changeset
|
42 |
ArpHeader::SetReply (Address sourceHardwareAddress, |
242 | 43 |
Ipv4Address sourceProtocolAddress, |
1161
bb72baff8b26
replace MacAddress by Address
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
902
diff
changeset
|
44 |
Address destinationHardwareAddress, |
242 | 45 |
Ipv4Address destinationProtocolAddress) |
46 |
{ |
|
47 |
m_type = ARP_TYPE_REPLY; |
|
48 |
m_macSource = sourceHardwareAddress; |
|
49 |
m_macDest = destinationHardwareAddress; |
|
50 |
m_ipv4Source = sourceProtocolAddress; |
|
51 |
m_ipv4Dest = destinationProtocolAddress; |
|
52 |
} |
|
53 |
bool |
|
54 |
ArpHeader::IsRequest (void) const |
|
55 |
{ |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
56 |
return (m_type == ARP_TYPE_REQUEST) ? true : false; |
242 | 57 |
} |
58 |
bool |
|
59 |
ArpHeader::IsReply (void) const |
|
60 |
{ |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
61 |
return (m_type == ARP_TYPE_REPLY) ? true : false; |
242 | 62 |
} |
1161
bb72baff8b26
replace MacAddress by Address
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
902
diff
changeset
|
63 |
Address |
242 | 64 |
ArpHeader::GetSourceHardwareAddress (void) |
65 |
{ |
|
66 |
return m_macSource; |
|
67 |
} |
|
1161
bb72baff8b26
replace MacAddress by Address
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
902
diff
changeset
|
68 |
Address |
242 | 69 |
ArpHeader::GetDestinationHardwareAddress (void) |
70 |
{ |
|
71 |
return m_macDest; |
|
72 |
} |
|
73 |
Ipv4Address |
|
74 |
ArpHeader::GetSourceIpv4Address (void) |
|
75 |
{ |
|
76 |
return m_ipv4Source; |
|
77 |
} |
|
78 |
Ipv4Address |
|
79 |
ArpHeader::GetDestinationIpv4Address (void) |
|
80 |
{ |
|
81 |
return m_ipv4Dest; |
|
82 |
} |
|
83 |
||
2646
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
84 |
|
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
85 |
TypeId |
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
86 |
ArpHeader::GetTypeId (void) |
795
cd108c9817d0
add Chunk::GetName and implement it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
524
diff
changeset
|
87 |
{ |
2646
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
88 |
static TypeId tid = TypeId ("ns3::ArpHeader") |
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
89 |
.SetParent<Header> () |
2650
3de4cacb8981
make sure all headers and trailers gets a constructor registered in their TypeId.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2646
diff
changeset
|
90 |
.AddConstructor<ArpHeader> () |
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
91 |
; |
2646
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
92 |
return tid; |
795
cd108c9817d0
add Chunk::GetName and implement it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
524
diff
changeset
|
93 |
} |
2646
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
94 |
TypeId |
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
95 |
ArpHeader::GetInstanceTypeId (void) const |
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
96 |
{ |
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
97 |
return GetTypeId (); |
c1fef7686472
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2643
diff
changeset
|
98 |
} |
242 | 99 |
void |
1232
2e8c3f8bb77f
remove Chunk base class
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1198
diff
changeset
|
100 |
ArpHeader::Print (std::ostream &os) const |
242 | 101 |
{ |
102 |
if (IsRequest ()) |
|
103 |
{ |
|
2653
b8eff7186c5c
improve pretty-printing output.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2650
diff
changeset
|
104 |
os << "request " |
902
b85919168708
change arp pretty printing to new format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
795
diff
changeset
|
105 |
<< "source mac: " << m_macSource << " " |
b85919168708
change arp pretty printing to new format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
795
diff
changeset
|
106 |
<< "source ipv4: " << m_ipv4Source << " " |
b85919168708
change arp pretty printing to new format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
795
diff
changeset
|
107 |
<< "dest ipv4: " << m_ipv4Dest |
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
108 |
; |
242 | 109 |
} |
110 |
else |
|
111 |
{ |
|
286
57e6a2006962
convert use of <cassert> to "ns3/assert.h"
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
258
diff
changeset
|
112 |
NS_ASSERT (IsReply ()); |
2653
b8eff7186c5c
improve pretty-printing output.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2650
diff
changeset
|
113 |
os << "reply " |
902
b85919168708
change arp pretty printing to new format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
795
diff
changeset
|
114 |
<< "source mac: " << m_macSource << " " |
b85919168708
change arp pretty printing to new format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
795
diff
changeset
|
115 |
<< "source ipv4: " << m_ipv4Source << " " |
b85919168708
change arp pretty printing to new format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
795
diff
changeset
|
116 |
<< "dest mac: " << m_macDest << " " |
b85919168708
change arp pretty printing to new format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
795
diff
changeset
|
117 |
<< "dest ipv4: " <<m_ipv4Dest |
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
118 |
; |
242 | 119 |
} |
120 |
} |
|
121 |
uint32_t |
|
122 |
ArpHeader::GetSerializedSize (void) const |
|
123 |
{ |
|
124 |
/* this is the size of an ARP payload. */ |
|
125 |
return 28; |
|
126 |
} |
|
127 |
||
463
c2082308e01a
merge Packet API changes needed for Packet pretty printing.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
128 |
void |
1232
2e8c3f8bb77f
remove Chunk base class
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1198
diff
changeset
|
129 |
ArpHeader::Serialize (Buffer::Iterator start) const |
242 | 130 |
{ |
131 |
Buffer::Iterator i = start; |
|
286
57e6a2006962
convert use of <cassert> to "ns3/assert.h"
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
258
diff
changeset
|
132 |
NS_ASSERT (m_macSource.GetLength () == m_macDest.GetLength ()); |
242 | 133 |
|
134 |
/* ethernet */ |
|
135 |
i.WriteHtonU16 (0x0001); |
|
136 |
/* ipv4 */ |
|
137 |
i.WriteHtonU16 (0x0800); |
|
258
df3bca25ae37
make sure that mac addr len is correctly written
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
242
diff
changeset
|
138 |
i.WriteU8 (m_macSource.GetLength ()); |
242 | 139 |
i.WriteU8 (4); |
140 |
i.WriteHtonU16 (m_type); |
|
141 |
WriteTo (i, m_macSource); |
|
142 |
WriteTo (i, m_ipv4Source); |
|
143 |
WriteTo (i, m_macDest); |
|
144 |
WriteTo (i, m_ipv4Dest); |
|
145 |
} |
|
4170
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
146 |
|
463
c2082308e01a
merge Packet API changes needed for Packet pretty printing.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
147 |
uint32_t |
1232
2e8c3f8bb77f
remove Chunk base class
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1198
diff
changeset
|
148 |
ArpHeader::Deserialize (Buffer::Iterator start) |
242 | 149 |
{ |
150 |
Buffer::Iterator i = start; |
|
4184
e7a525be7dc2
remove misguided __attribute__ ((unused))s
Craig Dowell <craigdo@ee.washington.edu>
parents:
4183
diff
changeset
|
151 |
i.Next (2); // Skip HRD |
4170
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
152 |
uint32_t protocolType = i.ReadNtohU16 (); // Read PRO |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
153 |
uint32_t hardwareAddressLen = i.ReadU8 (); // Read HLN |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
154 |
uint32_t protocolAddressLen = i.ReadU8 (); // Read PLN |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
155 |
|
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
156 |
// |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
157 |
// It is implicit here that we have a protocol type of 0x800 (IP). |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
158 |
// It is also implicit here that we are using Ipv4 (PLN == 4). |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
159 |
// If this isn't the case, we need to return an error since we don't want to |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
160 |
// be too fragile if we get connected to real networks. |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
161 |
// |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
162 |
if (protocolType != 0x800 || protocolAddressLen != 4) |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
163 |
{ |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
164 |
return 0; |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
165 |
} |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
166 |
|
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
167 |
m_type = i.ReadNtohU16 (); // Read OP |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
168 |
ReadFrom (i, m_macSource, hardwareAddressLen); // Read SHA (size HLN) |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
169 |
ReadFrom (i, m_ipv4Source); // Read SPA (size PLN == 4) |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
170 |
ReadFrom (i, m_macDest, hardwareAddressLen); // Read THA (size HLN) |
8e5bdeb6a71b
turn on checksums in example csma-tap-bridge and tweaks to make ping demo work
Craig Dowell <craigdo@ee.washington.edu>
parents:
3260
diff
changeset
|
171 |
ReadFrom (i, m_ipv4Dest); // Read TPA (size PLN == 4) |
463
c2082308e01a
merge Packet API changes needed for Packet pretty printing.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
286
diff
changeset
|
172 |
return GetSerializedSize (); |
242 | 173 |
} |
174 |
||
175 |
}; // namespace ns3 |