4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@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 public header */
26 #ifndef HH_SENF_PPI_SocketSink_
27 #define HH_SENF_PPI_SocketSink_ 1
30 #include "../Packets/Packets.hh"
31 #include "../Socket/ClientSocketHandle.hh"
32 #include "../Socket/SocketPolicy.hh"
33 #include "../Socket/ReadWritePolicy.hh"
34 #include "../Socket/FramingPolicy.hh"
35 #include "../Socket/CommunicationPolicy.hh"
37 #include "Connectors.hh"
38 #include "../Socket/Protocols/INet/INetAddressing.hh"
41 //#include "SocketSink.mpp"
42 ///////////////////////////////hh.p////////////////////////////////////////
47 /** \brief Writer for module::ActiveSocketSink / module::PassiveSocketSink
49 This writer will write the packets completely as datagrams to the given socket which must be connected.
51 class ConnectedDgramWriter
54 typedef senf::ClientSocketHandle<
55 senf::MakeSocketPolicy< senf::WriteablePolicy,
56 senf::DatagramFramingPolicy,
57 senf::ConnectedCommunicationPolicy>::policy > Handle;
58 ///< Handle type supported by this writer
59 typedef Packet PacketType;
61 void operator()(Handle handle, Packet const & packet);
62 ///< Write \a packet to \a handle
63 /**< Write the complete \a packet as a datagram to \a
65 \param[in] handle Handle to write data to
66 \param[in] packet Packet to write */
69 /** \brief Writer sending data with ClientSocketHandle::writeto()
71 This writer will send out data using ClientSocketHandle::writeto(). The target address can
72 be specified in the writer constructor and can be adjusted at any time.
74 If no target address is set, incoming data will be <em>silently dropped</em>.
76 template <class HandleType>
77 class TargetDgramWriter
80 typedef HandleType Handle;
81 typedef Packet PacketType;
83 TargetDgramWriter(); ///< Create TargetDgramWriter with unset target address
84 TargetDgramWriter(typename Handle::Address const & target);
85 ///< Create TargetDgramWriter sending to \a target
87 typename Handle::Address target() const; ///< Get current target address
88 void target(typename Handle::Address const & target); ///< Set target address
90 void operator()(Handle handle, Packet const & packet); ///< Write \a packet to \a handle
91 /**< Write the complete \a packet as a datagram to \a
93 \param[in] handle Handle to write data to
94 \param[in] packet Packet to write */
97 typename Handle::Address target_;
100 class IPv4SourceForcingDgramWriter : ConnectedDgramWriter
103 IPv4SourceForcingDgramWriter();
104 IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr);
105 typedef senf::ClientSocketHandle<
106 senf::MakeSocketPolicy< senf::WriteablePolicy,
107 senf::DatagramFramingPolicy>::policy > Handle;
108 ///< Handle type supported by this writer
109 typedef Packet PacketType;
111 void source(senf::INet4Address & source);
112 senf::INet4Address source();
113 void destination(senf::INet4SocketAddress & dest);
114 senf::INet4SocketAddress destination();
116 void operator()(Handle handle, Packet const & packet);
117 ///< Write \a packet to \a handle
118 /**< Write the complete \a packet as a datagram to \a
120 \param[in] handle Handle to write data to
121 \param[in] packet Packet to write */
123 int sendtoandfrom(int sock, const void *data, size_t dataLen, const in_addr *dst, int port, const in_addr *src);
124 senf::INet4Address source_;
125 senf::INet4Address destination_;
126 unsigned int protocolId_;
129 class IPv6SourceForcingDgramWriter : ConnectedDgramWriter
132 IPv6SourceForcingDgramWriter();
133 IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr);
134 typedef senf::ClientSocketHandle<
135 senf::MakeSocketPolicy< senf::WriteablePolicy,
136 senf::DatagramFramingPolicy>::policy > Handle;
137 ///< Handle type supported by this writer
138 typedef Packet PacketType;
140 void source(senf::INet6Address & source);
141 senf::INet6Address source();
142 void destination(senf::INet6SocketAddress & dest);
143 senf::INet6SocketAddress destination();
145 void operator()(Handle handle, Packet const & packet);
146 ///< Write \a packet to \a handle
147 /**< Write the complete \a packet as a datagram to \a
149 \param[in] handle Handle to write data to
150 \param[in] packet Packet to write */
152 int sendtoandfrom(int sock, const void *data, size_t dataLen, const in6_addr *dst, int port, const in6_addr *src);
153 senf::INet6Address source_;
154 senf::INet6Address destination_;
155 unsigned int protocolId_;
165 /** \brief Output %module writing data to a FileHandle using the provided Writer.
166 If using the default ConnectedDgramWriter the filehandle must be writable, connected and
167 able to handle complete datagrams.
169 This output %module will write data to a FileHandle object using a given \a Writer. This
170 output %module is active. This requires the file handle to be able to signal its readiness to
171 accept more data via the Scheduler.
173 The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
176 A \a Writer must fulfill the following interface:
181 typedef unspecified Handle; // type of handle requested
182 typedef unspecified_type PacketType // type of packet read
184 SomeWriter(); // EITHER default constructible OR
185 SomeWriter(SomeWriter const & other); // copy constructible
187 void operator()(Handle handle, Packet packet); // insertion function
190 Whenever a packet is received for sending, the \a Writer's \c operator() is called.
194 template <class Writer=ConnectedDgramWriter>
195 class ActiveSocketSink : public Module
197 SENF_PPI_MODULE(ActiveSocketSink);
200 typedef typename Writer::Handle Handle; ///< Handle type requested by writer
202 connector::ActiveInput<typename Writer::PacketType> input; ///< Input connector from which data is received
204 ActiveSocketSink(); ///< Create non-connected writer
205 /**< The writer will be disabled until a socket is set
206 \pre Requires \a Writer to be default constructible */
207 explicit ActiveSocketSink(Writer const & writer);
208 ///< Create non-connected writer
209 /**< The writer will be disabled until a socket is set
210 \pre Requires \a Writer to be copy constructible
211 \param[in] writer Writer helper writing packet date to
213 explicit ActiveSocketSink(Handle handle); ///< Create new writer for the given handle
214 /**< Data will be written to \a handle using \a Writer.
215 \pre Requires \a Writer to be default constructible
216 \param[in] handle Handle to write data to */
217 ActiveSocketSink(Handle handle, Writer const & writer);
218 ///< Create new writer for the given handle
219 /**< Data will be written to \a handle using \a Writer.
220 \pre Requires \a Writer to be copy constructible
221 \param[in] handle Handle to write data to
222 \param[in] writer Writer helper writing packet date to
225 Writer & writer(); ///< Access the Writer
226 Handle handle(); ///< Access handle
227 void handle(Handle handle); ///< Set handle
228 /**< Assigning an empty or in-valid() handle will disable
229 the module until a new. valid handle is assigned. */
239 /** \brief Output module writing data to a FileHandle using the provided \a Writer.
240 If using the default ConnectedDgramWriter the filehandle must be writable, connected and
241 able to handle complete datagrams.
243 This output module will write data to a FileHandle object using a given \a Writer. This
244 output module is passive. This implies, that the output handle may not block. This also
245 implies, that data will probably get lost if written to fast for the underlying transport
246 mechanism. Either this is desired (like for a UDP socket) or some additional bandwidth
247 shaping needs to be used.
249 The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
252 The \a Writer must fulfill the following interface:
257 typedef unspecified Handle; // type of handle requested
258 typedef unspecified_type PacketType // type of packet read
260 SomeWriter(); // EITHER default constructible
261 SomeWriter(SomeWriter const & other); // OR copy constructible
263 void operator()(Handle handle, Packet packet); // insertion function
266 Whenever a packet is received for sending, the \a Writer's \c operator() is called.
270 template <class Writer=ConnectedDgramWriter>
271 class PassiveSocketSink : public Module
273 SENF_PPI_MODULE(PassiveSocketSink);
276 typedef typename Writer::Handle Handle; ///< Handle type requested by writer
278 connector::PassiveInput<typename Writer::PacketType> input; ///< Input connector from which data is received
280 PassiveSocketSink(); ///< Create non-connected writer
281 /**< The writer will be disabled until a socket is set
282 \pre Requires \a Writer to be default constructible */
283 explicit PassiveSocketSink(Writer const & writer);
284 ///< Create non-connected writer
285 /**< The writer will be disabled until a socket is set
286 \pre Requires \a Writer to be copy constructible
287 \param[in] writer Writer helper writing packet date to
289 explicit PassiveSocketSink(Handle handle); ///< Create new writer for the given handle
290 /**< Data will be written to \a handle using \a Writer.
291 \pre Requires \a Writer to be default constructible
292 \param[in] handle Handle to write data to */
293 PassiveSocketSink(Handle handle, Writer const & writer);
294 ///< Create new writer for the given handle
295 /**< Data will be written to \a handle using \a Writer.
296 \pre Requires \a Writer to be copy constructible
297 \param[in] handle Handle to write data to
298 \param[in] writer Writer helper writing packet date to
301 Writer & writer(); ///< Access the Writer
302 Handle & handle(); ///< Access handle
303 void handle(Handle handle); ///< Set handle
304 /**< Assigning an empty or in-valid() handle will disable
305 the module until a new. valid handle is assigned. */
308 void replaceHandle(Handle newHandle);
313 void checkThrottle();
322 ///////////////////////////////hh.e////////////////////////////////////////
323 #include "SocketSink.cci"
324 #include "SocketSink.ct"
325 #include "SocketSink.cti"
332 // c-file-style: "senf"
333 // indent-tabs-mode: nil
334 // ispell-local-dictionary: "american"
335 // compile-command: "scons -u test"
336 // comment-column: 40