gnunet-zig

GNUnet Bindings for Zig
Log | Files | Refs | README

commit 6ae76988cd09422fbf866559645b70715e53631c
parent ea0f36da8cc34b10866763847ef2ce4b47a70fd8
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Wed, 29 Jan 2025 15:27:39 +0100

add service example

Diffstat:
Mbuild.zig | 24+++++++++++++++++++-----
Asrc/gnunet-service-zig.zig | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 5 deletions(-)

diff --git a/build.zig b/build.zig @@ -17,13 +17,21 @@ pub fn build(b: *std.Build) !void { "additional-lib-paths", "Path to additional libs", ) orelse ""; - + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); const exe = b.addExecutable(.{ - .name = "gnunet-zig-identity", + .name = "gnunet-identity", .root_source_file = b.path("src/gnunet.zig"), - .target = b.standardTargetOptions(.{}), - .optimize = b.standardOptimizeOption(.{}), - .strip = true, + .target = target, + .optimize = optimize, + .strip = false, // TODO make option + }); + const svc_exe = b.addExecutable(.{ + .name = "gnunet-service-zig", + .root_source_file = b.path("src/gnunet-service-zig.zig"), + .target = target, + .optimize = optimize, + .strip = false, // TODO make option }); // create our general purpose allocator var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -39,4 +47,10 @@ pub fn build(b: *std.Build) !void { exe.linkSystemLibrary("gnunetutil"); exe.linkSystemLibrary("gnunetidentity"); b.installArtifact(exe); + svc_exe.addIncludePath(.{ .cwd_relative = gnunet_include_path }); + svc_exe.addLibraryPath(.{ .cwd_relative = gnunet_lib_path }); + svc_exe.addIncludePath(.{ .cwd_relative = additional_include_paths }); + svc_exe.addLibraryPath(.{ .cwd_relative = additional_lib_paths }); + svc_exe.linkSystemLibrary("gnunetutil"); + b.installArtifact(svc_exe); } diff --git a/src/gnunet-service-zig.zig b/src/gnunet-service-zig.zig @@ -0,0 +1,53 @@ +const gnunet = @cImport({ + @cInclude("gnunet/gnunet_util_lib.h"); +}); + +const std = @import("std"); +const print = std.debug.print; + +var id_svc: ?*gnunet.GNUNET_IDENTITY_Handle = undefined; + +pub fn do_shutdown(cls: ?*anyopaque) callconv(.C) void { + _ = cls; + std.debug.print("Shutting down!\n", .{}); +} + +pub fn client_conn(cls: ?*anyopaque, c: ?*gnunet.GNUNET_SERVICE_Client, mq: ?*gnunet.GNUNET_MQ_Handle) callconv(.C) ?*anyopaque { + _ = cls; + _ = c; + _ = mq; + return null; +} + +pub fn client_disco(cls: ?*anyopaque, c: ?*gnunet.GNUNET_SERVICE_Client, internal_cls: ?*anyopaque) callconv(.C) void { + _ = cls; + _ = c; + _ = internal_cls; +} + +pub fn run(cls: ?*anyopaque, cfg: ?*const gnunet.GNUNET_CONFIGURATION_Handle, svc: ?*gnunet.GNUNET_SERVICE_Handle) callconv(.C) void { + // Explicit discard, C compiler usually only warns, zig errors out! + _ = svc; + _ = cfg; + // Recover closure from anonymous anyopaque + const hi: [*:0]u8 = @as([*:0]u8, @ptrCast(cls)); + std.debug.print("{s}\n", .{hi}); + // This does a lot of stuff internally, opening UDS, sending/receiving messages etc. + _ = gnunet.GNUNET_SCHEDULER_add_shutdown(do_shutdown, null); + return; +} + +pub fn main() !u8 { + // you are an anonymous empty object + const argv = std.os.argv; + const c_ptr: [*c][*c]u8 = @ptrCast(argv.ptr); + const hi = "hihihi"; + + const mh: *const gnunet.GNUNET_MQ_MessageHandler = &.{}; + const ret = gnunet.GNUNET_SERVICE_run_(gnunet.GNUNET_OS_project_data_gnunet(), @intCast(std.os.argv.len), c_ptr, "zig-example", gnunet.GNUNET_SERVICE_OPTION_NONE, &run, &client_conn, &client_disco, @constCast(hi), mh); + if (ret != 0) { + std.debug.print("Return val: {d}, expected: {d}\n", .{ ret, gnunet.GNUNET_OK }); + } + return @intCast(ret); + //try stdout.writeAll(res); +}