Packets/DefaultBundle: Make all packet aprsers use the new addressing classes
g0dil [Tue, 31 Jul 2007 09:39:56 +0000 (09:39 +0000)]
Examples/Sniffer: Add 'interface' command line argument

git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@364 270642c3-0616-0410-b53a-bc976706d245

Examples/Sniffer/Sniffer.cc
Packets/DefaultBundle/IpV4Packet.cc
Packets/DefaultBundle/IpV4Packet.hh
Packets/DefaultBundle/IpV4Packet.test.cc
Packets/DefaultBundle/IpV6Extensions.test.cc
Packets/DefaultBundle/IpV6Packet.cc
Packets/DefaultBundle/IpV6Packet.hh
Packets/DefaultBundle/IpV6Packet.test.cc

index d748b11..05a0dbc 100644 (file)
@@ -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;
 }
 
index aa5018c..b88987b 100644 (file)
@@ -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////////////////////////////////////////
index 7fca40e..0dd8344 100644 (file)
@@ -27,6 +27,7 @@
 #define HH_IpV4Packet_ 1
 
 // Custom includes
+#include "Socket/Protocols/INet/INet4Address.hh"
 #include "Packets/Packets.hh"
 
 //#include "IpV4Packet.mpp"
 
 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
 
index a27238c..9cc976b 100644 (file)
@@ -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////////////////////////////////////////
index 457597b..aa5bf45 100644 (file)
@@ -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>() );
 
     senf::IpV6Extension_Fragment f (p.next().as<senf::IpV6Extension_Fragment>());
index 520772e..dfb1aea 100644 (file)
@@ -25,6 +25,7 @@
 //#include "IpV6Packet.ih"
 
 // Custom includes
+#include <boost/io/ios_state.hpp>
 #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////////////////////////////////////////
index 33b5fc2..b93a81b 100644 (file)
@@ -25,6 +25,7 @@
 #define HH_IpV6Packet_ 1
 
 // Custom includes
+#include "Socket/Protocols/INet/INet6Address.hh"
 #include "Packets/Packets.hh"
 #include "IpV4Packet.hh"
 
 
 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
             <a href="http://tools.ietf.org/html/rfc2460">RFC 2460</a>
@@ -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
 
index c65b062..c3bf90b 100644 (file)
@@ -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() );