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
|
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/thread-entry.hpp>
#include <mlibc/debug.hpp>
#include <abi-bits/in.h>
#include <abi-bits/errno.h>
#include <aero/syscall.h>
#include <unistd.h>
#include <stdint.h>
#include <net/if.h>
#include <sys/ioctl.h>
namespace {
int fcntl_helper(int fd, int request, int *result, ...) {
va_list args;
va_start(args, result);
if(!mlibc::sys_fcntl) {
return ENOSYS;
}
int ret = mlibc::sys_fcntl(fd, request, args, result);
va_end(args);
return ret;
}
}
namespace mlibc {
int sys_socket(int family, int type, int protocol, int *fd) {
auto result = syscall(SYS_SOCKET, family, type, protocol);
if (result < 0) {
return -result;
}
*fd = result;
return 0;
}
int sys_bind(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
auto result = syscall(SYS_BIND, fd, addr_ptr, (sc_word_t)addr_length);
if (result < 0) {
return -result;
}
return 0;
}
int sys_connect(int fd, const struct sockaddr *addr_ptr,
socklen_t addr_length) {
auto result = syscall(SYS_CONNECT, fd, addr_ptr, (sc_word_t)addr_length);
if (result < 0) {
return -result;
}
return 0;
}
int sys_listen(int fd, int backlog) {
auto result = syscall(SYS_LISTEN, fd, backlog);
if (result < 0) {
return -result;
}
return 0;
}
int sys_accept(int sockfd, int *newfd, struct sockaddr *addr_ptr,
socklen_t *addr_length, int flags) {
auto result = syscall(SYS_ACCEPT, sockfd, addr_ptr, addr_length);
if (result < 0) {
return -result;
}
*newfd = result;
if(flags & SOCK_NONBLOCK) {
int fcntl_ret = 0;
fcntl_helper(*newfd, F_GETFL, &fcntl_ret);
fcntl_helper(*newfd, F_SETFL, &fcntl_ret, fcntl_ret | O_NONBLOCK);
}
if(flags & SOCK_CLOEXEC) {
int fcntl_ret = 0;
fcntl_helper(*newfd, F_GETFD, &fcntl_ret);
fcntl_helper(*newfd, F_SETFD, &fcntl_ret, fcntl_ret | FD_CLOEXEC);
}
return 0;
}
int sys_msg_send(int fd, const struct msghdr *hdr, int flags, ssize_t *length) {
auto result = syscall(SYS_SOCK_SEND, fd, hdr, flags);
if (result < 0)
return -result;
*length = result;
return 0;
}
int sys_msg_recv(int sockfd, struct msghdr *msg_hdr, int flags,
ssize_t *length) {
auto result = syscall(SYS_SOCK_RECV, sockfd, msg_hdr, flags);
if (result < 0) {
return -result;
}
*length = result;
return 0;
}
int sys_socketpair(int domain, int type_and_flags, int proto, int *fds) {
auto result = syscall(SYS_SOCKET_PAIR, domain, type_and_flags, proto, fds);
if (result < 0) {
return -result;
}
return 0;
}
int sys_getsockopt(int fd, int layer, int number, void *__restrict buffer,
socklen_t *__restrict size) {
(void)fd;
(void)size;
if (layer == SOL_SOCKET && number == SO_PEERCRED) {
mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET "
"and SO_PEERCRED is unimplemented\e[39m"
<< frg::endlog;
*(int *)buffer = 0;
return 0;
} else if (layer == SOL_SOCKET && number == SO_SNDBUF) {
mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET "
"and SO_SNDBUF is unimplemented\e[39m"
<< frg::endlog;
*(int *)buffer = 4096;
return 0;
} else if (layer == SOL_SOCKET && number == SO_TYPE) {
mlibc::infoLogger()
<< "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_TYPE is "
"unimplemented, hardcoding SOCK_STREAM\e[39m"
<< frg::endlog;
*(int *)buffer = SOCK_STREAM;
return 0;
} else if (layer == SOL_SOCKET && number == SO_ERROR) {
mlibc::infoLogger()
<< "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_ERROR is "
"unimplemented, hardcoding 0\e[39m"
<< frg::endlog;
*(int *)buffer = 0;
return 0;
} else if (layer == SOL_SOCKET && number == SO_KEEPALIVE) {
mlibc::infoLogger()
<< "\e[31mmlibc: getsockopt() call with SOL_SOCKET and "
"SO_KEEPALIVE is unimplemented, hardcoding 0\e[39m"
<< frg::endlog;
*(int *)buffer = 0;
return 0;
} else {
mlibc::panicLogger()
<< "\e[31mmlibc: Unexpected getsockopt() call, layer: " << layer
<< " number: " << number << "\e[39m" << frg::endlog;
__builtin_unreachable();
}
return 0;
}
int sys_setsockopt(int fd, int layer, int number, const void *buffer,
socklen_t size) {
(void)fd;
(void)buffer;
(void)size;
if (layer == SOL_SOCKET && number == SO_PASSCRED) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt(SO_PASSCRED) is not "
"implemented correctly\e[39m"
<< frg::endlog;
return 0;
} else if (layer == SOL_SOCKET && number == SO_ATTACH_FILTER) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt(SO_ATTACH_FILTER) is "
"not implemented correctly\e[39m"
<< frg::endlog;
return 0;
} else if (layer == SOL_SOCKET && number == SO_RCVBUFFORCE) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt(SO_RCVBUFFORCE) is not "
"implemented correctly\e[39m"
<< frg::endlog;
return 0;
} else if (layer == SOL_SOCKET && number == SO_SNDBUF) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET "
"and SO_SNDBUF is unimplemented\e[39m"
<< frg::endlog;
return 0;
} else if (layer == SOL_SOCKET && number == SO_KEEPALIVE) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET "
"and SO_KEEPALIVE is unimplemented\e[39m"
<< frg::endlog;
return 0;
} else if (layer == SOL_SOCKET && number == SO_REUSEADDR) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET "
"and SO_REUSEADDR is unimplemented\e[39m"
<< frg::endlog;
return 0;
} else if (layer == AF_NETLINK && number == SO_ACCEPTCONN) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with AF_NETLINK "
"and SO_ACCEPTCONN is unimplemented\e[39m"
<< frg::endlog;
return 0;
} else {
mlibc::infoLogger()
<< "\e[31mmlibc: Unexpected setsockopt() call, layer: " << layer
<< " number: " << number << "\e[39m" << frg::endlog;
return 0;
}
}
int sys_shutdown(int sockfd, int how) {
auto ret = syscall(SYS_SOCK_SHUTDOWN, sockfd, how);
if(int e = sc_error(ret); e)
return e;
return 0;
}
int sys_if_nametoindex(const char *name, unsigned int *ret) {
int fd = 0;
int r = sys_socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd);
if (r)
return r;
struct ifreq ifr;
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
r = sys_ioctl(fd, SIOCGIFINDEX, &ifr, NULL);
close(fd);
if (r)
return r;
*ret = ifr.ifr_ifindex;
return 0;
}
} // namespace mlibc
|