2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:tcpserversink
23 * @short_description: a sink that acts as a TCP server and sends data to
25 * @see_also: #multifdsink
31 #include <gst/gst-i18n-plugin.h>
33 #include <sys/ioctl.h>
35 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
36 #include <sys/filio.h>
40 #include "gsttcpserversink.h"
41 #include "gsttcp-marshal.h"
45 /* elementfactory information */
46 static GstElementDetails gst_tcp_server_sink_details =
47 GST_ELEMENT_DETAILS ("TCP Server sink",
49 "Send data as a server over the network via TCP",
50 "Thomas Vander Stichele <thomas at apestaart dot org>");
52 GST_DEBUG_CATEGORY (tcpserversink_debug);
53 #define GST_CAT_DEFAULT (tcpserversink_debug)
62 static void gst_tcp_server_sink_finalize (GObject * gobject);
64 static gboolean gst_tcp_server_sink_handle_wait (GstMultiFdSink * sink,
66 static gboolean gst_tcp_server_sink_init_send (GstMultiFdSink * this);
67 static gboolean gst_tcp_server_sink_close (GstMultiFdSink * this);
68 static void gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd);
70 static void gst_tcp_server_sink_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_tcp_server_sink_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
76 GST_BOILERPLATE (GstTCPServerSink, gst_tcp_server_sink, GstMultiFdSink,
77 GST_TYPE_MULTI_FD_SINK);
81 gst_tcp_server_sink_base_init (gpointer g_class)
83 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
85 gst_element_class_set_details (element_class, &gst_tcp_server_sink_details);
89 gst_tcp_server_sink_class_init (GstTCPServerSinkClass * klass)
91 GObjectClass *gobject_class;
92 GstMultiFdSinkClass *gstmultifdsink_class;
94 gobject_class = (GObjectClass *) klass;
95 gstmultifdsink_class = (GstMultiFdSinkClass *) klass;
97 gobject_class->set_property = gst_tcp_server_sink_set_property;
98 gobject_class->get_property = gst_tcp_server_sink_get_property;
99 gobject_class->finalize = gst_tcp_server_sink_finalize;
101 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HOST,
102 g_param_spec_string ("host", "host", "The host/IP to send the packets to",
103 TCP_DEFAULT_HOST, G_PARAM_READWRITE));
104 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
105 g_param_spec_int ("port", "port", "The port to send the packets to",
106 0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE));
108 gstmultifdsink_class->init = gst_tcp_server_sink_init_send;
109 gstmultifdsink_class->wait = gst_tcp_server_sink_handle_wait;
110 gstmultifdsink_class->close = gst_tcp_server_sink_close;
111 gstmultifdsink_class->removed = gst_tcp_server_sink_removed;
113 GST_DEBUG_CATEGORY_INIT (tcpserversink_debug, "tcpserversink", 0, "TCP sink");
117 gst_tcp_server_sink_init (GstTCPServerSink * this,
118 GstTCPServerSinkClass * klass)
120 this->server_port = TCP_DEFAULT_PORT;
121 /* should support as minimum 576 for IPV4 and 1500 for IPV6 */
122 /* this->mtu = 1500; */
123 this->host = g_strdup (TCP_DEFAULT_HOST);
125 this->server_sock.fd = -1;
129 gst_tcp_server_sink_finalize (GObject * gobject)
131 GstTCPServerSink *this = GST_TCP_SERVER_SINK (gobject);
136 /* handle a read request on the server,
137 * which indicates a new client connection */
139 gst_tcp_server_sink_handle_server_read (GstTCPServerSink * sink)
143 struct sockaddr_in client_address;
144 unsigned int client_address_len;
146 /* For some stupid reason, client_address and client_address_len has to be
148 memset (&client_address, 0, sizeof (client_address));
149 client_address_len = 0;
152 accept (sink->server_sock.fd, (struct sockaddr *) &client_address,
153 &client_address_len);
154 if (client_sock_fd == -1) {
155 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
156 ("Could not accept client on server socket %d: %s (%d)",
157 sink->server_sock.fd, g_strerror (errno), errno));
161 gst_multi_fd_sink_add (GST_MULTI_FD_SINK (sink), client_sock_fd);
163 GST_DEBUG_OBJECT (sink, "added new client ip %s with fd %d",
164 inet_ntoa (client_address.sin_addr), client_sock_fd);
170 gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd)
172 GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink);
174 GST_LOG_OBJECT (this, "closing fd %d", fd);
175 if (close (fd) < 0) {
176 GST_WARNING_OBJECT (this, "error closing fd %d: %s", fd,
182 gst_tcp_server_sink_handle_wait (GstMultiFdSink * sink, GstFDSet * set)
184 GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink);
186 if (gst_fdset_fd_can_read (set, &this->server_sock)) {
187 /* handle new client connection on server socket */
188 if (!gst_tcp_server_sink_handle_server_read (this)) {
189 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
190 ("client connection failed: %s", g_strerror (errno)));
198 gst_tcp_server_sink_set_property (GObject * object, guint prop_id,
199 const GValue * value, GParamSpec * pspec)
201 GstTCPServerSink *sink;
203 g_return_if_fail (GST_IS_TCP_SERVER_SINK (object));
204 sink = GST_TCP_SERVER_SINK (object);
208 if (!g_value_get_string (value)) {
209 g_warning ("host property cannot be NULL");
213 sink->host = g_strdup (g_value_get_string (value));
216 sink->server_port = g_value_get_int (value);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gst_tcp_server_sink_get_property (GObject * object, guint prop_id,
227 GValue * value, GParamSpec * pspec)
229 GstTCPServerSink *sink;
231 g_return_if_fail (GST_IS_TCP_SERVER_SINK (object));
232 sink = GST_TCP_SERVER_SINK (object);
236 g_value_set_string (value, sink->host);
239 g_value_set_int (value, sink->server_port);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 /* create a socket for sending to remote machine */
251 gst_tcp_server_sink_init_send (GstMultiFdSink * parent)
254 GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
256 /* create sending server socket */
257 if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
258 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
261 GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d",
262 this->server_sock.fd);
264 /* make address reusable */
266 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
267 (void *) &ret, sizeof (ret)) < 0) {
268 gst_tcp_socket_close (&this->server_sock.fd);
269 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
270 ("Could not setsockopt: %s", g_strerror (errno)));
273 /* keep connection alive; avoids SIGPIPE during write */
275 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
276 (void *) &ret, sizeof (ret)) < 0) {
277 gst_tcp_socket_close (&this->server_sock.fd);
278 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
279 ("Could not setsockopt: %s", g_strerror (errno)));
283 /* name the socket */
284 memset (&this->server_sin, 0, sizeof (this->server_sin));
285 this->server_sin.sin_family = AF_INET; /* network socket */
286 this->server_sin.sin_port = htons (this->server_port); /* on port */
287 this->server_sin.sin_addr.s_addr = htonl (INADDR_ANY); /* for hosts */
290 GST_DEBUG_OBJECT (this, "binding server socket to address");
291 ret = bind (this->server_sock.fd, (struct sockaddr *) &this->server_sin,
292 sizeof (this->server_sin));
295 gst_tcp_socket_close (&this->server_sock.fd);
298 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
299 ("bind on port %d failed: %s", this->server_port,
300 g_strerror (errno)));
306 /* set the server socket to nonblocking */
307 fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK);
309 GST_DEBUG_OBJECT (this, "listening on server socket %d with queue of %d",
310 this->server_sock.fd, TCP_BACKLOG);
311 if (listen (this->server_sock.fd, TCP_BACKLOG) == -1) {
312 gst_tcp_socket_close (&this->server_sock.fd);
313 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
314 ("Could not listen on server socket: %s", g_strerror (errno)));
317 GST_DEBUG_OBJECT (this,
318 "listened on server socket %d, returning from connection setup",
319 this->server_sock.fd);
321 gst_fdset_add_fd (parent->fdset, &this->server_sock);
322 gst_fdset_fd_ctl_read (parent->fdset, &this->server_sock, TRUE);
328 gst_tcp_server_sink_close (GstMultiFdSink * parent)
330 GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
332 if (this->server_sock.fd != -1) {
333 gst_fdset_remove_fd (parent->fdset, &this->server_sock);
335 close (this->server_sock.fd);
336 this->server_sock.fd = -1;