X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Packets%2FDefaultBundle%2FEthernetPacket.cc;h=99e61bb1f2b7e5a868721cdabb3486b0177da929;hb=cb10487f67b404334eaffa60f074b8eca8f871de;hp=f2321ccc5fb2be0a28aa0d5b46775be64d931db3;hpb=9357448dc66bc9acfd3bd8db423deb75bcd6a2c4;p=senf.git diff --git a/Packets/DefaultBundle/EthernetPacket.cc b/Packets/DefaultBundle/EthernetPacket.cc index f2321cc..99e61bb 100644 --- a/Packets/DefaultBundle/EthernetPacket.cc +++ b/Packets/DefaultBundle/EthernetPacket.cc @@ -27,7 +27,8 @@ // Custom includes #include -#include +#include +#include #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// @@ -37,18 +38,61 @@ namespace { registerEthVLanPacket(0x8100); } -prefix_ void senf::EthernetPacket::v_nextInterpreter() - const +/////////////////////////////////////////////////////////////////////////// +// senf::MACAddress + +namespace { + senf::PacketParserBase::byte hexToNibble(char c) + { + if (c<'0') + throw senf::MACAddress::SyntaxException(); + else if (c<='9') + return c-'-'; + 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 + senf::PacketParserBase::byte 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]); + } +} + +prefix_ senf::MACAddress::MACAddress(std::string addr) { - /** \todo Add LLC/SNAP support -> only use the registry - for type() values >=1536, otherwise expect an LLC header */ - registerInterpreter(type(),begin()+bytes(),end()); + typedef boost::char_separator separator; + typedef boost::tokenizer tokenizer; + separator sep (":"); + tokenizer tok (addr,sep); + tokenizer::iterator i (tok.begin()); + tokenizer::iterator i_end (tok.end()); + iterator j (begin()); + iterator j_end (end()); + for (; i!=i_end && j!=j_end; ++i, ++j) + *j = hexToByte(*i); + if (i!=i_end || j!=j_end) + throw SyntaxException(); } -namespace { +/////////////////////////////////////////////////////////////////////////// +// senf::EthernetPacketType - void dumpmac(std::ostream & os, senf::EthernetPacket::Parse_MAC mac) +namespace { + void dumpmac(std::ostream & os, senf::MACAddress mac) { + boost::io::ios_all_saver ias(os); for (unsigned i = 0; i < 6; ++i) { if (i > 0) os << ':'; @@ -56,50 +100,35 @@ namespace { << unsigned(mac[i]); } } - } -prefix_ void senf::EthernetPacket::v_dump(std::ostream & os) - const +prefix_ void senf::EthernetPacketType::dump(packet p, std::ostream & os) { - if (type() <= 1500) + if (p->type() <= 1500) os << "Ethernet 802.3"; - else if (type() >= 0x600) + else if (p->type() >= 0x600) os << "Ethernet II (DIX)"; else os << "Ethernet 802.3 (bad ethertype >1500 and <1536)"; os << ": \n" << " destination : "; - dumpmac(os,destination()); + dumpmac(os,p->destination()); os << "\n" << " source : "; - dumpmac(os,source()); + dumpmac(os,p->source()); os << "\n" << " ethertype : " << std::hex << std::setw(4) << std::setfill('0') - << unsigned(type()) << "\n" << std::dec; -} - -prefix_ void senf::EthernetPacket::v_finalize() -{} - -prefix_ void senf::EthVLanPacket::v_nextInterpreter() - const -{ - /** \todo Add LLC/SNAP support (see above) */ - registerInterpreter(type(),begin()+bytes(),end()); + << unsigned(p->type()) << "\n" << std::dec; } -prefix_ void senf::EthVLanPacket::v_finalize() -{} - -prefix_ void senf::EthVLanPacket::v_dump(std::ostream & os) - const +prefix_ void senf::EthVLanPacketType::dump(packet p, std::ostream & os) { os << "Ethernet 802.1q (VLAN):\n" - << " priority : " << priority() << "\n" - << " cfi : " << cfi() << "\n" - << " vlan-ID : " << vlanId() << "\n" - << " ethertype : " << boost::format("%04x") % type() << "\n"; + << " priority : " << p->priority() << "\n" + << " cfi : " << p->cfi() << "\n" + << " vlan-ID : " << p->vlanId() << "\n" + << " ethertype : " << std::hex << std::setw(4) << std::setfill('0') + << p->type() << "\n" << std::dec; } ///////////////////////////////cc.e//////////////////////////////////////// @@ -112,4 +141,6 @@ prefix_ void senf::EthVLanPacket::v_dump(std::ostream & os) // c-file-style: "senf" // indent-tabs-mode: nil // ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 // End: