inet.cc revision 7777:369f90d32e2e
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#include <cstdio>
32#include <sstream>
33#include <string>
34
35#include "base/cprintf.hh"
36#include "base/inet.hh"
37#include "base/types.hh"
38
39using namespace std;
40namespace Net {
41
42EthAddr::EthAddr()
43{
44    memset(data, 0, ETH_ADDR_LEN);
45}
46
47EthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN])
48{
49    *data = *ea;
50}
51
52EthAddr::EthAddr(const eth_addr &ea)
53{
54    *data = *ea.data;
55}
56
57EthAddr::EthAddr(const std::string &addr)
58{
59    parse(addr);
60}
61
62const EthAddr &
63EthAddr::operator=(const eth_addr &ea)
64{
65    *data = *ea.data;
66    return *this;
67}
68
69const EthAddr &
70EthAddr::operator=(const std::string &addr)
71{
72    parse(addr);
73    return *this;
74}
75
76void
77EthAddr::parse(const std::string &addr)
78{
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);
85        return;
86    }
87
88    for (int i = 0; i < ETH_ADDR_LEN; ++i) {
89        if (bytes[i] & ~0xff) {
90            memset(data, 0xff, ETH_ADDR_LEN);
91            return;
92        }
93
94        data[i] = bytes[i];
95    }
96}
97
98string
99EthAddr::string() const
100{
101    stringstream stream;
102    stream << *this;
103    return stream.str();
104}
105
106bool
107operator==(const EthAddr &left, const EthAddr &right)
108{
109    return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN);
110}
111
112ostream &
113operator<<(ostream &stream, const EthAddr &ea)
114{
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]);
117    return stream;
118}
119
120string
121IpAddress::string() const
122{
123    stringstream stream;
124    stream << *this;
125    return stream.str();
126}
127
128bool
129operator==(const IpAddress &left, const IpAddress &right)
130{
131    return left.ip() == right.ip();
132}
133
134ostream &
135operator<<(ostream &stream, const IpAddress &ia)
136{
137    uint32_t ip = ia.ip();
138    ccprintf(stream, "%x.%x.%x.%x",
139            (uint8_t)(ip >> 0),  (uint8_t)(ip >> 8),
140            (uint8_t)(ip >> 16), (uint8_t)(ip >> 24));
141    return stream;
142}
143
144string
145IpNetmask::string() const
146{
147    stringstream stream;
148    stream << *this;
149    return stream.str();
150}
151
152bool
153operator==(const IpNetmask &left, const IpNetmask &right)
154{
155    return (left.ip() == right.ip()) &&
156        (left.netmask() == right.netmask());
157}
158
159ostream &
160operator<<(ostream &stream, const IpNetmask &in)
161{
162    ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask());
163    return stream;
164}
165
166string
167IpWithPort::string() const
168{
169    stringstream stream;
170    stream << *this;
171    return stream.str();
172}
173
174bool
175operator==(const IpWithPort &left, const IpWithPort &right)
176{
177    return (left.ip() == right.ip()) && (left.port() == right.port());
178}
179
180ostream &
181operator<<(ostream &stream, const IpWithPort &iwp)
182{
183    ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port());
184    return stream;
185}
186
187uint16_t
188cksum(const IpPtr &ptr)
189{
190    int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0);
191    return ip_cksum_carry(sum);
192}
193
194uint16_t
195__tu_cksum(const IpPtr &ip)
196{
197    int tcplen = ip->len() - ip->hlen();
198    int sum = ip_cksum_add(ip->payload(), tcplen, 0);
199    sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination
200    sum += htons(ip->ip_p + tcplen);
201    return ip_cksum_carry(sum);
202}
203
204uint16_t
205cksum(const TcpPtr &tcp)
206{ return __tu_cksum(IpPtr(tcp.packet())); }
207
208uint16_t
209cksum(const UdpPtr &udp)
210{ return __tu_cksum(IpPtr(udp.packet())); }
211
212bool
213IpHdr::options(vector<const IpOpt *> &vec) const
214{
215    vec.clear();
216
217    const uint8_t *data = bytes() + sizeof(struct ip_hdr);
218    int all = hlen() - sizeof(struct ip_hdr);
219    while (all > 0) {
220        const IpOpt *opt = (const IpOpt *)data;
221        int len = opt->len();
222        if (all < len)
223            return false;
224
225        vec.push_back(opt);
226        all -= len;
227        data += len;
228    }
229
230    return true;
231}
232
233bool
234TcpHdr::options(vector<const TcpOpt *> &vec) const
235{
236    vec.clear();
237
238    const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
239    int all = off() - sizeof(struct tcp_hdr);
240    while (all > 0) {
241        const TcpOpt *opt = (const TcpOpt *)data;
242        int len = opt->len();
243        if (all < len)
244            return false;
245
246        vec.push_back(opt);
247        all -= len;
248        data += len;
249    }
250
251    return true;
252}
253
254bool
255TcpOpt::sack(vector<SackRange> &vec) const
256{
257    vec.clear();
258
259    const uint8_t *data = bytes() + sizeof(struct tcp_hdr);
260    int all = len() - offsetof(tcp_opt, opt_data.sack);
261    while (all > 0) {
262        const uint16_t *sack = (const uint16_t *)data;
263        int len = sizeof(uint16_t) * 2;
264        if (all < len) {
265            vec.clear();
266            return false;
267        }
268
269        vec.push_back(RangeIn(ntohs(sack[0]), ntohs(sack[1])));
270        all -= len;
271        data += len;
272    }
273
274    return false;
275}
276
277int
278hsplit(const EthPacketPtr &ptr)
279{
280    int split_point = 0;
281
282    IpPtr ip(ptr);
283    if (ip) {
284        split_point = ip.pstart();
285
286        TcpPtr tcp(ip);
287        if (tcp)
288            split_point = tcp.pstart();
289
290        UdpPtr udp(ip);
291        if (udp)
292            split_point = udp.pstart();
293    }
294    return split_point;
295}
296
297
298/* namespace Net */ }
299