inet.cc revision 7777
12SN/A/* 21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292SN/A */ 302SN/A 316712Snate@binkert.org#include <cstdio> 322SN/A#include <sstream> 332SN/A#include <string> 342SN/A 3556SN/A#include "base/cprintf.hh" 366216Snate@binkert.org#include "base/inet.hh" 376214Snate@binkert.org#include "base/types.hh" 382SN/A 391078SN/Ausing namespace std; 401114SN/Anamespace Net { 411114SN/A 421114SN/AEthAddr::EthAddr() 431114SN/A{ 441114SN/A memset(data, 0, ETH_ADDR_LEN); 451114SN/A} 461114SN/A 471114SN/AEthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN]) 481114SN/A{ 491114SN/A *data = *ea; 501114SN/A} 511114SN/A 521114SN/AEthAddr::EthAddr(const eth_addr &ea) 531114SN/A{ 541114SN/A *data = *ea.data; 551114SN/A} 561114SN/A 571114SN/AEthAddr::EthAddr(const std::string &addr) 581114SN/A{ 591114SN/A parse(addr); 601114SN/A} 611114SN/A 621114SN/Aconst EthAddr & 631114SN/AEthAddr::operator=(const eth_addr &ea) 641114SN/A{ 651114SN/A *data = *ea.data; 661114SN/A return *this; 671114SN/A} 681114SN/A 691114SN/Aconst EthAddr & 701114SN/AEthAddr::operator=(const std::string &addr) 711114SN/A{ 721114SN/A parse(addr); 731114SN/A return *this; 741114SN/A} 751114SN/A 761114SN/Avoid 771114SN/AEthAddr::parse(const std::string &addr) 781114SN/A{ 791114SN/A // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise 801114SN/A // the sscanf function won't work. 811114SN/A int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1]; 821114SN/A if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1], 831114SN/A &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) { 841114SN/A memset(data, 0xff, ETH_ADDR_LEN); 851114SN/A return; 861114SN/A } 871114SN/A 881114SN/A for (int i = 0; i < ETH_ADDR_LEN; ++i) { 891114SN/A if (bytes[i] & ~0xff) { 901114SN/A memset(data, 0xff, ETH_ADDR_LEN); 911114SN/A return; 921114SN/A } 931114SN/A 941114SN/A data[i] = bytes[i]; 951114SN/A } 961114SN/A} 971114SN/A 982SN/Astring 991114SN/AEthAddr::string() const 1002SN/A{ 1012SN/A stringstream stream; 1021114SN/A stream << *this; 1032SN/A return stream.str(); 1042SN/A} 1052SN/A 1061114SN/Abool 1071114SN/Aoperator==(const EthAddr &left, const EthAddr &right) 1082SN/A{ 1091114SN/A return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN); 1101114SN/A} 1111114SN/A 1121114SN/Aostream & 1131114SN/Aoperator<<(ostream &stream, const EthAddr &ea) 1141114SN/A{ 1151114SN/A const uint8_t *a = ea.addr(); 1161114SN/A ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]); 1171114SN/A return stream; 1182SN/A} 1192SN/A 1207777Sgblack@eecs.umich.edustring 1217777Sgblack@eecs.umich.eduIpAddress::string() const 1227777Sgblack@eecs.umich.edu{ 1237777Sgblack@eecs.umich.edu stringstream stream; 1247777Sgblack@eecs.umich.edu stream << *this; 1257777Sgblack@eecs.umich.edu return stream.str(); 1267777Sgblack@eecs.umich.edu} 1277777Sgblack@eecs.umich.edu 1287777Sgblack@eecs.umich.edubool 1297777Sgblack@eecs.umich.eduoperator==(const IpAddress &left, const IpAddress &right) 1307777Sgblack@eecs.umich.edu{ 1317777Sgblack@eecs.umich.edu return left.ip() == right.ip(); 1327777Sgblack@eecs.umich.edu} 1337777Sgblack@eecs.umich.edu 1347777Sgblack@eecs.umich.eduostream & 1357777Sgblack@eecs.umich.eduoperator<<(ostream &stream, const IpAddress &ia) 1367777Sgblack@eecs.umich.edu{ 1377777Sgblack@eecs.umich.edu uint32_t ip = ia.ip(); 1387777Sgblack@eecs.umich.edu ccprintf(stream, "%x.%x.%x.%x", 1397777Sgblack@eecs.umich.edu (uint8_t)(ip >> 0), (uint8_t)(ip >> 8), 1407777Sgblack@eecs.umich.edu (uint8_t)(ip >> 16), (uint8_t)(ip >> 24)); 1417777Sgblack@eecs.umich.edu return stream; 1427777Sgblack@eecs.umich.edu} 1437777Sgblack@eecs.umich.edu 1447777Sgblack@eecs.umich.edustring 1457777Sgblack@eecs.umich.eduIpNetmask::string() const 1467777Sgblack@eecs.umich.edu{ 1477777Sgblack@eecs.umich.edu stringstream stream; 1487777Sgblack@eecs.umich.edu stream << *this; 1497777Sgblack@eecs.umich.edu return stream.str(); 1507777Sgblack@eecs.umich.edu} 1517777Sgblack@eecs.umich.edu 1527777Sgblack@eecs.umich.edubool 1537777Sgblack@eecs.umich.eduoperator==(const IpNetmask &left, const IpNetmask &right) 1547777Sgblack@eecs.umich.edu{ 1557777Sgblack@eecs.umich.edu return (left.ip() == right.ip()) && 1567777Sgblack@eecs.umich.edu (left.netmask() == right.netmask()); 1577777Sgblack@eecs.umich.edu} 1587777Sgblack@eecs.umich.edu 1597777Sgblack@eecs.umich.eduostream & 1607777Sgblack@eecs.umich.eduoperator<<(ostream &stream, const IpNetmask &in) 1617777Sgblack@eecs.umich.edu{ 1627777Sgblack@eecs.umich.edu ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask()); 1637777Sgblack@eecs.umich.edu return stream; 1647777Sgblack@eecs.umich.edu} 1657777Sgblack@eecs.umich.edu 1667777Sgblack@eecs.umich.edustring 1677777Sgblack@eecs.umich.eduIpWithPort::string() const 1687777Sgblack@eecs.umich.edu{ 1697777Sgblack@eecs.umich.edu stringstream stream; 1707777Sgblack@eecs.umich.edu stream << *this; 1717777Sgblack@eecs.umich.edu return stream.str(); 1727777Sgblack@eecs.umich.edu} 1737777Sgblack@eecs.umich.edu 1747777Sgblack@eecs.umich.edubool 1757777Sgblack@eecs.umich.eduoperator==(const IpWithPort &left, const IpWithPort &right) 1767777Sgblack@eecs.umich.edu{ 1777777Sgblack@eecs.umich.edu return (left.ip() == right.ip()) && (left.port() == right.port()); 1787777Sgblack@eecs.umich.edu} 1797777Sgblack@eecs.umich.edu 1807777Sgblack@eecs.umich.eduostream & 1817777Sgblack@eecs.umich.eduoperator<<(ostream &stream, const IpWithPort &iwp) 1827777Sgblack@eecs.umich.edu{ 1837777Sgblack@eecs.umich.edu ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port()); 1847777Sgblack@eecs.umich.edu return stream; 1857777Sgblack@eecs.umich.edu} 1867777Sgblack@eecs.umich.edu 1871078SN/Auint16_t 1881114SN/Acksum(const IpPtr &ptr) 1891078SN/A{ 1901114SN/A int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0); 1911114SN/A return ip_cksum_carry(sum); 1921078SN/A} 1931114SN/A 1941114SN/Auint16_t 1951114SN/A__tu_cksum(const IpPtr &ip) 1961114SN/A{ 1971114SN/A int tcplen = ip->len() - ip->hlen(); 1981114SN/A int sum = ip_cksum_add(ip->payload(), tcplen, 0); 1991114SN/A sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination 2001114SN/A sum += htons(ip->ip_p + tcplen); 2011114SN/A return ip_cksum_carry(sum); 2021114SN/A} 2031114SN/A 2041114SN/Auint16_t 2051114SN/Acksum(const TcpPtr &tcp) 2061114SN/A{ return __tu_cksum(IpPtr(tcp.packet())); } 2071114SN/A 2081114SN/Auint16_t 2091114SN/Acksum(const UdpPtr &udp) 2101114SN/A{ return __tu_cksum(IpPtr(udp.packet())); } 2111114SN/A 2121114SN/Abool 2131114SN/AIpHdr::options(vector<const IpOpt *> &vec) const 2141114SN/A{ 2151114SN/A vec.clear(); 2161114SN/A 2171114SN/A const uint8_t *data = bytes() + sizeof(struct ip_hdr); 2181114SN/A int all = hlen() - sizeof(struct ip_hdr); 2191114SN/A while (all > 0) { 2201114SN/A const IpOpt *opt = (const IpOpt *)data; 2211114SN/A int len = opt->len(); 2221114SN/A if (all < len) 2231114SN/A return false; 2241114SN/A 2251114SN/A vec.push_back(opt); 2261114SN/A all -= len; 2271114SN/A data += len; 2281114SN/A } 2291114SN/A 2301114SN/A return true; 2311114SN/A} 2321114SN/A 2331114SN/Abool 2341114SN/ATcpHdr::options(vector<const TcpOpt *> &vec) const 2351114SN/A{ 2361114SN/A vec.clear(); 2371114SN/A 2381114SN/A const uint8_t *data = bytes() + sizeof(struct tcp_hdr); 2391114SN/A int all = off() - sizeof(struct tcp_hdr); 2401114SN/A while (all > 0) { 2411114SN/A const TcpOpt *opt = (const TcpOpt *)data; 2421114SN/A int len = opt->len(); 2431114SN/A if (all < len) 2441114SN/A return false; 2451114SN/A 2461114SN/A vec.push_back(opt); 2471114SN/A all -= len; 2481114SN/A data += len; 2491114SN/A } 2501114SN/A 2511114SN/A return true; 2521114SN/A} 2531114SN/A 2541114SN/Abool 2551114SN/ATcpOpt::sack(vector<SackRange> &vec) const 2561114SN/A{ 2571114SN/A vec.clear(); 2581114SN/A 2591114SN/A const uint8_t *data = bytes() + sizeof(struct tcp_hdr); 2601114SN/A int all = len() - offsetof(tcp_opt, opt_data.sack); 2611114SN/A while (all > 0) { 2621114SN/A const uint16_t *sack = (const uint16_t *)data; 2631114SN/A int len = sizeof(uint16_t) * 2; 2641114SN/A if (all < len) { 2651114SN/A vec.clear(); 2661114SN/A return false; 2671114SN/A } 2681114SN/A 2691114SN/A vec.push_back(RangeIn(ntohs(sack[0]), ntohs(sack[1]))); 2701114SN/A all -= len; 2711114SN/A data += len; 2721114SN/A } 2731114SN/A 2741114SN/A return false; 2751114SN/A} 2761114SN/A 2775782Ssaidi@eecs.umich.eduint 2785782Ssaidi@eecs.umich.eduhsplit(const EthPacketPtr &ptr) 2795782Ssaidi@eecs.umich.edu{ 2805782Ssaidi@eecs.umich.edu int split_point = 0; 2815782Ssaidi@eecs.umich.edu 2825782Ssaidi@eecs.umich.edu IpPtr ip(ptr); 2835782Ssaidi@eecs.umich.edu if (ip) { 2845782Ssaidi@eecs.umich.edu split_point = ip.pstart(); 2855782Ssaidi@eecs.umich.edu 2865782Ssaidi@eecs.umich.edu TcpPtr tcp(ip); 2875782Ssaidi@eecs.umich.edu if (tcp) 2885782Ssaidi@eecs.umich.edu split_point = tcp.pstart(); 2895782Ssaidi@eecs.umich.edu 2905782Ssaidi@eecs.umich.edu UdpPtr udp(ip); 2915782Ssaidi@eecs.umich.edu if (udp) 2925782Ssaidi@eecs.umich.edu split_point = udp.pstart(); 2935782Ssaidi@eecs.umich.edu } 2945782Ssaidi@eecs.umich.edu return split_point; 2955782Ssaidi@eecs.umich.edu} 2965782Ssaidi@eecs.umich.edu 2975782Ssaidi@eecs.umich.edu 2981114SN/A/* namespace Net */ } 299