mirror of
https://github.com/coredhcp/coredhcp.git
synced 2025-08-11 08:37:21 +02:00
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>
191 lines
5.3 KiB
Go
191 lines
5.3 KiB
Go
package clientport
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/coredhcp/coredhcp/handler"
|
|
"github.com/coredhcp/coredhcp/logger"
|
|
"github.com/coredhcp/coredhcp/plugins"
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
)
|
|
|
|
var log = logger.GetLogger("plugins/file")
|
|
|
|
func init() {
|
|
plugins.RegisterPlugin("file", setupFile6, setupFile4)
|
|
}
|
|
|
|
// StaticRecords holds a MAC -> IP address mapping
|
|
var StaticRecords map[string]net.IP
|
|
|
|
// DHCPv6Records and DHCPv4Records are mappings between MAC addresses in
|
|
// form of a string, to network configurations.
|
|
var (
|
|
DHCPv6Records map[string]net.IP
|
|
DHCPv4Records map[string]net.IP
|
|
)
|
|
|
|
// LoadDHCPv4Records loads the DHCPv4Records global map with records stored on
|
|
// the specified file. The records have to be one per line, a mac address and an
|
|
// IPv4 address.
|
|
func LoadDHCPv4Records(filename string) (map[string]net.IP, error) {
|
|
log.Printf("reading leases from %s", filename)
|
|
data, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
records := make(map[string]net.IP)
|
|
for _, lineBytes := range bytes.Split(data, []byte{'\n'}) {
|
|
line := string(lineBytes)
|
|
if len(line) == 0 {
|
|
continue
|
|
}
|
|
tokens := strings.Fields(line)
|
|
if len(tokens) != 2 {
|
|
return nil, fmt.Errorf("malformed line, want 2 fields, got %d: %s", len(tokens), line)
|
|
}
|
|
hwaddr, err := net.ParseMAC(tokens[0])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("malformed hardware address: %s", tokens[0])
|
|
}
|
|
ipaddr := net.ParseIP(tokens[1])
|
|
if ipaddr.To4() == nil {
|
|
return nil, fmt.Errorf("expected an IPv4 address, got: %v", ipaddr)
|
|
}
|
|
records[hwaddr.String()] = ipaddr
|
|
}
|
|
|
|
return records, nil
|
|
}
|
|
|
|
// LoadDHCPv6Records loads the DHCPv6Records global map with records stored on
|
|
// the specified file. The records have to be one per line, a mac address and an
|
|
// IPv6 address.
|
|
func LoadDHCPv6Records(filename string) (map[string]net.IP, error) {
|
|
log.Printf("reading leases from %s", filename)
|
|
data, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
records := make(map[string]net.IP)
|
|
// TODO ignore comments
|
|
for _, lineBytes := range bytes.Split(data, []byte{'\n'}) {
|
|
line := string(lineBytes)
|
|
if len(line) == 0 {
|
|
continue
|
|
}
|
|
tokens := strings.Fields(line)
|
|
if len(tokens) != 2 {
|
|
return nil, fmt.Errorf("malformed line: %s", line)
|
|
}
|
|
hwaddr, err := net.ParseMAC(tokens[0])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("malformed hardware address: %s", tokens[0])
|
|
}
|
|
ipaddr := net.ParseIP(tokens[1])
|
|
if ipaddr.To16() == nil {
|
|
return nil, fmt.Errorf("expected an IPv6 address, got: %v", ipaddr)
|
|
}
|
|
records[hwaddr.String()] = ipaddr
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
// Handler6 handles DHCPv6 packets for the file plugin
|
|
func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
|
|
mac, err := dhcpv6.ExtractMAC(req)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
log.Printf("looking up an IP address for MAC %s", mac.String())
|
|
|
|
ipaddr, ok := StaticRecords[mac.String()]
|
|
if !ok {
|
|
log.Warningf("MAC address %s is unknown", mac.String())
|
|
return nil, false
|
|
}
|
|
log.Printf("found IP address %s for MAC %s", ipaddr, mac.String())
|
|
resp.AddOption(&dhcpv6.OptIANA{
|
|
// FIXME copy this field from the client, reject/drop if missing
|
|
IaId: [4]byte{0xaa, 0xbb, 0xcc, 0xdd},
|
|
Options: []dhcpv6.Option{
|
|
&dhcpv6.OptIAAddress{
|
|
IPv6Addr: ipaddr,
|
|
PreferredLifetime: 3600,
|
|
ValidLifetime: 3600,
|
|
},
|
|
},
|
|
})
|
|
resp.AddOption(&dhcpv6.OptDNSRecursiveNameServer{
|
|
NameServers: []net.IP{
|
|
// FIXME this must be read from the config file
|
|
net.ParseIP("2001:4860:4860::8888"),
|
|
net.ParseIP("2001:4860:4860::4444"),
|
|
},
|
|
})
|
|
if oro := req.GetOption(dhcpv6.OptionORO); len(oro) > 0 {
|
|
for _, code := range oro[0].(*dhcpv6.OptRequestedOption).RequestedOptions() {
|
|
if code == dhcpv6.OptionBootfileURL {
|
|
// bootfile URL is requested
|
|
// FIXME this field should come from the configuration, not
|
|
// being hardcoded
|
|
resp.AddOption(
|
|
&dhcpv6.OptBootFileURL{BootFileURL: []byte("http://[2001:db8::0:1]/nbp")},
|
|
)
|
|
}
|
|
}
|
|
}
|
|
return resp, true
|
|
}
|
|
|
|
// Handler4 handles DHCPv4 packets for the file plugin
|
|
func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
|
|
ipaddr, ok := StaticRecords[req.ClientHWAddr.String()]
|
|
if !ok {
|
|
log.Warningf("MAC address %s is unknown", req.ClientHWAddr.String())
|
|
return resp, false
|
|
}
|
|
resp.YourIPAddr = ipaddr
|
|
log.Printf("found IP address %s for MAC %s", ipaddr, req.ClientHWAddr.String())
|
|
return resp, true
|
|
}
|
|
|
|
func setupFile6(args ...string) (handler.Handler6, error) {
|
|
h6, _, err := setupFile(true, args...)
|
|
return h6, err
|
|
}
|
|
|
|
func setupFile4(args ...string) (handler.Handler4, error) {
|
|
_, h4, err := setupFile(false, args...)
|
|
return h4, err
|
|
}
|
|
|
|
func setupFile(v6 bool, args ...string) (handler.Handler6, handler.Handler4, error) {
|
|
var err error
|
|
var records map[string]net.IP
|
|
if len(args) < 1 {
|
|
return nil, nil, errors.New("need a file name")
|
|
}
|
|
filename := args[0]
|
|
if filename == "" {
|
|
return nil, nil, errors.New("got empty file name")
|
|
}
|
|
if v6 {
|
|
records, err = LoadDHCPv6Records(filename)
|
|
} else {
|
|
records, err = LoadDHCPv4Records(filename)
|
|
}
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to load DHCPv6 records: %v", err)
|
|
}
|
|
StaticRecords = records
|
|
log.Printf("loaded %d leases from %s", len(records), filename)
|
|
return Handler6, Handler4, nil
|
|
}
|