standardise rng for dhcp example

This commit is contained in:
soypat
2025-07-01 00:29:11 -03:00
parent 6884d5e859
commit 5cea9727f0
2 changed files with 12 additions and 6 deletions
+1
View File
@@ -40,6 +40,7 @@ type StackNode interface {
// A change in the ID means the node is no longer valid and should be discarded. // A change in the ID means the node is no longer valid and should be discarded.
// A change in the ID could mean the connection was closed by the user or that the node will not send nor receive any more data over said connection ID. // A change in the ID could mean the connection was closed by the user or that the node will not send nor receive any more data over said connection ID.
ConnectionID() *uint64 ConnectionID() *uint64
}
``` ```
## Install ## Install
+11 -6
View File
@@ -9,6 +9,7 @@ import (
"net" "net"
"net/netip" "net/netip"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
@@ -23,6 +24,8 @@ import (
"github.com/soypat/lneto/internet/pcap" "github.com/soypat/lneto/internet/pcap"
) )
var softRand = time.Now().Unix()
func main() { func main() {
err := run() err := run()
if err != nil { if err != nil {
@@ -37,11 +40,14 @@ func run() (err error) {
flagInterface = "tap0" flagInterface = "tap0"
flagUseHTTP = false flagUseHTTP = false
flagHostToResolve = "" flagHostToResolve = ""
flagRequestedIP = ""
) )
flag.StringVar(&flagInterface, "i", flagInterface, "Interface to use. Either tap* or the name of an existing interface to bridge to.") flag.StringVar(&flagInterface, "i", flagInterface, "Interface to use. Either tap* or the name of an existing interface to bridge to.")
flag.BoolVar(&flagUseHTTP, "http", flagUseHTTP, "Use HTTP tap interface.") flag.BoolVar(&flagUseHTTP, "http", flagUseHTTP, "Use HTTP tap interface.")
flag.StringVar(&flagHostToResolve, "host", flagHostToResolve, "Hostname to resolve via DNS.") flag.StringVar(&flagHostToResolve, "host", flagHostToResolve, "Hostname to resolve via DNS.")
flag.StringVar(&flagRequestedIP, "addr", flagRequestedIP, "IP address to request via DHCP.")
flag.Parse() flag.Parse()
fmt.Println("softrand", softRand)
_, err = dns.NewName(flagHostToResolve) _, err = dns.NewName(flagHostToResolve)
if err != nil { if err != nil {
flag.Usage() flag.Usage()
@@ -72,7 +78,7 @@ func run() (err error) {
return err return err
} }
brHW := nicHW brHW := nicHW
brHW[5]++ // We'll be using a similar HW address but with NIC specific identifier modified. brHW[5] += byte(softRand)%128 + 1 // We'll be using a similar HW address but with NIC specific identifier modified.
mtu, err := iface.MTU() mtu, err := iface.MTU()
if err != nil { if err != nil {
return err return err
@@ -87,7 +93,7 @@ func run() (err error) {
if err != nil { if err != nil {
return err return err
} }
err = stack.BeginDHCPRequest() err = stack.BeginDHCPRequest([4]byte{192, 168, 1, 199})
if err != nil { if err != nil {
return err return err
} }
@@ -272,15 +278,14 @@ func (s *Stack) ResultLookupIP() ([]netip.Addr, error) {
return addrs, nil return addrs, nil
} }
func (s *Stack) BeginDHCPRequest() error { func (s *Stack) BeginDHCPRequest(request [4]byte) error {
addr4 := s.ip.Addr().As4()
var buf [4]byte var buf [4]byte
rand.Read(buf[:]) rand.Read(buf[:])
xid := binary.LittleEndian.Uint32(buf[:]) xid := binary.LittleEndian.Uint32(buf[:])
err := s.dhcp.BeginRequest(xid, dhcpv4.RequestConfig{ err := s.dhcp.BeginRequest(xid, dhcpv4.RequestConfig{
RequestedAddr: addr4, RequestedAddr: request,
ClientHardwareAddr: s.link.HardwareAddr6(), ClientHardwareAddr: s.link.HardwareAddr6(),
Hostname: "lneto", Hostname: "lneto" + strconv.FormatInt(softRand%100, 16),
}) })
if err != nil { if err != nil {
return err return err