-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvunetpicox.c
131 lines (118 loc) · 3.52 KB
/
vunetpicox.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
/*********************************************************************
* Pico-X BSD
* Copyright (C) 2020 Renzo Davoli <[email protected]>, Daniele Lacamera <[email protected]>
* VirtualSquare team.
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
*
* Pico-X BSD is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) version 3.
*
* Pico-X BSD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*
*
*********************************************************************/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <vunet.h>
#include <picoxnet.h>
static int supported_domain (int domain) {
switch (domain) {
case AF_INET:
case AF_INET6:
case AF_NETLINK:
case AF_PACKET:
return 1;
default:
return 0;
}
}
static int supported_ioctl (unsigned long request) {
return vunet_is_netdev_ioctl(request);
}
static int _picox_socket(int domain, int type, int protocol) {
struct picox *stack = vunet_get_private_data();
type &= ~SOCK_CLOEXEC;
int rv = picox_msocket(stack, domain, type & ~SOCK_NONBLOCK, protocol);
if (rv >= 0 && (type & SOCK_NONBLOCK) != 0)
picox_fcntl(rv, F_SETFL, O_NONBLOCK);
return rv;
}
static int vunetpicox_ioctl(int fd, unsigned long request, void *addr) {
if (fd == -1 && addr == NULL) {
int retval = vunet_ioctl_parms(request);
if (retval == 0) {
errno = ENOSYS; return -1;
} else
return retval;
} else {
int tmpfd, retval;
switch (request) {
case FIONREAD:
case FIONBIO:
//printf("forward ioctl\n");
if (fd == -1)
return errno = EINVAL, -1;
else
return picox_ioctl(fd, request, addr);
default:
//printf("fake netlink socket\n");
tmpfd = _picox_socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, 0);
if (tmpfd < 0)
return -1;
else {
retval = picox_ioctl(tmpfd, request, addr);
picox_close(tmpfd);
return retval;
}
}
}
}
static int vunetpicox_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
return picox_accept(fd, addr, addrlen);
}
int vunetpicox_init(const char *source, unsigned long flags, const char *args, void **private_data) {
struct picox *vdestack = picox_newstack((char *) source);
if (vdestack != NULL) {
*private_data = vdestack;
return 0;
} else {
errno = EINVAL;
return -1;
}
}
int vunetpicox_fini(void *private_data) {
picox_delstack(private_data);
return 0;
}
struct vunet_operations vunet_ops = {
.socket = _picox_socket,
.bind = picox_bind,
.connect = picox_connect,
.listen = picox_listen,
.accept4 = vunetpicox_accept4,
.getsockname = picox_getsockname,
.getpeername = picox_getpeername,
.recvmsg = picox_recvmsg,
.sendmsg = picox_sendmsg,
.getsockopt = picox_getsockopt,
.setsockopt = picox_setsockopt,
.shutdown = picox_shutdown,
.ioctl = vunetpicox_ioctl,
.close = picox_close,
.epoll_ctl = epoll_ctl,
.supported_domain = supported_domain,
.supported_ioctl = supported_ioctl,
.init = vunetpicox_init,
.fini = vunetpicox_fini,
};