gosnmp
/
v3_map.go
53 строки · 1.4 Кб
1// Copyright 2023 The GoSNMP Authors. All rights reserved. Use of this
2// source code is governed by a BSD-style license that can be found in the
3// LICENSE file.
4
5package gosnmp
6
7import (
8"fmt"
9"sync"
10)
11
12// SnmpV3SecurityParametersTable is a mapping of identifiers to corresponding SNMP V3 Security Model parameters
13type SnmpV3SecurityParametersTable struct {
14table map[string][]SnmpV3SecurityParameters
15Logger Logger
16mu sync.RWMutex
17}
18
19func NewSnmpV3SecurityParametersTable(logger Logger) *SnmpV3SecurityParametersTable {
20return &SnmpV3SecurityParametersTable{
21table: make(map[string][]SnmpV3SecurityParameters),
22Logger: logger,
23}
24}
25
26func (spm *SnmpV3SecurityParametersTable) Add(key string, sp SnmpV3SecurityParameters) error {
27spm.mu.Lock()
28defer spm.mu.Unlock()
29
30if err := sp.InitSecurityKeys(); err != nil {
31return err
32}
33
34// If no logger is set for the security params (empty struct), use the one from the table
35if (Logger{}) == sp.getLogger() {
36sp.setLogger(spm.Logger)
37}
38
39spm.table[key] = append(spm.table[key], sp)
40spm.Logger.Printf("Added security parameters %s for key: %s", sp.SafeString(), key)
41
42return nil
43}
44
45func (spm *SnmpV3SecurityParametersTable) Get(key string) ([]SnmpV3SecurityParameters, error) {
46spm.mu.RLock()
47defer spm.mu.RUnlock()
48
49if sp, ok := spm.table[key]; ok {
50return sp, nil
51}
52return nil, fmt.Errorf("no security parameters found for the key %s", key)
53}
54