ncdinterfacemonitor_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file ncdinterfacemonitor_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <inttypes.h>
  31. #include <stdio.h>
  32. #include <misc/get_iface_info.h>
  33. #include <misc/ipaddr6.h>
  34. #include <misc/debug.h>
  35. #include <base/BLog.h>
  36. #include <system/BTime.h>
  37. #include <system/BReactor.h>
  38. #include <system/BSignal.h>
  39. #include <ncd/NCDInterfaceMonitor.h>
  40. BReactor reactor;
  41. NCDInterfaceMonitor monitor;
  42. static void signal_handler (void *user);
  43. static void monitor_handler (void *unused, struct NCDInterfaceMonitor_event event);
  44. static void monitor_handler_error (void *unused);
  45. int main (int argc, char **argv)
  46. {
  47. int ret = 1;
  48. if (argc != 2) {
  49. fprintf(stderr, "Usage: %s <interface>\n", (argc > 0 ? argv[0] : ""));
  50. goto fail0;
  51. }
  52. int ifindex;
  53. if (!get_iface_info(argv[1], NULL, NULL, &ifindex)) {
  54. DEBUG("get_iface_info failed");
  55. goto fail0;
  56. }
  57. BTime_Init();
  58. BLog_InitStdout();
  59. if (!BNetwork_GlobalInit()) {
  60. DEBUG("BNetwork_GlobalInit failed");
  61. goto fail1;
  62. }
  63. if (!BReactor_Init(&reactor)) {
  64. DEBUG("BReactor_Init failed");
  65. goto fail1;
  66. }
  67. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  68. DEBUG("BSignal_Init failed");
  69. goto fail2;
  70. }
  71. int watch_flags = NCDIFMONITOR_WATCH_LINK|NCDIFMONITOR_WATCH_IPV4_ADDR|NCDIFMONITOR_WATCH_IPV6_ADDR;
  72. if (!NCDInterfaceMonitor_Init(&monitor, ifindex, watch_flags, &reactor, NULL, monitor_handler, monitor_handler_error)) {
  73. DEBUG("NCDInterfaceMonitor_Init failed");
  74. goto fail3;
  75. }
  76. ret = BReactor_Exec(&reactor);
  77. NCDInterfaceMonitor_Free(&monitor);
  78. fail3:
  79. BSignal_Finish();
  80. fail2:
  81. BReactor_Free(&reactor);
  82. fail1:
  83. BLog_Free();
  84. fail0:
  85. DebugObjectGlobal_Finish();
  86. return ret;
  87. }
  88. void signal_handler (void *user)
  89. {
  90. DEBUG("termination requested");
  91. BReactor_Quit(&reactor, 1);
  92. }
  93. void monitor_handler (void *unused, struct NCDInterfaceMonitor_event event)
  94. {
  95. switch (event.event) {
  96. case NCDIFMONITOR_EVENT_LINK_UP:
  97. case NCDIFMONITOR_EVENT_LINK_DOWN: {
  98. const char *type = (event.event == NCDIFMONITOR_EVENT_LINK_UP) ? "up" : "down";
  99. printf("link %s\n", type);
  100. } break;
  101. case NCDIFMONITOR_EVENT_IPV4_ADDR_ADDED:
  102. case NCDIFMONITOR_EVENT_IPV4_ADDR_REMOVED: {
  103. const char *type = (event.event == NCDIFMONITOR_EVENT_IPV4_ADDR_ADDED) ? "added" : "removed";
  104. uint8_t *addr = (uint8_t *)&event.u.ipv4_addr.addr;
  105. printf("ipv4 addr %s %d.%d.%d.%d/%d\n", type, (int)addr[0], (int)addr[1], (int)addr[2], (int)addr[3], event.u.ipv4_addr.addr.prefix);
  106. } break;
  107. case NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED:
  108. case NCDIFMONITOR_EVENT_IPV6_ADDR_REMOVED: {
  109. const char *type = (event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED) ? "added" : "removed";
  110. char str[IPADDR6_PRINT_MAX];
  111. ipaddr6_print_addr(event.u.ipv6_addr.addr.addr, str);
  112. int dynamic = !!(event.u.ipv6_addr.addr_flags & NCDIFMONITOR_ADDR_FLAG_DYNAMIC);
  113. printf("ipv6 addr %s %s/%d scope=%"PRIu8" dynamic=%d\n", type, str, event.u.ipv6_addr.addr.prefix, event.u.ipv6_addr.scope, dynamic);
  114. } break;
  115. default: ASSERT(0);
  116. }
  117. }
  118. void monitor_handler_error (void *unused)
  119. {
  120. DEBUG("monitor error");
  121. BReactor_Quit(&reactor, 1);
  122. }