-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsyssocket.cc
190 lines (167 loc) · 4.44 KB
/
syssocket.cc
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
#include "types.h"
#include "kernel.hh"
#include "net.hh"
#include "filetable.hh"
#include <fcntl.h>
#include <uk/stat.h>
#include <sys/socket.h>
// Copy *sa into *ss, where sa is sa_len bytes long, and make sure
// there's a NUL after the end of the copied sockaddr.
static int
sockaddr_from_user(struct sockaddr_storage *ss,
const userptr<struct sockaddr> sa, uint32_t sa_len)
{
assert(sa);
// Make sure source sockaddr isn't too big. -1 leaves room for the
// final padding byte that we add in the kernel.
if (sa_len > sizeof *ss - 1)
return -1;
if (!userptr<void>(sa.unsafe_get()).load_bytes(ss, sa_len))
return -1;
// Make sure there's a terminating NUL just past the end of what the
// user passed. This matters for sockaddr_un and does no harm for
// other sockaddrs.
((char*)ss)[sa_len] = 0;
return 0;
}
// Copy *ss to *sa, where sa can store at most sa_len bytes. sa_len
// will be updated to store ss_len. This is a common pattern in
// sockets calls.
static int
sockaddr_to_user(userptr<struct sockaddr> sa, userptr<socklen_t> sa_len,
const struct sockaddr_storage *ss, size_t ss_len)
{
if (!sa || !sa_len)
return 0;
socklen_t addrlen;
if (!sa_len.load(&addrlen))
return -1;
if (addrlen > ss_len)
addrlen = ss_len;
if (!userptr<void>(sa.unsafe_get()).store_bytes(ss, addrlen))
return -1;
socklen_t ss_len_2 = ss_len;
if (!sa_len.store(&ss_len_2))
return -1;
return 0;
}
//SYSCALL
long
sys_socket(int domain, int type, int protocol)
{
extern int unixsocket(int domain, int type, int protocol, file **out);
file *f;
int r;
if (domain == PF_LOCAL)
r = unixsocket(domain, type, protocol, &f);
else
r = netsocket(domain, type, protocol, &f);
if (r < 0)
return r;
if (!f)
return -1;
return myproc()->ftable->allocfd(sref<file>::transfer(f), 0, 0);
}
//SYSCALL
long
sys_bind(int xsock, const userptr<struct sockaddr> xaddr, uint32_t xaddrlen)
{
sref<file> f = getfile(xsock);
if (!f)
return -1;
struct sockaddr_storage ss;
if (!xaddr)
return -1;
int r = sockaddr_from_user(&ss, xaddr, xaddrlen);
if (r < 0)
return r;
STRACE_PARAMS("%d, addr<%s>, %u", xsock, ss.ss_family == AF_INET ? "INET" : ss.ss_family == AF_INET6 ? "INET6" : "???", xaddrlen);
return f->bind((ward_sockaddr*)(sockaddr*)&ss, xaddrlen);
}
//SYSCALL
long
sys_listen(int xsock, int backlog)
{
STRACE_PARAMS("%d, %d", xsock, backlog);
sref<file> f = getfile(xsock);
if (!f)
return -1;
return f->listen(backlog);
}
//SYSCALL
long
sys_accept(int xsock, userptr<struct sockaddr> xaddr,
userptr<uint32_t> xaddrlen)
{
sref<file> f = getfile(xsock);
if (!f)
return -1;
struct sockaddr_storage ss;
size_t ss_len;
file *newfp;
int r = f->accept((ward_sockaddr_storage*)&ss, &ss_len, &newfp);
if (r < 0)
return r;
sref<file> newf(sref<file>::transfer(newfp));
if ((r = sockaddr_to_user(xaddr, xaddrlen, &ss, ss_len)) < 0)
return r;
if (!newf)
return -1;
return myproc()->ftable->allocfd(std::move(newf), 0, 0);
}
//SYSCALL
ssize_t
sys_recvfrom(int sockfd, userptr<void> buf, size_t len, int flags,
userptr<struct sockaddr> src_addr, userptr<uint32_t> addrlen)
{
sref<file> f = getfile(sockfd);
if (!f)
return -1;
struct sockaddr_storage ss;
size_t ss_len;
ssize_t size = f->recvfrom(buf, len, flags,
src_addr ? (ward_sockaddr_storage*)&ss : nullptr, &ss_len);
if (size < 0)
return size;
int r = sockaddr_to_user(src_addr, addrlen, &ss, ss_len);
if (r < 0)
return r;
return size;
}
//SYSCALL
ssize_t
sys_sendto(int sockfd, const userptr<void> buf, size_t len, int flags,
const userptr<struct sockaddr> dest_addr, uint32_t addrlen)
{
sref<file> f = getfile(sockfd);
if (!f)
return -1;
// Fetch sockaddr
struct sockaddr_storage ss;
if (dest_addr) {
int r = sockaddr_from_user(&ss, dest_addr, addrlen);
if (r < 0)
return r;
}
return f->sendto(buf, len, flags,
dest_addr ? (ward_sockaddr*)(sockaddr*)&ss : nullptr,
addrlen);
}
//SYSCALL
long
sys_connect(int sockfd, const userptr<struct sockaddr> addr, u32 addrlen)
{
return -1;
}
//SYSCALL
ssize_t
sys_send(int sockfd, const userptr<void> buf, size_t len, int flags)
{
return -1;
}
//SYSCALL
ssize_t
sys_recv(int sockfd, userptr<void> buf, size_t len, int flags)
{
return sys_recvfrom(sockfd, buf, len, flags, nullptr, nullptr);
}