Skip to content

Commit

Permalink
Fixes #21236 - No ping check causes duplicated IP with MS DHCP
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Chevaletà committed Oct 8, 2017
1 parent d46d7c7 commit 9023c7b
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion modules/dhcp_native_ms/dhcp_native_ms_main.rb
Expand Up @@ -70,6 +70,21 @@ def set_option_values(ip_address, subnet_address, option_values)
end
end

def icmp_pingable? ip
# Always shell to ping, instead of using net-ping
if PLATFORM =~ /mingw/
# Windows uses different options for ping and does not have /dev/null
system("ping -n 1 -w 1000 #{ip} > NUL")
else
# Default to Linux ping options and send to /dev/null
system("ping -c 1 -W 1 #{ip} > /dev/null")
end
rescue => err
# We failed to check this address so we should not use it
logger.warn "Unable to icmp ping #{ip} because #{err.inspect}. Skipping this address..."
true
end

def unused_ip(subnet_address, mac_address, from_address, to_address)
client = dhcpsapi.get_client_by_mac_address(subnet_address, mac_address) rescue nil
return client[:client_ip_address] unless client.nil?
Expand All @@ -78,7 +93,24 @@ def unused_ip(subnet_address, mac_address, from_address, to_address)
raise Proxy::DHCP::NotImplemented.new("DhcpsApi::Server#get_free_ip_address is not available on Windows Server 2008 and earlier versions.")
end

return dhcpsapi.get_free_ip_address(subnet_address, from_address, to_address).first
max_free_ips = 10
free_ips = dhcpsapi.get_free_ip_address(subnet_address, from_address, to_address, max_free_ips)
if free_ips.empty?
logger.warn "No free IPs returned by DHCP for subnet #{subnet_address} from #{from_address} to #{to_address}"
return nil
end
logger.debug "DHCP returned #{free_ips.size} free IPs (max: #{max_free_ips})"
free_ips.each do |ip|
logger.debug "Searching for free IP - pinging #{ip}"
if icmp_pingable?(ip)
logger.debug "Found a pingable IP address which does not have a DHCP record: #{ip}"
else
logger.info "Found free IP #{ip}"
return ip
end
end
logger.warn "All #{free_ips.size} IPs returned by the DHCP are already in use"
return nil
end

def retrieve_subnet_from_server(subnet_address)
Expand Down

0 comments on commit 9023c7b

Please sign in to comment.