libnl  3.4.0
nl-addr-add.c
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@suug.ch>
9  */
10 
11 #include <netlink/cli/utils.h>
12 #include <netlink/cli/addr.h>
13 #include <netlink/cli/link.h>
14 
15 #include <linux/netlink.h>
16 
17 static int quiet = 0;
18 
19 static void print_usage(void)
20 {
21  printf(
22 "Usage: nl-addr-add [OPTION]... [ADDRESS]\n"
23 "\n"
24 "Options\n"
25 " --replace Replace the address if it exists.\n"
26 " -q, --quiet Do not print informal notifications.\n"
27 " -h, --help Show this help.\n"
28 " -v, --version Show versioning information.\n"
29 "\n"
30 "Address Options\n"
31 " -a, --local=ADDR Address to be considered local.\n"
32 " -d, --dev=DEV Device the address should be assigned to.\n"
33 " --family=FAMILY Address family (normally autodetected).\n"
34 " --broadcast=ADDR Broadcast address of network (IPv4).\n"
35 " --peer=ADDR Peer address (IPv4).\n"
36 " --label=STRING Additional address label (IPv4).\n"
37 " --scope=SCOPE Scope of local address (IPv4).\n"
38 " --preferred=TIME Preferred lifetime (IPv6).\n"
39 " --valid=TIME Valid lifetime (IPv6).\n"
40  );
41 
42  exit(0);
43 }
44 
45 int main(int argc, char *argv[])
46 {
47  struct nl_sock *sock;
48  struct rtnl_addr *addr;
49  struct nl_cache *link_cache;
50  struct nl_dump_params dp = {
52  .dp_fd = stdout,
53  };
54  int err, nlflags = NLM_F_CREATE;
55 
56  sock = nl_cli_alloc_socket();
57  nl_cli_connect(sock, NETLINK_ROUTE);
58  link_cache = nl_cli_link_alloc_cache(sock);
59  addr = nl_cli_addr_alloc();
60 
61  for (;;) {
62  int c, optidx = 0;
63  enum {
64  ARG_FAMILY = 257,
65  ARG_LABEL = 258,
66  ARG_PEER,
67  ARG_SCOPE,
68  ARG_BROADCAST,
69  ARG_REPLACE,
70  ARG_PREFERRED,
71  ARG_VALID,
72  };
73  static struct option long_opts[] = {
74  { "replace", 0, 0, ARG_REPLACE },
75  { "quiet", 0, 0, 'q' },
76  { "help", 0, 0, 'h' },
77  { "version", 0, 0, 'v' },
78  { "local", 1, 0, 'a' },
79  { "dev", 1, 0, 'd' },
80  { "family", 1, 0, ARG_FAMILY },
81  { "label", 1, 0, ARG_LABEL },
82  { "peer", 1, 0, ARG_PEER },
83  { "scope", 1, 0, ARG_SCOPE },
84  { "broadcast", 1, 0, ARG_BROADCAST },
85  { "preferred", 1, 0, ARG_PREFERRED },
86  { "valid", 1, 0, ARG_VALID },
87  { 0, 0, 0, 0 }
88  };
89 
90  c = getopt_long(argc, argv, "qhva:d:", long_opts, &optidx);
91  if (c == -1)
92  break;
93 
94  switch (c) {
95  case '?': exit(NLE_INVAL);
96  case ARG_REPLACE: nlflags |= NLM_F_REPLACE; break;
97  case 'q': quiet = 1; break;
98  case 'h': print_usage(); break;
99  case 'v': nl_cli_print_version(); break;
100  case 'a': nl_cli_addr_parse_local(addr, optarg); break;
101  case 'd': nl_cli_addr_parse_dev(addr, link_cache, optarg); break;
102  case ARG_FAMILY: nl_cli_addr_parse_family(addr, optarg); break;
103  case ARG_LABEL: nl_cli_addr_parse_label(addr, optarg); break;
104  case ARG_PEER: nl_cli_addr_parse_peer(addr, optarg); break;
105  case ARG_SCOPE: nl_cli_addr_parse_scope(addr, optarg); break;
106  case ARG_BROADCAST: nl_cli_addr_parse_broadcast(addr, optarg); break;
107  case ARG_PREFERRED: nl_cli_addr_parse_preferred(addr, optarg); break;
108  case ARG_VALID: nl_cli_addr_parse_valid(addr, optarg); break;
109  }
110  }
111 
112  if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0)
113  nl_cli_fatal(err, "Unable to add address: %s",
114  nl_geterror(err));
115 
116  if (!quiet) {
117  printf("Added ");
118  nl_object_dump(OBJ_CAST(addr), &dp);
119  }
120 
121  return 0;
122 }
Dump object briefly on one line.
Definition: types.h:22
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:288
int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
Request addition of new address.
Definition: addr.c:747
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:77
Dumping parameters.
Definition: types.h:33