2021-07-30 15:27:24 +03:00
|
|
|
//go:build freebsd
|
|
|
|
|
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2024-08-05 17:12:33 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-07-30 15:27:24 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func ifaceHasStaticIP(ifaceName string) (ok bool, err error) {
|
2021-10-14 19:39:21 +03:00
|
|
|
const rcConfFilename = "etc/rc.conf"
|
2021-07-30 15:27:24 +03:00
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
walker := aghos.FileWalker(interfaceName(ifaceName).rcConfStaticConfig)
|
|
|
|
|
2022-03-30 15:11:57 +03:00
|
|
|
return walker.Walk(rootDirFS, rcConfFilename)
|
2021-07-30 15:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// rcConfStaticConfig checks if the interface is configured by /etc/rc.conf to
|
|
|
|
// have a static IP.
|
2021-08-13 19:20:17 +03:00
|
|
|
func (n interfaceName) rcConfStaticConfig(r io.Reader) (_ []string, cont bool, err error) {
|
2021-07-30 15:27:24 +03:00
|
|
|
s := bufio.NewScanner(r)
|
2021-08-13 19:20:17 +03:00
|
|
|
for pref := fmt.Sprintf("ifconfig_%s=", n); s.Scan(); {
|
2021-07-30 15:27:24 +03:00
|
|
|
line := strings.TrimSpace(s.Text())
|
2021-08-13 19:20:17 +03:00
|
|
|
if !strings.HasPrefix(line, pref) {
|
2021-07-30 15:27:24 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-13 19:20:17 +03:00
|
|
|
cfgLeft, cfgRight := len(pref)+1, len(line)-1
|
|
|
|
if cfgLeft >= cfgRight {
|
2021-07-30 15:27:24 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-13 19:20:17 +03:00
|
|
|
// TODO(e.burkov): Expand the check to cover possible
|
|
|
|
// configurations from man rc.conf(5).
|
|
|
|
fields := strings.Fields(line[cfgLeft:cfgRight])
|
2024-08-05 17:12:33 +03:00
|
|
|
switch {
|
|
|
|
case
|
|
|
|
len(fields) < 2,
|
|
|
|
!strings.EqualFold(fields[0], "inet"),
|
|
|
|
!netutil.IsValidIPString(fields[1]):
|
|
|
|
continue
|
|
|
|
default:
|
2021-08-13 19:20:17 +03:00
|
|
|
return nil, false, s.Err()
|
2021-07-30 15:27:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 19:20:17 +03:00
|
|
|
return nil, true, s.Err()
|
2021-07-30 15:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ifaceSetStaticIP(string) (err error) {
|
|
|
|
return aghos.Unsupported("setting static ip")
|
|
|
|
}
|