Repository navigation
Expand file tree
/
Copy pathsocket.wave
More file actions
211 lines (186 loc) · 5.28 KB
/
Copy pathsocket.wave
File metadata and controls
211 lines (186 loc) · 5.28 KB
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
// This file is part of the Wave language project.
// Copyright (c) 2024-2026 Wave Foundation
// Copyright (c) 2024-2026 LunaStev and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// =======================================================
// macOS socket syscalls
// =======================================================
//
// Raw socket layer for macOS built on syscall.wave.
// =======================================================
import("std::sys::macos::arm64::syscall")::{
syscall0,
syscall1,
syscall2,
syscall3,
syscall4,
syscall5,
syscall6,
};
import("std::sys::macos::arm64::fs")::{close};
pub const AF_INET: i32 = 2;
pub const AF_INET6: i32 = 30;
pub const SOCK_STREAM: i32 = 1;
pub const SOCK_DGRAM: i32 = 2;
pub const IPPROTO_IP: i32 = 0;
pub const IPPROTO_IPV6: i32 = 41;
pub const IPPROTO_TCP: i32 = 6;
pub const IPPROTO_UDP: i32 = 17;
pub const SHUT_RD: i32 = 0;
pub const SHUT_WR: i32 = 1;
pub const SHUT_RDWR: i32 = 2;
pub const SOL_SOCKET: i32 = 0xFFFF;
pub const SO_REUSEADDR: i32 = 0x0004;
pub const SO_REUSEPORT: i32 = 0x0200;
pub const SO_ERROR: i32 = 0x1007;
pub const SO_RCVBUF: i32 = 0x1002;
pub const SO_SNDBUF: i32 = 0x1001;
pub const SO_BROADCAST: i32 = 0x0020;
pub const SO_KEEPALIVE: i32 = 0x0008;
pub const TCP_NODELAY: i32 = 1;
pub const IP_TTL: i32 = 4;
pub const IP_MULTICAST_IF: i32 = 9;
pub const IP_MULTICAST_TTL: i32 = 10;
pub const IP_MULTICAST_LOOP: i32 = 11;
pub const IP_ADD_MEMBERSHIP: i32 = 12;
pub const IP_DROP_MEMBERSHIP: i32 = 13;
pub const IPV6_MULTICAST_IF: i32 = 9;
pub const IPV6_MULTICAST_HOPS: i32 = 10;
pub const IPV6_MULTICAST_LOOP: i32 = 11;
pub const IPV6_JOIN_GROUP: i32 = 12;
pub const IPV6_LEAVE_GROUP: i32 = 13;
pub const SO_NOSIGPIPE: i32 = 0x1022;
pub const MSG_NOSIGNAL: i32 = 0;
pub const MSG_DONTWAIT: i32 = 128;
pub const POLLIN: i16 = 0x001;
pub const POLLOUT: i16 = 0x004;
pub const POLLERR: i16 = 0x008;
pub const POLLHUP: i16 = 0x010;
pub const POLLNVAL: i16 = 0x020;
pub struct PollFd {
fd: i32;
events: i16;
revents: i16;
}
pub fun socket(domain: i32, ty: i32, protocol: i32) -> i64 {
return syscall3(97, domain as i64, ty as i64, protocol as i64);
}
pub fun bind(fd: i64, addr: ptr<i8>, len: i32) -> i64 {
return syscall3(104, fd, addr as i64, len as i64);
}
pub fun listen(fd: i64, backlog: i32) -> i64 {
return syscall2(106, fd, backlog as i64);
}
pub fun accept(fd: i64, addr: ptr<i8>, len: ptr<i32>) -> i64 {
return syscall3(30, fd, addr as i64, len as i64);
}
pub fun connect(fd: i64, addr: ptr<i8>, len: i32) -> i64 {
return syscall3(98, fd, addr as i64, len as i64);
}
pub fun getsockname(fd: i64, addr: ptr<i8>, len: ptr<i32>) -> i64 {
return syscall3(32, fd, addr as i64, len as i64);
}
pub fun getpeername(fd: i64, addr: ptr<i8>, len: ptr<i32>) -> i64 {
return syscall3(31, fd, addr as i64, len as i64);
}
pub fun shutdown(fd: i64, how: i32) -> i64 {
return syscall2(134, fd, how as i64);
}
pub fun close_socket(fd: i64) -> i64 { return close(fd); }
pub fun setsockopt(
fd: i64,
level: i32,
optname: i32,
optval: ptr<i8>,
optlen: i32
) -> i64 {
return syscall5(
105,
fd,
level as i64,
optname as i64,
optval as i64,
optlen as i64
);
}
pub fun getsockopt(
fd: i64,
level: i32,
optname: i32,
optval: ptr<i8>,
optlen: ptr<i32>
) -> i64 {
return syscall5(
118,
fd,
level as i64,
optname as i64,
optval as i64,
optlen as i64
);
}
pub fun poll(fds: ptr<PollFd>, nfds: i64, timeout_ms: i32) -> i64 {
return syscall3(230, fds as i64, nfds, timeout_ms as i64);
}
pub fun send(fd: i64, buf: ptr<u8>, len: i64, flags: i32) -> i64 {
return sendto(fd, buf, len, flags, null, 0);
}
pub fun recv(fd: i64, buf: ptr<u8>, len: i64, flags: i32) -> i64 {
return recvfrom(fd, buf, len, flags, null, null);
}
pub fun sendto(
fd: i64,
buf: ptr<u8>,
len: i64,
flags: i32,
addr: ptr<i8>,
addrlen: i32
) -> i64 {
// XNU uses MSG_DONTWAIT for the send-buffer lock and MSG_NBIO for
// waiting on buffer space. Raw per-call nonblocking sends need both;
// toggling O_NONBLOCK would also change every descriptor alias.
var send_flags: i32 = flags;
if ((flags & MSG_DONTWAIT) != 0) {
send_flags = flags | 0x20000;
}
return syscall6(
133,
fd,
buf as i64,
len,
send_flags as i64,
addr as i64,
addrlen as i64
);
}
pub fun recvfrom(
fd: i64,
buf: ptr<u8>,
len: i64,
flags: i32,
addr: ptr<i8>,
addrlen: ptr<i32>
) -> i64 {
return syscall6(
29,
fd,
buf as i64,
len,
flags as i64,
addr as i64,
addrlen as i64
);
}