func (msg *messageV3) Marshal() (b []byte, err error) { var buf []byte raw := asn1.RawValue{Class: classUniversal, Tag: 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...) raw.Bytes = append(raw.Bytes, msg.pduBytes...) return asn1.Marshal(raw) }
func (sec *securityParameterV3) Marshal() ([]byte, error) { raw := asn1.RawValue{Class: classUniversal, Tag: tagOctetString, IsCompound: false} buf, err := asn1.Marshal(*sec) if err != nil { return nil, err } raw.Bytes = buf return asn1.Marshal(raw) }
func (msg *messageV1) Marshal() (b []byte, err error) { var buf []byte raw := asn1.RawValue{Class: classUniversal, Tag: 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) }
// convertClass converts the encoding of values in SNMP response from // "custom" class to the corresponding "universal" class, thus enabling // use of the asn1 parser from the encoding/asn1 package. func convertClass(v *asn1.RawValue) { if v.Class != 1 { // Not a custom type. return } switch v.Tag { case 0, 4: // IpAddress ::= [APPLICATION 0] IMPLICIT OCTET STRING (SIZE (4)) // Opaque ::= [APPLICATION 4] IMPLICIT OCTET STRING v.FullBytes[0] = 0x04 v.Class = 0 v.Tag = 4 case 1, 2, 3, 6: // Counter32 ::= [APPLICATION 1] IMPLICIT INTEGER (0..4294967295) // Unsigned32 ::= [APPLICATION 2] IMPLICIT INTEGER (0..4294967295) // TimeTicks ::= [APPLICATION 3] IMPLICIT INTEGER (0..4294967295) // Counter64 ::= [APPLICATION 6] IMPLICIT INTEGER (0..18446744073709551615) v.FullBytes[0] = 0x02 v.Class = 0 v.Tag = 2 } }
func (v *VarBind) Marshal() (b []byte, err error) { var buf []byte raw := asn1.RawValue{Class: classUniversal, Tag: tagSequence, IsCompound: true} if v.Oid == nil || 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 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()) } if err != nil { return } raw := asn1.RawValue{Class: classUniversal, Tag: tagOctetString, IsCompound: false} raw.Bytes = dst dst, err = asn1.Marshal(raw) if err == nil { msg.SetPduBytes(dst) msg.PrivParameter = priv } return }
func (pdu *ScopedPdu) Marshal() (b []byte, err error) { var buf []byte raw := asn1.RawValue{Class: classUniversal, Tag: 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 (pdu *PduV1) Marshal() (b []byte, err error) { var buf []byte raw := asn1.RawValue{Class: classContextSpecific, Tag: int(pdu.pduType), IsCompound: true} 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...) varBinds := asn1.RawValue{Class: classUniversal, Tag: tagSequence, IsCompound: true} for i := 0; i < len(pdu.varBinds); i++ { buf, err = pdu.varBinds[i].Marshal() if err != nil { return } varBinds.Bytes = append(varBinds.Bytes, buf...) } buf, err = asn1.Marshal(varBinds) if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) return asn1.Marshal(raw) }
func (s *SNMP) V1Trap(varPduV1 TrapPduV1) (err error) { if s.args.Version > V1 { return ArgumentError{ Value: s.args.Version, Message: "V1trap Unsupported other SNMP Version", } } var buf []byte raw := asn1.RawValue{Class: classUniversal, Tag: tagSequence, IsCompound: true} //Version buf, err = asn1.Marshal(s.args.Version) if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) //Community buf, err = NewOctetString([]byte(s.args.Community)).Marshal() if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) //Data Trap buf = []byte{0xa4, 0x00} raw.Bytes = append(raw.Bytes, buf...) dataTrapLength := len(raw.Bytes) //Enterprise oid, _ := NewOid(varPduV1.Enterprise) buf, err = oid.Marshal() if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) //AgentAddr var ipByte [4]byte for n, v := range strings.Split(varPduV1.AgentAddr, ".") { input, _ := strconv.Atoi(v) ipByte[n] = (byte)(input) } ip := NewIpaddress(ipByte[0], ipByte[1], ipByte[2], ipByte[3]) buf, err = ip.Marshal() if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) //GenericTrap buf, err = NewInteger((int32)(varPduV1.GenericTrap)).Marshal() if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) //SpecificTrap buf, err = NewInteger((int32)(varPduV1.SpecificTrap)).Marshal() if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) //TimeStamp buf, err = NewTimeTicks((uint32)(varPduV1.TimeStamp)).Marshal() if err != nil { return } raw.Bytes = append(raw.Bytes, buf...) //VarBinds buf = []byte{0x30, 0x00} raw.Bytes = append(raw.Bytes, buf...) raw.Bytes[dataTrapLength-1] = (byte)(len(raw.Bytes) - dataTrapLength) marbuf, _ := asn1.Marshal(raw) fmt.Println(hex.Dump(marbuf)) s.conn.SetWriteDeadline(time.Now().Add(s.args.Timeout)) _, err = s.conn.Write(marbuf[:len(marbuf)]) fmt.Println("err = ", err) return err }