2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
35 #include "base/cprintf.hh"
36 #include "base/inet.hh"
37 #include "base/types.hh"
44 memset(data
, 0, ETH_ADDR_LEN
);
47 EthAddr::EthAddr(const uint8_t ea
[ETH_ADDR_LEN
])
52 EthAddr::EthAddr(const eth_addr
&ea
)
57 EthAddr::EthAddr(const std::string
&addr
)
63 EthAddr::operator=(const eth_addr
&ea
)
70 EthAddr::operator=(const std::string
&addr
)
77 EthAddr::parse(const std::string
&addr
)
79 // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise
80 // the sscanf function won't work.
81 int bytes
[ETH_ADDR_LEN
== 6 ? ETH_ADDR_LEN
: -1];
82 if (sscanf(addr
.c_str(), "%x:%x:%x:%x:%x:%x", &bytes
[0], &bytes
[1],
83 &bytes
[2], &bytes
[3], &bytes
[4], &bytes
[5]) != ETH_ADDR_LEN
) {
84 memset(data
, 0xff, ETH_ADDR_LEN
);
88 for (int i
= 0; i
< ETH_ADDR_LEN
; ++i
) {
89 if (bytes
[i
] & ~0xff) {
90 memset(data
, 0xff, ETH_ADDR_LEN
);
99 EthAddr::string() const
107 operator==(const EthAddr
&left
, const EthAddr
&right
)
109 return memcmp(left
.bytes(), right
.bytes(), ETH_ADDR_LEN
);
113 operator<<(ostream
&stream
, const EthAddr
&ea
)
115 const uint8_t *a
= ea
.addr();
116 ccprintf(stream
, "%x:%x:%x:%x:%x:%x", a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
121 cksum(const IpPtr
&ptr
)
123 int sum
= ip_cksum_add(ptr
->bytes(), ptr
->hlen(), 0);
124 return ip_cksum_carry(sum
);
128 __tu_cksum(const IpPtr
&ip
)
130 int tcplen
= ip
->len() - ip
->hlen();
131 int sum
= ip_cksum_add(ip
->payload(), tcplen
, 0);
132 sum
= ip_cksum_add(&ip
->ip_src
, 8, sum
); // source and destination
133 sum
+= htons(ip
->ip_p
+ tcplen
);
134 return ip_cksum_carry(sum
);
138 cksum(const TcpPtr
&tcp
)
139 { return __tu_cksum(IpPtr(tcp
.packet())); }
142 cksum(const UdpPtr
&udp
)
143 { return __tu_cksum(IpPtr(udp
.packet())); }
146 IpHdr::options(vector
<const IpOpt
*> &vec
) const
150 const uint8_t *data
= bytes() + sizeof(struct ip_hdr
);
151 int all
= hlen() - sizeof(struct ip_hdr
);
153 const IpOpt
*opt
= (const IpOpt
*)data
;
154 int len
= opt
->len();
167 TcpHdr::options(vector
<const TcpOpt
*> &vec
) const
171 const uint8_t *data
= bytes() + sizeof(struct tcp_hdr
);
172 int all
= off() - sizeof(struct tcp_hdr
);
174 const TcpOpt
*opt
= (const TcpOpt
*)data
;
175 int len
= opt
->len();
188 TcpOpt::sack(vector
<SackRange
> &vec
) const
192 const uint8_t *data
= bytes() + sizeof(struct tcp_hdr
);
193 int all
= len() - offsetof(tcp_opt
, opt_data
.sack
);
195 const uint16_t *sack
= (const uint16_t *)data
;
196 int len
= sizeof(uint16_t) * 2;
202 vec
.push_back(RangeIn(ntohs(sack
[0]), ntohs(sack
[1])));
211 hsplit(const EthPacketPtr
&ptr
)
217 split_point
= ip
.pstart();
221 split_point
= tcp
.pstart();
225 split_point
= udp
.pstart();
231 /* namespace Net */ }