NCDInterfaceMonitor.h 5.6 KB

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