2021-07-30 12:27:24 +00: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) {
|
|
|
|
const filename = "/etc/rc.conf"
|
|
|
|
|
2021-08-13 16:20:17 +00:00
|
|
|
return aghos.FileWalker(interfaceName(ifaceName).rcConfStaticConfig).Walk(filename)
|
2021-07-30 12:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rcConfStaticConfig checks if the interface is configured by /etc/rc.conf to
|
|
|
|
// have a static IP.
|
2021-08-13 16:20:17 +00:00
|
|
|
func (n interfaceName) rcConfStaticConfig(r io.Reader) (_ []string, cont bool, err error) {
|
2021-07-30 12:27:24 +00:00
|
|
|
s := bufio.NewScanner(r)
|
2021-08-13 16:20:17 +00:00
|
|
|
for pref := fmt.Sprintf("ifconfig_%s=", n); s.Scan(); {
|
2021-07-30 12:27:24 +00:00
|
|
|
line := strings.TrimSpace(s.Text())
|
2021-08-13 16:20:17 +00:00
|
|
|
if !strings.HasPrefix(line, pref) {
|
2021-07-30 12:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-13 16:20:17 +00:00
|
|
|
cfgLeft, cfgRight := len(pref)+1, len(line)-1
|
|
|
|
if cfgLeft >= cfgRight {
|
2021-07-30 12:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-13 16:20:17 +00: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 12:27:24 +00:00
|
|
|
if len(fields) >= 2 &&
|
2021-08-13 16:20:17 +00:00
|
|
|
strings.EqualFold(fields[0], "inet") &&
|
2021-07-30 12:27:24 +00:00
|
|
|
net.ParseIP(fields[1]) != nil {
|
2021-08-13 16:20:17 +00:00
|
|
|
return nil, false, s.Err()
|
2021-07-30 12:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 16:20:17 +00:00
|
|
|
return nil, true, s.Err()
|
2021-07-30 12:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ifaceSetStaticIP(string) (err error) {
|
|
|
|
return aghos.Unsupported("setting static ip")
|
|
|
|
}
|