-
Notifications
You must be signed in to change notification settings - Fork 84
/
api.c
360 lines (347 loc) · 11.9 KB
/
api.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* SPDX-License-Identifier: GPL-2.0-or-later */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
#include <glib.h>
#include <libslirp.h>
#include "vendor/parson/parson.h"
#include "api.h"
#include "slirp4netns.h"
int api_bindlisten(const char *api_socket)
{
int fd;
struct sockaddr_un addr;
unlink(api_socket); /* avoid EADDRINUSE */
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("api_bindlisten: socket");
goto cleanup_and_fail;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if (strlen(api_socket) >= sizeof(addr.sun_path)) {
fprintf(stderr, "the specified API socket path is too long (>= %lu)\n",
sizeof(addr.sun_path));
goto cleanup_and_fail;
}
strncpy(addr.sun_path, api_socket, sizeof(addr.sun_path) - 1);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("api_bindlisten: bind");
goto cleanup_and_fail;
}
if (listen(fd, 0) < 0) {
perror("api_bindlisten: listen");
goto cleanup_and_fail;
}
return fd;
cleanup_and_fail:
close(fd);
return -1;
}
struct api_hostfwd {
int id;
int is_udp;
struct in_addr host_addr;
int host_port;
struct in_addr guest_addr;
int guest_port;
};
struct api_ctx {
uint8_t *buf;
size_t buflen;
GList *hostfwds;
int hostfwds_nextid;
struct slirp4netns_config *cfg;
};
struct api_ctx *api_ctx_alloc(struct slirp4netns_config *cfg)
{
struct api_ctx *ctx = (struct api_ctx *)calloc(1, sizeof(*ctx));
if (ctx == NULL) {
return NULL;
}
ctx->buflen = 4096;
ctx->buf = malloc(ctx->buflen); /* FIXME: realloc */
if (ctx->buf == NULL) {
free(ctx);
return NULL;
}
ctx->cfg = cfg;
ctx->hostfwds = NULL;
ctx->hostfwds_nextid = 1;
return ctx;
}
void api_ctx_free(struct api_ctx *ctx)
{
if (ctx != NULL) {
if (ctx->buf != NULL) {
free(ctx->buf);
}
g_list_free_full(ctx->hostfwds, g_free);
free(ctx);
}
}
/*
Handler for add_hostfwd.
e.g. {"execute": "add_hostfwd", "arguments": {"proto": "tcp", "host_addr":
"0.0.0.0", "host_port": 8080, "guest_addr": "10.0.2.100", "guest_port": 80}}
This function returns the return value of write(2), not the return value of
slirp_add_hostfwd().
*/
static int api_handle_req_add_hostfwd(Slirp *slirp, int fd, struct api_ctx *ctx,
JSON_Object *jo)
{
int wrc = 0;
char idbuf[64];
const char *proto_s = json_object_dotget_string(jo, "arguments.proto");
const char *host_addr_s =
json_object_dotget_string(jo, "arguments.host_addr");
const char *guest_addr_s =
json_object_dotget_string(jo, "arguments.guest_addr");
struct api_hostfwd *fwd = g_malloc0(sizeof(*fwd));
if (fwd == NULL) {
perror("fatal: malloc");
exit(EXIT_FAILURE);
}
fwd->is_udp = -1; /* TODO: support SCTP */
if (strcmp(proto_s, "udp") == 0) {
fwd->is_udp = 1;
} else if (strcmp(proto_s, "tcp") == 0) {
fwd->is_udp = 0;
}
if (fwd->is_udp == -1) {
const char *err = "{\"error\":{\"desc\":\"bad request: add_hostfwd: "
"bad arguments.proto\"}}";
wrc = write(fd, err, strlen(err));
g_free(fwd);
goto finish;
}
if (host_addr_s == NULL || host_addr_s[0] == '\0') {
host_addr_s = "0.0.0.0";
}
if (inet_pton(AF_INET, host_addr_s, &fwd->host_addr) != 1) {
const char *err = "{\"error\":{\"desc\":\"bad request: add_hostfwd: "
"bad arguments.host_addr\"}}";
wrc = write(fd, err, strlen(err));
g_free(fwd);
goto finish;
}
fwd->host_port = (int)json_object_dotget_number(jo, "arguments.host_port");
if (fwd->host_port == 0) {
const char *err = "{\"error\":{\"desc\":\"bad request: add_hostfwd: "
"bad arguments.host_port\"}}";
wrc = write(fd, err, strlen(err));
g_free(fwd);
goto finish;
}
if (guest_addr_s == NULL || guest_addr_s[0] == '\0') {
fwd->guest_addr = ctx->cfg->recommended_vguest;
} else if (inet_pton(AF_INET, guest_addr_s, &fwd->guest_addr) != 1) {
const char *err = "{\"error\":{\"desc\":\"bad request: add_hostfwd: "
"bad arguments.guest_addr\"}}";
wrc = write(fd, err, strlen(err));
g_free(fwd);
goto finish;
}
fwd->guest_port =
(int)json_object_dotget_number(jo, "arguments.guest_port");
if (fwd->guest_port == 0) {
const char *err = "{\"error\":{\"desc\":\"bad request: add_hostfwd: "
"bad arguments.guest_port\"}}";
wrc = write(fd, err, strlen(err));
g_free(fwd);
goto finish;
}
if (slirp_add_hostfwd(slirp, fwd->is_udp, fwd->host_addr, fwd->host_port,
fwd->guest_addr, fwd->guest_port) < 0) {
const char *err = "{\"error\":{\"desc\":\"bad request: add_hostfwd: "
"slirp_add_hostfwd failed\"}}";
wrc = write(fd, err, strlen(err));
g_free(fwd);
goto finish;
}
fwd->id = ctx->hostfwds_nextid;
ctx->hostfwds_nextid++;
ctx->hostfwds = g_list_append(ctx->hostfwds, fwd);
if (snprintf(idbuf, sizeof(idbuf), "{\"return\":{\"id\":%d}}", fwd->id) >
sizeof(idbuf)) {
fprintf(stderr, "fatal: unexpected id=%d\n", fwd->id);
exit(EXIT_FAILURE);
}
wrc = write(fd, idbuf, strlen(idbuf));
finish:
return wrc;
}
static void api_handle_req_list_hostfwd_foreach(gpointer data,
gpointer user_data)
{
struct api_hostfwd *fwd = data;
JSON_Array *entries_array = (JSON_Array *)user_data;
JSON_Value *entry_value = json_value_init_object();
JSON_Object *entry_object = json_value_get_object(entry_value);
char host_addr[INET_ADDRSTRLEN], guest_addr[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &fwd->host_addr, host_addr, sizeof(host_addr)) ==
NULL) {
perror("fatal: inet_ntop");
exit(EXIT_FAILURE);
}
if (inet_ntop(AF_INET, &fwd->guest_addr, guest_addr, sizeof(guest_addr)) ==
NULL) {
perror("fatal: inet_ntop");
exit(EXIT_FAILURE);
}
json_object_set_number(entry_object, "id", fwd->id);
json_object_set_string(entry_object, "proto", fwd->is_udp ? "udp" : "tcp");
json_object_set_string(entry_object, "host_addr", host_addr);
json_object_set_number(entry_object, "host_port", fwd->host_port);
json_object_set_string(entry_object, "guest_addr", guest_addr);
json_object_set_number(entry_object, "guest_port", fwd->guest_port);
/* json_array_append_value does not copy passed value */
if (json_array_append_value(entries_array, entry_value) != JSONSuccess) {
fprintf(stderr, "fatal: json_array_append_value\n");
exit(EXIT_FAILURE);
}
}
/*
Handler for list_hostfwd.
e.g. {"execute": "list_hostfwd"}
*/
static int api_handle_req_list_hostfwd(Slirp *slirp, int fd,
struct api_ctx *ctx, JSON_Object *jo)
{
int wrc = 0;
JSON_Value *root_value = json_value_init_object(),
*entries_value = json_value_init_array();
JSON_Object *root_object = json_value_get_object(root_value);
JSON_Array *entries_array = json_array(entries_value);
char *serialized_string = NULL;
g_list_foreach(ctx->hostfwds, api_handle_req_list_hostfwd_foreach,
entries_array);
json_object_set_value(root_object, "entries", entries_value);
serialized_string = json_serialize_to_string(root_value);
wrc = write(fd, serialized_string, strlen(serialized_string));
json_free_serialized_string(serialized_string);
json_value_free(root_value);
return wrc;
}
static int api_handle_remove_list_hostfwd_find(gconstpointer gcp_fwd,
gconstpointer gcp_id)
{
struct api_hostfwd *fwd = (struct api_hostfwd *)gcp_fwd;
int id = *(int *)gcp_id;
return id == fwd->id ? 0 : 1;
}
/*
Handler for remove_hostfwd.
e.g. {"execute": "remove_hostfwd", "arguments": {"id": 42}}
*/
static int api_handle_req_remove_hostfwd(Slirp *slirp, int fd,
struct api_ctx *ctx, JSON_Object *jo)
{
int wrc = 0;
int id = (int)json_object_dotget_number(jo, "arguments.id");
GList *found = g_list_find_custom(ctx->hostfwds, &id,
api_handle_remove_list_hostfwd_find);
if (found == NULL) {
const char *err = "{\"error\":{\"desc\":\"bad request: remove_hostfwd: "
"bad arguments.id\"}}";
wrc = write(fd, err, strlen(err));
} else {
struct api_hostfwd *fwd = found->data;
const char *api_ok = "{\"return\":{}}";
if (slirp_remove_hostfwd(slirp, fwd->is_udp, fwd->host_addr,
fwd->host_port) < 0) {
const char *err = "{\"error\":{\"desc\":\"bad request: "
"remove_hostfwd: slirp_remove_hostfwd failed\"}}";
wrc = write(fd, err, strlen(err));
} else {
ctx->hostfwds = g_list_remove(ctx->hostfwds, fwd);
g_free(fwd);
wrc = write(fd, api_ok, strlen(api_ok));
}
}
return wrc;
}
static int api_handle_req(Slirp *slirp, int fd, struct api_ctx *ctx)
{
JSON_Value *jv = NULL;
JSON_Object *jo = NULL;
const char *execute = NULL;
int wrc = 0;
if ((jv = json_parse_string((const char *)ctx->buf)) == NULL) {
const char *err =
"{\"error\":{\"desc\":\"bad request: cannot parse JSON\"}}";
wrc = write(fd, err, strlen(err));
goto finish;
}
if ((jo = json_object(jv)) == NULL) {
const char *err =
"{\"error\":{\"desc\":\"bad request: json_object() failed\"}}";
wrc = write(fd, err, strlen(err));
goto finish;
}
/* TODO: json_validate */
if ((execute = json_object_get_string(jo, "execute")) == NULL) {
const char *err =
"{\"error\":{\"desc\":\"bad request: no execute found\"}}";
wrc = write(fd, err, strlen(err));
goto finish;
}
if ((strcmp(execute, "add_hostfwd")) == 0) {
wrc = api_handle_req_add_hostfwd(slirp, fd, ctx, jo);
} else if ((strcmp(execute, "list_hostfwd")) == 0) {
wrc = api_handle_req_list_hostfwd(slirp, fd, ctx, jo);
} else if ((strcmp(execute, "remove_hostfwd")) == 0) {
wrc = api_handle_req_remove_hostfwd(slirp, fd, ctx, jo);
} else {
const char *err =
"{\"error\":{\"desc\":\"bad request: unknown execute\"}}";
wrc = write(fd, err, strlen(err));
goto finish;
}
finish:
if (jv != NULL) {
json_value_free(jv);
}
return wrc;
}
/*
API handler.
This function returns the return value of either read(2) or write(2).
*/
int api_handler(Slirp *slirp, int listenfd, struct api_ctx *ctx)
{
struct sockaddr_un addr;
socklen_t addrlen = sizeof(struct sockaddr_un);
int fd;
int rc = 0, wrc = 0;
ssize_t len;
memset(&addr, 0, sizeof(addr));
if ((fd = accept(listenfd, (struct sockaddr *)&addr, &addrlen)) < 0) {
perror("api_handler: accept");
return -1;
}
if ((len = read(fd, ctx->buf, ctx->buflen)) < 0) {
perror("api_handler: read");
rc = len;
goto finish;
}
if (len == ctx->buflen) {
const char *err =
"{\"error\":{\"desc\":\"bad request: too large message\"}}";
fprintf(stderr, "api_handler: too large message (>= %ld bytes)\n", len);
wrc = write(fd, err, strlen(err));
rc = -1;
goto finish;
}
ctx->buf[len] = 0;
fprintf(stderr, "api_handler: got request: %s\n", ctx->buf);
wrc = api_handle_req(slirp, fd, ctx);
finish:
shutdown(fd, SHUT_RDWR);
if (rc == 0 && wrc != 0) {
rc = wrc;
}
close(fd);
return rc;
}