1
0
mirror of https://git.baguette.netlib.re/Baguette/networkctl synced 2024-12-18 13:03:30 +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 aliasses_v4 : Array(IPAddress)
property aliasses_v6 : Array(IPAddress)
property dns : Array(IPAddress)

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


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

def to_string
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#{@up? "up" : "down"}\n"
str << "\tmtu #{mtu}\n" unless mtu.nil?
str << "\t\t#description #{description.not_nil!}\n" unless description.nil?
str << "\t\t#{@up? "up" : "down"}\n"
str << "\t\tmtu #{mtu}\n" unless mtu.nil?

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

# ipv6
str << "\tinet6 #{@main_ip_v6}\n"
str << "\t\tinet6 #{@main_ip_v6}\n"
unless @aliasses_v6.empty?
@aliasses_v6.each do |a|
str << "\talias6 #{a}\n"
str << "\t\talias6 #{a}\n"
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
@ -361,21 +374,24 @@ class InterfaceConfiguration
property aliasses_v4 : Array(IPAddress)
property aliasses_v6 : Array(IPAddress)
property wireless_networks : Hash(String, WirelessAPSetup)
property dns : Array(IPAddress)

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

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

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

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

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

if wireless_networks.empty?
puts "no wireless connection configured" if wireless
if dns.empty?
str << "\tno dns configured\n"
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|
puts v
str << v
end
end
end
@ -432,30 +459,28 @@ class InterfaceConfiguration
raise "The interface #{@name} doesn't exists, yet."
end

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

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

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

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

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

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

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

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

dns = Array(IPAddress).new

aliasses = [] of IPAddress

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

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

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

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

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

# TODO
access_point = wireless_networks[ssid].not_nil!

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

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