adding IPv4SourceForcingDgramWriter. use inheritance for IPv?SourceForcingWriter
dw6 [Wed, 19 Nov 2008 13:52:16 +0000 (13:52 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@966 270642c3-0616-0410-b53a-bc976706d245

PPI/SocketSink.cc
PPI/SocketSink.hh

index 5c13c6c..6c03858 100644 (file)
 
 #define prefix_
 
+prefix_ senf::ppi::IPv4SourceForcingDgramWriter::IPv4SourceForcingDgramWriter(){
+    source_ = senf::INet4Address::from_string("0.0.0.0");
+    destination_ = senf::INet4Address::from_string("0.0.0.0");
+    protocolId_ = 0;
+}
+
+prefix_ senf::ppi::IPv4SourceForcingDgramWriter::IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr){
+    source(sourceAddr);
+    destination(destAddr);
+}
+
+prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::source(senf::INet4Address & source){
+    source_ = source;
+}
+prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::destination(senf::INet4SocketAddress & dest){
+    destination_ = dest.address();
+    protocolId_ = dest.port();
+}
+
+prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::operator()(Handle handle, Packet packet){
+    sendtoandfrom(
+            handle.fd(),
+            reinterpret_cast<void*> (&*packet.data().begin()),
+            packet.size(),
+            reinterpret_cast<in_addr*> (&destination_),
+            protocolId_,
+            reinterpret_cast<in_addr*> (&source_));
+}
+
+prefix_ int senf::ppi::IPv4SourceForcingDgramWriter::sendtoandfrom(
+        int sock,
+        const void *data,
+        size_t dataLen,
+        const in_addr *dst,
+        int port,
+        const in_addr *src)
+{
+    uint8_t cbuf[CMSG_SPACE(sizeof(struct in_pktinfo))];
+    struct cmsghdr *c = (struct cmsghdr *)cbuf;
+    struct in_pktinfo *pi;
+    struct iovec iov;
+    struct msghdr h;
+
+    c->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
+    c->cmsg_level = IPPROTO_IP;
+    c->cmsg_type = IP_PKTINFO;
+
+    pi = (struct in_pktinfo *)CMSG_DATA(c);
+    pi->ipi_ifindex = 0;
+    memcpy(&pi->ipi_addr, src, 16);
+
+    iov.iov_base = (void *)data;
+    iov.iov_len = dataLen;
+
+    sockaddr_in dstpeer;
+    memset(&dstpeer, 0, sizeof(dstpeer));
+    dstpeer.sin_family = AF_INET;
+    dstpeer.sin_addr = *dst;
+    dstpeer.sin_port = htons(port);
+
+    memset(&h, 0, sizeof(h));
+    h.msg_name = (struct sockaddr *)&dstpeer;
+    h.msg_namelen = sizeof(dstpeer);
+    h.msg_iov = &iov;
+    h.msg_iovlen = 1;
+    h.msg_control = c;
+    h.msg_controllen = sizeof(cbuf);
+
+    return sendmsg(sock, &h, 0);
+}
+
 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter(){
     source_ = senf::INet6Address::from_string("0::0");
     destination_ = senf::INet6Address::from_string("0::0");
-    protocolId_ = 0; 
+    protocolId_ = 0;
 }
-        
+
 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr){
     source(sourceAddr);
     destination(destAddr);
 }
 
 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::source(senf::INet6Address & source){
-    source_ = source; 
+    source_ = source;
 }
 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::destination(senf::INet6SocketAddress & dest){
     destination_ = dest.address();
@@ -50,20 +121,20 @@ prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::destination(senf::INet6Soc
 
 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::operator()(Handle handle, Packet packet){
     sendtoandfrom(
-            handle.fd(), 
-            reinterpret_cast<void*> (&*packet.data().begin()), 
-            packet.size(), 
-            reinterpret_cast<in6_addr*> (&destination_), 
-            protocolId_, 
+            handle.fd(),
+            reinterpret_cast<void*> (&*packet.data().begin()),
+            packet.size(),
+            reinterpret_cast<in6_addr*> (&destination_),
+            protocolId_,
             reinterpret_cast<in6_addr*> (&source_));
 }
 
 prefix_ int senf::ppi::IPv6SourceForcingDgramWriter::sendtoandfrom(
-        int sock, 
-        const void *data, 
-        size_t dataLen, 
+        int sock,
+        const void *data,
+        size_t dataLen,
         const in6_addr *dst,
-        int port, 
+        int port,
         const in6_addr *src)
 {
     uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
index a5c384b..985befd 100644 (file)
@@ -45,8 +45,8 @@ namespace senf {
 namespace ppi {
 
     /** \brief Writer for module::ActiveSocketSink / module::PassiveSocketSink
-        
-        This writer will write the packets completely as datagrams to the given socket which must be connected. 
+
+        This writer will write the packets completely as datagrams to the given socket which must be connected.
      */
     class ConnectedDgramWriter
     {
@@ -56,7 +56,7 @@ namespace ppi {
                                     senf::DatagramFramingPolicy,
                                     senf::ConnectedCommunicationPolicy>::policy > Handle;
                                         ///< Handle type supported by this writer
-        
+
         void operator()(Handle handle, Packet packet);
                                         ///< Write \a packet to \a handle
                                         /**< Write the complete \a packet as a datagram to \a
@@ -65,7 +65,35 @@ namespace ppi {
                                              \param[in] packet Packet to write */
     };
 
-    class IPv6SourceForcingDgramWriter
+    class IPv4SourceForcingDgramWriter : ConnectedDgramWriter
+    {
+    public:
+        IPv4SourceForcingDgramWriter();
+        IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr);
+        typedef senf::ClientSocketHandle<
+            senf::MakeSocketPolicy< senf::WriteablePolicy,
+                                    senf::DatagramFramingPolicy>::policy > Handle;
+                                        ///< Handle type supported by this writer
+
+        void source(senf::INet4Address & source);
+        senf::INet4Address source();
+        void destination(senf::INet4SocketAddress & dest);
+        senf::INet4SocketAddress destination();
+
+        void operator()(Handle handle, Packet packet);
+                                        ///< Write \a packet to \a handle
+                                        /**< Write the complete \a packet as a datagram to \a
+                                             handle.
+                                             \param[in] handle Handle to write data to
+                                             \param[in] packet Packet to write */
+    private:
+        int sendtoandfrom(int sock, const void *data, size_t dataLen, const in_addr *dst, int port, const in_addr *src);
+        senf::INet4Address source_;
+        senf::INet4Address destination_;
+        unsigned int protocolId_;
+};
+
+    class IPv6SourceForcingDgramWriter : ConnectedDgramWriter
     {
     public:
         IPv6SourceForcingDgramWriter();
@@ -74,7 +102,7 @@ namespace ppi {
             senf::MakeSocketPolicy< senf::WriteablePolicy,
                                     senf::DatagramFramingPolicy>::policy > Handle;
                                         ///< Handle type supported by this writer
-        
+
         void source(senf::INet6Address & source);
         senf::INet6Address source();
         void destination(senf::INet6SocketAddress & dest);
@@ -91,9 +119,9 @@ namespace ppi {
         senf::INet6Address source_;
         senf::INet6Address destination_;
         unsigned int protocolId_;
-};    
-    
-    
+};
+
+
 }}
 
 namespace senf {
@@ -101,9 +129,9 @@ namespace ppi {
 namespace module {
 
     /** \brief Output %module writing data to a FileHandle using the provided Writer.
-        If using the default ConnectedDgramWriter the filehandle must be writable, connected and 
-        able to handle complete datagrams.  
-        
+        If using the default ConnectedDgramWriter the filehandle must be writable, connected and
+        able to handle complete datagrams.
+
         This output %module will write data to a FileHandle object using a given \a Writer. This
         output %module is active. This requires the file handle to be able to signal its readiness to
         accept more data via the Scheduler.
@@ -137,16 +165,16 @@ namespace module {
         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
 
         connector::ActiveInput<> input; ///< Input connector from which data is received
-        
+
         ActiveSocketSink(Handle handle); ///< Create new writer for the given handle
                                         /**< Data will be written to \a handle using \a Writer.
                                              \pre Requires \a Writer to be default constructible
                                              \param[in] handle Handle to write data to */
-        ActiveSocketSink(Handle handle, Writer const & writer); 
+        ActiveSocketSink(Handle handle, Writer const & writer);
                                         ///< Create new writer for the given handle
                                         /**< Data will be written to \a handle using \a Writer.
                                              \pre Requires \a Writer to be copy constructible
-                                             \param[in] handle Handle to write data to 
+                                             \param[in] handle Handle to write data to
                                              \param[in] writer Writer helper writing packet date to the
                                                  socket */
 
@@ -160,9 +188,9 @@ namespace module {
     };
 
     /** \brief Output module writing data to a FileHandle using the provided \a Writer.
-        If using the default ConnectedDgramWriter the filehandle must be writable, connected and 
-        able to handle complete datagrams.  
-        
+        If using the default ConnectedDgramWriter the filehandle must be writable, connected and
+        able to handle complete datagrams.
+
         This output module will write data to a FileHandle object using a given \a Writer. This
         output module is passive. This implies, that the output handle may not block. This also
         implies, that data will probably get lost if written to fast for the underlying transport
@@ -198,7 +226,7 @@ namespace module {
         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
 
         connector::PassiveInput<> input; ///< Input connector from which data is received
-        
+
         PassiveSocketSink(Handle handle); ///< Create new writer for the given handle
                                         /**< Data will be written to \a handle using \a Writer.
                                              \pre Requires \a Writer to be default constructible
@@ -210,7 +238,7 @@ namespace module {
                                              \param[in] handle Handle to write data to */
 
         Writer & writer();      ///< Access the Writer
-        Handle & handle();      /**< Access the handle. This is intendet to be mainly used to reconnect 
+        Handle & handle();      /**< Access the handle. This is intendet to be mainly used to reconnect
                                      the underlying socket. */
        /* void reconnect(senf::SocketAddress newAddress);
         ///< Reconnect the handle to which the packets are written
@@ -218,9 +246,9 @@ namespace module {
         void replaceHandle(Handle newHandle);
                                         /**< Replace the handle to which the packets are written
                                          * Normally you should access the handle and call connect with
-                                         * the new address. This also works for other 
+                                         * the new address. This also works for other
                                          * (active) ConnectedSocketSinks/Sources */
-                                        
+
     private:
         void write();
 
@@ -237,7 +265,7 @@ namespace module {
 #include "SocketSink.cti"
 #endif
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100