// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The socket under the LocalTalk bridge, shared by the IIgs front end and //! the headless one. const std = @import("std"); const builtin = @import("builtin"); const zig65 = @import("zig65"); /// The socket end of the LocalTalk bridge: a UDP socket in the multicast /// group LocalTalk over UDP uses, bound the way Mini vMac binds its own /// (Fort, Pratt & Mitchelmore, 2012) — to any address, with the address /// reusable so that two of these on one machine can both be on the network, /// and non-blocking, since the machine cannot wait on it. /// /// This is the one place in the program that talks to the operating system /// directly rather than through the standard library's I/O, because joining /// a multicast group is not something that library offers, and it is /// therefore the one place that is Linux only. pub const Network = struct { fd: i32, bridge: zig65.localtalk.Bridge, /// Datagrams the system would not take, which is a network with no way /// to the group rather than anything the machine did. failed_sends: u64 = 0, const linux = std.os.linux; const Membership = extern struct { multiaddr: [4]u8, interface: u32 }; /// `interface` is the address of the interface to join the group on and /// send through, or null for whichever the system chooses. On a host /// whose firewall keeps multicast from looping back on its network /// interface, two of these on one host need the loopback interface, /// 127.0.0.1, to hear each other. pub fn open(tag: u32, interface: ?[4]u8) !Network { if (comptime builtin.os.tag != .linux) return error.NotOnThisSystem; const created = linux.socket(linux.AF.INET, linux.SOCK.DGRAM | linux.SOCK.NONBLOCK | linux.SOCK.CLOEXEC, 0); if (linux.errno(created) != .SUCCESS) return error.NoSocket; const fd: i32 = @intCast(created); errdefer _ = linux.close(fd); const one: u32 = 1; _ = linux.setsockopt(fd, linux.SOL.SOCKET, linux.SO.REUSEADDR, @ptrCast(&one), @sizeOf(u32)); _ = linux.setsockopt(fd, linux.SOL.SOCKET, linux.SO.REUSEPORT, @ptrCast(&one), @sizeOf(u32)); const address: linux.sockaddr.in = .{ .port = std.mem.nativeToBig(u16, zig65.localtalk.port), .addr = 0, }; if (linux.errno(linux.bind(fd, @ptrCast(&address), @sizeOf(linux.sockaddr.in))) != .SUCCESS) return error.PortInUse; const on: u32 = if (interface) |chosen| @bitCast(chosen) else 0; const membership: Membership = .{ .multiaddr = zig65.localtalk.group, .interface = on }; if (linux.errno(linux.setsockopt(fd, linux.IPPROTO.IP, linux.IP.ADD_MEMBERSHIP, @ptrCast(&membership), @sizeOf(Membership))) != .SUCCESS) return error.NoMulticast; if (interface) |chosen| { if (linux.errno(linux.setsockopt(fd, linux.IPPROTO.IP, linux.IP.MULTICAST_IF, @ptrCast(&chosen), 4)) != .SUCCESS) return error.NoSuchInterface; } return .{ .fd = fd, .bridge = .{ .tag = tag } }; } /// What the bridge calls to put a datagram on the group. pub fn send(self: *Network, bytes: []const u8) void { const to: linux.sockaddr.in = .{ .port = std.mem.nativeToBig(u16, zig65.localtalk.port), .addr = @bitCast(zig65.localtalk.group), }; const rc = linux.sendto(self.fd, bytes.ptr, bytes.len, 0, @ptrCast(&to), @sizeOf(linux.sockaddr.in)); if (linux.errno(rc) != .SUCCESS) self.failed_sends += 1; } /// The next datagram waiting, if there is one. pub fn receive(self: *Network, buffer: []u8) ?[]u8 { const got = linux.recvfrom(self.fd, buffer.ptr, buffer.len, linux.MSG.DONTWAIT, null, null); if (linux.errno(got) != .SUCCESS) return null; return buffer[0..got]; } pub fn close(self: *Network) void { _ = linux.close(self.fd); } }; // An interface address is parsed by `zig65.ethernet.parseAddress`, which is // z46's parser and the one place either UDP-multicast bridge turns text into an // address; this module no longer carries a parser of its own.