]>
Commit | Line | Data |
---|---|---|
2fb975da TT |
1 | /* strongSwan VICI protocol implementation for NHRP |
2 | * Copyright (c) 2014-2015 Timo Teräs | |
3 | * | |
4 | * This file is free software: you may copy, redistribute and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | */ | |
9 | ||
b45ac5f5 DL |
10 | #ifdef HAVE_CONFIG_H |
11 | #include "config.h" | |
12 | #endif | |
13 | ||
2fb975da TT |
14 | #include <string.h> |
15 | #include <sys/socket.h> | |
16 | #include <sys/un.h> | |
17 | ||
18 | #include "thread.h" | |
19 | #include "zbuf.h" | |
20 | #include "log.h" | |
aed07011 | 21 | #include "lib_errors.h" |
2fb975da | 22 | |
aed07011 | 23 | #include "nhrpd.h" |
2fb975da | 24 | #include "vici.h" |
aed07011 | 25 | #include "nhrp_errors.h" |
2fb975da TT |
26 | |
27 | #define ERRNO_IO_RETRY(EN) (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR)) | |
28 | ||
29 | struct blob { | |
30 | char *ptr; | |
31 | int len; | |
32 | }; | |
33 | ||
34 | static int blob_equal(const struct blob *b, const char *str) | |
35 | { | |
996c9314 LB |
36 | if (!b || b->len != (int)strlen(str)) |
37 | return 0; | |
2fb975da TT |
38 | return memcmp(b->ptr, str, b->len) == 0; |
39 | } | |
40 | ||
41 | static int blob2buf(const struct blob *b, char *buf, size_t n) | |
42 | { | |
996c9314 LB |
43 | if (!b || b->len >= (int)n) |
44 | return 0; | |
2fb975da TT |
45 | memcpy(buf, b->ptr, b->len); |
46 | buf[b->len] = 0; | |
47 | return 1; | |
48 | } | |
49 | ||
50 | struct vici_conn { | |
51 | struct thread *t_reconnect, *t_read, *t_write; | |
52 | struct zbuf ibuf; | |
53 | struct zbuf_queue obuf; | |
54 | int fd; | |
55 | uint8_t ibuf_data[VICI_MAX_MSGLEN]; | |
56 | }; | |
57 | ||
58 | struct vici_message_ctx { | |
59 | const char *sections[8]; | |
60 | int nsections; | |
61 | }; | |
62 | ||
63 | static int vici_reconnect(struct thread *t); | |
64 | static void vici_submit_request(struct vici_conn *vici, const char *name, ...); | |
65 | ||
66 | static void vici_zbuf_puts(struct zbuf *obuf, const char *str) | |
67 | { | |
68 | size_t len = strlen(str); | |
69 | zbuf_put8(obuf, len); | |
70 | zbuf_put(obuf, str, len); | |
71 | } | |
72 | ||
73 | static void vici_connection_error(struct vici_conn *vici) | |
74 | { | |
75 | nhrp_vc_reset(); | |
76 | ||
77 | THREAD_OFF(vici->t_read); | |
78 | THREAD_OFF(vici->t_write); | |
79 | zbuf_reset(&vici->ibuf); | |
80 | zbufq_reset(&vici->obuf); | |
81 | ||
82 | close(vici->fd); | |
83 | vici->fd = -1; | |
ffa2c898 | 84 | thread_add_timer(master, vici_reconnect, vici, 2, &vici->t_reconnect); |
2fb975da TT |
85 | } |
86 | ||
996c9314 LB |
87 | static void vici_parse_message(struct vici_conn *vici, struct zbuf *msg, |
88 | void (*parser)(struct vici_message_ctx *ctx, | |
89 | enum vici_type_t msgtype, | |
90 | const struct blob *key, | |
91 | const struct blob *val), | |
92 | struct vici_message_ctx *ctx) | |
2fb975da TT |
93 | { |
94 | uint8_t *type; | |
996c9314 LB |
95 | struct blob key = {0}; |
96 | struct blob val = {0}; | |
2fb975da TT |
97 | |
98 | while ((type = zbuf_may_pull(msg, uint8_t)) != NULL) { | |
99 | switch (*type) { | |
100 | case VICI_SECTION_START: | |
101 | key.len = zbuf_get8(msg); | |
102 | key.ptr = zbuf_pulln(msg, key.len); | |
996c9314 LB |
103 | debugf(NHRP_DEBUG_VICI, "VICI: Section start '%.*s'", |
104 | key.len, key.ptr); | |
2fb975da TT |
105 | parser(ctx, *type, &key, NULL); |
106 | ctx->nsections++; | |
107 | break; | |
108 | case VICI_SECTION_END: | |
109 | debugf(NHRP_DEBUG_VICI, "VICI: Section end"); | |
110 | parser(ctx, *type, NULL, NULL); | |
111 | ctx->nsections--; | |
112 | break; | |
113 | case VICI_KEY_VALUE: | |
114 | key.len = zbuf_get8(msg); | |
115 | key.ptr = zbuf_pulln(msg, key.len); | |
116 | val.len = zbuf_get_be16(msg); | |
117 | val.ptr = zbuf_pulln(msg, val.len); | |
996c9314 LB |
118 | debugf(NHRP_DEBUG_VICI, "VICI: Key '%.*s'='%.*s'", |
119 | key.len, key.ptr, val.len, val.ptr); | |
2fb975da TT |
120 | parser(ctx, *type, &key, &val); |
121 | break; | |
122 | case VICI_LIST_START: | |
123 | key.len = zbuf_get8(msg); | |
124 | key.ptr = zbuf_pulln(msg, key.len); | |
996c9314 LB |
125 | debugf(NHRP_DEBUG_VICI, "VICI: List start '%.*s'", |
126 | key.len, key.ptr); | |
2fb975da TT |
127 | break; |
128 | case VICI_LIST_ITEM: | |
129 | val.len = zbuf_get_be16(msg); | |
130 | val.ptr = zbuf_pulln(msg, val.len); | |
996c9314 LB |
131 | debugf(NHRP_DEBUG_VICI, "VICI: List item: '%.*s'", |
132 | val.len, val.ptr); | |
2fb975da TT |
133 | parser(ctx, *type, &key, &val); |
134 | break; | |
135 | case VICI_LIST_END: | |
136 | debugf(NHRP_DEBUG_VICI, "VICI: List end"); | |
137 | break; | |
138 | default: | |
996c9314 LB |
139 | debugf(NHRP_DEBUG_VICI, |
140 | "VICI: Unsupported message component type %d", | |
141 | *type); | |
2fb975da TT |
142 | return; |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | struct handle_sa_ctx { | |
148 | struct vici_message_ctx msgctx; | |
149 | int event; | |
150 | int child_ok; | |
151 | int kill_ikesa; | |
152 | uint32_t child_uniqueid, ike_uniqueid; | |
153 | struct { | |
154 | union sockunion host; | |
155 | struct blob id, cert; | |
156 | } local, remote; | |
157 | }; | |
158 | ||
996c9314 LB |
159 | static void parse_sa_message(struct vici_message_ctx *ctx, |
160 | enum vici_type_t msgtype, const struct blob *key, | |
161 | const struct blob *val) | |
2fb975da | 162 | { |
996c9314 LB |
163 | struct handle_sa_ctx *sactx = |
164 | container_of(ctx, struct handle_sa_ctx, msgctx); | |
2fb975da TT |
165 | struct nhrp_vc *vc; |
166 | char buf[512]; | |
167 | ||
168 | switch (msgtype) { | |
169 | case VICI_SECTION_START: | |
170 | if (ctx->nsections == 3) { | |
171 | /* Begin of child-sa section, reset child vars */ | |
172 | sactx->child_uniqueid = 0; | |
173 | sactx->child_ok = 0; | |
174 | } | |
175 | break; | |
176 | case VICI_SECTION_END: | |
177 | if (ctx->nsections == 3) { | |
178 | /* End of child-sa section, update nhrp_vc */ | |
179 | int up = sactx->child_ok || sactx->event == 1; | |
180 | if (up) { | |
996c9314 LB |
181 | vc = nhrp_vc_get(&sactx->local.host, |
182 | &sactx->remote.host, up); | |
2fb975da | 183 | if (vc) { |
996c9314 LB |
184 | blob2buf(&sactx->local.id, vc->local.id, |
185 | sizeof(vc->local.id)); | |
186 | if (blob2buf(&sactx->local.cert, | |
187 | (char *)vc->local.cert, | |
188 | sizeof(vc->local.cert))) | |
189 | vc->local.certlen = | |
190 | sactx->local.cert.len; | |
191 | blob2buf(&sactx->remote.id, | |
192 | vc->remote.id, | |
193 | sizeof(vc->remote.id)); | |
194 | if (blob2buf(&sactx->remote.cert, | |
195 | (char *)vc->remote.cert, | |
196 | sizeof(vc->remote.cert))) | |
197 | vc->remote.certlen = | |
198 | sactx->remote.cert.len; | |
199 | sactx->kill_ikesa |= | |
200 | nhrp_vc_ipsec_updown( | |
201 | sactx->child_uniqueid, | |
202 | vc); | |
2fb975da TT |
203 | } |
204 | } else { | |
205 | nhrp_vc_ipsec_updown(sactx->child_uniqueid, 0); | |
206 | } | |
207 | } | |
208 | break; | |
209 | default: | |
6b07f6e1 JB |
210 | if (!key) |
211 | break; | |
212 | ||
2fb975da TT |
213 | switch (key->ptr[0]) { |
214 | case 'l': | |
996c9314 LB |
215 | if (blob_equal(key, "local-host") |
216 | && ctx->nsections == 1) { | |
2fb975da | 217 | if (blob2buf(val, buf, sizeof(buf))) |
996c9314 LB |
218 | if (str2sockunion(buf, |
219 | &sactx->local.host) | |
220 | < 0) | |
af4c2728 | 221 | flog_err(NHRP_ERR_SWAN, |
aed07011 DS |
222 | "VICI: bad strongSwan local-host: %s", |
223 | buf); | |
996c9314 LB |
224 | } else if (blob_equal(key, "local-id") |
225 | && ctx->nsections == 1) { | |
2fb975da | 226 | sactx->local.id = *val; |
996c9314 LB |
227 | } else if (blob_equal(key, "local-cert-data") |
228 | && ctx->nsections == 1) { | |
2fb975da TT |
229 | sactx->local.cert = *val; |
230 | } | |
231 | break; | |
232 | case 'r': | |
996c9314 LB |
233 | if (blob_equal(key, "remote-host") |
234 | && ctx->nsections == 1) { | |
2fb975da | 235 | if (blob2buf(val, buf, sizeof(buf))) |
996c9314 LB |
236 | if (str2sockunion(buf, |
237 | &sactx->remote.host) | |
238 | < 0) | |
af4c2728 | 239 | flog_err(NHRP_ERR_SWAN, |
aed07011 DS |
240 | "VICI: bad strongSwan remote-host: %s", |
241 | buf); | |
996c9314 LB |
242 | } else if (blob_equal(key, "remote-id") |
243 | && ctx->nsections == 1) { | |
2fb975da | 244 | sactx->remote.id = *val; |
996c9314 LB |
245 | } else if (blob_equal(key, "remote-cert-data") |
246 | && ctx->nsections == 1) { | |
2fb975da TT |
247 | sactx->remote.cert = *val; |
248 | } | |
249 | break; | |
250 | case 'u': | |
996c9314 LB |
251 | if (blob_equal(key, "uniqueid") |
252 | && blob2buf(val, buf, sizeof(buf))) { | |
2fb975da | 253 | if (ctx->nsections == 3) |
996c9314 LB |
254 | sactx->child_uniqueid = |
255 | strtoul(buf, NULL, 0); | |
2fb975da | 256 | else if (ctx->nsections == 1) |
996c9314 LB |
257 | sactx->ike_uniqueid = |
258 | strtoul(buf, NULL, 0); | |
2fb975da TT |
259 | } |
260 | break; | |
261 | case 's': | |
262 | if (blob_equal(key, "state") && ctx->nsections == 3) { | |
263 | sactx->child_ok = | |
996c9314 LB |
264 | (sactx->event == 0 |
265 | && (blob_equal(val, "INSTALLED") | |
266 | || blob_equal(val, "REKEYED"))); | |
2fb975da TT |
267 | } |
268 | break; | |
269 | } | |
270 | break; | |
271 | } | |
272 | } | |
273 | ||
996c9314 LB |
274 | static void parse_cmd_response(struct vici_message_ctx *ctx, |
275 | enum vici_type_t msgtype, const struct blob *key, | |
276 | const struct blob *val) | |
d139786a TT |
277 | { |
278 | char buf[512]; | |
279 | ||
280 | switch (msgtype) { | |
281 | case VICI_KEY_VALUE: | |
996c9314 LB |
282 | if (blob_equal(key, "errmsg") |
283 | && blob2buf(val, buf, sizeof(buf))) | |
af4c2728 | 284 | flog_err(NHRP_ERR_SWAN, "VICI: strongSwan: %s", buf); |
d139786a TT |
285 | break; |
286 | default: | |
287 | break; | |
288 | } | |
289 | } | |
290 | ||
2fb975da TT |
291 | static void vici_recv_sa(struct vici_conn *vici, struct zbuf *msg, int event) |
292 | { | |
293 | char buf[32]; | |
294 | struct handle_sa_ctx ctx = { | |
295 | .event = event, | |
0a939f4f | 296 | .msgctx.nsections = 0 |
2fb975da TT |
297 | }; |
298 | ||
299 | vici_parse_message(vici, msg, parse_sa_message, &ctx.msgctx); | |
300 | ||
301 | if (ctx.kill_ikesa && ctx.ike_uniqueid) { | |
996c9314 LB |
302 | debugf(NHRP_DEBUG_COMMON, "VICI: Deleting IKE_SA %u", |
303 | ctx.ike_uniqueid); | |
2fb975da | 304 | snprintf(buf, sizeof buf, "%u", ctx.ike_uniqueid); |
996c9314 LB |
305 | vici_submit_request(vici, "terminate", VICI_KEY_VALUE, "ike-id", |
306 | strlen(buf), buf, VICI_END); | |
2fb975da TT |
307 | } |
308 | } | |
309 | ||
310 | static void vici_recv_message(struct vici_conn *vici, struct zbuf *msg) | |
311 | { | |
312 | uint32_t msglen; | |
313 | uint8_t msgtype; | |
314 | struct blob name; | |
7ea5df54 | 315 | struct vici_message_ctx ctx = { .nsections = 0 }; |
2fb975da TT |
316 | |
317 | msglen = zbuf_get_be32(msg); | |
318 | msgtype = zbuf_get8(msg); | |
319 | debugf(NHRP_DEBUG_VICI, "VICI: Message %d, %d bytes", msgtype, msglen); | |
320 | ||
321 | switch (msgtype) { | |
322 | case VICI_EVENT: | |
323 | name.len = zbuf_get8(msg); | |
324 | name.ptr = zbuf_pulln(msg, name.len); | |
325 | ||
996c9314 LB |
326 | debugf(NHRP_DEBUG_VICI, "VICI: Event '%.*s'", name.len, |
327 | name.ptr); | |
328 | if (blob_equal(&name, "list-sa") | |
329 | || blob_equal(&name, "child-updown") | |
330 | || blob_equal(&name, "child-rekey")) | |
2fb975da | 331 | vici_recv_sa(vici, msg, 0); |
996c9314 LB |
332 | else if (blob_equal(&name, "child-state-installed") |
333 | || blob_equal(&name, "child-state-rekeyed")) | |
2fb975da TT |
334 | vici_recv_sa(vici, msg, 1); |
335 | else if (blob_equal(&name, "child-state-destroying")) | |
336 | vici_recv_sa(vici, msg, 2); | |
337 | break; | |
d139786a | 338 | case VICI_CMD_RESPONSE: |
6c8ca260 | 339 | vici_parse_message(vici, msg, parse_cmd_response, &ctx); |
d139786a | 340 | break; |
2fb975da | 341 | case VICI_EVENT_UNKNOWN: |
d139786a | 342 | case VICI_CMD_UNKNOWN: |
af4c2728 | 343 | flog_err(NHRP_ERR_SWAN, |
996c9314 | 344 | "VICI: StrongSwan does not support mandatory events (unpatched?)"); |
2fb975da TT |
345 | break; |
346 | case VICI_EVENT_CONFIRM: | |
2fb975da TT |
347 | break; |
348 | default: | |
349 | zlog_notice("VICI: Unrecognized message type %d", msgtype); | |
350 | break; | |
351 | } | |
352 | } | |
353 | ||
354 | static int vici_read(struct thread *t) | |
355 | { | |
356 | struct vici_conn *vici = THREAD_ARG(t); | |
357 | struct zbuf *ibuf = &vici->ibuf; | |
358 | struct zbuf pktbuf; | |
359 | ||
360 | vici->t_read = NULL; | |
996c9314 | 361 | if (zbuf_read(ibuf, vici->fd, (size_t)-1) < 0) { |
2fb975da TT |
362 | vici_connection_error(vici); |
363 | return 0; | |
364 | } | |
365 | ||
366 | /* Process all messages in buffer */ | |
367 | do { | |
368 | uint32_t *hdrlen = zbuf_may_pull(ibuf, uint32_t); | |
369 | if (!hdrlen) | |
370 | break; | |
371 | if (!zbuf_may_pulln(ibuf, ntohl(*hdrlen))) { | |
372 | zbuf_reset_head(ibuf, hdrlen); | |
373 | break; | |
374 | } | |
375 | ||
376 | /* Handle packet */ | |
996c9314 LB |
377 | zbuf_init(&pktbuf, hdrlen, htonl(*hdrlen) + 4, |
378 | htonl(*hdrlen) + 4); | |
2fb975da TT |
379 | vici_recv_message(vici, &pktbuf); |
380 | } while (1); | |
381 | ||
ffa2c898 | 382 | thread_add_read(master, vici_read, vici, vici->fd, &vici->t_read); |
2fb975da TT |
383 | return 0; |
384 | } | |
385 | ||
386 | static int vici_write(struct thread *t) | |
387 | { | |
388 | struct vici_conn *vici = THREAD_ARG(t); | |
389 | int r; | |
390 | ||
391 | vici->t_write = NULL; | |
392 | r = zbufq_write(&vici->obuf, vici->fd); | |
393 | if (r > 0) { | |
ffa2c898 QY |
394 | thread_add_write(master, vici_write, vici, vici->fd, |
395 | &vici->t_write); | |
2fb975da TT |
396 | } else if (r < 0) { |
397 | vici_connection_error(vici); | |
398 | } | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
403 | static void vici_submit(struct vici_conn *vici, struct zbuf *obuf) | |
404 | { | |
405 | if (vici->fd < 0) { | |
406 | zbuf_free(obuf); | |
407 | return; | |
408 | } | |
409 | ||
410 | zbufq_queue(&vici->obuf, obuf); | |
ffa2c898 | 411 | thread_add_write(master, vici_write, vici, vici->fd, &vici->t_write); |
2fb975da TT |
412 | } |
413 | ||
414 | static void vici_submit_request(struct vici_conn *vici, const char *name, ...) | |
415 | { | |
416 | struct zbuf *obuf; | |
417 | uint32_t *hdrlen; | |
418 | va_list va; | |
419 | size_t len; | |
420 | int type; | |
421 | ||
422 | obuf = zbuf_alloc(256); | |
996c9314 LB |
423 | if (!obuf) |
424 | return; | |
2fb975da TT |
425 | |
426 | hdrlen = zbuf_push(obuf, uint32_t); | |
427 | zbuf_put8(obuf, VICI_CMD_REQUEST); | |
428 | vici_zbuf_puts(obuf, name); | |
429 | ||
430 | va_start(va, name); | |
431 | for (type = va_arg(va, int); type != VICI_END; type = va_arg(va, int)) { | |
432 | zbuf_put8(obuf, type); | |
433 | switch (type) { | |
434 | case VICI_KEY_VALUE: | |
435 | vici_zbuf_puts(obuf, va_arg(va, const char *)); | |
436 | len = va_arg(va, size_t); | |
437 | zbuf_put_be16(obuf, len); | |
438 | zbuf_put(obuf, va_arg(va, void *), len); | |
439 | break; | |
2fb975da TT |
440 | default: |
441 | break; | |
442 | } | |
443 | } | |
444 | va_end(va); | |
445 | *hdrlen = htonl(zbuf_used(obuf) - 4); | |
446 | vici_submit(vici, obuf); | |
447 | } | |
448 | ||
449 | static void vici_register_event(struct vici_conn *vici, const char *name) | |
450 | { | |
451 | struct zbuf *obuf; | |
452 | uint32_t *hdrlen; | |
453 | uint8_t namelen; | |
454 | ||
455 | namelen = strlen(name); | |
456 | obuf = zbuf_alloc(4 + 1 + 1 + namelen); | |
996c9314 LB |
457 | if (!obuf) |
458 | return; | |
2fb975da TT |
459 | |
460 | hdrlen = zbuf_push(obuf, uint32_t); | |
461 | zbuf_put8(obuf, VICI_EVENT_REGISTER); | |
462 | zbuf_put8(obuf, namelen); | |
463 | zbuf_put(obuf, name, namelen); | |
464 | *hdrlen = htonl(zbuf_used(obuf) - 4); | |
465 | ||
466 | vici_submit(vici, obuf); | |
467 | } | |
468 | ||
469 | static int vici_reconnect(struct thread *t) | |
470 | { | |
471 | struct vici_conn *vici = THREAD_ARG(t); | |
472 | int fd; | |
473 | ||
474 | vici->t_reconnect = NULL; | |
996c9314 LB |
475 | if (vici->fd >= 0) |
476 | return 0; | |
2fb975da TT |
477 | |
478 | fd = sock_open_unix("/var/run/charon.vici"); | |
479 | if (fd < 0) { | |
996c9314 LB |
480 | debugf(NHRP_DEBUG_VICI, |
481 | "%s: failure connecting VICI socket: %s", | |
482 | __PRETTY_FUNCTION__, strerror(errno)); | |
ffa2c898 QY |
483 | thread_add_timer(master, vici_reconnect, vici, 2, |
484 | &vici->t_reconnect); | |
2fb975da TT |
485 | return 0; |
486 | } | |
487 | ||
488 | debugf(NHRP_DEBUG_COMMON, "VICI: Connected"); | |
489 | vici->fd = fd; | |
ffa2c898 | 490 | thread_add_read(master, vici_read, vici, vici->fd, &vici->t_read); |
2fb975da TT |
491 | |
492 | /* Send event subscribtions */ | |
996c9314 LB |
493 | // vici_register_event(vici, "child-updown"); |
494 | // vici_register_event(vici, "child-rekey"); | |
2fb975da TT |
495 | vici_register_event(vici, "child-state-installed"); |
496 | vici_register_event(vici, "child-state-rekeyed"); | |
497 | vici_register_event(vici, "child-state-destroying"); | |
498 | vici_register_event(vici, "list-sa"); | |
499 | vici_submit_request(vici, "list-sas", VICI_END); | |
500 | ||
501 | return 0; | |
502 | } | |
503 | ||
504 | static struct vici_conn vici_connection; | |
505 | ||
506 | void vici_init(void) | |
507 | { | |
508 | struct vici_conn *vici = &vici_connection; | |
509 | ||
510 | vici->fd = -1; | |
511 | zbuf_init(&vici->ibuf, vici->ibuf_data, sizeof(vici->ibuf_data), 0); | |
512 | zbufq_init(&vici->obuf); | |
ffa2c898 QY |
513 | thread_add_timer_msec(master, vici_reconnect, vici, 10, |
514 | &vici->t_reconnect); | |
2fb975da TT |
515 | } |
516 | ||
517 | void vici_terminate(void) | |
518 | { | |
519 | } | |
520 | ||
996c9314 LB |
521 | void vici_request_vc(const char *profile, union sockunion *src, |
522 | union sockunion *dst, int prio) | |
2fb975da TT |
523 | { |
524 | struct vici_conn *vici = &vici_connection; | |
525 | char buf[2][SU_ADDRSTRLEN]; | |
526 | ||
527 | sockunion2str(src, buf[0], sizeof buf[0]); | |
528 | sockunion2str(dst, buf[1], sizeof buf[1]); | |
529 | ||
996c9314 LB |
530 | vici_submit_request(vici, "initiate", VICI_KEY_VALUE, "child", |
531 | strlen(profile), profile, VICI_KEY_VALUE, "timeout", | |
532 | (size_t)2, "-1", VICI_KEY_VALUE, "async", (size_t)1, | |
533 | "1", VICI_KEY_VALUE, "init-limits", (size_t)1, | |
534 | prio ? "0" : "1", VICI_KEY_VALUE, "my-host", | |
535 | strlen(buf[0]), buf[0], VICI_KEY_VALUE, | |
536 | "other-host", strlen(buf[1]), buf[1], VICI_END); | |
2fb975da TT |
537 | } |
538 | ||
539 | int sock_open_unix(const char *path) | |
540 | { | |
541 | int ret, fd; | |
542 | struct sockaddr_un addr; | |
543 | ||
544 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
545 | if (fd < 0) | |
546 | return -1; | |
547 | ||
996c9314 | 548 | memset(&addr, 0, sizeof(struct sockaddr_un)); |
2fb975da | 549 | addr.sun_family = AF_UNIX; |
6c8ca260 | 550 | strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); |
2fb975da | 551 | |
996c9314 LB |
552 | ret = connect(fd, (struct sockaddr *)&addr, |
553 | sizeof(addr.sun_family) + strlen(addr.sun_path)); | |
2fb975da TT |
554 | if (ret < 0) { |
555 | close(fd); | |
556 | return -1; | |
557 | } | |
558 | ||
6c8ca260 JB |
559 | ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
560 | if (ret < 0) { | |
561 | close(fd); | |
562 | return -1; | |
563 | } | |
2fb975da TT |
564 | |
565 | return fd; | |
566 | } |