Skip to content

Commit

Permalink
Add ntb_netaddress_is_ipv6
Browse files Browse the repository at this point in the history
Adds a function to determine whether an IP address is IPv6 and also
modifies ntb-netaddress.c to use it internally instead of explicitly
comparing against the IPv4 magic.
  • Loading branch information
bpeel committed Sep 11, 2014
1 parent 8a08b4e commit 26df85a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/ntb-netaddress.c
Expand Up @@ -74,12 +74,12 @@ void
ntb_netaddress_to_native(const struct ntb_netaddress *address,
struct ntb_netaddress_native *native)
{
if (!memcmp(address->host, ipv4_magic, sizeof ipv4_magic)) {
ntb_netaddress_to_native_ipv4(address, &native->sockaddr_in);
native->length = sizeof native->sockaddr_in;
} else {
if (ntb_netaddress_is_ipv6(address)) {
ntb_netaddress_to_native_ipv6(address, &native->sockaddr_in6);
native->length = sizeof native->sockaddr_in6;
} else {
ntb_netaddress_to_native_ipv4(address, &native->sockaddr_in);
native->length = sizeof native->sockaddr_in;
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ ntb_netaddress_to_string(const struct ntb_netaddress *address)
char *buf = ntb_alloc(buffer_length);
int len;

if (memcmp(address->host, ipv4_magic, sizeof(ipv4_magic))) {
if (ntb_netaddress_is_ipv6(address)) {
buf[0] = '[';
inet_ntop(AF_INET6,
address->host,
Expand Down Expand Up @@ -238,7 +238,7 @@ ntb_netaddress_is_allowed(const struct ntb_netaddress *address,
{
const uint8_t *host;

if (memcmp(address->host, ipv4_magic, sizeof ipv4_magic)) {
if (ntb_netaddress_is_ipv6(address)) {
/* IPv6 */
/* Ignore localhost */
if (!memcmp(address->host,
Expand Down Expand Up @@ -274,3 +274,9 @@ ntb_netaddress_is_allowed(const struct ntb_netaddress *address,

return true;
}

bool
ntb_netaddress_is_ipv6(const struct ntb_netaddress *address)
{
return memcmp(address->host, ipv4_magic, sizeof ipv4_magic);
}
3 changes: 3 additions & 0 deletions src/ntb-netaddress.h
Expand Up @@ -66,4 +66,7 @@ bool
ntb_netaddress_is_allowed(const struct ntb_netaddress *address,
bool allow_private_addresses);

bool
ntb_netaddress_is_ipv6(const struct ntb_netaddress *address);

#endif /* NTB_NETADDRESS_H */

0 comments on commit 26df85a

Please sign in to comment.