Reputation: 8717
I am adding SNMP EtherLike MIB variables to our existing SNMP C++ codebase and doing it for very first time.
The difficulty I am facing is understanding the data types of MIB variables and map it to C++ data types - for example dot3StatsIndex MIB variable syntax is InterfaceIndex so what is it equivalent data type in C++.
Since I am implementing for very first time or rather is in learning phase - please let me know what are the different ASN.1 data types and whether the types all are listed above and how they are mapped to C++ datatypes?
Also how to derive from RFC the data type of the MIB variable?
Upvotes: 1
Views: 1154
Reputation: 385295
In my C++ SNMP Agent, I wrote something like the following (unfortunately the actual source code is no longer available to me, and I'm sure there were many more options, but you get the idea):
using varbind_val_t = std::variant<
// ASN.1 INTEGER, SMIv2 Integer32
int32_t,
// ASN.1 OCTET STRING
std::string,
// ASN.1 OBJECT IDENTIFIER
oid_t,
// RFC4001 InetAddressType (TextualConvention)
IpAddress::Type, // enum saying IPv4 or IPv6
// RFC4001 InetAddress (TextualConvention)
IpAddress, // can be an IPv4 or IPv6 address
// SMIv2 IpAddress
ipv4_t,
// ASN.1 MacAddress (TextualConvention)
mac_address_t,
// SMIv2 Counter32/Gauge32/TimeTicks/Unsigned32
uint32_t,
// SMIv2 Counter64
uint64_t
>;
When building or receiving a PDU, the relevant visitors for this variant would encode or decode the wire representation of the given value from a value of the C++ type given above. Sometimes that was a very thin wrapper (an integer is an integer!) and sometimes it required a little more work (oid_t needed unwrapping). This was very fast and flexible. The point is that the mapping is entirely up to you — try to pick types that have the same range of possible values, otherwise you could lose information somewhere along the way. But there is no "the" mapping of C++ types to ASN.1 or SMIv2 types.
Any textual conventions like InterfaceIndex
are not really types, but clues to SNMP managers as to how to represent certain fields. For those not included in the above list as special cases, I simply used the actual underlying type which is, in this case, as Ilya showed, Integer32
. You can read the MIBs to track down what these things really mean (or use a manager like iReasoning which does a rather good job of telling you).
You should refer to the relevant specifications to determine what types you need to support:
Upvotes: 1
Reputation: 5555
I think if you just follow the type inheritance chain, you end up with a base SNMP type which should map to the types you have.
For InterfaceIndex
, see from which MIB it's imported (it's IF-MIB
):
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, OBJECT-IDENTITY,
Integer32, Counter32, Counter64, mib-2, transmission
FROM SNMPv2-SMI
MODULE-COMPLIANCE, OBJECT-GROUP
FROM SNMPv2-CONF
TruthValue
FROM SNMPv2-TC
ifIndex, InterfaceIndex
FROM IF-MIB;
Then find its definition which should reference a base type (it's Integer32
which probably maps to ASN1_INT
).
InterfaceIndex ::= TEXTUAL-CONVENTION
DISPLAY-HINT "d"
STATUS current
DESCRIPTION
"A unique value, greater than zero, for each interface or ...
network management system to the next re-initialization."
SYNTAX Integer32 (1..2147483647)
Upvotes: 1