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