BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file net_ipv4_arp_probe.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 | * ARP probing module. |
||
32 | * |
||
33 | * Synopsis: |
||
34 | * net.ipv4.arp_probe(string ifname, string addr) |
||
35 | * |
||
36 | * Description: |
||
37 | * Monitors local presence of an IPv4 host on a network interface. |
||
38 | * On initialization, may take some time to determine whether |
||
39 | * the host is present or not, then goes to UP state. When it |
||
40 | * determines that presence has changed, toggles itself DOWN then |
||
41 | * UP to expose the new determination. |
||
42 | * |
||
43 | * Variables: |
||
44 | * exists - "true" if the host exists, "false" if not |
||
45 | */ |
||
46 | |||
47 | #include <stdlib.h> |
||
48 | |||
49 | #include <misc/ipaddr.h> |
||
50 | #include <arpprobe/BArpProbe.h> |
||
51 | |||
52 | #include <ncd/module_common.h> |
||
53 | |||
54 | #include <generated/blog_channel_ncd_net_ipv4_arp_probe.h> |
||
55 | |||
56 | #define STATE_UNKNOWN 1 |
||
57 | #define STATE_EXIST 2 |
||
58 | #define STATE_NOEXIST 3 |
||
59 | |||
60 | struct instance { |
||
61 | NCDModuleInst *i; |
||
62 | BArpProbe arpprobe; |
||
63 | int state; |
||
64 | }; |
||
65 | |||
66 | static void instance_free (struct instance *o, int is_error); |
||
67 | |||
68 | static void arpprobe_handler (struct instance *o, int event) |
||
69 | { |
||
70 | switch (event) { |
||
71 | case BARPPROBE_EVENT_EXIST: { |
||
72 | ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_NOEXIST) |
||
73 | |||
74 | ModuleLog(o->i, BLOG_INFO, "exist"); |
||
75 | |||
76 | if (o->state == STATE_NOEXIST) { |
||
77 | // signal down |
||
78 | NCDModuleInst_Backend_Down(o->i); |
||
79 | } |
||
80 | |||
81 | // signal up |
||
82 | NCDModuleInst_Backend_Up(o->i); |
||
83 | |||
84 | // set state exist |
||
85 | o->state = STATE_EXIST; |
||
86 | } break; |
||
87 | |||
88 | case BARPPROBE_EVENT_NOEXIST: { |
||
89 | ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_EXIST) |
||
90 | |||
91 | ModuleLog(o->i, BLOG_INFO, "noexist"); |
||
92 | |||
93 | if (o->state == STATE_EXIST) { |
||
94 | // signal down |
||
95 | NCDModuleInst_Backend_Down(o->i); |
||
96 | } |
||
97 | |||
98 | // signal up |
||
99 | NCDModuleInst_Backend_Up(o->i); |
||
100 | |||
101 | // set state noexist |
||
102 | o->state = STATE_NOEXIST; |
||
103 | } break; |
||
104 | |||
105 | case BARPPROBE_EVENT_ERROR: { |
||
106 | ModuleLog(o->i, BLOG_ERROR, "error"); |
||
107 | |||
108 | // die |
||
109 | instance_free(o, 1); |
||
110 | return; |
||
111 | } break; |
||
112 | |||
113 | default: ASSERT(0); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
118 | { |
||
119 | struct instance *o = vo; |
||
120 | o->i = i; |
||
121 | |||
122 | // read arguments |
||
123 | NCDValRef arg_ifname; |
||
124 | NCDValRef arg_addr; |
||
125 | if (!NCDVal_ListRead(params->args, 2, &arg_ifname, &arg_addr)) { |
||
126 | ModuleLog(o->i, BLOG_ERROR, "wrong arity"); |
||
127 | goto fail0; |
||
128 | } |
||
129 | if (!NCDVal_IsStringNoNulls(arg_ifname) || !NCDVal_IsString(arg_addr)) { |
||
130 | ModuleLog(o->i, BLOG_ERROR, "wrong type"); |
||
131 | goto fail0; |
||
132 | } |
||
133 | |||
134 | // parse address |
||
135 | uint32_t addr; |
||
136 | if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(arg_addr), &addr)) { |
||
137 | ModuleLog(o->i, BLOG_ERROR, "wrong address"); |
||
138 | goto fail0; |
||
139 | } |
||
140 | |||
141 | // null terminate ifname |
||
142 | NCDValNullTermString ifname_nts; |
||
143 | if (!NCDVal_StringNullTerminate(arg_ifname, &ifname_nts)) { |
||
144 | ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); |
||
145 | goto fail0; |
||
146 | } |
||
147 | |||
148 | // init arpprobe |
||
149 | int res = BArpProbe_Init(&o->arpprobe, ifname_nts.data, addr, i->params->iparams->reactor, o, (BArpProbe_handler)arpprobe_handler); |
||
150 | NCDValNullTermString_Free(&ifname_nts); |
||
151 | if (!res) { |
||
152 | ModuleLog(o->i, BLOG_ERROR, "BArpProbe_Init failed"); |
||
153 | goto fail0; |
||
154 | } |
||
155 | |||
156 | // set state unknown |
||
157 | o->state = STATE_UNKNOWN; |
||
158 | return; |
||
159 | |||
160 | fail0: |
||
161 | NCDModuleInst_Backend_DeadError(i); |
||
162 | } |
||
163 | |||
164 | static void instance_free (struct instance *o, int is_error) |
||
165 | { |
||
166 | // free arpprobe |
||
167 | BArpProbe_Free(&o->arpprobe); |
||
168 | |||
169 | if (is_error) { |
||
170 | NCDModuleInst_Backend_DeadError(o->i); |
||
171 | } else { |
||
172 | NCDModuleInst_Backend_Dead(o->i); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | static void func_die (void *vo) |
||
177 | { |
||
178 | struct instance *o = vo; |
||
179 | |||
180 | instance_free(o, 0); |
||
181 | } |
||
182 | |||
183 | static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) |
||
184 | { |
||
185 | struct instance *o = vo; |
||
186 | ASSERT(o->state == STATE_EXIST || o->state == STATE_NOEXIST) |
||
187 | |||
188 | if (!strcmp(name, "exists")) { |
||
189 | *out = ncd_make_boolean(mem, o->state == STATE_EXIST); |
||
190 | return 1; |
||
191 | } |
||
192 | |||
193 | return 0; |
||
194 | } |
||
195 | |||
196 | static struct NCDModule modules[] = { |
||
197 | { |
||
198 | .type = "net.ipv4.arp_probe", |
||
199 | .func_new2 = func_new, |
||
200 | .func_die = func_die, |
||
201 | .func_getvar = func_getvar, |
||
202 | .alloc_size = sizeof(struct instance) |
||
203 | }, { |
||
204 | .type = NULL |
||
205 | } |
||
206 | }; |
||
207 | |||
208 | const struct NCDModuleGroup ncdmodule_net_ipv4_arp_probe = { |
||
209 | .modules = modules |
||
210 | }; |