00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "plugin_util.h"
00048 #include "ipcalc.h"
00049 #include "olsr.h"
00050 #include "defs.h"
00051 #include "common/string.h"
00052 #include "olsr_logging.h"
00053
00054 #include <arpa/inet.h>
00055
00056 int
00057 set_plugin_port(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00058 {
00059 char *endptr;
00060 const unsigned int port = strtoul(value, &endptr, 0);
00061 if (*endptr != '\0' || endptr == value) {
00062 OLSR_WARN(LOG_PLUGINS, "Illegal port number \"%s\"", value);
00063 return 1;
00064 }
00065 if (port > 65535) {
00066 OLSR_WARN(LOG_PLUGINS, "Port number %u out of range", port);
00067 return 1;
00068 }
00069 if (data != NULL) {
00070 int *v = data;
00071 *v = port;
00072 OLSR_INFO(LOG_PLUGINS, "%s port number %u\n", "Got", port);
00073 } else {
00074 OLSR_INFO(LOG_PLUGINS, "%s port number %u\n", "Ignored", port);
00075 }
00076 return 0;
00077 }
00078
00079 int
00080 set_plugin_ipaddress(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00081 {
00082 char buf[INET6_ADDRSTRLEN];
00083 union olsr_ip_addr ip_addr;
00084 if (inet_pton(olsr_cnf->ip_version, value, &ip_addr) <= 0) {
00085 OLSR_WARN(LOG_PLUGINS, "Illegal IP address \"%s\"", value);
00086 return 1;
00087 }
00088 inet_ntop(olsr_cnf->ip_version, &ip_addr, buf, sizeof(buf));
00089 if (data != NULL) {
00090 union olsr_ip_addr *v = data;
00091 *v = ip_addr;
00092 OLSR_INFO(LOG_PLUGINS, "%s IP address %s\n", "Got", buf);
00093 } else {
00094 OLSR_INFO(LOG_PLUGINS, "%s IP address %s\n", "Ignored", buf);
00095 }
00096 return 0;
00097 }
00098
00099
00100 int
00101 set_plugin_boolean(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00102 {
00103 int *v = data;
00104 if (strcasecmp(value, "yes") == 0 || strcasecmp(value, "true") == 0) {
00105 *v = 1;
00106 } else if (strcasecmp(value, "no") == 0 || strcasecmp(value, "false") == 0) {
00107 *v = 0;
00108 } else {
00109 return 1;
00110 }
00111 return 0;
00112 }
00113
00114 int
00115 set_plugin_int(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00116 {
00117 char *endptr;
00118 const int theint = strtol(value, &endptr, 0);
00119 if (*endptr != '\0' || endptr == value) {
00120 OLSR_WARN(LOG_PLUGINS, "Illegal int \"%s\"", value);
00121 return 1;
00122 }
00123 if (data != NULL) {
00124 int *v = data;
00125 *v = theint;
00126 OLSR_INFO(LOG_PLUGINS, "%s int %d\n", "Got", theint);
00127 } else {
00128 OLSR_INFO(LOG_PLUGINS, "%s int %d\n", "Ignored", theint);
00129 }
00130 return 0;
00131 }
00132
00133 int
00134 set_plugin_string(const char *value, void *data, set_plugin_parameter_addon addon)
00135 {
00136 if (data != NULL) {
00137 char *v = data;
00138 if ((unsigned)strlen(value) >= addon.ui) {
00139 OLSR_WARN(LOG_PLUGINS, "String too long \"%s\"", value);
00140 return 1;
00141 }
00142 strscpy(v, value, addon.ui);
00143 OLSR_INFO(LOG_PLUGINS, "%s string %s\n", "Got", value);
00144 } else {
00145 OLSR_INFO(LOG_PLUGINS, "%s string %s\n", "Ignored", value);
00146 }
00147 return 0;
00148 }
00149
00150 static int
00151 ip_acl_plugin_parse(const char *value, union olsr_ip_addr *addr)
00152 {
00153
00154 static char arg[INET6_ADDRSTRLEN + 5];
00155
00156 char *c, *slash;
00157 bool ipv4 = false;
00158 bool ipv6 = false;
00159 int prefix;
00160
00161 prefix = olsr_cnf->ip_version == AF_INET ? 32 : 128;
00162
00163 strncpy(arg, value, sizeof(arg));
00164 arg[sizeof(arg) - 1] = 0;
00165
00166 c = arg;
00167 slash = NULL;
00168
00169 while (*c && *c != ' ' && *c != '\t') {
00170 switch (*c) {
00171 case '.':
00172 ipv4 = true;
00173 break;
00174 case ':':
00175 ipv6 = true;
00176 break;
00177 case '/':
00178 slash = c;
00179 break;
00180 }
00181 c++;
00182 }
00183
00184
00185 while (*c == ' ' || *c == '\t')
00186 c++;
00187
00188 if (ipv4 == ipv6) {
00189 OLSR_WARN(LOG_PLUGINS, "Error, illegal ip net '%s'\n", value);
00190 return -1;
00191 }
00192
00193 if (slash) {
00194
00195 *slash++ = 0;
00196 }
00197
00198 if (inet_pton(ipv4 ? AF_INET : AF_INET6, arg, addr) < 0) {
00199 OLSR_WARN(LOG_PLUGINS, "Error, illegal ip net '%s'\n", value);
00200 return -1;
00201 }
00202
00203 if (ipv4 && prefix == 128) {
00204
00205 ip_map_4to6(addr);
00206 } else if (ipv6 && olsr_cnf->ip_version == AF_INET) {
00207 OLSR_WARN(LOG_PLUGINS, "Ignore Ipv6 address '%s' in ipv4 mode\n", value);
00208 return -1;
00209 }
00210
00211 if (slash) {
00212
00213 prefix = (int)strtoul(slash, NULL, 10);
00214 if (prefix < 0 || prefix > (olsr_cnf->ip_version == AF_INET ? 32 : 128)) {
00215 OLSR_WARN(LOG_PLUGINS, "Error, illegal prefix length in '%s'\n", value);
00216 return -1;
00217 }
00218 } else if (ipv4 && *c) {
00219
00220 union olsr_ip_addr netmask;
00221
00222 if (inet_pton(AF_INET, c, &netmask) > 0) {
00223 prefix = olsr_netmask_to_prefix(&netmask);
00224 if (olsr_cnf->ip_version == AF_INET6) {
00225 prefix += 96;
00226 }
00227 }
00228 }
00229 return prefix;
00230 }
00231
00232 int
00233 ip_acl_add_plugin_accept(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00234 {
00235 union olsr_ip_addr ip;
00236 int prefix;
00237
00238 prefix = ip_acl_plugin_parse(value, &ip);
00239 if (prefix == -1)
00240 return -1;
00241
00242 ip_acl_add(data, &ip, prefix, false);
00243 return 0;
00244 }
00245
00246 int
00247 ip_acl_add_plugin_reject(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00248 {
00249 union olsr_ip_addr ip;
00250 int prefix;
00251
00252 prefix = ip_acl_plugin_parse(value, &ip);
00253 if (prefix == -1)
00254 return -1;
00255
00256 ip_acl_add(data, &ip, prefix, true);
00257 return 0;
00258 }
00259
00260 int
00261 ip_acl_add_plugin_checkFirst(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00262 {
00263 struct ip_acl *acl = data;
00264 acl->first_accept = strcasecmp(value, "accept") == 0;
00265 return 0;
00266 }
00267
00268 int
00269 ip_acl_add_plugin_defaultPolicy(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
00270 {
00271 struct ip_acl *acl = data;
00272 acl->default_accept = strcasecmp(value, "accept") == 0;
00273 return 0;
00274 }
00275
00276
00277
00278
00279
00280
00281
00282
00283