Пример #1
0
        public void TestLargeMessage()
        {
            using (var engine = CreateEngine())
            {
                engine.Listener.ClearBindings();
                var serverEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId);
                engine.Listener.AddBinding(serverEndPoint);

                engine.Start();

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                var    list   = new List <Variable>();
                for (int i = 0; i < 1000; i++)
                {
                    list.Add(new Variable(new ObjectIdentifier("1.3.6.1.1.1.0")));
                }

                GetRequestMessage message = new GetRequestMessage(
                    0x4bed,
                    VersionCode.V2,
                    new OctetString("public"),
                    list);

                Assert.True(message.ToBytes().Length > 10000);

                var time = 1500;
                //IMPORTANT: test against an agent that doesn't exist.
                var result = message.GetResponse(time, serverEndPoint, socket);

                Assert.True(result.Scope.Pdu.ErrorStatus.ToErrorCode() == ErrorCode.NoError);
            }
        }
Пример #2
0
        public void TestDiscoveryV3()
        {
            const string bytes = "30 3A 02 01 03 30 0F 02 02 6A 09 02 03 00 FF E3" +
                                 " 04 01 04 02 01 03 04 10 30 0E 04 00 02 01 00 02" +
                                 " 01 00 04 00 04 00 04 00 30 12 04 00 04 00 A0 0C" +
                                 " 02 02 2C 6B 02 01 00 02 01 00 30 00";
            GetRequestMessage request = new GetRequestMessage(
                VersionCode.V3,
                new Header(
                    new Integer32(0x6A09),
                    new Integer32(0xFFE3),
                    Levels.Reportable),
                new SecurityParameters(
                    OctetString.Empty,
                    Integer32.Zero,
                    Integer32.Zero,
                    OctetString.Empty,
                    OctetString.Empty,
                    OctetString.Empty),
                new Scope(
                    OctetString.Empty,
                    OctetString.Empty,
                    new GetRequestPdu(0x2C6B, new List <Variable>())),
                DefaultPrivacyProvider.DefaultPair,
                null
                );
            string test = ByteTool.Convert(request.ToBytes());

            Assert.Equal(bytes, test);
        }
 public void TestConstructor()
 {
     List<Variable> list = new List<Variable>(1)
                               {
                                   new Variable(new ObjectIdentifier(new uint[] {1, 3, 6, 1, 2, 1, 1, 6, 0}),
                                                new Null())
                               };
     GetRequestMessage message = new GetRequestMessage(0, VersionCode.V2, new OctetString("public"), list);
     Assert.GreaterOrEqual(Properties.Resources.get.Length, message.ToBytes().Length);
 }
Пример #4
0
        public void TestConstructor()
        {
            List <Variable> list = new List <Variable>(1)
            {
                new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 }),
                             new Null())
            };
            GetRequestMessage message = new GetRequestMessage(0, VersionCode.V2, new OctetString("public"), list);

            Assert.True(Properties.Resources.get.Length >= message.ToBytes().Length);
        }
Пример #5
0
        public void TestConstructorV3AuthMd5()
        {
            const string bytes = "30 73" +
                                 "02 01  03 " +
                                 "30 0F " +
                                 "02  02 35 41 " +
                                 "02  03 00 FF E3" +
                                 "04 01 05" +
                                 "02  01 03" +
                                 "04 2E  " +
                                 "30 2C" +
                                 "04 0D  80 00 1F 88 80 E9 63 00  00 D6 1F F4  49 " +
                                 "02 01 0D  " +
                                 "02 01 57 " +
                                 "04 05 6C 65 78  6C 69 " +
                                 "04 0C  1C 6D 67 BF  B2 38 ED 63 DF 0A 05 24  " +
                                 "04 00 " +
                                 "30 2D  " +
                                 "04 0D 80 00  1F 88 80 E9 63 00 00 D6  1F F4 49 " +
                                 "04  00 " +
                                 "A0 1A 02  02 01 AF 02 01 00 02 01  00 30 0E 30  0C 06 08 2B  06 01 02 01 01 03 00 05  00";
            IPrivacyProvider  pair    = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass")));
            GetRequestMessage request = new GetRequestMessage(
                VersionCode.V3,
                new Header(
                    new Integer32(13633),
                    new Integer32(0xFFE3),
                    new OctetString(new byte[] { 0x5 }),
                    new Integer32(3)),
                new SecurityParameters(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    new Integer32(0x0d),
                    new Integer32(0x57),
                    new OctetString("lexli"),
                    new OctetString(ByteTool.Convert("1C 6D 67 BF  B2 38 ED 63 DF 0A 05 24")),
                    OctetString.Empty),
                new Scope(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    OctetString.Empty,
                    new GetRequestPdu(
                        0x01AF,
                        ErrorCode.NoError,
                        0,
                        new List <Variable>(1)
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null())
            })),
                pair);

            Assert.AreEqual(Levels.Authentication, request.Level);
            Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
        }
Пример #6
0
        public void TestToBytes()
        {
            const string s = "30 27 02 01  01 04 06 70  75 62 6C 69  63 A0 1A 02" +
                             "02 4B ED 02  01 00 02 01  00 30 0E 30  0C 06 08 2B" +
                             "06 01 02 01  01 01 00 05  00                      ";

            byte[]            expected = ByteTool.Convert(s);
            GetRequestMessage message  = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List <Variable> {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))
            });

            Assert.Equal(expected, message.ToBytes());
        }
Пример #7
0
        public void TestLargeMessage()
        {
            var engine = CreateEngine();

            engine.Listener.ClearBindings();
            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, Port.NextId);

            engine.Listener.AddBinding(serverEndPoint);
            engine.Start();

            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                var    list   = new List <Variable>();
                for (int i = 0; i < 1000; i++)
                {
                    list.Add(new Variable(new ObjectIdentifier("1.3.6.1.1.1.0")));
                }

                GetRequestMessage message = new GetRequestMessage(
                    0x4bed,
                    VersionCode.V2,
                    new OctetString("public"),
                    list);

                Assert.True(message.ToBytes().Length > 10000);

                var time = 3000;
                if (SnmpMessageExtension.IsRunningOnMac)
                {
                    var exception =
                        Assert.Throws <SocketException>(() => message.GetResponse(time, serverEndPoint, socket));
                    Assert.Equal(SocketError.MessageSize, exception.SocketErrorCode);
                }
                else
                {
                    // IMPORTANT: test against an agent that doesn't exist.
                    var result = message.GetResponse(time, serverEndPoint, socket);
                    Assert.True(result.Scope.Pdu.ErrorStatus.ToErrorCode() == ErrorCode.NoError);
                }
            }
            finally
            {
                if (SnmpMessageExtension.IsRunningOnWindows)
                {
                    engine.Stop();
                }
            }
        }
Пример #8
0
        public void TestConstructorV2AuthMd5PrivDes()
        {
            if (!DESPrivacyProvider.IsSupported)
            {
                return;
            }

            const string bytes = "30 81 80 02  01 03 30 0F  02 02 6C 99  02 03 00 FF" +
                                 "E3 04 01 07  02 01 03 04  38 30 36 04  0D 80 00 1F" +
                                 "88 80 E9 63  00 00 D6 1F  F4 49 02 01  14 02 01 35" +
                                 "04 07 6C 65  78 6D 61 72  6B 04 0C 80  50 D9 A1 E7" +
                                 "81 B6 19 80  4F 06 C0 04  08 00 00 00  01 44 2C A3" +
                                 "B5 04 30 4B  4F 10 3B 73  E1 E4 BD 91  32 1B CB 41" +
                                 "1B A1 C1 D1  1D 2D B7 84  16 CA 41 BF  B3 62 83 C4" +
                                 "29 C5 A4 BC  32 DA 2E C7  65 A5 3D 71  06 3C 5B 56" +
                                 "FB 04 A4";
            MD5AuthenticationProvider auth    = new MD5AuthenticationProvider(new OctetString("testpass"));
            IPrivacyProvider          privacy = new DESPrivacyProvider(new OctetString("passtest"), auth);
            GetRequestMessage         request = new GetRequestMessage(
                VersionCode.V3,
                new Header(
                    new Integer32(0x6C99),
                    new Integer32(0xFFE3),
                    Levels.Authentication | Levels.Privacy | Levels.Reportable),
                new SecurityParameters(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    new Integer32(0x14),
                    new Integer32(0x35),
                    new OctetString("lexmark"),
                    new OctetString(ByteTool.Convert("80  50 D9 A1 E7 81 B6 19 80  4F 06 C0")),
                    new OctetString(ByteTool.Convert("00 00 00  01 44 2C A3 B5"))),
                new Scope(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    OctetString.Empty,
                    new GetRequestPdu(
                        0x3A25,
                        new List <Variable>(1)
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"))
            })),
                privacy,
                null);

            Assert.Equal(Levels.Authentication | Levels.Privacy | Levels.Reportable, request.Header.SecurityLevel);
            Assert.Equal(ByteTool.Convert(bytes), request.ToBytes());
        }
Пример #9
0
        public void TestConstructorV3AuthSha()
        {
            const string bytes = "30 77 02 01  03 30 0F 02  02 47 21 02  03 00 FF E3" +
                                 "04 01 05 02  01 03 04 32  30 30 04 0D  80 00 1F 88" +
                                 "80 E9 63 00  00 D6 1F F4  49 02 01 15  02 02 01 5B" +
                                 "04 08 6C 65  78 74 75 64  69 6F 04 0C  7B 62 65 AE" +
                                 "D3 8F E3 7D  58 45 5C 6C  04 00 30 2D  04 0D 80 00" +
                                 "1F 88 80 E9  63 00 00 D6  1F F4 49 04  00 A0 1A 02" +
                                 "02 56 FF 02  01 00 02 01  00 30 0E 30  0C 06 08 2B" +
                                 "06 01 02 01  01 03 00 05  00";
            IPrivacyProvider  pair    = new DefaultPrivacyProvider(new SHA1AuthenticationProvider(new OctetString("password")));
            GetRequestMessage request = new GetRequestMessage(
                VersionCode.V3,
                new Header(
                    new Integer32(0x4721),
                    new Integer32(0xFFE3),
                    new OctetString(new byte[] { 0x5 }),
                    new Integer32(3)),
                new SecurityParameters(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    new Integer32(0x15),
                    new Integer32(0x015B),
                    new OctetString("lextudio"),
                    new OctetString(ByteTool.Convert("7B 62 65 AE D3 8F E3 7D  58 45 5C 6C")),
                    OctetString.Empty),
                new Scope(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    OctetString.Empty,
                    new GetRequestPdu(
                        0x56FF,
                        ErrorCode.NoError,
                        0,
                        new List <Variable>(1)
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null())
            })),
                pair);

            Assert.AreEqual(Levels.Authentication, request.Level);
            Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
        }
Пример #10
0
        public void TestConstructorV3OverDtls()
        {
            const string bytes = "30 3c 02 01  03 30 11 02  04 2f 1f 26  a3 02 03 00" +
                                 "ff e3 04 01  07 02 01 04  04 00 30 22  04 00 04 00" +
                                 "a0 1c 02 04  ab 53 bd 3f  02 01 00 02  01 00 30 0e" +
                                 "30 0c 06 08  2b 06 01 02  01 01 03 00  05 00";

            var request = new GetRequestMessage(
                VersionCode.V3,
                790570659,
                -1420575425,
                OctetString.Empty,
                new List <Variable>()
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"))
            },
                new TsmPrivacyProvider(TsmAuthenticationProvider.Instance),
                65507);

            Assert.Equal(Levels.Authentication | Levels.Reportable | Levels.Privacy, request.Header.SecurityLevel);
            Assert.Equal(ByteTool.Convert(bytes), request.ToBytes());
        }
Пример #11
0
 public void TestToBytes()
 {
     const string s = "30 27 02 01  01 04 06 70  75 62 6C 69  63 A0 1A 02" +
                      "02 4B ED 02  01 00 02 01  00 30 0E 30  0C 06 08 2B" +
                      "06 01 02 01  01 01 00 05  00                      ";
     byte[] expected = ByteTool.Convert(s);
     GetRequestMessage message = new GetRequestMessage(0x4bed, VersionCode.V2, new OctetString("public"), new List<Variable> { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0")) });
     Assert.AreEqual(expected, message.ToBytes());
 }
Пример #12
0
        public void TestConstructorV3Auth2()
        {
            const string bytes = "30 73" +
                                 "02 01  03 " +
                                 "30 0F " +
                                 "02  02 35 41 " +
                                 "02  03 00 FF E3" +
                                 "04 01 05" +
                                 "02  01 03" +
                                 "04 2E  " +
                                 "30 2C" +
                                 "04 0D  80 00 1F 88 80 E9 63 00  00 D6 1F F4  49 " +
                                 "02 01 0D  " +
                                 "02 01 57 " +
                                 "04 05 6C 65 78  6C 69 " +
                                 "04 0C  1C 6D 67 BF  B2 38 ED 63 DF 0A 05 24  " +
                                 "04 00 " +
                                 "30 2D  " +
                                 "04 0D 80 00  1F 88 80 E9 63 00 00 D6  1F F4 49 " +
                                 "04  00 " +
                                 "A0 1A 02  02 01 AF 02 01 00 02 01  00 30 0E 30  0C 06 08 2B  06 01 02 01 01 03 00 05  00";
            ReportMessage report = new ReportMessage(
                VersionCode.V3,
                new Header(
                    new Integer32(13633),
                    new Integer32(0xFFE3),
                    0),
                new SecurityParameters(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    new Integer32(0x0d),
                    new Integer32(0x57),
                    new OctetString("lexli"),
                    new OctetString(new byte[12]),
                    OctetString.Empty),
                new Scope(
                    new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
                    OctetString.Empty,
                    new ReportPdu(
                        0x01AF,
                        ErrorCode.NoError,
                        0,
                        new List <Variable>(1)
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"))
            })),
                DefaultPrivacyProvider.DefaultPair,
                null);

            IPrivacyProvider  privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass")));
            GetRequestMessage request = new GetRequestMessage(
                VersionCode.V3,
                13633,
                0x01AF,
                new OctetString("lexli"),
                OctetString.Empty,
                new List <Variable>(1)
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"))
            },
                privacy,
                Messenger.MaxMessageSize,
                report,
                SecurityModel.Usm);

            Assert.Equal(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel);
            Assert.Equal(ByteTool.Convert(bytes), request.ToBytes());
        }
Пример #13
0
 public void TestConstructorV2AuthMd5PrivDes()
 {
     const string bytes = "30 81 80 02  01 03 30 0F  02 02 6C 99  02 03 00 FF" +
                          "E3 04 01 07  02 01 03 04  38 30 36 04  0D 80 00 1F" +
                          "88 80 E9 63  00 00 D6 1F  F4 49 02 01  14 02 01 35" +
                          "04 07 6C 65  78 6D 61 72  6B 04 0C 80  50 D9 A1 E7" +
                          "81 B6 19 80  4F 06 C0 04  08 00 00 00  01 44 2C A3" +
                          "B5 04 30 4B  4F 10 3B 73  E1 E4 BD 91  32 1B CB 41" +
                          "1B A1 C1 D1  1D 2D B7 84  16 CA 41 BF  B3 62 83 C4" +
                          "29 C5 A4 BC  32 DA 2E C7  65 A5 3D 71  06 3C 5B 56" +
                          "FB 04 A4";
     MD5AuthenticationProvider auth = new MD5AuthenticationProvider(new OctetString("testpass"));
     IPrivacyProvider privacy = new DESPrivacyProvider(new OctetString("passtest"), auth);
     GetRequestMessage request = new GetRequestMessage(
         VersionCode.V3,
         new Header(
             new Integer32(0x6C99),
             new Integer32(0xFFE3),
             Levels.Authentication | Levels.Privacy | Levels.Reportable),
         new SecurityParameters(
             new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
             new Integer32(0x14),
             new Integer32(0x35),
             new OctetString("lexmark"),
             new OctetString(ByteTool.Convert("80  50 D9 A1 E7 81 B6 19 80  4F 06 C0")),
             new OctetString(ByteTool.Convert("00 00 00  01 44 2C A3 B5"))),
         new Scope(
             new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
             OctetString.Empty,
             new GetRequestPdu(
                 0x3A25,
                 new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })),
         privacy,
         null);
     Assert.AreEqual(Levels.Authentication | Levels.Privacy | Levels.Reportable, request.Header.SecurityLevel);
     Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
 }
Пример #14
0
        public static void Main(string[] args)
        {
            string      community      = "public";
            bool        showHelp       = false;
            bool        showVersion    = false;
            VersionCode version        = VersionCode.V1;
            int         timeout        = 1000;
            int         retry          = 0;
            Levels      level          = Levels.Reportable;
            string      user           = string.Empty;
            string      authentication = string.Empty;
            string      authPhrase     = string.Empty;
            string      privacy        = string.Empty;
            string      privPhrase     = string.Empty;
            bool        dump           = false;

            OptionSet p = new OptionSet()
                          .Add("c:", "Community name, (default is public)", delegate(string v) { if (v != null)
                                                                                                 {
                                                                                                     community = v;
                                                                                                 }
                               })
                          .Add("l:", "Security level, (default is noAuthNoPriv)", delegate(string v)
            {
                if (v.ToUpperInvariant() == "NOAUTHNOPRIV")
                {
                    level = Levels.Reportable;
                }
                else if (v.ToUpperInvariant() == "AUTHNOPRIV")
                {
                    level = Levels.Authentication | Levels.Reportable;
                }
                else if (v.ToUpperInvariant() == "AUTHPRIV")
                {
                    level = Levels.Authentication | Levels.Privacy | Levels.Reportable;
                }
                else
                {
                    throw new ArgumentException("no such security mode: " + v);
                }
            })
                          .Add("a:", "Authentication method (MD5 or SHA)", delegate(string v) { authentication = v; })
                          .Add("A:", "Authentication passphrase", delegate(string v) { authPhrase = v; })
                          .Add("x:", "Privacy method", delegate(string v) { privacy = v; })
                          .Add("X:", "Privacy passphrase", delegate(string v) { privPhrase = v; })
                          .Add("u:", "Security name", delegate(string v) { user = v; })
                          .Add("h|?|help", "Print this help information.", delegate(string v) { showHelp = v != null; })
                          .Add("V", "Display version number of this application.", delegate(string v) { showVersion = v != null; })
                          .Add("d", "Display message dump", delegate(string v) { dump = true; })
                          .Add("t:", "Timeout value (unit is second).", delegate(string v) { timeout = int.Parse(v) * 1000; })
                          .Add("r:", "Retry count (default is 0)", delegate(string v) { retry = int.Parse(v); })
                          .Add("v|version:", "SNMP version (1, 2, and 3 are currently supported)", delegate(string v)
            {
                switch (int.Parse(v))
                {
                case 1:
                    version = VersionCode.V1;
                    break;

                case 2:
                    version = VersionCode.V2;
                    break;

                case 3:
                    version = VersionCode.V3;
                    break;

                default:
                    throw new ArgumentException("no such version: " + v);
                }
            });

            if (args.Length == 0)
            {
                ShowHelp(p);
                return;
            }

            List <string> extra;

            try
            {
                extra = p.Parse(args);
            }
            catch (OptionException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            if (showHelp)
            {
                ShowHelp(p);
                return;
            }

            if (extra.Count < 2)
            {
                Console.WriteLine("invalid variable number: " + extra.Count);
                return;
            }

            if (showVersion)
            {
                Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
                return;
            }

            IPAddress ip;
            bool      parsed = IPAddress.TryParse(extra[0], out ip);

            if (!parsed)
            {
                foreach (IPAddress address in
                         Dns.GetHostAddresses(extra[0]).Where(address => address.AddressFamily == AddressFamily.InterNetwork))
                {
                    ip = address;
                    break;
                }

                if (ip == null)
                {
                    Console.WriteLine("invalid host or wrong IP address found: " + extra[0]);
                    return;
                }
            }

            try
            {
                List <Variable> vList = new List <Variable>();
                for (int i = 1; i < extra.Count; i++)
                {
                    Variable test = new Variable(new ObjectIdentifier(extra[i]));
                    vList.Add(test);
                }

                IPEndPoint receiver = new IPEndPoint(ip, 161);
                if (version != VersionCode.V3)
                {
                    foreach (
                        Variable variable in
                        Messenger.Get(version, receiver, new OctetString(community), vList, timeout))
                    {
                        Console.WriteLine(variable);
                    }

                    return;
                }

                if (string.IsNullOrEmpty(user))
                {
                    Console.WriteLine("User name need to be specified for v3.");
                    return;
                }

                IAuthenticationProvider auth = (level & Levels.Authentication) == Levels.Authentication
                                                   ? GetAuthenticationProviderByName(authentication, authPhrase)
                                                   : DefaultAuthenticationProvider.Instance;

                IPrivacyProvider priv;
                if ((level & Levels.Privacy) == Levels.Privacy)
                {
                    priv = new DESPrivacyProvider(new OctetString(privPhrase), auth);
                }
                else
                {
                    priv = new DefaultPrivacyProvider(auth);
                }

                Discovery     discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
                ReportMessage report    = discovery.GetResponse(timeout, receiver);

                GetRequestMessage request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(user), vList, priv, Messenger.MaxMessageSize, report);
                ISnmpMessage      reply   = request.GetResponse(timeout, receiver);
                if (dump)
                {
                    Console.WriteLine("Request message bytes:");
                    Console.WriteLine(ByteTool.Convert(request.ToBytes()));
                    Console.WriteLine("Response message bytes:");
                    Console.WriteLine(ByteTool.Convert(reply.ToBytes()));
                }

                if (reply is ReportMessage)
                {
                    if (reply.Pdu().Variables.Count == 0)
                    {
                        Console.WriteLine("wrong report message received");
                        return;
                    }

                    var id = reply.Pdu().Variables[0].Id;
                    if (id != Messenger.NotInTimeWindow)
                    {
                        var error = id.GetErrorMessage();
                        Console.WriteLine(error);
                        return;
                    }

                    // according to RFC 3414, send a second request to sync time.
                    request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(user), vList, priv, Messenger.MaxMessageSize, reply);
                    reply   = request.GetResponse(timeout, receiver);
                }
                else if (reply.Pdu().ErrorStatus.ToInt32() != 0) // != ErrorCode.NoError
                {
                    throw ErrorException.Create(
                              "error in response",
                              receiver.Address,
                              reply);
                }

                foreach (Variable v in reply.Pdu().Variables)
                {
                    Console.WriteLine(v);
                }
            }
            catch (SnmpException ex)
            {
                Console.WriteLine(ex);
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex);
            }
        }
Пример #15
0
 public void TestConstructorV3Auth1()
 {
     const string bytes = "30 73" +
                          "02 01  03 " +
                          "30 0F " +
                          "02  02 35 41 " +
                          "02  03 00 FF E3" +
                          "04 01 05" +
                          "02  01 03" +
                          "04 2E  " +
                          "30 2C" +
                          "04 0D  80 00 1F 88 80 E9 63 00  00 D6 1F F4  49 " +
                          "02 01 0D  " +
                          "02 01 57 " +
                          "04 05 6C 65 78  6C 69 " +
                          "04 0C  1C 6D 67 BF  B2 38 ED 63 DF 0A 05 24  " +
                          "04 00 " +
                          "30 2D  " +
                          "04 0D 80 00  1F 88 80 E9 63 00 00 D6  1F F4 49 " +
                          "04  00 " +
                          "A0 1A 02  02 01 AF 02 01 00 02 01  00 30 0E 30  0C 06 08 2B  06 01 02 01 01 03 00 05  00";
     ReportMessage report = new ReportMessage(
         VersionCode.V3,
         new Header(
             new Integer32(13633),
             new Integer32(0xFFE3),
             0),
         new SecurityParameters(
             new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
             new Integer32(0x0d),
             new Integer32(0x57),
             new OctetString("lexli"),
             new OctetString(new byte[12]),
             OctetString.Empty),
         new Scope(
             new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
             OctetString.Empty,
             new ReportPdu(
                 0x01AF,
                 ErrorCode.NoError,
                 0,
                 new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) })),
         DefaultPrivacyProvider.DefaultPair,
         null);
     
     IPrivacyProvider privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("testpass")));
     GetRequestMessage request = new GetRequestMessage(
         VersionCode.V3,
         13633,
         0x01AF,
         new OctetString("lexli"),
         new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")) },
         privacy,
         Messenger.MaxMessageSize,
         report);
     
     Assert.AreEqual(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel);
     Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
 }
Пример #16
0
 public void TestConstructorV3AuthSha()
 {
     const string bytes = "30 77 02 01  03 30 0F 02  02 47 21 02  03 00 FF E3" +
                          "04 01 05 02  01 03 04 32  30 30 04 0D  80 00 1F 88" +
                          "80 E9 63 00  00 D6 1F F4  49 02 01 15  02 02 01 5B" +
                          "04 08 6C 65  78 74 75 64  69 6F 04 0C  7B 62 65 AE" +
                          "D3 8F E3 7D  58 45 5C 6C  04 00 30 2D  04 0D 80 00" +
                          "1F 88 80 E9  63 00 00 D6  1F F4 49 04  00 A0 1A 02" +
                          "02 56 FF 02  01 00 02 01  00 30 0E 30  0C 06 08 2B" +
                          "06 01 02 01  01 03 00 05  00";
     IPrivacyProvider pair = new DefaultPrivacyProvider(new SHA1AuthenticationProvider(new OctetString("password")));
     GetRequestMessage request = new GetRequestMessage(
         VersionCode.V3,
         new Header(
             new Integer32(0x4721),
             new Integer32(0xFFE3),
             Levels.Authentication | Levels.Reportable),
         new SecurityParameters(
             new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
             new Integer32(0x15),
             new Integer32(0x015B),
             new OctetString("lextudio"),
             new OctetString(ByteTool.Convert("7B 62 65 AE D3 8F E3 7D  58 45 5C 6C")),
             OctetString.Empty),
         new Scope(
             new OctetString(ByteTool.Convert("80 00 1F 88 80 E9 63 00  00 D6 1F F4  49")),
             OctetString.Empty,
             new GetRequestPdu(
                 0x56FF,
                 new List<Variable>(1) { new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"), new Null()) })),
         pair,
         null);
     Assert.AreEqual(Levels.Authentication | Levels.Reportable, request.Header.SecurityLevel);
     Assert.AreEqual(ByteTool.Convert(bytes), request.ToBytes());
 }
Пример #17
0
 public void TestDiscoveryV3()
 {
     const string bytes = "30 3A 02 01 03 30 0F 02 02 6A 09 02 03 00 FF E3" +
                          " 04 01 04 02 01 03 04 10 30 0E 04 00 02 01 00 02" +
                          " 01 00 04 00 04 00 04 00 30 12 04 00 04 00 A0 0C" +
                          " 02 02 2C 6B 02 01 00 02 01 00 30 00";
     GetRequestMessage request = new GetRequestMessage(
         VersionCode.V3,
         new Header(
             new Integer32(0x6A09),
             new Integer32(0xFFE3),
             Levels.Reportable),
         new SecurityParameters(
             OctetString.Empty,
             Integer32.Zero,
             Integer32.Zero,
             OctetString.Empty,
             OctetString.Empty,
             OctetString.Empty),
         new Scope(
             OctetString.Empty,
             OctetString.Empty,
             new GetRequestPdu(0x2C6B, new List<Variable>())),
         DefaultPrivacyProvider.DefaultPair,
         null
        );
     string test = ByteTool.Convert(request.ToBytes());
     Assert.AreEqual(bytes, test);
 }