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) }
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) }
func (v *TimeTicks) Marshal() ([]byte, error) { b, err := asn1.Marshal(int64(v.Value)) if err == nil { b[0] = asn1.TagTimeticks } return b, err }
func (v *Opaque) Marshal() ([]byte, error) { b, err := asn1.Marshal(v.Value) if err == nil { b[0] = asn1.TagOpaque } return b, err }
func (v *Ipaddress) Marshal() ([]byte, error) { b, err := asn1.Marshal(v.Value) if err == nil { b[0] = asn1.TagIPAddress } return b, err }
func (v *Gauge32) Marshal() ([]byte, error) { b, err := asn1.Marshal(int64(v.Value)) if err == nil { b[0] = asn1.TagGauge32 } return b, err }
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 }
func (v *Counter64) Marshal() ([]byte, error) { i := big.NewInt(0).SetUint64(v.Value) b, err := asn1.Marshal(i) if err == nil { b[0] = asn1.TagCounter64 } return b, err }
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) }
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) }
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) }
func (h *globalDataV3) Marshal() (b []byte, err error) { return asn1.Marshal(*h) }
func (v *Oid) Marshal() ([]byte, error) { return asn1.Marshal(asn1.ObjectIdentifier(v.Value)) }
func (v *OctetString) Marshal() ([]byte, error) { return asn1.Marshal(v.Value) }
func (v *Integer) Marshal() ([]byte, error) { return asn1.Marshal(v.Value) }
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) }