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:element-tcpserversink
23 * @see_also: #multifdsink
26 * <title>Example launch line</title>
29 * gst-launch fdsrc fd=1 ! tcpserversink protocol=none port=3000
31 * gst-launch tcpclientsrc protocol=none port=3000 ! fdsink fd=2
39 #include <gst/gst-i18n-plugin.h>
40 #include <string.h> /* memset */
42 #include <sys/ioctl.h>
44 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
45 #include <sys/filio.h>
49 #include "gsttcpserversink.h"
50 #include "gsttcp-marshal.h"
54 GST_DEBUG_CATEGORY_STATIC (tcpserversink_debug);
55 #define GST_CAT_DEFAULT (tcpserversink_debug)
64 static void gst_tcp_server_sink_finalize (GObject * gobject);
66 static gboolean gst_tcp_server_sink_handle_wait (GstMultiFdSink * sink,
68 static gboolean gst_tcp_server_sink_init_send (GstMultiFdSink * this);
69 static gboolean gst_tcp_server_sink_close (GstMultiFdSink * this);
70 static void gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd);
72 static void gst_tcp_server_sink_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_tcp_server_sink_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * pspec);
78 GST_BOILERPLATE (GstTCPServerSink, gst_tcp_server_sink, GstMultiFdSink,
79 GST_TYPE_MULTI_FD_SINK);
83 gst_tcp_server_sink_base_init (gpointer g_class)
85 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
87 gst_element_class_set_details_simple (element_class,
88 "TCP server sink", "Sink/Network",
89 "Send data as a server over the network via TCP",
90 "Thomas Vander Stichele <thomas at apestaart dot org>");
94 gst_tcp_server_sink_class_init (GstTCPServerSinkClass * klass)
96 GObjectClass *gobject_class;
97 GstMultiFdSinkClass *gstmultifdsink_class;
99 gobject_class = (GObjectClass *) klass;
100 gstmultifdsink_class = (GstMultiFdSinkClass *) klass;
102 gobject_class->set_property = gst_tcp_server_sink_set_property;
103 gobject_class->get_property = gst_tcp_server_sink_get_property;
104 gobject_class->finalize = gst_tcp_server_sink_finalize;
106 g_object_class_install_property (gobject_class, ARG_HOST,
107 g_param_spec_string ("host", "host", "The host/IP to send the packets to",
108 TCP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
109 g_object_class_install_property (gobject_class, ARG_PORT,
110 g_param_spec_int ("port", "port", "The port to send the packets to",
111 0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT,
112 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114 gstmultifdsink_class->init = gst_tcp_server_sink_init_send;
115 gstmultifdsink_class->wait = gst_tcp_server_sink_handle_wait;
116 gstmultifdsink_class->close = gst_tcp_server_sink_close;
117 gstmultifdsink_class->removed = gst_tcp_server_sink_removed;
119 GST_DEBUG_CATEGORY_INIT (tcpserversink_debug, "tcpserversink", 0, "TCP sink");
123 gst_tcp_server_sink_init (GstTCPServerSink * this,
124 GstTCPServerSinkClass * klass)
126 this->server_port = TCP_DEFAULT_PORT;
127 /* should support as minimum 576 for IPV4 and 1500 for IPV6 */
128 /* this->mtu = 1500; */
129 this->host = g_strdup (TCP_DEFAULT_HOST);
131 this->server_sock.fd = -1;
135 gst_tcp_server_sink_finalize (GObject * gobject)
137 GstTCPServerSink *this = GST_TCP_SERVER_SINK (gobject);
141 G_OBJECT_CLASS (parent_class)->finalize (gobject);
144 /* handle a read request on the server,
145 * which indicates a new client connection */
147 gst_tcp_server_sink_handle_server_read (GstTCPServerSink * sink)
151 struct sockaddr_in client_address;
152 socklen_t client_address_len;
154 /* For some stupid reason, client_address and client_address_len has to be
156 memset (&client_address, 0, sizeof (client_address));
157 client_address_len = 0;
160 accept (sink->server_sock.fd, (struct sockaddr *) &client_address,
161 &client_address_len);
162 if (client_sock_fd == -1)
165 gst_multi_fd_sink_add (GST_MULTI_FD_SINK (sink), client_sock_fd);
167 GST_DEBUG_OBJECT (sink, "added new client ip %s with fd %d",
168 inet_ntoa (client_address.sin_addr), client_sock_fd);
175 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
176 ("Could not accept client on server socket %d: %s (%d)",
177 sink->server_sock.fd, g_strerror (errno), errno));
183 gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd)
185 #ifndef GST_DISABLE_GST_DEBUG
186 GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink);
189 GST_LOG_OBJECT (this, "closing fd %d", fd);
190 if (close (fd) < 0) {
191 GST_WARNING_OBJECT (this, "error closing fd %d: %s", fd,
197 gst_tcp_server_sink_handle_wait (GstMultiFdSink * sink, GstPoll * set)
199 GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink);
201 if (gst_poll_fd_can_read (set, &this->server_sock)) {
202 /* handle new client connection on server socket */
203 if (!gst_tcp_server_sink_handle_server_read (this))
204 goto connection_failed;
211 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
212 ("client connection failed: %s", g_strerror (errno)));
218 gst_tcp_server_sink_set_property (GObject * object, guint prop_id,
219 const GValue * value, GParamSpec * pspec)
221 GstTCPServerSink *sink;
223 g_return_if_fail (GST_IS_TCP_SERVER_SINK (object));
224 sink = GST_TCP_SERVER_SINK (object);
228 if (!g_value_get_string (value)) {
229 g_warning ("host property cannot be NULL");
233 sink->host = g_strdup (g_value_get_string (value));
236 sink->server_port = g_value_get_int (value);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 gst_tcp_server_sink_get_property (GObject * object, guint prop_id,
247 GValue * value, GParamSpec * pspec)
249 GstTCPServerSink *sink;
251 g_return_if_fail (GST_IS_TCP_SERVER_SINK (object));
252 sink = GST_TCP_SERVER_SINK (object);
256 g_value_set_string (value, sink->host);
259 g_value_set_int (value, sink->server_port);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 /* create a socket for sending to remote machine */
271 gst_tcp_server_sink_init_send (GstMultiFdSink * parent)
274 GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
276 /* create sending server socket */
277 if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
280 GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d",
281 this->server_sock.fd);
283 /* make address reusable */
285 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
286 (void *) &ret, sizeof (ret)) < 0)
289 /* keep connection alive; avoids SIGPIPE during write */
291 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
292 (void *) &ret, sizeof (ret)) < 0)
293 goto keepalive_failed;
295 /* name the socket */
296 memset (&this->server_sin, 0, sizeof (this->server_sin));
297 this->server_sin.sin_family = AF_INET; /* network socket */
298 this->server_sin.sin_port = htons (this->server_port); /* on port */
299 this->server_sin.sin_addr.s_addr = htonl (INADDR_ANY); /* for hosts */
302 GST_DEBUG_OBJECT (this, "binding server socket to address");
303 ret = bind (this->server_sock.fd, (struct sockaddr *) &this->server_sin,
304 sizeof (this->server_sin));
308 /* set the server socket to nonblocking */
309 fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK);
311 GST_DEBUG_OBJECT (this, "listening on server socket %d with queue of %d",
312 this->server_sock.fd, TCP_BACKLOG);
313 if (listen (this->server_sock.fd, TCP_BACKLOG) == -1)
316 GST_DEBUG_OBJECT (this,
317 "listened on server socket %d, returning from connection setup",
318 this->server_sock.fd);
320 gst_poll_add_fd (parent->fdset, &this->server_sock);
321 gst_poll_fd_ctl_read (parent->fdset, &this->server_sock, TRUE);
328 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
333 gst_tcp_socket_close (&this->server_sock);
334 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
335 ("Could not setsockopt: %s", g_strerror (errno)));
340 gst_tcp_socket_close (&this->server_sock);
341 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
342 ("Could not setsockopt: %s", g_strerror (errno)));
347 gst_tcp_socket_close (&this->server_sock);
348 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
349 ("Could not listen on server socket: %s", g_strerror (errno)));
354 gst_tcp_socket_close (&this->server_sock);
357 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
358 ("bind on port %d failed: %s", this->server_port,
359 g_strerror (errno)));
367 gst_tcp_server_sink_close (GstMultiFdSink * parent)
369 GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
371 if (this->server_sock.fd != -1) {
372 gst_poll_remove_fd (parent->fdset, &this->server_sock);
374 close (this->server_sock.fd);
375 this->server_sock.fd = -1;