Files
go-agentx
marshaler
pdu
allocate_index.go
close.go
deallocate_index.go
error.go
flags.go
get.go
get_next.go
header.go
header_packet.go
object_identifier.go
octet_string.go
open.go
packet.go
range.go
ranges.go
reason.go
register.go
response.go
timeout.go
type.go
unregister.go
variable.go
variable_type.go
variables.go
value
.gitignore
AUTHORS
LICENSE
README.md
client.go
environment_test.go
go.mod
go.sum
handler.go
helper_test.go
list_handler.go
list_handler_test.go
list_item.go
request.go
session.go
session_test.go
shell.nix
snmpd.conf
ifmib
vppstats
.gitignore
README.md
go.mod
go.sum
main.go
govpp-snmp-agentx/go-agentx/pdu/object_identifier.go

97 lines
2.5 KiB
Go

// Copyright 2018 The agentx authors
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package pdu
import (
"bytes"
"encoding/binary"
"github.com/posteo/go-agentx/value"
)
// ObjectIdentifier defines the pdu object identifier packet.
type ObjectIdentifier struct {
Prefix uint8
Include byte
Subidentifiers []uint32
}
// SetInclude sets the include field.
func (o *ObjectIdentifier) SetInclude(value bool) {
if value {
o.Include = 0x01
} else {
o.Include = 0x00
}
}
// GetInclude returns true if the include field ist set, false otherwise.
func (o *ObjectIdentifier) GetInclude() bool {
if o.Include == 0x00 {
return false
}
return true
}
// SetIdentifier set the subidentifiers by the provided oid string.
func (o *ObjectIdentifier) SetIdentifier(oid value.OID) {
o.Subidentifiers = make([]uint32, 0)
if len(oid) > 4 && oid[0] == 1 && oid[1] == 3 && oid[2] == 6 && oid[3] == 1 {
o.Subidentifiers = append(o.Subidentifiers, uint32(1), uint32(3), uint32(6), uint32(1), uint32(oid[4]))
oid = oid[5:]
}
o.Subidentifiers = append(o.Subidentifiers, oid...)
}
// GetIdentifier returns the identifier as an oid string.
func (o *ObjectIdentifier) GetIdentifier() value.OID {
var oid value.OID
if o.Prefix != 0 {
oid = append(oid, 1, 3, 6, 1, uint32(o.Prefix))
}
return append(oid, o.Subidentifiers...)
}
// ByteSize returns the number of bytes, the binding would need in the encoded version.
func (o *ObjectIdentifier) ByteSize() int {
return 4 + len(o.Subidentifiers)*4
}
// MarshalBinary returns the pdu packet as a slice of bytes.
func (o *ObjectIdentifier) MarshalBinary() ([]byte, error) {
buffer := bytes.NewBuffer([]byte{byte(len(o.Subidentifiers)), o.Prefix, o.Include, 0x00})
for _, subidentifier := range o.Subidentifiers {
binary.Write(buffer, binary.LittleEndian, &subidentifier)
}
return buffer.Bytes(), nil
}
// UnmarshalBinary sets the packet structure from the provided slice of bytes.
func (o *ObjectIdentifier) UnmarshalBinary(data []byte) error {
count := data[0]
o.Prefix = data[1]
o.Include = data[2]
o.Subidentifiers = make([]uint32, 0)
buffer := bytes.NewBuffer(data[4:])
for index := byte(0); index < count; index++ {
var subidentifier uint32
if err := binary.Read(buffer, binary.LittleEndian, &subidentifier); err != nil {
return err
}
o.Subidentifiers = append(o.Subidentifiers, subidentifier)
}
return nil
}
func (o ObjectIdentifier) String() string {
return o.GetIdentifier().String()
}