avtp: Refactor if_index code

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.
This commit is contained in:
Andre Guedes 2019-10-04 10:56:30 -07:00 committed by Olivier Crête
parent 478fb29974
commit e74c807633
2 changed files with 17 additions and 19 deletions

View 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);

View 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);