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/raw"
"github.com/rtr7/dhcp4"
)
func main() {
iface, err := net.InterfaceByName("uplink0")
if err != nil {
log.Fatal(err)
}
xidGen := dhcp4.XIDGenerator(iface.HardwareAddr)
conn, err := raw.ListenPacket(iface, syscall.ETH_P_IP, &raw.Config{LinuxSockDGRAM: true})
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)
}
}
Index ¶
- func ClientIDOpt(linkType layers.LinkType, hwaddr net.HardwareAddr) layers.DHCPOption
- func HasMessageType(opts []layers.DHCPOption, mt layers.DHCPMsgType) bool
- func HostnameOpt(hostname string) layers.DHCPOption
- func MessageTypeOpt(msgType layers.DHCPMsgType) layers.DHCPOption
- func ParamsRequestOpt(opts ...layers.DHCPOpt) layers.DHCPOption
- func Read(pc net.PacketConn) (*layers.DHCPv4, error)
- func RequestIPOpt(requestIP net.IP) layers.DHCPOption
- func ServerID(opts []layers.DHCPOption) []layers.DHCPOption
- func Write(pc net.PacketConn, pkt *layers.DHCPv4) error
- func XIDGenerator(hwaddr net.HardwareAddr) func() uint32
- type Lease
- type OptBroadcast
- type OptDNS
- type OptDomainName
- type OptLeaseTime
- type OptRouter
- type OptSubnetMask
- type OptT1
- type Option
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 ¶
LeaseFromACK constructs a Lease from the specified DHCPACK packet.
type OptBroadcast ¶
OptBroadcast represents a Broadcast DHCP option.
type OptDomainName ¶
type OptDomainName struct {
DomainName string
}
OptDomainName represents a DomainName DHCP option.
type OptLeaseTime ¶
OptLeaseTime represents a LeaseTime DHCP option.
type OptSubnetMask ¶
OptSubnetMask represents a SubnetMask DHCP option.
type Option ¶
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.