From 2e7d3c8f2802f43fc10fc3e4f48d433ea4bf6b42 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Thu, 17 Oct 2019 18:29:26 +0200 Subject: [PATCH] DNS: 'nameserver' and 'search' lines working. --- src/configuration.cr | 34 +++++++++++++++++++++-------- src/network_commands.cr | 24 ++++++++++++++++---- src/network_configuration_parser.cr | 6 ++--- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/configuration.cr b/src/configuration.cr index ebafe4c..8106002 100644 --- a/src/configuration.cr +++ b/src/configuration.cr @@ -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 diff --git a/src/network_commands.cr b/src/network_commands.cr index 1365282..8af9626 100644 --- a/src/network_commands.cr +++ b/src/network_commands.cr @@ -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 diff --git a/src/network_configuration_parser.cr b/src/network_configuration_parser.cr index 4c3f33b..9d38c0c 100644 --- a/src/network_configuration_parser.cr +++ b/src/network_configuration_parser.cr @@ -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