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