1/* MiniUPnP project
2 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
3 * author: Ryan Wagoner
4 *
5 * Copyright (c) 2006, Thomas Bernard
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in the
14 *       documentation and/or other materials provided with the distribution.
15 *     * The name of the author may not 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"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <ctype.h>
34#include "options.h"
35#include "upnpglobalvars.h"
36
37struct option * ary_options = NULL;
38int num_options = 0;
39
40static const struct {
41	enum upnpconfigoptions id;
42	const char * name;
43} optionids[] = {
44	{ UPNPIFNAME, "network_interface" },
45	{ UPNPLISTENING_IP, "listening_ip" },
46	{ UPNPPORT, "port" },
47	{ UPNPPRESENTATIONURL, "presentation_url" },
48	{ UPNPNOTIFY_INTERVAL, "notify_interval" },
49	{ UPNPSYSTEM_UPTIME, "system_uptime" },
50	{ UPNPUUID, "uuid"},
51	{ UPNPSERIAL, "serial"},
52	{ UPNPMODEL_NAME, "model_name"},
53	{ UPNPMODEL_NUMBER, "model_number"},
54	{ UPNPFRIENDLYNAME, "friendly_name"},
55	{ UPNPMEDIADIR, "media_dir"},
56	{ UPNPALBUMART_NAMES, "album_art_names"},
57	{ UPNPINOTIFY, "inotify" },
58	{ UPNPDBDIR, "db_dir" },
59	{ UPNPLOGDIR, "log_dir" },
60	{ UPNPLOGLEVEL, "log_level" },
61	{ UPNPMINISSDPDSOCKET, "minissdpdsocket"},
62	{ ENABLE_TIVO, "enable_tivo" },
63	{ ENABLE_DLNA_STRICT, "strict_dlna" },
64	{ ROOT_CONTAINER, "root_container" }
65};
66
67int
68readoptionsfile(const char * fname)
69{
70	FILE *hfile = NULL;
71	char buffer[1024];
72	char *equals;
73	char *name;
74	char *value;
75	char *t;
76	int linenum = 0;
77	int i;
78	enum upnpconfigoptions id;
79
80	if(!fname || (strlen(fname) == 0))
81		return -1;
82
83	memset(buffer, 0, sizeof(buffer));
84
85#ifdef DEBUG
86	printf("Reading configuration from file %s\n", fname);
87#endif
88
89	if(!(hfile = fopen(fname, "r")))
90		return -1;
91
92	if(ary_options != NULL)
93	{
94		free(ary_options);
95		num_options = 0;
96	}
97
98	while(fgets(buffer, sizeof(buffer), hfile))
99	{
100		linenum++;
101		t = strchr(buffer, '\n');
102		if(t)
103		{
104			*t = '\0';
105			t--;
106			while((t >= buffer) && isspace(*t))
107			{
108				*t = '\0';
109				t--;
110			}
111		}
112
113		/* skip leading whitespaces */
114		name = buffer;
115		while(isspace(*name))
116			name++;
117
118		/* check for comments or empty lines */
119		if(name[0] == '#' || name[0] == '\0') continue;
120
121		if(!(equals = strchr(name, '=')))
122		{
123			fprintf(stderr, "parsing error file %s line %d : %s\n",
124			        fname, linenum, name);
125			continue;
126		}
127
128		/* remove ending whitespaces */
129		for(t=equals-1; t>name && isspace(*t); t--)
130			*t = '\0';
131
132		*equals = '\0';
133		value = equals+1;
134
135		/* skip leading whitespaces */
136		while(isspace(*value))
137			value++;
138
139		id = UPNP_INVALID;
140		for(i=0; i<sizeof(optionids)/sizeof(optionids[0]); i++)
141		{
142			/*printf("%2d %2d %s %s\n", i, optionids[i].id, name,
143			       optionids[i].name); */
144
145			if(0 == strcmp(name, optionids[i].name))
146			{
147				id = optionids[i].id;
148				break;
149			}
150		}
151
152		if(id == UPNP_INVALID)
153		{
154			fprintf(stderr, "parsing error file %s line %d : %s=%s\n",
155			        fname, linenum, name, value);
156		}
157		else
158		{
159			num_options += 1;
160			ary_options = (struct option *) realloc(ary_options, num_options * sizeof(struct option));
161
162			ary_options[num_options-1].id = id;
163			strncpy(ary_options[num_options-1].value, value, MAX_OPTION_VALUE_LEN);
164		}
165
166	}
167
168	fclose(hfile);
169
170	return 0;
171}
172
173void
174freeoptions(void)
175{
176	if(ary_options)
177	{
178		free(ary_options);
179		ary_options = NULL;
180		num_options = 0;
181	}
182}
183
184