Esempio n. 1
0
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)
}
Esempio n. 2
0
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)
}
Esempio n. 3
0
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)
}
Esempio n. 4
0
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)
}
Esempio n. 5
0
File: pdu.go Progetto: mgenov/snmpgo
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)
}
Esempio n. 6
0
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)
}
Esempio n. 7
0
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
}
Esempio n. 8
0
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
}