inet.cc (7811:a8fc35183c10) inet.cc (7814:c819526b7c2a)
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 * Gabe Black
31 */
32
33#include <cstdio>
34#include <sstream>
35#include <string>
36
37#include "base/cprintf.hh"
38#include "base/inet.hh"
39#include "base/types.hh"
40
41using namespace std;
42namespace Net {
43
44EthAddr::EthAddr()
45{
46 memset(data, 0, ETH_ADDR_LEN);
47}
48
49EthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN])
50{
51 *data = *ea;
52}
53
54EthAddr::EthAddr(const eth_addr &ea)
55{
56 *data = *ea.data;
57}
58
59EthAddr::EthAddr(const std::string &addr)
60{
61 parse(addr);
62}
63
64const EthAddr &
65EthAddr::operator=(const eth_addr &ea)
66{
67 *data = *ea.data;
68 return *this;
69}
70
71const EthAddr &
72EthAddr::operator=(const std::string &addr)
73{
74 parse(addr);
75 return *this;
76}
77
78void
79EthAddr::parse(const std::string &addr)
80{
81 // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise
82 // the sscanf function won't work.
83 int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1];
84 if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1],
85 &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) {
86 memset(data, 0xff, ETH_ADDR_LEN);
87 return;
88 }
89
90 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
91 if (bytes[i] & ~0xff) {
92 memset(data, 0xff, ETH_ADDR_LEN);
93 return;
94 }
95
96 data[i] = bytes[i];
97 }
98}
99
100string
101EthAddr::string() const
102{
103 stringstream stream;
104 stream << *this;
105 return stream.str();
106}
107
108bool
109operator==(const EthAddr &left, const EthAddr &right)
110{
111 return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN);
112}
113
114ostream &
115operator<<(ostream &stream, const EthAddr &ea)
116{
117 const uint8_t *a = ea.addr();
118 ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]);
119 return stream;
120}
121
122string
123IpAddress::string() const
124{
125 stringstream stream;
126 stream << *this;
127 return stream.str();
128}
129
130bool
131operator==(const IpAddress &left, const IpAddress &right)
132{
133 return left.ip() == right.ip();
134}
135
136ostream &
137operator<<(ostream &stream, const IpAddress &ia)
138{
139 uint32_t ip = ia.ip();
140 ccprintf(stream, "%x.%x.%x.%x",
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 * Gabe Black
31 */
32
33#include <cstdio>
34#include <sstream>
35#include <string>
36
37#include "base/cprintf.hh"
38#include "base/inet.hh"
39#include "base/types.hh"
40
41using namespace std;
42namespace Net {
43
44EthAddr::EthAddr()
45{
46 memset(data, 0, ETH_ADDR_LEN);
47}
48
49EthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN])
50{
51 *data = *ea;
52}
53
54EthAddr::EthAddr(const eth_addr &ea)
55{
56 *data = *ea.data;
57}
58
59EthAddr::EthAddr(const std::string &addr)
60{
61 parse(addr);
62}
63
64const EthAddr &
65EthAddr::operator=(const eth_addr &ea)
66{
67 *data = *ea.data;
68 return *this;
69}
70
71const EthAddr &
72EthAddr::operator=(const std::string &addr)
73{
74 parse(addr);
75 return *this;
76}
77
78void
79EthAddr::parse(const std::string &addr)
80{
81 // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise
82 // the sscanf function won't work.
83 int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1];
84 if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1],
85 &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) {
86 memset(data, 0xff, ETH_ADDR_LEN);
87 return;
88 }
89
90 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
91 if (bytes[i] & ~0xff) {
92 memset(data, 0xff, ETH_ADDR_LEN);
93 return;
94 }
95
96 data[i] = bytes[i];
97 }
98}
99
100string
101EthAddr::string() const
102{
103 stringstream stream;
104 stream << *this;
105 return stream.str();
106}
107
108bool
109operator==(const EthAddr &left, const EthAddr &right)
110{
111 return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN);
112}
113
114ostream &
115operator<<(ostream &stream, const EthAddr &ea)
116{
117 const uint8_t *a = ea.addr();
118 ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]);
119 return stream;
120}
121
122string
123IpAddress::string() const
124{
125 stringstream stream;
126 stream << *this;
127 return stream.str();
128}
129
130bool
131operator==(const IpAddress &left, const IpAddress &right)
132{
133 return left.ip() == right.ip();
134}
135
136ostream &
137operator<<(ostream &stream, const IpAddress &ia)
138{
139 uint32_t ip = ia.ip();
140 ccprintf(stream, "%x.%x.%x.%x",
141 (uint8_t)(ip >> 0), (uint8_t)(ip >> 8),
142 (uint8_t)(ip >> 16), (uint8_t)(ip >> 24));
141 (uint8_t)(ip >> 24), (uint8_t)(ip >> 16),
142 (uint8_t)(ip >> 8), (uint8_t)(ip >> 0));
143 return stream;
144}
145
146string
147IpNetmask::string() const
148{
149 stringstream stream;
150 stream << *this;
151 return stream.str();
152}
153
154bool
155operator==(const IpNetmask &left, const IpNetmask &right)
156{
157 return (left.ip() == right.ip()) &&
158 (left.netmask() == right.netmask());
159}
160
161ostream &
162operator<<(ostream &stream, const IpNetmask &in)
163{
164 ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask());
165 return stream;
166}
167
168string
169IpWithPort::string() const
170{
171 stringstream stream;
172 stream << *this;
173 return stream.str();
174}
175
176bool
177operator==(const IpWithPort &left, const IpWithPort &right)
178{
179 return (left.ip() == right.ip()) && (left.port() == right.port());
180}
181
182ostream &
183operator<<(ostream &stream, const IpWithPort &iwp)
184{
185 ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port());
186 return stream;
187}
188
189uint16_t
190cksum(const IpPtr &ptr)
191{
192 int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0);
193 return ip_cksum_carry(sum);
194}
195
196uint16_t
197__tu_cksum(const IpPtr &ip)
198{
199 int tcplen = ip->len() - ip->hlen();
200 int sum = ip_cksum_add(ip->payload(), tcplen, 0);
201 sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination
202 sum += htons(ip->ip_p + tcplen);
203 return ip_cksum_carry(sum);
204}
205
206uint16_t
207cksum(const TcpPtr &tcp)
208{ return __tu_cksum(IpPtr(tcp.packet())); }
209
210uint16_t
211cksum(const UdpPtr &udp)
212{ return __tu_cksum(IpPtr(udp.packet())); }
213
214bool
215IpHdr::options(vector<const IpOpt *> &vec) const
216{
217 vec.clear();
218
219 const uint8_t *data = bytes() + sizeof(struct ip_hdr);
220 int all = hlen() - sizeof(struct ip_hdr);
221 while (all > 0) {
222 const IpOpt *opt = (const IpOpt *)data;
223 int len = opt->len();
224 if (all < len)
225 return false;
226
227 vec.push_back(opt);
228 all -= len;
229 data += len;
230 }
231
232 return true;
233}
234
235bool
236TcpHdr::options(vector<const TcpOpt *> &vec) const
237{
238 vec.clear();
239
240 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
241 int all = off() - sizeof(struct tcp_hdr);
242 while (all > 0) {
243 const TcpOpt *opt = (const TcpOpt *)data;
244 int len = opt->len();
245 if (all < len)
246 return false;
247
248 vec.push_back(opt);
249 all -= len;
250 data += len;
251 }
252
253 return true;
254}
255
256bool
257TcpOpt::sack(vector<SackRange> &vec) const
258{
259 vec.clear();
260
261 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
262 int all = len() - offsetof(tcp_opt, opt_data.sack);
263 while (all > 0) {
264 const uint16_t *sack = (const uint16_t *)data;
265 int len = sizeof(uint16_t) * 2;
266 if (all < len) {
267 vec.clear();
268 return false;
269 }
270
271 vec.push_back(RangeIn(ntohs(sack[0]), ntohs(sack[1])));
272 all -= len;
273 data += len;
274 }
275
276 return false;
277}
278
279int
280hsplit(const EthPacketPtr &ptr)
281{
282 int split_point = 0;
283
284 IpPtr ip(ptr);
285 if (ip) {
286 split_point = ip.pstart();
287
288 TcpPtr tcp(ip);
289 if (tcp)
290 split_point = tcp.pstart();
291
292 UdpPtr udp(ip);
293 if (udp)
294 split_point = udp.pstart();
295 }
296 return split_point;
297}
298
299
300} // namespace Net
143 return stream;
144}
145
146string
147IpNetmask::string() const
148{
149 stringstream stream;
150 stream << *this;
151 return stream.str();
152}
153
154bool
155operator==(const IpNetmask &left, const IpNetmask &right)
156{
157 return (left.ip() == right.ip()) &&
158 (left.netmask() == right.netmask());
159}
160
161ostream &
162operator<<(ostream &stream, const IpNetmask &in)
163{
164 ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask());
165 return stream;
166}
167
168string
169IpWithPort::string() const
170{
171 stringstream stream;
172 stream << *this;
173 return stream.str();
174}
175
176bool
177operator==(const IpWithPort &left, const IpWithPort &right)
178{
179 return (left.ip() == right.ip()) && (left.port() == right.port());
180}
181
182ostream &
183operator<<(ostream &stream, const IpWithPort &iwp)
184{
185 ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port());
186 return stream;
187}
188
189uint16_t
190cksum(const IpPtr &ptr)
191{
192 int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0);
193 return ip_cksum_carry(sum);
194}
195
196uint16_t
197__tu_cksum(const IpPtr &ip)
198{
199 int tcplen = ip->len() - ip->hlen();
200 int sum = ip_cksum_add(ip->payload(), tcplen, 0);
201 sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination
202 sum += htons(ip->ip_p + tcplen);
203 return ip_cksum_carry(sum);
204}
205
206uint16_t
207cksum(const TcpPtr &tcp)
208{ return __tu_cksum(IpPtr(tcp.packet())); }
209
210uint16_t
211cksum(const UdpPtr &udp)
212{ return __tu_cksum(IpPtr(udp.packet())); }
213
214bool
215IpHdr::options(vector<const IpOpt *> &vec) const
216{
217 vec.clear();
218
219 const uint8_t *data = bytes() + sizeof(struct ip_hdr);
220 int all = hlen() - sizeof(struct ip_hdr);
221 while (all > 0) {
222 const IpOpt *opt = (const IpOpt *)data;
223 int len = opt->len();
224 if (all < len)
225 return false;
226
227 vec.push_back(opt);
228 all -= len;
229 data += len;
230 }
231
232 return true;
233}
234
235bool
236TcpHdr::options(vector<const TcpOpt *> &vec) const
237{
238 vec.clear();
239
240 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
241 int all = off() - sizeof(struct tcp_hdr);
242 while (all > 0) {
243 const TcpOpt *opt = (const TcpOpt *)data;
244 int len = opt->len();
245 if (all < len)
246 return false;
247
248 vec.push_back(opt);
249 all -= len;
250 data += len;
251 }
252
253 return true;
254}
255
256bool
257TcpOpt::sack(vector<SackRange> &vec) const
258{
259 vec.clear();
260
261 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
262 int all = len() - offsetof(tcp_opt, opt_data.sack);
263 while (all > 0) {
264 const uint16_t *sack = (const uint16_t *)data;
265 int len = sizeof(uint16_t) * 2;
266 if (all < len) {
267 vec.clear();
268 return false;
269 }
270
271 vec.push_back(RangeIn(ntohs(sack[0]), ntohs(sack[1])));
272 all -= len;
273 data += len;
274 }
275
276 return false;
277}
278
279int
280hsplit(const EthPacketPtr &ptr)
281{
282 int split_point = 0;
283
284 IpPtr ip(ptr);
285 if (ip) {
286 split_point = ip.pstart();
287
288 TcpPtr tcp(ip);
289 if (tcp)
290 split_point = tcp.pstart();
291
292 UdpPtr udp(ip);
293 if (udp)
294 split_point = udp.pstart();
295 }
296 return split_point;
297}
298
299
300} // namespace Net