From 3123051ef00ee21857f72cba5117cce4e8aade82 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 18 Jan 2023 23:55:48 +0100 Subject: [PATCH] Fix switch read message + add print debug. --- zig-impl/src/context.zig | 2 +- zig-impl/src/ipcd.zig | 2 +- zig-impl/src/pongd.zig | 7 ++++--- zig-impl/src/switch.zig | 31 +++++++------------------------ zig-impl/src/tcpd.zig | 14 +++++++++++++- zig-impl/src/util.zig | 17 ++++++++++++++++- 6 files changed, 42 insertions(+), 31 deletions(-) diff --git a/zig-impl/src/context.zig b/zig-impl/src/context.zig index 3a3878b..7b6864c 100644 --- a/zig-impl/src/context.zig +++ b/zig-impl/src/context.zig @@ -681,7 +681,7 @@ test "Context - creation, echo once" { try c.server_path("simple-context-test", writer); var path = fbs.getWritten(); - // SERVER SIDE: creating a service. + // SERVER SIDE: creating a service. var server = c.server_init("simple-context-test") catch |err| switch(err) { error.FileNotFound => { print ("\nError: cannot init server at {s}\n", .{path}); diff --git a/zig-impl/src/ipcd.zig b/zig-impl/src/ipcd.zig index a8d245d..65a8c35 100644 --- a/zig-impl/src/ipcd.zig +++ b/zig-impl/src/ipcd.zig @@ -139,7 +139,7 @@ fn create_service() !void { // 3. connect whether asked to and send a message if (final_destination) |dest| { - print("Let's contact {s} (service requested: {s})\n" + print("Connecting to {s} (service requested: {s})\n" , .{dest, service_to_contact}); var uri = URI.read(dest); diff --git a/zig-impl/src/pongd.zig b/zig-impl/src/pongd.zig index 85ea0e9..2019fc7 100644 --- a/zig-impl/src/pongd.zig +++ b/zig-impl/src/pongd.zig @@ -15,7 +15,7 @@ const builtin = @import("builtin"); const native_os = builtin.target.os.tag; const print = std.debug.print; const testing = std.testing; -const print_eq = @import("./util.zig").print_eq; +const util = @import("./util.zig"); fn create_service() !void { const config = .{.safety = true}; @@ -81,8 +81,9 @@ fn create_service() !void { .MESSAGE => { if (some_event.m) |m| { - print("New message: {}\n", .{m}); - print("Let's echo it...\n", .{}); + print("New message ({} bytes)\n", .{m.payload.len}); + util.print_message ("RECEIVED MESSAGE", m); + print("Echoing it...\n", .{}); try ctx.schedule(m); } else { diff --git a/zig-impl/src/switch.zig b/zig-impl/src/switch.zig index 9117dd7..18fbe8e 100644 --- a/zig-impl/src/switch.zig +++ b/zig-impl/src/switch.zig @@ -11,7 +11,8 @@ const CBEventType = ipc.CBEvent.Type; const Allocator = std.mem.Allocator; -const print_eq = @import("./util.zig").print_eq; +const util = @import("./util.zig"); +const print_eq = util.print_eq; const print = std.debug.print; const Event = ipc.Event; @@ -80,7 +81,6 @@ pub const SwitchDB = struct { var message_size: u32 = @truncate(u32, buffer.len); var r: CBEventType = managedconnection.in(fd, &buffer, &message_size); - print ("MESSAGE READ: {} (from {} to {})\n", .{r, fd, managedconnection.dest}); switch (r) { // The message should be ignored (protocol specific). CBEventType.IGNORE => { return null; }, @@ -89,9 +89,9 @@ pub const SwitchDB = struct { // TODO: better allocator? // TODO: better errors? var message: Message - = Message.init(managedconnection.dest - , std.heap.c_allocator - , buffer[0..message_size]) catch { + = Message.read(managedconnection.dest + , buffer[0..message_size] + , std.heap.c_allocator) catch { return error.generic; }; return message; @@ -295,44 +295,27 @@ test "nuke 'em" { } fn default_in (origin: i32, mcontent: [*]u8, mlen: *u32) CBEventType { - // print ("receiving a message originated from {}\n", .{origin}); - // This may be kinda hacky, idk. var stream: net.Stream = .{ .handle = origin }; var packet_size: usize = stream.read(mcontent[0..mlen.*]) catch return CBEventType.ERROR; // Let's handle this as a disconnection. if (packet_size < 4) { - print("message is less than 4 bytes ({} bytes)\n", .{packet_size}); + // print("message is less than 4 bytes ({} bytes)\n", .{packet_size}); return CBEventType.FD_CLOSING; } mlen.* = @truncate(u32, packet_size); - var hexbuf: [4000]u8 = undefined; - var hexfbs = std.io.fixedBufferStream(&hexbuf); - var hexwriter = hexfbs.writer(); - hexdump.hexdump(hexwriter, "DEFAULT IN: MESSAGE RECEIVED", mcontent[0..packet_size]) catch unreachable; - print("{s}\n", .{hexfbs.getWritten()}); - print("packet_size {}, mlen.* {}\n", .{packet_size, mlen.*}); - return CBEventType.NO_ERROR; } fn default_out (fd: i32, mcontent: [*]const u8, mlen: u32) CBEventType { - // print ("sending a message originated from {}\n", .{origin}); // Message contains the fd, no need to search for the right structure to copy, // let's just recreate a Stream from the fd. var to_send = mcontent[0..mlen]; - var hexbuf: [4000]u8 = undefined; - var hexfbs = std.io.fixedBufferStream(&hexbuf); - var hexwriter = hexfbs.writer(); - hexdump.hexdump(hexwriter, "DEFAULT OUT: MESSAGE TO SEND", to_send) catch unreachable; - print("{s}\n", .{hexfbs.getWritten()}); - var stream = net.Stream { .handle = fd }; - var bytes_sent = stream.write (to_send) catch return CBEventType.ERROR; - print("sent {} to {}\n", .{bytes_sent, fd}); + _ = stream.write (to_send) catch return CBEventType.ERROR; return CBEventType.NO_ERROR; } diff --git a/zig-impl/src/tcpd.zig b/zig-impl/src/tcpd.zig index 31e4a5e..17447c9 100644 --- a/zig-impl/src/tcpd.zig +++ b/zig-impl/src/tcpd.zig @@ -135,8 +135,20 @@ fn create_service() !void { var size = try client.stream.read(&buffer); var service_to_contact = buffer[0..size]; + if (service_to_contact.len == 0) { + print("Error, no service provided, closing the connection.\n", .{}); + client.stream.close(); + continue; + } + print ("Ask to connect to service [{s}].\n", .{service_to_contact}); - var servicefd = try ctx.connect_service (service_to_contact); + 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; + }; errdefer ctx.close_fd (servicefd) catch {}; print ("Send a message to inform remote TCPd that the connection is established.\n", .{}); diff --git a/zig-impl/src/util.zig b/zig-impl/src/util.zig index 73ec334..e081cee 100644 --- a/zig-impl/src/util.zig +++ b/zig-impl/src/util.zig @@ -1,7 +1,10 @@ const std = @import("std"); -// const hexdump = @import("./hexdump.zig"); +const hexdump = @import("./hexdump.zig"); const testing = std.testing; +const print = std.debug.print; +const Message = @import("./message.zig").Message; + /// A VERY LIGHT and INCOMPLETE way of decoding URI. /// DO NOT USE IT UNLESS YOU KNOW WHAT TO EXPECT. pub const URI = struct { @@ -34,6 +37,18 @@ test "URI simple decoding" { try testing.expectEqualSlices(u8, uri.path, "some-path"); } +pub fn print_buffer (header: []const u8, buffer: []const u8) void { + var hexbuf: [4000]u8 = undefined; + var hexfbs = std.io.fixedBufferStream(&hexbuf); + var hexwriter = hexfbs.writer(); + hexdump.hexdump(hexwriter, header, buffer) catch unreachable; + print("{s}\n", .{hexfbs.getWritten()}); +} + +pub fn print_message (header: []const u8, m: Message) void { + print_buffer (header, m.payload); +} + pub fn print_eq(expected: anytype, obj: anytype) !void { var buffer: [4096]u8 = undefined; var fbs = std.io.fixedBufferStream(&buffer);