feat: Working impl with offer / ack
This commit is contained in:
213
main.go
213
main.go
@@ -4,35 +4,13 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type DhcpOp uint8
|
||||
|
||||
const (
|
||||
BOOTREQUEST DhcpOp = iota + 1
|
||||
BOOTREPLY
|
||||
)
|
||||
|
||||
type DhcpMessage struct {
|
||||
Op DhcpOp
|
||||
Htype uint8
|
||||
Hlen uint8
|
||||
Hops uint8
|
||||
Xid uint32
|
||||
Secs uint16
|
||||
Flags uint16
|
||||
Ciaddr uint32
|
||||
Yiaddr uint32
|
||||
Siaddr uint32
|
||||
Giaddr uint32
|
||||
Chaddr [16]byte
|
||||
Sname [64]byte
|
||||
File [128]byte
|
||||
Magic [4]byte
|
||||
}
|
||||
var leases []Lease = make([]Lease, 0)
|
||||
|
||||
func (dhcp DhcpMessage) Debug(s *zap.SugaredLogger) {
|
||||
s.Debugf("op=%x, htype=%x, hlen=%x, hops=%x", dhcp.Op, dhcp.Htype, dhcp.Hlen, dhcp.Hops)
|
||||
@@ -49,17 +27,21 @@ func (dhcp DhcpMessage) Debug(s *zap.SugaredLogger) {
|
||||
s.Debugf("magic=%x", dhcp.Magic)
|
||||
}
|
||||
|
||||
func parseOptions(data []byte) {
|
||||
func parseOptions(data []byte) Options {
|
||||
s := zap.S()
|
||||
i := 0
|
||||
for i < len(data) {
|
||||
code := data[i]
|
||||
if code == 255 {
|
||||
s.Debug("Found END option at offset ", i)
|
||||
break
|
||||
}
|
||||
|
||||
if code == 0 {
|
||||
options := make(Options)
|
||||
|
||||
out:
|
||||
for i < len(data) {
|
||||
code := OptionCode(data[i])
|
||||
switch code {
|
||||
case OptionEnd:
|
||||
s.Debug("Found END option at offset ", i)
|
||||
break out
|
||||
|
||||
case OptionPad:
|
||||
s.Debug("Found padding option at offset ", i)
|
||||
i += 1
|
||||
continue
|
||||
@@ -69,8 +51,12 @@ func parseOptions(data []byte) {
|
||||
payload := data[i+2 : i+2+size]
|
||||
|
||||
s.Debugf("code=%d, size=%d, payload=%x", code, size, payload)
|
||||
options[code] = Option{Code: code, Data: data[i+1 : i+2+size]}
|
||||
i += size + 2
|
||||
}
|
||||
|
||||
s.Debugf("Parsed options: ", options)
|
||||
return options
|
||||
}
|
||||
|
||||
func prepareOffer(request DhcpMessage) DhcpMessage {
|
||||
@@ -98,6 +84,133 @@ func prepareOffer(request DhcpMessage) DhcpMessage {
|
||||
return dhcp
|
||||
}
|
||||
|
||||
func prepareAck(request DhcpMessage) DhcpMessage {
|
||||
var sname [64]byte
|
||||
var file [128]byte
|
||||
copy(sname[:], "go-dhcp-server")
|
||||
|
||||
dhcp := DhcpMessage{
|
||||
Op: BOOTREPLY,
|
||||
Htype: 1, // Ethernet
|
||||
Hlen: 6, // Ethernet address length
|
||||
Hops: 0,
|
||||
Secs: 0,
|
||||
Flags: 0,
|
||||
Xid: request.Xid,
|
||||
Siaddr: binary.BigEndian.Uint32(net.IPv4(10, 0, 0, 1).To4()),
|
||||
Ciaddr: request.Ciaddr,
|
||||
Yiaddr: binary.BigEndian.Uint32(net.IPv4(10, 0, 0, 2).To4()),
|
||||
Giaddr: request.Giaddr,
|
||||
Chaddr: request.Chaddr,
|
||||
Magic: [4]byte{0x63, 0x82, 0x53, 0x63},
|
||||
Sname: sname,
|
||||
File: file,
|
||||
}
|
||||
return dhcp
|
||||
}
|
||||
|
||||
// sendMessage transmits a DHCP message with options via a UDP connection
|
||||
// The end option (code 255) is automatically appended and does not need to
|
||||
// be passed explicitly.
|
||||
func sendMessage(conn *net.UDPConn, message DhcpMessage, options []Option) error {
|
||||
buf := make([]byte, 0)
|
||||
w := bytes.NewBuffer(buf)
|
||||
err := binary.Write(w, binary.BigEndian, message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, v := range options {
|
||||
err = w.WriteByte(byte(v.Code))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(v.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Automatically add END option, so the caller doesn't
|
||||
// need to specificy it for every invocation.
|
||||
err = w.WriteByte(byte(OptionEnd))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := w.Bytes()
|
||||
zap.S().Debug("Msg", msg)
|
||||
_, err = conn.Write(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleOffer(dhcp DhcpMessage, remote *net.UDPAddr) error {
|
||||
s := zap.S()
|
||||
|
||||
offer := prepareOffer(dhcp)
|
||||
localAddr, _ := net.ResolveUDPAddr("udp", "172.17.0.1:68")
|
||||
clientAddr, _ := net.ResolveUDPAddr("udp", "255.255.255.255:68")
|
||||
conn, err := net.DialUDP("udp", localAddr, clientAddr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
s.Info("Sending DHCPOFFER...")
|
||||
options := []Option{
|
||||
{
|
||||
Code: OptionDHCPMessageType,
|
||||
Data: []byte{1, byte(MessageTypeOffer)},
|
||||
},
|
||||
}
|
||||
sendMessage(conn, offer, options)
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleAck(dhcp DhcpMessage, remote *net.UDPAddr) error {
|
||||
s := zap.S()
|
||||
|
||||
ack := prepareAck(dhcp)
|
||||
|
||||
options := []Option{
|
||||
{
|
||||
Code: OptionDHCPMessageType,
|
||||
Data: []byte{1, byte(MessageTypeAck)},
|
||||
},
|
||||
{
|
||||
Code: OptionIPAddressLeaseTime,
|
||||
Data: append([]byte{4}, u32tob(3600)...),
|
||||
},
|
||||
}
|
||||
s.Debug("Options: ", options)
|
||||
|
||||
localAddr, _ := net.ResolveUDPAddr("udp", "172.17.0.1:68")
|
||||
clientAddr, _ := net.ResolveUDPAddr("udp", "255.255.255.255:68")
|
||||
conn, err := net.DialUDP("udp", localAddr, clientAddr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
s.Info("Sending DHCPACK")
|
||||
sendMessage(conn, ack, options)
|
||||
|
||||
lease := Lease{
|
||||
TransactionId: ack.Xid,
|
||||
TTL: time.Now().Add(1 * time.Hour),
|
||||
State: Requested,
|
||||
ClientAddr: ack.Chaddr,
|
||||
Address: int2ip(ack.Yiaddr),
|
||||
}
|
||||
leases = append(leases, lease)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleMsg(data []byte, remote *net.UDPAddr) {
|
||||
s := zap.S()
|
||||
|
||||
@@ -116,26 +229,17 @@ func handleMsg(data []byte, remote *net.UDPAddr) {
|
||||
optData := data[optDataOffset:]
|
||||
s.Debug("Raw options data:", optData)
|
||||
|
||||
parseOptions(optData)
|
||||
options := parseOptions(optData)
|
||||
dhcpMsgType := options[OptionDHCPMessageType]
|
||||
s.Info("DHCP message type ", dhcpMsgType)
|
||||
|
||||
offer := prepareOffer(dhcp)
|
||||
localAddr, _ := net.ResolveUDPAddr("udp", "172.17.0.1:68")
|
||||
clientAddr, _ := net.ResolveUDPAddr("udp", "255.255.255.255:68")
|
||||
conn, err := net.DialUDP("udp", localAddr, clientAddr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
switch MessageType(dhcpMsgType.Data[1]) {
|
||||
case MessageTypeDiscover:
|
||||
go handleOffer(dhcp, remote)
|
||||
|
||||
case MessageTypeRequest:
|
||||
go handleAck(dhcp, remote)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
s.Info("Sending DHCPOFFER...")
|
||||
buf := make([]byte, 0)
|
||||
w := bytes.NewBuffer(buf)
|
||||
binary.Write(w, binary.BigEndian, offer)
|
||||
w.Write([]byte{53, 1, 2})
|
||||
w.WriteByte(255)
|
||||
msg := w.Bytes()
|
||||
s.Debug("Msg", msg)
|
||||
conn.Write(msg)
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -155,12 +259,17 @@ func main() {
|
||||
|
||||
for {
|
||||
buf := make([]byte, 1024)
|
||||
rlen, remote, err := conn.ReadFromUDP(buf[:])
|
||||
rlen, remote, err := conn.ReadFrom(buf[:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Do stuff with the read bytes
|
||||
handleMsg(buf[0:rlen], remote)
|
||||
remoteAddr, ok := remote.(*net.UDPAddr)
|
||||
if !ok {
|
||||
zap.S().Warn("Not a valid remote IP address: ", remote)
|
||||
continue
|
||||
}
|
||||
go handleMsg(buf[0:rlen], remoteAddr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user