// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The single category an address falls into, as opposed to the individual //! predicates that answer one question each. const std = @import("std"); const Writer = std.Io.Writer; /// What an address is for, as one mutually exclusive answer. /// /// The classification predicates on the address types each answer one /// question, and asking "what is this address" with them means calling a /// dozen of them in an order that matters — `255.255.255.255` is inside /// `240.0.0.0/4`, so a check for the reserved block ahead of the broadcast /// address gets the wrong answer, and `ff02::1` is not global however much /// `2000::/3` is. `scope` runs that cascade in the right order once. /// /// It is a summary and not a substitute: an address can be a documentation /// address *and* inside `2001::/23`, and only the predicates will say so. /// Reach for this when something has to be told to a person or written to a /// log, and for the predicates when a decision turns on one specific fact. pub const Scope = enum { /// `0.0.0.0` or `::`, which name no address at all. unspecified, /// `127.0.0.0/8` or `::1`. loopback, /// `169.254.0.0/16` or `fe80::/10`, self-assigned and meaningful only on /// one link. link_local, /// `fec0::/10`, the deprecated IPv6 site-local range (RFC 3879). It is /// reported for what it is rather than as global, which is what /// `Ipv6Address.isGlobal` says about it; see the note there. site_local, /// RFC 1918 space for IPv4, or a unique local address for IPv6. private, /// `100.64.0.0/10`, the shared address space carrier-grade NAT is /// numbered out of (RFC 6598). IPv4 only. shared, /// `224.0.0.0/4` or `ff00::/8`. Ask `Ipv6Address.multicastScope` how far /// an IPv6 multicast address reaches. multicast, /// `255.255.255.255`, the limited broadcast address. IPv4 only. broadcast, /// One of the blocks set aside for writing examples in. documentation, /// `198.18.0.0/15` or `2001:2::/48`, for measuring equipment against. benchmarking, /// `240.0.0.0/4`, reserved for whatever IPv4 might have become. IPv4 /// only. reserved, /// Reachable from anywhere on the Internet, so far as the IANA registries /// say. global, /// Covered by a special-purpose registry entry, not globally reachable, /// and not one of the categories above: a Teredo or ORCHIDv2 address, an /// IPv4-mapped one, the discard prefix, an IETF protocol assignment. special_purpose, /// The scope written the way it is spelled in prose, with a hyphen where /// the enum has an underscore. Use the `{f}` specifier to write it /// straight to a stream. pub fn name(s: Scope) []const u8 { return switch (s) { .unspecified => "unspecified", .loopback => "loopback", .link_local => "link-local", .site_local => "site-local", .private => "private", .shared => "shared", .multicast => "multicast", .broadcast => "broadcast", .documentation => "documentation", .benchmarking => "benchmarking", .reserved => "reserved", .global => "global", .special_purpose => "special-purpose", }; } pub fn format(s: Scope, w: *Writer) Writer.Error!void { try w.writeAll(s.name()); } test name { try std.testing.expectEqualStrings("link-local", Scope.link_local.name()); try std.testing.expectEqualStrings("global", Scope.global.name()); } test format { var buffer: [32]u8 = undefined; var w: Writer = .fixed(&buffer); try w.print("{f}", .{Scope.special_purpose}); try std.testing.expectEqualStrings("special-purpose", w.buffered()); } };