ncdinterfacemonitor_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <misc/get_iface_info.h>
  31. #include <misc/ipaddr6.h>
  32. #include <misc/debug.h>
  33. #include <base/BLog.h>
  34. #include <system/BTime.h>
  35. #include <system/BReactor.h>
  36. #include <system/BSignal.h>
  37. #include <ncd/NCDInterfaceMonitor.h>
  38. BReactor reactor;
  39. NCDInterfaceMonitor monitor;
  40. static void signal_handler (void *user);
  41. static void monitor_handler (void *unused, struct NCDInterfaceMonitor_event event);
  42. static void monitor_handler_error (void *unused);
  43. int main (int argc, char **argv)
  44. {
  45. int ret = 1;
  46. if (argc != 2) {
  47. fprintf(stderr, "Usage: %s <interface>\n", (argc > 0 ? argv[0] : ""));
  48. goto fail0;
  49. }
  50. int ifindex;
  51. if (!get_iface_info(argv[1], NULL, NULL, &ifindex)) {
  52. DEBUG("get_iface_info failed");
  53. goto fail0;
  54. }
  55. BTime_Init();
  56. BLog_InitStdout();
  57. if (!BNetwork_GlobalInit()) {
  58. DEBUG("BNetwork_GlobalInit failed");
  59. goto fail1;
  60. }
  61. if (!BReactor_Init(&reactor)) {
  62. DEBUG("BReactor_Init failed");
  63. goto fail1;
  64. }
  65. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  66. DEBUG("BSignal_Init failed");
  67. goto fail2;
  68. }
  69. int watch_flags = NCDIFMONITOR_WATCH_LINK;
  70. if (!NCDInterfaceMonitor_Init(&monitor, ifindex, watch_flags, &reactor, NULL, monitor_handler, monitor_handler_error)) {
  71. DEBUG("NCDInterfaceMonitor_Init failed");
  72. goto fail3;
  73. }
  74. ret = BReactor_Exec(&reactor);
  75. NCDInterfaceMonitor_Free(&monitor);
  76. fail3:
  77. BSignal_Finish();
  78. fail2:
  79. BReactor_Free(&reactor);
  80. fail1:
  81. BLog_Free();
  82. fail0:
  83. DebugObjectGlobal_Finish();
  84. return ret;
  85. }
  86. void signal_handler (void *user)
  87. {
  88. DEBUG("termination requested");
  89. BReactor_Quit(&reactor, 1);
  90. }
  91. void monitor_handler (void *unused, struct NCDInterfaceMonitor_event event)
  92. {
  93. switch (event.event) {
  94. case NCDIFMONITOR_EVENT_LINK_UP:
  95. case NCDIFMONITOR_EVENT_LINK_DOWN: {
  96. const char *type = (event.event == NCDIFMONITOR_EVENT_LINK_UP) ? "up" : "down";
  97. printf("link %s\n", type);
  98. } break;
  99. case NCDIFMONITOR_EVENT_IPV4_ADDR_ADDED:
  100. case NCDIFMONITOR_EVENT_IPV4_ADDR_REMOVED: {
  101. const char *type = (event.event == NCDIFMONITOR_EVENT_IPV4_ADDR_ADDED) ? "added" : "removed";
  102. uint8_t *addr = (uint8_t *)&event.u.ipv4_addr.addr;
  103. 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);
  104. } break;
  105. case NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED:
  106. case NCDIFMONITOR_EVENT_IPV6_ADDR_REMOVED: {
  107. const char *type = (event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED) ? "added" : "removed";
  108. char str[IPADDR6_PRINT_MAX];
  109. if (!ipaddr6_print_addr(event.u.ipv6_addr.addr.addr, str)) {
  110. DEBUG("ipaddr6_print_addr failed");
  111. return;
  112. }
  113. int dynamic = !!(event.u.ipv6_addr.addr_flags & NCDIFMONITOR_ADDR_FLAG_DYNAMIC);
  114. printf("ipv6 addr %s %s/%d scope=%d dynamic=%d\n", type, str, event.u.ipv6_addr.addr.prefix, event.u.ipv6_addr.addr.scope, dynamic);
  115. } break;
  116. default: ASSERT(0);
  117. }
  118. }
  119. void monitor_handler_error (void *unused)
  120. {
  121. DEBUG("monitor error");
  122. BReactor_Quit(&reactor, 1);
  123. }