From 4b9d16f22abdba1bf7e4df0594104d65c763407e Mon Sep 17 00:00:00 2001 From: soypat Date: Thu, 14 Sep 2023 10:00:07 -0300 Subject: [PATCH] add some more form to DoDHCP --- cmd/cyrw/main.go | 53 ++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/cmd/cyrw/main.go b/cmd/cyrw/main.go index 887139b..ccda3bb 100644 --- a/cmd/cyrw/main.go +++ b/cmd/cyrw/main.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "errors" "time" + "unsafe" "github.com/soypat/cyw43439/cyrw" "github.com/soypat/cyw43439/internal/slog" @@ -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()) @@ -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. @@ -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 @@ -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] @@ -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 }