// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The host end of an Uthernet (the original, "Uthernet I"): the wire the //! CS8900A's frames go out on and come in from. //! //! The CS8900A (`cs8900.zig`) is a raw Ethernet controller — a driver builds //! whole Ethernet frames and hands them over — so unlike the Uthernet II, whose //! W5100 maps onto the host's own sockets, there is no socket to map onto here. //! What the chip maps onto is a length of Ethernet cable, and this stands in //! for that cable with a UDP multicast group, exactly as `ltoudp.zig` does for //! LocalTalk: every zig65 (or any emulator that adopts the same convention) on //! the group is a node on one virtual Ethernet, and can talk to the others. It //! is not a route to the real internet — a driver's own IP stack reaches only //! what is on the group — which is the trade for needing no privilege and no //! host setup. //! //! A frame goes out with a four-byte tag in front of it, and a frame coming in //! with this node's own tag is dropped, because a multicast socket hears its //! own sends: this is the same arrangement, and for the same reason, as the //! LocalTalk bridge (Fort, Pratt & Mitchelmore, 2012). What follows the tag is //! the raw Ethernet frame, destination MAC first, as the chip emitted it. //! //! Linux only, and for the same reason the LocalTalk bridge is: joining a //! multicast group is not something the standard library's I/O offers. On any //! other system the card is quiet, a cable with nothing on the other end. const std = @import("std"); const builtin = @import("builtin"); const z46 = @import("z46"); const macaddress = @import("macaddress"); /// The multicast group and port zig65 carries virtual Ethernet on. It is an /// administratively-scoped address distinct from the one the LocalTalk bridge /// uses, and the port is a nod to the processor these machines are built on. pub const group = [4]u8{ 239, 192, 76, 85 }; pub const port: u16 = 6502; /// The tag that goes in front of every frame, so a node can tell its own sends /// from everyone else's when the group hands them back. pub const tag_len = 4; /// The largest datagram worth reading: a full Ethernet frame and the tag. const buffer_size = tag_len + 1518; pub const Bridge = struct { fd: i32, tag: u32, /// Frames the system would not take — a group with no way out — 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, or /// null for whichever the system picks; a host whose firewall keeps /// multicast from looping back wants the loopback interface, 127.0.0.1, so /// two instances on one machine hear each other. pub fn open(tag: u32, interface: ?[4]u8) !Bridge { 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, 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 = 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, .tag = tag }; } /// Puts a frame on the group, this node's tag in front of it. pub fn send(self: *Bridge, frame: []const u8) void { var datagram: [buffer_size]u8 = undefined; if (frame.len > buffer_size - tag_len) return; std.mem.writeInt(u32, datagram[0..tag_len], self.tag, .big); @memcpy(datagram[tag_len..][0..frame.len], frame); const to: linux.sockaddr.in = .{ .port = std.mem.nativeToBig(u16, port), .addr = @bitCast(group), }; const rc = linux.sendto(self.fd, &datagram, tag_len + frame.len, 0, @ptrCast(&to), @sizeOf(linux.sockaddr.in)); if (linux.errno(rc) != .SUCCESS) self.failed_sends += 1; } /// The next frame from someone else on the group, if there is one. This /// node's own frames, which the group loops back, are dropped; so is /// anything too short to carry a tag and a frame. pub fn receive(self: *Bridge, buffer: []u8) ?[]const u8 { var datagram: [buffer_size]u8 = undefined; while (true) { const got = linux.recvfrom(self.fd, &datagram, datagram.len, linux.MSG.DONTWAIT, null, null); if (linux.errno(got) != .SUCCESS) return null; if (got < tag_len + 14) continue; // no room for a tag and an Ethernet header if (std.mem.readInt(u32, datagram[0..tag_len], .big) == self.tag) continue; // our own const frame = datagram[tag_len..got]; const len = @min(frame.len, buffer.len); @memcpy(buffer[0..len], frame[0..len]); return buffer[0..len]; } } pub fn close(self: *Bridge) void { _ = linux.close(self.fd); } }; /// Whether a frame's destination is one this node should take: its own MAC, the /// broadcast address, or any multicast (the low bit of the first byte set). It /// is what a card set to accept individual, broadcast and multicast frames /// would let through, which is how a driver leaves the receiver. pub fn addressedTo(frame: []const u8, mac: [6]u8) bool { if (frame.len < 6) return false; const dest = frame[0..6]; if (std.mem.eql(u8, dest, &mac)) return true; if (std.mem.eql(u8, dest, &[_]u8{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff })) return true; return dest[0] & 0x01 != 0; // group (multicast) bit } /// Reads an interface address, which the bridges name a network interface by. /// The parsing is z46's — the user's own IPv4 parser — so it is strict about /// what a dotted quad is, and this is the one place either UDP-multicast bridge /// turns text into an address. pub fn parseAddress(text: []const u8) ?[4]u8 { const address = z46.Ipv4Address.parse(text) catch return null; return address.octets; } /// Reads a hardware address for the card, in any of the notations macaddress /// takes — `00:11:22:33:44:55`, `0011.2233.4455`, and the rest — into the six /// bytes a chip holds, most significant octet first. pub fn parseMac(text: []const u8) ?[6]u8 { const mac = macaddress.parse(text) catch return null; var bytes: [6]u8 = undefined; std.mem.writeInt(u48, &bytes, mac.data, .big); return bytes; } const testing = std.testing; test "an interface address is parsed by z46" { try testing.expectEqual([4]u8{ 127, 0, 0, 1 }, parseAddress("127.0.0.1").?); try testing.expect(parseAddress("10.0.0") == null); try testing.expect(parseAddress("256.0.0.1") == null); } test "a hardware address is parsed by macaddress" { try testing.expectEqual([6]u8{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }, parseMac("00:11:22:33:44:55").?); try testing.expectEqual([6]u8{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }, parseMac("0011.2233.4455").?); try testing.expect(parseMac("nonsense") == null); } test "a frame is taken for our own MAC, for broadcast and for multicast, and not for a stranger" { const mac = [6]u8{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; try testing.expect(addressedTo(&([_]u8{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 } ++ [_]u8{0} ** 8), mac)); try testing.expect(addressedTo(&([_]u8{0xff} ** 6 ++ [_]u8{0} ** 8), mac)); try testing.expect(addressedTo(&([_]u8{ 0x01, 0x00, 0x5e, 0, 0, 1 } ++ [_]u8{0} ** 8), mac)); try testing.expect(!addressedTo(&([_]u8{ 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee } ++ [_]u8{0} ** 8), mac)); }