This is mainly done for usage in a test context, where it would be nice to ensure that only a single IP address can be used.
Signed-off-by: Wielewout <vandewiel.wout@gmail.com>
The range plugin appends every new entry to a text file, without
deduplication, and collisions are possible.
Implementing deduplication on a text file is either inefficient or
tricky. Detecting collisions is inefficient, no matter if it's done in-memory
and then rewriting the entire text file every time, or by parsing the text
file every time.
Sqlite3 offers consistency, uniqueness without performance hit, and can
enforce more complex constraints.
However using sqlite3 requires using CGo, which has a bunch of issues:
* it complicates cross-compiling
* builds are slower
* deployments are more complicated (no more single binary, not easily at least)
I have been running CoreDHCP with the sqlite3-based range plugin in my
home network without issues so far, and no more duplication nor
collisions.
Signed-off-by: Andrea Barberio <insomniac@slackware.it>
Mixed improvements to overhaul the plugin
* Use the allocator interface to allocate IPs
* Focus on correctness by wrapping a lock for now; we'll improve that
with some more fine-grained concurrency later
* move things related to file storage of leases to a separate source
file and add some minimal sanity-check tests
* Don't open and close the file descriptor on every single packet
Signed-off-by: Anatole Denis <anatole@unverle.fr>
If the leases.txt file was not present on the system, coredhcp would
fail to start --
```
level=info msg="DHCPv4: loading plugin `range`" prefix=plugins
level=warning msg="Failed to close file leases.txt: invalid argument" prefix="plugins/range"
level=fatal msg="cannot open lease file leases.txt: open leases.txt: no such file or directory" prefix=main
```
This changes the call to include the O_CREATE flag to create the
leases file if it does not already exist.
Signed-off-by: Brad Beam <brad.beam@b-rad.info>
Fixes#45
Do not use `init()` magic to register plugins, do it explicitly. This is
done by requiring plugins to declare a populated `Plugin` symbol of type
`plugins.Plugin`.
Signed-off-by: Andrea Barberio <insomniac@slackware.it>
This is useful to automatically provide context about what component of
the framework is emitting a log line.
Example output:
```
$ go build && sudo ./coredhcp
[2019-08-25T21:39:26+01:00] INFO plugins: Registering plugin "dns"
[2019-08-25T21:39:26+01:00] INFO plugins: Registering plugin "file"
[2019-08-25T21:39:26+01:00] INFO plugins: Registering plugin "netmask"
[2019-08-25T21:39:26+01:00] INFO plugins: Registering plugin "range"
[2019-08-25T21:39:26+01:00] INFO plugins: Registering plugin "router"
[2019-08-25T21:39:26+01:00] INFO plugins: Registering plugin "server_id"
[2019-08-25T21:39:26+01:00] INFO config: Loading configuration
[2019-08-25T21:39:26+01:00] INFO config: DHCPv4: found plugin `server_id` with 1 args: [10.10.10.1]
[2019-08-25T21:39:26+01:00] INFO config: DHCPv4: found plugin `dns` with 2 args: [8.8.8.8 8.8.4.4]
[2019-08-25T21:39:26+01:00] INFO config: DHCPv4: found plugin `router` with 1 args: [10.10.10.1]
[2019-08-25T21:39:26+01:00] INFO config: DHCPv4: found plugin `netmask` with 1 args: [255.255.255.0]
[2019-08-25T21:39:26+01:00] INFO config: DHCPv4: found plugin `range` with 4 args: [leases.txt 10.10.10.100 10.10.10.200 60s]
[2019-08-25T21:39:26+01:00] INFO coredhcp: Loading plugins...
[2019-08-25T21:39:26+01:00] INFO coredhcp: DHCPv4: loading plugin `server_id`
[2019-08-25T21:39:26+01:00] INFO plugins/server_id: plugins/server_id: loading `server_id` plugin for DHCPv4
[2019-08-25T21:39:26+01:00] INFO coredhcp: DHCPv4: loading plugin `dns`
[2019-08-25T21:39:26+01:00] INFO plugins/dns: loaded plugin for DHCPv4.
[2019-08-25T21:39:26+01:00] INFO plugins/dns: loaded 2 DNS servers.
[2019-08-25T21:39:26+01:00] INFO coredhcp: DHCPv4: loading plugin `router`
[2019-08-25T21:39:26+01:00] INFO plugins/router: plugins/router: loaded plugin for DHCPv4.
[2019-08-25T21:39:26+01:00] INFO plugins/router: plugins/router: loaded 1 router IP addresses.
[2019-08-25T21:39:26+01:00] INFO coredhcp: DHCPv4: loading plugin `netmask`
[2019-08-25T21:39:26+01:00] INFO plugins/netmask: plugins/netmask: loaded plugin for DHCPv4.
[2019-08-25T21:39:26+01:00] INFO plugins/netmask: plugins/netmask: loaded client netmask
[2019-08-25T21:39:26+01:00] INFO coredhcp: DHCPv4: loading plugin `range`
[2019-08-25T21:39:26+01:00] INFO plugins/range: plugins/range: reading leases from leases.txt
[2019-08-25T21:39:26+01:00] INFO plugins/range: plugins/range: loaded 1 leases from leases.txt
[2019-08-25T21:39:26+01:00] INFO coredhcp: Starting DHCPv4 listener on 0.0.0.0:67
[2019-08-25T21:39:26+01:00] INFO coredhcp: Waiting
```
Signed-off-by: Andrea Barberio <insomniac@slackware.it>