BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file net_backend_waitlink.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 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Module which waits for the link on a network interface. |
||
32 | * |
||
33 | * Synopsis: net.backend.waitlink(string ifname) |
||
34 | */ |
||
35 | |||
36 | #include <stdlib.h> |
||
37 | #include <string.h> |
||
38 | |||
39 | #include <misc/get_iface_info.h> |
||
40 | #include <ncd/extra/NCDIfConfig.h> |
||
41 | #include <ncd/extra/NCDInterfaceMonitor.h> |
||
42 | |||
43 | #include <ncd/module_common.h> |
||
44 | |||
45 | #include <generated/blog_channel_ncd_net_backend_waitlink.h> |
||
46 | |||
47 | struct instance { |
||
48 | NCDModuleInst *i; |
||
49 | NCDInterfaceMonitor monitor; |
||
50 | int up; |
||
51 | }; |
||
52 | |||
53 | static void instance_free (struct instance *o, int is_error); |
||
54 | |||
55 | static void monitor_handler (struct instance *o, struct NCDInterfaceMonitor_event event) |
||
56 | { |
||
57 | ASSERT(event.event == NCDIFMONITOR_EVENT_LINK_UP || event.event == NCDIFMONITOR_EVENT_LINK_DOWN) |
||
58 | |||
59 | int was_up = o->up; |
||
60 | o->up = (event.event == NCDIFMONITOR_EVENT_LINK_UP); |
||
61 | |||
62 | if (o->up && !was_up) { |
||
63 | NCDModuleInst_Backend_Up(o->i); |
||
64 | } |
||
65 | else if (!o->up && was_up) { |
||
66 | NCDModuleInst_Backend_Down(o->i); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | static void monitor_handler_error (struct instance *o) |
||
71 | { |
||
72 | ModuleLog(o->i, BLOG_ERROR, "monitor error"); |
||
73 | |||
74 | instance_free(o, 1); |
||
75 | } |
||
76 | |||
77 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
78 | { |
||
79 | struct instance *o = vo; |
||
80 | o->i = i; |
||
81 | |||
82 | // check arguments |
||
83 | NCDValRef ifname_arg; |
||
84 | if (!NCDVal_ListRead(params->args, 1, &ifname_arg)) { |
||
85 | ModuleLog(o->i, BLOG_ERROR, "wrong arity"); |
||
86 | goto fail0; |
||
87 | } |
||
88 | if (!NCDVal_IsStringNoNulls(ifname_arg)) { |
||
89 | ModuleLog(o->i, BLOG_ERROR, "wrong type"); |
||
90 | goto fail0; |
||
91 | } |
||
92 | |||
93 | // null terminate ifname |
||
94 | NCDValNullTermString ifname_nts; |
||
95 | if (!NCDVal_StringNullTerminate(ifname_arg, &ifname_nts)) { |
||
96 | ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); |
||
97 | goto fail0; |
||
98 | } |
||
99 | |||
100 | // get interface index |
||
101 | int ifindex; |
||
102 | int res = badvpn_get_iface_info(ifname_nts.data, NULL, NULL, &ifindex); |
||
103 | NCDValNullTermString_Free(&ifname_nts); |
||
104 | if (!res) { |
||
105 | ModuleLog(o->i, BLOG_ERROR, "failed to get interface index"); |
||
106 | goto fail0; |
||
107 | } |
||
108 | |||
109 | // init monitor |
||
110 | if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_LINK, i->params->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) { |
||
111 | ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed"); |
||
112 | goto fail0; |
||
113 | } |
||
114 | |||
115 | // set not up |
||
116 | o->up = 0; |
||
117 | return; |
||
118 | |||
119 | fail0: |
||
120 | NCDModuleInst_Backend_DeadError(i); |
||
121 | } |
||
122 | |||
123 | static void instance_free (struct instance *o, int is_error) |
||
124 | { |
||
125 | // free monitor |
||
126 | NCDInterfaceMonitor_Free(&o->monitor); |
||
127 | |||
128 | if (is_error) { |
||
129 | NCDModuleInst_Backend_DeadError(o->i); |
||
130 | } else { |
||
131 | NCDModuleInst_Backend_Dead(o->i); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | static void func_die (void *vo) |
||
136 | { |
||
137 | struct instance *o = vo; |
||
138 | instance_free(o, 0); |
||
139 | } |
||
140 | |||
141 | static struct NCDModule modules[] = { |
||
142 | { |
||
143 | .type = "net.backend.waitlink", |
||
144 | .func_new2 = func_new, |
||
145 | .func_die = func_die, |
||
146 | .alloc_size = sizeof(struct instance) |
||
147 | }, { |
||
148 | .type = NULL |
||
149 | } |
||
150 | }; |
||
151 | |||
152 | const struct NCDModuleGroup ncdmodule_net_backend_waitlink = { |
||
153 | .modules = modules |
||
154 | }; |