1 /* $Id: options.c,v 1.26 2012/06/29 19:26:09 nanard Exp $ */
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
5 * (c) 2006-2012 Thomas Bernard
6 * This software is subject to the conditions detailed
7 * in the LICENCE file provided within the distribution */
16 #include "upnppermissions.h"
17 #include "upnpglobalvars.h"
19 #ifndef DISABLE_CONFIG_FILE
20 struct option
* ary_options
= NULL
;
21 static char * string_repo
= NULL
;
22 unsigned int num_options
= 0;
25 enum upnpconfigoptions id
;
28 { UPNPEXT_IFNAME
, "ext_ifname" },
29 { UPNPEXT_IP
, "ext_ip" },
30 { UPNPLISTENING_IP
, "listening_ip" },
32 { UPNPBITRATE_UP
, "bitrate_up" },
33 { UPNPBITRATE_DOWN
, "bitrate_down" },
34 { UPNPPRESENTATIONURL
, "presentation_url" },
35 { UPNPFRIENDLY_NAME
, "friendly_name" },
36 { UPNPNOTIFY_INTERVAL
, "notify_interval" },
37 { UPNPSYSTEM_UPTIME
, "system_uptime" },
38 { UPNPPACKET_LOG
, "packet_log" },
40 { UPNPSERIAL
, "serial"},
41 { UPNPMODEL_NUMBER
, "model_number"},
42 { UPNPCLEANTHRESHOLD
, "clean_ruleset_threshold"},
43 { UPNPCLEANINTERVAL
, "clean_ruleset_interval"},
45 { UPNPFORWARDCHAIN
, "upnp_forward_chain"},
46 { UPNPNATCHAIN
, "upnp_nat_chain"},
49 { UPNPENABLENATPMP
, "enable_natpmp"},
51 { UPNPENABLE
, "enable_upnp"},
53 { UPNPANCHOR
, "anchor"},
54 { UPNPQUEUE
, "queue"},
57 #ifdef PF_ENABLE_FILTER_RULES
58 { UPNPQUICKRULES
, "quickrules" },
60 #ifdef ENABLE_LEASEFILE
61 { UPNPLEASEFILE
, "lease_file"},
63 { UPNPMINISSDPDSOCKET
, "minissdpdsocket"},
64 { UPNPSECUREMODE
, "secure_mode"}
68 readoptionsfile(const char * fname
)
78 enum upnpconfigoptions id
;
79 size_t string_repo_len
= 0;
83 if(!fname
|| (strlen(fname
) == 0))
86 memset(buffer
, 0, sizeof(buffer
));
89 printf("Reading configuration from file %s\n", fname
);
92 if(!(hfile
= fopen(fname
, "r")))
95 if(ary_options
!= NULL
)
101 while(fgets(buffer
, sizeof(buffer
), hfile
))
104 t
= strchr(buffer
, '\n');
109 /* remove spaces at the end of the line */
110 while((t
>= buffer
) && isspace(*t
))
117 /* skip leading whitespaces */
119 while(isspace(*name
))
122 /* check for comments or empty lines */
123 if(name
[0] == '#' || name
[0] == '\0') continue;
125 /* check for UPnP permissions rule */
126 if(0 == memcmp(name
, "allow", 5) || 0 == memcmp(name
, "deny", 4))
128 tmp
= realloc(upnppermlist
, sizeof(struct upnpperm
) * (num_upnpperm
+1));
131 fprintf(stderr
, "memory allocation error. Permission line in file %s line %d\n",
138 if(read_permission_line(upnppermlist
+ num_upnpperm
, name
) >= 0)
144 fprintf(stderr
, "parsing error file %s line %d : %s\n",
145 fname
, linenum
, name
);
150 if(!(equals
= strchr(name
, '=')))
152 fprintf(stderr
, "parsing error file %s line %d : %s\n",
153 fname
, linenum
, name
);
157 /* remove ending whitespaces */
158 for(t
=equals
-1; t
>name
&& isspace(*t
); t
--)
164 /* skip leading whitespaces */
165 while(isspace(*value
))
169 for(i
=0; i
<sizeof(optionids
)/sizeof(optionids
[0]); i
++)
171 /*printf("%2d %2d %s %s\n", i, optionids[i].id, name,
172 optionids[i].name); */
174 if(0 == strcmp(name
, optionids
[i
].name
))
176 id
= optionids
[i
].id
;
181 if(id
== UPNP_INVALID
)
183 fprintf(stderr
, "invalid option in file %s line %d : %s=%s\n",
184 fname
, linenum
, name
, value
);
188 tmp
= realloc(ary_options
, (num_options
+ 1) * sizeof(struct option
));
191 fprintf(stderr
, "memory allocation error. Option in file %s line %d.\n",
197 len
= strlen(value
) + 1; /* +1 for terminating '\0' */
198 tmp
= realloc(string_repo
, string_repo_len
+ len
);
201 fprintf(stderr
, "memory allocation error, Option value in file %s line %d : %s=%s\n",
202 fname
, linenum
, name
, value
);
207 memcpy(string_repo
+ string_repo_len
, value
, len
);
208 ary_options
[num_options
].id
= id
;
209 /* save the offset instead of the absolute address because realloc() could
211 ary_options
[num_options
].value
= (const char *)string_repo_len
;
213 string_repo_len
+= len
;
222 for(i
= 0; i
< num_options
; i
++)
224 /* add start address of string_repo to get right pointer */
225 ary_options
[i
].value
= string_repo
+ (size_t)ary_options
[i
].value
;
253 #endif /* DISABLE_CONFIG_FILE */