0

https://github.com/lextm/sharpsnmplib/blob/master/SharpSnmpLib/IP.cs

System.ArgumentException: bytes must contain 4 or 16 elements
at Lextm.SharpSnmpLib.IP..ctor(Tuple`2 length, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream)
at Lextm.SharpSnmpLib.Sequence..ctor(Tuple`2 length, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream)
at Lextm.SharpSnmpLib.Sequence..ctor(Tuple`2 length, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream)
at Lextm.SharpSnmpLib.ResponsePdu..ctor(Tuple`2 length, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream)
at Lextm.SharpSnmpLib.Sequence..ctor(Tuple`2 length, Stream stream)
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream)
at Lextm.SharpSnmpLib.Messaging.MessageFactory.ParseMessage(Int32 first, Stream stream,            UserRegistry registry)
at Lextm.SharpSnmpLib.Messaging.MessageFactory.ParseMessages(Byte[] buffer, Int32 index, Int32  length, UserRegistry registry)
at Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension.GetResponse(ISnmpMessage request, Int32   timeout, IPEndPoint receiver, UserRegistry registry, Socket udpSocket)
at Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension.GetResponse(ISnmpMessage request, Int32 timeout, IPEndPoint receiver, Socket udpSocket)
at Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension.GetResponse(ISnmpMessage request, Int32 timeout, IPEndPoint receiver)
at Lextm.SharpSnmpLib.Messaging.Messenger.BulkHasNext(VersionCode version, IPEndPoint endpoint,  OctetString community, Variable seed, Int32 timeout, Int32 maxRepetitions, IList`1& next,  IPrivacyProvider privacy, ISnmpMessage& report)
at Lextm.SharpSnmpLib.Messaging.Messenger.BulkWalk(VersionCode version, IPEndPoint endpoint, OctetString community, ObjectIdentifier table, IList`1 list, Int32 timeout, Int32 maxRepetitions, WalkMode mode, IPrivacyProvider privacy, ISnmpMessage report)
at Maprinter.snmpWalk..ctor(String IP, String ID, Int32 timeOut)

I'm using this library in order to pull some data from network printers. So far all works good and most of the printers return me the data I'm looking for. But when I get this error I don't receive anything from the printer so what's causes this error?

Messenger.BulkWalk(VersionCode.V2,
                                   new IPEndPoint(IPAddress.Parse("10.0.0.101"), 161),
                                   new OctetString("public"),
                                   new ObjectIdentifier("1.3.6.1"),
                                   result,
                                   timeOut,
                                   10,
                                   WalkMode.Default,
                                   null,
                                   null);
Guy Messika
  • 169
  • 1
  • 15
  • Please show us the code that is causing the exception? More specifically show us the paramteres you're passing to the IP class constructor – Yuval Itzchakov Jul 05 '14 at 09:38
  • I get the IP directly from the printer and I updated the question to show my code. @YuvalItzchakov – Guy Messika Jul 05 '14 at 09:48
  • Which line is causing the exception? – Yuval Itzchakov Jul 05 '14 at 09:52
  • Messenger.BulkWalk(VersionCode.V2, new IPEndPoint(IPAddress.Parse(IP), 161), new OctetString("public"), new ObjectIdentifier(ID), result, timeOut, 10, WalkMode.Default, null, null); @YuvalItzchakov – Guy Messika Jul 05 '14 at 10:02
  • When does this error occur? does it repeat itself in certain scenarios? or does it happen randomly? – Yuval Itzchakov Jul 05 '14 at 10:10
  • It happens in specific printers with specific IP. @YuvalItzchakov – Guy Messika Jul 05 '14 at 10:15
  • It looks like the problem is with the [StreamExtension](https://github.com/lextm/sharpsnmplib/blob/master/SharpSnmpLib/StreamExtension.cs) class which is trying to parse a `MemoryStream` and is probably doing it incorrectly. Seems like a bug in the library, or you're passing an invalid IP – Yuval Itzchakov Jul 05 '14 at 10:51
  • yes, I see what you mean. using the Messenger.Get() function in the library I'm able to pull data from the printer. but when I want to use the Messenger.BulkWalk() function it doesn't work with same IP and the same OID. @YuvalItzchakov – Guy Messika Jul 06 '14 at 09:55
  • You should debug the source code when inputing a problematic IP address and see why it throws. – Yuval Itzchakov Jul 06 '14 at 09:58
  • currently it throws timeout cause I'm not on the customer's coumputer so I don't have this IP in my local network. how can I debug this thing? @YuvalItzchakov – Guy Messika Jul 07 '14 at 07:27
  • Lets [continue this in chat](http://chat.stackoverflow.com/rooms/56872/sharpsnmplib-bytes-must-contain-4-or-16-elements-what-causes-this-error) – Yuval Itzchakov Jul 07 '14 at 07:32

1 Answers1

1

The exception was likely caused by the SNMP agent on that device who sent an empty IpAddress body (0x40, 0x00). That violates the standard, as the result should be a Null body (0x05, 0x00).

IpAddress is defined in RFC2578, which is strictly of 4 bytes. That's why #SNMP checks against 4. The check against 16 is for IPv6 addresses, although they in fact should be supported by custom conventions.

In your case, the options can be,

  • Fix the firmware so the correct body is sent.
  • Modify #SNMP code base (MIT/X11 license) and suit your needs.
Community
  • 1
  • 1
Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • I want to be able to work with every printer that supports SNMP.. fix the firmware wouldn't fix the problem with other printers. I clearly see that you understand what should be done and I know you're the developer of this library. Can you pleas suggest to me what needs to be done? What should I modify in your library and which file should I go to? @Lex Li – Guy Messika Jul 08 '14 at 10:16
  • As I understand your answer I need to allow my software to receive both (0x40, 0x00) and (0x05, 0x00) as null from the printer. So I found this file: https://github.com/lextm/sharpsnmplib/blob/master/SharpSnmpLib/SnmpType.cs#L59 it defines Null as 0x05. How can I define Null both to 0x05 and 0x40? @Lex Li – Guy Messika Jul 08 '14 at 10:51
  • I wish you had time to carefully read every word I typed. Obviously I showed you 0x40 is for `IpAddress`. To allow 0x40, 0x00 to be received without exceptions, you might simply comment out the line that throws the exception. If you could not even understand that, you probably need to learn more C#. I cannot suggest you further, as all key points are covered. If you do need commercial consulting services, let me know. – Lex Li Jul 08 '14 at 12:29
  • comment out the line that throws the exception did nothing to resolve my problem. – Guy Messika Jul 22 '14 at 21:54