Skip to content

Commit

Permalink
add some more form to DoDHCP
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Sep 14, 2023
1 parent f298641 commit 4b9d16f
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions cmd/cyrw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"time"
"unsafe"

"github.com/soypat/cyw43439/cyrw"
"github.com/soypat/cyw43439/internal/slog"
Expand Down Expand Up @@ -94,6 +95,7 @@ func DoDHCP(s *tcpctl.Stack, dev *cyrw.Device) error {
var ehdr eth.EthernetHeader
var ihdr eth.IPv4Header
var uhdr eth.UDPHeader
var dhdr DHCPHeader

copy(ehdr.Destination[:], eth.BroadcastHW())
copy(ehdr.Source[:], dev.MAC())
Expand All @@ -112,9 +114,18 @@ func DoDHCP(s *tcpctl.Stack, dev *cyrw.Device) error {
ehdr.Put(txbuf[:])
ihdr.Put(txbuf[eth.SizeEthernetHeader:])
uhdr.Put(txbuf[eth.SizeEthernetHeader+4*ihdr.IHL():])
dhcppayload := txbuf[eth.SizeEthernetHeader+4*ihdr.IHL()+eth.SizeUDPHeader:]

dev.SendEth(txbuf[:])
dhcppayload := txbuf[eth.SizeEthernetHeader+4*ihdr.IHL()+eth.SizeUDPHeader:]
dhdr.OP = 1
dhdr.HType = 1
dhdr.HLen = 6
dhdr.Xid = 0x12345678
// What do with addrs?

// Send DHCP Discover.
dhdr.Put(dhcppayload[:])
totalSize := eth.SizeEthernetHeader + 4*ihdr.IHL() + eth.SizeUDPHeader + 44
dev.SendEth(txbuf[:totalSize])
for retry := 0; retry < 20 && state == none; retry++ {
time.Sleep(50 * time.Millisecond)
// We should see received packets received on callback passed into OpenUDP.
Expand All @@ -125,26 +136,28 @@ func DoDHCP(s *tcpctl.Stack, dev *cyrw.Device) error {
return nil
}

// DHCPHeader specifies the first 44 bytes of a DHCP packet payload
// not including BOOTP, magic cookie and options.
// DHCPHeader specifies the first 34 bytes of a DHCP packet payload. It does
// not include BOOTP, magic cookie and options.
type DHCPHeader struct {
OP byte // 0:1
HType byte // 1:2
HLen byte // 2:3
HOps byte // 3:4
Xid uint32 // 4:8
Secs uint16 // 8:10
Flags uint16 // 10:12
CIAddr [4]byte // 12:16
YIAddr [4]byte // 16:20
SIAddr [4]byte // 20:24
GIAddr [4]byte // 24:28
CHAddr [4 * 4]byte // 28:44
OP byte // 0:1
HType byte // 1:2
HLen byte // 2:3
HOps byte // 3:4
Xid uint32 // 4:8
Secs uint16 // 8:10
Flags uint16 // 10:12
CIAddr [4]byte // 12:16
YIAddr [4]byte // 16:20
SIAddr [4]byte // 20:24
GIAddr [4]byte // 24:28
CHAddr [6]byte // 28:34
// BOOTP, Magic Cookie, and DHCP Options not included.
}

const a = unsafe.Sizeof(DHCPHeader{})

func (d *DHCPHeader) Put(dst []byte) {
_ = dst[43]
_ = dst[33]
dst[0] = d.OP
dst[1] = d.HType
dst[2] = d.HLen
Expand All @@ -156,11 +169,11 @@ func (d *DHCPHeader) Put(dst []byte) {
copy(dst[16:20], d.YIAddr[:])
copy(dst[20:24], d.SIAddr[:])
copy(dst[24:28], d.GIAddr[:])
copy(dst[28:44], d.CHAddr[:])
copy(dst[28:34], d.CHAddr[:])
}

func DecodeDHCPHeader(src []byte) (d DHCPHeader) {
_ = src[43]
_ = src[33]
d.OP = src[0]
d.HType = src[1]
d.HLen = src[2]
Expand All @@ -172,6 +185,6 @@ func DecodeDHCPHeader(src []byte) (d DHCPHeader) {
copy(d.YIAddr[:], src[16:20])
copy(d.SIAddr[:], src[20:24])
copy(d.GIAddr[:], src[24:28])
copy(d.CHAddr[:], src[28:44])
copy(d.CHAddr[:], src[28:34])
return d
}

0 comments on commit 4b9d16f

Please sign in to comment.