1 // $Id: SocketSink.cc 661 2008-02-05 09:53:54Z dw6 $
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // David Wagner <dw6@berlios.de>
7 // The contents of this file are subject to the Fraunhofer FOKUS Public License
8 // Version 1.0 (the "License"); you may not use this file except in compliance
9 // with the License. You may obtain a copy of the License at
10 // http://senf.berlios.de/license.html
12 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
13 // but modifies the Mozilla Public License Version 1.1.
14 // See the full license text for the amendments.
16 // Software distributed under the License is distributed on an "AS IS" basis,
17 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
18 // for the specific language governing rights and limitations under the License.
20 // The Original Code is Fraunhofer FOKUS code.
22 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
23 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
24 // All Rights Reserved.
29 \brief SocketSink non-inline non-template implementation */
32 #include "SocketSink.hh"
33 #include <senf/Socket/ClientSocketHandle.hh>
37 prefix_ senf::ppi::IPv4SourceForcingDgramWriter::IPv4SourceForcingDgramWriter()
39 source_ = senf::INet4Address::from_string("0.0.0.0");
40 destination_ = senf::INet4Address::from_string("0.0.0.0");
44 prefix_ senf::ppi::IPv4SourceForcingDgramWriter::
45 IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr)
48 destination(destAddr);
51 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::source(senf::INet4Address & source)
56 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::destination(senf::INet4SocketAddress & dest)
58 destination_ = dest.address();
59 protocolId_ = dest.port();
62 prefix_ bool senf::ppi::IPv4SourceForcingDgramWriter::operator()(Handle & handle,
63 Packet const & packet)
67 reinterpret_cast<void*> (&*packet.data().begin()),
69 reinterpret_cast<in_addr*> (&destination_),
71 reinterpret_cast<in_addr*> (&source_)) > 0;
74 prefix_ int senf::ppi::IPv4SourceForcingDgramWriter::sendtoandfrom(
82 uint8_t cbuf[CMSG_SPACE(sizeof(struct in_pktinfo))];
83 struct cmsghdr *c = (struct cmsghdr *)cbuf;
84 struct in_pktinfo *pi;
88 c->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
89 c->cmsg_level = IPPROTO_IP;
90 c->cmsg_type = IP_PKTINFO;
92 pi = (struct in_pktinfo *)CMSG_DATA(c);
94 memcpy(&pi->ipi_addr, src, 16);
96 iov.iov_base = (void *)data;
97 iov.iov_len = dataLen;
100 memset(&dstpeer, 0, sizeof(dstpeer));
101 dstpeer.sin_family = AF_INET;
102 dstpeer.sin_addr = *dst;
103 dstpeer.sin_port = htons(port);
105 memset(&h, 0, sizeof(h));
106 h.msg_name = (struct sockaddr *)&dstpeer;
107 h.msg_namelen = sizeof(dstpeer);
111 h.msg_controllen = sizeof(cbuf);
113 return sendmsg(sock, &h, 0);
116 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter()
118 source_ = senf::INet6Address::from_string("0::0");
119 destination_ = senf::INet6Address::from_string("0::0");
123 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::
124 IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr)
127 destination(destAddr);
130 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::source(senf::INet6Address & source)
135 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::destination(senf::INet6SocketAddress & dest)
137 destination_ = dest.address();
138 protocolId_ = dest.port();
141 prefix_ bool senf::ppi::IPv6SourceForcingDgramWriter::operator()(Handle & handle,
142 Packet const & packet)
144 return sendtoandfrom(
146 reinterpret_cast<void*> (&*packet.data().begin()),
148 reinterpret_cast<in6_addr*> (&destination_),
150 reinterpret_cast<in6_addr*> (&source_)) > 0;
153 prefix_ int senf::ppi::IPv6SourceForcingDgramWriter::sendtoandfrom(
161 uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
162 struct cmsghdr *c = (struct cmsghdr *)cbuf;
163 struct in6_pktinfo *pi;
167 c->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
168 c->cmsg_level = IPPROTO_IPV6;
169 c->cmsg_type = IPV6_PKTINFO;
171 pi = (struct in6_pktinfo *)CMSG_DATA(c);
172 pi->ipi6_ifindex = 0;
173 memcpy(&pi->ipi6_addr, src, 16);
175 iov.iov_base = (void *)data;
176 iov.iov_len = dataLen;
178 sockaddr_in6 dstpeer;
179 memset(&dstpeer, 0, sizeof(dstpeer));
180 dstpeer.sin6_family = AF_INET6;
181 dstpeer.sin6_addr = *dst;
182 dstpeer.sin6_port = htons(port);
184 memset(&h, 0, sizeof(h));
185 h.msg_name = (struct sockaddr *)&dstpeer;
186 h.msg_namelen = sizeof(dstpeer);
190 h.msg_controllen = sizeof(cbuf);
192 return sendmsg(sock, &h, 0);