sharpsnmplib
sharpsnmplib copied to clipboard
Accessing raw data in ISnmpData
The extension ToBytes provided in SnmpDataExtension does this, but it apparently includes the data-type bytes too. For instance, for an IPAddress datatype it returns 6 bytes instead of 4.
It would be nice if ISnmpData interface expose a function like GetRaw to let the user access the underyling raw bytes without type.
For instance for value of 255.255.0.0 I get:
{ 0x40, 0x04, 0xFF, 0xFF, 0x00, 0x00 }
Apparently 0x40 is type for IpAddress and 0x04 specifies value length. As a workaround one could ignore the first two bytes.
It is not a standard API yet, but some ISnmpData derived types have a GetRaw method, which exposes the raw bytes.
Your workaround might not work in all cases, as the length bytes can be more than one.
Thanks for extremely fast response!
It is not a standard API yet
Does that you mean keeping the current API as is to avoid any unexpected breaking change?
If someone add GetRaw to ISnmpData interface and implement it for derived types in sharpsnmplib would that be OK? Or there is more obstacles than that?
Does sharpsnmplib follows a particular API? I suspected this because the ToBytes method is implemented as extension method in SnmpDataExtension instead of directly implementing it inside ISnmpData.
some ISnmpData derived types have a GetRaw method, which exposes the raw bytes.
Thats' correct, but still for each case one has to cast to ISnmpData to corresponding derived type based on TypeCode.
Another solution could be adding GetRaw() as an extension method.
Your workaround might not work in all cases
I was afraid of that.
There is no plan to fix the API for current 9.x/10.x releases.
I am investigating Microsoft's latest API additions (Span and System.IO.Pipelines), so the next major release 11.x should contain significant API surface changes, where GetRaw is very likely to be exposed as part of ISnmpData.