xref: /freebsd/contrib/wireguard-tools/set.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 
10 #include "containers.h"
11 #include "config.h"
12 #include "ipc.h"
13 #include "subcommands.h"
14 
15 int set_main(int argc, const char *argv[])
16 {
17 	struct wgdevice *device = NULL;
18 	int ret = 1;
19 
20 	if (argc < 3) {
21 		fprintf(stderr, "Usage: %s %s <interface> [listen-port <port>] [fwmark <mark>] [private-key <file path>] [peer <base64 public key> [remove] [preshared-key <file path>] [endpoint <ip>:<port>] [persistent-keepalive <interval seconds>] [allowed-ips <ip1>/<cidr1>[,<ip2>/<cidr2>]...] ]...\n", PROG_NAME, argv[0]);
22 		return 1;
23 	}
24 
25 	device = config_read_cmd(argv + 2, argc - 2);
26 	if (!device)
27 		goto cleanup;
28 	strncpy(device->name, argv[1], IFNAMSIZ -  1);
29 	device->name[IFNAMSIZ - 1] = '\0';
30 
31 	if (ipc_set_device(device) != 0) {
32 		perror("Unable to modify interface");
33 		goto cleanup;
34 	}
35 
36 	ret = 0;
37 
38 cleanup:
39 	free_wgdevice(device);
40 	return ret;
41 }
42