avtp: Refactor if_index code
authorAndre Guedes <andre.guedes@intel.com>
Fri, 4 Oct 2019 17:56:30 +0000 (10:56 -0700)
committerOlivier CrĂȘte <olivier.crete@ocrete.ca>
Wed, 5 Feb 2020 22:28:12 +0000 (22:28 +0000)
This patch refactors both avtpsink and avtpsrc code so we use the
if_nametoindex() helper instead of building a request and issuing an
ioctl to get the if_index.

ext/avtp/gstavtpsink.c
ext/avtp/gstavtpsrc.c

index 4b56934..b878742 100644 (file)
@@ -208,11 +208,17 @@ static gboolean
 gst_avtp_sink_start (GstBaseSink * basesink)
 {
   int fd, res;
-  struct ifreq req;
+  unsigned int index;
   guint8 addr[ETH_ALEN];
   struct sockaddr_ll sk_addr;
   GstAvtpSink *avtpsink = GST_AVTP_SINK (basesink);
 
+  index = if_nametoindex (avtpsink->ifname);
+  if (!index) {
+    GST_ERROR_OBJECT (avtpsink, "Failed to get if_index: %s", strerror (errno));
+    return FALSE;
+  }
+
   fd = socket (AF_PACKET, SOCK_DGRAM | SOCK_NONBLOCK, htons (ETH_P_TSN));
   if (fd < 0) {
     GST_ERROR_OBJECT (avtpsink, "Failed to open socket: %s", strerror (errno));
@@ -234,17 +240,10 @@ gst_avtp_sink_start (GstBaseSink * basesink)
     goto err;
   }
 
-  snprintf (req.ifr_name, sizeof (req.ifr_name), "%s", avtpsink->ifname);
-  res = ioctl (fd, SIOCGIFINDEX, &req);
-  if (res < 0) {
-    GST_ERROR_OBJECT (avtpsink, "Failed to ioctl(): %s", strerror (errno));
-    goto err;
-  }
-
   sk_addr.sll_family = AF_PACKET;
   sk_addr.sll_protocol = htons (ETH_P_TSN);
   sk_addr.sll_halen = ETH_ALEN;
-  sk_addr.sll_ifindex = req.ifr_ifindex;
+  sk_addr.sll_ifindex = index;
   sk_addr.sll_hatype = 0;
   sk_addr.sll_pkttype = 0;
   memcpy (sk_addr.sll_addr, addr, ETH_ALEN);
index 39fa7a2..9db1fd1 100644 (file)
@@ -195,28 +195,27 @@ static gboolean
 gst_avtp_src_start (GstBaseSrc * basesrc)
 {
   int fd, res;
+  unsigned int index;
   guint8 addr[ETH_ALEN];
-  struct ifreq req = { 0 };
   struct sockaddr_ll sk_addr = { 0 };
   struct packet_mreq mreq = { 0 };
   GstAvtpSrc *avtpsrc = GST_AVTP_SRC (basesrc);
 
+  index = if_nametoindex (avtpsrc->ifname);
+  if (!index) {
+    GST_ERROR_OBJECT (avtpsrc, "Failed to get if_index: %s", strerror (errno));
+    return FALSE;
+  }
+
   fd = socket (AF_PACKET, SOCK_DGRAM, htons (ETH_P_TSN));
   if (fd < 0) {
     GST_ERROR_OBJECT (avtpsrc, "Failed to open socket: %s", strerror (errno));
     return FALSE;
   }
 
-  snprintf (req.ifr_name, sizeof (req.ifr_name), "%s", avtpsrc->ifname);
-  res = ioctl (fd, SIOCGIFINDEX, &req);
-  if (res < 0) {
-    GST_ERROR_OBJECT (avtpsrc, "Failed to ioctl(): %s", strerror (errno));
-    goto err;
-  }
-
   sk_addr.sll_family = AF_PACKET;
   sk_addr.sll_protocol = htons (ETH_P_TSN);
-  sk_addr.sll_ifindex = req.ifr_ifindex;
+  sk_addr.sll_ifindex = index;
 
   res = bind (fd, (struct sockaddr *) &sk_addr, sizeof (sk_addr));
   if (res < 0) {
@@ -231,7 +230,7 @@ gst_avtp_src_start (GstBaseSrc * basesrc)
     goto err;
   }
 
-  mreq.mr_ifindex = req.ifr_ifindex;
+  mreq.mr_ifindex = index;
   mreq.mr_type = PACKET_MR_MULTICAST;
   mreq.mr_alen = ETH_ALEN;
   memcpy (&mreq.mr_address, addr, ETH_ALEN);