inet.cc (11793:ef606668d247) inet.cc (13449:2f7efa89c58b)
1/*
2 * Copyright (c) 2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 * Gabe Black
43 * Geoffrey Blake
44 */
45
46#include "base/inet.hh"
47
48#include <cstddef>
49#include <cstdio>
50#include <sstream>
51#include <string>
52
53#include "base/cprintf.hh"
1/*
2 * Copyright (c) 2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 * Gabe Black
43 * Geoffrey Blake
44 */
45
46#include "base/inet.hh"
47
48#include <cstddef>
49#include <cstdio>
50#include <sstream>
51#include <string>
52
53#include "base/cprintf.hh"
54#include "base/logging.hh"
54#include "base/types.hh"
55
56using namespace std;
57namespace Net {
58
59EthAddr::EthAddr()
60{
61 memset(data, 0, ETH_ADDR_LEN);
62}
63
64EthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN])
65{
66 for (int i = 0; i < ETH_ADDR_LEN; ++i)
67 data[i] = ea[i];
68}
69
70EthAddr::EthAddr(const eth_addr &ea)
71{
72 for (int i = 0; i < ETH_ADDR_LEN; ++i)
73 data[i] = ea.data[i];
74}
75
76EthAddr::EthAddr(const std::string &addr)
77{
78 parse(addr);
79}
80
81const EthAddr &
82EthAddr::operator=(const eth_addr &ea)
83{
84 *data = *ea.data;
85 return *this;
86}
87
88const EthAddr &
89EthAddr::operator=(const std::string &addr)
90{
91 parse(addr);
92 return *this;
93}
94
95void
96EthAddr::parse(const std::string &addr)
97{
98 // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise
99 // the sscanf function won't work.
100 int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1];
101 if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1],
102 &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) {
103 memset(data, 0xff, ETH_ADDR_LEN);
104 return;
105 }
106
107 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
108 if (bytes[i] & ~0xff) {
109 memset(data, 0xff, ETH_ADDR_LEN);
110 return;
111 }
112
113 data[i] = bytes[i];
114 }
115}
116
117string
118EthAddr::string() const
119{
120 stringstream stream;
121 stream << *this;
122 return stream.str();
123}
124
125bool
126operator==(const EthAddr &left, const EthAddr &right)
127{
128 return !memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN);
129}
130
131ostream &
132operator<<(ostream &stream, const EthAddr &ea)
133{
134 const uint8_t *a = ea.addr();
135 ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]);
136 return stream;
137}
138
139string
140IpAddress::string() const
141{
142 stringstream stream;
143 stream << *this;
144 return stream.str();
145}
146
147bool
148operator==(const IpAddress &left, const IpAddress &right)
149{
150 return left.ip() == right.ip();
151}
152
153ostream &
154operator<<(ostream &stream, const IpAddress &ia)
155{
156 uint32_t ip = ia.ip();
157 ccprintf(stream, "%x.%x.%x.%x",
158 (uint8_t)(ip >> 24), (uint8_t)(ip >> 16),
159 (uint8_t)(ip >> 8), (uint8_t)(ip >> 0));
160 return stream;
161}
162
163string
164IpNetmask::string() const
165{
166 stringstream stream;
167 stream << *this;
168 return stream.str();
169}
170
171bool
172operator==(const IpNetmask &left, const IpNetmask &right)
173{
174 return (left.ip() == right.ip()) &&
175 (left.netmask() == right.netmask());
176}
177
178ostream &
179operator<<(ostream &stream, const IpNetmask &in)
180{
181 ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask());
182 return stream;
183}
184
185string
186IpWithPort::string() const
187{
188 stringstream stream;
189 stream << *this;
190 return stream.str();
191}
192
193bool
194operator==(const IpWithPort &left, const IpWithPort &right)
195{
196 return (left.ip() == right.ip()) && (left.port() == right.port());
197}
198
199ostream &
200operator<<(ostream &stream, const IpWithPort &iwp)
201{
202 ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port());
203 return stream;
204}
205
206uint16_t
207cksum(const IpPtr &ptr)
208{
209 int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0);
210 return ip_cksum_carry(sum);
211}
212
213uint16_t
214__tu_cksum(const IpPtr &ip)
215{
216 int tcplen = ip->len() - ip->hlen();
217 int sum = ip_cksum_add(ip->payload(), tcplen, 0);
218 sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination
219 sum += htons(ip->ip_p + tcplen);
220 return ip_cksum_carry(sum);
221}
222
223uint16_t
224__tu_cksum6(const Ip6Ptr &ip6)
225{
226 int tcplen = ip6->plen() - ip6->extensionLength();
227 int sum = ip_cksum_add(ip6->payload(), tcplen, 0);
228 sum = ip_cksum_add(ip6->src(), 32, sum);
229 sum += htons(ip6->proto() + tcplen);
230 return ip_cksum_carry(sum);
231}
232
233uint16_t
234cksum(const TcpPtr &tcp)
235{
236 if (IpPtr(tcp.packet())) {
237 return __tu_cksum(IpPtr(tcp.packet()));
238 } else if (Ip6Ptr(tcp.packet())) {
239 return __tu_cksum6(Ip6Ptr(tcp.packet()));
240 } else {
55#include "base/types.hh"
56
57using namespace std;
58namespace Net {
59
60EthAddr::EthAddr()
61{
62 memset(data, 0, ETH_ADDR_LEN);
63}
64
65EthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN])
66{
67 for (int i = 0; i < ETH_ADDR_LEN; ++i)
68 data[i] = ea[i];
69}
70
71EthAddr::EthAddr(const eth_addr &ea)
72{
73 for (int i = 0; i < ETH_ADDR_LEN; ++i)
74 data[i] = ea.data[i];
75}
76
77EthAddr::EthAddr(const std::string &addr)
78{
79 parse(addr);
80}
81
82const EthAddr &
83EthAddr::operator=(const eth_addr &ea)
84{
85 *data = *ea.data;
86 return *this;
87}
88
89const EthAddr &
90EthAddr::operator=(const std::string &addr)
91{
92 parse(addr);
93 return *this;
94}
95
96void
97EthAddr::parse(const std::string &addr)
98{
99 // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise
100 // the sscanf function won't work.
101 int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1];
102 if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1],
103 &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) {
104 memset(data, 0xff, ETH_ADDR_LEN);
105 return;
106 }
107
108 for (int i = 0; i < ETH_ADDR_LEN; ++i) {
109 if (bytes[i] & ~0xff) {
110 memset(data, 0xff, ETH_ADDR_LEN);
111 return;
112 }
113
114 data[i] = bytes[i];
115 }
116}
117
118string
119EthAddr::string() const
120{
121 stringstream stream;
122 stream << *this;
123 return stream.str();
124}
125
126bool
127operator==(const EthAddr &left, const EthAddr &right)
128{
129 return !memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN);
130}
131
132ostream &
133operator<<(ostream &stream, const EthAddr &ea)
134{
135 const uint8_t *a = ea.addr();
136 ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]);
137 return stream;
138}
139
140string
141IpAddress::string() const
142{
143 stringstream stream;
144 stream << *this;
145 return stream.str();
146}
147
148bool
149operator==(const IpAddress &left, const IpAddress &right)
150{
151 return left.ip() == right.ip();
152}
153
154ostream &
155operator<<(ostream &stream, const IpAddress &ia)
156{
157 uint32_t ip = ia.ip();
158 ccprintf(stream, "%x.%x.%x.%x",
159 (uint8_t)(ip >> 24), (uint8_t)(ip >> 16),
160 (uint8_t)(ip >> 8), (uint8_t)(ip >> 0));
161 return stream;
162}
163
164string
165IpNetmask::string() const
166{
167 stringstream stream;
168 stream << *this;
169 return stream.str();
170}
171
172bool
173operator==(const IpNetmask &left, const IpNetmask &right)
174{
175 return (left.ip() == right.ip()) &&
176 (left.netmask() == right.netmask());
177}
178
179ostream &
180operator<<(ostream &stream, const IpNetmask &in)
181{
182 ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask());
183 return stream;
184}
185
186string
187IpWithPort::string() const
188{
189 stringstream stream;
190 stream << *this;
191 return stream.str();
192}
193
194bool
195operator==(const IpWithPort &left, const IpWithPort &right)
196{
197 return (left.ip() == right.ip()) && (left.port() == right.port());
198}
199
200ostream &
201operator<<(ostream &stream, const IpWithPort &iwp)
202{
203 ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port());
204 return stream;
205}
206
207uint16_t
208cksum(const IpPtr &ptr)
209{
210 int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0);
211 return ip_cksum_carry(sum);
212}
213
214uint16_t
215__tu_cksum(const IpPtr &ip)
216{
217 int tcplen = ip->len() - ip->hlen();
218 int sum = ip_cksum_add(ip->payload(), tcplen, 0);
219 sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination
220 sum += htons(ip->ip_p + tcplen);
221 return ip_cksum_carry(sum);
222}
223
224uint16_t
225__tu_cksum6(const Ip6Ptr &ip6)
226{
227 int tcplen = ip6->plen() - ip6->extensionLength();
228 int sum = ip_cksum_add(ip6->payload(), tcplen, 0);
229 sum = ip_cksum_add(ip6->src(), 32, sum);
230 sum += htons(ip6->proto() + tcplen);
231 return ip_cksum_carry(sum);
232}
233
234uint16_t
235cksum(const TcpPtr &tcp)
236{
237 if (IpPtr(tcp.packet())) {
238 return __tu_cksum(IpPtr(tcp.packet()));
239 } else if (Ip6Ptr(tcp.packet())) {
240 return __tu_cksum6(Ip6Ptr(tcp.packet()));
241 } else {
241 assert(0);
242 panic("Unrecognized IP packet format");
242 }
243 // Should never reach here
244 return 0;
245}
246
247uint16_t
248cksum(const UdpPtr &udp)
249{
250 if (IpPtr(udp.packet())) {
251 return __tu_cksum(IpPtr(udp.packet()));
252 } else if (Ip6Ptr(udp.packet())) {
253 return __tu_cksum6(Ip6Ptr(udp.packet()));
254 } else {
243 }
244 // Should never reach here
245 return 0;
246}
247
248uint16_t
249cksum(const UdpPtr &udp)
250{
251 if (IpPtr(udp.packet())) {
252 return __tu_cksum(IpPtr(udp.packet()));
253 } else if (Ip6Ptr(udp.packet())) {
254 return __tu_cksum6(Ip6Ptr(udp.packet()));
255 } else {
255 assert(0);
256 panic("Unrecognized IP packet format");
256 }
257 return 0;
258}
259
260bool
261IpHdr::options(vector<const IpOpt *> &vec) const
262{
263 vec.clear();
264
265 const uint8_t *data = bytes() + sizeof(struct ip_hdr);
266 int all = hlen() - sizeof(struct ip_hdr);
267 while (all > 0) {
268 const IpOpt *opt = (const IpOpt *)data;
269 int len = opt->len();
270 if (all < len)
271 return false;
272
273 vec.push_back(opt);
274 all -= len;
275 data += len;
276 }
277
278 return true;
279}
280
281#define IP6_EXTENSION(nxt) (nxt == IP_PROTO_HOPOPTS) ? true : \
282 (nxt == IP_PROTO_ROUTING) ? true : \
283 (nxt == IP_PROTO_FRAGMENT) ? true : \
284 (nxt == IP_PROTO_AH) ? true : \
285 (nxt == IP_PROTO_ESP) ? true: \
286 (nxt == IP_PROTO_DSTOPTS) ? true : false
287
288/* Scan the IP6 header for all header extensions
289 * and return the number of headers found
290 */
291int
292Ip6Hdr::extensionLength() const
293{
294 const uint8_t *data = bytes() + IP6_HDR_LEN;
295 uint8_t nxt = ip6_nxt;
296 int len = 0;
297 int all = plen();
298
299 while (IP6_EXTENSION(nxt)) {
300 const Ip6Opt *ext = (const Ip6Opt *)data;
301 nxt = ext->nxt();
302 len += ext->len();
303 data += ext->len();
304 all -= ext->len();
305 assert(all >= 0);
306 }
307 return len;
308}
309
310/* Scan the IP6 header for a particular extension
311 * header type and return a pointer to it if it
312 * exists, otherwise return NULL
313 */
314const Ip6Opt*
315Ip6Hdr::getExt(uint8_t ext_type) const
316{
317 const uint8_t *data = bytes() + IP6_HDR_LEN;
318 uint8_t nxt = ip6_nxt;
319 Ip6Opt* opt = NULL;
320 int all = plen();
321
322 while (IP6_EXTENSION(nxt)) {
323 opt = (Ip6Opt *)data;
324 if (nxt == ext_type) {
325 break;
326 }
327 nxt = opt->nxt();
328 data += opt->len();
329 all -= opt->len();
330 opt = NULL;
331 assert(all >= 0);
332 }
333 return (const Ip6Opt*)opt;
334}
335
336/* Scan the IP6 header and any extension headers
337 * to find what type of Layer 4 header exists
338 * after this header
339 */
340uint8_t
341Ip6Hdr::proto() const
342{
343 const uint8_t *data = bytes() + IP6_HDR_LEN;
344 uint8_t nxt = ip6_nxt;
345 int all = plen();
346
347 while (IP6_EXTENSION(nxt)) {
348 const Ip6Opt *ext = (const Ip6Opt *)data;
349 nxt = ext->nxt();
350 data += ext->len();
351 all -= ext->len();
352 assert(all >= 0);
353 }
354 return nxt;
355}
356
357bool
358TcpHdr::options(vector<const TcpOpt *> &vec) const
359{
360 vec.clear();
361
362 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
363 int all = off() - sizeof(struct tcp_hdr);
364 while (all > 0) {
365 const TcpOpt *opt = (const TcpOpt *)data;
366 int len = opt->len();
367 if (all < len)
368 return false;
369
370 vec.push_back(opt);
371 all -= len;
372 data += len;
373 }
374
375 return true;
376}
377
378int
379hsplit(const EthPacketPtr &ptr)
380{
381 int split_point = 0;
382
383 IpPtr ip(ptr);
384 Ip6Ptr ip6(ptr);
385 if (ip) {
386 split_point = ip.pstart();
387
388 TcpPtr tcp(ip);
389 if (tcp)
390 split_point = tcp.pstart();
391
392 UdpPtr udp(ip);
393 if (udp)
394 split_point = udp.pstart();
395 } else if (ip6) {
396 split_point = ip6.pstart();
397
398 TcpPtr tcp(ip6);
399 if (tcp)
400 split_point = tcp.pstart();
401 UdpPtr udp(ip6);
402 if (udp)
403 split_point = udp.pstart();
404 }
405 return split_point;
406}
407
408
409} // namespace Net
257 }
258 return 0;
259}
260
261bool
262IpHdr::options(vector<const IpOpt *> &vec) const
263{
264 vec.clear();
265
266 const uint8_t *data = bytes() + sizeof(struct ip_hdr);
267 int all = hlen() - sizeof(struct ip_hdr);
268 while (all > 0) {
269 const IpOpt *opt = (const IpOpt *)data;
270 int len = opt->len();
271 if (all < len)
272 return false;
273
274 vec.push_back(opt);
275 all -= len;
276 data += len;
277 }
278
279 return true;
280}
281
282#define IP6_EXTENSION(nxt) (nxt == IP_PROTO_HOPOPTS) ? true : \
283 (nxt == IP_PROTO_ROUTING) ? true : \
284 (nxt == IP_PROTO_FRAGMENT) ? true : \
285 (nxt == IP_PROTO_AH) ? true : \
286 (nxt == IP_PROTO_ESP) ? true: \
287 (nxt == IP_PROTO_DSTOPTS) ? true : false
288
289/* Scan the IP6 header for all header extensions
290 * and return the number of headers found
291 */
292int
293Ip6Hdr::extensionLength() const
294{
295 const uint8_t *data = bytes() + IP6_HDR_LEN;
296 uint8_t nxt = ip6_nxt;
297 int len = 0;
298 int all = plen();
299
300 while (IP6_EXTENSION(nxt)) {
301 const Ip6Opt *ext = (const Ip6Opt *)data;
302 nxt = ext->nxt();
303 len += ext->len();
304 data += ext->len();
305 all -= ext->len();
306 assert(all >= 0);
307 }
308 return len;
309}
310
311/* Scan the IP6 header for a particular extension
312 * header type and return a pointer to it if it
313 * exists, otherwise return NULL
314 */
315const Ip6Opt*
316Ip6Hdr::getExt(uint8_t ext_type) const
317{
318 const uint8_t *data = bytes() + IP6_HDR_LEN;
319 uint8_t nxt = ip6_nxt;
320 Ip6Opt* opt = NULL;
321 int all = plen();
322
323 while (IP6_EXTENSION(nxt)) {
324 opt = (Ip6Opt *)data;
325 if (nxt == ext_type) {
326 break;
327 }
328 nxt = opt->nxt();
329 data += opt->len();
330 all -= opt->len();
331 opt = NULL;
332 assert(all >= 0);
333 }
334 return (const Ip6Opt*)opt;
335}
336
337/* Scan the IP6 header and any extension headers
338 * to find what type of Layer 4 header exists
339 * after this header
340 */
341uint8_t
342Ip6Hdr::proto() const
343{
344 const uint8_t *data = bytes() + IP6_HDR_LEN;
345 uint8_t nxt = ip6_nxt;
346 int all = plen();
347
348 while (IP6_EXTENSION(nxt)) {
349 const Ip6Opt *ext = (const Ip6Opt *)data;
350 nxt = ext->nxt();
351 data += ext->len();
352 all -= ext->len();
353 assert(all >= 0);
354 }
355 return nxt;
356}
357
358bool
359TcpHdr::options(vector<const TcpOpt *> &vec) const
360{
361 vec.clear();
362
363 const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
364 int all = off() - sizeof(struct tcp_hdr);
365 while (all > 0) {
366 const TcpOpt *opt = (const TcpOpt *)data;
367 int len = opt->len();
368 if (all < len)
369 return false;
370
371 vec.push_back(opt);
372 all -= len;
373 data += len;
374 }
375
376 return true;
377}
378
379int
380hsplit(const EthPacketPtr &ptr)
381{
382 int split_point = 0;
383
384 IpPtr ip(ptr);
385 Ip6Ptr ip6(ptr);
386 if (ip) {
387 split_point = ip.pstart();
388
389 TcpPtr tcp(ip);
390 if (tcp)
391 split_point = tcp.pstart();
392
393 UdpPtr udp(ip);
394 if (udp)
395 split_point = udp.pstart();
396 } else if (ip6) {
397 split_point = ip6.pstart();
398
399 TcpPtr tcp(ip6);
400 if (tcp)
401 split_point = tcp.pstart();
402 UdpPtr udp(ip6);
403 if (udp)
404 split_point = udp.pstart();
405 }
406 return split_point;
407}
408
409
410} // namespace Net