• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 using System.Collections.Generic;
2 
3 namespace Lextm.SharpSnmpLib.Mib.Elements.Types
4 {
5     /**
6      * As this type is used for Counter32 and TimeTicks as well as Unsigned32
7      * and Gauge32 it incorrectly allows range restrictions of Counter32 and
8      * TimeTicks.  This is ok as currently we do not care about detecting
9      * incorrect MIBs and this doesn't block the decoding of correct MIBs.
10      */
11     public class UnsignedType : BaseType
12     {
13         public enum Types
14         {
15             Unsigned32,
16             Gauge32,
17             Counter32,
18             TimeTicks,
19             Counter64,
20         }
21 
22         private Types _type;
23         private ValueRanges _ranges;
24 
UnsignedType(IModule module, string name, Symbol type, ISymbolEnumerator symbols)25         public UnsignedType(IModule module, string name, Symbol type, ISymbolEnumerator symbols)
26             : base(module, name)
27         {
28             Types? t = GetExactType(type);
29             type.Assert(t.HasValue, "Unknown symbol for unsigned type!");
30             _type = t.Value;
31 
32             Symbol current = symbols.NextNonEOLSymbol();
33             if (current == Symbol.OpenParentheses)
34             {
35                 current.Assert((_type != Types.Counter64), "Ranges are not supported for Counter64 type!"); // our internal struct can only hold int64 values
36 
37                 symbols.PutBack(current);
38                 _ranges = Lexer.DecodeRanges(symbols);
39                 current.Assert(!_ranges.IsSizeDeclaration, "SIZE keyword is not allowed for ranges of unsigned types!");
40             }
41             else
42             {
43                 symbols.PutBack(current);
44             }
45         }
46 
47         public Types Type
48         {
49             get { return _type; }
50         }
51 
52         public ValueRanges Ranges
53         {
54             get { return _ranges; }
55         }
56 
GetExactType(Symbol symbol)57         internal static Types? GetExactType(Symbol symbol)
58         {
59             if (symbol == Symbol.Unsigned32)
60             {
61                 // [APPLICATION 2] IMPLICIT INTEGER (0..4294967295) // from SNMPv2-SMI
62                 return Types.Unsigned32;
63             }
64             else if (symbol == Symbol.Gauge32)
65             {
66                 // [APPLICATION 2] IMPLICIT INTEGER (0..4294967295) // from SNMPv2-SMI
67                 return Types.Gauge32;
68             }
69             else if (symbol == Symbol.Counter32)
70             {
71                 // [APPLICATION 1] IMPLICIT INTEGER (0..4294967295) // from SNMPv2-SMI
72                 return Types.Counter32;
73             }
74             else if (symbol == Symbol.TimeTicks)
75             {
76                 // [APPLICATION 3] IMPLICIT INTEGER (0..4294967295) // from SNMPv2-SMI + RFC1155-SMI
77                 return Types.TimeTicks;
78             }
79             else if (symbol == Symbol.Gauge)
80             {
81                 // [APPLICATION 2] IMPLICIT INTEGER (0..4294967295) // from RFC1155-SMI
82                 return Types.Gauge32;
83             }
84             else if (symbol == Symbol.Counter)
85             {
86                 // [APPLICATION 1] IMPLICIT INTEGER (0..4294967295) // from RFC1155-SMI
87                 return Types.Counter32;
88             }
89             else if (symbol == Symbol.Counter64)
90             {
91                // [APPLICATION 6] IMPLICIT INTEGER (0..18446744073709551615) // from SNMPv2-SMI
92                return Types.Counter64;
93             }
94 
95             return null;
96         }
97 
IsUnsignedType(Symbol symbol)98         internal static bool IsUnsignedType(Symbol symbol)
99         {
100             return GetExactType(symbol).HasValue;
101         }
102     }
103 }
104