Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rework find_ip() to work with netmasks

We want the same IP with different (optional) netmask to be found,
i.e. we want to treat addresses 1.2.3.4, 1.2.3.4/16 and 1.2.3.4/32
as the same one.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
  • Loading branch information...
commit 0f8c65271d98f7edd5b947a4fc98c7d69f7dc4a2 1 parent d625152
Kirill Kolyshkin kolyshkin authored
Showing with 27 additions and 6 deletions.
  1. +1 −1  include/net.h
  2. +26 −5 src/lib/net.c
2  include/net.h
View
@@ -67,7 +67,7 @@ int vps_net_ctl(vps_handler *h, envid_t veid, int op, net_param *net,
int vps_set_netdev(vps_handler *h, envid_t veid, ub_param *ub,
net_param *net_add, net_param *net_del);
-int find_ip(list_head_t *ip_h, char *ipaddr);
+char *find_ip(list_head_t *ip_h, const char *ipaddr);
/** Obtain list of IP addresses belonging to the CT.
*
31 src/lib/net.c
View
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2000-2009, Parallels, Inc. All rights reserved.
+ * Copyright (C) 2000-2011, Parallels, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -43,17 +43,38 @@
#include "vps_configure.h"
-int find_ip(list_head_t *ip_h, char *ipaddr)
+char *find_ip(list_head_t *ip_h, const char *ipaddr)
{
ip_param *ip;
+ char *slash, *ip_slash, *ip_only;
+ int len;
if (list_empty(ip_h))
- return 0;
+ return NULL;
+
+ slash = strchr(ipaddr, '/');
+ if (slash) {
+ len = slash - ipaddr + 1;
+ ip_slash = strndupa(ipaddr, len);
+ ip_only = strndupa(ipaddr, len - 1);
+ }
+ else {
+ ip_only = NULL;
+ len = asprintf(&ip_slash, "%s/", ipaddr);
+ }
+
list_for_each(ip, ip_h, list) {
+ /* Match complete IP or IP/mask string */
if (!strcmp(ip->val, ipaddr))
- return 1;
+ return ip->val;
+ /* Match only IP */
+ if ((ip_only != NULL) && (!strcmp(ip->val, ip_only)))
+ return ip->val;
+ /* Match IP/ (same IP, different mask) */
+ if (!strncmp(ip->val, ip_slash, len))
+ return ip->val;
}
- return 0;
+ return NULL;
}
static inline int _ip_ctl(vps_handler *h, envid_t veid, int op,
Please sign in to comment.
Something went wrong with that request. Please try again.