Conversation
1cea5bc to
369a3ac
Compare
608349c to
ecdd932
Compare
| d := r.Intn(254) + 1 // #nosec G404 | ||
|
|
||
| ip := net.IPv4(byte(a), byte(b), byte(c), byte(d)) | ||
| if ip.IsPrivate() || ip.IsLoopback() || ip.IsMulticast() || ip.IsLinkLocalUnicast() { |
There was a problem hiding this comment.
Nit: It only filters out private, loopback, multicast, and link-local addresses, so it can still emit many non-routable/reserved blocks such as 100.64.0.0/10 (CGNAT). That will leak obviously fake or non-routable “public” IPs into any simulator output
There was a problem hiding this comment.
Reworked RandomPublicIPv4 to reject candidates against an explicit, RFC-attributed list of reserved IPv4 blocks. Each entry in reservedIPv4Blocks carries the originating RFC, a human-readable name, an ietf.org datatracker URL, and the parsed CIDRs. Coverage now includes:
- RFC 1112 (Class E)
- RFC 1122 (loopback + "this network")
- RFC 1918 (private-use)
- RFC 2544 (benchmark testing)
- RFC 3927 (link-local)
- RFC 5736 (IETF protocol assignments)
- RFC 5737 (TEST-NET-1/2/3)
- RFC 6598 (CGNAT)
RFC 6890 noted in a comment as the umbrella special-purpose registry; entries cite the originating RFC for traceability rather than referencing 6890 directly. Adding new reserved blocks as new RFCs land is a one-struct-append. The same paradigm scaffolds IPv6 — reservedIPv6Block + empty reservedIPv6Blocks slice are in place, with a comment listing v6 candidates (RFC 4291, 4193, 3849, 5180, 6052, 6666). Populating that and adding RandomPublicIPv6 is tracked in PIPE-1001.
While at it: also discovered that NetworkIdentity has no IPv6 field at all (the CIDR field is implicitly v4-only). PIPE-1001 covers wiring that through too. Thanks for the push on this — the framing turned a nit into a clean separation between "reserved-block list" (data) and "filter logic" (one method) that v6 work can reuse without restructuring.
| } | ||
|
|
||
| // RandomIPInCIDR generates a random IP within the given CIDR range. | ||
| // The network and broadcast addresses are excluded. |
There was a problem hiding this comment.
It says it excludes network and broadcast addresses, but for /31 and /32 ranges it falls back to ip.String(), which is the network address returned by ParseCIDR. That directly violates the documented behavior and will generate unusable host addresses for small subnets. The tests only exercise /24, so this edge case currently slips through.
There was a problem hiding this comment.
You're right that the /31 case violated the documented behavior, and the /24-only test coverage hid it. Reworked the contract per a discussion with Dylan: blitz networks model "subnets with hosts" — broadcast domains where simulated systems live — so /30, /31, and /32 are out of scope by design (P2P router links and host routes, not host-bearing subnets).
Concretely:
- Floor at /29 (≥ 8 total addresses, 6 usable hosts). Anything stricter (
/30,/31,/32) is rejected. - Added
ValidateCIDR(cidr string) errorand(*NetworkIdentity).Validate() error. Config-loading code that accepts user-supplied CIDRs MUST call these and fail the entire config load on error — blitz refuses to start rather than silently substituting defaults. PIPE-1002 tracks wiring this into the config-load path; the validation function is exported for that purpose. RandomIPInCIDRretains a soft fallback toRandomIPv4(r)for unparseable / non-IPv4 //30–/32inputs as defense in depth. Reaching that fallback indicates a missing validation call upstream, not behavior to depend on. The docstring says so explicitly.- New tests cover
/29(works),/30//31//32(soft fallback returns a valid IPv4), andValidateCIDRaccepting/29and shorter while rejecting/30//31//32, unparseable input, and IPv6 (the last per PIPE-1001). docs/datagen.md(in this stack's docs PR) now spells out the contract explicitly under "NetworkIdentity CIDR contract".
Good catch — the /24-only test coverage was the actual hole; the /31 behavior just made it visible.
369a3ac to
c378aeb
Compare
ecdd932 to
ee7b49c
Compare
ee7b49c to
df89093
Compare
df89093 to
4196ac8
Compare

Proposed Change
Adds
NetworkIdentityplus IP, port, and MAC address generation utilities.Part of PIPE-927 common data generation package stack. Foundation for PIPE-785, PIPE-928, PIPE-943, and the rest of the simulator stack.
Checklist