SocketSink.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 // Custom includes
18 #include "SocketSink.hh"
20 
21 #define prefix_
22 
24 {
25  source_ = senf::INet4Address::from_string("0.0.0.0");
26  destination_ = senf::INet4Address::from_string("0.0.0.0");
27  protocolId_ = 0;
28 }
29 
32 {
33  source(sourceAddr);
34  destination(destAddr);
35 }
36 
38 {
39  source_ = source;
40 }
41 
43 {
44  destination_ = dest.address();
45  protocolId_ = dest.port();
46 }
47 
49  Packet const & packet)
50 {
51  return sendtoandfrom(
52  handle.fd(),
53  reinterpret_cast<void*> (&*packet.data().begin()),
54  packet.size(),
55  reinterpret_cast<in_addr*> (&destination_),
56  protocolId_,
57  reinterpret_cast<in_addr*> (&source_)) > 0;
58 }
59 
60 prefix_ int senf::ppi::IPv4SourceForcingDgramWriter::sendtoandfrom(
61  int sock,
62  const void *data,
63  size_t dataLen,
64  const in_addr *dst,
65  int port,
66  const in_addr *src)
67 {
68  uint8_t cbuf[CMSG_SPACE(sizeof(struct in_pktinfo))];
69  struct cmsghdr *c = reinterpret_cast<cmsghdr *>(cbuf);
70  struct in_pktinfo *pi;
71  struct iovec iov;
72  struct msghdr h;
73 
74  c->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
75  c->cmsg_level = IPPROTO_IP;
76  c->cmsg_type = IP_PKTINFO;
77 
78  pi = reinterpret_cast<in_pktinfo *>(CMSG_DATA(c));
79  pi->ipi_ifindex = 0;
80  memcpy(&pi->ipi_addr, &src->s_addr, sizeof(src->s_addr));
81 
82  iov.iov_base = const_cast<void *>(data);
83  iov.iov_len = dataLen;
84 
85  sockaddr_in dstpeer;
86  memset(&dstpeer, 0, sizeof(dstpeer));
87  dstpeer.sin_family = AF_INET;
88  dstpeer.sin_addr = *dst;
89  dstpeer.sin_port = htons(port);
90 
91  memset(&h, 0, sizeof(h));
92  h.msg_name = (struct sockaddr *)&dstpeer;
93  h.msg_namelen = sizeof(dstpeer);
94  h.msg_iov = &iov;
95  h.msg_iovlen = 1;
96  h.msg_control = c;
97  h.msg_controllen = sizeof(cbuf);
98 
99  return sendmsg(sock, &h, 0);
100 }
101 
103 {
104  source_ = senf::INet6Address::from_string("0::0");
105  destination_ = senf::INet6Address::from_string("0::0");
106  protocolId_ = 0;
107 }
108 
111 {
112  source(sourceAddr);
113  destination(destAddr);
114 }
115 
117 {
118  source_ = source;
119 }
120 
122 {
123  destination_ = dest.address();
124  protocolId_ = dest.port();
125 }
126 
128  Packet const & packet)
129 {
130  return sendtoandfrom(
131  handle.fd(),
132  reinterpret_cast<void*> (&*packet.data().begin()),
133  packet.size(),
134  reinterpret_cast<in6_addr*> (&destination_),
135  protocolId_,
136  reinterpret_cast<in6_addr*> (&source_)) > 0;
137 }
138 
139 prefix_ int senf::ppi::IPv6SourceForcingDgramWriter::sendtoandfrom(
140  int sock,
141  const void *data,
142  size_t dataLen,
143  const in6_addr *dst,
144  int port,
145  const in6_addr *src)
146 {
147  uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
148  struct cmsghdr *c = reinterpret_cast<cmsghdr *>(cbuf);
149  struct in6_pktinfo *pi;
150  struct iovec iov;
151  struct msghdr h;
152 
153  c->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
154  c->cmsg_level = IPPROTO_IPV6;
155  c->cmsg_type = IPV6_PKTINFO;
156 
157  pi = reinterpret_cast<in6_pktinfo *>(CMSG_DATA(c));
158  pi->ipi6_ifindex = 0;
159  memcpy(&pi->ipi6_addr, src, 16);
160 
161  iov.iov_base = const_cast<void *>(data);
162  iov.iov_len = dataLen;
163 
164  sockaddr_in6 dstpeer;
165  memset(&dstpeer, 0, sizeof(dstpeer));
166  dstpeer.sin6_family = AF_INET6;
167  dstpeer.sin6_addr = *dst;
168  dstpeer.sin6_port = htons(port);
169 
170  memset(&h, 0, sizeof(h));
171  h.msg_name = (struct sockaddr *)&dstpeer;
172  h.msg_namelen = sizeof(dstpeer);
173  h.msg_iov = &iov;
174  h.msg_iovlen = 1;
175  h.msg_control = c;
176  h.msg_controllen = sizeof(cbuf);
177 
178  return sendmsg(sock, &h, 0);
179 }
180 
181 
183 {
184  ::bind(handle.fd(),
185  const_cast<sockaddr *>(source.sockaddr_p()),
186  source.socklen());
187 }
188 
190 {
191  target_ = target;
192 }
193 
195 {}
196 
197 #undef prefix_
bool operator()(Handle &handle, Packet const &packet)
Write packet to handle.
Definition: SocketSink.cc:127
senf::PacketSocketHandle Handle
Definition: SocketSink.hh:156
static INet6Address from_string(std::string const &s, Resolve_t resolve=ResolveINet6)
u8 data[SPECTRAL_HT20_NUM_BINS]
iterator begin() const
PacketData & data() const
unsigned port() const
bool operator()(Handle &handle, Packet const &packet)
Write packet to handle.
Definition: SocketSink.cc:48
struct sockaddr * sockaddr_p()
int fd() const
senf::INet6SocketAddress destination()
void socklen(socklen_t len)
SocketSink public header.
static INet4Address from_string(std::string const &s)
#define prefix_
Definition: SocketSink.cc:21
void target(LLSocketAddress const &target)
Definition: SocketSink.cc:189
size_type size() const
senf::INet4SocketAddress destination()
INet6Address address() const
unsigned port() const
INet4Address address() const
void source(Handle &, LLSocketAddress source)
Definition: SocketSink.cc:182