2017-11-30 16:23:50 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-09-25 04:22:09 +02:00
|
|
|
#include "containers.h"
|
2015-06-05 15:58:00 +02:00
|
|
|
#include "config.h"
|
2016-07-20 21:24:27 +02:00
|
|
|
#include "ipc.h"
|
2015-06-05 15:58:00 +02:00
|
|
|
#include "subcommands.h"
|
|
|
|
|
|
|
|
int setconf_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct wgdevice *device = NULL;
|
|
|
|
struct config_ctx ctx;
|
|
|
|
FILE *config_input = NULL;
|
|
|
|
char *config_buffer = NULL;
|
|
|
|
size_t config_buffer_len = 0;
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: %s %s <interface> <configuration filename>\n", PROG_NAME, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
config_input = fopen(argv[2], "r");
|
|
|
|
if (!config_input) {
|
|
|
|
perror("fopen");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-09-25 04:22:09 +02:00
|
|
|
if (!config_read_init(&ctx, !strcmp(argv[0], "addconf"))) {
|
2015-06-05 15:58:00 +02:00
|
|
|
fclose(config_input);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while (getline(&config_buffer, &config_buffer_len, config_input) >= 0) {
|
|
|
|
if (!config_read_line(&ctx, config_buffer)) {
|
|
|
|
fprintf(stderr, "Configuration parsing error\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2017-09-25 04:22:09 +02:00
|
|
|
device = config_read_finish(&ctx);
|
|
|
|
if (!device) {
|
2015-06-05 15:58:00 +02:00
|
|
|
fprintf(stderr, "Invalid configuration\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-09-25 04:22:09 +02:00
|
|
|
strncpy(device->name, argv[1], IFNAMSIZ - 1);
|
|
|
|
device->name[IFNAMSIZ - 1] = 0;
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2016-07-20 21:24:27 +02:00
|
|
|
if (ipc_set_device(device) != 0) {
|
2015-06-05 15:58:00 +02:00
|
|
|
perror("Unable to set device");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (config_input)
|
|
|
|
fclose(config_input);
|
|
|
|
free(config_buffer);
|
2017-09-25 04:22:09 +02:00
|
|
|
free_wgdevice(device);
|
2015-06-05 15:58:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|