dhcp

package
v0.0.0-...-2208570 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2021 License: BSD-2-Clause Imports: 22 Imported by: 0

README

Testing against a local DHCP server

These instructions will run a DHCP server locally on your computer for testing the DHCP client.

Run qemu with Fuchsia. We'll give Fuchsia two NICs so that we can test the interaction between them and DHCP. Make all the tunnels:

sudo tunctl -u $USER -t qemu
sudo tunctl -u $USER -t qemu-extra
sudo ifconfig qemu up
sudo ifconfig qemu-extra up

Now build and run Fuchsia with those two ports:

fx build && fx qemu -kN -- -nic tap,ifname=qemu-extra,model=e1000

Now install a DHCP server.

sudo apt-get install isc-dhcp-server
ps aux | grep dhcp

If dhcpd was started as part of that, kill it:

sudo systemctl disable isc-dhcp-server.service

Make a file called /tmp/dhcpd.conf with these contents:

default-lease-time 3600;
max-lease-time 7200;
authoritative;
subnet 172.18.0.0 netmask 255.255.0.0 {
  option routers                  172.18.0.1;
  option subnet-mask              255.255.0.0;
  option domain-search            "testdomain18.lan";
  option domain-name-servers      172.18.0.1;
  range   172.18.0.10   172.18.0.100;
}
subnet 172.19.0.0 netmask 255.255.0.0 {
  option routers                  172.19.0.1;
  option subnet-mask              255.255.0.0;
  option domain-search            "testdomain19.lan";
  option domain-name-servers      172.19.0.1;
  range   172.19.0.10   172.19.0.100;
}

dhcpd knows which addresses to serve on which nics based on matching the existing IP address on the NIC so you need to set those:

sudo ifconfig qemu 172.18.0.1
sudo ifconfig qemu-extra 172.19.0.1

Now we'll run dhcpd. You can use the filenames below or modify them.

If the leases file doesn't already exist, dhcpd might complain. Go ahead and create the file first:

sudo touch /tmp/dhcpd.leases

Now run:

sudo dhcpd -4 -f -d -cf /tmp/dhcpd.conf -lf /tmp/dhcpd.leases qemu qemu-extra

Now Fuchsia should get DHCP addresses and you'll see debug output for each step in the dhcpd output.

Documentation

Overview

Package dhcp implements a DHCP client and server as described in RFC 2131.

Index

Constants

View Source
const (
	// ServerPort is the well-known UDP port number for a DHCP server.
	ServerPort = 67
	// ClientPort is the well-known UDP port number for a DHCP client.
	ClientPort = 68
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AcquiredFunc

type AcquiredFunc func(oldAddr, newAddr tcpip.AddressWithPrefix, cfg Config)

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a DHCP client.

func NewClient

func NewClient(
	s *stack.Stack,
	nicid tcpip.NICID,
	linkAddr tcpip.LinkAddress,
	acquisition,
	backoff,
	retransmission time.Duration,
	acquiredFunc AcquiredFunc,
) *Client

NewClient creates a DHCP client.

acquiredFunc will be called after each DHCP acquisition, and is responsible for making necessary modifications to the stack state.

func (*Client) Info

func (c *Client) Info() Info

Info returns a copy of the synchronized state of the Info.

func (*Client) Run

func (c *Client) Run(ctx context.Context)

Run runs the DHCP client.

The function periodically searches for a new IP address.

func (*Client) Stats

func (c *Client) Stats() *Stats

Stats returns a reference to the Client`s stats.

type Config

type Config struct {
	ServerAddress tcpip.Address     // address of the server
	SubnetMask    tcpip.AddressMask // client address subnet mask
	Router        []tcpip.Address   // client router addresses
	DNS           []tcpip.Address   // client DNS server addresses
	LeaseLength   Seconds           // length of the address lease
	RenewTime     Seconds           // time until client enters RENEWING state
	RebindTime    Seconds           // time until client enters REBINDING state
	Declined      bool              // server sent a NAK
}

Config is standard DHCP configuration.

type Info

type Info struct {
	// NICID is the identifer to the associated NIC.
	NICID tcpip.NICID
	// LinkAddr is the link-address of the associated NIC.
	LinkAddr tcpip.LinkAddress
	// Acquisition is the duration within which a complete DHCP transaction must
	// complete before timing out.
	Acquisition time.Duration
	// Backoff is the duration for which the client must wait before starting a
	// new DHCP transaction after a failed transaction.
	Backoff time.Duration
	// Retransmission is the duration to wait before resending a DISCOVER or
	// REQUEST within an active transaction.
	Retransmission time.Duration
	// Addr is the acquired network address.
	Addr tcpip.AddressWithPrefix
	// Server is the network address of the DHCP server.
	Server tcpip.Address
	// State is the DHCP client state.
	State dhcpClientState
	// OldAddr is the address reported in the last call to acquiredFunc.
	OldAddr tcpip.AddressWithPrefix
}

type Seconds

type Seconds uint32

Seconds represents a time duration in seconds.

RFC 2131 Section 3.3 https://tools.ietf.org/html/rfc2131#section-3.3

Throughout the protocol, times are to be represented in units of seconds.

Representing relative times in units of seconds in an unsigned 32 bit
word gives a range of relative times from 0 to approximately 100 years,
which is sufficient for the relative times to be measured using DHCP.

func (Seconds) Duration

func (s Seconds) Duration() time.Duration

func (Seconds) String

func (s Seconds) String() string

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a DHCP server.

func NewServer

func NewServer(ctx context.Context, c conn, addrs []tcpip.Address, cfg Config) (*Server, error)

NewServer creates a new DHCP server and begins serving. The server continues serving until ctx is done.

type Stats

type Stats struct {
	InitAcquire                 tcpip.StatCounter
	RenewAcquire                tcpip.StatCounter
	RebindAcquire               tcpip.StatCounter
	SendDiscovers               tcpip.StatCounter
	RecvOffers                  tcpip.StatCounter
	SendRequests                tcpip.StatCounter
	RecvAcks                    tcpip.StatCounter
	RecvNaks                    tcpip.StatCounter
	SendDiscoverErrors          tcpip.StatCounter
	SendRequestErrors           tcpip.StatCounter
	RecvOfferErrors             tcpip.StatCounter
	RecvOfferUnexpectedType     tcpip.StatCounter
	RecvOfferOptsDecodeErrors   tcpip.StatCounter
	RecvOfferTimeout            tcpip.StatCounter
	RecvOfferAcquisitionTimeout tcpip.StatCounter
	RecvAckErrors               tcpip.StatCounter
	RecvNakErrors               tcpip.StatCounter
	RecvAckOptsDecodeErrors     tcpip.StatCounter
	RecvAckAddrErrors           tcpip.StatCounter
	RecvAckUnexpectedType       tcpip.StatCounter
	RecvAckTimeout              tcpip.StatCounter
	RecvAckAcquisitionTimeout   tcpip.StatCounter
	ReacquireAfterNAK           tcpip.StatCounter
}

Stats collects DHCP statistics per client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL