00001 #include "icmpv4.h"
00002 #include "ns3/packet.h"
00003
00004 namespace ns3 {
00005
00006
00007
00008
00009
00010 TypeId
00011 Icmpv4Header::GetTypeId (void)
00012 {
00013 static TypeId tid = TypeId ("ns3::Icmpv4Header")
00014 .SetParent<Header> ()
00015 .AddConstructor<Icmpv4Header> ()
00016 ;
00017 return tid;
00018 }
00019 Icmpv4Header::Icmpv4Header ()
00020 : m_type (0),
00021 m_code (0),
00022 m_calcChecksum (false)
00023 {}
00024 Icmpv4Header::~Icmpv4Header ()
00025 {}
00026 void
00027 Icmpv4Header::EnableChecksum (void)
00028 {
00029 m_calcChecksum = true;
00030 }
00031 TypeId
00032 Icmpv4Header::GetInstanceTypeId (void) const
00033 {
00034 return GetTypeId ();
00035 }
00036 uint32_t
00037 Icmpv4Header::GetSerializedSize (void) const
00038 {
00039 return 4;
00040 }
00041 void
00042 Icmpv4Header::Serialize (Buffer::Iterator start) const
00043 {
00044 Buffer::Iterator i = start;
00045 i.WriteU8 (m_type);
00046 i.WriteU8 (m_code);
00047 i.WriteHtonU16 (0);
00048 if (m_calcChecksum)
00049 {
00050 i = start;
00051 uint16_t checksum = i.CalculateIpChecksum (i.GetSize ());
00052 i = start;
00053 i.Next (2);
00054 i.WriteU16 (checksum);
00055 }
00056
00057 }
00058 uint32_t
00059 Icmpv4Header::Deserialize (Buffer::Iterator start)
00060 {
00061 m_type = start.ReadU8 ();
00062 m_code = start.ReadU8 ();
00063 uint16_t checksum;
00064 checksum = start.ReadNtohU16 ();
00065 return 4;
00066 }
00067 void
00068 Icmpv4Header::Print (std::ostream &os) const
00069 {
00070 os << "type=" << (uint32_t)m_type << ", code=" << (uint32_t)m_code;
00071 }
00072
00073 void
00074 Icmpv4Header::SetType (uint8_t type)
00075 {
00076 m_type = type;
00077 }
00078 void
00079 Icmpv4Header::SetCode (uint8_t code)
00080 {
00081 m_code = code;
00082 }
00083 uint8_t
00084 Icmpv4Header::GetType (void) const
00085 {
00086 return m_type;
00087 }
00088 uint8_t
00089 Icmpv4Header::GetCode (void) const
00090 {
00091 return m_code;
00092 }
00093
00094
00095
00096
00097
00098 void
00099 Icmpv4Echo::SetIdentifier (uint16_t id)
00100 {
00101 m_identifier = id;
00102 }
00103 void
00104 Icmpv4Echo::SetSequenceNumber (uint16_t seq)
00105 {
00106 m_sequence = seq;
00107 }
00108 void
00109 Icmpv4Echo::SetData (Ptr<const Packet> data)
00110 {
00111 m_data = data->Copy ();
00112 }
00113 uint16_t
00114 Icmpv4Echo::GetIdentifier (void) const
00115 {
00116 return m_identifier;
00117 }
00118 uint16_t
00119 Icmpv4Echo::GetSequenceNumber (void) const
00120 {
00121 return m_sequence;
00122 }
00123 Ptr<const Packet>
00124 Icmpv4Echo::GetData (void) const
00125 {
00126 return m_data->Copy ();
00127 }
00128
00129
00130 TypeId
00131 Icmpv4Echo::GetTypeId (void)
00132 {
00133 static TypeId tid = TypeId ("ns3::Icmpv4Echo")
00134 .SetParent<Header> ()
00135 .AddConstructor<Icmpv4Echo> ()
00136 ;
00137 return tid;
00138 }
00139 Icmpv4Echo::Icmpv4Echo ()
00140 : m_identifier (0),
00141 m_sequence (0),
00142 m_data (0)
00143 {}
00144 Icmpv4Echo::~Icmpv4Echo ()
00145 {}
00146 TypeId
00147 Icmpv4Echo::GetInstanceTypeId (void) const
00148 {
00149 return GetTypeId ();
00150 }
00151 uint32_t
00152 Icmpv4Echo::GetSerializedSize (void) const
00153 {
00154 return 4 + m_data->GetSize ();
00155 }
00156 void
00157 Icmpv4Echo::Serialize (Buffer::Iterator start) const
00158 {
00159 start.WriteHtonU16 (m_identifier);
00160 start.WriteHtonU16 (m_sequence);
00161 start.Write (m_data->PeekData (), m_data->GetSize ());
00162 }
00163 uint32_t
00164 Icmpv4Echo::Deserialize (Buffer::Iterator start)
00165 {
00166 m_identifier = start.ReadNtohU16 ();
00167 m_sequence = start.ReadNtohU16 ();
00168 NS_ASSERT (start.GetSize () >= 4);
00169 uint32_t size = start.GetSize () - 4;
00170 uint8_t *buffer = new uint8_t[size] ();
00171 start.Read (buffer, size);
00172 m_data = Create<Packet> (buffer, size);
00173 delete[] buffer;
00174 return start.GetSize ();
00175 }
00176 void
00177 Icmpv4Echo::Print (std::ostream &os) const
00178 {
00179 os << "identifier=" << m_identifier << ", sequence=" << m_sequence;
00180 }
00181
00182
00183
00184
00185
00186
00187 TypeId
00188 Icmpv4DestinationUnreachable::GetTypeId (void)
00189 {
00190 static TypeId tid = TypeId ("ns3::Icmpv4DestinationUnreachable")
00191 .SetParent<Header> ()
00192 .AddConstructor<Icmpv4DestinationUnreachable> ()
00193 ;
00194 return tid;
00195 }
00196 Icmpv4DestinationUnreachable::Icmpv4DestinationUnreachable ()
00197 {
00198
00199
00200 for (uint8_t j = 0; j < 8; j++)
00201 {
00202 m_data[j] = 0;
00203 }
00204 }
00205
00206 void
00207 Icmpv4DestinationUnreachable::SetNextHopMtu (uint16_t mtu)
00208 {
00209 m_nextHopMtu = mtu;
00210 }
00211 uint16_t
00212 Icmpv4DestinationUnreachable::GetNextHopMtu (void) const
00213 {
00214 return m_nextHopMtu;
00215 }
00216
00217 void
00218 Icmpv4DestinationUnreachable::SetData (Ptr<const Packet> data)
00219 {
00220 data->CopyData (m_data, 8);
00221 }
00222 void
00223 Icmpv4DestinationUnreachable::SetHeader (Ipv4Header header)
00224 {
00225 m_header = header;
00226 }
00227 void
00228 Icmpv4DestinationUnreachable::GetData (uint8_t payload[8]) const
00229 {
00230 memcpy (payload, m_data, 8);
00231 }
00232 Ipv4Header
00233 Icmpv4DestinationUnreachable::GetHeader (void) const
00234 {
00235 return m_header;
00236 }
00237
00238
00239 Icmpv4DestinationUnreachable::~Icmpv4DestinationUnreachable ()
00240 {}
00241 TypeId
00242 Icmpv4DestinationUnreachable::GetInstanceTypeId (void) const
00243 {
00244 return GetTypeId ();
00245 }
00246 uint32_t
00247 Icmpv4DestinationUnreachable::GetSerializedSize (void) const
00248 {
00249 return 4 + m_header.GetSerializedSize () + 8;
00250 }
00251 void
00252 Icmpv4DestinationUnreachable::Serialize (Buffer::Iterator start) const
00253 {
00254 start.WriteU16 (0);
00255 start.WriteHtonU16 (m_nextHopMtu);
00256 uint32_t size = m_header.GetSerializedSize ();
00257 m_header.Serialize (start);
00258 start.Next (size);
00259 start.Write (m_data, 8);
00260 }
00261
00262 uint32_t
00263 Icmpv4DestinationUnreachable::Deserialize (Buffer::Iterator start)
00264 {
00265 Buffer::Iterator i = start;
00266 i.Next (2);
00267 m_nextHopMtu = i.ReadNtohU16 ();
00268 uint32_t read = m_header.Deserialize (i);
00269 i.Next (read);
00270 for (uint8_t j = 0; j < 8; j++)
00271 {
00272 m_data[j] = i.ReadU8 ();
00273 }
00274 return i.GetDistanceFrom (start);
00275 }
00276 void
00277 Icmpv4DestinationUnreachable::Print (std::ostream &os) const
00278 {
00279 m_header.Print (os);
00280 os << " org data=";
00281 for (uint8_t i = 0; i < 8; i++)
00282 {
00283 os << (uint32_t) m_data[i];
00284 if (i != 8)
00285 {
00286 os << " ";
00287 }
00288 }
00289 }
00290
00291
00292
00293
00294
00295 TypeId
00296 Icmpv4TimeExceeded::GetTypeId (void)
00297 {
00298 static TypeId tid = TypeId ("ns3::Icmpv4TimeExceeded")
00299 .SetParent<Header> ()
00300 .AddConstructor<Icmpv4TimeExceeded> ()
00301 ;
00302 return tid;
00303 }
00304 Icmpv4TimeExceeded::Icmpv4TimeExceeded ()
00305 {
00306
00307
00308 for (uint8_t j = 0; j < 8; j++)
00309 {
00310 m_data[j] = 0;
00311 }
00312 }
00313
00314
00315 void
00316 Icmpv4TimeExceeded::SetData (Ptr<const Packet> data)
00317 {
00318 data->CopyData (m_data, 8);
00319 }
00320 void
00321 Icmpv4TimeExceeded::SetHeader (Ipv4Header header)
00322 {
00323 m_header = header;
00324 }
00325 void
00326 Icmpv4TimeExceeded::GetData (uint8_t payload[8]) const
00327 {
00328 memcpy (payload, m_data, 8);
00329 }
00330 Ipv4Header
00331 Icmpv4TimeExceeded::GetHeader (void) const
00332 {
00333 return m_header;
00334 }
00335
00336
00337 Icmpv4TimeExceeded::~Icmpv4TimeExceeded ()
00338 {}
00339 TypeId
00340 Icmpv4TimeExceeded::GetInstanceTypeId (void) const
00341 {
00342 return GetTypeId ();
00343 }
00344 uint32_t
00345 Icmpv4TimeExceeded::GetSerializedSize (void) const
00346 {
00347 return 4 + m_header.GetSerializedSize () + 8;
00348 }
00349 void
00350 Icmpv4TimeExceeded::Serialize (Buffer::Iterator start) const
00351 {
00352 start.WriteU32 (0);
00353 uint32_t size = m_header.GetSerializedSize ();
00354 m_header.Serialize (start);
00355 start.Next (size);
00356 start.Write (m_data, 8);
00357 }
00358
00359 uint32_t
00360 Icmpv4TimeExceeded::Deserialize (Buffer::Iterator start)
00361 {
00362 Buffer::Iterator i = start;
00363 i.Next (4);
00364 uint32_t read = m_header.Deserialize (i);
00365 i.Next (read);
00366 for (uint8_t j = 0; j < 8; j++)
00367 {
00368 m_data[j] = i.ReadU8 ();
00369 }
00370 return i.GetDistanceFrom (start);
00371 }
00372 void
00373 Icmpv4TimeExceeded::Print (std::ostream &os) const
00374 {
00375 m_header.Print (os);
00376 os << " org data=";
00377 for (uint8_t i = 0; i < 8; i++)
00378 {
00379 os << (uint32_t) m_data[i];
00380 if (i != 8)
00381 {
00382 os << " ";
00383 }
00384 }
00385 }
00386
00387 }