gst-ptp-helper.c 17.6 KB
Newer Older
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
/* GStreamer
 * Copyright (C) 2015 Sebastian Dröge <sebastian@centricular.com>
 *
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/* Helper process that runs setuid root or with appropriate privileges to
 * listen on ports < 1024, do multicast operations and get MAC addresses of
 * interfaces. Privileges are dropped after these operations are done.
 *
 * It listens on the PTP multicast group on port 319 and 320 and forwards
 * everything received there to stdout, while forwarding everything received
 * on stdout to those sockets.
 * Additionally it provides the MAC address of a network interface via stdout
 */

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

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string.h>

#ifdef HAVE_PTP_HELPER_SETUID
#include <grp.h>
#include <pwd.h>
#endif

#ifdef HAVE_PTP_HELPER_CAPABILITIES
#include <sys/capability.h>
#endif

#include <glib.h>
#include <gio/gio.h>

#include <gst/gst.h>
#include <gst/net/gstptp_private.h>

#define PTP_MULTICAST_GROUP "224.0.1.129"
#define PTP_EVENT_PORT   319
#define PTP_GENERAL_PORT 320

static gchar **ifaces = NULL;
static gboolean verbose = FALSE;
static guint64 clock_id = (guint64) - 1;
static guint8 clock_id_array[8];

static GOptionEntry opt_entries[] = {
  {"interface", 'i', 0, G_OPTION_ARG_STRING_ARRAY, &ifaces,
      "Interface to listen on", NULL},
  {"clock-id", 'c', 0, G_OPTION_ARG_INT64, &clock_id,
      "PTP clock id", NULL},
  {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
      "Be verbose", NULL},
  {NULL}
};

static GSocketAddress *event_saddr, *general_saddr;
static GSocket *socket_event, *socket_general;
static GIOChannel *stdin_channel, *stdout_channel;

static gboolean
have_socket_data_cb (GSocket * socket, GIOCondition condition,
    gpointer user_data)
{
  gchar buffer[8192];
  gssize read;
  gsize written;
  GError *err = NULL;
  GIOStatus status;
  StdIOHeader header = { 0, };

  read = g_socket_receive (socket, buffer, sizeof (buffer), NULL, &err);
  if (read == -1)
    g_error ("Failed to read from socket: %s", err->message);

  if (verbose)
    g_message ("Received %" G_GSSIZE_FORMAT " bytes from %s socket", read,
        (socket == socket_event ? "event" : "general"));

  header.size = read;
  header.type = (socket == socket_event) ? TYPE_EVENT : TYPE_GENERAL;

  status =
      g_io_channel_write_chars (stdout_channel, (gchar *) & header,
      sizeof (header), &written, &err);
  if (status == G_IO_STATUS_ERROR) {
    g_error ("Failed to write to stdout: %s", err->message);
  } else if (status == G_IO_STATUS_EOF) {
    g_message ("EOF on stdout");
    exit (0);
  } else if (status != G_IO_STATUS_NORMAL) {
    g_error ("Unexpected stdout write status: %d", status);
  } else if (written != sizeof (header)) {
    g_error ("Unexpected write size: %" G_GSIZE_FORMAT, written);
  }

  status =
      g_io_channel_write_chars (stdout_channel, buffer, read, &written, &err);
  if (status == G_IO_STATUS_ERROR) {
    g_error ("Failed to write to stdout: %s", err->message);
  } else if (status == G_IO_STATUS_EOF) {
    g_message ("EOF on stdout");
    exit (0);
  } else if (status != G_IO_STATUS_NORMAL) {
    g_error ("Unexpected stdout write status: %d", status);
  } else if (written != read) {
    g_error ("Unexpected write size: %" G_GSIZE_FORMAT, written);
  }

  return G_SOURCE_CONTINUE;
}

static gboolean
have_stdin_data_cb (GIOChannel * channel, GIOCondition condition,
    gpointer user_data)
{
  GIOStatus status;
  StdIOHeader header = { 0, };
  gchar buffer[8192];
  GError *err = NULL;
  gsize read;
  gssize written;

  if ((condition & G_IO_STATUS_EOF)) {
    g_message ("EOF on stdin");
    exit (0);
  }

  status =
      g_io_channel_read_chars (channel, (gchar *) & header, sizeof (header),
      &read, &err);
  if (status == G_IO_STATUS_ERROR) {
    g_error ("Failed to read from stdin: %s", err->message);
  } else if (status == G_IO_STATUS_EOF) {
    g_message ("EOF on stdin");
    exit (0);
  } else if (status != G_IO_STATUS_NORMAL) {
    g_error ("Unexpected stdin read status: %d", status);
  } else if (read != sizeof (header)) {
    g_error ("Unexpected read size: %" G_GSIZE_FORMAT, read);
  } else if (header.size > 8192) {
    g_error ("Unexpected size: %u", header.size);
  }

  status = g_io_channel_read_chars (channel, buffer, header.size, &read, &err);
  if (status == G_IO_STATUS_ERROR) {
    g_error ("Failed to read from stdin: %s", err->message);
  } else if (status == G_IO_STATUS_EOF) {
    g_message ("EOF on stdin");
    exit (0);
  } else if (status != G_IO_STATUS_NORMAL) {
    g_error ("Unexpected stdin read status: %d", status);
  } else if (read != header.size) {
    g_error ("Unexpected read size: %" G_GSIZE_FORMAT, read);
  }

  switch (header.type) {
    case TYPE_EVENT:
    case TYPE_GENERAL:
      written =
          g_socket_send_to (header.type ==
          TYPE_EVENT ? socket_event : socket_general,
          (header.type == TYPE_EVENT ? event_saddr : general_saddr), buffer,
          header.size, NULL, &err);
      if (written == -1)
        g_error ("Failed to write to socket: %s", err->message);
      else if (written != header.size)
        g_error ("Unexpected write size: %" G_GSSIZE_FORMAT, written);

      if (verbose)
        g_message ("Sent %" G_GSSIZE_FORMAT " bytes to %s socket", read,
            (header.type == TYPE_EVENT ? "event" : "general"));
      break;
    default:
      break;
  }

  return G_SOURCE_CONTINUE;
}

static void
setup_sockets (void)
{
  GInetAddress *bind_addr, *mcast_addr;
  GSocketAddress *bind_saddr;
  GSource *socket_event_source, *socket_general_source;
  gchar **probed_ifaces = NULL;
  GError *err = NULL;

  /* Create sockets */
  socket_event =
      g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
      G_SOCKET_PROTOCOL_UDP, &err);
  if (!socket_event)
    g_error ("Couldn't create event socket: %s", err->message);

  socket_general =
      g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
      G_SOCKET_PROTOCOL_UDP, &err);
  if (!socket_general)
    g_error ("Couldn't create general socket: %s", err->message);

  /* Bind sockets */
  bind_addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
  bind_saddr = g_inet_socket_address_new (bind_addr, PTP_EVENT_PORT);
  if (!g_socket_bind (socket_event, bind_saddr, TRUE, &err))
    g_error ("Couldn't bind event socket: %s", err->message);
  g_object_unref (bind_saddr);
  bind_saddr = g_inet_socket_address_new (bind_addr, PTP_GENERAL_PORT);
  if (!g_socket_bind (socket_general, bind_saddr, TRUE, &err))
    g_error ("Couldn't bind general socket: %s", err->message);
  g_object_unref (bind_saddr);
  g_object_unref (bind_addr);

  /* Probe all non-loopback interfaces */
  if (!ifaces) {
    struct ifreq ifr;
    struct ifconf ifc;
    gchar buf[8192];

    ifc.ifc_len = sizeof (buf);
    ifc.ifc_buf = buf;
    if (ioctl (g_socket_get_fd (socket_event), SIOCGIFCONF, &ifc) != -1) {
      struct ifreq *it = ifc.ifc_req;
      const struct ifreq *const end =
          it + (ifc.ifc_len / sizeof (struct ifreq));
      guint idx = 0;

      probed_ifaces = g_new0 (gchar *, ifc.ifc_len + 1);

      for (; it != end; ++it) {
        strcpy (ifr.ifr_name, it->ifr_name);
        if (ioctl (g_socket_get_fd (socket_event), SIOCGIFFLAGS, &ifr) == 0) {
          if ((ifr.ifr_flags & IFF_LOOPBACK))
            continue;
          probed_ifaces[idx] = g_strdup (it->ifr_name);
          idx++;
        } else {
          g_warning ("can't get flags of interface '%s'", it->ifr_name);
          probed_ifaces[idx] = g_strdup (it->ifr_name);
          idx++;
        }
      }

      if (idx != 0)
        ifaces = probed_ifaces;
    }
  }

  /* Get a clock id from the MAC address if none was given */
  if (clock_id == (guint64) - 1) {
    gboolean success = FALSE;

275
276
277
278
279
280
    /* FIXME: On Apple platforms use IOKit APIs to iterate
     * network interfaces and get MAC addresses. Think different!
     */
#ifndef __APPLE__
    struct ifreq ifr;

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
    if (ifaces) {
      gchar **ptr = ifaces;

      while (*ptr) {
        strcpy (ifr.ifr_name, *ptr);
        if (ioctl (g_socket_get_fd (socket_event), SIOCGIFHWADDR, &ifr) == 0) {
          clock_id_array[0] = ifr.ifr_hwaddr.sa_data[0];
          clock_id_array[1] = ifr.ifr_hwaddr.sa_data[1];
          clock_id_array[2] = ifr.ifr_hwaddr.sa_data[2];
          clock_id_array[3] = 0xff;
          clock_id_array[4] = 0xfe;
          clock_id_array[5] = ifr.ifr_hwaddr.sa_data[3];
          clock_id_array[6] = ifr.ifr_hwaddr.sa_data[4];
          clock_id_array[7] = ifr.ifr_hwaddr.sa_data[5];
          success = TRUE;
          break;
        }
      }

      ptr++;
    } else {
      struct ifconf ifc;
      gchar buf[8192];

      ifc.ifc_len = sizeof (buf);
      ifc.ifc_buf = buf;
      if (ioctl (g_socket_get_fd (socket_event), SIOCGIFCONF, &ifc) != -1) {
        struct ifreq *it = ifc.ifc_req;
        const struct ifreq *const end =
            it + (ifc.ifc_len / sizeof (struct ifreq));

        for (; it != end; ++it) {
          strcpy (ifr.ifr_name, it->ifr_name);
          if (ioctl (g_socket_get_fd (socket_event), SIOCGIFFLAGS, &ifr) == 0) {
            if ((ifr.ifr_flags & IFF_LOOPBACK))
              continue;

            if (ioctl (g_socket_get_fd (socket_event), SIOCGIFHWADDR,
                    &ifr) == 0) {
              clock_id_array[0] = ifr.ifr_hwaddr.sa_data[0];
              clock_id_array[1] = ifr.ifr_hwaddr.sa_data[1];
              clock_id_array[2] = ifr.ifr_hwaddr.sa_data[2];
              clock_id_array[3] = 0xff;
              clock_id_array[4] = 0xfe;
              clock_id_array[5] = ifr.ifr_hwaddr.sa_data[3];
              clock_id_array[6] = ifr.ifr_hwaddr.sa_data[4];
              clock_id_array[7] = ifr.ifr_hwaddr.sa_data[5];
              success = TRUE;
              break;
            }
          } else {
            g_warning ("can't get flags of interface '%s'", it->ifr_name);
          }
        }
      }
    }
337
#endif
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

    if (!success) {
      g_warning ("can't get any MAC address, using random clock id");
      clock_id = (((guint64) g_random_int ()) << 32) | (g_random_int ());
      GST_WRITE_UINT64_BE (clock_id_array, clock_id);
      clock_id_array[3] = 0xff;
      clock_id_array[4] = 0xfe;
    }
  } else {
    GST_WRITE_UINT64_BE (clock_id_array, clock_id);
  }

  /* Join multicast groups */
  mcast_addr = g_inet_address_new_from_string (PTP_MULTICAST_GROUP);
  if (ifaces) {
    gchar **ptr = ifaces;
    gboolean success = FALSE;

    while (*ptr) {
      gint c = 0;
      if (!g_socket_join_multicast_group (socket_event, mcast_addr, FALSE, *ptr,
              &err))
        g_warning ("Couldn't join multicast group on interface '%s': %s",
            *ptr, err->message);
      else
        c++;

      if (!g_socket_join_multicast_group (socket_general, mcast_addr, FALSE,
              *ptr, &err))
        g_warning ("Couldn't join multicast group on interface '%s': %s",
            *ptr, err->message);
      else
        c++;

      if (c == 2)
        success = TRUE;
      ptr++;
    }

    if (!success) {
      /* Join multicast group without any interface */
      if (!g_socket_join_multicast_group (socket_event, mcast_addr, FALSE, NULL,
              &err))
        g_error ("Couldn't join multicast group: %s", err->message);
      if (!g_socket_join_multicast_group (socket_general, mcast_addr, FALSE,
              NULL, &err))
        g_error ("Couldn't join multicast group: %s", err->message);
    }
  } else {
    /* Join multicast group without any interface */
    if (!g_socket_join_multicast_group (socket_event, mcast_addr, FALSE, NULL,
            &err))
      g_error ("Couldn't join multicast group: %s", err->message);
    if (!g_socket_join_multicast_group (socket_general, mcast_addr, FALSE, NULL,
            &err))
      g_error ("Couldn't join multicast group: %s", err->message);
  }

  event_saddr = g_inet_socket_address_new (mcast_addr, PTP_EVENT_PORT);
  general_saddr = g_inet_socket_address_new (mcast_addr, PTP_GENERAL_PORT);

  /* Create socket sources */
  socket_event_source =
      g_socket_create_source (socket_event, G_IO_IN | G_IO_PRI, NULL);
  g_source_set_priority (socket_event_source, G_PRIORITY_HIGH);
  g_source_set_callback (socket_event_source, (GSourceFunc) have_socket_data_cb,
      NULL, NULL);
  g_source_attach (socket_event_source, NULL);
  socket_general_source =
      g_socket_create_source (socket_general, G_IO_IN | G_IO_PRI, NULL);
  g_source_set_priority (socket_general_source, G_PRIORITY_DEFAULT);
  g_source_set_callback (socket_general_source,
      (GSourceFunc) have_socket_data_cb, NULL, NULL);
  g_source_attach (socket_general_source, NULL);

  g_strfreev (probed_ifaces);
}

static void
drop_privileges (void)
{
#ifdef HAVE_PTP_HELPER_SETUID
  /* Switch to the given user/group */
#ifdef HAVE_PTP_HELPER_SETUID_GROUP
  {
    struct group *grp;

    grp = getgrnam (HAVE_PTP_HELPER_SETUID_GROUP);
    if (!grp)
      g_error ("Failed to get group information '%s': %s",
          HAVE_PTP_HELPER_SETUID_GROUP, g_strerror (errno));

    if (setgid (grp->gr_gid) != 0)
      g_error ("Failed to change to group '%s': %s",
          HAVE_PTP_HELPER_SETUID_GROUP, g_strerror (errno));
  }
#endif

#ifdef HAVE_PTP_HELPER_SETUID_USER
  {
    struct passwd *pwd;

    pwd = getpwnam (HAVE_PTP_HELPER_SETUID_USER);
    if (!pwd)
      g_error ("Failed to get user information '%s': %s",
          HAVE_PTP_HELPER_SETUID_USER, g_strerror (errno));

#ifndef HAVE_PTP_HELPER_SETUID_GROUP
    if (setgid (pwd->pw_gid) != 0)
      g_error ("Failed to change to user group '%s': %s",
          HAVE_PTP_HELPER_SETUID_USER, g_strerror (errno));
#endif

    if (setuid (pwd->pw_uid) != 0)
      g_error ("Failed to change to user '%s': %s", HAVE_PTP_HELPER_SETUID_USER,
          g_strerror (errno));
  }
#endif
#endif
#ifdef HAVE_PTP_HELPER_CAPABILITIES
  /* Drop all capabilities */
  {
    cap_t caps;

    caps = cap_get_proc ();
    if (caps == 0)
      g_error ("Failed to get process caps: %s", g_strerror (errno));
    if (cap_clear (caps) != 0)
      g_error ("Failed to clear caps: %s", g_strerror (errno));
    if (cap_set_proc (caps) != 0)
      g_error ("Failed to set process caps: %s", g_strerror (errno));
  }
#endif
}

static void
setup_stdio_channels (void)
{
  GSource *stdin_source;

  /* Create stdin source */
  stdin_channel = g_io_channel_unix_new (STDIN_FILENO);
  if (g_io_channel_set_encoding (stdin_channel, NULL,
          NULL) == G_IO_STATUS_ERROR)
    g_error ("Failed to set stdin to binary encoding");
  g_io_channel_set_buffered (stdin_channel, FALSE);
  stdin_source =
      g_io_create_watch (stdin_channel, G_IO_IN | G_IO_PRI | G_IO_HUP);
  g_source_set_priority (stdin_source, G_PRIORITY_DEFAULT);
  g_source_set_callback (stdin_source, (GSourceFunc) have_stdin_data_cb, NULL,
      NULL);
  g_source_attach (stdin_source, NULL);

  /* Create stdout channel */
  stdout_channel = g_io_channel_unix_new (STDOUT_FILENO);
  if (g_io_channel_set_encoding (stdout_channel, NULL,
          NULL) == G_IO_STATUS_ERROR)
    g_error ("Failed to set stdout to binary encoding");
  g_io_channel_set_buffered (stdout_channel, FALSE);
}

static void
write_clock_id (void)
{
  GError *err = NULL;
  GIOStatus status;
  StdIOHeader header = { 0, };
  gsize written;

  /* Write clock id to stdout */

  header.type = TYPE_CLOCK_ID;
  header.size = 8;
  status =
      g_io_channel_write_chars (stdout_channel, (gchar *) & header,
      sizeof (header), &written, &err);
  if (status == G_IO_STATUS_ERROR) {
    g_error ("Failed to write to stdout: %s", err->message);
  } else if (status == G_IO_STATUS_EOF) {
    g_message ("EOF on stdout");
    exit (0);
  } else if (status != G_IO_STATUS_NORMAL) {
    g_error ("Unexpected stdout write status: %d", status);
  } else if (written != sizeof (header)) {
    g_error ("Unexpected write size: %" G_GSIZE_FORMAT, written);
  }

  status =
      g_io_channel_write_chars (stdout_channel,
      (const gchar *) clock_id_array, sizeof (clock_id_array), &written, &err);
  if (status == G_IO_STATUS_ERROR) {
    g_error ("Failed to write to stdout: %s", err->message);
  } else if (status == G_IO_STATUS_EOF) {
    g_message ("EOF on stdout");
    exit (0);
  } else if (status != G_IO_STATUS_NORMAL) {
    g_error ("Unexpected stdout write status: %d", status);
  } else if (written != sizeof (clock_id_array)) {
    g_error ("Unexpected write size: %" G_GSIZE_FORMAT, written);
  }
}

gint
main (gint argc, gchar ** argv)
{
  GOptionContext *opt_ctx;
  GMainLoop *loop;
  GError *err = NULL;

  opt_ctx = g_option_context_new ("- GStreamer PTP helper process");
  g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
  if (!g_option_context_parse (opt_ctx, &argc, &argv, &err))
    g_error ("Error parsing options: %s", err->message);
  g_option_context_free (opt_ctx);

  setup_sockets ();
  drop_privileges ();
  setup_stdio_channels ();
  write_clock_id ();

  /* Get running */
  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

  /* We never exit cleanly, so don't do cleanup */
  g_assert_not_reached ();

  return 0;
}