inet.hh revision 1092
1/*
2 * Copyright (c) 2002-2003 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
29#ifndef __BASE_INET_HH__
30#define __BASE_INET_HH__
31
32#include <string>
33
34#include "dnet/os.h"
35
36#include "dnet/eth.h"
37#include "dnet/ip.h"
38#include "dnet/ip6.h"
39#include "dnet/addr.h"
40#include "dnet/arp.h"
41#include "dnet/icmp.h"
42#include "dnet/tcp.h"
43#include "dnet/udp.h"
44
45#include "dnet/intf.h"
46#include "dnet/route.h"
47#include "dnet/fw.h"
48
49#include "dnet/blob.h"
50#include "dnet/rand.h"
51
52#include "sim/host.hh"
53
54std::string eaddr_string(const uint8_t a[6]);
55
56struct EthHdr;
57struct IpHdr;
58struct TcpHdr;
59struct UdpHdr;
60
61struct EthHdr : protected eth_hdr
62{
63    uint16_t type() const { return ntohs(eth_type); }
64
65    const IpHdr *ip() const
66    { return type() == ETH_TYPE_IP ? (const IpHdr *)payload() : 0; }
67
68    IpHdr *ip()
69    { return type() == ETH_TYPE_IP ? (IpHdr *)payload() : 0; }
70
71    bool unicast() { return eth_dst.data[0] == 0x00; }
72    bool multicast() { return eth_dst.data[0] == 0x01; }
73    bool broadcast() { return eth_dst.data[0] == 0xff; }
74
75    int size() const { return sizeof(EthHdr); }
76    const uint8_t *bytes() const { return (const uint8_t *)this; }
77    const uint8_t *payload() const { return bytes() + size(); }
78    uint8_t *bytes() { return (uint8_t *)this; }
79    uint8_t *payload() { return bytes() + size(); }
80};
81
82struct IpHdr : protected ip_hdr
83{
84    uint8_t  version() const { return ip_v; }
85    uint8_t  hlen() const { return ip_hl * 4; }
86    uint8_t  tos() const { return ip_tos; }
87    uint16_t len() const { return ntohs(ip_len); }
88    uint16_t id() const { return ntohs(ip_id); }
89    uint16_t frag_flags() const { return ntohs(ip_off) >> 13; }
90    uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
91    uint8_t  ttl() const { return ip_ttl; }
92    uint8_t  proto() const { return ip_p; }
93    uint16_t sum() const { return ip_sum; }
94    uint32_t src() const { return ntohl(ip_src); }
95    uint32_t dst() const { return ntohl(ip_dst); }
96
97    void sum(uint16_t sum) { ip_sum = sum; }
98
99    uint16_t ip_cksum() const;
100    uint16_t tu_cksum() const;
101
102    const TcpHdr *tcp() const
103    { return proto() == IP_PROTO_TCP ? (const TcpHdr *)payload() : 0; }
104    const UdpHdr *udp() const
105    { return proto() == IP_PROTO_UDP ? (const UdpHdr *)payload() : 0; }
106
107    TcpHdr *tcp()
108    { return proto() == IP_PROTO_TCP ? (TcpHdr *)payload() : 0; }
109    UdpHdr *udp()
110    { return proto() == IP_PROTO_UDP ? (UdpHdr *)payload() : 0; }
111
112
113    int size() const { return hlen(); }
114    const uint8_t *bytes() const { return (const uint8_t *)this; }
115    const uint8_t *payload() const { return bytes() + size(); }
116    uint8_t *bytes() { return (uint8_t *)this; }
117    uint8_t *payload() { return bytes() + size(); }
118};
119
120struct TcpHdr : protected tcp_hdr
121{
122    uint16_t sport() const { return ntohs(th_sport); }
123    uint16_t dport() const { return ntohs(th_dport); }
124    uint32_t seq() const { return ntohl(th_seq); }
125    uint32_t ack() const { return ntohl(th_ack); }
126    uint8_t  off() const { return th_off; }
127    uint8_t  flags() const { return th_flags & 0x3f; }
128    uint16_t win() const { return ntohs(th_win); }
129    uint16_t sum() const { return th_sum; }
130    uint16_t urp() const { return ntohs(th_urp); }
131
132    void sum(uint16_t sum) { th_sum = sum; }
133
134    int size() const { return off(); }
135    const uint8_t *bytes() const { return (const uint8_t *)this; }
136    const uint8_t *payload() const { return bytes() + size(); }
137    uint8_t *bytes() { return (uint8_t *)this; }
138    uint8_t *payload() { return bytes() + size(); }
139};
140
141struct UdpHdr : protected udp_hdr
142{
143    uint16_t sport() const { return ntohs(uh_sport); }
144    uint16_t dport() const { return ntohs(uh_dport); }
145    uint16_t len() const { return ntohs(uh_ulen); }
146    uint16_t sum() const { return ntohs(uh_sum); }
147
148    void sum(uint16_t sum) { uh_sum = htons(sum); }
149
150    int size() const { return sizeof(UdpHdr); }
151    const uint8_t *bytes() const { return (const uint8_t *)this; }
152    const uint8_t *payload() const { return bytes() + size(); }
153    uint8_t *bytes() { return (uint8_t *)this; }
154    uint8_t *payload() { return bytes() + size(); }
155};
156
157#endif // __BASE_INET_HH__
158