pull/3/head
Jack O'Sullivan 3 years ago
parent ef4b51b2f1
commit fbd52ee886

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"net"
"os"
log "github.com/sirupsen/logrus"
@ -22,9 +23,13 @@ func main() {
switch event.Type {
case "bound", "renew":
if v6, ok := os.LookupEnv("ipv6"); ok {
event.Data.IP = v6
// Clean up the IP (udhcpc6 emits a _lot_ of zeros)
_, netV6, err := net.ParseCIDR(v6 + "/128")
if err != nil {
log.WithError(err).Warn("Failed to parse IPv6 address")
}
// TODO: Sort out router and domain options for IPv6
event.Data.IP = netV6.String()
} else {
event.Data.IP = os.Getenv("ip") + "/" + os.Getenv("mask")
event.Data.Gateway = os.Getenv("router")

@ -204,15 +204,13 @@ func (p *Plugin) CreateEndpoint(ctx context.Context, r CreateEndpointRequest) (C
return fmt.Errorf("failed to get initial IP%v address via DHCP%v: %w", v6str, v6str, err)
}
hint := p.gatewayHints[r.EndpointID]
if v6 {
res.Interface.AddressIPv6 = info.IP
hint.V6 = info.Gateway
// No gateways in DHCPv6!
} else {
res.Interface.Address = info.IP
hint.V4 = info.Gateway
p.gatewayHints[r.EndpointID] = info.Gateway
}
p.gatewayHints[r.EndpointID] = hint
return nil
}
@ -290,14 +288,12 @@ func (p *Plugin) Join(ctx context.Context, r JoinRequest) (JoinResponse, error)
if hint, ok := p.gatewayHints[r.EndpointID]; ok {
log.WithFields(log.Fields{
"network": r.NetworkID[:12],
"endpoint": r.EndpointID[:12],
"sandbox": r.SandboxKey,
"gateway_v4": hint.V4,
"gateway_v6": hint.V6,
}).Info("[Join] Setting gateway(s) retrieved from CreateEndpoint")
res.Gateway = hint.V4
res.GatewayIPv6 = hint.V6
"network": r.NetworkID[:12],
"endpoint": r.EndpointID[:12],
"sandbox": r.SandboxKey,
"gateway": hint,
}).Info("[Join] Setting IPv4 gateway retrieved from CreateEndpoint")
res.Gateway = hint
delete(p.gatewayHints, r.EndpointID)
}

@ -28,8 +28,9 @@ type DHCPNetworkOptions struct {
func decodeOpts(input interface{}) (DHCPNetworkOptions, error) {
var opts DHCPNetworkOptions
optsDecoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &opts,
ErrorUnused: true,
Result: &opts,
ErrorUnused: true,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
),
@ -45,17 +46,12 @@ func decodeOpts(input interface{}) (DHCPNetworkOptions, error) {
return opts, nil
}
type gatewayHint struct {
V4 string
V6 string
}
// Plugin is the DHCP network plugin
type Plugin struct {
docker *docker.Client
server http.Server
gatewayHints map[string]gatewayHint
gatewayHints map[string]string
}
// NewPlugin creates a new Plugin
@ -68,7 +64,7 @@ func NewPlugin() (*Plugin, error) {
p := Plugin{
docker: client,
gatewayHints: make(map[string]gatewayHint),
gatewayHints: make(map[string]string),
}
mux := http.NewServeMux()

@ -2,7 +2,10 @@ package udhcpc
import (
"bufio"
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"
@ -74,8 +77,20 @@ func NewDHCPClient(iface string, opts *DHCPClientOptions) (*DHCPClient, error) {
}
if opts.Hostname != "" {
// TODO: This used to be broken for udhcpc6, is that still the case?
c.cmd.Args = append(c.cmd.Args, "-x", "hostname:"+opts.Hostname)
hostnameOpt := "hostname:" + opts.Hostname
if opts.V6 {
// TODO: We encode the fqdn for DHCPv6 because udhcpc6 seems to be broken
var data bytes.Buffer
// flags: S bit set (see RFC4704)
binary.Write(&data, binary.BigEndian, uint8(0b0001))
binary.Write(&data, binary.BigEndian, uint8(len(opts.Hostname)))
data.WriteString(opts.Hostname)
hostnameOpt = "0x27:" + hex.EncodeToString(data.Bytes())
}
c.cmd.Args = append(c.cmd.Args, "-x", hostnameOpt)
}
// Vendor ID string option is not available for udhcpc6

@ -2,6 +2,8 @@
BRIDGE=net-dhcp
BRIDGE_IP="10.123.0.1/24"
DHCP_RANGE="10.123.0.5,10.123.0.254"
BRIDGE_IP6="fd69::1/64"
DHCP6_RANGE="fd69::5,fd69::1000,64"
DOMAIN=cool-dhcp
quit() {
@ -14,10 +16,12 @@ trap quit SIGINT SIGTERM
ip link add "$BRIDGE" type bridge
ip link set up dev "$BRIDGE"
ip addr add "$BRIDGE_IP" dev "$BRIDGE"
ip addr add "$BRIDGE_IP6" dev "$BRIDGE"
dnsmasq --no-daemon --conf-file=/dev/null \
--port=0 --interface="$BRIDGE" --bind-interfaces \
--domain="$DOMAIN" \
--dhcp-range="$DHCP_RANGE" --dhcp-leasefile=/dev/null
--dhcp-range="$DHCP_RANGE" --dhcp-leasefile=/dev/null \
--dhcp-range="$DHCP6_RANGE" --enable-ra
quit

Loading…
Cancel
Save