BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file net_ipv4_addr_in_network.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 * Synopsis:
32 * net.ipv4.addr_in_network(string addr, string net_addr, string net_prefix)
33 * net.ipv4.addr_in_network(string addr, string cidr_net_addr)
34 * net.ipv4.ifnot_addr_in_network(string addr, string net_addr, string net_prefix)
35 * net.ipv4.ifnot_addr_in_network(string addr, string cidr_net_addr)
36 *
37 * Description:
38 * Checks if two IPv4 addresses belong to the same subnet.
39 * The prefix length is given either in the a separate argument or along with
40 * the second address in CIDR notation (address/prefix).
41 * This can be used to check whether an address belongs to a certain
42 * subnet, hence the name.
43 *
44 * Variables:
45 * (empty) - "true" if addresses belong to the same subnet, "false" if not
46 */
47  
48 #include <string.h>
49  
50 #include <misc/ipaddr.h>
51  
52 #include <ncd/module_common.h>
53  
54 #include <generated/blog_channel_ncd_net_ipv4_addr_in_network.h>
55  
56 struct instance {
57 NCDModuleInst *i;
58 int value;
59 };
60  
61 static void func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_ifnot)
62 {
63 struct instance *o = vo;
64 o->i = i;
65  
66 // read arguments
67 NCDValRef arg_addr;
68 NCDValRef arg_net_addr;
69 NCDValRef arg_net_prefix = NCDVal_NewInvalid();
70 if (!NCDVal_ListRead(params->args, 2, &arg_addr, &arg_net_addr) &&
71 !NCDVal_ListRead(params->args, 3, &arg_addr, &arg_net_addr, &arg_net_prefix)
72 ) {
73 ModuleLog(o->i, BLOG_ERROR, "wrong arity");
74 goto fail0;
75 }
76 if (!NCDVal_IsString(arg_addr) || !NCDVal_IsString(arg_net_addr) ||
77 (!NCDVal_IsInvalid(arg_net_prefix) && !NCDVal_IsString(arg_net_prefix))
78 ) {
79 ModuleLog(o->i, BLOG_ERROR, "wrong type");
80 goto fail0;
81 }
82  
83 // parse addr
84 uint32_t addr;
85 if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(arg_addr), &addr)) {
86 ModuleLog(o->i, BLOG_ERROR, "bad address");
87 goto fail0;
88 }
89  
90 // parse network
91 struct ipv4_ifaddr network;
92 if (NCDVal_IsInvalid(arg_net_prefix)) {
93 if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringMemRef(arg_net_addr), &network)) {
94 ModuleLog(o->i, BLOG_ERROR, "bad network in CIDR notation");
95 goto fail0;
96 }
97 } else {
98 if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(arg_net_addr), &network.addr)) {
99 ModuleLog(o->i, BLOG_ERROR, "bad network address");
100 goto fail0;
101 }
102 if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(arg_net_prefix), &network.prefix)) {
103 ModuleLog(o->i, BLOG_ERROR, "bad network prefix");
104 goto fail0;
105 }
106 }
107  
108 // test
109 o->value = ipaddr_ipv4_addrs_in_network(addr, network.addr, network.prefix);
110  
111 if (is_ifnot && o->value) {
112 ModuleLog(o->i, BLOG_ERROR, "addresses belong to same subnet, not proceeding");
113 }
114  
115 // signal up
116 if (!is_ifnot || !o->value) {
117 NCDModuleInst_Backend_Up(o->i);
118 }
119  
120 return;
121  
122 fail0:
123 NCDModuleInst_Backend_DeadError(i);
124 }
125  
126 static void func_new_normal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
127 {
128 func_new_common(vo, i, params, 0);
129 }
130  
131 static void func_new_ifnot (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
132 {
133 func_new_common(vo, i, params, 1);
134 }
135  
136 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
137 {
138 struct instance *o = vo;
139  
140 if (!strcmp(name, "")) {
141 *out = ncd_make_boolean(mem, o->value);
142 return 1;
143 }
144  
145 return 0;
146 }
147  
148 static struct NCDModule modules[] = {
149 {
150 .type = "net.ipv4.addr_in_network",
151 .func_new2 = func_new_normal,
152 .func_getvar = func_getvar,
153 .alloc_size = sizeof(struct instance)
154 }, {
155 .type = "ip_in_network", // compatibility name
156 .func_new2 = func_new_normal,
157 .func_getvar = func_getvar,
158 .alloc_size = sizeof(struct instance)
159 }, {
160 .type = "net.ipv4.ifnot_addr_in_network",
161 .func_new2 = func_new_ifnot,
162 .func_getvar = func_getvar,
163 .alloc_size = sizeof(struct instance)
164 }, {
165 .type = NULL
166 }
167 };
168  
169 const struct NCDModuleGroup ncdmodule_net_ipv4_addr_in_network = {
170 .modules = modules
171 };