-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsock_listen.c
68 lines (54 loc) · 1.13 KB
/
vsock_listen.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
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <linux/vm_sockets.h>
#include "cmd.h"
#include "core.h"
#include "vsock.h"
#define USAGE "[-p port]"
static int vsock_listen_main(int argc, char **argv)
{
int opt, port = 4242;
unsigned serve_flags = 0;
struct sockaddr_vm addr;
int sd;
while ((opt = getopt(argc, argv, "p:s")) != -1) {
switch (opt) {
case 'p':
port = atoi(optarg);
break;
case 's':
serve_flags |= SERVE_SPLICE;
break;
default:
goto usage;
}
}
if (optind < argc)
goto usage;
printf("local CID: %d\n", get_local_cid());
addr = (struct sockaddr_vm) {
.svm_family = AF_VSOCK,
.svm_cid = VMADDR_CID_ANY,
.svm_port = port,
};
sd = socket(AF_VSOCK, SOCK_STREAM, 0);
if (sd < 0)
err(-1, "socket");
if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)))
err(-1, "bind");
if (listen(sd, SOMAXCONN))
err(-1, "listen");
do_serve(sd, serve_flags);
close(sd);
return 0;
usage:
errx(-1, "Usage: %s " USAGE, argv[0]);
}
struct cmd vsock_listen_cmd = {
.name = "vsock-listen",
.main = vsock_listen_main,
.usage = USAGE,
};