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

Fix flawed logic

parent 3ee385e6
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include <stdio.h> #include <stdio.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
using namespace std; using namespace std;
#include <vector> #include <vector>
...@@ -53,10 +54,18 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1), ...@@ -53,10 +54,18 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
{ {
/* Initializes IPv6 socket */ /* Initializes IPv6 socket */
if (psz_iface != NULL) if (psz_iface != NULL)
{
scope_id = if_nametoindex (psz_iface); scope_id = if_nametoindex (psz_iface);
if (scope_id == 0)
{
errno = ENODEV;
perror (psz_iface);
return;
}
}
fd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP); fd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (fd6 != -1) while (fd6 != -1)
{ {
struct sockaddr_in6 addr; struct sockaddr_in6 addr;
memset (&addr, 0, sizeof (addr)); memset (&addr, 0, sizeof (addr));
...@@ -65,42 +74,33 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1), ...@@ -65,42 +74,33 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
addr.sin6_len = sizeof (addr); addr.sin6_len = sizeof (addr);
#endif #endif
if (psz_iface != NULL) if (scope_id != 0
&& setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_IF,
&scope_id, sizeof (scope_id)))
{ {
if (scope_id != 0) perror("setsockopt(IPV6_MULTICAST_IF)");
{ close (fd6);
if (setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_IF, fd6 = -1;
&scope_id, sizeof (scope_id)) == 0) break;
scope_id = 0;
else
perror("setsockopt(IPV6_MULTICAST_IF)");
}
else
perror (psz_iface);
if (scope_id == 0)
{
close (fd6);
fd6 = -1;
}
} }
if (i_ttl != 0) if (i_ttl != 0)
setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
&i_ttl, sizeof(i_ttl)); &i_ttl, sizeof(i_ttl));
if (bind (fd6, (struct sockaddr *)&addr, sizeof (addr)) == 0) if (bind (fd6, (struct sockaddr *)&addr, sizeof (addr)) == 0)
shutdown (fd6, SHUT_RD);
else
{ {
close (fd6); close (fd6);
fd6 = -1; fd6 = -1;
break;
} }
shutdown (fd6, SHUT_RD);
break;
} }
/* Initializes IPv4 socket */ /* Initializes IPv4 socket */
fd4 = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); fd4 = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd4 != -1) while (fd4 != -1)
{ {
struct sockaddr_in addr; struct sockaddr_in addr;
memset (&addr, 0, sizeof (addr)); memset (&addr, 0, sizeof (addr));
...@@ -121,18 +121,18 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1), ...@@ -121,18 +121,18 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
perror("ioctl(SIOCGIFADDR)"); perror("ioctl(SIOCGIFADDR)");
close (fd4); close (fd4);
fd4 = -1; 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,
(char *)ip, sizeof (*ip)))
{ {
struct in_addr *ip; perror("setsockopt(IP_MULTICAST_IF)");
ip = &((struct sockaddr_in *)(&req.ifr_addr))->sin_addr; close (fd4);
if (setsockopt(fd4, IPPROTO_IP, IP_MULTICAST_IF, fd4 = -1;
(char *)ip, sizeof (*ip))) break;
{
perror("setsockopt(IP_MULTICAST_IF)");
close (fd4);
fd4 = -1;
}
} }
} }
...@@ -143,13 +143,14 @@ Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1), ...@@ -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); setsockopt (fd4, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 1);
} }
if (bind (fd4, (struct sockaddr *)&addr, sizeof (addr)) == 0) if (bind (fd4, (struct sockaddr *)&addr, sizeof (addr)))
shutdown (fd4, SHUT_RD);
else
{ {
close (fd4); close (fd4);
fd4 = -1; 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