-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge in DNS/adguard-home from 3289-freebsd-dhcp to master Updates #3289. Squashed commit of the following: commit 1365d8f Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Jul 30 15:01:13 2021 +0300 dhcpd: fix doc commit 26724df Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Jul 30 14:52:58 2021 +0300 all: imp code & docs commit 9a9574a Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Jul 29 15:51:07 2021 +0300 all: fix broadcasting, sup freebsd dhcp, fix http response
- Loading branch information
1 parent
63ee95d
commit 6fa1167
Showing
7 changed files
with
178 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//go:build freebsd | ||
// +build freebsd | ||
|
||
package aghnet | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"net" | ||
"os" | ||
"strings" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghio" | ||
"github.com/AdguardTeam/AdGuardHome/internal/aghos" | ||
"github.com/AdguardTeam/golibs/errors" | ||
) | ||
|
||
func canBindPrivilegedPorts() (can bool, err error) { | ||
return aghos.HaveAdminRights() | ||
} | ||
|
||
// maxCheckedFileSize is the maximum acceptable length of the /etc/rc.conf file. | ||
const maxCheckedFileSize = 1024 * 1024 | ||
|
||
func ifaceHasStaticIP(ifaceName string) (ok bool, err error) { | ||
const filename = "/etc/rc.conf" | ||
|
||
var f *os.File | ||
f, err = os.Open(filename) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer func() { err = errors.WithDeferred(err, f.Close()) }() | ||
|
||
var r io.Reader | ||
r, err = aghio.LimitReader(f, maxCheckedFileSize) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return rcConfStaticConfig(r, ifaceName) | ||
} | ||
|
||
// rcConfStaticConfig checks if the interface is configured by /etc/rc.conf to | ||
// have a static IP. | ||
func rcConfStaticConfig(r io.Reader, ifaceName string) (has bool, err error) { | ||
s := bufio.NewScanner(r) | ||
for ifaceLinePref := fmt.Sprintf("ifconfig_%s", ifaceName); s.Scan(); { | ||
line := strings.TrimSpace(s.Text()) | ||
if !strings.HasPrefix(line, ifaceLinePref) { | ||
continue | ||
} | ||
|
||
eqIdx := len(ifaceLinePref) | ||
if line[eqIdx] != '=' { | ||
continue | ||
} | ||
|
||
fieldsStart, fieldsEnd := eqIdx+2, len(line)-1 | ||
if fieldsStart >= fieldsEnd { | ||
continue | ||
} | ||
|
||
fields := strings.Fields(line[fieldsStart:fieldsEnd]) | ||
if len(fields) >= 2 && | ||
strings.ToLower(fields[0]) == "inet" && | ||
net.ParseIP(fields[1]) != nil { | ||
return true, s.Err() | ||
} | ||
} | ||
|
||
return false, s.Err() | ||
} | ||
|
||
func ifaceSetStaticIP(string) (err error) { | ||
return aghos.Unsupported("setting static ip") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//go:build freebsd | ||
// +build freebsd | ||
|
||
package aghnet | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRcConfStaticConfig(t *testing.T) { | ||
const ifaceName = `em0` | ||
const nl = "\n" | ||
|
||
testCases := []struct { | ||
name string | ||
rcconfData string | ||
wantHas bool | ||
}{{ | ||
name: "simple", | ||
rcconfData: `ifconfig_em0="inet 127.0.0.253 netmask 0xffffffff"` + nl, | ||
wantHas: true, | ||
}, { | ||
name: "case_insensitiveness", | ||
rcconfData: `ifconfig_em0="InEt 127.0.0.253 NeTmAsK 0xffffffff"` + nl, | ||
wantHas: true, | ||
}, { | ||
name: "comments_and_trash", | ||
rcconfData: `# comment 1` + nl + | ||
`` + nl + | ||
`# comment 2` + nl + | ||
`ifconfig_em0="inet 127.0.0.253 netmask 0xffffffff"` + nl, | ||
wantHas: true, | ||
}, { | ||
name: "aliases", | ||
rcconfData: `ifconfig_em0_alias="inet 127.0.0.1/24"` + nl + | ||
`ifconfig_em0="inet 127.0.0.253 netmask 0xffffffff"` + nl, | ||
wantHas: true, | ||
}, { | ||
name: "incorrect_config", | ||
rcconfData: `ifconfig_em0="inet6 127.0.0.253 netmask 0xffffffff"` + nl + | ||
`ifconfig_em0="inet 127.0.0.253 net-mask 0xffffffff"` + nl + | ||
`ifconfig_em0="inet 256.256.256.256 netmask 0xffffffff"` + nl + | ||
`ifconfig_em0=""` + nl, | ||
wantHas: false, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
r := strings.NewReader(tc.rcconfData) | ||
t.Run(tc.name, func(t *testing.T) { | ||
has, err := rcConfStaticConfig(r, ifaceName) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, tc.wantHas, has) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
//go:build !(linux || darwin) | ||
// +build !linux,!darwin | ||
//go:build !(linux || darwin || freebsd) | ||
// +build !linux,!darwin,!freebsd | ||
|
||
package aghnet | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghos" | ||
) | ||
|
||
func canBindPrivilegedPorts() (can bool, err error) { | ||
return aghos.HaveAdminRights() | ||
} | ||
|
||
func ifaceHasStaticIP(string) (bool, error) { | ||
return false, fmt.Errorf("cannot check if IP is static: not supported on %s", runtime.GOOS) | ||
func ifaceHasStaticIP(string) (ok bool, err error) { | ||
return false, aghos.Unsupported("checking static ip") | ||
} | ||
|
||
func ifaceSetStaticIP(string) error { | ||
return fmt.Errorf("cannot set static IP on %s", runtime.GOOS) | ||
func ifaceSetStaticIP(string) (err error) { | ||
return aghos.Unsupported("setting static ip") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters