BadVPN – Blame information for rev 1

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