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 | |
---|
37 | struct option * ary_options = NULL; |
---|
38 | int num_options = 0; |
---|
39 | |
---|
40 | static 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 | { UPNPMINISSDPDSOCKET, "minissdpdsocket"}, |
---|
61 | { ENABLE_TIVO, "enable_tivo" }, |
---|
62 | { ENABLE_DLNA_STRICT, "strict_dlna" }, |
---|
63 | { ROOT_CONTAINER, "root_container" } |
---|
64 | }; |
---|
65 | |
---|
66 | int |
---|
67 | readoptionsfile(const char * fname) |
---|
68 | { |
---|
69 | FILE *hfile = NULL; |
---|
70 | char buffer[1024]; |
---|
71 | char *equals; |
---|
72 | char *name; |
---|
73 | char *value; |
---|
74 | char *t; |
---|
75 | int linenum = 0; |
---|
76 | int i; |
---|
77 | enum upnpconfigoptions id; |
---|
78 | |
---|
79 | if(!fname || (strlen(fname) == 0)) |
---|
80 | return -1; |
---|
81 | |
---|
82 | memset(buffer, 0, sizeof(buffer)); |
---|
83 | |
---|
84 | #ifdef DEBUG |
---|
85 | printf("Reading configuration from file %s\n", fname); |
---|
86 | #endif |
---|
87 | |
---|
88 | if(!(hfile = fopen(fname, "r"))) |
---|
89 | return -1; |
---|
90 | |
---|
91 | if(ary_options != NULL) |
---|
92 | { |
---|
93 | free(ary_options); |
---|
94 | num_options = 0; |
---|
95 | } |
---|
96 | |
---|
97 | while(fgets(buffer, sizeof(buffer), hfile)) |
---|
98 | { |
---|
99 | linenum++; |
---|
100 | t = strchr(buffer, '\n'); |
---|
101 | if(t) |
---|
102 | { |
---|
103 | *t = '\0'; |
---|
104 | t--; |
---|
105 | while((t >= buffer) && isspace(*t)) |
---|
106 | { |
---|
107 | *t = '\0'; |
---|
108 | t--; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | /* skip leading whitespaces */ |
---|
113 | name = buffer; |
---|
114 | while(isspace(*name)) |
---|
115 | name++; |
---|
116 | |
---|
117 | /* check for comments or empty lines */ |
---|
118 | if(name[0] == '#' || name[0] == '\0') continue; |
---|
119 | |
---|
120 | if(!(equals = strchr(name, '='))) |
---|
121 | { |
---|
122 | fprintf(stderr, "parsing error file %s line %d : %s\n", |
---|
123 | fname, linenum, name); |
---|
124 | continue; |
---|
125 | } |
---|
126 | |
---|
127 | /* remove ending whitespaces */ |
---|
128 | for(t=equals-1; t>name && isspace(*t); t--) |
---|
129 | *t = '\0'; |
---|
130 | |
---|
131 | *equals = '\0'; |
---|
132 | value = equals+1; |
---|
133 | |
---|
134 | /* skip leading whitespaces */ |
---|
135 | while(isspace(*value)) |
---|
136 | value++; |
---|
137 | |
---|
138 | id = UPNP_INVALID; |
---|
139 | for(i=0; i<sizeof(optionids)/sizeof(optionids[0]); i++) |
---|
140 | { |
---|
141 | /*printf("%2d %2d %s %s\n", i, optionids[i].id, name, |
---|
142 | optionids[i].name); */ |
---|
143 | |
---|
144 | if(0 == strcmp(name, optionids[i].name)) |
---|
145 | { |
---|
146 | id = optionids[i].id; |
---|
147 | break; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | if(id == UPNP_INVALID) |
---|
152 | { |
---|
153 | fprintf(stderr, "parsing error file %s line %d : %s=%s\n", |
---|
154 | fname, linenum, name, value); |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | num_options += 1; |
---|
159 | ary_options = (struct option *) realloc(ary_options, num_options * sizeof(struct option)); |
---|
160 | |
---|
161 | ary_options[num_options-1].id = id; |
---|
162 | strncpy(ary_options[num_options-1].value, value, MAX_OPTION_VALUE_LEN); |
---|
163 | } |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | fclose(hfile); |
---|
168 | |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | void |
---|
173 | freeoptions(void) |
---|
174 | { |
---|
175 | if(ary_options) |
---|
176 | { |
---|
177 | free(ary_options); |
---|
178 | ary_options = NULL; |
---|
179 | num_options = 0; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|