nl-neigh-add.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * src/ nl-neigh-add.c Add a neighbour
  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) 2003-2009 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include <netlink/cli/utils.h>
  12. #include <netlink/cli/neigh.h>
  13. #include <netlink/cli/link.h>
  14. static int quiet = 0;
  15. static void print_usage(void)
  16. {
  17. printf(
  18. "Usage: nl-neigh-add [OPTION]... NEIGHBOUR\n"
  19. "\n"
  20. "Options\n"
  21. " --update-only Do not create neighbour, updates exclusively\n"
  22. " --create-only Do not update neighbour if it exists already.\n"
  23. " -q, --quiet Do not print informal notifications\n"
  24. " -h, --help Show this help\n"
  25. " -v, --version Show versioning information\n"
  26. "\n"
  27. "Neighbour Options\n"
  28. " -a, --addr=ADDR Destination address of neighbour\n"
  29. " -l, --lladdr=ADDR Link layer address of neighbour\n"
  30. " -d, --dev=DEV Device the neighbour is connected to\n"
  31. " --state=STATE Neighbour state, (default = permanent)\n"
  32. "\n"
  33. "Example\n"
  34. " nl-neigh-add --create-only --addr=10.0.0.1 --dev=eth0 \\\n"
  35. " --lladdr=AA:BB:CC:DD:EE:FF\n"
  36. );
  37. exit(0);
  38. }
  39. int main(int argc, char *argv[])
  40. {
  41. struct nl_sock *sock;
  42. struct rtnl_neigh *neigh;
  43. struct nl_cache *link_cache;
  44. struct nl_dump_params dp = {
  45. .dp_type = NL_DUMP_LINE,
  46. .dp_fd = stdout,
  47. };
  48. int err, ok = 0, nlflags = NLM_F_REPLACE | NLM_F_CREATE;
  49. sock = nl_cli_alloc_socket();
  50. nl_cli_connect(sock, NETLINK_ROUTE);
  51. link_cache = nl_cli_link_alloc_cache(sock);
  52. neigh = nl_cli_neigh_alloc();
  53. for (;;) {
  54. int c, optidx = 0;
  55. enum {
  56. ARG_UPDATE_ONLY = 257,
  57. ARG_CREATE_ONLY = 258,
  58. ARG_STATE,
  59. };
  60. static struct option long_opts[] = {
  61. { "update-only", 0, 0, ARG_UPDATE_ONLY },
  62. { "create-only", 0, 0, ARG_CREATE_ONLY },
  63. { "quiet", 0, 0, 'q' },
  64. { "help", 0, 0, 'h' },
  65. { "version", 0, 0, 'v' },
  66. { "addr", 1, 0, 'a' },
  67. { "lladdr", 1, 0, 'l' },
  68. { "dev", 1, 0, 'd' },
  69. { "state", 1, 0, ARG_STATE },
  70. { 0, 0, 0, 0 }
  71. };
  72. c = getopt_long(argc, argv, "qhva:l:d:", long_opts, &optidx);
  73. if (c == -1)
  74. break;
  75. switch (c) {
  76. case ARG_UPDATE_ONLY: nlflags &= ~NLM_F_CREATE; break;
  77. case ARG_CREATE_ONLY: nlflags |= NLM_F_EXCL; break;
  78. case 'q': quiet = 1; break;
  79. case 'h': print_usage(); break;
  80. case 'v': nl_cli_print_version(); break;
  81. case 'a': ok++; nl_cli_neigh_parse_dst(neigh, optarg); break;
  82. case 'l': nl_cli_neigh_parse_lladdr(neigh, optarg); break;
  83. case 'd': nl_cli_neigh_parse_dev(neigh, link_cache, optarg); break;
  84. case ARG_STATE: nl_cli_neigh_parse_state(neigh, optarg); break;
  85. }
  86. }
  87. if (!ok)
  88. print_usage();
  89. if ((err = rtnl_neigh_add(sock, neigh, nlflags)) < 0)
  90. nl_cli_fatal(err, "Unable to add neighbour: %s",
  91. nl_geterror(err));
  92. if (!quiet) {
  93. printf("Added ");
  94. nl_object_dump(OBJ_CAST(neigh), &dp);
  95. }
  96. return 0;
  97. }