DNS: 'nameserver' and 'search' lines working.

This commit is contained in:
Philippe PITTOLI 2019-10-17 18:29:26 +02:00
parent 2cf04c331a
commit 2e7d3c8f28
3 changed files with 48 additions and 16 deletions

View File

@ -29,7 +29,7 @@ class WirelessAPSetup
property main_ip_v6 : IPAddress | DHCP | Autoconfiguration | NotSetup
property aliasses_v4 : Array(IPAddress)
property aliasses_v6 : Array(IPAddress)
property dns : Array(IPAddress)
property dns : NetworkCommands::DNS
# we currently only support WPA2-PSK wireless security mechanism
property security : WPA
@ -46,7 +46,7 @@ class WirelessAPSetup
@aliasses_v4 = Array(IPAddress).new
@aliasses_v6 = Array(IPAddress).new
@up = true
@dns = Array(IPAddress).new
@dns = NetworkCommands::DNS.new
end
@ -80,14 +80,25 @@ class WirelessAPSetup
end
end
dns.each do |ip|
dns.addresses.each do |ip|
str << "\t\tdns: #{ip}\n"
end
dns.search.each do |localdomain|
str << "\t\tdomain: #{localdomain}\n"
end
# to improve readability
str << "\n"
end
end
def execute
puts "TODO: wireless configuration"
# DNS configuration
dns.execute
end
end
@ -107,14 +118,14 @@ class InterfaceConfiguration
property aliasses_v4 : Array(IPAddress)
property aliasses_v6 : Array(IPAddress)
property wireless_networks : Hash(String, WirelessAPSetup)
property dns : Array(IPAddress)
property dns : NetworkCommands::DNS
def initialize (@name, @up,
@description,
@mtu,
@main_ip_v4, @main_ip_v6, aliasses,
@wireless, @wireless_networks,
@dns = Array(IPAddress).new
@dns
)
@aliasses_v4 = Array(IPAddress).new
@ -179,10 +190,12 @@ class InterfaceConfiguration
str << "\t#{CRED}Should main ipv6 be obtained from autoconfiguration? DHCP? Static configuration?#{CRESET}\n"
end
unless dns.empty?
dns.each do |ip|
str << "\tdns: #{ip}\n"
end
dns.addresses.each do |ip|
str << "\tdns: #{ip}\n"
end
dns.search.each do |localdomain|
str << "\t\tdomain: #{localdomain}\n"
end
unless wireless_networks.empty?
@ -270,6 +283,9 @@ class InterfaceConfiguration
end
end
end
# DNS configuration
dns.execute
end
def down

View File

@ -9,16 +9,32 @@ class NetworkCommands
class_property cmd_dhcp_client : UDHCPCCommand.class | DHClientCommand.class | NotSetup.class = NotSetup
class DNS
def initialize(@addresses : Array(String), @search : Array(String))
property addresses : Array(String)
property search : Array(String)
def initialize()
@addresses = Array(String).new
@search = Array(String).new
end
def execute
File.open("#{Context.root}/etc/resolv.conf", "w") do |file|
Dir.mkdir_p("#{Context.root}/etc/")
if Context.simulation
puts "simulation, writing in #{Context.root}/etc/resolv.conf:"
@addresses.each do |address|
file.puts "nameserver #{address}\n"
puts "\tnameserver #{address}\n"
end
file.puts "search #{@search.join(" ")}"
puts "\tsearch #{@search.join(" ")}" unless search.empty?
else
File.open("#{Context.root}/etc/resolv.conf", "w+") do |file|
@addresses.each do |address|
file.puts "nameserver #{address}\n"
end
file.puts "search #{@search.join(" ")}" unless search.empty?
end
end
end
end

View File

@ -20,7 +20,7 @@ class NetworkConfigurationParser
main_ip_v4 = NotSetup.new
main_ip_v6 = NotSetup.new
dns = Array(IPAddress).new
dns = NetworkCommands::DNS.new
aliasses = [] of IPAddress
@ -170,7 +170,7 @@ class NetworkConfigurationParser
access_point = wireless_networks[ssid].not_nil!
ipaddr = IPAddress.parse ipstr
access_point.dns << ipaddr
access_point.dns.addresses << ipaddr.to_s
when /^mtu [0-9]+/
mtu = /^mtu ([0-9]+)/.match(line).try &.[1].to_i
@ -188,7 +188,7 @@ class NetworkConfigurationParser
end
ipaddr = IPAddress.parse ipstr
dns << ipaddr
dns.addresses << ipaddr.to_s
when /^#.*$/
# simple comment