From bd41fcf11dc8849d4b7ba2af39f808a4a6c0a62e Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Mon, 20 Nov 2006 00:23:13 +0000 Subject: [PATCH] Make the prefix-to-netmask conversion work on big-endian too. Check for invalid prefixes. Improve help message. Add --set operation that removes all the old IP addresses first. git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2385 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- src/Makefile-files | 2 +- src/naddress.c | 42 +++++++++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Makefile-files b/src/Makefile-files index 01f8af2..4cd4f59 100644 --- a/src/Makefile-files +++ b/src/Makefile-files @@ -316,7 +316,7 @@ src_nattribute_LDADD = $(VSERVER_LDADDS) src_nattribute_LDFLAGS = $(VSERVER_LDFLGS) src_naddress_SOURCES = src/naddress.c -src_naddress_LDADD = $(VSERVER_LDADDS) +src_naddress_LDADD = $(VSERVER_LDADDS) $(LIBINTERNAL) src_naddress_LDFLAGS = $(VSERVER_LDFLGS) EXTRA_PROGRAMS += $(src_sbin_CXX_PROGS) $(src_pkglib_CXX_PROGS) diff --git a/src/naddress.c b/src/naddress.c index 0d1efc1..9ff1037 100644 --- a/src/naddress.c +++ b/src/naddress.c @@ -54,6 +54,7 @@ #define CMD_NID 0x2001 #define CMD_ADD 0x2002 #define CMD_REMOVE 0x2003 +#define CMD_SET 0x2004 #define CMD_IP 0x2010 #define CMD_BCAST 0x2011 @@ -67,6 +68,7 @@ CMDLINE_OPTIONS[] = { { "silent", no_argument, 0, CMD_SILENT }, { "add", no_argument, 0, CMD_ADD }, { "remove", no_argument, 0, CMD_REMOVE }, + { "set", no_argument, 0, CMD_SET }, { "nid", required_argument, 0, CMD_NID }, { "ip", required_argument, 0, CMD_IP }, { "bcast", required_argument, 0, CMD_BCAST }, @@ -84,6 +86,7 @@ struct Arguments { bool is_silent; bool do_add; bool do_remove; + bool do_set; }; static void @@ -92,7 +95,8 @@ showHelp(int fd, char const *cmd, int res) WRITE_MSG(fd, "Usage:\n "); WRITE_STR(fd, cmd); WRITE_MSG(fd, - " [--silent] [--nid ] [--ip [/]] [--bcast ] [--] *\n\n" + " (--add|--remove|--set) [--silent] [--nid ]\n" + " [--ip [/]] [--bcast ] [--] *\n\n" "Please report bugs to " PACKAGE_BUGREPORT "\n"); exit(res); @@ -239,10 +243,24 @@ readIP(char const *str, struct vc_ips **ips) else { // Ok, we have a network size, not a netmask if (strchr(pt,'.')==0 && strchr(pt,':')==0) { - int sz = atoi(pt); + unsigned long sz, limit = 0; + + switch ((*ips)->a.type) { + case vcNET_IPV4: limit = 32; break; + case vcNET_IPV6: limit = 128; break; + default: break; + } + + if (!isNumberUnsigned(pt, &sz, true) || sz > limit) { + WRITE_MSG(2, "Invalid prefix '"); + WRITE_STR(2, pt); + WRITE_MSG(2, "'\n"); + exit(wrapper_exit_code); + } + switch ((*ips)->a.type) { case vcNET_IPV4: - mask[0] = htonl((1 << sz) - 1); + mask[0] = htonl(~((1 << (32 - sz)) - 1)); break; case vcNET_IPV6: mask[0] = sz; @@ -294,7 +312,7 @@ tellAddress(struct vc_net_nx *addr, bool silent) if (silent) return; if (inet_ntop(addr->type == vcNET_IPV6 ? AF_INET6 : AF_INET, - &addr->ip, buf, sizeof(buf)) == NULL) { + addr->ip, buf, sizeof(buf)) == NULL) { WRITE_MSG(1, " "); return; } @@ -306,7 +324,16 @@ static inline void doit(struct Arguments *args) { struct vc_ips *ips; - if (args->do_add) { + + if (args->do_set) { + struct vc_net_nx remove = { .type = vcNET_ANY }; + if (vc_net_remove(args->nid, &remove) == -1) { + perror(ENSC_WRAPPERS_PREFIX "vc_net_remove()"); + exit(wrapper_exit_code); + } + } + + if (args->do_add || args->do_set) { if (!args->is_silent) WRITE_MSG(1, "Adding"); for (ips = &args->head; ips->next; ips = ips->next) { @@ -357,6 +384,7 @@ int main (int argc, char *argv[]) case CMD_NID : args.nid = Evc_nidopt2nid(optarg,true); break; case CMD_ADD : args.do_add = true; break; case CMD_REMOVE : args.do_remove = true; break; + case CMD_SET : args.do_set = true; break; case CMD_IP : readIP(optarg, &ips); break; default : WRITE_MSG(2, "Try '"); @@ -369,11 +397,11 @@ int main (int argc, char *argv[]) if (args.nid == VC_NOCTX) args.nid = Evc_get_task_nid(0); - if (!args.do_add && !args.do_remove) { + if (!args.do_add && !args.do_remove && !args.do_set) { WRITE_MSG(2, "No operation specified; try '--help' for more information\n"); exit(wrapper_exit_code); } - else if (args.do_add && args.do_remove) { + else if (((args.do_add ? 1 : 0) + (args.do_remove ? 1 : 0) + (args.do_set ? 1 : 0)) > 1) { WRITE_MSG(2, "Multiple operations specified; try '--help' for more information\n"); exit(wrapper_exit_code); } -- 1.8.1.5