summaryrefslogtreecommitdiffstats
path: root/src/lib/addr.c
diff options
context:
space:
mode:
authorThomas Graf <tgraf@suug.ch>2009-12-16 15:20:46 (GMT)
committerThomas Graf <tgraf@suug.ch>2009-12-16 15:20:46 (GMT)
commit8808743839b0f459394ecd00cb0f7c1896c0ab7a (patch)
treea3ab1da0c8bb02390662891bcb92e2130662b5d7 /src/lib/addr.c
parentff76549013c31082d303b3feef755bbd35e13ec6 (diff)
downloadlibnl-8808743839b0f459394ecd00cb0f7c1896c0ab7a.zip
libnl-8808743839b0f459394ecd00cb0f7c1896c0ab7a.tar.gz
libnl-8808743839b0f459394ecd00cb0f7c1896c0ab7a.tar.bz2
CLI - Command Line Interface Library
Moved common code in src/ used by CLI tools to src/lib/ for possible use by other CLI tools. Just link to libnl-cli.{so|la}
Diffstat (limited to 'src/lib/addr.c')
-rw-r--r--src/lib/addr.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/lib/addr.c b/src/lib/addr.c
new file mode 100644
index 0000000..a9c137b
--- /dev/null
+++ b/src/lib/addr.c
@@ -0,0 +1,135 @@
+/*
+ * src/lib/addr.c Address Helpers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
+ */
+
+/**
+ * @ingroup cli
+ * @defgroup cli_addr Addresses
+ *
+ * @{
+ */
+
+#include <netlink/cli/utils.h>
+#include <netlink/cli/addr.h>
+
+struct rtnl_addr *nl_cli_addr_alloc(void)
+{
+ struct rtnl_addr *addr;
+
+ addr = rtnl_addr_alloc();
+ if (!addr)
+ nl_cli_fatal(ENOMEM, "Unable to allocate address object");
+
+ return addr;
+}
+
+void nl_cli_addr_parse_family(struct rtnl_addr *addr, char *arg)
+{
+ int family;
+
+ if ((family = nl_str2af(arg)) != AF_UNSPEC)
+ rtnl_addr_set_family(addr, family);
+}
+
+void nl_cli_addr_parse_local(struct rtnl_addr *addr, char *arg)
+{
+ struct nl_addr *a;
+ int err;
+
+ a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
+ if ((err = rtnl_addr_set_local(addr, a)) < 0)
+ nl_cli_fatal(err, "Unable to set local address: %s",
+ nl_geterror(err));
+
+ nl_addr_put(a);
+}
+
+void nl_cli_addr_parse_dev(struct rtnl_addr *addr, struct nl_cache *link_cache,
+ char *arg)
+{
+ int ival;
+
+ if (!(ival = rtnl_link_name2i(link_cache, arg)))
+ nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg);
+
+ rtnl_addr_set_ifindex(addr, ival);
+}
+
+void nl_cli_addr_parse_label(struct rtnl_addr *addr, char *arg)
+{
+ int err;
+
+ if ((err = rtnl_addr_set_label(addr, arg)) < 0)
+ nl_cli_fatal(err, "Unable to set address label: %s",
+ nl_geterror(err));
+}
+
+void nl_cli_addr_parse_peer(struct rtnl_addr *addr, char *arg)
+{
+ struct nl_addr *a;
+ int err;
+
+ a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
+ if ((err = rtnl_addr_set_peer(addr, a)) < 0)
+ nl_cli_fatal(err, "Unable to set peer address: %s",
+ nl_geterror(err));
+
+ nl_addr_put(a);
+}
+
+void nl_cli_addr_parse_scope(struct rtnl_addr *addr, char *arg)
+{
+ int ival;
+
+ if ((ival = rtnl_str2scope(arg)) < 0)
+ nl_cli_fatal(EINVAL, "Unknown address scope \"%s\"", arg);
+
+ rtnl_addr_set_scope(addr, ival);
+}
+
+void nl_cli_addr_parse_broadcast(struct rtnl_addr *addr, char *arg)
+{
+ struct nl_addr *a;
+ int err;
+
+ a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
+ if ((err = rtnl_addr_set_broadcast(addr, a)) < 0)
+ nl_cli_fatal(err, "Unable to set broadcast address: %s",
+ nl_geterror(err));
+
+ nl_addr_put(a);
+}
+
+static uint32_t parse_lifetime(const char *arg)
+{
+ uint64_t msecs;
+ int err;
+
+ if (!strcasecmp(arg, "forever"))
+ return 0xFFFFFFFFU;
+
+ if ((err = nl_str2msec(arg, &msecs)) < 0)
+ nl_cli_fatal(err, "Unable to parse time string \"%s\": %s",
+ arg, nl_geterror(err));
+
+ return (msecs / 1000);
+}
+
+void nl_cli_addr_parse_preferred(struct rtnl_addr *addr, char *arg)
+{
+ rtnl_addr_set_preferred_lifetime(addr, parse_lifetime(arg));
+}
+
+void nl_cli_addr_parse_valid(struct rtnl_addr *addr, char *arg)
+{
+ rtnl_addr_set_valid_lifetime(addr, parse_lifetime(arg));
+}
+
+/** @} */