Something went wrong. Try again.
A reader for SNMP MIB modules: SMIv1 and SMIv2, in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! `snmptranslate -Tz`, so that the two can be diffed.//!//! net-snmp will print its entire symbol table as `"name" "oid"` lines, which//! makes the whole of name resolution comparable in one command rather than//! object by object://!//! ```console//! $ diff <(snmptranslate -Tz -M mibs -m ALL | sort) \//! <(smitranslate mibs/*.txt | sort)//! ```//!//! That is the strongest form the oracle takes for a MIB reader: 2,637 names//! out of the 65 base modules, every one of which has to agree.//!//! It agrees on all 2,619 that this prints. The other 18 are the *second*//! definition of a name defined twice -- `linux` sits under both//! `ucdSnmpAgent` and `netSnmpAgentOIDs`, `snmpCommunityGroup` under both//! SNMPv2-MIB and SNMP-COMMUNITY-MIB -- and they are a difference of shape//! rather than of conclusion. net-snmp dumps a tree, in which a name defined//! twice is two nodes; this is a map from name to node, in which it is one,//! and the discarded definition is recorded as a conflict instead. Nothing is//! lost by that: net-snmp cannot translate such a name either, answering//! `snmptranslate linux` with "Sub-id not found".
const std = @import("std");const Io = std.Io;const smi = @import("smi");
pub fn main(init: std.process.Init) !void { const io = init.io; const gpa = init.arena.allocator();
var ob: [256 * 1024]u8 = undefined; var out: Io.File.Writer = .init(.stdout(), io, &ob); defer out.interface.flush() catch {}; var eb: [8 * 1024]u8 = undefined; var err: Io.File.Writer = .init(.stderr(), io, &eb); defer err.interface.flush() catch {};
const args = try init.minimal.args.toSlice(gpa); if (args.len < 2) { try err.interface.writeAll( \\usage: smitranslate <mib-file>... \\ \\Prints every resolved name and its OID, in the format \\`snmptranslate -Tz` uses, so the two can be diffed. \\ ); try err.interface.flush(); std.process.exit(2); }
var registry: smi.Registry = .init(gpa); defer registry.deinit();
var unreadable: usize = 0; for (args[1..]) |path| { var file = Io.Dir.cwd().openFile(io, path, .{}) catch { unreadable += 1; continue; }; defer file.close(io); var buf: [4096]u8 = undefined; var reader = file.readerStreaming(io, &buf); const source = reader.interface.allocRemaining(gpa, .unlimited) catch { unreadable += 1; continue; }; registry.addModule(source, .{}) catch |e| switch (e) { // A file with no module is not a failure; a file that will not // parse contributes nothing but should not stop the rest. error.NoModule, error.ParseFailed => { unreadable += 1; continue; }, else => return e, }; }
const unresolved = try registry.resolve();
// Sorted, so the diff is about content rather than about hash order. var names: std.ArrayList(smi.Registry.Node) = .empty; var it = registry.iterator(); while (it.next()) |node| { // The three seeded roots -- `ccitt`, `iso`, `joint-iso-ccitt` -- have // no module because no module defines them, and net-snmp does not // list them in `-Tz` either. Skipping them here is what makes this // output directly diffable against `snmptranslate -Tz`. if (node.module.len == 0) continue; try names.append(gpa, node.*); } std.mem.sort(smi.Registry.Node, names.items, {}, lessByName);
for (names.items) |node| { // net-snmp's exact spelling: the name in quotes, a tab, the OID in // quotes -- and no leading dot, which is the one place its -Tz output // differs from everywhere else it prints an OID. try out.interface.print("\"{s}\"\t\"", .{node.name}); try smi.Registry.formatOid(node.oid, &out.interface); try out.interface.writeAll("\"\n"); }
try err.interface.print( "{d} names, {d} unresolved, {d} files unreadable, " ++ "{d} conflicts (each a name whose second definition net-snmp " ++ "would list again and this discards)\n", .{ names.items.len, unresolved, unreadable, registry.conflicts.items.len }, );}
fn lessByName(_: void, a: smi.Registry.Node, b: smi.Registry.Node) bool { return std.mem.order(u8, a.name, b.name) == .lt;}