minor changes to xml, small additions to network

This commit is contained in:
brent s 2019-11-12 01:27:48 -05:00
parent 5371ae2361
commit 8ee5137059
4 changed files with 77 additions and 28 deletions

54
aif.xsd
View File

@ -219,25 +219,35 @@
<xs:choice maxOccurs="unbounded"> <xs:choice maxOccurs="unbounded">
<xs:element name="addresses" minOccurs="0" maxOccurs="1"> <xs:element name="addresses" minOccurs="0" maxOccurs="1">
<xs:complexType> <xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded"> <xs:sequence>
<xs:element name="ipv4"> <xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:complexType> <xs:element name="ipv4">
<xs:sequence> <xs:complexType>
<xs:element name="address" type="aif:t_address_ip4" <xs:sequence>
minOccurs="1" maxOccurs="unbounded"/> <xs:element name="address" type="aif:t_address_ip4"
</xs:sequence> minOccurs="0" maxOccurs="unbounded"/>
<xs:attribute name="gateway" type="aif:t_address_ip4" use="optional"/> </xs:sequence>
</xs:complexType> <xs:attribute name="gateway" type="aif:t_address_ip4" use="optional"/>
</xs:element> </xs:complexType>
<xs:element name="ipv6"> <xs:unique name="uniq_ipv4_addr">
<xs:complexType> <xs:selector xpath="aif:address"/>
<xs:sequence> <xs:field xpath="."/>
<xs:element name="address" type="aif:t_address_ip6" </xs:unique>
minOccurs="1" maxOccurs="unbounded"/> </xs:element>
</xs:sequence> <xs:element name="ipv6">
<xs:attribute name="gateway" type="aif:t_address_ip6" use="optional"/> <xs:complexType>
</xs:complexType> <xs:sequence>
</xs:element> <xs:element name="address" type="aif:t_address_ip6"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="gateway" type="aif:t_address_ip6" use="optional"/>
</xs:complexType>
<xs:unique name="uniq_ipv6_addr">
<xs:selector xpath="aif:address"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:choice>
</xs:sequence> </xs:sequence>
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>
@ -749,11 +759,7 @@
<xs:attribute name="provider" type="aif:t_netprov" use="optional" default="netctl"/> <xs:attribute name="provider" type="aif:t_netprov" use="optional" default="netctl"/>
</xs:complexType> </xs:complexType>
<xs:unique name="uniq_iface_eth"> <xs:unique name="uniq_iface_eth">
<xs:selector xpath="aif:ethernet"/> <xs:selector xpath="aif:ethernet|aif:wireless"/>
<xs:field xpath="@device"/>
</xs:unique>
<xs:unique name="uniq_iface_wlan">
<xs:selector xpath="aif:wireless"/>
<xs:field xpath="@device"/> <xs:field xpath="@device"/>
</xs:unique> </xs:unique>
</xs:element> </xs:element>

View File

@ -5,15 +5,20 @@ class Network(object):
self.provider = self.xml.attrib.get('provider', 'netctl') self.provider = self.xml.attrib.get('provider', 'netctl')
handler = None handler = None
if self.provider == 'netctl': if self.provider == 'netctl':
from . import netctl as handler import aif.network.netctl as handler
elif self.provider == 'nm': elif self.provider == 'nm':
from . import networkmanager as handler import aif.network.networkmanager as handler
elif self.provider == 'systemd': elif self.provider == 'systemd':
from . import networkd as handler import aif.network.networkd as handler
self.provider = handler self.provider = handler
if not self.provider: if not self.provider:
raise RuntimeError('Could not determine handler') raise RuntimeError('Could not determine handler')
self.connections = [] self.connections = []


def _initConns(self): def _initConns(self):
pass for e in self.xml.xpath('ethernet|wireless'):
if e.tag == 'ethernet':
conn = self.provider.Ethernet(e)
elif e.tag == 'wireless':
conn = self.provider.Wireless(e)
self.connections.append(conn)

View File

@ -1,3 +1,5 @@
import ipaddress
##
from . import _common from . import _common


_NM = _common.NM _NM = _common.NM
@ -9,6 +11,36 @@ class Connection(object):
self.connection_type = None self.connection_type = None
self.provider_type = 'NetworkManager' self.provider_type = 'NetworkManager'
self.client = _NM.Client.new() self.client = _NM.Client.new()
self.addrs = {'ipv4': [],
'ipv6': []}
self.resolvers = []
self._initAddrs()
self._initResolvers()

def _initAddrs(self):
for t in ('ipv4', 'ipv6'):
for a in self.xml.findall('addresses/{0}/address'.format(t)):
if a.text in ('dhcp', 'dhcp6', 'slaac'):
addr = net = None
else:
components = a.text.split('/')
if len(components) > 2:
raise ValueError('Invalid IP/CIDR format: {0}'.format(a.text))
if len(components) == 1:
addr = components[0]
if t == 'ipv4':
components.append('24')
elif t == 'ipv6':
components.append('64')
addr = ipaddress.ip_address(components[0])
net = ipaddress.ip_network('/'.join(components), strict = False)
self.addrs[t].append((addr, net))
return()

def _initResolvers(self):
for r in self.xml.findall('resolvers/resolver'):
self.resolvers.append(ipaddress.ip_address(r.text))
return()




class Ethernet(Connection): class Ethernet(Connection):

View File

@ -124,6 +124,7 @@
<addresses> <addresses>
<ipv4> <ipv4>
<address>dhcp</address> <address>dhcp</address>
<address>192.168.1.5/24</address>
</ipv4> </ipv4>
<ipv6> <ipv6>
<address>slaac</address> <address>slaac</address>
@ -135,6 +136,11 @@
</resolvers> </resolvers>
</ethernet> </ethernet>
<wireless device="wlp2s0" essid="MyWirelessLan" bssid="00-00-5E-00-53-00" defroute="false"> <wireless device="wlp2s0" essid="MyWirelessLan" bssid="00-00-5E-00-53-00" defroute="false">
<addresses>
<ipv4>
<address>dhcp</address>
</ipv4>
</addresses>
<encryption> <encryption>
<type>wpa2</type> <type>wpa2</type>
<mode>personal</mode> <mode>personal</mode>