登录
首页 >  Golang >  Go问答

定义 SNMP 消息的递归方式

来源:stackoverflow

时间:2024-03-13 10:54:23 393浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《定义 SNMP 消息的递归方式》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

我正在弄乱 go 中的 snmp 库,并想出了一个类型 field ,它根据本文档定义了 snmp ber 编码字段。每个字段由类型、长度和值组成,其中类型是 asn.1 类型,长度是字段值的长度,值可以是另一个字段、字段序列或字节序列。这让我思考递归定义 snmp 消息的可能性。这是我想出的一些代码,但我一直试图将其转换为递归结构:

package main

import "fmt"

// ASN.1 BER encoded types.
type ASN1BER byte

const (
    Integer          ASN1BER = 0x02
    BitString                = 0x03
    OctetString              = 0x04
    Null                     = 0x05
    ObjectIdentifier         = 0x06
    Sequence                 = 0x30
    GetRequest               = 0xa0
)

// SNMP versions.
type Version byte

const (
    Version1  Version = 0x00
    Version2c         = 0x01
)

// SNMP message field.
type Field struct {
    Type  ASN1BER
    Len   int
    Value interface{}
}

func main() {
    // SNMP Message
    msg := &Field{Type: Sequence, Len: 44, Value: []*Field{
        // SNMP Version
        {Type: Integer, Len: 1, Value: Version2c},
        // Community String
        {Type: OctetString, Len: 7, Value: []byte("private")},
        // Get-Request PDU
        {Type: GetRequest, Len: 30, Value: []*Field{
            // Request ID
            {Type: Integer, Len: 1, Value: []byte{0x01}},
            // Error Status
            {Type: Integer, Len: 1, Value: []byte{0x00}},
            // Error Index
            {Type: Integer, Len: 1, Value: []byte{0x00}},
            // Varbind List
            {Type: Sequence, Len: 19, Value: []*Field{
                // Varbind
                {Type: Sequence, Len: 17, Value: []*Field{
                    // OID 1.3.6.1.4.1.2680.1.2.7.3.2.0
                    {Type: ObjectIdentifier, Len: 13, Value: []byte{0x2b, 0x06, 0x01, 0x04, 0x01, 0x94, 0x78, 0x01, 0x02, 0x07, 0x03, 0x02, 0x00}},
                    // Value
                    {Type: Null, Len: 0},
                }}},
            }},
        }},
    }
    fmt.Println(msg)
}

是否可以递归地表示 snmp 消息?如果是这样,基本情况和递归情况是什么?目标是能够递归地打印、编码和解码 snmp 消息。


解决方案


解决方案是有一个 type switch 这将根据值的实际类型分支到不同的代码路径 - 可能是递归的。

package main

import (
    "fmt"
    "log"
    "reflect"
)

// ASN.1 BER encoded types.
type ASN1BER byte

const (
    Integer          ASN1BER = 0x02
    BitString                = 0x03
    OctetString              = 0x04
    Null                     = 0x05
    ObjectIdentifier         = 0x06
    Sequence                 = 0x30
    GetRequest               = 0xa0
)

// SNMP versions.
type Version byte

const (
    Version1  Version = 0x00
    Version2c         = 0x01
)

// SNMP message field.
type Field struct {
    Type  ASN1BER
    Len   int
    Value interface{}
}

func walk(f *Field, indent string) error {
    switch f.Type {
    case GetRequest, Sequence:
        indent += "\t"
        switch v := f.Value.(type) {
        case *Field:
            return walk(v, indent)
        case []*Field:
            for _, f := range v {
                err := walk(f, indent)
                if err != nil {
                    return err
                }
            }
        default:
            return &ValueTypeError{
                ASNType:   f.Type,
                ValueType: reflect.TypeOf(v),
            }
        }
    default:
        fmt.Printf("%sType: %d; value: %v\n", indent, f.Type, f.Value)
    }
    return nil
}

type ValueTypeError struct {
    ASNType   ASN1BER
    ValueType reflect.Type
}

func (e *ValueTypeError) Error() string {
    return fmt.Sprintf("invalid Go type (%s) for ASN1BER type %d",
        e.ValueType.Name(), e.ASNType)
}

func main() {
    // SNMP Message
    msg := Field{Type: Sequence, Len: 44, Value: []*Field{
        // SNMP Version
        {Type: Integer, Len: 1, Value: Version2c},
        // Community String
        {Type: OctetString, Len: 7, Value: []byte("private")},
        // Get-Request PDU
        {Type: GetRequest, Len: 30, Value: []*Field{
            // Request ID
            {Type: Integer, Len: 1, Value: []byte{0x01}},
            // Error Status
            {Type: Integer, Len: 1, Value: []byte{0x00}},
            // Error Index
            {Type: Integer, Len: 1, Value: []byte{0x00}},
            // Varbind List
            {Type: Sequence, Len: 19, Value: []*Field{
                // Varbind
                {Type: Sequence, Len: 17, Value: []*Field{
                    // OID 1.3.6.1.4.1.2680.1.2.7.3.2.0
                    {Type: ObjectIdentifier, Len: 13, Value: []byte{0x2b, 0x06, 0x01, 0x04, 0x01, 0x94, 0x78, 0x01, 0x02, 0x07, 0x03, 0x02, 0x00}},
                    // Value
                    {Type: Null, Len: 0},
                }}},
            }},
        }},
    }

    bad := Field{
        Type:  Sequence,
        Value: 42,
    }

    for i, f := range [...]*Field{&msg, &bad} {
        log.Println("walking:", i)
        err := walk(f, "")
        if err != nil {
            log.Println("error:", err)
        }
    }
}

Playground link

今天关于《定义 SNMP 消息的递归方式》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>