コード例 #1
0
ファイル: message.go プロジェクト: runner-mei/snmpclient2
func (msg *MessageV3) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	buf, err = asn1.Marshal(msg.version)
	if err != nil {
		return
	}
	raw.Bytes = buf

	buf, err = msg.globalDataV3.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	buf, err = msg.securityParameterV3.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	// if 0 == len(msg.pduBytes) {
	// 	panic("pdu bytes is empty.")
	// }
	raw.Bytes = append(raw.Bytes, msg.pduBytes...)
	return asn1.Marshal(raw)
}
コード例 #2
0
ファイル: security.go プロジェクト: runner-mei/snmpclient2
func encrypt(msg *MessageV3, proto PrivProtocol, key []byte) (err error) {
	var dst, priv []byte
	src := msg.PduBytes()

	switch proto {
	case Des:
		dst, priv, err = EncryptDES(src, key, int32(msg.AuthEngineBoots), genSalt32())
	case Aes:
		dst, priv, err = EncryptAES(
			src, key, int32(msg.AuthEngineBoots), int32(msg.AuthEngineTime), genSalt64())
	default:
		err = errors.New("'" + fmt.Sprint(proto) + "' is unsupported crypto.")
	}
	if err != nil {
		return
	}

	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagOctetString, IsCompound: false}
	raw.Bytes = dst
	dst, err = asn1.Marshal(raw)
	if err == nil {
		msg.SetPduBytes(dst)
		msg.PrivParameter = priv
	}
	return
}
コード例 #3
0
ファイル: message.go プロジェクト: runner-mei/snmpclient2
func (sec *securityParameterV3) Marshal() ([]byte, error) {
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagOctetString, IsCompound: false}

	buf, err := asn1.Marshal(*sec)
	if err != nil {
		return nil, err
	}
	raw.Bytes = buf

	return asn1.Marshal(raw)
}
コード例 #4
0
ファイル: message.go プロジェクト: runner-mei/snmpclient2
func (msg *MessageV1) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	buf, err = asn1.Marshal(msg.version)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	buf, err = asn1.Marshal(msg.Community)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	raw.Bytes = append(raw.Bytes, msg.pduBytes...)
	return asn1.Marshal(raw)
}
コード例 #5
0
ファイル: pdu.go プロジェクト: runner-mei/snmpclient2
func (v *VariableBinding) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	if v.Variable == nil {
		return asn1.Marshal(raw)
	}

	buf, err = v.Oid.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = buf

	buf, err = v.Variable.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	return asn1.Marshal(raw)
}
コード例 #6
0
ファイル: pdu.go プロジェクト: runner-mei/snmpclient2
func (pdu *ScopedPdu) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}

	buf, err = asn1.Marshal(pdu.ContextEngineId)
	if err != nil {
		return
	}
	raw.Bytes = buf

	buf, err = asn1.Marshal(pdu.ContextName)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	buf, err = pdu.PduV1.Marshal()
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)

	return asn1.Marshal(raw)
}
コード例 #7
0
ファイル: pdu.go プロジェクト: runner-mei/snmpclient2
func (pdu *PduV1) Marshal() (b []byte, err error) {
	var buf []byte
	raw := asn1.RawValue{Class: asn1.ClassContextSpecific, Tag: int(pdu.pduType), IsCompound: true}

	if Trap == pdu.pduType {
		buf, err = pdu.Enterprise.Marshal()
		if err != nil {
			return
		}
		raw.Bytes = buf

		buf, err = pdu.AgentAddress.Marshal()
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.GenericTrap)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.SpecificTrap)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.Timestamp)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)
	} else {
		buf, err = asn1.Marshal(pdu.requestId)
		if err != nil {
			return
		}
		raw.Bytes = buf

		buf, err = asn1.Marshal(pdu.errorStatus)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)

		buf, err = asn1.Marshal(pdu.errorIndex)
		if err != nil {
			return
		}
		raw.Bytes = append(raw.Bytes, buf...)
	}

	VariableBindings := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true}
	for i := 0; i < len(pdu.variableBindings); i++ {
		buf, err = pdu.variableBindings[i].Marshal()
		if err != nil {
			return
		}
		VariableBindings.Bytes = append(VariableBindings.Bytes, buf...)
	}

	buf, err = asn1.Marshal(VariableBindings)
	if err != nil {
		return
	}
	raw.Bytes = append(raw.Bytes, buf...)
	return asn1.Marshal(raw)
}