mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Compare commits
5 Commits
push-tznmz
...
c490d4eec6
| Author | SHA1 | Date | |
|---|---|---|---|
| c490d4eec6 | |||
| a1b3c07f0e | |||
| b619d86665 | |||
| 89bfbe4854 | |||
| fe26cb002d |
21
build.zig
21
build.zig
@@ -1,4 +1,5 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const Step = std.Build.Step;
|
||||||
|
|
||||||
// Although this function looks imperative, note that its job is to
|
// Although this function looks imperative, note that its job is to
|
||||||
// declaratively construct a build graph that will be executed by an external
|
// declaratively construct a build graph that will be executed by an external
|
||||||
@@ -37,13 +38,29 @@ pub fn build(b: *std.Build) void {
|
|||||||
exe_mod.addImport("zaprus", lib_mod);
|
exe_mod.addImport("zaprus", lib_mod);
|
||||||
exe_mod.addImport("clap", b.dependency("clap", .{}).module("clap"));
|
exe_mod.addImport("clap", b.dependency("clap", .{}).module("clap"));
|
||||||
|
|
||||||
const lib = b.addLibrary(.{
|
const static_lib = b.addLibrary(.{
|
||||||
.linkage = .static,
|
.linkage = .static,
|
||||||
.name = "zaprus",
|
.name = "zaprus",
|
||||||
.root_module = lib_mod,
|
.root_module = lib_mod,
|
||||||
});
|
});
|
||||||
|
static_lib.addIncludePath(.{ .cwd_relative = "include" });
|
||||||
|
static_lib.linkLibC();
|
||||||
|
|
||||||
b.installArtifact(lib);
|
b.installArtifact(static_lib);
|
||||||
|
|
||||||
|
const dynamic_lib = b.addLibrary(.{
|
||||||
|
.linkage = .dynamic,
|
||||||
|
.name = "zaprus",
|
||||||
|
.root_module = lib_mod,
|
||||||
|
});
|
||||||
|
dynamic_lib.addIncludePath(.{ .cwd_relative = "include" });
|
||||||
|
dynamic_lib.linkLibC();
|
||||||
|
|
||||||
|
b.installArtifact(dynamic_lib);
|
||||||
|
|
||||||
|
// C Headers
|
||||||
|
const c_header = b.addInstallHeaderFile(b.path("include/zaprus.h"), "zaprus.h");
|
||||||
|
b.getInstallStep().dependOn(&c_header.step);
|
||||||
|
|
||||||
// This creates another `std.Build.Step.Compile`, but this one builds an executable
|
// This creates another `std.Build.Step.Compile`, but this one builds an executable
|
||||||
// rather than a static library.
|
// rather than a static library.
|
||||||
|
|||||||
51
include/zaprus.h
Normal file
51
include/zaprus.h
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
// client
|
||||||
|
|
||||||
|
#include<stdint.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
int zaprus_init(void);
|
||||||
|
|
||||||
|
int zaprus_deinit(void);
|
||||||
|
|
||||||
|
int zaprus_send_relay(const char* payload, size_t len, char dest[4]);
|
||||||
|
|
||||||
|
int zaprus_send_initial_connection(const char* payload, size_t len, uint16_t initial_port);
|
||||||
|
|
||||||
|
struct SaprusMessage* zaprus_connect(const char* payload, size_t len);
|
||||||
|
|
||||||
|
// message
|
||||||
|
#define SAPRUS_RELAY_MESSAGE_TYPE 0x003C
|
||||||
|
#define SAPRUS_FILE_TRANSFER_MESSAGE_TYPE 0x8888
|
||||||
|
#define SAPRUS_CONNECTION_MESSAGE_TYPE 0x00E9
|
||||||
|
|
||||||
|
struct SaprusMessage {
|
||||||
|
uint16_t packet_type;
|
||||||
|
uint16_t payload_len;
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
struct {
|
||||||
|
char dest[4];
|
||||||
|
};
|
||||||
|
} relay;
|
||||||
|
struct {
|
||||||
|
struct {
|
||||||
|
uint16_t src_port;
|
||||||
|
uint16_t dest_port;
|
||||||
|
uint32_t seq_num;
|
||||||
|
uint32_t msg_id;
|
||||||
|
char _reserved;
|
||||||
|
char options;
|
||||||
|
};
|
||||||
|
|
||||||
|
} connection;
|
||||||
|
} headers;
|
||||||
|
char *payload;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ptr should be freed by the caller.
|
||||||
|
int zaprus_message_to_bytes(struct SaprusMessage msg, char** ptr, size_t* len);
|
||||||
|
|
||||||
|
// Return value should be destroyed with zaprus_message_deinit.
|
||||||
|
struct SaprusMessage* zaprus_message_from_bytes(const char* bytes, size_t len);
|
||||||
|
|
||||||
|
void zaprus_message_deinit(struct SaprusMessage* msg);
|
||||||
115
src/c_api.zig
Normal file
115
src/c_api.zig
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
const c = @cImport({
|
||||||
|
@cInclude("zaprus.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
fn zigToCMessage(msg: ?*zaprus.Message) !?*c.SaprusMessage {
|
||||||
|
if (msg) |m| {
|
||||||
|
var res = c.SaprusMessage{
|
||||||
|
.packet_type = @intFromEnum(m.*),
|
||||||
|
};
|
||||||
|
switch (m.*) {
|
||||||
|
.relay => |r| {
|
||||||
|
res.headers.relay = .{
|
||||||
|
.unnamed_0 = .{
|
||||||
|
.dest = r.header.dest,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res.payload_len = @intCast(r.payload.len);
|
||||||
|
res.payload = (try allocator.alloc(u8, r.payload.len)).ptr;
|
||||||
|
},
|
||||||
|
.connection => |con| {
|
||||||
|
res.headers.connection = .{
|
||||||
|
.unnamed_0 = .{
|
||||||
|
.src_port = con.header.src_port,
|
||||||
|
.dest_port = con.header.dest_port,
|
||||||
|
.seq_num = con.header.seq_num,
|
||||||
|
.msg_id = con.header.msg_id,
|
||||||
|
._reserved = con.header.reserved,
|
||||||
|
.options = @bitCast(con.header.options),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
res.payload_len = @intCast(con.payload.len);
|
||||||
|
res.payload = (try allocator.alloc(u8, con.payload.len)).ptr;
|
||||||
|
},
|
||||||
|
.file_transfer => return zaprus.Error.NotImplementedSaprusType,
|
||||||
|
else => return zaprus.Error.UnknownSaprusType,
|
||||||
|
}
|
||||||
|
return &res;
|
||||||
|
} else return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cToZigMessage(msg: ?*c.SaprusMessage) ?*zaprus.Message {
|
||||||
|
_ = msg;
|
||||||
|
return @constCast(&zaprus.Message{
|
||||||
|
.relay = .{
|
||||||
|
.header = .{ .dest = @splat(0) },
|
||||||
|
.payload = &.{0},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// client
|
||||||
|
export fn zaprus_init() c_int {
|
||||||
|
SaprusClient.init() catch return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn zaprus_deinit() c_int {
|
||||||
|
SaprusClient.deinit();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn zaprus_send_relay(payload: [*]const u8, len: usize, dest: [*]u8) c_int {
|
||||||
|
SaprusClient.sendRelay(payload[0..len], dest[0..4].*, allocator) catch return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn zaprus_send_initial_connection(payload: [*]const u8, len: usize, initial_port: u16) c_int {
|
||||||
|
_ = SaprusClient.sendInitialConnection(payload[0..len], initial_port, allocator) catch return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn zaprus_connect(payload: [*]const u8, len: usize) ?*c.SaprusMessage {
|
||||||
|
if (SaprusClient.connect(payload[0..len], allocator)) |msg| {
|
||||||
|
return zigToCMessage(@constCast(&(msg.?))) catch null;
|
||||||
|
} else |_| {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// message
|
||||||
|
|
||||||
|
/// ptr should be freed by the caller.
|
||||||
|
export fn zaprus_message_to_bytes(msg: c.SaprusMessage, ptr: *[*]u8, len: *usize) c_int {
|
||||||
|
if (cToZigMessage(@constCast(&msg))) |m| {
|
||||||
|
const bytes = m.toBytes(allocator) catch return 1;
|
||||||
|
ptr.* = bytes.ptr;
|
||||||
|
len.* = bytes.len;
|
||||||
|
return 0;
|
||||||
|
} else return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return value should be destroyed with zaprus_message_deinit.
|
||||||
|
export fn zaprus_message_from_bytes(bytes: [*]const u8, len: usize) ?*c.SaprusMessage {
|
||||||
|
if (zaprus.Message.fromBytes(bytes[0..len], allocator)) |msg| {
|
||||||
|
return zigToCMessage(@constCast(&msg)) catch null;
|
||||||
|
} else |_| return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export fn zaprus_message_deinit(msg: *c.SaprusMessage) void {
|
||||||
|
// noop
|
||||||
|
_ = msg;
|
||||||
|
// msg.*.deinit(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const zaprus = @import("./root.zig");
|
||||||
|
const SaprusClient = zaprus.Client;
|
||||||
|
|
||||||
|
const allocator = std.heap.c_allocator;
|
||||||
|
|
||||||
|
test {
|
||||||
|
std.testing.refAllDeclsRecursively(@This());
|
||||||
|
}
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
pub const Client = @import("Client.zig");
|
pub const Client = @import("Client.zig");
|
||||||
pub usingnamespace @import("message.zig");
|
pub usingnamespace @import("message.zig");
|
||||||
|
|
||||||
|
pub usingnamespace @import("c_api.zig");
|
||||||
|
|||||||
Reference in New Issue
Block a user