1 // $Id: SocketSink.cc 661 2008-02-05 09:53:54Z dw6 $
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // David Wagner <dw6@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief SocketSink non-inline non-template implementation */
27 #include "SocketSink.hh"
28 #include "../Socket/ClientSocketHandle.hh"
32 prefix_ senf::ppi::IPv4SourceForcingDgramWriter::IPv4SourceForcingDgramWriter(){
33 source_ = senf::INet4Address::from_string("0.0.0.0");
34 destination_ = senf::INet4Address::from_string("0.0.0.0");
38 prefix_ senf::ppi::IPv4SourceForcingDgramWriter::IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr){
40 destination(destAddr);
43 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::source(senf::INet4Address & source){
46 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::destination(senf::INet4SocketAddress & dest){
47 destination_ = dest.address();
48 protocolId_ = dest.port();
51 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::operator()(Handle handle,
52 Packet const & packet){
55 reinterpret_cast<void*> (&*packet.data().begin()),
57 reinterpret_cast<in_addr*> (&destination_),
59 reinterpret_cast<in_addr*> (&source_));
62 prefix_ int senf::ppi::IPv4SourceForcingDgramWriter::sendtoandfrom(
70 uint8_t cbuf[CMSG_SPACE(sizeof(struct in_pktinfo))];
71 struct cmsghdr *c = (struct cmsghdr *)cbuf;
72 struct in_pktinfo *pi;
76 c->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
77 c->cmsg_level = IPPROTO_IP;
78 c->cmsg_type = IP_PKTINFO;
80 pi = (struct in_pktinfo *)CMSG_DATA(c);
82 memcpy(&pi->ipi_addr, src, 16);
84 iov.iov_base = (void *)data;
85 iov.iov_len = dataLen;
88 memset(&dstpeer, 0, sizeof(dstpeer));
89 dstpeer.sin_family = AF_INET;
90 dstpeer.sin_addr = *dst;
91 dstpeer.sin_port = htons(port);
93 memset(&h, 0, sizeof(h));
94 h.msg_name = (struct sockaddr *)&dstpeer;
95 h.msg_namelen = sizeof(dstpeer);
99 h.msg_controllen = sizeof(cbuf);
101 return sendmsg(sock, &h, 0);
104 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter(){
105 source_ = senf::INet6Address::from_string("0::0");
106 destination_ = senf::INet6Address::from_string("0::0");
110 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr){
112 destination(destAddr);
115 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::source(senf::INet6Address & source){
118 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::destination(senf::INet6SocketAddress & dest){
119 destination_ = dest.address();
120 protocolId_ = dest.port();
123 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::operator()(Handle handle,
124 Packet const & packet){
127 reinterpret_cast<void*> (&*packet.data().begin()),
129 reinterpret_cast<in6_addr*> (&destination_),
131 reinterpret_cast<in6_addr*> (&source_));
134 prefix_ int senf::ppi::IPv6SourceForcingDgramWriter::sendtoandfrom(
142 uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
143 struct cmsghdr *c = (struct cmsghdr *)cbuf;
144 struct in6_pktinfo *pi;
148 c->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
149 c->cmsg_level = IPPROTO_IPV6;
150 c->cmsg_type = IPV6_PKTINFO;
152 pi = (struct in6_pktinfo *)CMSG_DATA(c);
153 pi->ipi6_ifindex = 0;
154 memcpy(&pi->ipi6_addr, src, 16);
156 iov.iov_base = (void *)data;
157 iov.iov_len = dataLen;
159 sockaddr_in6 dstpeer;
160 memset(&dstpeer, 0, sizeof(dstpeer));
161 dstpeer.sin6_family = AF_INET6;
162 dstpeer.sin6_addr = *dst;
163 dstpeer.sin6_port = htons(port);
165 memset(&h, 0, sizeof(h));
166 h.msg_name = (struct sockaddr *)&dstpeer;
167 h.msg_namelen = sizeof(dstpeer);
171 h.msg_controllen = sizeof(cbuf);
173 return sendmsg(sock, &h, 0);