gosnmp

Форк
0
/
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

5
package gosnmp
6

7
import (
8
	"fmt"
9
	"sync"
10
)
11

12
// SnmpV3SecurityParametersTable is a mapping of identifiers to corresponding SNMP V3 Security Model parameters
13
type SnmpV3SecurityParametersTable struct {
14
	table  map[string][]SnmpV3SecurityParameters
15
	Logger Logger
16
	mu     sync.RWMutex
17
}
18

19
func NewSnmpV3SecurityParametersTable(logger Logger) *SnmpV3SecurityParametersTable {
20
	return &SnmpV3SecurityParametersTable{
21
		table:  make(map[string][]SnmpV3SecurityParameters),
22
		Logger: logger,
23
	}
24
}
25

26
func (spm *SnmpV3SecurityParametersTable) Add(key string, sp SnmpV3SecurityParameters) error {
27
	spm.mu.Lock()
28
	defer spm.mu.Unlock()
29

30
	if err := sp.InitSecurityKeys(); err != nil {
31
		return err
32
	}
33

34
	// If no logger is set for the security params (empty struct), use the one from the table
35
	if (Logger{}) == sp.getLogger() {
36
		sp.setLogger(spm.Logger)
37
	}
38

39
	spm.table[key] = append(spm.table[key], sp)
40
	spm.Logger.Printf("Added security parameters %s for key: %s", sp.SafeString(), key)
41

42
	return nil
43
}
44

45
func (spm *SnmpV3SecurityParametersTable) Get(key string) ([]SnmpV3SecurityParameters, error) {
46
	spm.mu.RLock()
47
	defer spm.mu.RUnlock()
48

49
	if sp, ok := spm.table[key]; ok {
50
		return sp, nil
51
	}
52
	return nil, fmt.Errorf("no security parameters found for the key %s", key)
53
}
54

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.