X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Socket%2FProtocols%2FRaw%2FMACAddress.cc;h=53a96fbc6ade90bc6e11fc083f52bb1789ec5f94;hb=5443435c4c2b6e4386c5334b5b8358273f2bae93;hp=48d2a1b413b01e5d78e810e8fd6cf4022e8ce18d;hpb=66293acca094b2f29e26e70208691db4af274efb;p=senf.git diff --git a/Socket/Protocols/Raw/MACAddress.cc b/Socket/Protocols/Raw/MACAddress.cc index 48d2a1b..53a96fb 100644 --- a/Socket/Protocols/Raw/MACAddress.cc +++ b/Socket/Protocols/Raw/MACAddress.cc @@ -1,8 +1,8 @@ // $Id$ // -// Copyright (C) 2007 -// Fraunhofer Institute for Open Communication Systems (FOKUS) -// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Copyright (C) 2007 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY // Stefan Bund // // This program is free software; you can redistribute it and/or modify @@ -28,89 +28,50 @@ // Custom includes #include -#include +#include +#include #include -#include +#include "ParseString.hh" +#include "EUI64.hh" //#include "MACAddress.mpp" #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -namespace { - - boost::uint8_t hexToNibble(char c) - { - if (c<'0') - throw senf::MACAddress::SyntaxException(); - else if (c<='9') - return c-'0'; - else if (c<'A') - throw senf::MACAddress::SyntaxException(); - else if (c<='F') - return c-'A'+10; - else if (c<'a') - throw senf::MACAddress::SyntaxException(); - else if (c<='f') - return c-'a'+10; - else - throw senf::MACAddress::SyntaxException(); - } - - template - boost::uint8_t hexToByte(Range const & range) - { - if (boost::size(range) != 2) - throw senf::MACAddress::SyntaxException(); - typename boost::range_const_iterator::type i (boost::begin(range)); - return hexToNibble(i[0])*16+hexToNibble(i[1]); - } - -} - /////////////////////////////////////////////////////////////////////////// // senf::MACAddress prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string const & s) { MACAddress mac (senf::noinit); - typedef boost::char_separator separator; - typedef boost::tokenizer tokenizer; - separator sep (":-"); - tokenizer tok (s,sep); - tokenizer::iterator i (tok.begin()); - tokenizer::iterator const i_end (tok.end()); - iterator j (mac.begin()); - iterator const j_end (mac.end()); - for (; i!=i_end && j!=j_end; ++i, ++j) - *j = hexToByte(*i); - if (i!=i_end || j!=j_end) - throw SyntaxException(); + detail::parseHexString(s, ":-", mac.begin(), mac.end()); return mac; } -prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v) +prefix_ senf::MACAddress senf::MACAddress::from_eui64(senf::EUI64 const & eui) { - if ( boost::uint16_t(v>>24) != 0xfffe ) - throw SyntaxException(); + if (eui[3] != 0xffu || eui[4] != 0xfeu) + throw AddressSyntaxException(); MACAddress mac (senf::noinit); - mac[0] = boost::uint8_t( v>>56 ); - mac[1] = boost::uint8_t( v>>48 ); - mac[2] = boost::uint8_t( v>>40 ); - mac[3] = boost::uint8_t( v>>16 ); - mac[4] = boost::uint8_t( v>> 8 ); - mac[5] = boost::uint8_t( v ); + mac[0] = eui[0]; + mac[1] = eui[1]; + mac[2] = eui[2]; + mac[3] = eui[5]; + mac[4] = eui[6]; + mac[5] = eui[7]; return mac; } senf::MACAddress const senf::MACAddress::Broadcast = senf::MACAddress(0xFFFFFFFFFFFFull); senf::MACAddress const senf::MACAddress::None; + /////////////////////////////////////////////////////////////////////////// // namespace members prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac) { - boost::io::ios_all_saver ias(os); + boost::io::ios_all_saver ias (os); os << std::hex << std::setfill('0'); for (MACAddress::const_iterator i (mac.begin()); i != mac.end(); ++i) { if (i != mac.begin()) @@ -120,11 +81,25 @@ prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & ma return os; } +prefix_ std::istream & senf::operator>>(std::istream & is, MACAddress & mac) +{ + std::string s; + if (!(is >> s)) + return is; + try { + mac = MACAddress::from_string(s); + } + catch (AddressException &) { + is.setstate(std::ios::failbit); + } + return is; +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "MACAddress.mpp" - + // Local Variables: // mode: c++ // fill-column: 100