-
Notifications
You must be signed in to change notification settings - Fork 1
/
netext.zig
276 lines (262 loc) · 8.76 KB
/
netext.zig
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
///
/// network functions that both log errors and return actionable error codes
///
const std = @import("std");
const os = std.os;
const net = std.net;
const fd_t = os.fd_t;
const socket_t = os.socket_t;
const Address = net.Address;
const logging = @import("./logging.zig");
const common = @import("./common.zig");
const proxy = @import("./proxy.zig");
const panic = std.debug.panic;
const log = logging.log;
const Proxy = proxy.Proxy;
/// logs errors and returns either fatal or retry
pub fn socket(domain: u32, socketType: u32, proto: u32) !socket_t {
return os.socket(domain, socketType, proto) catch |e| switch (e) {
error.ProcessFdQuotaExceeded
,error.SystemFdQuotaExceeded
,error.SystemResources
=> {
log("WARNING: socket function error: {}", .{e});
return error.Retry;
},
error.PermissionDenied
,error.AddressFamilyNotSupported
,error.SocketTypeNotSupported
,error.ProtocolFamilyNotAvailable
,error.ProtocolNotSupported
,error.Unexpected
=> panic("socket function failed with: {}", .{e}),
};
}
pub fn connect(sockfd: socket_t, addr: *const Address) !void {
return common.connect(sockfd, addr) catch |e| switch (e) {
error.AddressNotAvailable
,error.AddressInUse
,error.ConnectionRefused
,error.ConnectionTimedOut
,error.ConnectionResetByPeer
,error.NetworkUnreachable
,error.SystemResources
,error.WouldBlock
,error.ConnectionPending
=> {
log("WARNING: connect function returned error: {}", .{e});
return error.Retry;
},
error.PermissionDenied
,error.AddressFamilyNotSupported
,error.Unexpected
,error.FileNotFound
=> panic("connect function failed with: {}", .{e}),
};
}
pub fn proxyConnect(prox: *const Proxy, host: []const u8, port: u16) !socket_t {
return prox.connectHost(host, port) catch |e| switch (e) {
error.AddressNotAvailable
,error.AddressInUse
,error.ConnectionRefused
,error.ConnectionTimedOut
,error.NetworkUnreachable
,error.SystemResources
,error.ProcessFdQuotaExceeded
,error.SystemFdQuotaExceeded
,error.WouldBlock
,error.SendReturnedZero
,error.ConnectionResetByPeer
,error.MessageTooBig
,error.BrokenPipe
,error.InputOutput
,error.NotOpenForReading
,error.OperationAborted
,error.HttpProxyDisconnectedDurringReply
,error.HttpProxyUnexpectedReply
,error.NetworkSubsystemFailed
,error.ConnectionPending
=> {
log("WARNING: proxy connectHost returned error: {}", .{e});
return error.Retry;
},
error.AccessDenied
,error.PermissionDenied
,error.AddressFamilyNotSupported
,error.Unexpected
,error.FileDescriptorNotASocket
,error.FileNotFound
,error.ProtocolFamilyNotAvailable
,error.ProtocolNotSupported
,error.DnsNotSupported
,error.IsDir
,error.FastOpenAlreadyInProgress // this is from sendto, EALREADY, not sure what it means
,error.SocketTypeNotSupported
=> panic("proxy connectHost failed with: {}", .{e}),
};
}
pub fn send(sockfd: socket_t, buf: []const u8, flags: u32) !void {
common.sendfull(sockfd, buf, flags) catch |e| switch (e) {
error.ConnectionResetByPeer
,error.BrokenPipe
,error.NetworkUnreachable
,error.NetworkSubsystemFailed
=> {
log("send function error: {}", .{e});
return error.Disconnected;
},
error.WouldBlock
,error.MessageTooBig
,error.SystemResources
,error.SendReturnedZero
=> {
log("WARNING: send function error: {}", .{e});
return error.Retry;
},
error.AccessDenied
,error.FastOpenAlreadyInProgress // don't know what this is
,error.FileDescriptorNotASocket
,error.Unexpected
=> panic("send function failed with: {}", .{e}),
};
}
pub fn read(fd: fd_t, buf: []u8) !usize {
return os.read(fd, buf) catch |e| switch (e) {
error.BrokenPipe
,error.ConnectionResetByPeer
,error.ConnectionTimedOut
,error.InputOutput
,error.NotOpenForReading
=> {
log("read function disconnect error: {}", .{e});
return error.Disconnected;
},
error.WouldBlock
,error.SystemResources
,error.OperationAborted
=> {
log("WARNING: read function retry error: {}", .{e});
return error.Retry;
},
error.IsDir
,error.Unexpected
,error.AccessDenied
=> panic("read function failed with: {}", .{e}),
};
}
pub fn recvfullTimeout(sockfd: socket_t, buf: []u8, timeoutMillis: u32) !bool {
return common.recvfullTimeout(sockfd, buf, timeoutMillis) catch |e| switch (e) {
error.BrokenPipe
,error.ConnectionResetByPeer
,error.ConnectionTimedOut
,error.InputOutput
,error.NotOpenForReading
=> {
log("read function disconnect error: {}", .{e});
return error.Disconnected;
},
error.Retry => return error.Retry, // already logged
error.WouldBlock
,error.SystemResources
,error.OperationAborted
=> {
log("WARNING: read function retry error: {}", .{e});
return error.Retry;
},
error.IsDir
,error.Unexpected
,error.AccessDenied
=> panic("read function failed with: {}", .{e}),
};
}
pub fn setsockopt(sockfd: socket_t, level: u32, optname: u32, opt: []const u8) !void {
os.setsockopt(sockfd, level, optname, opt) catch |e| switch (e) {
error.SystemResources
,error.NetworkSubsystemFailed
=> {
log("WARNING: setsockopt function error: {}", .{e});
return error.Retry;
},
error.InvalidProtocolOption
,error.FileDescriptorNotASocket
,error.TimeoutTooBig
,error.AlreadyConnected
,error.SocketNotBound
,error.Unexpected
,error.PermissionDenied
=> panic("setsockopt function fatal with: {}", .{e}),
};
}
pub fn bind(sockfd: socket_t, addr: *const os.sockaddr, len: os.socklen_t) !void {
os.bind(sockfd, addr, len) catch |e| switch (e) {
error.SystemResources
,error.AddressInUse
,error.AddressNotAvailable
,error.NetworkSubsystemFailed
=> {
log("WARNING: bind function error: {}", .{e});
return error.Retry;
},
error.AccessDenied
,error.AlreadyBound
,error.Unexpected
,error.FileNotFound
,error.FileDescriptorNotASocket
,error.NotDir
,error.ReadOnlyFileSystem
,error.SymLinkLoop
,error.NameTooLong
=> panic("bind function failed with: {}", .{e}),
};
}
pub fn listen(sockfd: socket_t, backlog: u31) !void {
os.listen(sockfd, backlog) catch |e| switch (e) {
error.AddressInUse
,error.NetworkSubsystemFailed
,error.SystemResources
=> {
log("WARNING: listen function error: {}", .{e});
return error.Retry;
},
error.OperationNotSupported
,error.AlreadyConnected
,error.FileDescriptorNotASocket
,error.SocketNotBound
,error.Unexpected
=> panic("listen function failed with: {}", .{e}),
};
}
pub fn makeListenSock(addr: *std.net.Address, backlog: u31) !socket_t {
const sockfd = try socket(addr.any.family, os.SOCK.STREAM, os.IPPROTO.TCP);
errdefer os.close(sockfd);
try setsockopt(sockfd, os.SOL.SOCKET, os.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1)));
try bind(sockfd, &addr.any, addr.getOsSockLen());
try listen(sockfd, backlog);
return sockfd;
}
pub fn accept(sockfd: socket_t, addr: *os.sockaddr, addr_size: *os.socklen_t, flags: u32) !socket_t {
return os.accept(sockfd, addr, addr_size, flags) catch |e| switch (e) {
error.ConnectionAborted
,error.ConnectionResetByPeer
,error.ProtocolFailure
,error.BlockedByFirewall
,error.WouldBlock
=> {
log("accept dropped client: {}", .{e});
return error.ClientDropped;
},
error.SystemResources
,error.ProcessFdQuotaExceeded
,error.SystemFdQuotaExceeded
,error.NetworkSubsystemFailed
=> {
log("WARNING: accept function error: {}", .{e});
return error.Retry;
},
error.Unexpected
,error.FileDescriptorNotASocket
,error.SocketNotListening
,error.OperationNotSupported
=> panic("accept function failed with: {}", .{e}),
};
}