NCDInterfaceMonitor.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @file NCDInterfaceMonitor.h
  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. #ifndef BADVPN_NCD_NCDINTERFACEMONITOR_H
  30. #define BADVPN_NCD_NCDINTERFACEMONITOR_H
  31. #include <sys/socket.h>
  32. #include <linux/netlink.h>
  33. #include <misc/debug.h>
  34. #include <misc/ipaddr.h>
  35. #include <misc/ipaddr6.h>
  36. #include <misc/debugerror.h>
  37. #include <base/DebugObject.h>
  38. #include <system/BReactor.h>
  39. #include <system/BNetwork.h>
  40. #define NCDIFMONITOR_WATCH_LINK (1 << 0)
  41. #define NCDIFMONITOR_WATCH_IPV4_ADDR (1 << 1)
  42. #define NCDIFMONITOR_WATCH_IPV6_ADDR (1 << 2)
  43. #define NCDIFMONITOR_EVENT_LINK_UP 1
  44. #define NCDIFMONITOR_EVENT_LINK_DOWN 2
  45. #define NCDIFMONITOR_EVENT_IPV4_ADDR_ADDED 3
  46. #define NCDIFMONITOR_EVENT_IPV4_ADDR_REMOVED 4
  47. #define NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED 5
  48. #define NCDIFMONITOR_EVENT_IPV6_ADDR_REMOVED 6
  49. #define NCDIFMONITOR_ADDR_FLAG_DYNAMIC (1 << 0)
  50. struct NCDInterfaceMonitor_event {
  51. int event;
  52. union {
  53. struct {
  54. struct ipv4_ifaddr addr;
  55. } ipv4_addr;
  56. struct {
  57. struct ipv6_ifaddr addr;
  58. int addr_flags;
  59. } ipv6_addr;
  60. } u;
  61. };
  62. /**
  63. * Handler called to report an interface event.
  64. * Note that the event reporter does not keep any interface state, and as such may
  65. * report redundant events. You should therefore handle events in an idempotent
  66. * fashion.
  67. *
  68. * @param event.event event type. One of:
  69. * - NCDIFMONITOR_EVENT_LINK_UP, NCDIFMONITOR_EVENT_LINK_DOWN,
  70. * - NCDIFMONITOR_EVENT_IPV4_ADDR_ADDED, NCDIFMONITOR_EVENT_IPV4_ADDR_REMOVED,
  71. * - NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED, NCDIFMONITOR_EVENT_IPV6_ADDR_REMOVED.
  72. * Only events that correspont to enabled watch flags are reported.
  73. * @param event.ipv4_addr.addr the IPv4 address and prefix length
  74. * @param event.ipv6_addr.addr the IPv6 address, prefix length and scope
  75. * @param event.ipv6_addr.addr_flags IPv6 address flags. Valid flags:
  76. * - NCDIFMONITOR_ADDR_FLAG_DYNAMIC - this address was assigned dynamically (NDP)
  77. */
  78. typedef void (*NCDInterfaceMonitor_handler) (void *user, struct NCDInterfaceMonitor_event event);
  79. /**
  80. * Handler called when an error occurs.
  81. * The event reporter must be freed from within the job context of this
  82. * handler, and no other operations must be performed.
  83. */
  84. typedef void (*NCDInterfaceMonitor_handler_error) (void *user);
  85. /**
  86. * Watches for network interface events using a Linux rtnetlink socket.
  87. */
  88. typedef struct {
  89. int ifindex;
  90. int watch_events;
  91. BReactor *reactor;
  92. void *user;
  93. NCDInterfaceMonitor_handler handler;
  94. NCDInterfaceMonitor_handler_error handler_error;
  95. int netlink_fd;
  96. int event_netlink_fd;
  97. int dump_queue;
  98. uint32_t dump_seq;
  99. BFileDescriptor bfd;
  100. int have_bfd;
  101. union {
  102. uint8_t buf[4096];
  103. struct nlmsghdr nlh;
  104. } buf;
  105. struct nlmsghdr *buf_nh;
  106. int buf_left;
  107. BPending more_job;
  108. DebugError d_err;
  109. DebugObject d_obj;
  110. } NCDInterfaceMonitor;
  111. /**
  112. * Initializes the event reporter.
  113. * The reporter is not paused initially.
  114. * {@link BNetwork_GlobalInit} must have been done.
  115. *
  116. * @param ifindex index of network interface to report events for
  117. * @param watch_events mask specifying what kind of events to report. Valid flags are
  118. * NCDIFMONITOR_WATCH_LINK, NCDIFMONITOR_WATCH_IPV4_ADDR, NCDIFMONITOR_WATCH_IPV6_ADDR.
  119. * At least one flag must be provided.
  120. * @param reactor reactor we live in
  121. * @param user argument to handlers
  122. * @param handler handler to report interface events to
  123. * @param handler_error error handler
  124. * @return 1 on success, 0 on failure
  125. */
  126. int NCDInterfaceMonitor_Init (NCDInterfaceMonitor *o, int ifindex, int watch_events, BReactor *reactor, void *user,
  127. NCDInterfaceMonitor_handler handler,
  128. NCDInterfaceMonitor_handler_error handler_error) WARN_UNUSED;
  129. /**
  130. * Frees the event reporter.
  131. */
  132. void NCDInterfaceMonitor_Free (NCDInterfaceMonitor *o);
  133. /**
  134. * Pauses event reporting.
  135. * This operation is idempotent.
  136. */
  137. void NCDInterfaceMonitor_Pause (NCDInterfaceMonitor *o);
  138. /**
  139. * Resumes event reporting.
  140. * This operation is idempotent.
  141. */
  142. void NCDInterfaceMonitor_Continue (NCDInterfaceMonitor *o);
  143. #endif