6c03858a0243cd0dbe74b944d29b0706eabd62c9
[senf.git] / PPI / SocketSink.cc
1 // $Id: SocketSink.cc 661 2008-02-05 09:53:54Z dw6 $
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //    David Wagner <dw6@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief SocketSink non-inline non-template implementation */
25
26 // Custom includes
27 #include "SocketSink.hh"
28 #include "../Socket/ClientSocketHandle.hh"
29
30 #define prefix_
31
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");
35     protocolId_ = 0;
36 }
37
38 prefix_ senf::ppi::IPv4SourceForcingDgramWriter::IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr){
39     source(sourceAddr);
40     destination(destAddr);
41 }
42
43 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::source(senf::INet4Address & source){
44     source_ = source;
45 }
46 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::destination(senf::INet4SocketAddress & dest){
47     destination_ = dest.address();
48     protocolId_ = dest.port();
49 }
50
51 prefix_ void senf::ppi::IPv4SourceForcingDgramWriter::operator()(Handle handle, Packet packet){
52     sendtoandfrom(
53             handle.fd(),
54             reinterpret_cast<void*> (&*packet.data().begin()),
55             packet.size(),
56             reinterpret_cast<in_addr*> (&destination_),
57             protocolId_,
58             reinterpret_cast<in_addr*> (&source_));
59 }
60
61 prefix_ int senf::ppi::IPv4SourceForcingDgramWriter::sendtoandfrom(
62         int sock,
63         const void *data,
64         size_t dataLen,
65         const in_addr *dst,
66         int port,
67         const in_addr *src)
68 {
69     uint8_t cbuf[CMSG_SPACE(sizeof(struct in_pktinfo))];
70     struct cmsghdr *c = (struct cmsghdr *)cbuf;
71     struct in_pktinfo *pi;
72     struct iovec iov;
73     struct msghdr h;
74
75     c->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
76     c->cmsg_level = IPPROTO_IP;
77     c->cmsg_type = IP_PKTINFO;
78
79     pi = (struct in_pktinfo *)CMSG_DATA(c);
80     pi->ipi_ifindex = 0;
81     memcpy(&pi->ipi_addr, src, 16);
82
83     iov.iov_base = (void *)data;
84     iov.iov_len = dataLen;
85
86     sockaddr_in dstpeer;
87     memset(&dstpeer, 0, sizeof(dstpeer));
88     dstpeer.sin_family = AF_INET;
89     dstpeer.sin_addr = *dst;
90     dstpeer.sin_port = htons(port);
91
92     memset(&h, 0, sizeof(h));
93     h.msg_name = (struct sockaddr *)&dstpeer;
94     h.msg_namelen = sizeof(dstpeer);
95     h.msg_iov = &iov;
96     h.msg_iovlen = 1;
97     h.msg_control = c;
98     h.msg_controllen = sizeof(cbuf);
99
100     return sendmsg(sock, &h, 0);
101 }
102
103 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter(){
104     source_ = senf::INet6Address::from_string("0::0");
105     destination_ = senf::INet6Address::from_string("0::0");
106     protocolId_ = 0;
107 }
108
109 prefix_ senf::ppi::IPv6SourceForcingDgramWriter::IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr){
110     source(sourceAddr);
111     destination(destAddr);
112 }
113
114 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::source(senf::INet6Address & source){
115     source_ = source;
116 }
117 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::destination(senf::INet6SocketAddress & dest){
118     destination_ = dest.address();
119     protocolId_ = dest.port();
120 }
121
122 prefix_ void senf::ppi::IPv6SourceForcingDgramWriter::operator()(Handle handle, Packet packet){
123     sendtoandfrom(
124             handle.fd(),
125             reinterpret_cast<void*> (&*packet.data().begin()),
126             packet.size(),
127             reinterpret_cast<in6_addr*> (&destination_),
128             protocolId_,
129             reinterpret_cast<in6_addr*> (&source_));
130 }
131
132 prefix_ int senf::ppi::IPv6SourceForcingDgramWriter::sendtoandfrom(
133         int sock,
134         const void *data,
135         size_t dataLen,
136         const in6_addr *dst,
137         int port,
138         const in6_addr *src)
139 {
140     uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
141     struct cmsghdr *c = (struct cmsghdr *)cbuf;
142     struct in6_pktinfo *pi;
143     struct iovec iov;
144     struct msghdr h;
145
146     c->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
147     c->cmsg_level = IPPROTO_IPV6;
148     c->cmsg_type = IPV6_PKTINFO;
149
150     pi = (struct in6_pktinfo *)CMSG_DATA(c);
151     pi->ipi6_ifindex = 0;
152     memcpy(&pi->ipi6_addr, src, 16);
153
154     iov.iov_base = (void *)data;
155     iov.iov_len = dataLen;
156
157     sockaddr_in6 dstpeer;
158     memset(&dstpeer, 0, sizeof(dstpeer));
159     dstpeer.sin6_family = AF_INET6;
160     dstpeer.sin6_addr = *dst;
161     dstpeer.sin6_port = htons(port);
162
163     memset(&h, 0, sizeof(h));
164     h.msg_name = (struct sockaddr *)&dstpeer;
165     h.msg_namelen = sizeof(dstpeer);
166     h.msg_iov = &iov;
167     h.msg_iovlen = 1;
168     h.msg_control = c;
169     h.msg_controllen = sizeof(cbuf);
170
171     return sendmsg(sock, &h, 0);
172 }
173
174 #undef prefix_