2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
5 * gsttcp.c: TCP functions
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
35 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
36 #include <sys/filio.h>
40 #include <gst/gst-i18n-plugin.h>
42 GST_DEBUG_CATEGORY_EXTERN (tcp_debug);
43 #define GST_CAT_DEFAULT tcp_debug
46 #define MSG_NOSIGNAL 0
49 /* resolve host to IP address, throwing errors if it fails */
50 /* host can already be an IP address */
51 /* returns a newly allocated gchar * with the dotted ip address,
52 or NULL, in which case it already fired an error. */
54 gst_tcp_host_to_ip (GstElement * element, const gchar * host)
56 struct hostent *hostinfo;
61 GST_DEBUG_OBJECT (element, "resolving host %s", host);
63 /* first check if it already is an IP address */
64 if (inet_aton (host, &addr)) {
68 /* FIXME: could do a localhost check here */
70 /* perform a name lookup */
71 if (!(hostinfo = gethostbyname (host)))
74 if (hostinfo->h_addrtype != AF_INET)
77 addrs = hostinfo->h_addr_list;
79 /* There could be more than one IP address, but we just return the first */
80 ip = g_strdup (inet_ntoa (*(struct in_addr *) *addrs));
83 GST_DEBUG_OBJECT (element, "resolved to IP %s", ip);
88 GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, (NULL),
89 ("Could not find IP address for host \"%s\".", host));
94 GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, (NULL),
95 ("host \"%s\" is not an IP host", host));
100 /* write buffer to given socket incrementally.
101 * Returns number of bytes written.
104 gst_tcp_socket_write (int socket, const void *buf, size_t count)
106 size_t bytes_written = 0;
108 while (bytes_written < count) {
109 ssize_t wrote = send (socket, (const char *) buf + bytes_written,
110 count - bytes_written, MSG_NOSIGNAL);
113 GST_WARNING ("error while writing");
114 return bytes_written;
116 bytes_written += wrote;
119 GST_LOG ("wrote %" G_GSIZE_FORMAT " bytes succesfully", bytes_written);
120 return bytes_written;
123 /* atomically read count bytes into buf, cancellable. return val of GST_FLOW_OK
124 * indicates success, anything else is failure.
127 gst_tcp_socket_read (GstElement * this, int socket, void *buf, size_t count,
137 while (bytes_read < count) {
138 /* do a blocking select on the socket */
139 /* no action (0) is an error too in our case */
140 if ((ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE)) <= 0) {
141 if (ret == -1 && errno == EBUSY)
147 /* ask how much is available for reading on the socket */
148 if (ioctl (socket, FIONREAD, &num_to_read) < 0)
151 if (num_to_read == 0)
154 /* sizeof(ssize_t) >= sizeof(int), so I know num_to_read <= SSIZE_MAX */
156 num_to_read = MIN (num_to_read, count - bytes_read);
158 n = read (socket, ((guint8 *) buf) + bytes_read, num_to_read);
166 bytes_read += num_to_read;
174 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
175 ("select failed: %s", g_strerror (errno)));
176 return GST_FLOW_ERROR;
180 GST_DEBUG_OBJECT (this, "Select was cancelled");
181 return GST_FLOW_WRONG_STATE;
185 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
186 ("ioctl failed: %s", g_strerror (errno)));
187 return GST_FLOW_ERROR;
191 GST_DEBUG_OBJECT (this, "Got EOS on socket stream");
192 return GST_FLOW_UNEXPECTED;
196 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
197 ("read failed: %s", g_strerror (errno)));
198 return GST_FLOW_ERROR;
202 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
203 ("short read: wanted %d bytes, got %" G_GSSIZE_FORMAT, num_to_read, n));
204 return GST_FLOW_ERROR;
208 /* close the socket and reset the fd. Used to clean up after errors. */
210 gst_tcp_socket_close (GstPollFD * socket)
212 if (socket->fd >= 0) {
218 /* read a buffer from the given socket
220 * - a GstBuffer in which data should be read
221 * - NULL, indicating a connection close or an error, to be handled with
225 gst_tcp_read_buffer (GstElement * this, int socket, GstPoll * fdset,
235 /* do a blocking select on the socket */
236 /* no action (0) is an error too in our case */
237 if ((ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE)) <= 0) {
238 if (ret == -1 && errno == EBUSY)
244 /* ask how much is available for reading on the socket */
245 if (ioctl (socket, FIONREAD, &readsize) < 0)
251 /* sizeof(ssize_t) >= sizeof(int), so I know readsize <= SSIZE_MAX */
253 *buf = gst_buffer_new_and_alloc (readsize);
255 data = gst_buffer_map (*buf, NULL, NULL, GST_MAP_WRITE);
256 bytes_read = read (socket, data, readsize);
257 gst_buffer_unmap (*buf, data, bytes_read);
262 if (bytes_read < readsize)
263 /* but mom, you promised to give me readsize bytes! */
266 GST_LOG_OBJECT (this, "returning buffer of size %d", bytes_read);
272 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
273 ("select failed: %s", g_strerror (errno)));
274 return GST_FLOW_ERROR;
278 GST_DEBUG_OBJECT (this, "Select was cancelled");
279 return GST_FLOW_WRONG_STATE;
283 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
284 ("ioctl failed: %s", g_strerror (errno)));
285 return GST_FLOW_ERROR;
289 GST_DEBUG_OBJECT (this, "Got EOS on socket stream");
290 return GST_FLOW_UNEXPECTED;
294 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
295 ("read failed: %s", g_strerror (errno)));
296 gst_buffer_unref (*buf);
298 return GST_FLOW_ERROR;
302 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
303 ("short read: wanted %d bytes, got %" G_GSSIZE_FORMAT, readsize,
305 gst_buffer_unref (*buf);
307 return GST_FLOW_ERROR;
311 /* read a buffer from the given socket
313 * - a GstBuffer in which data should be read
314 * - NULL, indicating a connection close or an error, to be handled with
318 gst_tcp_gdp_read_buffer (GstElement * this, int socket, GstPoll * fdset,
322 guint8 *header = NULL;
326 GST_LOG_OBJECT (this, "Reading %d bytes for buffer packet header",
327 GST_DP_HEADER_LENGTH);
330 header = g_malloc (GST_DP_HEADER_LENGTH);
332 ret = gst_tcp_socket_read (this, socket, header, GST_DP_HEADER_LENGTH, fdset);
334 if (ret != GST_FLOW_OK)
335 goto header_read_error;
337 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
340 if (gst_dp_header_payload_type (header) != GST_DP_PAYLOAD_BUFFER)
343 GST_LOG_OBJECT (this, "validated buffer packet header");
345 *buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, header);
349 data = gst_buffer_map (*buf, &size, NULL, GST_MAP_WRITE);
350 ret = gst_tcp_socket_read (this, socket, data, size, fdset);
351 gst_buffer_unmap (*buf, data, size);
353 if (ret != GST_FLOW_OK)
354 goto data_read_error;
366 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
367 ("GDP buffer packet header does not validate"));
369 return GST_FLOW_ERROR;
373 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
374 ("GDP packet contains something that is not a buffer (type %d)",
375 gst_dp_header_payload_type (header)));
377 return GST_FLOW_ERROR;
381 gst_buffer_unref (*buf);
388 gst_tcp_gdp_read_caps (GstElement * this, int socket, GstPoll * fdset,
392 guint8 *header = NULL;
393 guint8 *payload = NULL;
394 size_t payload_length;
396 GST_LOG_OBJECT (this, "Reading %d bytes for caps packet header",
397 GST_DP_HEADER_LENGTH);
400 header = g_malloc (GST_DP_HEADER_LENGTH);
402 ret = gst_tcp_socket_read (this, socket, header, GST_DP_HEADER_LENGTH, fdset);
404 if (ret != GST_FLOW_OK)
405 goto header_read_error;
407 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
408 goto header_validate_error;
410 if (gst_dp_header_payload_type (header) != GST_DP_PAYLOAD_CAPS)
413 GST_LOG_OBJECT (this, "validated caps packet header");
415 payload_length = gst_dp_header_payload_length (header);
416 payload = g_malloc (payload_length);
418 GST_LOG_OBJECT (this,
419 "Reading %" G_GSIZE_FORMAT " bytes for caps packet payload",
422 ret = gst_tcp_socket_read (this, socket, payload, payload_length, fdset);
424 if (ret != GST_FLOW_OK)
425 goto payload_read_error;
427 if (!gst_dp_validate_payload (GST_DP_HEADER_LENGTH, header, payload))
428 goto payload_validate_error;
430 *caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, header, payload);
432 GST_DEBUG_OBJECT (this, "Got caps over GDP: %" GST_PTR_FORMAT, *caps);
445 header_validate_error:
447 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
448 ("GDP caps packet header does not validate"));
450 return GST_FLOW_ERROR;
454 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
455 ("GDP packet contains something that is not a caps (type %d)",
456 gst_dp_header_payload_type (header)));
458 return GST_FLOW_ERROR;
466 payload_validate_error:
468 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
469 ("GDP caps packet payload does not validate"));
472 return GST_FLOW_ERROR;