gpt4 book ai didi

go - 递归定义 SNMP 消息

转载 作者:数据小太阳 更新时间:2023-10-29 03:11:02 26 4
gpt4 key购买 nike

我在 Go 中弄乱了一个 SNMP 库,并提出了一个 Field 类型,它根据 this document 定义了一个 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 .

关于go - 递归定义 SNMP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51698879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com