1
0
mirror of https://git.baguette.netlib.re/Baguette/networkctl synced 2024-12-22 23:13:21 +00:00
This commit is contained in:
Philippe PITTOLI 2019-10-07 01:00:06 +02:00
parent e184ef6158
commit e45da83235

View File

@ -296,6 +296,7 @@ class WirelessAPSetup
property main_ip_v6 : IPAddress | DHCP | NotSetup property main_ip_v6 : IPAddress | DHCP | NotSetup
property aliasses_v4 : Array(IPAddress) property aliasses_v4 : Array(IPAddress)
property aliasses_v6 : Array(IPAddress) property aliasses_v6 : Array(IPAddress)
property dns : Array(IPAddress)


# we currently only support WPA2-PSK wireless security mechanism # we currently only support WPA2-PSK wireless security mechanism
property security : WPA property security : WPA
@ -312,6 +313,7 @@ class WirelessAPSetup
@aliasses_v4 = Array(IPAddress).new @aliasses_v4 = Array(IPAddress).new
@aliasses_v6 = Array(IPAddress).new @aliasses_v6 = Array(IPAddress).new
@up = true @up = true
@dns = Array(IPAddress).new
end end




@ -321,25 +323,36 @@ class WirelessAPSetup


def to_string def to_string
String.build do |str| String.build do |str|
str << "#{CBLUE}#{ssid}: {ssid}#{CRESET}\n" str << "\t#{CBLUE}#{ssid}#{CRESET}\n"


str << "\t#description #{description.not_nil!}\n" unless description.nil? str << "\t\t#description #{description.not_nil!}\n" unless description.nil?
str << "\t#{@up? "up" : "down"}\n" str << "\t\t#{@up? "up" : "down"}\n"
str << "\tmtu #{mtu}\n" unless mtu.nil? str << "\t\tmtu #{mtu}\n" unless mtu.nil?


# ipv4 # ipv4
str << "\tinet #{@main_ip_v4}\n" str << "\t\tinet #{@main_ip_v4}\n"
@aliasses_v4.each do |a| @aliasses_v4.each do |a|
str << "\talias #{a}\n" str << "\t\talias #{a}\n"
end end


# ipv6 # ipv6
str << "\tinet6 #{@main_ip_v6}\n" str << "\t\tinet6 #{@main_ip_v6}\n"
unless @aliasses_v6.empty? unless @aliasses_v6.empty?
@aliasses_v6.each do |a| @aliasses_v6.each do |a|
str << "\talias6 #{a}\n" str << "\t\talias6 #{a}\n"
end end
end end

if dns.empty?
str << "\t\tno dns configured\n"
else
dns.each do |ip|
str << "\t\tdns: #{ip}\n"
end
end

# to improve readability
str << "\n"
end end
end end
end end
@ -361,21 +374,24 @@ class InterfaceConfiguration
property aliasses_v4 : Array(IPAddress) property aliasses_v4 : Array(IPAddress)
property aliasses_v6 : Array(IPAddress) property aliasses_v6 : Array(IPAddress)
property wireless_networks : Hash(String, WirelessAPSetup) property wireless_networks : Hash(String, WirelessAPSetup)
property dns : Array(IPAddress)


def initialize (@name, @up, def initialize (@name, @up,
@description, @description,
@mtu, @mtu,
@main_ip_v4, @main_ip_v6, aliasses, @main_ip_v4, @main_ip_v6, aliasses,
@wireless, @wireless_networks) @wireless, @wireless_networks,
@dns = Array(IPAddress).new
)


@aliasses_v4 = Array(IPAddress).new @aliasses_v4 = Array(IPAddress).new
@aliasses_v6 = Array(IPAddress).new @aliasses_v6 = Array(IPAddress).new


aliasses.each do |ip| aliasses.each do |ip|
if ip.ipv4? if ip.ipv4?
@aliasses_v4 << ip aliasses_v4 << ip
else else
@aliasses_v6 << ip aliasses_v6 << ip
end end
end end
end end
@ -401,26 +417,37 @@ class InterfaceConfiguration
end end


# ipv4 # ipv4
str << "\tinet #{@main_ip_v4}\n" str << "\tinet #{main_ip_v4}\n"
unless @aliasses_v4.empty? unless aliasses_v4.empty?
@aliasses_v4.each do |a| aliasses_v4.each do |a|
str << "\talias #{a}\n" str << "\talias #{a}\n"
end end
end end


# ipv6 # ipv6
str << "\tinet6 #{@main_ip_v6}\n" str << "\tinet6 #{main_ip_v6}\n"
unless @aliasses_v6.empty? unless aliasses_v6.empty?
@aliasses_v6.each do |a| aliasses_v6.each do |a|
str << "\talias6 #{a}\n" str << "\talias6 #{a}\n"
end end
end end


if wireless_networks.empty? if dns.empty?
puts "no wireless connection configured" if wireless str << "\tno dns configured\n"
else else
dns.each do |ip|
str << "\tdns: #{ip}\n"
end
end

if wireless_networks.empty?
str << "\tno wireless connection configured\n" if wireless
else

# to improve readability
str << "\n"
wireless_networks.each do |k,v| wireless_networks.each do |k,v|
puts v str << v
end end
end end
end end
@ -432,30 +459,28 @@ class InterfaceConfiguration
raise "The interface #{@name} doesn't exists, yet." raise "The interface #{@name} doesn't exists, yet."
end end


if @up if up
NetworkCommands.up @name NetworkCommands.up name
else else
puts "not marked as 'up' -- ending here" puts "not marked as 'up' -- ending here"
return return
end end


mtu = @mtu
unless mtu.nil? unless mtu.nil?
NetworkCommands.mtu @name, mtu NetworkCommands.mtu name, mtu
end end


description = @description
unless description.nil? unless description.nil?
NetworkCommands.description @name, description NetworkCommands.description name, description.not_nil!
end end


# ipv4 configuration # ipv4 configuration
@main_ip_v4.tap do |ip| main_ip_v4.tap do |ip|
case ip case ip
when IPAddress when IPAddress
NetworkCommands.set_ip @name, ip NetworkCommands.set_ip name, ip
when DHCP when DHCP
NetworkCommands.dhcp @name NetworkCommands.dhcp name
when NotSetup when NotSetup
puts "no ipv4" puts "no ipv4"
else else
@ -464,23 +489,23 @@ class InterfaceConfiguration


# We wont setup aliasses unless there is an actual IP address # We wont setup aliasses unless there is an actual IP address
if ip != NotSetup if ip != NotSetup
@aliasses_v4.each do |ip_alias| aliasses_v4.each do |ip_alias|
NetworkCommands.set_alias @name, ip_alias NetworkCommands.set_alias name, ip_alias
end end
end end
end end


# ipv6 configuration # ipv6 configuration
@main_ip_v6.tap do |ip| main_ip_v6.tap do |ip|


case ip case ip
when IPAddress when IPAddress
NetworkCommands.set_ip @name, ip NetworkCommands.set_ip name, ip
# TODO # TODO
#when Autoconfiguration #when Autoconfiguration
# NetworkCommands.autoconfiguration @name # NetworkCommands.autoconfiguration name
#when DHCP #when DHCP
# NetworkCommands.dhcp6 @name # NetworkCommands.dhcp6 name
when NotSetup when NotSetup
puts "no ipv6" puts "no ipv6"
else else
@ -489,8 +514,8 @@ class InterfaceConfiguration


# We wont setup aliasses unless there is an actual IP address # We wont setup aliasses unless there is an actual IP address
if ip != NotSetup if ip != NotSetup
@aliasses_v6.each do |ip_alias| aliasses_v6.each do |ip_alias|
NetworkCommands.set_alias @name, ip_alias NetworkCommands.set_alias name, ip_alias
end end
end end
end end
@ -518,6 +543,8 @@ class NetworkConfigurationParser
main_ip_v4 = NotSetup.new main_ip_v4 = NotSetup.new
main_ip_v6 = NotSetup.new main_ip_v6 = NotSetup.new


dns = Array(IPAddress).new

aliasses = [] of IPAddress aliasses = [] of IPAddress


wireless_networks = {} of String => WirelessAPSetup wireless_networks = {} of String => WirelessAPSetup
@ -536,15 +563,16 @@ class NetworkConfigurationParser
end end
aliasses.push IPAddress.parse(ipstr) aliasses.push IPAddress.parse(ipstr)
when /^inet6? dhcp/ when /^inet6? dhcp/
# IPaddress is DHCP # IP address is DHCP
if /^inet /.match(line) if /^inet /.match(line)
main_ip_v4 = DHCP.new main_ip_v4 = DHCP.new
else else
main_ip_v6 = DHCP.new main_ip_v6 = DHCP.new
end end
when /^inet6 autoconf/ when /^inet6 autoconf/
# IPaddress is autoconfigured # IP address is autoconfigured
puts "TODO: IPv6 autoconfiguration" puts "TODO: IPv6 autoconfiguration"

when /^inet6? .*/ when /^inet6? .*/
ipstr = /^inet6? ([a-f0-9:.\/]+)/.match(line).try &.[1] ipstr = /^inet6? ([a-f0-9:.\/]+)/.match(line).try &.[1]
if ipstr.nil? if ipstr.nil?
@ -603,8 +631,6 @@ class NetworkConfigurationParser
ssid = m["ssid"] ssid = m["ssid"]
ipstr = m["ip"] ipstr = m["ip"]
end end
# ssid = /^network ([^ \t]+)/.match(line).try &.[1]
# ipstr = /^network [^ \t]+ inet6? (?<ip>[^ \t]+)/.match(line).try &.["ip"]


if ssid.nil? if ssid.nil?
puts "wrong SSID in line: #{line}" puts "wrong SSID in line: #{line}"
@ -631,14 +657,13 @@ class NetworkConfigurationParser
end end


when /^network [^ ]+ dhcp6?/ when /^network [^ ]+ dhcp6?/
ssid = /^network ([^ \t]+)/.match(line).try &.[1] ssid = /^network (?<ssid>[^ \t]+)/.match(line).try &.["ssid"]


if ssid.nil? if ssid.nil?
puts "wrong SSID in line: #{line}" puts "wrong SSID in line: #{line}"
next next
end end


# TODO
access_point = wireless_networks[ssid].not_nil! access_point = wireless_networks[ssid].not_nil!


if /dhcp6/.match(line) if /dhcp6/.match(line)
@ -650,11 +675,46 @@ class NetworkConfigurationParser
end end


when /^network [^ ]+ dns .*/ when /^network [^ ]+ dns .*/
puts "TODO: network SSID dns" ssid = nil
ipstr = nil

/^network (?<ssid>[^ \t]+) dns (?<ip>[^ \t]+)/.match(line).try do |m|
ssid = m["ssid"]
ipstr = m["ip"]
end

if ssid.nil?
puts "wrong SSID in line: #{line}"
next
end

if ipstr.nil?
puts "wrong ip address in line: #{line}"
next
end

access_point = wireless_networks[ssid].not_nil!
ipaddr = IPAddress.parse ipstr
access_point.dns << ipaddr


when /^mtu [0-9]+/ when /^mtu [0-9]+/
mtu = /^mtu ([0-9]+)/.match(line).try &.[1].to_i mtu = /^mtu ([0-9]+)/.match(line).try &.[1].to_i


when /^dns [^ \t]+/
ipstr = nil

/^dns (?<ip>[^ \t]+)/.match(line).try do |m|
ipstr = m["ip"]
end

if ipstr.nil?
puts "wrong ip address in line: #{line}"
next
end

ipaddr = IPAddress.parse ipstr
dns << ipaddr

when /^#.*$/ when /^#.*$/
# simple comment # simple comment
when /^[ \t]*$/ when /^[ \t]*$/
@ -669,7 +729,8 @@ class NetworkConfigurationParser
mtu, mtu,
main_ip_v4, main_ip_v6, main_ip_v4, main_ip_v6,
aliasses, aliasses,
wireless, wireless_networks) wireless, wireless_networks,
dns)
end end
end end