public static SnmpDatagram ToSnmpDatagram()

in Source/Tx.Network/Snmp/SnmpEncodeDecoder.cs [92:179]


        public static SnmpDatagram ToSnmpDatagram(
            this ArraySegment<byte> byteSegment,
            DateTimeOffset timestamp,
            string sourceIpAddress)
        {
            var bytes = byteSegment.Array;
            if(bytes == null || bytes.Length == 0)
            {
                throw new ArgumentNullException("byteSegment");
            }

            int offset = byteSegment.Offset;
            int length;
            offset = bytes.NextValueLength(offset, -1, -1, -1, out length);
            offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
            SnmpVersion snmpVersion = (SnmpVersion)bytes.ReadInteger(offset, length);
            offset += length;

            if (snmpVersion == SnmpVersion.V3)
            {
                throw new InvalidDataException("Snmp Version V3 not supported");
            }

            offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.OctetString, out length);
            string community = bytes.ReadOctetString(offset, length);
            offset += length;

            PduType pduType = (PduType)(bytes[offset++] & 0x1F);
            offset = bytes.ReadLength(offset, out length);
            if (snmpVersion == SnmpVersion.V1 && pduType== PduType.Trap)
            {
                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.ObjectIdentifier, out length);
                ObjectIdentifier oid = new ObjectIdentifier(bytes.ReadOids(offset, length));
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1SnmpTag.IpAddress, out length);
                IPAddress ipAddress = bytes.ReadIpAddress(offset);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                GenericTrap genericTrap = (GenericTrap)bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                int specificTrap = bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1SnmpTag.TimeTicks, out length);
                uint timeStamp = bytes.ReadUnsignedInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, (int)Asn1Class.Universal, (int)ConstructType.Constructed, (int)Asn1Tag.Sequence, out length);
                VarBind[] varBinds = bytes.ReadVarBinds(offset, length);

                return new SnmpDatagramV1(
                    timestamp,
                    sourceIpAddress,
                    new SnmpHeader(snmpVersion, community), 
                    varBinds);
            }
            else
            {
                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                int requestId = bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                SnmpErrorStatus errorStatus = (SnmpErrorStatus)bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length);
                int errorIndex = bytes.ReadInteger(offset, length);
                offset += length;

                offset = bytes.NextValueLength(offset, (int)Asn1Class.Universal, (int)ConstructType.Constructed, (int)Asn1Tag.Sequence, out length);
                VarBind[] varBinds = bytes.ReadVarBinds(offset, length);

                return new SnmpDatagramV2C(
                    timestamp, 
                    sourceIpAddress, 
                    new SnmpHeader(snmpVersion, community), 
                    varBinds,
                    pduType,
                    requestId,
                    errorStatus,
                    errorIndex);
            }
        }