Something went wrong. Try again.
An SNMP manager library and command line tools for Zig, covering versions 1, 2c and 3.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Which SNMP a message is, and what that permits.//!//! The number on the wire is not a version of the protocol so much as a//! version of the *message format*, and the three differ more than their//! numbering suggests. Version 1 and 2c share a wrapper -- a version, a//! community string and a PDU -- and differ in which PDUs and which value//! types are legal. Version 3 replaces the wrapper entirely with a//! security-model-agnostic header and a scoped PDU, and carries the same PDUs//! as 2c inside it.//!//! This type is threaded through decoding because several rules depend on it//! and an agent is not obliged to be well behaved. The Catalyst is the clean//! demonstration: ask it for `ifHCInOctets.1` over v2c and it answers with a//! `Counter64`; ask for the same OID over v1 and it answers `noSuchName`,//! because SNMPv1 has no type that could hold the reply.
const std = @import("std");const testing = std.testing;
pub const Version = enum(i32) { /// RFC 1157. v1 = 0, /// RFC 1901's community-based framing around RFC 3416's PDUs. The `c` is /// for "community": there was a `v2p` and a `v2u` and neither survived, /// which is why the numbering has a gap that 2c fills. v2c = 1, /// RFC 3412. The number is 3 and there is no 2 on the wire, because /// version 2 was the abandoned party-based SNMPv2. v3 = 3,
/// `Counter64` arrived with SNMPv2 (RFC 1902 §7.1.10). pub fn hasCounter64(v: Version) bool { return v != .v1; }
/// The `noSuchObject`, `noSuchInstance` and `endOfMibView` markers arrived /// with SNMPv2's PDU definitions (RFC 3416 §3). pub fn hasExceptions(v: Version) bool { return v != .v1; }
/// `GetBulkRequest`, `InformRequest`, `SNMPv2-Trap` and `Report` are all /// SNMPv2 and later. A v1 agent answers a GetBulk with nothing at all, /// since it cannot parse the PDU. pub fn hasBulkAndInform(v: Version) bool { return v != .v1; }
/// The v1 `Trap-PDU` exists only in v1: SNMPv2 replaced it with /// `SNMPv2-Trap`, which is an ordinary PDU with a varbind list rather /// than a structure of its own. pub fn hasV1Trap(v: Version) bool { return v == .v1; }
/// Whether the message is wrapped in a community string rather than a v3 /// security header. pub fn isCommunityBased(v: Version) bool { return v == .v1 or v == .v2c; }
/// The richer error codes -- `noAccess` through `inconsistentName` -- /// were added by RFC 3416 §3. A v1 agent has only the original six, which /// is why it says `noSuchName` where a v2c agent says `noSuchObject` in /// the varbind. pub fn hasExtendedErrors(v: Version) bool { return v != .v1; }
/// As net-snmp spells it on the command line: `-v1`, `-v2c`, `-v3`. pub fn parse(text: []const u8) ?Version { if (std.mem.eql(u8, text, "1")) return .v1; if (std.mem.eql(u8, text, "2c")) return .v2c; // net-snmp accepts `-v 2` as an alias, and so does everything else. if (std.mem.eql(u8, text, "2")) return .v2c; if (std.mem.eql(u8, text, "3")) return .v3; return null; }
pub fn name(v: Version) []const u8 { return switch (v) { .v1 => "1", .v2c => "2c", .v3 => "3", }; }};
test "the number on the wire" { // The gap at 2 is the abandoned party-based SNMPv2, and getting this // wrong means every v3 message is rejected by every agent. try testing.expectEqual(@as(i32, 0), @backingInt(Version.v1)); try testing.expectEqual(@as(i32, 1), @backingInt(Version.v2c)); try testing.expectEqual(@as(i32, 3), @backingInt(Version.v3));}
test "what each version permits" { try testing.expect(!Version.v1.hasCounter64()); try testing.expect(Version.v2c.hasCounter64()); try testing.expect(Version.v3.hasCounter64());
try testing.expect(Version.v1.hasV1Trap()); try testing.expect(!Version.v2c.hasV1Trap());
try testing.expect(Version.v1.isCommunityBased()); try testing.expect(Version.v2c.isCommunityBased()); try testing.expect(!Version.v3.isCommunityBased());}
test "spelled as net-snmp spells it" { try testing.expectEqual(Version.v1, Version.parse("1").?); try testing.expectEqual(Version.v2c, Version.parse("2c").?); try testing.expectEqual(Version.v2c, Version.parse("2").?); try testing.expectEqual(Version.v3, Version.parse("3").?); try testing.expectEqual(@as(?Version, null), Version.parse("")); try testing.expectEqual(@as(?Version, null), Version.parse("2u")); try testing.expectEqual(@as(?Version, null), Version.parse("4")); // Round trip through the name. for ([_]Version{ .v1, .v2c, .v3 }) |v| { try testing.expectEqual(v, Version.parse(v.name()).?); }}