summaryrefslogtreecommitdiff
path: root/src/charon/plugins/uci/uci_parser.c
blob: f994e36f7be5b7759a44fa44e2a827eb1be2a1f7 (plain)
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
/*
 * Copyright (C) 2008 Martin Willi
 * Copyright (C) 2008 Thomas Kallenberg
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "uci_parser.h"

#include <stdarg.h>

#include <library.h>
#include <uci.h>

typedef struct private_uci_parser_t private_uci_parser_t;

/**
 * Private data of an uci_parser_t object
 */
struct private_uci_parser_t {

	/**
	 * Public part
	 */
	uci_parser_t public;
	
	/**
	 * UCI package name this parser reads
	 */
	char *package;
};

/**
 * enumerator implementation create_section_enumerator
 */
typedef struct {
	/** implements enumerator */
	enumerator_t public;
	/** currently enumerated uci section */
	struct uci_element *current;
	/** all uci ipsec config sections */
	struct uci_list *list;
	/** uci conntext */
	struct uci_context *ctx;
	/** ipsec uci package */
	struct uci_package *package;
	/** NULL terminated list of keywords */
	char *keywords[];
} section_enumerator_t;

/**
 * Implementation of section_enumerator_t.enumerate
 */
static bool section_enumerator_enumerate(section_enumerator_t *this, ...)
{
	struct uci_element *element;
	char **value;
	va_list args;
	int i;
	
	if (&this->current->list == this->list)
	{
		return FALSE;
	}
	
	va_start(args, this);

	value = va_arg(args, char**);
	if (value)
	{
		if (uci_lookup(this->ctx, &element, this->package,
					   this->current->name, "name") == UCI_OK)
		{	/* use "name" attribute as config name if available ... */
			*value = uci_to_option(element)->value;
		}
		else
		{	/* ... or the section name becomes config name */
			*value = uci_to_section(this->current)->type;
		}
	}
	
	/* followed by keyword parameters */
	for (i = 0; this->keywords[i]; i++)
	{
		value = va_arg(args, char**);
		if (value && uci_lookup(this->ctx, &element, this->package,
					 	  this->current->name, this->keywords[i]) == UCI_OK)
		{
			*value = uci_to_option(element)->value;
		}
	}
	va_end(args);
	
	this->current = list_to_element(this->current->list.next);
	return TRUE;
}

/**
 * Implementation of section_enumerator_t.public.destroy
 */
static void section_enumerator_destroy(section_enumerator_t *this)
{
	uci_free_context(this->ctx);
	free(this);
}

/**
 * Implementation of backend_t.create_section_enumerator.
 */
static enumerator_t* create_section_enumerator(private_uci_parser_t *this, ...)
{
	section_enumerator_t *e;
	va_list args;
	int i;
	
	/* allocate enumerator large enought to hold keyword pointers */
	i = 1;
	va_start(args, this);
	while (va_arg(args, char*))
	{
		i++;
	}
	va_end(args);
	e = malloc(sizeof(section_enumerator_t) + sizeof(char*) * i);
	i = 0;
	va_start(args, this);
	do 
	{
		e->keywords[i] = va_arg(args, char*);
	}
	while (e->keywords[i++]);
	va_end(args);
	
	e->public.enumerate = (void*)section_enumerator_enumerate;
	e->public.destroy = (void*)section_enumerator_destroy;
	
	/* load uci context */
	e->ctx = uci_alloc_context();
	if (uci_load(e->ctx, this->package, &e->package) != UCI_OK)
	{
		section_enumerator_destroy(e);
		return NULL;
	}
	e->list = &e->package->sections;
	e->current = list_to_element(e->list->next);
	if (e->current->type != UCI_TYPE_SECTION)
	{
		section_enumerator_destroy(e);
		return NULL;
	}
	return &e->public;
}

/**
 * Implementation of uci_parser_t.destroy.
 */
static void destroy(private_uci_parser_t *this)
{
	free(this->package);
	free(this);
}

/**
 * Described in header.
 */
uci_parser_t *uci_parser_create(char *package)
{
	private_uci_parser_t *this = malloc_thing(private_uci_parser_t);

	this->public.create_section_enumerator = (enumerator_t*(*)(uci_parser_t*, ...))create_section_enumerator;
	this->public.destroy = (void(*)(uci_parser_t*))destroy;
	
	this->package = strdup(package);
	
	return &this->public;
}