changes on behalf of acid-burn
[enigma2.git] / lib / python / Components / Network.py
index cb2727cbfae4ed81c31927f38e8f419b7fee399f..56506a50b725217bd03389cde93f15c221199a3a 100644 (file)
@@ -6,7 +6,7 @@ from enigma import eConsoleAppContainer
 class Network:
        def __init__(self):
                self.ifaces = {}
-               self.configuredInterfaces = {}
+               self.configuredInterfaces = []
                self.nameservers = []
                self.getInterfaces()
                self.ethtool_bin = "/usr/sbin/ethtool"
@@ -14,7 +14,7 @@ class Network:
 
        def getInterfaces(self):
                devicesPattern = re_compile('[a-z]+[0-9]+')
-
+               self.configuredInterfaces = []
                fp = file('/proc/net/dev', 'r')
                result = fp.readlines()
                fp.close()
@@ -30,7 +30,7 @@ class Network:
                        except AttributeError:
                                pass
 
-               print "self.ifaces:", self.ifaces
+               #print "self.ifaces:", self.ifaces
                self.loadNetworkConfig()
                #self.writeNetworkConfig()
                #print ord(' ')
@@ -63,11 +63,11 @@ class Network:
                ipPattern = re_compile(ipRegexp)
                upPattern = re_compile('UP ')
                macPattern = re_compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}')
-               
+
                fp = popen("ifconfig " + iface)
                result = fp.readlines()
                fp.close()
-               data = { 'up': False, 'dhcp': False }
+               data = { 'up': False, 'dhcp': False, 'preup' : False, 'postdown' : False }
                for line in result:
                        ip = self.regExpMatch(ipPattern, self.regExpMatch(ipLinePattern, line))
                        netmask = self.regExpMatch(ipPattern, self.regExpMatch(netmaskLinePattern, line))
@@ -82,13 +82,15 @@ class Network:
                                data['bcast'] = self.convertIP(bcast)
                        if up is not None:
                                data['up'] = True
+                               if iface is not 'lo':
+                                       self.configuredInterfaces.append(iface)
                        if mac is not None:
                                data['mac'] = mac
                if not data.has_key('ip'):
                        data['dhcp'] = True
-                       data['ip'] = [192, 168, 1, 2]
-                       data['netmask'] = [255, 255, 255, 0]
-                       data['gateway'] = [192, 168, 1, 1]
+                       data['ip'] = [0, 0, 0, 0]
+                       data['netmask'] = [0, 0, 0, 0]
+                       data['gateway'] = [0, 0, 0, 0]
                        
                fp = popen("route -n | grep  " + iface)
                result = fp.readlines()
@@ -102,6 +104,7 @@ class Network:
                return data
 
        def writeNetworkConfig(self):
+               self.configuredInterfaces = []
                fp = file('/etc/network/interfaces', 'w')
                fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
                fp.write("auto lo\n")
@@ -109,19 +112,23 @@ class Network:
                for ifacename, iface in self.ifaces.items():
                        if iface['up'] == True:
                                fp.write("auto " + ifacename + "\n")
-                               if iface['dhcp'] == True:
-                                       fp.write("iface "+ ifacename +" inet dhcp\n")
-                               if iface['dhcp'] == False:
-                                       fp.write("iface "+ ifacename +" inet static\n")
-                                       if iface.has_key('ip'):
-                                               print tuple(iface['ip'])
-                                               fp.write("      address %d.%d.%d.%d\n" % tuple(iface['ip']))
-                                               fp.write("      netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
-                                               if iface.has_key('gateway'):
-                                                       fp.write("      gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
-                               if iface.has_key("configStrings"):
-                                       fp.write("\n" + iface["configStrings"] + "\n")
-                               fp.write("\n")                          
+                               self.configuredInterfaces.append(ifacename)
+                       if iface['dhcp'] == True:
+                               fp.write("iface "+ ifacename +" inet dhcp\n")
+                       if iface['dhcp'] == False:
+                               fp.write("iface "+ ifacename +" inet static\n")
+                               if iface.has_key('ip'):
+                                       print tuple(iface['ip'])
+                                       fp.write("      address %d.%d.%d.%d\n" % tuple(iface['ip']))
+                                       fp.write("      netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
+                                       if iface.has_key('gateway'):
+                                               fp.write("      gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
+                       if iface.has_key("configStrings"):
+                               fp.write("\n" + iface["configStrings"] + "\n")
+                       if iface["preup"] is not False and not iface.has_key("configStrings"):
+                               fp.write(iface["preup"])
+                               fp.write(iface["postdown"])
+                       fp.write("\n")                          
                fp.close()
                self.writeNameserverConfig()
 
@@ -156,17 +163,30 @@ class Network:
                        if (currif != ""):
                                if (split[0] == "address"):
                                        ifaces[currif]["address"] = map(int, split[1].split('.'))
+                                       if self.ifaces[currif].has_key("ip"):
+                                               if self.ifaces[currif]["ip"] != ifaces[currif]["address"] and ifaces[currif]["dhcp"] == False:
+                                                       self.ifaces[currif]["ip"] = map(int, split[1].split('.'))
                                if (split[0] == "netmask"):
                                        ifaces[currif]["netmask"] = map(int, split[1].split('.'))
+                                       if self.ifaces[currif].has_key("netmask"):
+                                               if self.ifaces[currif]["netmask"] != ifaces[currif]["netmask"] and ifaces[currif]["dhcp"] == False:
+                                                       self.ifaces[currif]["netmask"] = map(int, split[1].split('.'))
                                if (split[0] == "gateway"):
                                        ifaces[currif]["gateway"] = map(int, split[1].split('.'))
-               
-               self.configuredInterfaces = ifaces
+                                       if self.ifaces[currif].has_key("gateway"):
+                                               if self.ifaces[currif]["gateway"] != ifaces[currif]["gateway"] and ifaces[currif]["dhcp"] == False:
+                                                       self.ifaces[currif]["gateway"] = map(int, split[1].split('.'))                                  
+                               if (split[0] == "pre-up"):
+                                       if self.ifaces[currif].has_key("preup"):
+                                               self.ifaces[currif]["preup"] = i
+                               if (split[0] == "post-down"):
+                                       if self.ifaces[currif].has_key("postdown"):
+                                               self.ifaces[currif]["postdown"] = i
+
                print "read interfaces:", ifaces
                for ifacename, iface in ifaces.items():
                        if self.ifaces.has_key(ifacename):
                                self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
-
                print "self.ifaces after loading:", self.ifaces
 
        def loadNameserverConfig(self):
@@ -294,20 +314,10 @@ class Network:
                self.getInterfaces()    
 
        def checkNetworkState(self):
-               ok_counter = 0
-               ret1=system("ping -c 1 www.dream-multimedia-tv.de")
-               if ret1 != 0:
-                       ok_counter = ok_counter + 1
-               ret2=system("ping -c 1 www.heise.de")
-               if ret2 != 0:
-                       ok_counter = ok_counter + 1
-               ret3=system("ping -c 1 www.google.de")
-               if ret2 != 0:
-                       ok_counter = ok_counter + 1             
-               if ok_counter <= 2:
-                       return True
-               else:
-                       return False
+                # www.dream-multimedia-tv.de, www.heise.de, www.google.de
+               return system("ping -c 1 82.149.226.170") == 0 or \
+                       system("ping -c 1 193.99.144.85") == 0 or \
+                       system("ping -c 1 209.85.135.103") == 0
 
        def restartNetwork(self):
                iNetwork.deactivateNetworkConfig()
@@ -324,6 +334,12 @@ class Network:
                self.container.appClosed.get().remove(self.cmdFinished)
                self.container.dataAvail.get().remove(self.dataAvail)
 
+       def stopContainer(self):
+               self.container.kill()
+               
+       def ContainerRunning(self):
+               return self.container.running()
+
        def checkforInterface(self,iface):
                if self.getAdapterAttribute(iface, 'up') is True:
                        return True
@@ -334,26 +350,15 @@ class Network:
                                return True
                        else:
                                return False
-               
+
        def checkDNSLookup(self):
-               ok_counter = 0
-               ret1=system("nslookup www.dream-multimedia-tv.de")
-               if ret1 != 0:
-                       ok_counter = ok_counter + 1
-               ret2=system("nslookup www.heise.de")
-               if ret2 != 0:
-                       ok_counter = ok_counter + 1
-               ret3=system("nslookup www.google.de")
-               if ret2 != 0:
-                       ok_counter = ok_counter + 1             
-               if ok_counter <= 2:
-                       return True
-               else:
-                       return False
-               
+               return system("nslookup www.dream-multimedia-tv.de") == 0 or \
+                       system("nslookup www.heise.de") == 0 or \
+                       system("nslookup www.google.de")
+
        def deactivateInterface(self,iface):
                system("ifconfig " + iface + " down")
-               
+
        def detectWlanModule(self):
                self.wlanmodule = None
                rt73_dir = "/sys/bus/usb/drivers/rt73/"
@@ -363,13 +368,13 @@ class Network:
                        files = listdir(madwifi_dir)
                        if len(files) >= 1:
                                self.wlanmodule = 'madwifi'
-               elif os_path.exists(rt73_dir):
-                       files = listdir(rt73_dir)
-                       if len(files) >= 1:
+               if os_path.exists(rt73_dir):
+                       rtfiles = listdir(rt73_dir)
+                       if len(rtfiles) == 2:
                                self.wlanmodule = 'ralink'
-               elif os_path.exists(zd1211b_dir):
-                       files = listdir(zd1211b_dir)
-                       if len(files) != 0:
+               if os_path.exists(zd1211b_dir):
+                       zdfiles = listdir(zd1211b_dir)
+                       if len(zdfiles) == 1:
                                self.wlanmodule = 'zydas'
                return self.wlanmodule