summaryrefslogtreecommitdiffstats
path: root/qtctl.c
blob: 24688468bfa02abb4649626fcb054f1f3cc716f1 (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
#include <stdio.h>
#include <string.h>

#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "quicktun.h"


void usage()
{
	fputs("Usage: qtctl add [ dev NAME ] [ mode { ethernet | ip } ]\n"
	      "                 [ local_address ADDRESS ] [ local_port PORT ]\n"
	      "                 [ remote_address ADDRESS ] [ remote_port PORT ]\n"
	      "                 [ remote_float ]\n"
	      "       qtctl del dev NAME\n", stderr);

	exit(2);
}

void add(int argc, char *argv[], struct nl_handle *sock, int family)
{
	struct nl_msg *msg;
	struct nl_cb *cb;

	int p = 1;
	uint16_t mode = QUICKTUN_MODE_ETHERNET;

	char *tmp_charp;
	struct in_addr tmp_addr;
	unsigned long tmp_ul;


	msg = nlmsg_alloc();
	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO, QUICKTUN_CMD_CREATE_DEVICE, 1);

	while ((++p) < argc) {
		if (!strcmp(argv[p], "dev")) {
			if ((++p) >= argc)
				usage();

			tmp_charp = strndup(argv[p], IFNAMSIZ - 1);
			nla_put_string(msg, QUICKTUN_A_IFNAME, tmp_charp);
			free(tmp_charp);
		}
		else if (!strcmp(argv[p], "mode")) {
			if ((++p) >= argc)
				usage();

			if (!strcmp(argv[p], "ethernet"))
				mode = QUICKTUN_MODE_ETHERNET;
			else if (!strcmp(argv[p], "ip"))
				mode = QUICKTUN_MODE_IP;
			else
				usage();
		}
		else if (!strcmp(argv[p], "local_address")) {
			if ((++p) >= argc)
				usage();

			if (!(inet_aton(argv[p], &tmp_addr)))
				usage();

			nla_put_u32(msg, QUICKTUN_A_LOCAL_ADDRESS, tmp_addr.s_addr);
		}
		else if (!strcmp(argv[p], "local_port")) {
			if ((++p) >= argc)
				usage();

			tmp_ul = strtoul(argv[p], &tmp_charp, 10);

			if (*tmp_charp)
				usage();

			nla_put_u16(msg, QUICKTUN_A_LOCAL_PORT, htons((short)tmp_ul));
		}
		else if (!strcmp(argv[p], "remote_address")) {
			if ((++p) >= argc)
				usage();

			if (!(inet_aton(argv[p], &tmp_addr)))
				usage();

			nla_put_u32(msg, QUICKTUN_A_REMOTE_ADDRESS, tmp_addr.s_addr);
		}
		else if (!strcmp(argv[p], "remote_port")) {
			if ((++p) >= argc)
				usage();

			tmp_ul = strtoul(argv[p], &tmp_charp, 10);

			if (*tmp_charp)
				usage();

			nla_put_u16(msg, QUICKTUN_A_REMOTE_PORT, htons((short)tmp_ul));
		}
		else if (!strcmp(argv[p], "remote_float")) {
			nla_put_flag(msg, QUICKTUN_A_REMOTE_FLOAT);
		}
		else
			usage();
	}

	nla_put_u16(msg, QUICKTUN_A_MODE, mode);

	nl_send_auto_complete(sock, msg);

	nlmsg_free(msg);

	cb = nl_cb_alloc(NL_CB_DEFAULT);
	nl_cb_err(cb, NL_CB_DEBUG, NULL, NULL);

	nl_recvmsgs(sock, cb);
}

void del(int argc, char *argv[], struct nl_handle *sock, int family)
{
	struct nl_msg *msg;
	struct nl_cb *cb;

	int p = 1;
	char *tmp_charp;

	int has_dev = 0;


	msg = nlmsg_alloc();
	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO, QUICKTUN_CMD_DELETE_DEVICE, 1);

	while ((++p) < argc) {
		if (!strcmp(argv[p], "dev")) {
			if ((++p) >= argc)
				usage();

			if (has_dev)
				usage();

			tmp_charp = strndup(argv[p], IFNAMSIZ - 1);
			nla_put_string(msg, QUICKTUN_A_IFNAME, tmp_charp);
			free(tmp_charp);

			has_dev = 1;
		}
	}

	if (!has_dev)
		usage();

	nl_send_auto_complete(sock, msg);

	nlmsg_free(msg);

	cb = nl_cb_alloc(NL_CB_DEFAULT);
	nl_cb_err(cb, NL_CB_DEBUG, NULL, NULL);

	nl_recvmsgs(sock, cb);
}

int main(int argc, char *argv[])
{
	struct nl_handle *sock;
	int family;

	sock = nl_handle_alloc();
	genl_connect(sock);
	family = genl_ctrl_resolve(sock, "quicktun");

	if (argc < 2)
		usage();

	if (!strcmp(argv[1], "add"))
		add(argc, argv, sock, family);
	else if (!strcmp(argv[1], "del"))
		del(argc, argv, sock, family);
	else
		usage();

	return 0;
}