netutils: Add util for setting socket DSCP
authorRobert Rosengren <robertr@axis.com>
Mon, 30 Oct 2017 09:49:06 +0000 (10:49 +0100)
committerSebastian Dröge <sebastian@centricular.com>
Wed, 1 Nov 2017 09:00:47 +0000 (11:00 +0200)
Util function for setting QoS DSCP added, to remove duplicated code in
netclientclock and nettimeprovider. Fix build error if missing IP_TOS.

https://bugzilla.gnome.org/show_bug.cgi?id=784737

libs/gst/net/Makefile.am
libs/gst/net/gstnetclientclock.c
libs/gst/net/gstnettimeprovider.c
libs/gst/net/gstnetutils.c [new file with mode: 0644]
libs/gst/net/gstnetutils.h [new file with mode: 0644]

index c7c6b50..35ed906 100644 (file)
@@ -18,9 +18,10 @@ libgstnet_@GST_API_VERSION@_la_SOURCES = \
     gstnettimepacket.c \
     gstnettimeprovider.c \
     gstptpclock.c \
-    gstntppacket.c
+    gstntppacket.c \
+    gstnetutils.c
 
-noinst_HEADERS = gstptp_private.h gstntppacket.h
+noinst_HEADERS = gstptp_private.h gstntppacket.h gstnetutils.h
 
 libgstnet_@GST_API_VERSION@_la_CFLAGS = $(GST_OBJ_CFLAGS) $(GIO_CFLAGS)
 libgstnet_@GST_API_VERSION@_la_LIBADD = $(GST_OBJ_LIBS) $(GIO_LIBS) \
index 49897d2..102ba19 100644 (file)
 #include "config.h"
 #endif
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-
-#ifndef G_OS_WIN32
-#include <netinet/in.h>
-#endif
-
 #include "gstnettimepacket.h"
 #include "gstntppacket.h"
 #include "gstnetclientclock.h"
+#include "gstnetutils.h"
 
 #include <gio/gio.h>
 
@@ -682,18 +675,8 @@ gst_net_client_internal_clock_thread (gpointer data)
 
         /* before next sending check if need to change QoS */
         new_qos_dscp = self->qos_dscp;
-        if (cur_qos_dscp != new_qos_dscp) {
-          gint tos, fd;
-          fd = g_socket_get_fd (socket);
-
-          /* Extract and shift 6 bits of DSFIELD */
-          tos = (new_qos_dscp & 0x3f) << 2;
-
-          if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
-            GST_ERROR_OBJECT (self, "could not set TOS: %s",
-                g_strerror (errno));
-          }
-
+        if (cur_qos_dscp != new_qos_dscp &&
+            gst_net_utils_set_socket_dscp (socket, new_qos_dscp)) {
           GST_DEBUG_OBJECT (self, "changed QoS DSCP to: %d", new_qos_dscp);
           cur_qos_dscp = new_qos_dscp;
         }
index 824e43d..b5432a1 100644 (file)
 #include "config.h"
 #endif
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-
-#ifndef G_OS_WIN32
-#include <netinet/in.h>
-#endif
-
 #include "gstnettimeprovider.h"
 #include "gstnettimepacket.h"
+#include "gstnetutils.h"
 
 GST_DEBUG_CATEGORY_STATIC (ntp_debug);
 #define GST_CAT_DEFAULT (ntp_debug)
@@ -225,17 +218,8 @@ gst_net_time_provider_thread (gpointer data)
 
     /* before next sending check if need to change QoS */
     new_qos_dscp = self->priv->qos_dscp;
-    if (cur_qos_dscp != new_qos_dscp) {
-      gint tos, fd;
-      fd = g_socket_get_fd (socket);
-
-      /* Extract and shift 6 bits of DSFIELD */
-      tos = (new_qos_dscp & 0x3f) << 2;
-
-      if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
-        GST_ERROR_OBJECT (self, "could not set TOS: %s", g_strerror (errno));
-      }
-
+    if (cur_qos_dscp != new_qos_dscp &&
+        gst_net_utils_set_socket_dscp (socket, new_qos_dscp)) {
       GST_DEBUG_OBJECT (self, "changed QoS DSCP to: %d", new_qos_dscp);
       cur_qos_dscp = new_qos_dscp;
     }
diff --git a/libs/gst/net/gstnetutils.c b/libs/gst/net/gstnetutils.c
new file mode 100644 (file)
index 0000000..a8a99d9
--- /dev/null
@@ -0,0 +1,66 @@
+/* GStreamer
+ * Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
+ * Copyright (C) 2017 Robert Rosengren <robertr@axis.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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "gstnetutils.h"
+#include <gst/gstinfo.h>
+#include <errno.h>
+
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+#ifndef G_OS_WIN32
+#include <netinet/in.h>
+#endif
+
+/**
+ * gst_net_time_packet_util_set_dscp:
+ * @socket: Socket to configure
+ * @qos_dscp: QoS DSCP value
+ *
+ * Configures IP_TOS value of socket, i.e. sets QoS DSCP.
+ *
+ * Returns: TRUE if successful, FALSE in case an error occurred.
+ */
+gboolean
+gst_net_utils_set_socket_dscp (GSocket * socket, gint qos_dscp)
+{
+  gboolean ret = FALSE;
+
+#ifdef IP_TOS
+  gint tos, fd;
+  fd = g_socket_get_fd (socket);
+
+  /* Extract and shift 6 bits of DSFIELD */
+  tos = (qos_dscp & 0x3f) << 2;
+
+  if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
+    GST_ERROR ("could not set TOS: %s", g_strerror (errno));
+  } else {
+    ret = TRUE;
+  }
+#endif
+
+  return ret;
+}
diff --git a/libs/gst/net/gstnetutils.h b/libs/gst/net/gstnetutils.h
new file mode 100644 (file)
index 0000000..053f187
--- /dev/null
@@ -0,0 +1,36 @@
+/* GStreamer
+ * Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
+ * Copyright (C) 2017 Robert Rosengren <robertr@axis.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.
+ */
+
+
+#ifndef __GST_NET_UTILS_H__
+#define __GST_NET_UTILS_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+G_GNUC_INTERNAL
+gboolean    gst_net_utils_set_socket_dscp (GSocket  * socket,
+                                           gint       qos_dscp);
+
+G_END_DECLS
+
+#endif /* __GST_NET_UTILS_H__ */