From: dw6 Date: Thu, 29 May 2008 10:51:32 +0000 (+0000) Subject: IPv6SourceForcingDgramWriter added. this is more a proof of concept, proof of variabi... X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=0d0ad213c710645ba63515cfcd0e734d8ff2f1a5;hp=45df7138999be77ea7949c6249ca973305290f48;p=senf.git IPv6SourceForcingDgramWriter added. this is more a proof of concept, proof of variability of the writer concept than a must-have integrated part of senf git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@859 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/PPI/SocketSink.cc b/PPI/SocketSink.cc new file mode 100644 index 0000000..5c13c6c --- /dev/null +++ b/PPI/SocketSink.cc @@ -0,0 +1,103 @@ +// $Id: SocketSink.cc 661 2008-02-05 09:53:54Z dw6 $ +// +// Copyright (C) 2007 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// David Wagner +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief SocketSink non-inline non-template implementation */ + +// Custom includes +#include "SocketSink.hh" +#include "../Socket/ClientSocketHandle.hh" + +#define prefix_ + +prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter(){ + source_ = senf::INet6Address::from_string("0::0"); + destination_ = senf::INet6Address::from_string("0::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; +} +prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::destination(senf::INet6SocketAddress & dest){ + destination_ = dest.address(); + protocolId_ = dest.port(); +} + +prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::operator()(Handle handle, Packet packet){ + sendtoandfrom( + handle.fd(), + reinterpret_cast (&*packet.data().begin()), + packet.size(), + reinterpret_cast (&destination_), + protocolId_, + reinterpret_cast (&source_)); +} + +prefix_ int senf::ppi::IPv6SourceForcingDgramWriter::sendtoandfrom( + int sock, + const void *data, + size_t dataLen, + const in6_addr *dst, + int port, + const in6_addr *src) +{ + uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; + struct cmsghdr *c = (struct cmsghdr *)cbuf; + struct in6_pktinfo *pi; + struct iovec iov; + struct msghdr h; + + c->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); + c->cmsg_level = IPPROTO_IPV6; + c->cmsg_type = IPV6_PKTINFO; + + pi = (struct in6_pktinfo *)CMSG_DATA(c); + pi->ipi6_ifindex = 0; + memcpy(&pi->ipi6_addr, src, 16); + + iov.iov_base = (void *)data; + iov.iov_len = dataLen; + + sockaddr_in6 dstpeer; + memset(&dstpeer, 0, sizeof(dstpeer)); + dstpeer.sin6_family = AF_INET6; + dstpeer.sin6_addr = *dst; + dstpeer.sin6_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); +} + +#undef prefix_ diff --git a/PPI/SocketSink.hh b/PPI/SocketSink.hh index 00ff17d..f7f61a5 100644 --- a/PPI/SocketSink.hh +++ b/PPI/SocketSink.hh @@ -35,6 +35,8 @@ #include "../Socket/CommunicationPolicy.hh" #include "Module.hh" #include "Connectors.hh" +#include "../Socket/Protocols/INet/INetAddressing.hh" +#include "IOEvent.hh" //#include "SocketSink.mpp" ///////////////////////////////hh.p//////////////////////////////////////// @@ -63,6 +65,35 @@ namespace ppi { \param[in] packet Packet to write */ }; + class IPv6SourceForcingDgramWriter + { + public: + IPv6SourceForcingDgramWriter(); + IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr); + typedef senf::ClientSocketHandle< + 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); + senf::INet6SocketAddress 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 in6_addr *dst, int port, const in6_addr *src); + senf::INet6Address source_; + senf::INet6Address destination_; + unsigned int protocolId_; +}; + + }} namespace senf {