From: g0dil Date: Tue, 31 Jul 2007 09:39:56 +0000 (+0000) Subject: Packets/DefaultBundle: Make all packet aprsers use the new addressing classes X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=3b088b5c4d84e10b42b36f047911b34b03b49b03;p=senf.git Packets/DefaultBundle: Make all packet aprsers use the new addressing classes Examples/Sniffer: Add 'interface' command line argument git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@364 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Examples/Sniffer/Sniffer.cc b/Examples/Sniffer/Sniffer.cc index d748b11..05a0dbc 100644 --- a/Examples/Sniffer/Sniffer.cc +++ b/Examples/Sniffer/Sniffer.cc @@ -84,7 +84,7 @@ int loop_main (int argc, char const * argv[]) { try { senf::PacketSocketHandle sock; - sock.bind(senf::LLSocketAddress("eth1")); + sock.bind(senf::LLSocketAddress(argv[2])); // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous); while (true) { // forever @@ -136,7 +136,7 @@ private: int scheduler_main(int argc, char const * argv[]) { try { - Sniffer sniffer ("eth1"); + Sniffer sniffer (argv[2]); sniffer.run(); } catch (std::exception const & ex) { @@ -147,13 +147,13 @@ int scheduler_main(int argc, char const * argv[]) int main(int argc, char const * argv[]) { - if (argc >= 2) + if (argc >= 3) if (std::string(argv[1]) == "loop") return loop_main(argc,argv); else if (std::string(argv[1]) == "scheduler") return scheduler_main(argc,argv); - std::cerr << "Usage: sniffer { loop | scheduler }" << std::endl; + std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl; return 1; } diff --git a/Packets/DefaultBundle/IpV4Packet.cc b/Packets/DefaultBundle/IpV4Packet.cc index aa5018c..b88987b 100644 --- a/Packets/DefaultBundle/IpV4Packet.cc +++ b/Packets/DefaultBundle/IpV4Packet.cc @@ -45,12 +45,6 @@ namespace { prefix_ void senf::IpV4PacketType::dump(packet p, std::ostream & os) { - struct in_addr in; - in.s_addr = htonl(p->source()); - char buffer[128]; - std::string src (inet_ntop(AF_INET,&in,buffer,128)); - in.s_addr = htonl(p->destination()); - std::string dst (inet_ntop(AF_INET,&in,buffer,128)); os << "Internet protocol Version 4:\n" << " version : " << p->version() << "\n" << " IHL : " << p->ihl() << "\n" @@ -63,8 +57,8 @@ prefix_ void senf::IpV4PacketType::dump(packet p, std::ostream & os) << " TTL : " << unsigned(p->ttl()) << "\n" << " protocol : " << unsigned(p->protocol()) << "\n" << " CRC : " << std::hex << p->crc() << std::dec << "\n" - << " source : " << src << "\n" - << " destination : " << dst << "\n"; + << " source : " << p->source() << "\n" + << " destination : " << p->destination() << "\n"; } ///////////////////////////////cc.e//////////////////////////////////////// diff --git a/Packets/DefaultBundle/IpV4Packet.hh b/Packets/DefaultBundle/IpV4Packet.hh index 7fca40e..0dd8344 100644 --- a/Packets/DefaultBundle/IpV4Packet.hh +++ b/Packets/DefaultBundle/IpV4Packet.hh @@ -27,6 +27,7 @@ #define HH_IpV4Packet_ 1 // Custom includes +#include "Socket/Protocols/INet/INet4Address.hh" #include "Packets/Packets.hh" //#include "IpV4Packet.mpp" @@ -34,6 +35,27 @@ namespace senf { + /** \brief Parse in IpV4 address + + \see INet4Address + */ + struct Parse_INet4Address : public PacketParserBase + { + Parse_INet4Address(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {} + + /////////////////////////////////////////////////////////////////////////// + + typedef INet4Address value_type; + static const size_type fixed_bytes = 4u; + + value_type value() const { return value_type::from_data(i()); } + void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); } + operator value_type() { return value(); } + byte & operator[](size_type index) { return *boost::next(i(),index); } + Parse_INet4Address const & operator= (value_type const & other) + { value(other); return *this; } + }; + /** \brief Parse an IpV4 packet Parser implementing the IpV4 header. The fields implemented are: @@ -54,6 +76,7 @@ namespace senf { typedef Parse_Flag < 2 > Parse_MF; typedef Parse_UIntField < 3, 16 > Parse_Frag; typedef Parse_UInt32 Parse_32bit; + typedef Parse_INet4Address Parse_Addr; # ifndef DOXYGEN @@ -72,8 +95,8 @@ namespace senf { ((Field )( ttl, Parse_8bit )) ((Field )( protocol, Parse_8bit )) ((Field )( crc, Parse_16bit )) - ((Field )( source, Parse_32bit )) - ((Field )( destination, Parse_32bit )) ); + ((Field )( source, Parse_Addr )) + ((Field )( destination, Parse_Addr )) ); # else @@ -89,8 +112,8 @@ namespace senf { Parse_8bit ttl() const; Parse_8bit protocol() const; Parse_16bit crc() const; - Parse_32bit source() const; - Parse_32bit destination() const; + Parse_Addr source() const; + Parse_Addr destination() const; # endif diff --git a/Packets/DefaultBundle/IpV4Packet.test.cc b/Packets/DefaultBundle/IpV4Packet.test.cc index a27238c..9cc976b 100644 --- a/Packets/DefaultBundle/IpV4Packet.test.cc +++ b/Packets/DefaultBundle/IpV4Packet.test.cc @@ -60,8 +60,8 @@ BOOST_AUTO_UNIT_TEST(ipV4Packet_packet) BOOST_CHECK_EQUAL( p->ttl(), 0x09u ); BOOST_CHECK_EQUAL( p->protocol(), 0x0Au ); BOOST_CHECK_EQUAL( p->crc(), 0x0B0Cu ); - BOOST_CHECK_EQUAL( p->source(), 0x11121314u ); - BOOST_CHECK_EQUAL( p->destination(), 0x15161718u ); + BOOST_CHECK_EQUAL( p->source().value(), senf::INet4Address(0x11121314u) ); + BOOST_CHECK_EQUAL( p->destination().value(), senf::INet4Address(0x15161718u) ); } ///////////////////////////////cc.e//////////////////////////////////////// diff --git a/Packets/DefaultBundle/IpV6Extensions.test.cc b/Packets/DefaultBundle/IpV6Extensions.test.cc index 457597b..aa5bf45 100644 --- a/Packets/DefaultBundle/IpV6Extensions.test.cc +++ b/Packets/DefaultBundle/IpV6Extensions.test.cc @@ -69,10 +69,8 @@ BOOST_AUTO_UNIT_TEST(ipv6Extension_Fragment_packet) BOOST_CHECK_EQUAL( p->version(), 6u ); BOOST_CHECK_EQUAL( p->length(), 20u ); BOOST_CHECK_EQUAL( p->nextHeader(), 44u ); - BOOST_CHECK_EQUAL( senf::INet6Address::from_data(p->source().i()), - senf::INet6Address::from_string("2001::1") ); - BOOST_CHECK_EQUAL( senf::INet6Address::from_data(p->destination().i()), - senf::INet6Address::from_string("2001::2") ); + BOOST_CHECK_EQUAL( p->source().value(), senf::INet6Address::from_string("2001::1") ); + BOOST_CHECK_EQUAL( p->destination().value(), senf::INet6Address::from_string("2001::2") ); BOOST_CHECK( p.next().is() ); senf::IpV6Extension_Fragment f (p.next().as()); diff --git a/Packets/DefaultBundle/IpV6Packet.cc b/Packets/DefaultBundle/IpV6Packet.cc index 520772e..dfb1aea 100644 --- a/Packets/DefaultBundle/IpV6Packet.cc +++ b/Packets/DefaultBundle/IpV6Packet.cc @@ -25,6 +25,7 @@ //#include "IpV6Packet.ih" // Custom includes +#include #include "EthernetPacket.hh" #include "Socket/Protocols/INet/INetAddressing.hh" @@ -45,6 +46,7 @@ namespace { prefix_ void senf::IpV6PacketType::dump(packet p, std::ostream & os) { + boost::io::ios_all_saver ias(os); os << "Internet protocol Version 6:\n" << " version : " << unsigned(p->version()) << "\n" << " traffic class : " << std::hex << unsigned(p->trafficClass()) << "\n" @@ -52,8 +54,8 @@ prefix_ void senf::IpV6PacketType::dump(packet p, std::ostream & os) << " length : " << std::dec << unsigned(p->length()) << "\n" << " next header : " << unsigned(p->nextHeader()) << "\n" << " hop limit : " << unsigned(p->hopLimit()) << "\n" - << " source : " << INet6Address::from_data(p->source().i()) << "\n" - << " destination : " << INet6Address::from_data(p->destination().i()) << "\n"; + << " source : " << p->source() << "\n" + << " destination : " << p->destination() << "\n"; } ///////////////////////////////cc.e//////////////////////////////////////// diff --git a/Packets/DefaultBundle/IpV6Packet.hh b/Packets/DefaultBundle/IpV6Packet.hh index 33b5fc2..b93a81b 100644 --- a/Packets/DefaultBundle/IpV6Packet.hh +++ b/Packets/DefaultBundle/IpV6Packet.hh @@ -25,6 +25,7 @@ #define HH_IpV6Packet_ 1 // Custom includes +#include "Socket/Protocols/INet/INet6Address.hh" #include "Packets/Packets.hh" #include "IpV4Packet.hh" @@ -33,9 +34,28 @@ namespace senf { - /** \brief Parse an IpV6 packet + /** \brief Parse an IpV6 address + + \see INet6Address + */ + struct Parse_INet6Address : public PacketParserBase + { + Parse_INet6Address(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {} + + /////////////////////////////////////////////////////////////////////////// - Parser implementing the IpV6 header. The fields implemented are: + typedef INet6Address value_type; + static const size_type fixed_bytes = 16u; + + value_type value() const { return value_type::from_data(i()); } + void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); } + operator value_type() { return value(); } + byte & operator[](size_type index) { return *boost::next(i(),index); } + Parse_INet6Address const & operator= (value_type const & other) + { value(other); return *this; } + }; + + /** \brief Parse an IpV6 packet \see IpV6PacketType \n RFC 2460 @@ -47,8 +67,7 @@ namespace senf { typedef Parse_UIntField < 12, 32 > Parse_FlowLabel; typedef Parse_UInt8 Parse_8bit; typedef Parse_UInt16 Parse_16bit; - - typedef Parse_Array < 16, Parse_8bit > Parse_Addr; + typedef Parse_INet6Address Parse_Addr; # ifndef DOXYGEN diff --git a/Packets/DefaultBundle/IpV6Packet.test.cc b/Packets/DefaultBundle/IpV6Packet.test.cc index c65b062..c3bf90b 100644 --- a/Packets/DefaultBundle/IpV6Packet.test.cc +++ b/Packets/DefaultBundle/IpV6Packet.test.cc @@ -54,9 +54,9 @@ BOOST_AUTO_UNIT_TEST(ipV6Packet_packet) BOOST_CHECK_EQUAL( p->length(), 0x0102u ); BOOST_CHECK_EQUAL( p->nextHeader(), 0x03u ); BOOST_CHECK_EQUAL( p->hopLimit(), 0x04u ); - BOOST_CHECK_EQUAL( senf::INet6Address::from_data(p->source().i()), + BOOST_CHECK_EQUAL( p->source().value(), senf::INet6Address::from_string("1011:1213:1415:1617:1819:1a1b:1c1d:1e1f") ); - BOOST_CHECK_EQUAL( senf::INet6Address::from_data(p->destination().i()), + BOOST_CHECK_EQUAL( p->destination().value(), senf::INet6Address::from_string("2021:2223:2425:2627:2829:2a2b:2c2d:2e2f") ); BOOST_CHECK( p.next() );