Skip to content

Commit e4f0722

Browse files
committed
chaos: refactor config parsing code
Make parsing of vifs consistent with the rest of the code, i.e. each object is fully handled by its own parsing function, like the root object, xen or vcpus.
1 parent 877e34c commit e4f0722

File tree

1 file changed

+73
-73
lines changed

1 file changed

+73
-73
lines changed

bin/chaos.src/config.c

+73-73
Original file line numberDiff line numberDiff line change
@@ -203,92 +203,102 @@ static int __parse_mac(uint8_t mac[6], const char* mac_str)
203203
}
204204

205205

206-
static void __parse_vif(json_t* vif, config* conf)
206+
static void __parse_vifs(json_t* vifs, config* conf)
207207
{
208208
int ret;
209209

210-
int vid;
210+
size_t idx;
211+
json_t* vif;
211212
json_t* value;
212213
const char* key;
213214

214-
if (!json_is_object(vif)) {
215-
fprintf(stderr, "Parameter 'vifs' contains invalid element. Must be array of objects.\n");
215+
if (!json_is_array(vifs)) {
216+
fprintf(stderr, "Parameter 'vifs' has invalid type, must be array.\n");
216217
conf->error = true;
217218
return;
218219
}
219220

220-
if (conf->vifs_count >= DEV_MAX_COUNT) {
221-
fprintf(stderr, "Too many vifs defined. Maximum %d supported.\n", DEV_MAX_COUNT);
222-
conf->error = true;
223-
return;
224-
}
221+
json_array_foreach(vifs, idx, vif) {
222+
/* In case of error the loop will `continue` without incrementing the
223+
* counter, so do it at the beginning and access `vifs_count - 1`.
224+
*/
225+
conf->vifs_count++;
225226

226-
vid = conf->vifs_count;
227+
if (conf->vifs_count >= DEV_MAX_COUNT) {
228+
fprintf(stderr, "Too many vifs defined. Maximum %d supported.\n", DEV_MAX_COUNT);
229+
conf->error = true;
230+
break;
231+
}
227232

228-
json_object_foreach(vif, key, value) {
229-
if (strcmp(key, "ip") == 0) {
230-
if (conf->vifs[vid].ip_set) {
231-
fprintf(stderr, "Parameter 'ip' defined multiple times.\n");
232-
conf->error = true;
233-
continue;
234-
}
233+
if (!json_is_object(vif)) {
234+
fprintf(stderr, "Parameter 'vifs' contains invalid element. Must be array of objects.\n");
235+
conf->error = true;
236+
continue;
237+
}
235238

236-
conf->vifs[vid].ip_set = true;
239+
json_object_foreach(vif, key, value) {
240+
if (strcmp(key, "ip") == 0) {
241+
if (conf->vifs[conf->vifs_count - 1].ip_set) {
242+
fprintf(stderr, "Parameter 'ip' defined multiple times.\n");
243+
conf->error = true;
244+
continue;
245+
}
237246

238-
const char* ip_str = json_string_value(value);
239-
if (ip_str == NULL) {
240-
fprintf(stderr, "Parameter 'ip' has invalid type, must be string.\n");
241-
conf->error = true;
242-
continue;
243-
}
247+
conf->vifs[conf->vifs_count - 1].ip_set = true;
244248

245-
ret = __parse_ip(&(conf->vifs[vid].ip), ip_str);
246-
if (ret) {
247-
fprintf(stderr, "Parameter 'ip' is an invalid IP.\n");
248-
conf->error = true;
249-
}
250-
} else if (strcmp(key, "mac") == 0) {
251-
if (conf->vifs[vid].mac_set) {
252-
fprintf(stderr, "Parameter 'mac' defined multiple times.\n");
253-
conf->error = true;
254-
continue;
255-
}
249+
const char* ip_str = json_string_value(value);
250+
if (ip_str == NULL) {
251+
fprintf(stderr, "Parameter 'ip' has invalid type, must be string.\n");
252+
conf->error = true;
253+
continue;
254+
}
255+
256+
ret = __parse_ip(&(conf->vifs[conf->vifs_count - 1].ip), ip_str);
257+
if (ret) {
258+
fprintf(stderr, "Parameter 'ip' is an invalid IP.\n");
259+
conf->error = true;
260+
}
261+
} else if (strcmp(key, "mac") == 0) {
262+
if (conf->vifs[conf->vifs_count - 1].mac_set) {
263+
fprintf(stderr, "Parameter 'mac' defined multiple times.\n");
264+
conf->error = true;
265+
continue;
266+
}
256267

257-
conf->vifs[vid].mac_set = true;
268+
conf->vifs[conf->vifs_count - 1].mac_set = true;
258269

259-
const char* mac_str = json_string_value(value);
260-
if (mac_str == NULL) {
261-
fprintf(stderr, "Parameter 'mac' has invalid type, must be string.\n");
262-
conf->error = true;
263-
continue;
264-
}
270+
const char* mac_str = json_string_value(value);
271+
if (mac_str == NULL) {
272+
fprintf(stderr, "Parameter 'mac' has invalid type, must be string.\n");
273+
conf->error = true;
274+
continue;
275+
}
265276

266-
ret = __parse_mac(conf->vifs[vid].mac, mac_str);
267-
if (ret) {
268-
fprintf(stderr, "Parameter 'mac' is an invalid MAC.\n");
269-
conf->error = true;
270-
}
271-
} else if (strcmp(key, "bridge") == 0) {
272-
if (conf->vifs[vid].bridge_set) {
273-
fprintf(stderr, "Parameter 'bridge' defined multiple times.\n");
274-
conf->error = true;
275-
continue;
276-
}
277+
ret = __parse_mac(conf->vifs[conf->vifs_count - 1].mac, mac_str);
278+
if (ret) {
279+
fprintf(stderr, "Parameter 'mac' is an invalid MAC.\n");
280+
conf->error = true;
281+
}
282+
} else if (strcmp(key, "bridge") == 0) {
283+
if (conf->vifs[conf->vifs_count - 1].bridge_set) {
284+
fprintf(stderr, "Parameter 'bridge' defined multiple times.\n");
285+
conf->error = true;
286+
continue;
287+
}
277288

278-
conf->vifs[vid].bridge_set = true;
289+
conf->vifs[conf->vifs_count - 1].bridge_set = true;
279290

280-
conf->vifs[vid].bridge = json_string_value(value);
281-
if (conf->vifs[vid].bridge == NULL) {
282-
fprintf(stderr, "Parameter 'bridge' has invalid type, must be string.\n");
291+
conf->vifs[conf->vifs_count - 1].bridge = json_string_value(value);
292+
if (conf->vifs[conf->vifs_count - 1].bridge == NULL) {
293+
fprintf(stderr, "Parameter 'bridge' has invalid type, must be string.\n");
294+
conf->error = true;
295+
}
296+
} else {
297+
fprintf(stderr, "Invalid parameter '%s' on vif definition.\n", key);
283298
conf->error = true;
284299
}
285-
} else {
286-
fprintf(stderr, "Invalid parameter '%s' on vif definition.\n", key);
287-
conf->error = true;
288300
}
289301
}
290-
291-
conf->vifs_count++;
292302
}
293303

294304
static void __parse_vcpus(json_t* vcpus, config* conf)
@@ -448,9 +458,7 @@ static void __parse_xen(json_t* xen, config* conf)
448458

449459
static void __parse_root(json_t* root, config* conf)
450460
{
451-
size_t idx;
452461
json_t* value;
453-
json_t* vif;
454462
const char* key;
455463

456464
if (!json_is_object(root)) {
@@ -551,15 +559,7 @@ static void __parse_root(json_t* root, config* conf)
551559

552560
conf->vifs_set = true;
553561

554-
if (!json_is_array(value)) {
555-
fprintf(stderr, "Parameter 'vifs' has invalid type, must be array.\n");
556-
conf->error = true;
557-
continue;
558-
}
559-
560-
json_array_foreach(value, idx, vif) {
561-
__parse_vif(vif, conf);
562-
}
562+
__parse_vifs(value, conf);
563563
} else if (strcmp(key, "paused") == 0) {
564564
if (conf->paused_set) {
565565
fprintf(stderr, "Parameter 'paused' defined multiple times.\n");

0 commit comments

Comments
 (0)