From c01d1f49c29c71962131021ffe3925a08c181a6c Mon Sep 17 00:00:00 2001 From: maride Date: Thu, 28 Nov 2019 19:16:43 +0100 Subject: [PATCH] Add support detecting the request for a specific IP address over DHCP --- ethernet/dhcpv4/dhcp.go | 21 ++++++++++++++++++--- ethernet/dhcpv4/dhcpResponse.go | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ethernet/dhcpv4/dhcp.go b/ethernet/dhcpv4/dhcp.go index d29ac2e..8093d21 100644 --- a/ethernet/dhcpv4/dhcp.go +++ b/ethernet/dhcpv4/dhcp.go @@ -45,7 +45,7 @@ func HandleDHCPv4Packet(packet gopacket.Packet) error { appendIfUnique(dhcppacket.ClientHWAddr.String(), requestMAC) } else { // Response/Offer packet - addResponseEntry(dhcppacket.ClientIP.String(), dhcppacket.ClientHWAddr.String(), ethernetpacket.SrcMAC.String()) + addResponseEntry(dhcppacket.ClientIP.String(), dhcppacket.YourClientIP.String(), dhcppacket.ClientHWAddr.String(), ethernetpacket.SrcMAC.String()) } return nil @@ -72,7 +72,13 @@ func printResponseSummary() { // Iterate over all responses for _, r := range responses { - tmpaddr = append(tmpaddr, fmt.Sprintf("%s offered %s IP address %s", r.serverMACAddr, r.destMACAddr, r.newIPAddr)) + addition := "" + + if r.askedFor { + addition = " which the client explicitly asked for." + } + + tmpaddr = append(tmpaddr, fmt.Sprintf("%s offered %s IP address %s%s", r.serverMACAddr, r.destMACAddr, r.newIPAddr, addition)) } // Draw as tree @@ -80,7 +86,15 @@ func printResponseSummary() { } // Adds a new response entry. If an IP address was already issued or a MAC asks multiple times for DNS, the case is examined further -func addResponseEntry(newIP string, destMAC string, serverMAC string) { +func addResponseEntry(newIP string, yourIP string, destMAC string, serverMAC string) { + // Check if client asked for a specific address (which was granted by the DHCP server) + askedFor := false + if newIP == "0.0.0.0" { + // Yes, client asked for a specific address. Most likely not the first time in this network. + newIP = yourIP + askedFor = true + } + for _, r := range responses { // Check for interesting cases if r.destMACAddr == destMAC { @@ -112,5 +126,6 @@ func addResponseEntry(newIP string, destMAC string, serverMAC string) { destMACAddr: destMAC, newIPAddr: newIP, serverMACAddr: serverMAC, + askedFor: askedFor, }) } \ No newline at end of file diff --git a/ethernet/dhcpv4/dhcpResponse.go b/ethernet/dhcpv4/dhcpResponse.go index b4cc2e5..077eb43 100644 --- a/ethernet/dhcpv4/dhcpResponse.go +++ b/ethernet/dhcpv4/dhcpResponse.go @@ -4,5 +4,6 @@ type dhcpResponse struct { destMACAddr string newIPAddr string serverMACAddr string + askedFor bool }