inet.hh revision 1078
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 : protected eth_hdr 57{ 58 uint16_t type() const { return ntohs(eth_type); } 59 const uint8_t *payload() const { return (const uint8_t *)this + sizeof(*this); } 60 uint8_t *payload() { return (uint8_t *)this + sizeof(*this); } 61 62 bool unicast() { return eth_dst.data[0] == 0x00; } 63 bool multicast() { return eth_dst.data[0] == 0x01; } 64 bool broadcast() { return eth_dst.data[0] == 0xff; } 65}; 66 67struct IpHdr : protected ip_hdr 68{ 69 uint8_t version() const { return ip_v; } 70 uint8_t hlen() const { return ip_hl; } 71 uint8_t tos() const { return ip_tos; } 72 uint16_t len() const { return ntohs(ip_len); } 73 uint16_t id() const { return ntohs(ip_id); } 74 uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } 75 uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } 76 uint8_t ttl() const { return ip_ttl; } 77 uint8_t proto() const { return ip_p; } 78 uint16_t sum() const { return ntohs(ip_sum); } 79 uint32_t src() const { return ntohl(ip_src); } 80 uint32_t dst() const { return ntohl(ip_dst); } 81 82 void sum(uint16_t sum) { ip_sum = htons(sum); } 83 84 uint16_t ip_cksum() const; 85 uint16_t tu_cksum() const; 86 const uint8_t *payload() const { return (const uint8_t *)this + hlen(); } 87 uint8_t *payload() { return (uint8_t *)this + hlen(); } 88}; 89 90struct TcpHdr : protected tcp_hdr 91{ 92 uint16_t sport() const { return ntohs(th_sport); } 93 uint16_t dport() const { return ntohs(th_dport); } 94 uint32_t seq() const { return ntohl(th_seq); } 95 uint32_t ack() const { return ntohl(th_ack); } 96 uint8_t off() const { return th_off; } 97 uint8_t flags() const { return th_flags & 0x3f; } 98 uint16_t win() const { return ntohs(th_win); } 99 uint16_t sum() const { return ntohs(th_sum); } 100 uint16_t urp() const { return ntohs(th_urp); } 101 102 void sum(uint16_t sum) { th_sum = htons(sum); } 103 104 const uint8_t *payload() const { return (const uint8_t *)this + off(); } 105 uint8_t *payload() { return (uint8_t *)this + off(); } 106}; 107 108struct UdpHdr : protected udp_hdr 109{ 110 uint16_t sport() const { return ntohs(uh_sport); } 111 uint16_t dport() const { return ntohs(uh_dport); } 112 uint16_t len() const { return ntohs(uh_ulen); } 113 uint16_t sum() const { return ntohs(uh_sum); } 114 115 void sum(uint16_t sum) { uh_sum = htons(sum); } 116 117 const uint8_t *payload() const { return (const uint8_t *)this + sizeof(*this); } 118 uint8_t *payload() { return (uint8_t *)this + sizeof(*this); } 119}; 120 121#endif // __BASE_INET_HH__ 122