Tomato 1.28
[tomato.git] / release / src / router / miniupnpd / options.c
bloba9c8347c6484961f0ed662c544a531469db4bb2e
1 /* $Id: options.c,v 1.20 2008/10/06 13:22:02 nanard Exp $ */
2 /* MiniUPnP project
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * author: Ryan Wagoner
5 * (c) 2006 Thomas Bernard
6 * This software is subject to the conditions detailed
7 * in the LICENCE file provided within the distribution */
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <syslog.h>
14 #include "options.h"
15 #include "upnppermissions.h"
16 #include "upnpglobalvars.h"
18 struct option * ary_options = NULL;
19 int num_options = 0;
21 static const struct {
22 enum upnpconfigoptions id;
23 const char * name;
24 } optionids[] = {
25 { UPNPEXT_IFNAME, "ext_ifname" },
26 { UPNPEXT_IP, "ext_ip" },
27 { UPNPLISTENING_IP, "listening_ip" },
28 { UPNPPORT, "port" },
29 { UPNPBITRATE_UP, "bitrate_up" },
30 { UPNPBITRATE_DOWN, "bitrate_down" },
31 { UPNPPRESENTATIONURL, "presentation_url" },
32 { UPNPNOTIFY_INTERVAL, "notify_interval" },
33 { UPNPSYSTEM_UPTIME, "system_uptime" },
34 { UPNPPACKET_LOG, "packet_log" },
35 { UPNPUUID, "uuid"},
36 { UPNPSERIAL, "serial"},
37 { UPNPMODEL_NUMBER, "model_number"},
38 { UPNPCLEANTHRESHOLD, "clean_ruleset_threshold"},
39 { UPNPCLEANINTERVAL, "clean_ruleset_interval"},
40 #ifdef USE_NETFILTER
41 { UPNPFORWARDCHAIN, "upnp_forward_chain"},
42 { UPNPNATCHAIN, "upnp_nat_chain"},
43 #endif
44 #ifdef ENABLE_NATPMP
45 { UPNPENABLENATPMP, "enable_natpmp"},
46 #endif
47 { UPNPENABLE, "enable_upnp"},
48 #ifdef USE_PF
49 { UPNPQUEUE, "queue"},
50 { UPNPTAG, "tag"},
51 #endif
52 #ifdef PF_ENABLE_FILTER_RULES
53 { UPNPQUICKRULES, "quickrules" },
54 #endif
55 #ifdef ENABLE_LEASEFILE
56 { UPNPLEASEFILE, "lease_file"},
57 #endif
58 { UPNPMINISSDPDSOCKET, "minissdpdsocket"},
59 { UPNPSECUREMODE, "secure_mode"}
62 int
63 readoptionsfile(const char * fname)
65 FILE *hfile = NULL;
66 char buffer[1024];
67 char *equals;
68 char *name;
69 char *value;
70 char *t;
71 int linenum = 0;
72 int i;
73 enum upnpconfigoptions id;
75 if(!fname || (strlen(fname) == 0))
76 return -1;
78 memset(buffer, 0, sizeof(buffer));
80 #ifdef DEBUG
81 printf("Reading configuration from file %s\n", fname);
82 #endif
84 if(!(hfile = fopen(fname, "r")))
85 return -1;
87 if(ary_options != NULL)
89 free(ary_options);
90 num_options = 0;
93 while(fgets(buffer, sizeof(buffer), hfile))
95 linenum++;
96 t = strchr(buffer, '\n');
97 if(t)
99 *t = '\0';
100 t--;
101 while((t >= buffer) && isspace(*t))
103 *t = '\0';
104 t--;
108 /* skip leading whitespaces */
109 name = buffer;
110 while(isspace(*name))
111 name++;
113 /* check for comments or empty lines */
114 if(name[0] == '#' || name[0] == '\0') continue;
116 /* check for UPnP permissions rule */
117 if(0 == memcmp(name, "allow", 5) || 0 == memcmp(name, "deny", 4))
119 upnppermlist = realloc(upnppermlist,
120 sizeof(struct upnpperm) * (num_upnpperm+1));
121 /* parse the rule */
122 if(read_permission_line(upnppermlist + num_upnpperm, name) >= 0)
124 num_upnpperm++;
126 else
128 fprintf(stderr, "parsing error file %s line %d : %s\n",
129 fname, linenum, name);
131 continue;
133 if(!(equals = strchr(name, '=')))
135 fprintf(stderr, "parsing error file %s line %d : %s\n",
136 fname, linenum, name);
137 continue;
140 /* remove ending whitespaces */
141 for(t=equals-1; t>name && isspace(*t); t--)
142 *t = '\0';
144 *equals = '\0';
145 value = equals+1;
147 /* skip leading whitespaces */
148 while(isspace(*value))
149 value++;
151 id = UPNP_INVALID;
152 for(i=0; i<sizeof(optionids)/sizeof(optionids[0]); i++)
154 /*printf("%2d %2d %s %s\n", i, optionids[i].id, name,
155 optionids[i].name); */
157 if(0 == strcmp(name, optionids[i].name))
159 id = optionids[i].id;
160 break;
164 if(id == UPNP_INVALID)
166 fprintf(stderr, "parsing error file %s line %d : %s=%s\n",
167 fname, linenum, name, value);
169 else
171 num_options += 1;
172 ary_options = (struct option *) realloc(ary_options, num_options * sizeof(struct option));
174 ary_options[num_options-1].id = id;
175 strncpy(ary_options[num_options-1].value, value, MAX_OPTION_VALUE_LEN);
180 fclose(hfile);
182 return 0;
185 void
186 freeoptions(void)
188 if(ary_options)
190 free(ary_options);
191 ary_options = NULL;
192 num_options = 0;