|
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 "udp-header.h" |
|
22 #include "ipv4-checksum.h" |
|
23 |
|
24 namespace ns3 { |
|
25 |
|
26 NS_OBJECT_ENSURE_REGISTERED (UdpHeader); |
|
27 |
|
28 bool UdpHeader::m_calcChecksum = false; |
|
29 |
|
30 /* The magic values below are used only for debugging. |
|
31 * They can be used to easily detect memory corruption |
|
32 * problems so you can see the patterns in memory. |
|
33 */ |
|
34 UdpHeader::UdpHeader () |
|
35 : m_sourcePort (0xfffd), |
|
36 m_destinationPort (0xfffd), |
|
37 m_payloadSize (0xfffd), |
|
38 m_initialChecksum (0) |
|
39 {} |
|
40 UdpHeader::~UdpHeader () |
|
41 { |
|
42 m_sourcePort = 0xfffe; |
|
43 m_destinationPort = 0xfffe; |
|
44 m_payloadSize = 0xfffe; |
|
45 } |
|
46 |
|
47 void |
|
48 UdpHeader::EnableChecksums (void) |
|
49 { |
|
50 m_calcChecksum = true; |
|
51 } |
|
52 |
|
53 void |
|
54 UdpHeader::SetDestinationPort (uint16_t port) |
|
55 { |
|
56 m_destinationPort = port; |
|
57 } |
|
58 void |
|
59 UdpHeader::SetSourcePort (uint16_t port) |
|
60 { |
|
61 m_sourcePort = port; |
|
62 } |
|
63 uint16_t |
|
64 UdpHeader::GetSourcePort (void) const |
|
65 { |
|
66 return m_sourcePort; |
|
67 } |
|
68 uint16_t |
|
69 UdpHeader::GetDestinationPort (void) const |
|
70 { |
|
71 return m_destinationPort; |
|
72 } |
|
73 void |
|
74 UdpHeader::SetPayloadSize (uint16_t size) |
|
75 { |
|
76 m_payloadSize = size; |
|
77 } |
|
78 void |
|
79 UdpHeader::InitializeChecksum (Ipv4Address source, |
|
80 Ipv4Address destination, |
|
81 uint8_t protocol) |
|
82 { |
|
83 uint8_t buf[12]; |
|
84 source.Serialize (buf); |
|
85 destination.Serialize (buf+4); |
|
86 buf[8] = 0; |
|
87 buf[9] = protocol; |
|
88 uint16_t udpLength = m_payloadSize + GetSerializedSize (); |
|
89 buf[10] = udpLength >> 8; |
|
90 buf[11] = udpLength & 0xff; |
|
91 |
|
92 m_initialChecksum = Ipv4ChecksumCalculate (0, buf, 12); |
|
93 } |
|
94 |
|
95 TypeId |
|
96 UdpHeader::GetTypeId (void) |
|
97 { |
|
98 static TypeId tid = TypeId ("ns3::UdpHeader") |
|
99 .SetParent<Header> () |
|
100 .AddConstructor<UdpHeader> () |
|
101 ; |
|
102 return tid; |
|
103 } |
|
104 TypeId |
|
105 UdpHeader::GetInstanceTypeId (void) const |
|
106 { |
|
107 return GetTypeId (); |
|
108 } |
|
109 void |
|
110 UdpHeader::Print (std::ostream &os) const |
|
111 { |
|
112 os << "length: " << m_payloadSize + GetSerializedSize () |
|
113 << " " |
|
114 << m_sourcePort << " > " << m_destinationPort |
|
115 ; |
|
116 } |
|
117 |
|
118 uint32_t |
|
119 UdpHeader::GetSerializedSize (void) const |
|
120 { |
|
121 return 8; |
|
122 } |
|
123 |
|
124 void |
|
125 UdpHeader::Serialize (Buffer::Iterator start) const |
|
126 { |
|
127 Buffer::Iterator i = start; |
|
128 i.WriteHtonU16 (m_sourcePort); |
|
129 i.WriteHtonU16 (m_destinationPort); |
|
130 i.WriteHtonU16 (m_payloadSize + GetSerializedSize ()); |
|
131 i.WriteU16 (0); |
|
132 |
|
133 if (m_calcChecksum) |
|
134 { |
|
135 #if 0 |
|
136 //XXXX |
|
137 uint16_t checksum = Ipv4ChecksumCalculate (m_initialChecksum, |
|
138 buffer->PeekData (), |
|
139 GetSerializedSize () + m_payloadSize); |
|
140 checksum = Ipv4ChecksumComplete (checksum); |
|
141 i = buffer->Begin (); |
|
142 i.Next (6); |
|
143 i.WriteU16 (checksum); |
|
144 #endif |
|
145 } |
|
146 } |
|
147 uint32_t |
|
148 UdpHeader::Deserialize (Buffer::Iterator start) |
|
149 { |
|
150 Buffer::Iterator i = start; |
|
151 m_sourcePort = i.ReadNtohU16 (); |
|
152 m_destinationPort = i.ReadNtohU16 (); |
|
153 m_payloadSize = i.ReadNtohU16 () - GetSerializedSize (); |
|
154 if (m_calcChecksum) |
|
155 { |
|
156 // XXX verify checksum. |
|
157 } |
|
158 return GetSerializedSize (); |
|
159 } |
|
160 |
|
161 |
|
162 }; // namespace ns3 |