Network/UDPForward: Add udpforward util master
g0dil [Tue, 12 May 2009 07:59:04 +0000 (07:59 +0000)]
SConstruct [new file with mode: 0644]
UDPForwarder.cc [new file with mode: 0644]
UDPForwarder.hh [new file with mode: 0644]
main.cc [new file with mode: 0644]

diff --git a/SConstruct b/SConstruct
new file mode 100644 (file)
index 0000000..b82dd62
--- /dev/null
@@ -0,0 +1,36 @@
+import sys
+sys.path.extend(('senf/senfscons','/usr/lib/senf/senfscons'))
+try:
+    import os.path, glob, senfutil
+except ImportError:
+    print "\nModule import failed. 'senf' symlink missing?\n"
+    raise
+
+env = Environment()
+
+senfutil.SetupForSENF( env )
+
+env.Append(
+
+    LIBS            = [ ],
+    CXXFLAGS        = [ '-Wall', '-Woverloaded-virtual' ],
+    LINKFLAGS       = [ ],
+
+    CXXFLAGS_debug  = [ ],
+    LINKFLAGS_debug = [ ],
+    LOGLEVELS_debug = [ 'senf::log::Debug||VERBOSE' ],
+
+    CXXFLAGS_final  = [ '-O3' ],
+    LINKFLAGS_final = [ ],
+    LOGLEVELS_final = [ ],
+
+    SENF_BUILDOPTS  = [ ],
+
+)
+
+env.Default(
+    env.Program( target = 'udpforward',
+                 source = glob.glob('*.cc') )
+)
+
+env.Clean(DEFAULT_TARGETS, [ 'udpforward.log', 'udpforward.pid' ])
diff --git a/UDPForwarder.cc b/UDPForwarder.cc
new file mode 100644 (file)
index 0000000..c09fab5
--- /dev/null
@@ -0,0 +1,64 @@
+// $Id$
+//
+// Copyright (C) 2008 
+
+/** \file
+    \brief UDPForwarder non-inline non-template implementation */
+
+#include "UDPForwarder.hh"
+//#include "UDPForwarder.ih"
+
+// Custom includes
+
+//#include "UDPForwarder.mpp"
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+prefix_ udpforward::UDPForwarder::UDPForwarder(senf::INet4SocketAddress const & addr,
+                                               senf::INet4SocketAddress const & target)
+    :  socket_ (addr), target_(target), peer_ (), 
+       event_ ("UDPForwarder::read", senf::membind(&UDPForwarder::readEvent, this),
+               socket_, senf::scheduler::FdEvent::EV_READ)
+{
+    SENF_LOG(("Input socket opened at " << addr << ". Forwarding to " << target));
+}
+
+prefix_ void udpforward::UDPForwarder::readEvent(int event)
+{
+    if (event != senf::scheduler::FdEvent::EV_READ) 
+        throw ForwarderException("Unexpected remote socket read failure");
+
+    std::string packet;
+    senf::INet4SocketAddress addr;
+    socket_.readfrom(packet, addr, 0);
+
+    if (addr == target_) {
+        if (! peer_)
+            SENF_LOG((senf::log::MESSAGE)("Return packet dropped (no peer)"));
+        else {
+            SENF_LOG((senf::log::VERBOSE)("Return packet to " << peer_));
+            socket_.writeto(peer_, packet);
+        }
+        return;
+    }
+    
+    if (addr != peer_) {
+        SENF_LOG((senf::log::NOTICE)("Peer address changed from " << peer_ << " to " << addr));
+        peer_ = addr;
+    }
+    socket_.writeto(target_, packet);
+    SENF_LOG((senf::log::VERBOSE)("Forward packet from " << peer_));
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+//#include "UDPForwarder.mpp"
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// indent-tabs-mode: nil
+// compile-command: "scons"
+// comment-column: 40
+// End:
diff --git a/UDPForwarder.hh b/UDPForwarder.hh
new file mode 100644 (file)
index 0000000..dd4c452
--- /dev/null
@@ -0,0 +1,59 @@
+// $Id$
+//
+// Copyright (C) 2008 
+
+/** \file
+    \brief UDPForwarder public header */
+
+#ifndef HH_UDPForwarder_
+#define HH_UDPForwarder_ 1
+
+// Custom includes
+#include <senf/Socket/Protocols/INet/UDPSocketHandle.hh>
+#include <senf/Socket/Protocols/INet/ConnectedUDPSocketHandle.hh>
+#include <senf/Scheduler/Scheduler.hh>
+#include <senf/Utils/membind.hh>
+#include <senf/Utils/Exception.hh>
+#include <senf/Utils/Logger.hh>
+
+//#include "UDPForwarder.mpp"
+///////////////////////////////hh.p////////////////////////////////////////
+
+namespace udpforward {
+
+    class UDPForwarder
+    {
+    public:
+        SENF_LOG_CLASS_AREA();
+
+       UDPForwarder(senf::INet4SocketAddress const & addr,
+                    senf::INet4SocketAddress const & target);
+       
+       struct ForwarderException : public senf::Exception
+       { ForwarderException(std::string const & msg) : senf::Exception(msg) {} };
+       
+    private:
+       void readEvent(int event);
+       
+       senf::UDPv4ClientSocketHandle socket_;
+        senf::INet4SocketAddress target_;
+       senf::INet4SocketAddress peer_;
+       senf::scheduler::FdEvent event_;
+    };
+
+}
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "UDPForwarder.cci"
+//#include "UDPForwarder.ct"
+//#include "UDPForwarder.cti"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// indent-tabs-mode: nil
+// compile-command: "scons"
+// comment-column: 40
+// End:
diff --git a/main.cc b/main.cc
new file mode 100644 (file)
index 0000000..508ad81
--- /dev/null
+++ b/main.cc
@@ -0,0 +1,92 @@
+// $Id$
+//
+// Copyright (C) 2008 
+
+/** \file
+    \brief main non-inline non-template implementation */
+
+//#include "main.hh"
+//#include "main.ih"
+
+// Custom includes
+#include <boost/algorithm/string/predicate.hpp>
+#include <senf/Utils/Daemon.hh>
+#include <senf/Utils/Logger/SenfLog.hh>
+#include "UDPForwarder.hh"
+
+//#include "main.mpp"
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+class UDPForwarderDaemon : public senf::Daemon
+{
+    SENF_LOG_CLASS_AREA();
+
+    void configure();
+    void main();
+
+    static void terminate(siginfo_t const &);
+};
+
+prefix_ void UDPForwarderDaemon::configure()
+{
+    pidFile("udpforward.pid");
+    consoleLog("udpforward.log");
+
+    senf::Daemon::configure();
+}
+
+prefix_ void UDPForwarderDaemon::main()
+{
+    try {
+        removeDaemonArgs();
+
+        if (argc() != 3) {
+            std::cerr << "Invalid number of arguments\n"
+                      << "Usage: " << argv()[0] << " <listen port> <forward port> <target>\n";
+            exit(1);
+        }
+        
+        senf::log::ConsoleTarget::instance().route<senf::log::VERBOSE>();
+
+        SENF_LOG(("udpforwarder starting"));
+        
+        senf::INet4SocketAddress listenPort (argv()[1]);
+        senf::INet4SocketAddress targetPort (argv()[2]);
+
+        udpforward::UDPForwarder forwarder ( listenPort, targetPort );
+
+       senf::scheduler::SignalEvent sigintEvent (SIGINT, &terminate);
+       senf::scheduler::SignalEvent sigtermEvent (SIGTERM, &terminate);
+
+        detach();
+        
+        senf::scheduler::process();
+
+        SENF_LOG(("signal received. Terminating."));
+    }
+    catch (std::exception & ex) {
+        SENF_LOG((senf::log::FATAL)(ex.what() << "\n" "terminating unexpectedly."));
+        exit(1);
+    }
+}
+
+prefix_ void UDPForwarderDaemon::terminate(siginfo_t const &) 
+{
+    senf::scheduler::terminate();
+}
+
+SENF_DAEMON_MAIN(UDPForwarderDaemon);
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+//#include "main.mpp"
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// indent-tabs-mode: nil
+// compile-command: "scons"
+// comment-column: 40
+// End: