1 /* GStreamer
2 * Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
3 * Copyright (C) 2017 Robert Rosengren <robertr@axis.com>
4 *
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.
9 *
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.
14 *
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20 /**
21 * SECTION:gstnetutils
22 * @title: GstNetUtils
23 * @short_description: Network utility functions.
24 *
25 * GstNetUtils gathers network utility functions, enabling use for all
26 * gstreamer plugins.
27 *
28 * Since: 1.18
29 *
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "gstnetutils.h"
37 #include <gst/gstinfo.h>
38 #include <errno.h>
39
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
42 #endif
43
44 #ifndef G_OS_WIN32
45 #include <netinet/in.h>
46 #endif
47
48 /**
49 * gst_net_utils_set_socket_tos:
50 * @socket: Socket to configure
51 * @qos_dscp: QoS DSCP value
52 *
53 * Configures IP_TOS value of socket, i.e. sets QoS DSCP.
54 *
55 * Returns: TRUE if successful, FALSE in case an error occurred.
56 *
57 * Since: 1.18
58 */
59 gboolean
gst_net_utils_set_socket_tos(GSocket * socket,gint qos_dscp)60 gst_net_utils_set_socket_tos (GSocket * socket, gint qos_dscp)
61 {
62 gboolean ret = FALSE;
63
64 #ifdef IP_TOS
65 gint tos, fd;
66 fd = g_socket_get_fd (socket);
67
68 /* Extract and shift 6 bits of DSFIELD */
69 tos = (qos_dscp & 0x3f) << 2;
70
71 if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) {
72 GST_ERROR ("could not set TOS: %s", g_strerror (errno));
73 } else {
74 ret = TRUE;
75 }
76 #ifdef IPV6_TCLASS
77 if (g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6) {
78 if (setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0) {
79 GST_ERROR ("could not set TCLASS: %s", g_strerror (errno));
80 } else {
81 ret = TRUE;
82 }
83 }
84 #endif
85 #endif
86
87 return ret;
88 }
89