/
nettap.go
111 lines (90 loc) 路 3.05 KB
/
nettap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Linsk - A utility to access Linux-native file systems on non-Linux operating systems.
// Copyright (c) 2023 The Linsk Authors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package storage
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/AlexSSD7/linsk/nettap"
"github.com/pkg/errors"
)
const tapAllocPrefix = "tap_alloc_"
func (s *Storage) getAllocFilePath(tapName string) (string, error) {
err := nettap.ValidateTapName(tapName)
if err != nil {
return "", errors.Wrap(err, "validate tap name")
}
return filepath.Join(s.path, tapAllocPrefix+tapName), nil
}
func (s *Storage) SaveNetTapAllocation(tapName string, pid int) error {
allocFilePath, err := s.getAllocFilePath(tapName)
if err != nil {
return errors.Wrap(err, "get alloc file path")
}
err = os.WriteFile(allocFilePath, []byte(fmt.Sprint(pid)), 0400)
if err != nil {
return errors.Wrap(err, "write alloc file")
}
return nil
}
func (s *Storage) ReleaseNetTapAllocation(tapName string) error {
allocFilePath, err := s.getAllocFilePath(tapName)
if err != nil {
return errors.Wrap(err, "get alloc file path")
}
err = os.Remove(allocFilePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
s.logger.Warn("Attempted to remove non-existent tap allocation", "tap-name", tapName)
return nil
}
return errors.Wrap(err, "remove alloc file")
}
return nil
}
func (s *Storage) ListNetTapAllocations() ([]nettap.Alloc, error) {
dirEntries, err := os.ReadDir(s.path)
if err != nil {
return nil, errors.Wrap(err, "read data dir")
}
var ret []nettap.Alloc
for _, entry := range dirEntries {
if strings.HasPrefix(entry.Name(), tapAllocPrefix) {
entryPath := filepath.Clean(filepath.Join(s.path, entry.Name()))
tapName := strings.TrimPrefix(entry.Name(), tapAllocPrefix)
err := nettap.ValidateTapName(tapName)
if err != nil {
s.logger.Error("Failed to validate network tap name in tap allocation file, skipping. External interference?", "error", err.Error(), "name", tapName, "path", entryPath)
continue
}
data, err := os.ReadFile(entryPath)
if err != nil {
return nil, errors.Wrapf(err, "read tap alloc file '%v'", entryPath)
}
pid, err := strconv.ParseUint(string(data), 10, strconv.IntSize-1) // We're aiming for `int` PID.
if err != nil {
return nil, errors.Wrapf(err, "parse pid (alloc file '%v')", entryPath)
}
ret = append(ret, nettap.Alloc{
TapName: tapName,
PID: int(pid),
})
}
}
return ret, nil
}