summaryrefslogtreecommitdiff
path: root/gst/tcp/gsttcp.c
blob: 9bf2666d79360b350494600a5915f2e68810ab34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
 *
 * gsttcp.c: TCP functions
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/ioctl.h>

#ifdef HAVE_FIONREAD_IN_SYS_FILIO
#include <sys/filio.h>
#endif

#include "gsttcp.h"
#include <gst/gst-i18n-plugin.h>

GST_DEBUG_CATEGORY_EXTERN (tcp_debug);
#define GST_CAT_DEFAULT tcp_debug

#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif

/* resolve host to IP address, throwing errors if it fails */
/* host can already be an IP address */
/* returns a newly allocated gchar * with the dotted ip address,
   or NULL, in which case it already fired an error. */
gchar *
gst_tcp_host_to_ip (GstElement * element, const gchar * host)
{
  struct hostent *hostinfo;
  char **addrs;
  gchar *ip;
  struct in_addr addr;

  GST_DEBUG_OBJECT (element, "resolving host %s", host);

  /* first check if it already is an IP address */
  if (inet_aton (host, &addr)) {
    ip = g_strdup (host);
    goto beach;
  }
  /* FIXME: could do a localhost check here */

  /* perform a name lookup */
  if (!(hostinfo = gethostbyname (host)))
    goto resolve_error;

  if (hostinfo->h_addrtype != AF_INET)
    goto not_ip;

  addrs = hostinfo->h_addr_list;

  /* There could be more than one IP address, but we just return the first */
  ip = g_strdup (inet_ntoa (*(struct in_addr *) *addrs));

beach:
  GST_DEBUG_OBJECT (element, "resolved to IP %s", ip);
  return ip;

resolve_error:
  {
    GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, (NULL),
        ("Could not find IP address for host \"%s\".", host));
    return NULL;
  }
not_ip:
  {
    GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, (NULL),
        ("host \"%s\" is not an IP host", host));
    return NULL;
  }
}

/* write buffer to given socket incrementally.
 * Returns number of bytes written.
 */
gint
gst_tcp_socket_write (int socket, const void *buf, size_t count)
{
  size_t bytes_written = 0;

  while (bytes_written < count) {
    ssize_t wrote = send (socket, (const char *) buf + bytes_written,
        count - bytes_written, MSG_NOSIGNAL);

    if (wrote <= 0) {
      GST_WARNING ("error while writing");
      return bytes_written;
    }
    bytes_written += wrote;
  }

  GST_LOG ("wrote %" G_GSIZE_FORMAT " bytes succesfully", bytes_written);
  return bytes_written;
}

/* atomically read count bytes into buf, cancellable. return val of GST_FLOW_OK
 * indicates success, anything else is failure.
 */
static GstFlowReturn
gst_tcp_socket_read (GstElement * this, int socket, void *buf, size_t count,
    GstPoll * fdset)
{
  ssize_t n;
  size_t bytes_read;
  int num_to_read;
  int ret;

  bytes_read = 0;

  while (bytes_read < count) {
    /* do a blocking select on the socket */
    /* no action (0) is an error too in our case */
    if ((ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE)) <= 0) {
      if (ret == -1 && errno == EBUSY)
        goto cancelled;
      else
        goto select_error;
    }

    /* ask how much is available for reading on the socket */
    if (ioctl (socket, FIONREAD, &num_to_read) < 0)
      goto ioctl_error;

    if (num_to_read == 0)
      goto got_eos;

    /* sizeof(ssize_t) >= sizeof(int), so I know num_to_read <= SSIZE_MAX */

    num_to_read = MIN (num_to_read, count - bytes_read);

    n = read (socket, ((guint8 *) buf) + bytes_read, num_to_read);

    if (n < 0)
      goto read_error;

    if (n < num_to_read)
      goto short_read;

    bytes_read += num_to_read;
  }

  return GST_FLOW_OK;

  /* ERRORS */
select_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("select failed: %s", g_strerror (errno)));
    return GST_FLOW_ERROR;
  }
cancelled:
  {
    GST_DEBUG_OBJECT (this, "Select was cancelled");
    return GST_FLOW_WRONG_STATE;
  }
ioctl_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("ioctl failed: %s", g_strerror (errno)));
    return GST_FLOW_ERROR;
  }
got_eos:
  {
    GST_DEBUG_OBJECT (this, "Got EOS on socket stream");
    return GST_FLOW_UNEXPECTED;
  }
read_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("read failed: %s", g_strerror (errno)));
    return GST_FLOW_ERROR;
  }
short_read:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("short read: wanted %d bytes, got %" G_GSSIZE_FORMAT, num_to_read, n));
    return GST_FLOW_ERROR;
  }
}

/* close the socket and reset the fd.  Used to clean up after errors. */
void
gst_tcp_socket_close (GstPollFD * socket)
{
  if (socket->fd >= 0) {
    close (socket->fd);
    socket->fd = -1;
  }
}

/* read a buffer from the given socket
 * returns:
 * - a GstBuffer in which data should be read
 * - NULL, indicating a connection close or an error, to be handled with
 *         EOS
 */
GstFlowReturn
gst_tcp_read_buffer (GstElement * this, int socket, GstPoll * fdset,
    GstBuffer ** buf)
{
  int ret;
  ssize_t bytes_read;
  int readsize;

  *buf = NULL;

  /* do a blocking select on the socket */
  /* no action (0) is an error too in our case */
  if ((ret = gst_poll_wait (fdset, GST_CLOCK_TIME_NONE)) <= 0) {
    if (ret == -1 && errno == EBUSY)
      goto cancelled;
    else
      goto select_error;
  }

  /* ask how much is available for reading on the socket */
  if ((ret = ioctl (socket, FIONREAD, &readsize)) < 0)
    goto ioctl_error;

  if (readsize == 0)
    goto got_eos;

  /* sizeof(ssize_t) >= sizeof(int), so I know readsize <= SSIZE_MAX */

  *buf = gst_buffer_new_and_alloc (readsize);

  bytes_read = read (socket, GST_BUFFER_DATA (*buf), readsize);

  if (bytes_read < 0)
    goto read_error;

  if (bytes_read < readsize)
    /* but mom, you promised to give me readsize bytes! */
    goto short_read;

  GST_LOG_OBJECT (this, "returning buffer of size %d", GST_BUFFER_SIZE (*buf));
  return GST_FLOW_OK;

  /* ERRORS */
select_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("select failed: %s", g_strerror (errno)));
    return GST_FLOW_ERROR;
  }
cancelled:
  {
    GST_DEBUG_OBJECT (this, "Select was cancelled");
    return GST_FLOW_WRONG_STATE;
  }
ioctl_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("ioctl failed: %s", g_strerror (errno)));
    return GST_FLOW_ERROR;
  }
got_eos:
  {
    GST_DEBUG_OBJECT (this, "Got EOS on socket stream");
    return GST_FLOW_WRONG_STATE;
  }
read_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("read failed: %s", g_strerror (errno)));
    gst_buffer_unref (*buf);
    *buf = NULL;
    return GST_FLOW_ERROR;
  }
short_read:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("short read: wanted %d bytes, got %" G_GSSIZE_FORMAT, readsize,
            bytes_read));
    gst_buffer_unref (*buf);
    *buf = NULL;
    return GST_FLOW_ERROR;
  }
}

/* read a buffer from the given socket
 * returns:
 * - a GstBuffer in which data should be read
 * - NULL, indicating a connection close or an error, to be handled with
 *         EOS
 */
GstFlowReturn
gst_tcp_gdp_read_buffer (GstElement * this, int socket, GstPoll * fdset,
    GstBuffer ** buf)
{
  GstFlowReturn ret;
  guint8 *header = NULL;

  GST_LOG_OBJECT (this, "Reading %d bytes for buffer packet header",
      GST_DP_HEADER_LENGTH);

  *buf = NULL;
  header = g_malloc (GST_DP_HEADER_LENGTH);

  ret = gst_tcp_socket_read (this, socket, header, GST_DP_HEADER_LENGTH, fdset);

  if (ret != GST_FLOW_OK)
    goto header_read_error;

  if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
    goto validate_error;

  if (gst_dp_header_payload_type (header) != GST_DP_PAYLOAD_BUFFER)
    goto is_not_buffer;

  GST_LOG_OBJECT (this, "validated buffer packet header");

  *buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, header);

  g_free (header);

  ret = gst_tcp_socket_read (this, socket, GST_BUFFER_DATA (*buf),
      GST_BUFFER_SIZE (*buf), fdset);

  if (ret != GST_FLOW_OK)
    goto data_read_error;

  return GST_FLOW_OK;

  /* ERRORS */
header_read_error:
  {
    g_free (header);
    return ret;
  }
validate_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("GDP buffer packet header does not validate"));
    g_free (header);
    return GST_FLOW_ERROR;
  }
is_not_buffer:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("GDP packet contains something that is not a buffer (type %d)",
            gst_dp_header_payload_type (header)));
    g_free (header);
    return GST_FLOW_ERROR;
  }
data_read_error:
  {
    gst_buffer_unref (*buf);
    *buf = NULL;
    return ret;
  }
}

GstFlowReturn
gst_tcp_gdp_read_caps (GstElement * this, int socket, GstPoll * fdset,
    GstCaps ** caps)
{
  GstFlowReturn ret;
  guint8 *header = NULL;
  guint8 *payload = NULL;
  size_t payload_length;

  GST_LOG_OBJECT (this, "Reading %d bytes for caps packet header",
      GST_DP_HEADER_LENGTH);

  *caps = NULL;
  header = g_malloc (GST_DP_HEADER_LENGTH);

  ret = gst_tcp_socket_read (this, socket, header, GST_DP_HEADER_LENGTH, fdset);

  if (ret != GST_FLOW_OK)
    goto header_read_error;

  if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
    goto header_validate_error;

  if (gst_dp_header_payload_type (header) != GST_DP_PAYLOAD_CAPS)
    goto is_not_caps;

  GST_LOG_OBJECT (this, "validated caps packet header");

  payload_length = gst_dp_header_payload_length (header);
  payload = g_malloc (payload_length);

  GST_LOG_OBJECT (this,
      "Reading %" G_GSIZE_FORMAT " bytes for caps packet payload",
      payload_length);

  ret = gst_tcp_socket_read (this, socket, payload, payload_length, fdset);

  if (ret != GST_FLOW_OK)
    goto payload_read_error;

  if (!gst_dp_validate_payload (GST_DP_HEADER_LENGTH, header, payload))
    goto payload_validate_error;

  *caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, header, payload);

  GST_DEBUG_OBJECT (this, "Got caps over GDP: %" GST_PTR_FORMAT, *caps);

  g_free (header);
  g_free (payload);

  return GST_FLOW_OK;

  /* ERRORS */
header_read_error:
  {
    g_free (header);
    return ret;
  }
header_validate_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("GDP caps packet header does not validate"));
    g_free (header);
    return GST_FLOW_ERROR;
  }
is_not_caps:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("GDP packet contains something that is not a caps (type %d)",
            gst_dp_header_payload_type (header)));
    g_free (header);
    return GST_FLOW_ERROR;
  }
payload_read_error:
  {
    g_free (header);
    g_free (payload);
    return ret;
  }
payload_validate_error:
  {
    GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
        ("GDP caps packet payload does not validate"));
    g_free (header);
    g_free (payload);
    return GST_FLOW_ERROR;
  }
}

/* write a GDP header to the socket.  Return false if fails. */
gboolean
gst_tcp_gdp_write_buffer (GstElement * this, int socket, GstBuffer * buffer,
    gboolean fatal, const gchar * host, int port)
{
  guint length;
  guint8 *header;
  size_t wrote;

  if (!gst_dp_header_from_buffer (buffer, 0, &length, &header))
    goto create_error;

  GST_LOG_OBJECT (this, "writing %d bytes for GDP buffer header", length);
  wrote = gst_tcp_socket_write (socket, header, length);
  g_free (header);

  if (wrote != length)
    goto write_error;

  return TRUE;

  /* ERRORS */
create_error:
  {
    if (fatal)
      GST_ELEMENT_ERROR (this, CORE, TOO_LAZY, (NULL),
          ("Could not create GDP header from buffer"));
    return FALSE;
  }
write_error:
  {
    if (fatal)
      GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
          (_("Error while sending data to \"%s:%d\"."), host, port),
          ("Only %" G_GSIZE_FORMAT " of %u bytes written: %s",
              wrote, GST_BUFFER_SIZE (buffer), g_strerror (errno)));
    return FALSE;
  }
}

/* write GDP header and payload to the given socket for the given caps.
 * Return false if fails. */
gboolean
gst_tcp_gdp_write_caps (GstElement * this, int socket, const GstCaps * caps,
    gboolean fatal, const char *host, int port)
{
  guint length;
  guint8 *header;
  guint8 *payload;
  size_t wrote;

  if (!gst_dp_packet_from_caps (caps, 0, &length, &header, &payload))
    goto create_error;

  GST_LOG_OBJECT (this, "writing %d bytes for GDP caps header", length);
  wrote = gst_tcp_socket_write (socket, header, length);
  if (wrote != length)
    goto write_header_error;

  length = gst_dp_header_payload_length (header);
  g_free (header);

  GST_LOG_OBJECT (this, "writing %d bytes for GDP caps payload", length);
  wrote = gst_tcp_socket_write (socket, payload, length);
  g_free (payload);

  if (wrote != length)
    goto write_payload_error;

  return TRUE;

  /* ERRORS */
create_error:
  {
    if (fatal)
      GST_ELEMENT_ERROR (this, CORE, TOO_LAZY, (NULL),
          ("Could not create GDP packet from caps"));
    return FALSE;
  }
write_header_error:
  {
    g_free (header);
    g_free (payload);
    if (fatal)
      GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
          (_("Error while sending gdp header data to \"%s:%d\"."), host, port),
          ("Only %" G_GSIZE_FORMAT " of %u bytes written: %s",
              wrote, length, g_strerror (errno)));
    return FALSE;
  }
write_payload_error:
  {
    if (fatal)
      GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
          (_("Error while sending gdp payload data to \"%s:%d\"."), host, port),
          ("Only %" G_GSIZE_FORMAT " of %u bytes written: %s",
              wrote, length, g_strerror (errno)));
    return FALSE;
  }
}