2024-06-25 12:44:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PathProcPCI = iota
|
|
|
|
PathProcUSB
|
|
|
|
)
|
|
|
|
|
|
|
|
func (i *iface) ProcLoc() (err error) {
|
|
|
|
spath := strings.Join([]string{i.netpath, "device"}, "/")
|
|
|
|
|
|
|
|
var real string
|
|
|
|
if real, err = filepath.EvalSymlinks(spath); err != nil {
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
err = fmt.Errorf("Interface %s isn't a hardware device", i.names[IfNameKern])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("Failed to resolve symlink to device: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var devpath string
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
sysdev := strings.Join([]string{SYSFS, "devices/"}, "/")
|
|
|
|
if devpath, ok = strings.CutPrefix(real, sysdev); !ok {
|
|
|
|
err = fmt.Errorf("Symlink doesn't point to %s\n", sysdev)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-06-25 13:21:30 +00:00
|
|
|
pci, usb, tmp string
|
|
|
|
procmode int
|
2024-06-25 12:44:53 +00:00
|
|
|
)
|
|
|
|
for _, v := range strings.Split(devpath, "/") {
|
|
|
|
if strings.HasPrefix(v, "pci") {
|
|
|
|
procmode = PathProcPCI
|
|
|
|
continue
|
|
|
|
} else if strings.HasPrefix(v, "usb") {
|
|
|
|
procmode = PathProcUSB
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch procmode {
|
|
|
|
case PathProcPCI:
|
|
|
|
{
|
2024-06-25 13:21:30 +00:00
|
|
|
if tmp, err = i.ProcPCI(v); err != nil {
|
2024-06-25 12:44:53 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-06-25 13:21:30 +00:00
|
|
|
pci = tmp
|
2024-06-25 12:44:53 +00:00
|
|
|
}
|
|
|
|
case PathProcUSB:
|
|
|
|
{
|
2024-06-25 13:21:30 +00:00
|
|
|
if tmp, err = i.ProcUSB(v); err != nil {
|
2024-06-25 12:44:53 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-06-25 13:21:30 +00:00
|
|
|
usb = tmp
|
2024-06-25 12:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-25 13:21:30 +00:00
|
|
|
if pci == "" && usb == "" {
|
|
|
|
return
|
|
|
|
}
|
2024-06-25 12:44:53 +00:00
|
|
|
i.names[IfNameLoc] = strings.Join([]string{i.pfx, pci, usb}, "")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|