Skip to content

Instantly share code, notes, and snippets.

@marijnfs
Created June 29, 2022 08:02
Show Gist options
  • Save marijnfs/dd6c30c094bf019ec4dc023bad46b60e to your computer and use it in GitHub Desktop.
Save marijnfs/dd6c30c094bf019ec4dc023bad46b60e to your computer and use it in GitHub Desktop.
const std = @import("std");
const os = std.os;
const net = std.net;
pub fn main() anyerror!void {
const address_all = net.Address.initIp4([_]u8{ 0, 0, 0, 0 }, 4040);
const address = net.Address.initIp4([_]u8{ 127, 0, 0, 1 }, 4040);
const address_target_ok = net.Address.initIp4([_]u8{ 127, 0, 0, 42 }, 4040);
const address_target_notok = net.Address.initIp4([_]u8{ 192, 168, 1, 1 }, 4040);
const sock_flags = os.SOCK.DGRAM | os.SOCK.CLOEXEC | os.SOCK.NONBLOCK;
const buf = "hello";
{
var fd_all = try os.socket(address_all.any.family, sock_flags, os.IPPROTO.UDP);
defer os.closeSocket(fd_all);
try os.bind(fd_all, &address_all.any, address_all.getOsSockLen());
_ = try os.sendto(fd_all, buf, os.MSG.NOSIGNAL, &address_target_ok.any, address_target_ok.getOsSockLen());
_ = try os.sendto(fd_all, buf, os.MSG.NOSIGNAL, &address_target_notok.any, address_target_notok.getOsSockLen());
}
{
var fd = try os.socket(address.any.family, sock_flags, os.IPPROTO.UDP);
defer os.closeSocket(fd);
try os.bind(fd, &address.any, address.getOsSockLen());
_ = try os.sendto(fd, buf, os.MSG.NOSIGNAL, &address_target_ok.any, address_target_ok.getOsSockLen());
_ = try os.sendto(fd, buf, os.MSG.NOSIGNAL, &address_target_notok.any, address_target_notok.getOsSockLen()); // This last one get .INVAL
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment