inet.cc revision 6216
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 312SN/A#include <sstream> 322SN/A#include <string> 332SN/A 3456SN/A#include "base/cprintf.hh" 356216Snate@binkert.org#include "base/inet.hh" 366214Snate@binkert.org#include "base/types.hh" 372SN/A 381078SN/Ausing namespace std; 391114SN/Anamespace Net { 401114SN/A 411114SN/AEthAddr::EthAddr() 421114SN/A{ 431114SN/A memset(data, 0, ETH_ADDR_LEN); 441114SN/A} 451114SN/A 461114SN/AEthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN]) 471114SN/A{ 481114SN/A *data = *ea; 491114SN/A} 501114SN/A 511114SN/AEthAddr::EthAddr(const eth_addr &ea) 521114SN/A{ 531114SN/A *data = *ea.data; 541114SN/A} 551114SN/A 561114SN/AEthAddr::EthAddr(const std::string &addr) 571114SN/A{ 581114SN/A parse(addr); 591114SN/A} 601114SN/A 611114SN/Aconst EthAddr & 621114SN/AEthAddr::operator=(const eth_addr &ea) 631114SN/A{ 641114SN/A *data = *ea.data; 651114SN/A return *this; 661114SN/A} 671114SN/A 681114SN/Aconst EthAddr & 691114SN/AEthAddr::operator=(const std::string &addr) 701114SN/A{ 711114SN/A parse(addr); 721114SN/A return *this; 731114SN/A} 741114SN/A 751114SN/Avoid 761114SN/AEthAddr::parse(const std::string &addr) 771114SN/A{ 781114SN/A // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise 791114SN/A // the sscanf function won't work. 801114SN/A int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1]; 811114SN/A if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1], 821114SN/A &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) { 831114SN/A memset(data, 0xff, ETH_ADDR_LEN); 841114SN/A return; 851114SN/A } 861114SN/A 871114SN/A for (int i = 0; i < ETH_ADDR_LEN; ++i) { 881114SN/A if (bytes[i] & ~0xff) { 891114SN/A memset(data, 0xff, ETH_ADDR_LEN); 901114SN/A return; 911114SN/A } 921114SN/A 931114SN/A data[i] = bytes[i]; 941114SN/A } 951114SN/A} 961114SN/A 972SN/Astring 981114SN/AEthAddr::string() const 992SN/A{ 1002SN/A stringstream stream; 1011114SN/A stream << *this; 1022SN/A return stream.str(); 1032SN/A} 1042SN/A 1051114SN/Abool 1061114SN/Aoperator==(const EthAddr &left, const EthAddr &right) 1072SN/A{ 1081114SN/A return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN); 1091114SN/A} 1101114SN/A 1111114SN/Aostream & 1121114SN/Aoperator<<(ostream &stream, const EthAddr &ea) 1131114SN/A{ 1141114SN/A const uint8_t *a = ea.addr(); 1151114SN/A ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]); 1161114SN/A return stream; 1172SN/A} 1182SN/A 1191078SN/Auint16_t 1201114SN/Acksum(const IpPtr &ptr) 1211078SN/A{ 1221114SN/A int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0); 1231114SN/A return ip_cksum_carry(sum); 1241078SN/A} 1251114SN/A 1261114SN/Auint16_t 1271114SN/A__tu_cksum(const IpPtr &ip) 1281114SN/A{ 1291114SN/A int tcplen = ip->len() - ip->hlen(); 1301114SN/A int sum = ip_cksum_add(ip->payload(), tcplen, 0); 1311114SN/A sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination 1321114SN/A sum += htons(ip->ip_p + tcplen); 1331114SN/A return ip_cksum_carry(sum); 1341114SN/A} 1351114SN/A 1361114SN/Auint16_t 1371114SN/Acksum(const TcpPtr &tcp) 1381114SN/A{ return __tu_cksum(IpPtr(tcp.packet())); } 1391114SN/A 1401114SN/Auint16_t 1411114SN/Acksum(const UdpPtr &udp) 1421114SN/A{ return __tu_cksum(IpPtr(udp.packet())); } 1431114SN/A 1441114SN/Abool 1451114SN/AIpHdr::options(vector<const IpOpt *> &vec) const 1461114SN/A{ 1471114SN/A vec.clear(); 1481114SN/A 1491114SN/A const uint8_t *data = bytes() + sizeof(struct ip_hdr); 1501114SN/A int all = hlen() - sizeof(struct ip_hdr); 1511114SN/A while (all > 0) { 1521114SN/A const IpOpt *opt = (const IpOpt *)data; 1531114SN/A int len = opt->len(); 1541114SN/A if (all < len) 1551114SN/A return false; 1561114SN/A 1571114SN/A vec.push_back(opt); 1581114SN/A all -= len; 1591114SN/A data += len; 1601114SN/A } 1611114SN/A 1621114SN/A return true; 1631114SN/A} 1641114SN/A 1651114SN/Abool 1661114SN/ATcpHdr::options(vector<const TcpOpt *> &vec) const 1671114SN/A{ 1681114SN/A vec.clear(); 1691114SN/A 1701114SN/A const uint8_t *data = bytes() + sizeof(struct tcp_hdr); 1711114SN/A int all = off() - sizeof(struct tcp_hdr); 1721114SN/A while (all > 0) { 1731114SN/A const TcpOpt *opt = (const TcpOpt *)data; 1741114SN/A int len = opt->len(); 1751114SN/A if (all < len) 1761114SN/A return false; 1771114SN/A 1781114SN/A vec.push_back(opt); 1791114SN/A all -= len; 1801114SN/A data += len; 1811114SN/A } 1821114SN/A 1831114SN/A return true; 1841114SN/A} 1851114SN/A 1861114SN/Abool 1871114SN/ATcpOpt::sack(vector<SackRange> &vec) const 1881114SN/A{ 1891114SN/A vec.clear(); 1901114SN/A 1911114SN/A const uint8_t *data = bytes() + sizeof(struct tcp_hdr); 1921114SN/A int all = len() - offsetof(tcp_opt, opt_data.sack); 1931114SN/A while (all > 0) { 1941114SN/A const uint16_t *sack = (const uint16_t *)data; 1951114SN/A int len = sizeof(uint16_t) * 2; 1961114SN/A if (all < len) { 1971114SN/A vec.clear(); 1981114SN/A return false; 1991114SN/A } 2001114SN/A 2011114SN/A vec.push_back(RangeIn(ntohs(sack[0]), ntohs(sack[1]))); 2021114SN/A all -= len; 2031114SN/A data += len; 2041114SN/A } 2051114SN/A 2061114SN/A return false; 2071114SN/A} 2081114SN/A 2095782Ssaidi@eecs.umich.eduint 2105782Ssaidi@eecs.umich.eduhsplit(const EthPacketPtr &ptr) 2115782Ssaidi@eecs.umich.edu{ 2125782Ssaidi@eecs.umich.edu int split_point = 0; 2135782Ssaidi@eecs.umich.edu 2145782Ssaidi@eecs.umich.edu IpPtr ip(ptr); 2155782Ssaidi@eecs.umich.edu if (ip) { 2165782Ssaidi@eecs.umich.edu split_point = ip.pstart(); 2175782Ssaidi@eecs.umich.edu 2185782Ssaidi@eecs.umich.edu TcpPtr tcp(ip); 2195782Ssaidi@eecs.umich.edu if (tcp) 2205782Ssaidi@eecs.umich.edu split_point = tcp.pstart(); 2215782Ssaidi@eecs.umich.edu 2225782Ssaidi@eecs.umich.edu UdpPtr udp(ip); 2235782Ssaidi@eecs.umich.edu if (udp) 2245782Ssaidi@eecs.umich.edu split_point = udp.pstart(); 2255782Ssaidi@eecs.umich.edu } 2265782Ssaidi@eecs.umich.edu return split_point; 2275782Ssaidi@eecs.umich.edu} 2285782Ssaidi@eecs.umich.edu 2295782Ssaidi@eecs.umich.edu 2301114SN/A/* namespace Net */ } 231