dhcp4

package module
v0.0.0-...-18c84d0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2022 License: Apache-2.0 Imports: 9 Imported by: 2

README

This package contains helpers to construct simple DHCPv4 clients.

There are currently no stability guarantees around this package. We can revisit this in early 2019, at which point router7 and gokrazy users should have complained in case their DHCP broke :).

Documentation

Overview

Package dhcp4 helps construct and interpret google/gopacket/layers.DHCPv4 messages, as well as reading from and writing to mdlayher/raw.Conns.

Example
package main

import (
	"log"
	"net"
	"syscall"
	"time"

	"github.com/google/gopacket/layers"
	"github.com/mdlayher/packet"
	"github.com/rtr7/dhcp4"
)

func main() {
	iface, err := net.InterfaceByName("uplink0")
	if err != nil {
		log.Fatal(err)
	}
	xidGen := dhcp4.XIDGenerator(iface.HardwareAddr)
	conn, err := packet.Listen(iface, packet.Datagram, syscall.ETH_P_IP, nil)
	if err != nil {
		log.Fatal(err)
	}
	discover := &layers.DHCPv4{
		Operation:    layers.DHCPOpRequest,
		HardwareType: layers.LinkTypeEthernet,
		HardwareLen:  uint8(len(layers.EthernetBroadcast)),
		Xid:          xidGen(),
		ClientHWAddr: iface.HardwareAddr,
		Options: []layers.DHCPOption{
			dhcp4.MessageTypeOpt(layers.DHCPMsgTypeDiscover),
			dhcp4.HostnameOpt("dhcpprobe"),
			dhcp4.ClientIDOpt(layers.LinkTypeEthernet, iface.HardwareAddr),
		},
	}
	if err := dhcp4.Write(conn, discover); err != nil {
		log.Fatal(err)
	}

	// Display all DHCPOFFER packets received within 5s:
	conn.SetDeadline(time.Now().Add(5 * time.Second))
	for {
		offer, err := dhcp4.Read(conn)
		if err != nil {
			log.Fatal(err)
		}
		if offer == nil {
			continue // not a DHCPv4 packet
		}
		if offer.Xid != discover.Xid {
			continue // broadcast reply for different DHCP transaction
		}
		if !dhcp4.HasMessageType(offer.Options, layers.DHCPMsgTypeOffer) {
			continue
		}
		log.Printf("DHCPOFFER: %+v", offer)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIDOpt

func ClientIDOpt(linkType layers.LinkType, hwaddr net.HardwareAddr) layers.DHCPOption

ClientIDOpt is a short-hand for constructing a ClientID DHCP option.

func HasMessageType

func HasMessageType(opts []layers.DHCPOption, mt layers.DHCPMsgType) bool

HasMessageType returns true if any of the specified DHCP options declares the DHCP message type to be mt.

func HostnameOpt

func HostnameOpt(hostname string) layers.DHCPOption

HostnameOpt is a short-hand for constructing a Hostname DHCP option.

func MessageTypeOpt

func MessageTypeOpt(msgType layers.DHCPMsgType) layers.DHCPOption

MessageTypeOpt is a short-hand for constructing a MessageType DHCP option.

func ParamsRequestOpt

func ParamsRequestOpt(opts ...layers.DHCPOpt) layers.DHCPOption

ParamsRequestOpt is a short-hand for constructing a ParamsRequest DHCP option.

func Read

func Read(pc net.PacketConn) (*layers.DHCPv4, error)

Read decodes an IPv4 packet from pc, returning its DHCPv4 layer or nil.

func RequestIPOpt

func RequestIPOpt(requestIP net.IP) layers.DHCPOption

RequestIPOpt is a short-hand for constructing a RequestIP DHCP option.

func ServerID

func ServerID(opts []layers.DHCPOption) []layers.DHCPOption

ServerID extracts all ServerID DHCP options. This is convenient when responding to a server.

func Write

func Write(pc net.PacketConn, pkt *layers.DHCPv4) error

Write encodes pkt as an IPv4 packet to pc.

func XIDGenerator

func XIDGenerator(hwaddr net.HardwareAddr) func() uint32

XIDGenerator returns a function which returns DHCP transaction IDs (xid).

Types

type Lease

type Lease struct {
	IP          net.IP
	Netmask     net.IPMask
	Broadcast   net.IP
	Router      net.IP
	DNS         []net.IP
	Domain      string
	RenewalTime time.Duration
}

Lease represents a DHCP lease.

func LeaseFromACK

func LeaseFromACK(ack *layers.DHCPv4) Lease

LeaseFromACK constructs a Lease from the specified DHCPACK packet.

type OptBroadcast

type OptBroadcast struct {
	Broadcast net.IP
}

OptBroadcast represents a Broadcast DHCP option.

func (*OptBroadcast) Type

func (o *OptBroadcast) Type() layers.DHCPOpt

Type implements Option.

type OptDNS

type OptDNS struct {
	DNS []net.IP
}

OptDNS represents a DNS DHCP option.

func (*OptDNS) Type

func (o *OptDNS) Type() layers.DHCPOpt

Type implements Option.

type OptDomainName

type OptDomainName struct {
	DomainName string
}

OptDomainName represents a DomainName DHCP option.

func (*OptDomainName) Type

func (o *OptDomainName) Type() layers.DHCPOpt

Type implements Option.

type OptLeaseTime

type OptLeaseTime struct {
	LeaseTime time.Duration
}

OptLeaseTime represents a LeaseTime DHCP option.

func (*OptLeaseTime) Type

func (o *OptLeaseTime) Type() layers.DHCPOpt

Type implements Option.

type OptRouter

type OptRouter struct {
	Router net.IP
}

OptRouter represents a Router DHCP option.

func (*OptRouter) Type

func (o *OptRouter) Type() layers.DHCPOpt

Type implements Option.

type OptSubnetMask

type OptSubnetMask struct {
	Mask net.IPMask
}

OptSubnetMask represents a SubnetMask DHCP option.

func (*OptSubnetMask) Type

func (o *OptSubnetMask) Type() layers.DHCPOpt

Type implements Option.

type OptT1

type OptT1 struct {
	T1 time.Duration
}

OptT1 represents a T1 DHCP option.

func (*OptT1) Type

func (o *OptT1) Type() layers.DHCPOpt

Type implements Option.

type Option

type Option interface {
	Type() layers.DHCPOpt
}

Option is an interface implemented by all Opt* DHCP option types.

func ParseOptions

func ParseOptions(opts []layers.DHCPOption) []Option

ParseOptions converts layers.DHCPOption type/byte-slice pairs into Option.

Jump to

Keyboard shortcuts

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