respondd: avoid calling if_nametoindex() twice

This commit is contained in:
lemoer 2016-12-01 01:24:46 +01:00
parent b67c212eab
commit d68f2814fb
1 changed files with 9 additions and 5 deletions

View File

@ -109,11 +109,12 @@ static void usage() {
puts(" -h this help\n"); puts(" -h this help\n");
} }
static void join_mcast(const int sock, const struct in6_addr addr, const char *iface) { // returns true on success
static bool join_mcast(const int sock, const struct in6_addr addr, unsigned int ifindex) {
struct ipv6_mreq mreq; struct ipv6_mreq mreq;
mreq.ipv6mr_multiaddr = addr; mreq.ipv6mr_multiaddr = addr;
mreq.ipv6mr_interface = if_nametoindex(iface); mreq.ipv6mr_interface = ifindex;
if (mreq.ipv6mr_interface == 0) if (mreq.ipv6mr_interface == 0)
goto error; goto error;
@ -121,11 +122,11 @@ static void join_mcast(const int sock, const struct in6_addr addr, const char *i
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1)
goto error; goto error;
return; return true;
error: error:
fprintf(stderr, "Could not join multicast group on %s: ", iface);
perror(NULL); perror(NULL);
return false;
} }
@ -609,7 +610,10 @@ int main(int argc, char **argv) {
} }
iface_set = true; iface_set = true;
last_ifindex = if_nametoindex(optarg); last_ifindex = if_nametoindex(optarg);
join_mcast(sock, mgroup_addr, optarg); if(!join_mcast(sock, mgroup_addr, last_ifindex)) {
fprintf(stderr, "Could not join multicast group on %s: ", optarg);
last_ifindex = 0;
}
break; break;
case 't': case 't':