initial coommit
This commit is contained in:
commit
5c5ea52deb
7 changed files with 134 additions and 0 deletions
3
.gitattributes
vendored
Normal file
3
.gitattributes
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
* text=auto
|
||||||
|
*.zig text eol=lf
|
||||||
|
zigmod.* text eol=lf
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
zig-*
|
||||||
|
.zigmod
|
||||||
|
deps.zig
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 Luna
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
36
build.zig
Normal file
36
build.zig
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const deps = @import("deps.zig");
|
||||||
|
|
||||||
|
pub fn build(b: *std.build.Builder) void {
|
||||||
|
// Standard target options allows the person running `zig build` to choose
|
||||||
|
// what target to build for. Here we do not override the defaults, which
|
||||||
|
// means any target is allowed, and the default is native. Other options
|
||||||
|
// for restricting supported target set are available.
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
||||||
|
// Standard release options allow the person running `zig build` to select
|
||||||
|
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||||
|
const mode = b.standardReleaseOptions();
|
||||||
|
|
||||||
|
const exe = b.addExecutable("unicorn_screams", "src/main.zig");
|
||||||
|
exe.setTarget(target);
|
||||||
|
exe.setBuildMode(mode);
|
||||||
|
exe.install();
|
||||||
|
deps.addAllTo(exe);
|
||||||
|
|
||||||
|
const run_cmd = exe.run();
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
if (b.args) |args| {
|
||||||
|
run_cmd.addArgs(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
const run_step = b.step("run", "Run the app");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
const exe_tests = b.addTest("src/main.zig");
|
||||||
|
exe_tests.setTarget(target);
|
||||||
|
exe_tests.setBuildMode(mode);
|
||||||
|
|
||||||
|
const test_step = b.step("test", "Run unit tests");
|
||||||
|
test_step.dependOn(&exe_tests.step);
|
||||||
|
}
|
63
src/main.zig
Normal file
63
src/main.zig
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const network = @import("network");
|
||||||
|
|
||||||
|
const logger = std.log.scoped(.udp2udp2udp);
|
||||||
|
|
||||||
|
fn incomingAddr(args_it: anytype) !network.EndPoint {
|
||||||
|
const addr_string = args_it.next().?;
|
||||||
|
const port_string = args_it.next().?;
|
||||||
|
|
||||||
|
const port = try std.fmt.parseInt(u16, port_string, 10);
|
||||||
|
const zig_addr = try std.net.Address.parseIp(addr_string, port);
|
||||||
|
|
||||||
|
const socklen = zig_addr.getOsSockLen();
|
||||||
|
return network.EndPoint.fromSocketAddress(&zig_addr.any, socklen);
|
||||||
|
}
|
||||||
|
|
||||||
|
const WantedMode = enum { send, recv };
|
||||||
|
const AddrTuple = struct { from: network.EndPoint, to: network.EndPoint };
|
||||||
|
const Mode = union(enum) { send: AddrTuple, recv: AddrTuple };
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
try network.init();
|
||||||
|
defer network.deinit();
|
||||||
|
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer {
|
||||||
|
_ = gpa.deinit();
|
||||||
|
}
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
var args_it = try std.process.argsWithAllocator(allocator);
|
||||||
|
defer args_it.deinit();
|
||||||
|
|
||||||
|
_ = args_it.skip();
|
||||||
|
|
||||||
|
const from = try incomingAddr(&args_it);
|
||||||
|
const to = try incomingAddr(&args_it);
|
||||||
|
std.log.info("taking udp from: {s}", .{from});
|
||||||
|
std.log.info("spitting udp to: {s}", .{to});
|
||||||
|
|
||||||
|
logger.info("init udp", .{});
|
||||||
|
var from_sock = try network.Socket.create(.ipv4, .udp);
|
||||||
|
defer from_sock.close();
|
||||||
|
try from_sock.enablePortReuse(true);
|
||||||
|
try from_sock.bind(from);
|
||||||
|
|
||||||
|
var to_sock = try network.Socket.create(.ipv4, .udp);
|
||||||
|
defer to_sock.close();
|
||||||
|
|
||||||
|
var receive_buffer: [2048]u8 = undefined;
|
||||||
|
while (true) {
|
||||||
|
const reader = from_sock.reader();
|
||||||
|
const received_bytes = try reader.read(&receive_buffer);
|
||||||
|
const bytes = receive_buffer[0..received_bytes];
|
||||||
|
const sent_bytes = try to_sock.sendTo(to, bytes);
|
||||||
|
if (sent_bytes != received_bytes) {
|
||||||
|
logger.warn(
|
||||||
|
"tried to send {d} bytes, actually sent {d}",
|
||||||
|
.{ received_bytes, sent_bytes },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
zigmod.lock
Normal file
2
zigmod.lock
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
2
|
||||||
|
git https://github.com/MasterQ32/zig-network commit-caa31ef8783695f0441b1f6812985e6a46b32c96
|
6
zigmod.yml
Normal file
6
zigmod.yml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
id: jsmj7bl93sbzxlt10ezhp6j2pe24xmotxmeerhoyxtmvsjkk
|
||||||
|
name: unicorn_screams
|
||||||
|
license: MIT
|
||||||
|
description: Relay Scream packets between two UDP addresses
|
||||||
|
root_dependencies:
|
||||||
|
- src: git https://github.com/MasterQ32/zig-network
|
Loading…
Reference in a new issue