inet.cc revision 7778
112771Sqtt2@cornell.edu/* 212771Sqtt2@cornell.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan 312771Sqtt2@cornell.edu * Copyright (c) 2010 Advanced Micro Devices, Inc. 412771Sqtt2@cornell.edu * All rights reserved. 512771Sqtt2@cornell.edu * 612771Sqtt2@cornell.edu * Redistribution and use in source and binary forms, with or without 712771Sqtt2@cornell.edu * modification, are permitted provided that the following conditions are 812771Sqtt2@cornell.edu * met: redistributions of source code must retain the above copyright 912771Sqtt2@cornell.edu * notice, this list of conditions and the following disclaimer; 1012771Sqtt2@cornell.edu * redistributions in binary form must reproduce the above copyright 1112771Sqtt2@cornell.edu * notice, this list of conditions and the following disclaimer in the 1212771Sqtt2@cornell.edu * documentation and/or other materials provided with the distribution; 1312771Sqtt2@cornell.edu * neither the name of the copyright holders nor the names of its 1412771Sqtt2@cornell.edu * contributors may be used to endorse or promote products derived from 1512771Sqtt2@cornell.edu * this software without specific prior written permission. 1612771Sqtt2@cornell.edu * 1712771Sqtt2@cornell.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1812771Sqtt2@cornell.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1912771Sqtt2@cornell.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2012771Sqtt2@cornell.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2112771Sqtt2@cornell.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2212771Sqtt2@cornell.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2312771Sqtt2@cornell.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2412771Sqtt2@cornell.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2512771Sqtt2@cornell.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2612771Sqtt2@cornell.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2712771Sqtt2@cornell.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2812771Sqtt2@cornell.edu * 2912771Sqtt2@cornell.edu * Authors: Nathan Binkert 3012771Sqtt2@cornell.edu * Gabe Black 3112771Sqtt2@cornell.edu */ 3212771Sqtt2@cornell.edu 3312771Sqtt2@cornell.edu#include <cstdio> 3412771Sqtt2@cornell.edu#include <sstream> 3512771Sqtt2@cornell.edu#include <string> 3612771Sqtt2@cornell.edu 3712771Sqtt2@cornell.edu#include "base/cprintf.hh" 3812771Sqtt2@cornell.edu#include "base/inet.hh" 3912771Sqtt2@cornell.edu#include "base/types.hh" 4012771Sqtt2@cornell.edu 4112771Sqtt2@cornell.eduusing namespace std; 4212771Sqtt2@cornell.edunamespace Net { 4312771Sqtt2@cornell.edu 4412771Sqtt2@cornell.eduEthAddr::EthAddr() 4512771Sqtt2@cornell.edu{ 4612771Sqtt2@cornell.edu memset(data, 0, ETH_ADDR_LEN); 4712771Sqtt2@cornell.edu} 4812771Sqtt2@cornell.edu 4912771Sqtt2@cornell.eduEthAddr::EthAddr(const uint8_t ea[ETH_ADDR_LEN]) 5012771Sqtt2@cornell.edu{ 5112771Sqtt2@cornell.edu *data = *ea; 5212771Sqtt2@cornell.edu} 5312771Sqtt2@cornell.edu 5412771Sqtt2@cornell.eduEthAddr::EthAddr(const eth_addr &ea) 5512771Sqtt2@cornell.edu{ 5612771Sqtt2@cornell.edu *data = *ea.data; 5712771Sqtt2@cornell.edu} 5812771Sqtt2@cornell.edu 5912771Sqtt2@cornell.eduEthAddr::EthAddr(const std::string &addr) 6012771Sqtt2@cornell.edu{ 6112771Sqtt2@cornell.edu parse(addr); 6212771Sqtt2@cornell.edu} 6312771Sqtt2@cornell.edu 6412771Sqtt2@cornell.educonst EthAddr & 6512771Sqtt2@cornell.eduEthAddr::operator=(const eth_addr &ea) 6612771Sqtt2@cornell.edu{ 6712771Sqtt2@cornell.edu *data = *ea.data; 6812771Sqtt2@cornell.edu return *this; 6912771Sqtt2@cornell.edu} 7012771Sqtt2@cornell.edu 7112771Sqtt2@cornell.educonst EthAddr & 7212771Sqtt2@cornell.eduEthAddr::operator=(const std::string &addr) 7312771Sqtt2@cornell.edu{ 7412771Sqtt2@cornell.edu parse(addr); 7512771Sqtt2@cornell.edu return *this; 7612771Sqtt2@cornell.edu} 7712771Sqtt2@cornell.edu 7812771Sqtt2@cornell.eduvoid 7912771Sqtt2@cornell.eduEthAddr::parse(const std::string &addr) 8012771Sqtt2@cornell.edu{ 8112771Sqtt2@cornell.edu // the hack below is to make sure that ETH_ADDR_LEN is 6 otherwise 8212771Sqtt2@cornell.edu // the sscanf function won't work. 8312771Sqtt2@cornell.edu int bytes[ETH_ADDR_LEN == 6 ? ETH_ADDR_LEN : -1]; 8412771Sqtt2@cornell.edu if (sscanf(addr.c_str(), "%x:%x:%x:%x:%x:%x", &bytes[0], &bytes[1], 8512771Sqtt2@cornell.edu &bytes[2], &bytes[3], &bytes[4], &bytes[5]) != ETH_ADDR_LEN) { 8612771Sqtt2@cornell.edu memset(data, 0xff, ETH_ADDR_LEN); 8712771Sqtt2@cornell.edu return; 8812771Sqtt2@cornell.edu } 8912771Sqtt2@cornell.edu 9012771Sqtt2@cornell.edu for (int i = 0; i < ETH_ADDR_LEN; ++i) { 9112771Sqtt2@cornell.edu if (bytes[i] & ~0xff) { 9212771Sqtt2@cornell.edu memset(data, 0xff, ETH_ADDR_LEN); 9312771Sqtt2@cornell.edu return; 9412771Sqtt2@cornell.edu } 9512771Sqtt2@cornell.edu 9612771Sqtt2@cornell.edu data[i] = bytes[i]; 9712771Sqtt2@cornell.edu } 9812771Sqtt2@cornell.edu} 9912771Sqtt2@cornell.edu 10012771Sqtt2@cornell.edustring 10112771Sqtt2@cornell.eduEthAddr::string() const 10212771Sqtt2@cornell.edu{ 10312771Sqtt2@cornell.edu stringstream stream; 10412771Sqtt2@cornell.edu stream << *this; 10512771Sqtt2@cornell.edu return stream.str(); 10612771Sqtt2@cornell.edu} 107 108bool 109operator==(const EthAddr &left, const EthAddr &right) 110{ 111 return memcmp(left.bytes(), right.bytes(), ETH_ADDR_LEN); 112} 113 114ostream & 115operator<<(ostream &stream, const EthAddr &ea) 116{ 117 const uint8_t *a = ea.addr(); 118 ccprintf(stream, "%x:%x:%x:%x:%x:%x", a[0], a[1], a[2], a[3], a[4], a[5]); 119 return stream; 120} 121 122string 123IpAddress::string() const 124{ 125 stringstream stream; 126 stream << *this; 127 return stream.str(); 128} 129 130bool 131operator==(const IpAddress &left, const IpAddress &right) 132{ 133 return left.ip() == right.ip(); 134} 135 136ostream & 137operator<<(ostream &stream, const IpAddress &ia) 138{ 139 uint32_t ip = ia.ip(); 140 ccprintf(stream, "%x.%x.%x.%x", 141 (uint8_t)(ip >> 0), (uint8_t)(ip >> 8), 142 (uint8_t)(ip >> 16), (uint8_t)(ip >> 24)); 143 return stream; 144} 145 146string 147IpNetmask::string() const 148{ 149 stringstream stream; 150 stream << *this; 151 return stream.str(); 152} 153 154bool 155operator==(const IpNetmask &left, const IpNetmask &right) 156{ 157 return (left.ip() == right.ip()) && 158 (left.netmask() == right.netmask()); 159} 160 161ostream & 162operator<<(ostream &stream, const IpNetmask &in) 163{ 164 ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask()); 165 return stream; 166} 167 168string 169IpWithPort::string() const 170{ 171 stringstream stream; 172 stream << *this; 173 return stream.str(); 174} 175 176bool 177operator==(const IpWithPort &left, const IpWithPort &right) 178{ 179 return (left.ip() == right.ip()) && (left.port() == right.port()); 180} 181 182ostream & 183operator<<(ostream &stream, const IpWithPort &iwp) 184{ 185 ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port()); 186 return stream; 187} 188 189uint16_t 190cksum(const IpPtr &ptr) 191{ 192 int sum = ip_cksum_add(ptr->bytes(), ptr->hlen(), 0); 193 return ip_cksum_carry(sum); 194} 195 196uint16_t 197__tu_cksum(const IpPtr &ip) 198{ 199 int tcplen = ip->len() - ip->hlen(); 200 int sum = ip_cksum_add(ip->payload(), tcplen, 0); 201 sum = ip_cksum_add(&ip->ip_src, 8, sum); // source and destination 202 sum += htons(ip->ip_p + tcplen); 203 return ip_cksum_carry(sum); 204} 205 206uint16_t 207cksum(const TcpPtr &tcp) 208{ return __tu_cksum(IpPtr(tcp.packet())); } 209 210uint16_t 211cksum(const UdpPtr &udp) 212{ return __tu_cksum(IpPtr(udp.packet())); } 213 214bool 215IpHdr::options(vector<const IpOpt *> &vec) const 216{ 217 vec.clear(); 218 219 const uint8_t *data = bytes() + sizeof(struct ip_hdr); 220 int all = hlen() - sizeof(struct ip_hdr); 221 while (all > 0) { 222 const IpOpt *opt = (const IpOpt *)data; 223 int len = opt->len(); 224 if (all < len) 225 return false; 226 227 vec.push_back(opt); 228 all -= len; 229 data += len; 230 } 231 232 return true; 233} 234 235bool 236TcpHdr::options(vector<const TcpOpt *> &vec) const 237{ 238 vec.clear(); 239 240 const uint8_t *data = bytes() + sizeof(struct tcp_hdr); 241 int all = off() - sizeof(struct tcp_hdr); 242 while (all > 0) { 243 const TcpOpt *opt = (const TcpOpt *)data; 244 int len = opt->len(); 245 if (all < len) 246 return false; 247 248 vec.push_back(opt); 249 all -= len; 250 data += len; 251 } 252 253 return true; 254} 255 256bool 257TcpOpt::sack(vector<SackRange> &vec) const 258{ 259 vec.clear(); 260 261 const uint8_t *data = bytes() + sizeof(struct tcp_hdr); 262 int all = len() - offsetof(tcp_opt, opt_data.sack); 263 while (all > 0) { 264 const uint16_t *sack = (const uint16_t *)data; 265 int len = sizeof(uint16_t) * 2; 266 if (all < len) { 267 vec.clear(); 268 return false; 269 } 270 271 vec.push_back(RangeIn(ntohs(sack[0]), ntohs(sack[1]))); 272 all -= len; 273 data += len; 274 } 275 276 return false; 277} 278 279int 280hsplit(const EthPacketPtr &ptr) 281{ 282 int split_point = 0; 283 284 IpPtr ip(ptr); 285 if (ip) { 286 split_point = ip.pstart(); 287 288 TcpPtr tcp(ip); 289 if (tcp) 290 split_point = tcp.pstart(); 291 292 UdpPtr udp(ip); 293 if (udp) 294 split_point = udp.pstart(); 295 } 296 return split_point; 297} 298 299 300/* namespace Net */ } 301