Skip to content

Commit

Permalink
add error_status for udp_server
Browse files Browse the repository at this point in the history
  • Loading branch information
mei-rune committed Nov 5, 2014
1 parent f768ba5 commit b75cb16
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ const (
SNMP_CODE_SYNTAX_NOSUCHINSTANCE SnmpResult = 17 /* exception */
SNMP_CODE_SYNTAX_ENDOFMIBVIEW SnmpResult = 18 /* exception */

cSNMP_CODE_ERR_NOERROR SnmpResult = 18
SNMP_CODE_ERR_TOOBIG SnmpResult = 19
SNMP_CODE_ERR_NOSUCHNAME SnmpResult = 20
SNMP_CODE_ERR_BADVALUE SnmpResult = 21
Expand Down
33 changes: 29 additions & 4 deletions pdu.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type V2CPDU struct {

max_repetitions int
non_repeaters int
error_status int32
}

func (pdu *V2CPDU) Init(params map[string]string) SnmpError {
Expand Down Expand Up @@ -98,6 +99,14 @@ func (pdu *V2CPDU) GetMaxMsgSize() uint {
return pdu.maxMsgSize
}

func (pdu *V2CPDU) GetErrorStatus() int32 {
return pdu.error_status
}

func (pdu *V2CPDU) SetErrorStatus(error_status int32) {
pdu.error_status = error_status
}

func (pdu *V2CPDU) GetVariableBindings() *VariableBindings {
return &pdu.variableBindings
}
Expand All @@ -113,6 +122,8 @@ func (pdu *V2CPDU) String() string {
buffer.WriteString(strconv.Itoa(pdu.GetRequestID()))
buffer.WriteString("' and version='")
buffer.WriteString(pdu.version.String())
buffer.WriteString("' and error_status='")
buffer.WriteString(strconv.Itoa(int(pdu.error_status)))
if SNMP_PDU_GETBULK == pdu.op {
buffer.WriteString("' and max_repetitions='")
buffer.WriteString(strconv.Itoa(pdu.max_repetitions))
Expand All @@ -131,6 +142,7 @@ func (pdu *V2CPDU) encodePDU(bs []byte, is_dump bool) ([]byte, SnmpError) {
releaseNativePdu(internal)
}()

internal.error_status = C.int32_t(pdu.error_status)
if SNMP_PDU_GETBULK == pdu.op {
if pdu.variableBindings.Len() < pdu.non_repeaters {
internal.error_status = C.int32_t(pdu.variableBindings.Len())
Expand Down Expand Up @@ -175,14 +187,13 @@ func (pdu *V2CPDU) encodePDU(bs []byte, is_dump bool) ([]byte, SnmpError) {
}

func (pdu *V2CPDU) decodePDU(native *C.snmp_pdu_t) (bool, SnmpError) {

native.community[MAX_COMMUNITY_LEN-1] = 0
pdu.community = C.GoString(&native.community[0])

pdu.requestId = int(native.request_id)
pdu.op = SnmpType(native.pdu_type)
pdu.version = SnmpVersion(native.version)

pdu.error_status = int32(native.error_status)
decodeBindings(native, pdu.GetVariableBindings())

if C.SNMP_ERR_NOERROR != native.error_status {
Expand All @@ -207,6 +218,7 @@ type V3PDU struct {

max_repetitions int
non_repeaters int
error_status int32
}

func (pdu *V3PDU) Init(params map[string]string) (err SnmpError) {
Expand Down Expand Up @@ -291,6 +303,14 @@ func (pdu *V3PDU) GetType() SnmpType {
return pdu.op
}

func (pdu *V3PDU) GetErrorStatus() int32 {
return pdu.error_status
}

func (pdu *V3PDU) SetErrorStatus(error_status int32) {
pdu.error_status = error_status
}

func (pdu *V3PDU) GetVariableBindings() *VariableBindings {
return &pdu.variableBindings
}
Expand Down Expand Up @@ -326,15 +346,18 @@ func (pdu *V3PDU) String() string {
}
buffer.WriteString(" and requestId='")
buffer.WriteString(strconv.Itoa(pdu.GetRequestID()))
buffer.WriteString(". and identifier='")
buffer.WriteString("' and identifier='")
buffer.WriteString(strconv.Itoa(pdu.identifier))
buffer.WriteString("' and version='v3'")
buffer.WriteString("' and version='v3' and error_status='")
buffer.WriteString(strconv.Itoa(int(pdu.error_status)))

if SNMP_PDU_GETBULK == pdu.op {
buffer.WriteString("' and max_repetitions='")
buffer.WriteString(strconv.Itoa(pdu.max_repetitions))
buffer.WriteString("' and non_repeaters='")
buffer.WriteString(strconv.Itoa(pdu.non_repeaters))
}
buffer.WriteString("'")
return buffer.String()
}

Expand All @@ -357,6 +380,7 @@ func (pdu *V3PDU) encodePDU(bs []byte, is_dump bool) ([]byte, SnmpError) {
internal.request_id = C.int32_t(pdu.requestId)
internal.pdu_type = C.u_int(pdu.op)
internal.version = uint32(SNMP_V3)
internal.error_status = C.int32_t(pdu.error_status)

if SNMP_PDU_GETBULK == pdu.op {
if pdu.variableBindings.Len() < pdu.non_repeaters {
Expand Down Expand Up @@ -449,6 +473,7 @@ func (pdu *V3PDU) decodePDU(native *C.snmp_pdu_t) (bool, SnmpError) {
pdu.engine.engine_boots = int(native.engine.engine_boots)
pdu.engine.engine_time = int(native.engine.engine_time)
pdu.maxMsgSize = uint(native.engine.max_msg_size)
pdu.error_status = int32(native.error_status)

pdu.securityModel = new(USM)
err := pdu.securityModel.Read(&native.user)
Expand Down
14 changes: 12 additions & 2 deletions udp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ type UdpServer struct {
priv_type PrivType
priv_key []byte

is_update_mibs bool
mibs *Tree
return_error_if_oid_not_exists bool
is_update_mibs bool
mibs *Tree
}

func NewUdpServerFromFile(nm, addr, file string, is_update_mibs bool) (*UdpServer, error) {
Expand Down Expand Up @@ -157,6 +158,11 @@ func NewUdpServerFromString(nm, addr, mibs string, is_update_mibs bool) (*UdpSer
return srv, srv.start()
}

func (self *UdpServer) ReturnErrorIfOidNotExists(status bool) *UdpServer {
self.return_error_if_oid_not_exists = status
return self
}

func (self *UdpServer) ReloadMibsFromString(mibs string) error {
self.mibs = NewMibTree()
if e := Read(bytes.NewReader([]byte(mibs)), func(oid SnmpOid, value SnmpValue) error {
Expand Down Expand Up @@ -263,6 +269,10 @@ func (self *UdpServer) on_v2(addr net.Addr, p *V2CPDU, cached_bytes []byte) {
for _, vb := range p.GetVariableBindings().All() {
v := self.GetValueByOid(vb.Oid)
if nil == v {
if self.return_error_if_oid_not_exists {
res.SetErrorStatus(int32(SNMP_CODE_ERR_NOSUCHNAME) - int32(cSNMP_CODE_ERR_NOERROR))
break
}
continue
}
res.GetVariableBindings().AppendWith(vb.Oid, v)
Expand Down

0 comments on commit b75cb16

Please sign in to comment.