forked from rootless-containers/slirp4netns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.c
333 lines (317 loc) · 10.3 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
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "qemu/slirp/slirp.h"
#include "libslirp.h"
#include "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");
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, api_socket, sizeof(addr.sun_path) - 1);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("api_bindlisten: bind");
return -1;
}
if (listen(fd, 0) < 0) {
perror("api_bindlisten: listen");
return -1;
}
return fd;
}
struct api_hostfwd {
QTAILQ_ENTRY(api_hostfwd) entry;
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;
QTAILQ_HEAD(hostfwds, api_hostfwd) hostfwds;
int hostfwds_nextid;
struct slirp_config *cfg;
};
struct api_ctx *api_ctx_alloc(struct slirp_config *cfg)
{
struct api_ctx *ctx = (struct api_ctx *)malloc(sizeof(*ctx));
if (ctx == NULL) {
return NULL;
}
memset(ctx, 0, sizeof(*ctx));
ctx->buflen = 4096;
ctx->buf = malloc(ctx->buflen); /* FIXME: realloc */
if (ctx->buf == NULL) {
free(ctx);
return NULL;
}
ctx->cfg = cfg;
QTAILQ_INIT(&ctx->hostfwds);
ctx->hostfwds_nextid = 1;
return ctx;
}
void api_ctx_free(struct api_ctx *ctx)
{
if (ctx != NULL) {
struct api_hostfwd *fwd = NULL, *fwd_next = NULL;
if (ctx->buf != NULL) {
free(ctx->buf);
}
QTAILQ_FOREACH_SAFE(fwd, &ctx->hostfwds, entry, fwd_next) {
QTAILQ_REMOVE(&ctx->hostfwds, fwd, entry);
free(fwd);
}
/* TODO: free hostfwds */
free(ctx);
}
}
int api_add_to_list(struct api_ctx *ctx, bool is_udp, struct in_addr hostaddr, int hostport, struct in_addr guestaddr, int guestport) {
struct api_hostfwd *fwd = malloc(sizeof(*fwd));
if (fwd == NULL) {
perror("fatal: malloc");
exit(EXIT_FAILURE);
}
memset(fwd, 0, sizeof(*fwd));
fwd->is_udp = is_udp;
fwd->host_addr = hostaddr;
fwd->host_port = hostport;
fwd->guest_addr = guestaddr;
fwd->guest_port = guestport;
fwd->id = ctx->hostfwds_nextid;
ctx->hostfwds_nextid++;
QTAILQ_INSERT_TAIL(&ctx->hostfwds, fwd, entry);
return fwd->id;
}
/*
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, slirprc = 0;
int id = -1;
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; // can be on stack - will be dynamicly added using api_add_to_list
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));
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));
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));
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));
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));
goto finish;
}
if ((slirprc =
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));
goto finish;
}
id = api_add_to_list(ctx, fwd.is_udp, fwd.host_addr, fwd.host_port, fwd.guest_addr,
fwd.guest_port);
if (snprintf(idbuf, sizeof(idbuf), "{\"return\":{\"id\":%d}}", id) > sizeof(idbuf)) {
fprintf(stderr, "fatal: unexpected id=%d\n", id);
exit(EXIT_FAILURE);
}
wrc = write(fd, idbuf, strlen(idbuf));
finish:
return wrc;
}
/*
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;
struct api_hostfwd *fwd = NULL;
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;
QTAILQ_FOREACH(fwd, &ctx->hostfwds, entry) {
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);
}
}
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));
finish:
json_free_serialized_string(serialized_string);
json_value_free(root_value);
return wrc;
}
/*
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)
{
const char *err = "{\"error\":{\"desc\":\"bad request: remove_hostfwd: bad arguments.id\"}}";
int wrc = 0;
struct api_hostfwd *fwd = NULL, *fwd_next = NULL;
int id = (int)json_object_dotget_number(jo, "arguments.id");
QTAILQ_FOREACH_SAFE(fwd, &ctx->hostfwds, entry, fwd_next) {
if (id == fwd->id) {
const char *api_ok = "{\"return\":{}}";
if (slirp_remove_hostfwd(slirp, fwd->is_udp, fwd->host_addr, fwd->host_port) < 0) {
err =
"{\"error\":{\"desc\":\"bad request: remove_hostfwd: slirp_remove_hostfwd failed\"}}";
wrc = write(fd, err, strlen(err));
goto finish;
}
QTAILQ_REMOVE(&ctx->hostfwds, fwd, entry);
wrc = write(fd, api_ok, strlen(api_ok));
goto finish;
}
}
wrc = write(fd, err, strlen(err));
finish:
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;
}