2022-12-27 22:39:23 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const net = std.net;
|
|
|
|
const fmt = std.fmt;
|
2022-12-29 12:16:12 +01:00
|
|
|
const os = std.os;
|
2022-12-27 22:39:23 +01:00
|
|
|
|
2023-01-20 02:02:47 +01:00
|
|
|
const ipc = @import("ipc");
|
|
|
|
const hexdump = ipc.hexdump;
|
2023-01-03 10:56:01 +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-03 10:56:01 +01:00
|
|
|
const send_fd = exchange_fd.send_fd;
|
2022-12-27 22:39:23 +01:00
|
|
|
|
2022-12-29 12:16:12 +01:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const native_os = builtin.target.os.tag;
|
2022-12-27 22:39:23 +01:00
|
|
|
const print = std.debug.print;
|
|
|
|
const testing = std.testing;
|
2023-01-20 02:02:47 +01:00
|
|
|
const print_eq = ipc.util.print_eq;
|
|
|
|
const URI = ipc.util.URI;
|
2022-12-27 22:39:23 +01:00
|
|
|
|
2023-01-05 17:28:24 +01:00
|
|
|
// Standard library is unecessary complex regarding networking.
|
|
|
|
// libipc drops it and uses plain old file descriptors instead.
|
2022-12-27 22:39:23 +01:00
|
|
|
|
2023-01-05 17:28:24 +01:00
|
|
|
// API should completely obfuscate the inner structures.
|
|
|
|
// Only libipc structures should be necessary to write any networking code,
|
|
|
|
// users should only work with Context and Message, mostly.
|
2022-12-27 22:39:23 +01:00
|
|
|
|
2023-01-05 17:28:24 +01:00
|
|
|
// QUESTION: should libipc use std.fs.path and not simple [] const u8?
|
2022-12-27 22:39:23 +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-05 17:28:24 +01:00
|
|
|
_ = try ctx.server_init("ipc");
|
2022-12-27 22:39:23 +01:00
|
|
|
|
2022-12-29 12:16:12 +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);
|
2022-12-27 22:39:23 +01:00
|
|
|
|
|
|
|
var some_event: ipc.Event = undefined;
|
2023-01-04 13:29:40 +01:00
|
|
|
ctx.timer = 1000; // 1 second
|
2023-01-04 11:34:49 +01:00
|
|
|
var count: u32 = 0;
|
2022-12-29 12:16:12 +01:00
|
|
|
while(! S.should_quit) {
|
2022-12-27 22:39:23 +01:00
|
|
|
some_event = try ctx.wait_event();
|
|
|
|
switch (some_event.t) {
|
|
|
|
.TIMER => {
|
2023-01-04 11:34:49 +01:00
|
|
|
print("\rTimer! ({})", .{count});
|
|
|
|
count += 1;
|
2022-12-27 22:39:23 +01:00
|
|
|
},
|
2022-12-29 10:12:40 +01:00
|
|
|
|
|
|
|
.CONNECTION => {
|
|
|
|
print("New connection: {} so far!\n", .{ctx.pollfd.items.len});
|
|
|
|
},
|
|
|
|
|
|
|
|
.DISCONNECTION => {
|
|
|
|
print("User {} disconnected, {} remainaing.\n"
|
|
|
|
, .{some_event.origin, ctx.pollfd.items.len});
|
|
|
|
},
|
|
|
|
|
2022-12-27 22:39:23 +01:00
|
|
|
.EXTERNAL => {
|
|
|
|
print("Message received from a non IPC socket.\n", .{});
|
|
|
|
print("NOT IMPLEMENTED, YET. It's a suicide, then.\n", .{});
|
|
|
|
break;
|
|
|
|
},
|
2022-12-29 10:12:40 +01:00
|
|
|
|
2023-01-10 17:09:34 +01:00
|
|
|
.SWITCH_RX => {
|
|
|
|
print("Message has been received (SWITCH).\n", .{});
|
|
|
|
print("NOT IMPLEMENTED, YET. It's a suicide, then.\n", .{});
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
|
|
|
|
.SWITCH_TX => {
|
|
|
|
print("Message has been sent (SWITCH).\n", .{});
|
2022-12-27 22:39:23 +01:00
|
|
|
print("NOT IMPLEMENTED, YET. It's a suicide, then.\n", .{});
|
|
|
|
break;
|
|
|
|
},
|
2022-12-29 10:12:40 +01:00
|
|
|
|
2023-01-20 23:12:04 +01:00
|
|
|
.MESSAGE_RX => {
|
2022-12-27 22:39:23 +01:00
|
|
|
print("Client asking for a service through ipcd.\n", .{});
|
2023-01-11 15:05:16 +01:00
|
|
|
defer ctx.close_fd (some_event.origin) catch {};
|
2023-01-03 10:56:01 +01:00
|
|
|
if (some_event.m) |m| {
|
2023-01-04 13:29:40 +01:00
|
|
|
print("{}\n", .{m});
|
2023-01-11 15:05:16 +01:00
|
|
|
defer m.deinit(); // Do not forget to free the message payload.
|
2023-01-04 11:34:49 +01:00
|
|
|
|
2023-01-03 10:56:01 +01:00
|
|
|
// 1. split message
|
2023-01-04 11:34:49 +01:00
|
|
|
var iterator = std.mem.split(u8, m.payload, ";");
|
|
|
|
var service_to_contact = iterator.first();
|
2023-01-04 13:29:40 +01:00
|
|
|
// print("service to contact: {s}\n", .{service_to_contact});
|
2023-01-04 11:34:49 +01:00
|
|
|
var final_destination: ?[]const u8 = null;
|
|
|
|
|
2023-01-03 10:56:01 +01:00
|
|
|
// 2. find relevant part of the message
|
2023-01-04 11:34:49 +01:00
|
|
|
while (iterator.next()) |next| {
|
2023-01-04 13:29:40 +01:00
|
|
|
// print("next part: {s}\n", .{next});
|
2023-01-04 11:34:49 +01:00
|
|
|
var iterator2 = std.mem.split(u8, next, " ");
|
|
|
|
var sname = iterator2.first();
|
|
|
|
var target = iterator2.next();
|
|
|
|
if (target) |t| {
|
2023-01-04 13:29:40 +01:00
|
|
|
// print ("sname: {s} - target: {s}\n", .{sname, t});
|
2023-01-04 11:34:49 +01:00
|
|
|
if (std.mem.eql(u8, service_to_contact, sname)) {
|
|
|
|
final_destination = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print("ERROR: no target in: {s}\n", .{next});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 16:30:08 +01:00
|
|
|
// 3. connect whether asked to and send a message
|
2023-01-04 11:34:49 +01:00
|
|
|
if (final_destination) |dest| {
|
2023-01-18 23:55:48 +01:00
|
|
|
print("Connecting to {s} (service requested: {s})\n"
|
2023-01-04 13:29:40 +01:00
|
|
|
, .{dest, service_to_contact});
|
2023-01-04 11:34:49 +01:00
|
|
|
|
2023-01-11 16:30:08 +01:00
|
|
|
var uri = URI.read(dest);
|
2023-01-04 11:46:34 +01:00
|
|
|
|
2023-01-11 15:05:16 +01:00
|
|
|
// 1. in case there is no URI
|
2023-01-11 16:30:08 +01:00
|
|
|
if (std.mem.eql(u8, uri.protocol, dest)) {
|
2023-01-11 15:05:16 +01:00
|
|
|
var newfd = try ctx.connect_service (dest);
|
|
|
|
send_fd (some_event.origin, "ok", newfd);
|
|
|
|
try ctx.close_fd (newfd);
|
|
|
|
}
|
2023-01-11 16:30:08 +01:00
|
|
|
else if (std.mem.eql(u8, uri.protocol, "unix")) {
|
|
|
|
var newfd = try ctx.connect_service (uri.address);
|
2023-01-11 15:05:16 +01:00
|
|
|
send_fd (some_event.origin, "ok", newfd);
|
|
|
|
try ctx.close_fd (newfd);
|
|
|
|
}
|
|
|
|
// 2. else, contact <protocol>d or directly the dest in case there is none.
|
|
|
|
else {
|
2023-01-11 16:30:08 +01:00
|
|
|
var servicefd = try ctx.connect_service (uri.protocol);
|
2023-01-11 15:05:16 +01:00
|
|
|
defer ctx.close_fd (servicefd) catch {};
|
|
|
|
// TODO: make a simple protocol between IPCd and <protocol>d
|
|
|
|
// NEED inform about the connection (success or fail)
|
|
|
|
// FIRST DRAFT:
|
|
|
|
// - IPCd: send a message containing the destination
|
|
|
|
// - PROTOCOLd: send "ok" to inform the connection is established
|
|
|
|
// - PROTOCOLd: send "no" in case there was an error
|
|
|
|
|
|
|
|
var message = try Message.init(servicefd, allocator, dest);
|
|
|
|
defer message.deinit();
|
|
|
|
try ctx.write(message);
|
|
|
|
var response_from_service = try ctx.read_fd(servicefd);
|
|
|
|
if (response_from_service) |r| {
|
2023-01-11 15:59:35 +01:00
|
|
|
defer r.deinit();
|
2023-01-11 15:05:16 +01:00
|
|
|
if (std.mem.eql(u8, r.payload, "ok")) {
|
|
|
|
// OK
|
2023-01-11 16:30:08 +01:00
|
|
|
// print("service has established the connection\n", .{});
|
2023-01-11 15:05:16 +01:00
|
|
|
send_fd (some_event.origin, "ok", servicefd);
|
|
|
|
}
|
|
|
|
else if (std.mem.eql(u8, r.payload, "ne")) {
|
|
|
|
// PROBLEM
|
|
|
|
print("service cannot establish the connection\n", .{});
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print("service isn't working properly, its response is: {s}\n", .{r.payload});
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No message = should be handled as a disconnection.
|
|
|
|
print("No response from service: let's drop everything\n", .{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 10:56:01 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// There is a problem: ipcd was contacted without providing
|
|
|
|
// a message, meaning there is nothing to do. This should be
|
|
|
|
// explicitely warned about.
|
2023-01-03 18:18:16 +01:00
|
|
|
var response = try Message.init(some_event.origin
|
2023-01-03 10:56:01 +01:00
|
|
|
, allocator
|
|
|
|
, "lookup message without data");
|
2023-01-11 15:05:16 +01:00
|
|
|
defer response.deinit();
|
2023-01-03 18:18:16 +01:00
|
|
|
try ctx.write(response);
|
2023-01-03 10:56:01 +01:00
|
|
|
}
|
2022-12-27 22:39:23 +01:00
|
|
|
},
|
2022-12-29 10:12:40 +01:00
|
|
|
|
2023-01-20 23:12:04 +01:00
|
|
|
.MESSAGE_TX => {
|
2022-12-27 22:39:23 +01:00
|
|
|
print("Message sent.\n", .{});
|
|
|
|
},
|
2022-12-29 10:12:40 +01:00
|
|
|
|
2022-12-27 22:39:23 +01:00
|
|
|
.ERROR => {
|
|
|
|
print("A problem occured, event: {}, let's suicide\n", .{some_event});
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 12:16:12 +01:00
|
|
|
print("Goodbye\n", .{});
|
2022-12-27 22:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !u8 {
|
|
|
|
try create_service();
|
|
|
|
return 0;
|
|
|
|
}
|