Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pacific: common/ipaddr: skip loopback interfaces named 'lo' and test it #40425

Merged
merged 2 commits into from Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/common/ipaddr.cc
Expand Up @@ -60,7 +60,7 @@ const struct ifaddrs *find_ipv4_in_subnet(const struct ifaddrs *addrs,
if (addrs->ifa_addr == NULL)
continue;

if (boost::starts_with(addrs->ifa_name, "lo:"))
if (strcmp(addrs->ifa_name, "lo") == 0 || boost::starts_with(addrs->ifa_name, "lo:"))
continue;

if (numa_node >= 0 && !match_numa_node(addrs->ifa_name, numa_node))
Expand Down Expand Up @@ -107,7 +107,7 @@ const struct ifaddrs *find_ipv6_in_subnet(const struct ifaddrs *addrs,
if (addrs->ifa_addr == NULL)
continue;

if (boost::starts_with(addrs->ifa_name, "lo"))
if (strcmp(addrs->ifa_name, "lo") == 0 || boost::starts_with(addrs->ifa_name, "lo:"))
continue;

if (numa_node >= 0 && !match_numa_node(addrs->ifa_name, numa_node))
Expand Down
67 changes: 67 additions & 0 deletions src/test/test_ipaddr.cc
Expand Up @@ -183,6 +183,41 @@ TEST(CommonIPAddr, TestV4_PrefixZero)
ASSERT_EQ((struct sockaddr*)&a_two, result->ifa_addr);
}

static char lo[] = "lo";
static char lo0[] = "lo:0";

TEST(CommonIPAddr, TestV4_SkipLoopback)
{
struct ifaddrs one, two, three;
struct sockaddr_in a_one;
struct sockaddr_in a_two;
struct sockaddr_in a_three;
struct sockaddr_in net;
const struct ifaddrs *result;

memset(&net, 0, sizeof(net));

one.ifa_next = &two;
one.ifa_addr = (struct sockaddr*)&a_one;
one.ifa_name = lo;

two.ifa_next = &three;
two.ifa_addr = (struct sockaddr*)&a_two;
two.ifa_name = lo0;

three.ifa_next = NULL;
three.ifa_addr = (struct sockaddr*)&a_three;
three.ifa_name = eth0;

ipv4(&a_one, "127.0.0.1");
ipv4(&a_two, "127.0.0.1");
ipv4(&a_three, "10.1.2.3");
ipv4(&net, "255.0.0.0");

result = find_ip_in_subnet(&one, (struct sockaddr*)&net, 0);
ASSERT_EQ((struct sockaddr*)&a_three, result->ifa_addr);
}

TEST(CommonIPAddr, TestV6_Simple)
{
struct ifaddrs one, two;
Expand Down Expand Up @@ -279,6 +314,38 @@ TEST(CommonIPAddr, TestV6_PrefixZero)
ASSERT_EQ((struct sockaddr*)&a_two, result->ifa_addr);
}

TEST(CommonIPAddr, TestV6_SkipLoopback)
{
struct ifaddrs one, two, three;
struct sockaddr_in6 a_one;
struct sockaddr_in6 a_two;
struct sockaddr_in6 a_three;
struct sockaddr_in6 net;
const struct ifaddrs *result;

memset(&net, 0, sizeof(net));

one.ifa_next = &two;
one.ifa_addr = (struct sockaddr*)&a_one;
one.ifa_name = lo;

two.ifa_next = &three;
two.ifa_addr = (struct sockaddr*)&a_two;
two.ifa_name = lo0;

three.ifa_next = NULL;
three.ifa_addr = (struct sockaddr*)&a_three;
three.ifa_name = eth0;

ipv6(&a_one, "::1");
ipv6(&a_two, "::1");
ipv6(&a_three, "2001:1234:5678:90ab::beef");
ipv6(&net, "ff00::1");

result = find_ip_in_subnet(&one, (struct sockaddr*)&net, 0);
ASSERT_EQ((struct sockaddr*)&a_three, result->ifa_addr);
}

TEST(CommonIPAddr, ParseNetwork_Empty)
{
struct sockaddr_storage network;
Expand Down