cfgfile_xml.c 22.6 KB
Newer Older
1
/*
Moritz Grimm's avatar
Moritz Grimm committed
2
 * Copyright (c) 2015, 2018 Moritz Grimm <mgrimm@mrsserver.net>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

21
22
23
#include <stdio.h>
#include <string.h>

24
#include "cfg.h"
25
#include "cfgfile_xml.h"
26
27
28
29
30
#include "log.h"
#include "xalloc.h"

#include <libxml/parser.h>

Moritz Grimm's avatar
Moritz Grimm committed
31
32
#define CFGFILE_XML_NAME	"ezstream"

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
static int	_cfgfile_xml_parse_server(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_servers(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_stream(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_streams(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_intake(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_intakes(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_metadata(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_decoder(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_decoders(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_encoder(xmlDocPtr, xmlNodePtr);
static int	_cfgfile_xml_parse_encoders(xmlDocPtr, xmlNodePtr);
static void	_cfgfile_xml_print_server(cfg_server_t, void *);
static void	_cfgfile_xml_print_stream(cfg_stream_t, void *);
static void	_cfgfile_xml_print_intake(cfg_intake_t, void *);
static void	_cfgfile_xml_print_decoder_ext(const char *, void *);
static void	_cfgfile_xml_print_decoder(cfg_decoder_t, void *);
static void	_cfgfile_xml_print_encoder(cfg_encoder_t, void *);
50

51
52
#define XML_CHAR(s)	(const xmlChar *)(s)
#define STD_CHAR(s)	(const char *)(s)
53

54
#define XML_SERVER_SET(c, l, f, e)	do {				\
55
56
57
	if (0 == xmlStrcasecmp(cur->name, XML_CHAR((e)))) {		\
		xmlChar 	*val;					\
		const char	*err_str;				\
58
		int		 error = 0;				\
59
60
									\
		val = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); \
61
		if (0 > (f)((c), (l), STD_CHAR(val), &err_str)) {	\
62
63
64
			log_error("%s[%ld]: server (%s): %s: %s",	\
			    doc->name, xmlGetLineNo(cur),		\
			    cfg_server_get_name((c)), (e), err_str);	\
65
66
67
			error = 1;					\
		}							\
		xmlFree(val);						\
68
69
		if (error)						\
			return (-1);					\
70
71
72
73
	}								\
} while (0)

static int
74
_cfgfile_xml_parse_server(xmlDocPtr doc, xmlNodePtr cur)
75
{
76
77
78
79
80
81
82
	cfg_server_list_t	 sl;
	cfg_server_t		 s;
	const char		*errstr;
	long int		 line_no = xmlGetLineNo(cur);

	sl = cfg_get_servers();
	s = cfg_server_list_get(sl, CFG_DEFAULT);
83
84

	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
85
86
87
88
89
90
91
92
93
94
95
96
		XML_SERVER_SET(s, sl, cfg_server_set_name,               "name");
		XML_SERVER_SET(s, sl, cfg_server_set_protocol,           "protocol");
		XML_SERVER_SET(s, sl, cfg_server_set_hostname,           "hostname");
		XML_SERVER_SET(s, sl, cfg_server_set_port,               "port");
		XML_SERVER_SET(s, sl, cfg_server_set_user,               "user");
		XML_SERVER_SET(s, sl, cfg_server_set_password,           "password");
		XML_SERVER_SET(s, sl, cfg_server_set_reconnect_attempts, "reconnect_attempts");
		XML_SERVER_SET(s, sl, cfg_server_set_tls,                "tls");
		XML_SERVER_SET(s, sl, cfg_server_set_tls_cipher_suite,   "tls_cipher_suite");
		XML_SERVER_SET(s, sl, cfg_server_set_ca_dir,             "ca_dir");
		XML_SERVER_SET(s, sl, cfg_server_set_ca_file,            "ca_file");
		XML_SERVER_SET(s, sl, cfg_server_set_client_cert,        "client_cert");
97
98
	}

99
100
101
	if (0 > cfg_server_validate(s, &errstr)) {
		log_error("%s[%ld]: server (%s): %s", doc->name, line_no,
		    cfg_server_get_name(s), errstr);
102
		return (-1);
103
	}
104
105
106
107

	return (0);
}

108
static int
109
_cfgfile_xml_parse_servers(xmlDocPtr doc, xmlNodePtr cur)
110
111
112
{
	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("server")) &&
113
		    0 > _cfgfile_xml_parse_server(doc, cur))
114
115
116
117
118
119
120
121
122
123
124
125
126
			return (-1);
	}

	return (0);
}

#define XML_STREAM_SET(c, l, f, e)	do {				\
	if (0 == xmlStrcasecmp(cur->name, XML_CHAR((e)))) {		\
		xmlChar 	*val;					\
		const char	*err_str;				\
		int		 error = 0;				\
									\
		val = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); \
127
		if (0 > (f)((c), (l), STD_CHAR(val), &err_str)) {	\
128
129
130
131
132
133
134
135
136
137
138
			log_error("%s[%ld]: stream (%s): %s: %s",	\
			    doc->name, xmlGetLineNo(cur),		\
			    cfg_stream_get_name((c)), (e), err_str);	\
			error = 1;					\
		}							\
		xmlFree(val);						\
		if (error)						\
			return (-1);					\
	}								\
} while (0)

139
static int
140
_cfgfile_xml_parse_stream(xmlDocPtr doc, xmlNodePtr cur)
141
{
142
143
144
145
146
147
148
	cfg_stream_list_t	 sl;
	cfg_stream_t		 s;
	const char		*errstr;
	long int		 line_no = xmlGetLineNo(cur);

	sl = cfg_get_streams();
	s = cfg_stream_list_get(sl, CFG_DEFAULT);
149
150

	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
151
152
		XML_STREAM_SET(s, sl, cfg_stream_set_name,               "name");
		XML_STREAM_SET(s, sl, cfg_stream_set_mountpoint,         "mountpoint");
153
		XML_STREAM_SET(s, sl, cfg_stream_set_intake,             "intake");
154
155
156
157
		XML_STREAM_SET(s, sl, cfg_stream_set_server,             "server");
		XML_STREAM_SET(s, sl, cfg_stream_set_public,             "public");
		XML_STREAM_SET(s, sl, cfg_stream_set_format,             "format");
		XML_STREAM_SET(s, sl, cfg_stream_set_encoder,            "encoder");
Moritz Grimm's avatar
Moritz Grimm committed
158
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_name,        "stream_name");
159
160
161
162
163
164
165
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_url,         "stream_url");
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_genre,       "stream_genre");
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_description, "stream_description");
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_quality,     "stream_quality");
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_bitrate,     "stream_bitrate");
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_samplerate,  "stream_samplerate");
		XML_STREAM_SET(s, sl, cfg_stream_set_stream_channels,    "stream_channels");
166
167
	}

168
169
170
	if (0 > cfg_stream_validate(s, &errstr)) {
		log_error("%s[%ld]: stream (%s): %s", doc->name, line_no,
		    cfg_stream_get_name(s), errstr);
171
		return (-1);
172
	}
173
174
175
176

	return (0);
}

177
static int
178
_cfgfile_xml_parse_streams(xmlDocPtr doc, xmlNodePtr cur)
179
180
181
{
	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("stream")) &&
182
		    0 > _cfgfile_xml_parse_stream(doc, cur))
183
184
185
186
187
188
			return (-1);
	}

	return (0);
}

189
#define XML_INPUT_SET(c, l, f, e)	do {				\
190
191
192
	if (0 == xmlStrcasecmp(cur->name, XML_CHAR((e)))) {		\
		xmlChar 	*val;					\
		const char	*err_str;				\
193
		int		 error = 0;				\
194
195
									\
		val = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); \
196
197
198
199
		if (0 > (f)((c), (l), STD_CHAR(val), &err_str)) {	\
			log_error("%s[%ld]: intake (%s): %s: %s",	\
			    doc->name, xmlGetLineNo(cur),		\
			    cfg_intake_get_name((c)), (e), err_str);	\
200
201
202
			error = 1;					\
		}							\
		xmlFree(val);						\
203
204
		if (error)						\
			return (-1);					\
205
206
207
	}								\
} while (0)

208
static int
209
_cfgfile_xml_parse_intake(xmlDocPtr doc, xmlNodePtr cur)
210
{
211
212
213
214
215
216
217
	cfg_intake_list_t	 il;
	cfg_intake_t		 i;
	const char		*errstr;
	long int		 line_no = xmlGetLineNo(cur);

	il = cfg_get_intakes();
	i = cfg_intake_list_get(il, CFG_DEFAULT);
218
219

	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
220
221
222
223
224
		XML_INPUT_SET(i, il, cfg_intake_set_name,        "name");
		XML_INPUT_SET(i, il, cfg_intake_set_type,        "type");
		XML_INPUT_SET(i, il, cfg_intake_set_filename,    "filename");
		XML_INPUT_SET(i, il, cfg_intake_set_shuffle,     "shuffle");
		XML_INPUT_SET(i, il, cfg_intake_set_stream_once, "stream_once");
225
226
	}

227
228
229
	if (0 > cfg_intake_validate(i, &errstr)) {
		log_error("%s[%ld]: intake (%s): %s", doc->name, line_no,
		    cfg_intake_get_name(i), errstr);
230
		return (-1);
231
	}
232
233
234
235

	return (0);
}

236
static int
237
_cfgfile_xml_parse_intakes(xmlDocPtr doc, xmlNodePtr cur)
238
239
240
{
	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("intake")) &&
241
		    0 > _cfgfile_xml_parse_intake(doc, cur))
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
			return (-1);
	}

	return (0);
}

#define XML_STRCONFIG(s, f, e)	do {					\
	if (0 == xmlStrcasecmp(cur->name, XML_CHAR((e)))) {		\
		xmlChar 	*val;					\
		const char	*err_str;				\
									\
		val = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); \
		if (0 > (f)(STD_CHAR(val), &err_str)) {			\
			log_error("%s[%ld]: %s: %s: %s", doc->name,	\
			    xmlGetLineNo(cur), (s), (e), err_str);	\
			error = 1;					\
		}							\
		xmlFree(val);						\
	}								\
} while (0)

263
static int
264
_cfgfile_xml_parse_metadata(xmlDocPtr doc, xmlNodePtr cur)
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{
	int	error = 0;

	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
		XML_STRCONFIG("metadata", cfg_set_metadata_program,    "program");
		XML_STRCONFIG("metadata", cfg_set_metadata_format_str, "format_str");
		XML_STRCONFIG("metadata", cfg_set_metadata_refresh_interval,
								       "refresh_interval");
		XML_STRCONFIG("metadata", cfg_set_metadata_normalize_strings,
								       "normalize_strings");
		XML_STRCONFIG("metadata", cfg_set_metadata_no_updates, "no_updates");
	}

	if (error)
		return (-1);

	return (0);
}

284
#define XML_DECODER_SET(c, l, f, e)	do {				\
285
286
287
288
289
290
	if (0 == xmlStrcasecmp(cur->name, XML_CHAR((e)))) {		\
		xmlChar 	*val;					\
		const char	*err_str;				\
		int		 error = 0;				\
									\
		val = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); \
291
		if (0 > (f)((c), (l), STD_CHAR(val), &err_str)) {	\
292
			log_error("%s[%ld]: decoder (%s): %s: %s",	\
293
294
295
296
297
298
299
300
301
302
303
			    doc->name, xmlGetLineNo(cur),		\
			    cfg_decoder_get_name((c)), (e), err_str);	\
			error = 1;					\
		}							\
		xmlFree(val);						\
		if (error)						\
			return (-1);					\
	}								\
} while (0)

static int
304
_cfgfile_xml_parse_decoder(xmlDocPtr doc, xmlNodePtr cur)
305
{
306
307
308
309
	cfg_decoder_list_t	 dl;
	cfg_decoder_t		 d;
	const char		*errstr;
	long int		 line_no = xmlGetLineNo(cur);
310

311
312
	dl = cfg_get_decoders();
	d = cfg_decoder_list_get(dl, CFG_DEFAULT);
313
314

	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
315
316
317
		XML_DECODER_SET(d, dl, cfg_decoder_set_name,    "name");
		XML_DECODER_SET(d, dl, cfg_decoder_set_program, "program");
		XML_DECODER_SET(d, dl, cfg_decoder_add_match,   "file_ext");
318
319
320
	}

	if (0 > cfg_decoder_validate(d, &errstr)) {
321
322
		log_error("%s[%ld]: decoder (%s): %s", doc->name, line_no,
		    cfg_decoder_get_name(d), errstr);
323
324
325
326
327
328
329
		return (-1);
	}

	return (0);
}

static int
330
_cfgfile_xml_parse_decoders(xmlDocPtr doc, xmlNodePtr cur)
331
332
333
{
	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("decoder")) &&
334
		    0 > _cfgfile_xml_parse_decoder(doc, cur))
335
336
337
338
339
340
			return (-1);
	}

	return (0);
}

341
#define XML_ENCODER_SET(c, l, f, e)	do {				\
342
343
344
345
346
347
	if (0 == xmlStrcasecmp(cur->name, XML_CHAR((e)))) {		\
		xmlChar 	*val;					\
		const char	*err_str;				\
		int		 error = 0;				\
									\
		val = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); \
348
		if (0 > (f)((c), (l), STD_CHAR(val), &err_str)) {	\
349
			log_error("%s[%ld]: encoder (%s): %s: %s",	\
350
351
352
353
354
355
356
357
358
359
360
			    doc->name, xmlGetLineNo(cur),		\
			    cfg_encoder_get_name((c)), (e), err_str);	\
			error = 1;					\
		}							\
		xmlFree(val);						\
		if (error)						\
			return (-1);					\
	}								\
} while (0)

static int
361
_cfgfile_xml_parse_encoder(xmlDocPtr doc, xmlNodePtr cur)
362
{
363
364
365
366
	cfg_encoder_list_t	 el;
	cfg_encoder_t		 e;
	const char		*errstr;
	long int		 line_no = xmlGetLineNo(cur);
367

368
369
	el = cfg_get_encoders();
	e = cfg_encoder_list_get(el, CFG_DEFAULT);
370
371

	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
372
373
374
		XML_ENCODER_SET(e, el, cfg_encoder_set_name,       "name");
		XML_ENCODER_SET(e, el, cfg_encoder_set_format_str, "format");
		XML_ENCODER_SET(e, el, cfg_encoder_set_program,    "program");
375
376
377
	}

	if (0 > cfg_encoder_validate(e, &errstr)) {
378
379
		log_error("%s[%ld]: encoder (%s): %s", doc->name, line_no,
		    cfg_encoder_get_name(e), errstr);
380
381
382
383
384
385
386
		return (-1);
	}

	return (0);
}

static int
387
_cfgfile_xml_parse_encoders(xmlDocPtr doc, xmlNodePtr cur)
388
389
390
{
	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("encoder")) &&
391
		    0 > _cfgfile_xml_parse_encoder(doc, cur))
392
393
394
395
396
397
398
399
400
401
			return (-1);
	}

	return (0);
}

/*
 * XML configuration file structure:
 *
 * ezstream
402
403
404
405
406
407
408
409
410
411
412
413
414
 *     servers
 *         server
 *             name
 *             protocol
 *             hostname
 *             port
 *             user
 *             password
 *             tls
 *             tls_cipher_suite
 *             ca_dir
 *             ca_file
 *             client_cert
415
 *             reconnect_attempts
416
417
418
419
420
 *         ...
 *     streams
 *         stream
 *             name
 *             mountpoint
421
 *             intake
422
 *             server
423
 *             public
424
425
426
427
428
429
430
431
432
433
434
 *             format
 *             encoder
 *             stream_name
 *             stream_url
 *             stream_genre
 *             stream_description
 *             stream_quality
 *             stream_bitrate
 *             stream_samplerate
 *             stream_channels
 *         ...
435
436
437
438
439
440
441
 *     intakes
 *         intake
 *             type
 *             filename
 *             shuffle
 *             stream_once
 *         ...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
 *     metadata
 *         program
 *         format_str
 *         refresh_interval
 *         normalize_strings
 *         no_updates
 *     decoders
 *         decoder
 *             name
 *             program
 *             file_ext
 *             ...
 *         ...
 *     encoders
 *         encoder
 *             name
 *             format
 *             program
 *         ...
 */
int
463
cfgfile_xml_parse(const char *config_file)
464
465
466
467
468
469
470
{
	xmlDocPtr	doc = NULL;
	xmlNodePtr	cur = NULL;
	int		error = 0;

	xmlLineNumbersDefault(1);

Moritz Grimm's avatar
Moritz Grimm committed
471
472
473
	if (0 > cfg_file_check(config_file))
		goto error;

474
475
476
477
478
479
	doc = xmlParseFile(config_file);
	if (!doc) {
		log_error("%s: not well-formed XML", config_file);
		goto error;
	}
	if (!doc->name)
480
		doc->name = (char *)xmlStrdup(XML_CHAR(config_file));
481
482
483
484
485
	cur = xmlDocGetRootElement(doc);
	if (!cur) {
		log_error("%s: empty document", config_file);
		goto error;
	}
Moritz Grimm's avatar
Moritz Grimm committed
486
487
488
	if (0 != xmlStrcasecmp(cur->name, XML_CHAR(CFGFILE_XML_NAME))) {
		log_error("%s: %s configuration not recognized", config_file,
		    CFGFILE_XML_NAME);
489
490
491
492
		goto error;
	}

	for (cur = cur->xmlChildrenNode; cur; cur = cur->next) {
493
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("servers"))) {
494
			if (0 > _cfgfile_xml_parse_servers(doc, cur))
495
496
497
				error = 1;
			continue;
		}
498
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("streams"))) {
499
			if (0 > _cfgfile_xml_parse_streams(doc, cur))
500
501
502
				error = 1;
			continue;
		}
503
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("intakes"))) {
504
			if (0 > _cfgfile_xml_parse_intakes(doc, cur))
505
506
507
508
				error = 1;
			continue;
		}
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("metadata"))) {
509
			if (0 > _cfgfile_xml_parse_metadata(doc, cur))
510
511
512
513
				error = 1;
			continue;
		}
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("decoders"))) {
514
			if (0 > _cfgfile_xml_parse_decoders(doc, cur))
515
516
517
518
				error = 1;
			continue;
		}
		if (0 == xmlStrcasecmp(cur->name, XML_CHAR("encoders"))) {
519
			if (0 > _cfgfile_xml_parse_encoders(doc, cur))
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
				error = 1;
			continue;
		}
	}
	if (error)
		goto error;

	xmlFreeDoc(doc);

	return (0);

error:
	if (doc)
		xmlFreeDoc(doc);

	return (-1);
}
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699

static void
_cfgfile_xml_print_server(cfg_server_t s, void *arg)
{
	FILE	*fp = (FILE *)arg;

	fprintf(fp, "    <server>\n");
	if (0 != strcasecmp(cfg_server_get_name(s), CFG_DEFAULT))
		fprintf(fp, "      <name>%s</name>\n",
		    cfg_server_get_name(s));
	fprintf(fp, "      <protocol>%s</protocol>\n",
	    cfg_server_get_protocol_str(s));
	if (cfg_server_get_hostname(s))
		fprintf(fp, "      <hostname>%s</hostname>\n",
		    cfg_server_get_hostname(s));
	if (cfg_server_get_port(s) != CFG_SERVER_DEFAULT_PORT)
		fprintf(fp, "      <port>%u</port>\n",
		    cfg_server_get_port(s));
	if (0 != strcasecmp(cfg_server_get_user(s), CFG_SERVER_DEFAULT_USER))
		fprintf(fp, "      <user>%s</user>\n",
		    cfg_server_get_user(s));
	if (cfg_server_get_password(s))
		fprintf(fp, "      <password>%s</password>\n",
		    cfg_server_get_password(s));
	if (cfg_server_get_tls(s) != CFG_TLS_MAY)
		fprintf(fp, "      <tls>%s</tls>\n",
		    cfg_server_get_tls_str(s));
	if (cfg_server_get_tls_cipher_suite(s))
		fprintf(fp, "      <tls_cipher_suite>%s</tls_cipher_suite>\n",
		    cfg_server_get_tls_cipher_suite(s));
	if (cfg_server_get_ca_dir(s))
		fprintf(fp, "      <ca_dir>%s</ca_dir>\n",
		    cfg_server_get_ca_dir(s));
	if (cfg_server_get_ca_file(s))
		fprintf(fp, "      <ca_file>%s</ca_file>\n",
		    cfg_server_get_ca_file(s));
	if (cfg_server_get_client_cert(s))
		fprintf(fp, "      <client_cert>%s</client_cert>\n",
		    cfg_server_get_client_cert(s));
	if (cfg_server_get_reconnect_attempts(s))
		fprintf(fp, "      <reconnect_attempts>%u</reconnect_attempts>\n",
		    cfg_server_get_reconnect_attempts(s));
	fprintf(fp, "    </server>\n");
}

static void
_cfgfile_xml_print_stream(cfg_stream_t s, void *arg)
{
	FILE	*fp = (FILE *)arg;

	fprintf(fp, "    <stream>\n");
	if (0 != strcasecmp(cfg_stream_get_name(s), CFG_DEFAULT))
		fprintf(fp, "      <name>%s</name>\n",
		    cfg_stream_get_name(s));
	if (cfg_stream_get_mountpoint(s))
		fprintf(fp, "      <mountpoint>%s</mountpoint>\n",
		    cfg_stream_get_mountpoint(s));
	if (cfg_stream_get_intake(s))
		fprintf(fp, "      <intake>%s</intake>\n",
		    cfg_stream_get_intake(s));
	if (cfg_stream_get_server(s))
		fprintf(fp, "      <server>%s</server>\n",
		    cfg_stream_get_server(s));
	if (cfg_stream_get_public(s))
		fprintf(fp, "      <public>yes</public>\n");
	fprintf(fp, "      <format>%s</format>\n",
	    cfg_stream_get_format_str(s));
	if (cfg_stream_get_encoder(s))
		fprintf(fp, "      <encoder>%s</encoder>\n",
		    cfg_stream_get_encoder(s));
	if (cfg_stream_get_stream_name(s))
		fprintf(fp, "      <stream_name>%s</stream_name>\n",
		    cfg_stream_get_stream_name(s));
	if (cfg_stream_get_stream_url(s))
		fprintf(fp, "      <stream_url>%s</stream_url>\n",
		    cfg_stream_get_stream_url(s));
	if (cfg_stream_get_stream_genre(s))
		fprintf(fp, "      <stream_genre>%s</stream_genre>\n",
		    cfg_stream_get_stream_genre(s));
	if (cfg_stream_get_stream_description(s))
		fprintf(fp, "      <stream_description>%s</stream_description>\n",
		    cfg_stream_get_stream_description(s));
	if (cfg_stream_get_stream_quality(s))
		fprintf(fp, "      <stream_quality>%s</stream_quality>\n",
		    cfg_stream_get_stream_quality(s));
	if (cfg_stream_get_stream_bitrate(s))
		fprintf(fp, "      <stream_bitrate>%s</stream_bitrate>\n",
		    cfg_stream_get_stream_bitrate(s));
	if (cfg_stream_get_stream_samplerate(s))
		fprintf(fp, "      <stream_samplerate>%s</stream_samplerate>\n",
		    cfg_stream_get_stream_samplerate(s));
	if (cfg_stream_get_stream_channels(s))
		fprintf(fp, "      <stream_channels>%s</stream_channels>\n",
		    cfg_stream_get_stream_channels(s));
	fprintf(fp, "    </stream>\n");
}

static void
_cfgfile_xml_print_intake(cfg_intake_t i, void *arg)
{
	FILE	*fp = (FILE *)arg;

	fprintf(fp, "    <intake>\n");
	if (0 != strcasecmp(cfg_intake_get_name(i), CFG_DEFAULT))
		fprintf(fp, "      <name>%s</name>\n",
		    cfg_intake_get_name(i));
	if (cfg_intake_get_type(i))
		fprintf(fp, "      <type>%s</type>\n",
		    cfg_intake_get_type_str(i));
	if (cfg_intake_get_filename(i))
		fprintf(fp, "      <filename>%s</filename>\n",
		    cfg_intake_get_filename(i));
	if (cfg_intake_get_shuffle(i))
		fprintf(fp, "      <shuffle>yes</shuffle>\n");
	if (cfg_intake_get_stream_once(i))
		fprintf(fp, "      <stream_once>yes</stream_once>\n");
	fprintf(fp, "    </intake>\n");
}

static void
_cfgfile_xml_print_decoder_ext(const char *ext, void *arg)
{
	FILE	*fp = (FILE *)arg;

	fprintf(fp, "      <file_ext>%s</file_ext>\n", ext);
}

static void
_cfgfile_xml_print_decoder(cfg_decoder_t d, void *arg)
{
	FILE	*fp = (FILE *)arg;

	fprintf(fp, "    <decoder>\n");
	if (0 != strcasecmp(cfg_decoder_get_name(d), CFG_DEFAULT))
		fprintf(fp, "      <name>%s</name>\n",
		    cfg_decoder_get_name(d));
	if (cfg_decoder_get_program(d))
		fprintf(fp, "      <program>%s</program>\n",
		    cfg_decoder_get_program(d));
	cfg_decoder_ext_foreach(d, _cfgfile_xml_print_decoder_ext, fp);
	fprintf(fp, "    </decoder>\n");
}

static void
_cfgfile_xml_print_encoder(cfg_encoder_t e, void *arg)
{
	FILE	*fp = (FILE *)arg;

	fprintf(fp, "    <encoder>\n");
	if (0 != strcasecmp(cfg_encoder_get_name(e), CFG_DEFAULT))
		fprintf(fp, "      <name>%s</name>\n",
		    cfg_encoder_get_name(e));
	fprintf(fp, "      <format>%s</format>\n",
	    cfg_stream_fmt2str(cfg_encoder_get_format(e)));
	if (cfg_encoder_get_program(e))
		fprintf(fp, "      <program>%s</program>\n",
		    cfg_encoder_get_program(e));
	fprintf(fp, "    </encoder>\n");
}

void
cfgfile_xml_print(FILE *fp)
{
Moritz Grimm's avatar
Moritz Grimm committed
700
	fprintf(fp, "<%s>\n", CFGFILE_XML_NAME);
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
	fprintf(fp, "\n");
	fprintf(fp, "  <servers>\n");
	cfg_server_list_foreach(cfg_get_servers(), _cfgfile_xml_print_server,
	    fp);
	fprintf(fp, "  </servers>\n");
	fprintf(fp, "\n");
	fprintf(fp, "  <streams>\n");
	cfg_stream_list_foreach(cfg_get_streams(), _cfgfile_xml_print_stream,
	    fp);
	fprintf(fp, "  </streams>\n");
	fprintf(fp, "\n");
	fprintf(fp, "  <intakes>\n");
	cfg_intake_list_foreach(cfg_get_intakes(), _cfgfile_xml_print_intake,
	    fp);
	fprintf(fp, "  </intakes>\n");
	if (cfg_decoder_list_nentries(cfg_get_decoders())) {
		fprintf(fp, "\n");
		fprintf(fp, "  <decoders>\n");
		cfg_decoder_list_foreach(cfg_get_decoders(),
		    _cfgfile_xml_print_decoder, fp);
		fprintf(fp, "  </decoders>\n");
	}
	if (cfg_encoder_list_nentries(cfg_get_encoders())) {
		fprintf(fp, "\n");
		fprintf(fp, "  <encoders>\n");
		cfg_encoder_list_foreach(cfg_get_encoders(),
		    _cfgfile_xml_print_encoder, fp);
		fprintf(fp, "  </encoders>\n");
	}
	if (cfg_get_metadata_program() ||
	    cfg_get_metadata_format_str() ||
	    0 <= cfg_get_metadata_refresh_interval() ||
	    cfg_get_metadata_normalize_strings() ||
	    cfg_get_metadata_no_updates()) {
		fprintf(fp, "\n");
		fprintf(fp, "  <metadata>\n");
		if (cfg_get_metadata_program())
			fprintf(fp, "    <program>%s</program>\n",
			    cfg_get_metadata_program());
		if (cfg_get_metadata_format_str())
			fprintf(fp, "    <format_str>%s</format_str>\n",
			    cfg_get_metadata_format_str());
		if (0 <= cfg_get_metadata_refresh_interval())
			fprintf(fp, "    <refresh_interval>%d</refresh_interval>\n",
			    cfg_get_metadata_refresh_interval());
		if (cfg_get_metadata_normalize_strings())
			fprintf(fp, "    <normalize_strings>yes</normalize_strings>\n");
		if (cfg_get_metadata_no_updates())
			fprintf(fp, "    <no_updates>yes</no_updates>\n");
		fprintf(fp, "  </metadata>\n");
	}
Moritz Grimm's avatar
Moritz Grimm committed
752
	fprintf(fp, "</%s>\n", CFGFILE_XML_NAME);
753
}