Skip to content

Commit

Permalink
Pull request: dhcpd imp code
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from dhcpd-imp-code to master

Squashed commit of the following:

commit 413403c
Merge: eed1838 0fec990
Author: Dimitry Kolyshev <[email protected]>
Date:   Fri May 26 12:46:25 2023 +0300

    Merge remote-tracking branch 'origin/master' into dhcpd-imp-code

commit eed1838
Author: Dimitry Kolyshev <[email protected]>
Date:   Fri May 26 12:16:46 2023 +0300

    dhcpd: imp docs

commit fa4fe03
Author: Dimitry Kolyshev <[email protected]>
Date:   Thu May 25 11:32:34 2023 +0300

    all: lint script

commit a4022b3
Merge: e08ff3a cbc7985
Author: Dimitry Kolyshev <[email protected]>
Date:   Thu May 25 11:29:57 2023 +0300

    Merge remote-tracking branch 'origin/master' into dhcpd-imp-code

    # Conflicts:
    #	scripts/make/go-lint.sh

commit e08ff3a
Author: Dimitry Kolyshev <[email protected]>
Date:   Wed May 24 15:43:11 2023 +0300

    dhcpd: imp code

commit 970b538
Author: Dimitry Kolyshev <[email protected]>
Date:   Wed May 24 15:40:36 2023 +0300

    dhcpd: imp code

commit 0e5916d
Author: Dimitry Kolyshev <[email protected]>
Date:   Wed May 24 15:37:17 2023 +0300

    dhcpd: imp code

commit e06a6c6
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 16:40:09 2023 +0300

    dhcpd: imp code

commit eed4ff1
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 15:45:06 2023 +0300

    all: lint script

commit 87f84ac
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 15:44:23 2023 +0300

    dhcpd: imp code

commit a54c992
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 14:29:42 2023 +0300

    dhcpd: imp code

commit 1bbea34
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 14:12:09 2023 +0300

    dhcpd: imp code

commit 48fb4ef
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 13:57:59 2023 +0300

    dhcpd: imp code

commit f6cd7fc
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 13:17:54 2023 +0300

    dhcpd: imp code

commit 2b91dc2
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue May 23 12:57:46 2023 +0300

    dhcpd: imp code

commit 34f5dd5
Author: Dimitry Kolyshev <[email protected]>
Date:   Mon May 22 15:31:39 2023 +0300

    dhcpd: imp code

commit 12ef0d2
Merge: 6b62a76 24b4110
Author: Dimitry Kolyshev <[email protected]>
Date:   Mon May 22 13:03:41 2023 +0300

    Merge remote-tracking branch 'origin/master' into dhcpd-imp-code

commit 6b62a76
Author: Dimitry Kolyshev <[email protected]>
Date:   Mon May 22 12:55:43 2023 +0300

    dhcpd: imp code

commit 18c5cdf
Author: Dimitry Kolyshev <[email protected]>
Date:   Mon May 22 12:48:30 2023 +0300

    dhcpd: imp code

commit e7c1f43
Author: Dimitry Kolyshev <[email protected]>
Date:   Mon May 22 12:37:15 2023 +0300

    dhcpd: imp code

... and 1 more commit
  • Loading branch information
Mizzick committed May 26, 2023
1 parent 0fec990 commit ea8d634
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 283 deletions.
86 changes: 59 additions & 27 deletions internal/dhcpd/README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
# DHCP server
# Testing DHCP Server

Contents:
* [Test setup with Virtual Box](#vbox)
* [Test setup with Virtual Box](#vbox)
* [Quick test with DHCPTest](#dhcptest)

<a id="vbox"></a>
## Test setup with Virtual Box
## <a href="#vbox" id="vbox" name="vbox">Test setup with Virtual Box</a>

To set up a test environment for DHCP server you need:
### Prerequisites

* Linux host machine
* Virtual Box
* Virtual machine (guest OS doesn't matter)
To set up a test environment for DHCP server you will need:

### Configure client
* Linux AG Home host machine (Virtual).
* Virtual Box.
* Virtual machine (guest OS doesn't matter).

1. Install Virtual Box and run the following command to create a Host-Only network:
### Configure Virtual Box

$ VBoxManage hostonlyif create
1. Install Virtual Box and run the following command to create a Host-Only
network:

You can check its status by `ip a` command.
```sh
$ VBoxManage hostonlyif create
```

You can check its status by `ip a` command.

You can also set up Host-Only network using Virtual Box menu:
You can also set up Host-Only network using Virtual Box menu:

```
File -> Host Network Manager...
```

File -> Host Network Manager...
2. Create your virtual machine and set up its network:

2. Create your virtual machine and set up its network:
```
VM Settings -> Network -> Host-only Adapter
```

VM Settings -> Network -> Host-only Adapter
3. Start your VM, install an OS. Configure your network interface to use
DHCP and the OS should ask for a IP address from our DHCP server.

3. Start your VM, install an OS. Configure your network interface to use DHCP and the OS should ask for a IP address from our DHCP server.
4. To see the current IP addresses on client OS you can use `ip a` command on
Linux or `ipconfig` on Windows.

4. To see the current IP address on client OS you can use `ip a` command on Linux or `ipconfig` on Windows.
5. To force the client OS to request an IP from DHCP server again, you can
use `dhclient` on Linux or `ipconfig /release` on Windows.

5. To force the client OS to request an IP from DHCP server again, you can use `dhclient` on Linux or `ipconfig /release` on Windows.
### Configure server

### Configure server
1. Edit server configuration file `AdGuardHome.yaml`, for example:

1. Edit server configuration file 'AdGuardHome.yaml', for example:

dhcp:
```yaml
dhcp:
enabled: true
interface_name: vboxnet0
local_domain_name: lan
dhcpv4:
gateway_ip: 192.168.56.1
subnet_mask: 255.255.255.0
Expand All @@ -54,11 +68,29 @@ To set up a test environment for DHCP server you need:
lease_duration: 86400
ra_slaac_only: false
ra_allow_slaac: false
```

2. Start the server

```sh
./AdGuardHome -v
```

There should be a message in log which shows that DHCP server is ready:

```
[info] DHCP: listening on 0.0.0.0:67
```

## <a href="#dhcptest" id="dhcptest" name="dhcptest">Quick test with DHCPTest utility</a>

### Prerequisites

2. Start the server
* [DHCP test utility][dhcptest-gh].

./AdGuardHome
### Quick test

There should be a message in log which shows that DHCP server is ready:
The DHCP server could be tested for DISCOVER-OFFER packets with in
interactive mode.

[info] DHCP: listening on 0.0.0.0:67
[dhcptest-gh]: https://github.com/CyberShadow/dhcptest
61 changes: 37 additions & 24 deletions internal/dhcpd/dhcpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,36 +239,16 @@ func Create(conf *ServerConfig) (s *server, err error) {
// [aghhttp.RegisterFunc].
s.registerHandlers()

v4conf := conf.Conf4
v4conf.InterfaceName = s.conf.InterfaceName
v4conf.notify = s.onNotify
v4conf.Enabled = s.conf.Enabled && v4conf.RangeStart.IsValid()

s.srv4, err = v4Create(&v4conf)
v4Enabled, v6Enabled, err := s.setServers(conf)
if err != nil {
if v4conf.Enabled {
return nil, fmt.Errorf("creating dhcpv4 srv: %w", err)
}

log.Debug("dhcpd: warning: creating dhcpv4 srv: %s", err)
}

v6conf := conf.Conf6
v6conf.Enabled = s.conf.Enabled
if len(v6conf.RangeStart) == 0 {
v6conf.Enabled = false
}
v6conf.InterfaceName = s.conf.InterfaceName
v6conf.notify = s.onNotify
s.srv6, err = v6Create(v6conf)
if err != nil {
return nil, fmt.Errorf("creating dhcpv6 srv: %w", err)
// Don't wrap the error, because it's informative enough as is.
return nil, err
}

s.conf.Conf4 = conf.Conf4
s.conf.Conf6 = conf.Conf6

if s.conf.Enabled && !v4conf.Enabled && !v6conf.Enabled {
if s.conf.Enabled && !v4Enabled && !v6Enabled {
return nil, fmt.Errorf("neither dhcpv4 nor dhcpv6 srv is configured")
}

Expand All @@ -289,6 +269,39 @@ func Create(conf *ServerConfig) (s *server, err error) {
return s, nil
}

// setServers updates DHCPv4 and DHCPv6 servers created from the provided
// configuration conf.
func (s *server) setServers(conf *ServerConfig) (v4Enabled, v6Enabled bool, err error) {
v4conf := conf.Conf4
v4conf.InterfaceName = s.conf.InterfaceName
v4conf.notify = s.onNotify
v4conf.Enabled = s.conf.Enabled && v4conf.RangeStart.IsValid()

s.srv4, err = v4Create(&v4conf)
if err != nil {
if v4conf.Enabled {
return true, false, fmt.Errorf("creating dhcpv4 srv: %w", err)
}

log.Debug("dhcpd: warning: creating dhcpv4 srv: %s", err)
}

v6conf := conf.Conf6
v6conf.InterfaceName = s.conf.InterfaceName
v6conf.notify = s.onNotify
v6conf.Enabled = s.conf.Enabled
if len(v6conf.RangeStart) == 0 {
v6conf.Enabled = false
}

s.srv6, err = v6Create(v6conf)
if err != nil {
return v4conf.Enabled, v6conf.Enabled, fmt.Errorf("creating dhcpv6 srv: %w", err)
}

return v4conf.Enabled, v6conf.Enabled, nil
}

// Enabled returns true when the server is enabled.
func (s *server) Enabled() (ok bool) {
return s.conf.Enabled
Expand Down
Loading

0 comments on commit ea8d634

Please sign in to comment.