Commit 9a7a7f6e authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Fix flawed logic

parent 3ee385e6
......@@ -40,6 +40,7 @@
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
using namespace std;
#include <vector>
......@@ -53,10 +54,18 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
{
/* Initializes IPv6 socket */
if (psz_iface != NULL)
{
scope_id = if_nametoindex (psz_iface);
if (scope_id == 0)
{
errno = ENODEV;
perror (psz_iface);
return;
}
}
fd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (fd6 != -1)
while (fd6 != -1)
{
struct sockaddr_in6 addr;
memset (&addr, 0, sizeof (addr));
......@@ -65,24 +74,14 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
addr.sin6_len = sizeof (addr);
#endif
if (psz_iface != NULL)
{
if (scope_id != 0)
if (scope_id != 0
&& setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_IF,
&scope_id, sizeof (scope_id)))
{
if (setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_IF,
&scope_id, sizeof (scope_id)) == 0)
scope_id = 0;
else
perror("setsockopt(IPV6_MULTICAST_IF)");
}
else
perror (psz_iface);
if (scope_id == 0)
{
close (fd6);
fd6 = -1;
}
break;
}
if (i_ttl != 0)
......@@ -90,17 +89,18 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
&i_ttl, sizeof(i_ttl));
if (bind (fd6, (struct sockaddr *)&addr, sizeof (addr)) == 0)
shutdown (fd6, SHUT_RD);
else
{
close (fd6);
fd6 = -1;
break;
}
shutdown (fd6, SHUT_RD);
break;
}
/* Initializes IPv4 socket */
fd4 = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd4 != -1)
while (fd4 != -1)
{
struct sockaddr_in addr;
memset (&addr, 0, sizeof (addr));
......@@ -121,9 +121,9 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
perror("ioctl(SIOCGIFADDR)");
close (fd4);
fd4 = -1;
break;
}
else
{
struct in_addr *ip;
ip = &((struct sockaddr_in *)(&req.ifr_addr))->sin_addr;
if (setsockopt(fd4, IPPROTO_IP, IP_MULTICAST_IF,
......@@ -132,7 +132,7 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
perror("setsockopt(IP_MULTICAST_IF)");
close (fd4);
fd4 = -1;
}
break;
}
}
......@@ -143,13 +143,14 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
setsockopt (fd4, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 1);
}
if (bind (fd4, (struct sockaddr *)&addr, sizeof (addr)) == 0)
shutdown (fd4, SHUT_RD);
else
if (bind (fd4, (struct sockaddr *)&addr, sizeof (addr)))
{
close (fd4);
fd4 = -1;
break;
}
shutdown (fd4, SHUT_RD);
break;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment