This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
forked from julienblitte/tiny-ssdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
298 lines (249 loc) · 7.56 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <syslog.h>
#include <stdbool.h>
#include "config.h"
#include "logfilter.h"
#define MAX_LINE 512
ssdp_param_set configuration;
const char *config_dummy_uuid()
{
static char device_uuid[MAX_PARAM];
if (device_uuid[0])
return device_uuid;
srand(time(NULL));
snprintf(device_uuid, MAX_PARAM, "%08x-%08x-%08x-%08x", rand(), rand(), rand(), rand());
return device_uuid;
}
void config_init()
{
int i;
char expand_set_default[MAX_PARAM];
configuration.network_port = DEFAULT_INT_NETWORK_PORT;
strncpy(configuration.network_multicast, DEFAULT_STR_NETWORK_MULTICAST, MAX_PARAM);
strncpy(configuration.network_interface, DEFAULT_STR_NETWORK_INTERFACE, MAX_PARAM);
strncpy(configuration.device_description, DEFAULT_STR_DEVICE_DESCRIPTION, MAX_PARAM);
strncpy(configuration.device_type_name, DEFAULT_STR_DEVICE_TYPE_NAME, MAX_PARAM);
configuration.device_type_version = DEFAULT_INT_DEVICE_TYPE_VERSION;
strncpy(configuration.device_uuid, config_dummy_uuid(), MAX_PARAM);
configuration.base_url_port = DEFAULT_INT_BASE_URL_PORT;
strncpy(configuration.base_url_resource, DEFAULT_STR_BASE_URL_RESOURCE, MAX_PARAM);
configuration.service_count = DEFAULT_INT_SERVICE_COUNT;
for(i=0; i < configuration.service_count; i++)
{
snprintf(expand_set_default, sizeof(expand_set_default), DEFAULT_SET_STR_SERVICE_NAME, i);
configuration.service_name[i] = strdup(expand_set_default); // no memory reserved by default
snprintf(expand_set_default, sizeof(expand_set_default), DEFAULT_SET_INT_SERVICE_VERSION, i);
str2int(expand_set_default, &configuration.service_version[i]);
}
configuration.ssdp_allowed_cache = DEFAULT_INT_SSDP_ALLOWED_CACHE;
}
char *strtrim(char *s)
{
char *start, *end;
if (!*s)
return s;
start = s;
end = s;
while(*(end+1)) end++;
/* Trim and delimit right side */
while ((isspace(*end)) && (end >= start)) end--;
*(end+1) = '\0';
/* Trim left side */
while ((isspace(*start)) && (start < end)) start++;
strcpy(s, start);
return s;
}
char *strcomment(char *s)
{
char *p;
for(p = s; *p != '\0'; p++)
{
if (*p == '#')
{
*p = '\0';
break;
}
}
return s;
}
int str2int(char *s, int *value)
{
char *start;
for(start = s; *start; start++)
{
if (*start < '0' || *start > '9')
return *value;
}
*value = atoi(s);
return *value;
}
void config_create()
{
FILE *f;
int i;
char expand_set_variable[MAX_PARAM];
f = fopen (CONFIG_FILE, "wt");
if (!f)
{
writelog(LOG_WARNING, "Cannot create configuration file '%s'", CONFIG_FILE);
return;
}
fprintf(f, "%s=%d\n", VAR_INT_NETWORK_PORT, configuration.network_port); //int
fprintf(f, "%s=%s\n", VAR_STR_NETWORK_MULTICAST, configuration.network_multicast);
fprintf(f, "%s=%s\n", VAR_STR_NETWORK_INTERFACE, configuration.network_interface);
fprintf(f, "\n");
fprintf(f, "%s=%s\n", VAR_STR_DEVICE_DESCRIPTION, configuration.device_description);
fprintf(f, "%s=%s\n", VAR_STR_DEVICE_TYPE_NAME, configuration.device_type_name);
fprintf(f, "%s=%d\n", VAR_INT_DEVICE_TYPE_VERSION, configuration.device_type_version); //int
fprintf(f, "%s=%s\n", VAR_STR_DEVICE_UUID, configuration.device_uuid); // random value
fprintf(f, "\n");
fprintf(f, "%s=%d\n", VAR_INT_BASE_URL_PORT, configuration.base_url_port); //int
fprintf(f, "%s=%s\n", VAR_STR_BASE_URL_RESOURCE, configuration.base_url_resource);
fprintf(f, "\n");
fprintf(f, "%s=%d\n", VAR_INT_SERVICE_COUNT, configuration.service_count); //int
for(i=0; i < configuration.service_count; i++)
{
snprintf(expand_set_variable, sizeof(expand_set_variable), VAR_SET_STR_SERVICE_NAME, i);
fprintf(f, "%s=%s\n", expand_set_variable, configuration.service_name[i]);
snprintf(expand_set_variable, sizeof(expand_set_variable), VAR_SET_INT_SERVICE_VERSION, i);
fprintf(f, "%s=%d\n", expand_set_variable, configuration.service_version[i]);
}
fprintf(f, "\n");
fprintf(f, "%s=%d\n", VAR_INT_SSDP_ALLOWED_CACHE, configuration.ssdp_allowed_cache); //int
fprintf(f, "\n");
fprintf(f, "%s=%d\n", VAR_INT_SYSLOG_LEVEL, syslog_filter); //int, global variable syslog_level
/* values for web services part */
fprintf(f, "\n");
fprintf(f, "%s", VARS_WEBSERVICES);
fclose(f);
}
void config_load()
{
char *s, line[MAX_LINE];
char name[MAX_PARAM], value[MAX_PARAM];
FILE *f;
int nline;
char service_name[MAX_PARAM];
bool handled;
int i;
config_init();
f = fopen (CONFIG_FILE, "rt");
if (!f)
{
writelog(LOG_NOTICE, "Configuration file '%s' not readable, creating default one...", CONFIG_FILE);
config_create();
return;
}
nline=0;
while ((s = fgets(line, sizeof(line), f)) != NULL)
{
nline++;
strcomment(s);
s = strtok(line, "=");
if (!s)
continue;
strncpy(name, s, sizeof(name));
strtrim(name);
if (strlen(name) == 0) /* empty line */
continue;
s = strtok(NULL, "=");
if (!s)
{
writelog(LOG_WARNING, "Error at '%s':%d.", CONFIG_FILE, nline);
continue;
}
strncpy(value, s, sizeof(value));
strtrim(value);
handled = false;
if (!handled && strcasecmp(name, VAR_INT_NETWORK_PORT) == 0)
{
str2int(value, &configuration.network_port);
handled = true;
}
if (!handled && strcasecmp(name, VAR_STR_NETWORK_MULTICAST)==0)
{
strncpy(configuration.network_multicast, value, MAX_PARAM);
handled = true;
}
if (!handled && strcasecmp(name, VAR_STR_NETWORK_INTERFACE) == 0)
{
strncpy(configuration.network_interface, value, MAX_PARAM);
handled = true;
}
if (!handled && strcasecmp(name, VAR_STR_DEVICE_DESCRIPTION) == 0)
{
strncpy(configuration.device_description, value, MAX_PARAM);
handled = true;
}
if (!handled && strcasecmp(name, VAR_STR_DEVICE_TYPE_NAME) == 0)
{
strncpy(configuration.device_type_name, value, MAX_PARAM);
handled = true;
}
if (!handled && strcasecmp(name, VAR_INT_DEVICE_TYPE_VERSION) == 0)
{
str2int(value, &configuration.device_type_version);
handled = true;
}
if (!handled && strcasecmp(name, VAR_STR_DEVICE_UUID) == 0)
{
strncpy(configuration.device_uuid, value, MAX_PARAM);
handled = true;
}
if (!handled && strcasecmp(name, VAR_INT_BASE_URL_PORT) == 0)
{
str2int(value, &configuration.base_url_port);
handled = true;
}
if (!handled && strcasecmp(name, VAR_STR_BASE_URL_RESOURCE) == 0)
{
strncpy(configuration.base_url_resource, value, MAX_PARAM);
handled = true;
}
if (!handled && strcasecmp(name, VAR_INT_SERVICE_COUNT) == 0)
{
str2int(value, &configuration.service_count);
if (configuration.service_count > MAX_SERVICE)
configuration.service_count = MAX_SERVICE;
handled = true;
}
if (!handled)
{
for(i=0; i < MAX_SERVICE; i++)
{
snprintf(service_name, sizeof(service_name), VAR_SET_STR_SERVICE_NAME, i);
if (strcasecmp(name, service_name) == 0)
{
// no space allocated by default to service_name
configuration.service_name[i] = strdup(value);
handled = true;
break;
}
snprintf(service_name, sizeof(service_name), VAR_SET_INT_SERVICE_VERSION, i);
if (strcasecmp(name, service_name) == 0)
{
str2int(value, &(configuration.service_version[i]));
handled = true;
break;
}
}
}
if (!handled && strcasecmp(name, VAR_INT_SSDP_ALLOWED_CACHE) == 0)
{
str2int(value, &configuration.ssdp_allowed_cache); //int
handled = true;
}
if (!handled && strcasecmp(name, VAR_INT_SYSLOG_LEVEL) == 0)
{
str2int(value, &syslog_filter); /* store in global syslog_level, not in configuration structure */
handled = true;
}
if (!handled)
writelog(LOG_DEBUG, "Ingored configuration statement at '%s':%d.", CONFIG_FILE, nline);
}
fclose (f);
}