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