2023-01-11 16:34:43 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const net = std.net;
|
|
|
|
const fmt = std.fmt;
|
|
|
|
const os = std.os;
|
2023-01-20 02:02:47 +01:00
|
|
|
const testing = std.testing;
|
|
|
|
const print = std.debug.print;
|
2023-01-11 16:34:43 +01:00
|
|
|
|
2023-01-20 02:02:47 +01:00
|
|
|
const ipc = @import("ipc");
|
|
|
|
const hexdump = ipc.hexdump;
|
2023-01-11 16:34:43 +01:00
|
|
|
const Message = ipc.Message;
|
|
|
|
|
|
|
|
// Import send_fd this way in order to produce docs for exchange-fd functions.
|
2023-01-20 02:02:47 +01:00
|
|
|
const exchange_fd = ipc.exchangefd;
|
2023-01-11 16:34:43 +01:00
|
|
|
const send_fd = exchange_fd.send_fd;
|
|
|
|
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const native_os = builtin.target.os.tag;
|
2023-01-20 02:02:47 +01:00
|
|
|
const print_eq = ipc.util.print_eq;
|
|
|
|
const URI = ipc.util.URI;
|
2023-01-11 16:34:43 +01:00
|
|
|
|
2023-01-11 19:58:45 +01:00
|
|
|
fn init_tcp_server(allocator: std.mem.Allocator, server: *net.StreamServer) !i32 {
|
|
|
|
var address = std.process.getEnvVarOwned(allocator, "ADDRESS") catch |err| switch(err) {
|
|
|
|
error.EnvironmentVariableNotFound => blk: {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("no ADDRESS envvar: TCPd will listen on 127.0.0.1:9000\n", .{});
|
2023-01-11 19:58:45 +01:00
|
|
|
break :blk try allocator.dupe(u8, "127.0.0.1:9000");
|
|
|
|
},
|
|
|
|
else => { return err; },
|
|
|
|
};
|
|
|
|
defer allocator.free(address);
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
var iterator = std.mem.split(u8, address, ":");
|
2023-01-11 19:58:45 +01:00
|
|
|
var real_tcp_address = iterator.first();
|
|
|
|
var real_tcp_port = try std.fmt.parseUnsigned(u16, iterator.rest(), 10);
|
|
|
|
|
|
|
|
print ("TCP address [{s}] port [{}]\n", .{real_tcp_address, real_tcp_port});
|
|
|
|
|
|
|
|
server.* = net.StreamServer.init(.{.reuse_address = true});
|
|
|
|
var socket_addr = try net.Address.parseIp(real_tcp_address, real_tcp_port);
|
|
|
|
try server.listen(socket_addr);
|
|
|
|
|
|
|
|
const newfd = server.sockfd orelse return error.SocketLOL; // TODO
|
|
|
|
return newfd;
|
|
|
|
}
|
|
|
|
|
2023-01-11 16:34:43 +01:00
|
|
|
fn create_service() !void {
|
|
|
|
const config = .{.safety = true};
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(config){};
|
|
|
|
defer _ = gpa.deinit();
|
|
|
|
const allocator = gpa.allocator();
|
|
|
|
|
|
|
|
var ctx = try ipc.Context.init(allocator);
|
|
|
|
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
|
|
|
|
|
|
|
|
// SERVER SIDE: creating a service.
|
2023-01-11 19:58:45 +01:00
|
|
|
var service_name = std.process.getEnvVarOwned(allocator, "IPC_SERVICE_NAME") catch |err| switch(err) {
|
|
|
|
error.EnvironmentVariableNotFound => blk: {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("no IPC_SERVICE_NAME envvar: TCPd will be named 'tcp'\n", .{});
|
2023-01-11 19:58:45 +01:00
|
|
|
break :blk try allocator.dupe(u8, "tcp");
|
|
|
|
},
|
|
|
|
else => { return err; },
|
|
|
|
};
|
|
|
|
defer allocator.free(service_name);
|
|
|
|
|
|
|
|
_ = try ctx.server_init(service_name);
|
2023-01-11 16:34:43 +01:00
|
|
|
|
|
|
|
// signal handler, to quit when asked
|
|
|
|
const S = struct {
|
|
|
|
var should_quit: bool = false;
|
|
|
|
|
|
|
|
fn handler(sig: i32, info: *const os.siginfo_t, _: ?*const anyopaque) callconv(.C) void {
|
|
|
|
print ("A signal has been received: {}\n", .{sig});
|
|
|
|
// Check that we received the correct signal.
|
|
|
|
switch (native_os) {
|
|
|
|
.netbsd => {
|
|
|
|
if (sig != os.SIG.HUP or sig != info.info.signo)
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
if (sig != os.SIG.HUP and sig != info.signo)
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
should_quit = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var sa = os.Sigaction{
|
|
|
|
.handler = .{ .sigaction = &S.handler },
|
|
|
|
.mask = os.empty_sigset, // Do not mask any signal.
|
|
|
|
.flags = os.SA.SIGINFO,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Quit on SIGHUP (kill -1).
|
|
|
|
try os.sigaction(os.SIG.HUP, &sa, null);
|
|
|
|
|
2023-01-11 19:58:45 +01:00
|
|
|
var server: net.StreamServer = undefined;
|
|
|
|
var serverfd = try init_tcp_server(allocator, &server);
|
|
|
|
try ctx.add_external (serverfd);
|
|
|
|
|
2023-01-11 16:34:43 +01:00
|
|
|
var some_event: ipc.Event = undefined;
|
2023-01-18 17:22:56 +01:00
|
|
|
var previous_event: ipc.Event.Type = ipc.Event.Type.ERROR;
|
2023-01-11 16:34:43 +01:00
|
|
|
ctx.timer = 1000; // 1 second
|
|
|
|
var count: u32 = 0;
|
|
|
|
while(! S.should_quit) {
|
|
|
|
some_event = try ctx.wait_event();
|
2023-01-18 17:22:56 +01:00
|
|
|
|
|
|
|
// For clarity in the output.
|
|
|
|
if (some_event.t != .TIMER and previous_event == .TIMER ) { print("\n", .{}); }
|
|
|
|
previous_event = some_event.t;
|
|
|
|
|
2023-01-11 16:34:43 +01:00
|
|
|
switch (some_event.t) {
|
|
|
|
.TIMER => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("\rTimer! ({})", .{count});
|
2023-01-11 16:34:43 +01:00
|
|
|
count += 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
.CONNECTION => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("New connection: {} so far!\n", .{ctx.pollfd.items.len});
|
2023-01-11 16:34:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
.DISCONNECTION => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("User {} disconnected, {} remaining.\n"
|
2023-01-11 16:34:43 +01:00
|
|
|
, .{some_event.origin, ctx.pollfd.items.len});
|
|
|
|
},
|
|
|
|
|
|
|
|
.EXTERNAL => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Message received from a non IPC socket.\n", .{});
|
2023-01-11 19:58:45 +01:00
|
|
|
var client = try server.accept(); // net.StreamServer.Connection
|
2023-01-12 21:02:05 +01:00
|
|
|
errdefer client.stream.close();
|
2023-01-11 19:58:45 +01:00
|
|
|
// Receiving a new client from the EXTERNAL socket.
|
|
|
|
// New client = new switch from a distant TCP connection to a
|
|
|
|
// local libipc service.
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
var buffer: [10000]u8 = undefined;
|
2023-01-11 19:58:45 +01:00
|
|
|
var size = try client.stream.read(&buffer);
|
2023-01-12 21:02:05 +01:00
|
|
|
var service_to_contact = buffer[0..size];
|
|
|
|
|
2023-01-18 23:55:48 +01:00
|
|
|
if (service_to_contact.len == 0) {
|
|
|
|
print("Error, no service provided, closing the connection.\n", .{});
|
|
|
|
client.stream.close();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Ask to connect to service [{s}].\n", .{service_to_contact});
|
2023-01-18 23:55:48 +01:00
|
|
|
var servicefd = ctx.connect_service (service_to_contact) catch |err| {
|
|
|
|
print("Error while connecting to the service {s}: {}.\n"
|
|
|
|
, .{service_to_contact, err});
|
|
|
|
print ("Closing the connection.\n", .{});
|
|
|
|
client.stream.close();
|
|
|
|
continue;
|
|
|
|
};
|
2023-01-12 21:02:05 +01:00
|
|
|
errdefer ctx.close_fd (servicefd) catch {};
|
|
|
|
|
2023-01-11 19:58:45 +01:00
|
|
|
print ("Send a message to inform remote TCPd that the connection is established.\n", .{});
|
|
|
|
_ = try client.stream.write("ok");
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Add current client as external connection (for now).\n", .{});
|
2023-01-11 19:58:45 +01:00
|
|
|
try ctx.add_external (client.stream.handle);
|
|
|
|
|
|
|
|
print ("Message sent, switching.\n", .{});
|
|
|
|
try ctx.add_switch(client.stream.handle, servicefd);
|
2023-01-12 21:02:05 +01:00
|
|
|
|
2023-01-11 19:58:45 +01:00
|
|
|
print ("DONE.\n", .{});
|
2023-01-12 21:02:05 +01:00
|
|
|
|
|
|
|
// Some protocols will require to change the default functions
|
|
|
|
// to read and to write on the client socket.
|
|
|
|
// Function to call: ctx.set_switch_callbacks(clientfd, infn, outfn);
|
2023-01-11 16:34:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
.SWITCH_RX => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Message has been received (SWITCH fd {}).\n", .{some_event.origin});
|
2023-01-18 17:22:56 +01:00
|
|
|
// if (some_event.m) |m| {
|
|
|
|
// var hexbuf: [4000]u8 = undefined;
|
|
|
|
// var hexfbs = std.io.fixedBufferStream(&hexbuf);
|
|
|
|
// var hexwriter = hexfbs.writer();
|
|
|
|
// try hexdump.hexdump(hexwriter, "Received", m.payload);
|
|
|
|
// print("{s}\n", .{hexfbs.getWritten()});
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// print ("Message received without actually a message?! {}", .{some_event});
|
|
|
|
// }
|
2023-01-11 16:34:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
.SWITCH_TX => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Message has been sent (SWITCH fd {}).\n", .{some_event.origin});
|
2023-01-11 16:34:43 +01:00
|
|
|
},
|
|
|
|
|
2023-01-20 23:12:04 +01:00
|
|
|
.MESSAGE_RX => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Client asking for a service through TCPd.\n", .{});
|
|
|
|
errdefer ctx.close (some_event.index) catch {};
|
2023-01-11 16:34:43 +01:00
|
|
|
if (some_event.m) |m| {
|
|
|
|
defer m.deinit(); // Do not forget to free the message payload.
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("URI to contact {s}\n", .{m.payload});
|
2023-01-11 16:34:43 +01:00
|
|
|
var uri = URI.read(m.payload);
|
|
|
|
print ("proto [{s}] address [{s}] path [{s}]\n"
|
|
|
|
, .{uri.protocol, uri.address, uri.path});
|
|
|
|
|
2023-01-11 19:58:45 +01:00
|
|
|
var iterator = std.mem.split(u8, uri.address, ":");
|
|
|
|
var real_tcp_address = iterator.first();
|
|
|
|
var real_tcp_port = try std.fmt.parseUnsigned(u16, iterator.rest(), 10);
|
2023-01-12 21:02:05 +01:00
|
|
|
|
2023-01-11 19:58:45 +01:00
|
|
|
var socket_addr = try net.Address.parseIp(real_tcp_address, real_tcp_port);
|
|
|
|
var stream = try net.tcpConnectToAddress(socket_addr);
|
2023-01-12 21:02:05 +01:00
|
|
|
errdefer stream.close();
|
2023-01-11 19:58:45 +01:00
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Writing URI PATH: {s}\n", .{uri.path});
|
2023-01-11 19:58:45 +01:00
|
|
|
_ = try stream.write(uri.path);
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Writing URI PATH - written, waiting for the final 'ok'.\n", .{});
|
|
|
|
var buffer: [10000]u8 = undefined;
|
2023-01-11 19:58:45 +01:00
|
|
|
var size = try stream.read(&buffer);
|
|
|
|
if (! std.mem.eql(u8, buffer[0..size], "ok")) {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("didn't receive 'ok', let's kill the connection\n", .{});
|
2023-01-11 19:58:45 +01:00
|
|
|
stream.close();
|
2023-01-12 21:02:05 +01:00
|
|
|
try ctx.close(some_event.index);
|
2023-01-11 19:58:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Final 'ok' received, sending 'ok' to IPCd.\n", .{});
|
2023-01-11 16:34:43 +01:00
|
|
|
|
|
|
|
// Connection is established, inform IPCd.
|
|
|
|
var response = try Message.init(some_event.origin, allocator, "ok");
|
|
|
|
defer response.deinit();
|
|
|
|
try ctx.write(response);
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Add current client as external connection (for now).\n", .{});
|
2023-01-11 19:58:45 +01:00
|
|
|
try ctx.add_external (stream.handle);
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Finally, add switching\n", .{});
|
2023-01-11 19:58:45 +01:00
|
|
|
try ctx.add_switch(some_event.origin, stream.handle);
|
2023-01-12 00:26:10 +01:00
|
|
|
// Could invoke ctx.set_switch_callbacks but TCP sockets are
|
|
|
|
// working pretty well with default functions.
|
2023-01-11 16:34:43 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TCPd was contacted without providing a message, nothing to do.
|
|
|
|
var response = try Message.init(some_event.origin, allocator, "no");
|
|
|
|
defer response.deinit();
|
|
|
|
try ctx.write(response);
|
|
|
|
try ctx.close(some_event.index);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-01-20 23:12:04 +01:00
|
|
|
.MESSAGE_TX => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Message sent.\n", .{});
|
2023-01-11 16:34:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
.ERROR => {
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("A problem occured, event: {}, let's suicide.\n", .{some_event});
|
2023-01-11 16:34:43 +01:00
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:02:05 +01:00
|
|
|
print ("Goodbye\n", .{});
|
2023-01-11 16:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !u8 {
|
|
|
|
try create_service();
|
|
|
|
return 0;
|
|
|
|
}
|