Home | History | Annotate | Download | only in src
      1 /*
      2  * src/nl-addr-add.c     Add addresses
      3  *
      4  *	This library is free software; you can redistribute it and/or
      5  *	modify it under the terms of the GNU General Public License as
      6  *	published by the Free Software Foundation version 2 of the License.
      7  *
      8  * Copyright (c) 2003-2009 Thomas Graf <tgraf (at) suug.ch>
      9  */
     10 
     11 #include <netlink/cli/utils.h>
     12 #include <netlink/cli/addr.h>
     13 #include <netlink/cli/link.h>
     14 
     15 static int quiet = 0;
     16 
     17 static void print_usage(void)
     18 {
     19 	printf(
     20 "Usage: nl-addr-add [OPTION]... [ADDRESS]\n"
     21 "\n"
     22 "Options\n"
     23 "     --replace             Replace the address if it exists.\n"
     24 " -q, --quiet               Do not print informal notifications.\n"
     25 " -h, --help                Show this help.\n"
     26 " -v, --version             Show versioning information.\n"
     27 "\n"
     28 "Address Options\n"
     29 " -a, --local=ADDR          Address to be considered local.\n"
     30 " -d, --dev=DEV             Device the address should be assigned to.\n"
     31 "     --family=FAMILY       Address family (normally autodetected).\n"
     32 "     --broadcast=ADDR      Broadcast address of network (IPv4).\n"
     33 "     --peer=ADDR           Peer address (IPv4).\n"
     34 "     --label=STRING        Additional address label (IPv4).\n"
     35 "     --scope=SCOPE         Scope of local address (IPv4).\n"
     36 "     --preferred=TIME      Preferred lifetime (IPv6).\n"
     37 "     --valid=TIME          Valid lifetime (IPv6).\n"
     38 	);
     39 
     40 	exit(0);
     41 }
     42 
     43 int main(int argc, char *argv[])
     44 {
     45 	struct nl_sock *sock;
     46 	struct rtnl_addr *addr;
     47 	struct nl_cache *link_cache;
     48 	struct nl_dump_params dp = {
     49 		.dp_type = NL_DUMP_LINE,
     50 		.dp_fd = stdout,
     51 	};
     52 	int err, nlflags = NLM_F_CREATE;
     53 
     54 	sock = nl_cli_alloc_socket();
     55 	nl_cli_connect(sock, NETLINK_ROUTE);
     56 	link_cache = nl_cli_link_alloc_cache(sock);
     57  	addr = nl_cli_addr_alloc();
     58 
     59 	for (;;) {
     60 		int c, optidx = 0;
     61 		enum {
     62 			ARG_FAMILY = 257,
     63 			ARG_LABEL = 258,
     64 			ARG_PEER,
     65 			ARG_SCOPE,
     66 			ARG_BROADCAST,
     67 			ARG_REPLACE,
     68 			ARG_PREFERRED,
     69 			ARG_VALID,
     70 		};
     71 		static struct option long_opts[] = {
     72 			{ "replace", 0, 0, ARG_REPLACE },
     73 			{ "quiet", 0, 0, 'q' },
     74 			{ "help", 0, 0, 'h' },
     75 			{ "version", 0, 0, 'v' },
     76 			{ "local", 1, 0, 'a' },
     77 			{ "dev", 1, 0, 'd' },
     78 			{ "family", 1, 0, ARG_FAMILY },
     79 			{ "label", 1, 0, ARG_LABEL },
     80 			{ "peer", 1, 0, ARG_PEER },
     81 			{ "scope", 1, 0, ARG_SCOPE },
     82 			{ "broadcast", 1, 0, ARG_BROADCAST },
     83 			{ "preferred", 1, 0, ARG_PREFERRED },
     84 			{ "valid", 1, 0, ARG_VALID },
     85 			{ 0, 0, 0, 0 }
     86 		};
     87 
     88 		c = getopt_long(argc, argv, "qhva:d:", long_opts, &optidx);
     89 		if (c == -1)
     90 			break;
     91 
     92 		switch (c) {
     93 		case '?': exit(NLE_INVAL);
     94 		case ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
     95 		case 'q': quiet = 1; break;
     96 		case 'h': print_usage(); break;
     97 		case 'v': nl_cli_print_version(); break;
     98 		case 'a': nl_cli_addr_parse_local(addr, optarg); break;
     99 		case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
    100 		case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
    101 		case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
    102 		case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
    103 		case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
    104 		case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
    105 		case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
    106 		case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
    107 		}
    108  	}
    109 
    110 	if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0)
    111 		nl_cli_fatal(err, "Unable to add address: %s",
    112 			     nl_geterror(err));
    113 
    114 	if (!quiet) {
    115 		printf("Added ");
    116 		nl_object_dump(OBJ_CAST(addr), &dp);
    117  	}
    118 
    119 	return 0;
    120 }
    121