Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

src/lib/util.c: introduce/use get_addr_family()

Make the code more readable in a couple of places.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
  • Loading branch information...
commit 8c22cfec4ddcf6d1af06b3fc29bd69b7e6a515cf 1 parent 2639f66
Kirill Kolyshkin kolyshkin authored
Showing with 19 additions and 9 deletions.
  1. +1 −0  include/util.h
  2. +4 −2 src/lib/net.c
  3. +14 −7 src/lib/util.c
1  include/util.h
View
@@ -42,6 +42,7 @@ char *arg2str(char **arg);
void free_arg(char **arg);
unsigned long min_ul(unsigned long val1, unsigned long val2);
int yesno2id(const char *str);
+int get_addr_family(const char *addr);
int get_netaddr(const char *ip_str, void *ip);
char *subst_VEID(envid_t veid, char *src);
int get_pagesize();
6 src/lib/net.c
View
@@ -328,7 +328,7 @@ static int remove_ipv6_addr(net_param *net)
cnt = 0;
list_for_each_safe(ip, tmp, head, list) {
- if (strchr(ip->val, ':')) {
+ if (get_addr_family(ip->val) == AF_INET6) {
free(ip->val);
list_del(&ip->list);
free(ip);
@@ -396,10 +396,12 @@ static int get_vps_ip_proc(envid_t veid, list_head_t *ip_h)
if (token == NULL)
break;
while ((token = strtok(NULL, " \t\n")) != NULL) {
- if (strchr(token, ':') &&
+ /* Canonicalize IPv6 addresses */
+ if ((get_addr_family(token) == AF_INET6) &&
inet_pton(AF_INET6, token, data) > 0 &&
!inet_ntop(AF_INET6, data, token, strlen(token)+1))
break;
+
if (add_str_param(ip_h, token)) {
free_str_param(ip_h);
cnt = -1;
21 src/lib/util.c
View
@@ -271,16 +271,23 @@ int yesno2id(const char *str)
return -1;
}
-int get_netaddr(const char *ip_str, void *ip)
+int get_addr_family(const char *addr)
{
- if (strchr(ip_str, ':')) {
- if (inet_pton(AF_INET6, ip_str, ip) <= 0)
- return -1;
+ if (strchr(addr, ':'))
return AF_INET6;
- }
- if (inet_pton(AF_INET, ip_str, ip) <= 0)
+ else
+ return AF_INET;
+}
+
+int get_netaddr(const char *ip_str, void *ip)
+{
+ int family;
+
+ family = get_addr_family(ip_str);
+
+ if (inet_pton(family, ip_str, ip) <= 0)
return -1;
- return AF_INET;
+ return family;
}
char *subst_VEID(envid_t veid, char *src)
Please sign in to comment.
Something went wrong with that request. Please try again.