Home | History | Annotate | Download | only in lib
      1 /*
      2  * src/lib/addr.c     Address Helpers
      3  *
      4  *	This library is free software; you can redistribute it and/or
      5  *	modify it under the terms of the GNU Lesser General Public
      6  *	License as published by the Free Software Foundation version 2.1
      7  *	of the License.
      8  *
      9  * Copyright (c) 2008-2009 Thomas Graf <tgraf (at) suug.ch>
     10  */
     11 
     12 /**
     13  * @ingroup cli
     14  * @defgroup cli_addr Addresses
     15  *
     16  * @{
     17  */
     18 
     19 #include <netlink/cli/utils.h>
     20 #include <netlink/cli/addr.h>
     21 
     22 struct rtnl_addr *nl_cli_addr_alloc(void)
     23 {
     24 	struct rtnl_addr *addr;
     25 
     26 	addr = rtnl_addr_alloc();
     27 	if (!addr)
     28 		nl_cli_fatal(ENOMEM, "Unable to allocate address object");
     29 
     30 	return addr;
     31 }
     32 
     33 void nl_cli_addr_parse_family(struct rtnl_addr *addr, char *arg)
     34 {
     35 	int family;
     36 
     37 	if ((family = nl_str2af(arg)) != AF_UNSPEC)
     38 		rtnl_addr_set_family(addr, family);
     39 }
     40 
     41 void nl_cli_addr_parse_local(struct rtnl_addr *addr, char *arg)
     42 {
     43 	struct nl_addr *a;
     44 	int err;
     45 
     46 	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
     47 	if ((err = rtnl_addr_set_local(addr, a)) < 0)
     48 		nl_cli_fatal(err, "Unable to set local address: %s",
     49 			     nl_geterror(err));
     50 
     51 	nl_addr_put(a);
     52 }
     53 
     54 void nl_cli_addr_parse_dev(struct rtnl_addr *addr, struct nl_cache *link_cache,
     55 			   char *arg)
     56 {
     57 	int ival;
     58 
     59 	if (!(ival = rtnl_link_name2i(link_cache, arg)))
     60 		nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg);
     61 
     62 	rtnl_addr_set_ifindex(addr, ival);
     63 }
     64 
     65 void nl_cli_addr_parse_label(struct rtnl_addr *addr, char *arg)
     66 {
     67 	int err;
     68 
     69 	if ((err = rtnl_addr_set_label(addr, arg)) < 0)
     70 		nl_cli_fatal(err, "Unable to set address label: %s",
     71 			     nl_geterror(err));
     72 }
     73 
     74 void nl_cli_addr_parse_peer(struct rtnl_addr *addr, char *arg)
     75 {
     76 	struct nl_addr *a;
     77 	int err;
     78 
     79 	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
     80 	if ((err = rtnl_addr_set_peer(addr, a)) < 0)
     81 		nl_cli_fatal(err, "Unable to set peer address: %s",
     82 			     nl_geterror(err));
     83 
     84 	nl_addr_put(a);
     85 }
     86 
     87 void nl_cli_addr_parse_scope(struct rtnl_addr *addr, char *arg)
     88 {
     89 	int ival;
     90 
     91 	if ((ival = rtnl_str2scope(arg)) < 0)
     92 		nl_cli_fatal(EINVAL, "Unknown address scope \"%s\"", arg);
     93 
     94 	rtnl_addr_set_scope(addr, ival);
     95 }
     96 
     97 void nl_cli_addr_parse_broadcast(struct rtnl_addr *addr, char *arg)
     98 {
     99 	struct nl_addr *a;
    100 	int err;
    101 
    102 	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
    103 	if ((err = rtnl_addr_set_broadcast(addr, a)) < 0)
    104 		nl_cli_fatal(err, "Unable to set broadcast address: %s",
    105 			     nl_geterror(err));
    106 
    107 	nl_addr_put(a);
    108 }
    109 
    110 static uint32_t parse_lifetime(const char *arg)
    111 {
    112 	uint64_t msecs;
    113 	int err;
    114 
    115 	if (!strcasecmp(arg, "forever"))
    116 		return 0xFFFFFFFFU;
    117 
    118 	if ((err = nl_str2msec(arg, &msecs)) < 0)
    119 		nl_cli_fatal(err, "Unable to parse time string \"%s\": %s",
    120 			     arg, nl_geterror(err));
    121 
    122 	return (msecs / 1000);
    123 }
    124 
    125 void nl_cli_addr_parse_preferred(struct rtnl_addr *addr, char *arg)
    126 {
    127 	rtnl_addr_set_preferred_lifetime(addr, parse_lifetime(arg));
    128 }
    129 
    130 void nl_cli_addr_parse_valid(struct rtnl_addr *addr, char *arg)
    131 {
    132 	rtnl_addr_set_valid_lifetime(addr, parse_lifetime(arg));
    133 }
    134 
    135 /** @} */
    136