Merge branch 'bug_474_fix_multicontent_clear'
[enigma2.git] / lib / python / Components / Network.py
index 7cc61d847516da7cfef0b0995ddbde81c3f4ef1e..e980cb8c2f0dcd5a68a840297f1f8c0c323edcc8 100755 (executable)
@@ -3,6 +3,9 @@ from re import compile as re_compile, search as re_search
 from socket import *
 from enigma import eConsoleAppContainer
 from Components.Console import Console
+from Components.PluginComponent import plugins
+from Components.About import about
+from Plugins.Plugin import PluginDescriptor
 
 class Network:
        def __init__(self):
@@ -12,7 +15,7 @@ class Network:
                self.NetworkState = 0
                self.DnsState = 0
                self.nameservers = []
-               self.ethtool_bin = "/usr/sbin/ethtool"
+               self.ethtool_bin = "ethtool"
                self.container = eConsoleAppContainer()
                self.Console = Console()
                self.LinkConsole = Console()
@@ -22,8 +25,23 @@ class Network:
                self.activateConsole = Console()
                self.resetNetworkConsole = Console()
                self.DnsConsole = Console()
+               self.PingConsole = Console()
+               self.config_ready = None
+               self.friendlyNames = {}
+               self.lan_interfaces = []
+               self.wlan_interfaces = []
                self.getInterfaces()
 
+       def onRemoteRootFS(self):
+               fp = file('/proc/mounts', 'r')
+               mounts = fp.readlines()
+               fp.close()
+               for line in mounts:
+                       parts = line.strip().split(' ')
+                       if parts[1] == '/' and (parts[2] == 'nfs' or parts[2] == 'smbfs'):
+                               return True
+               return False
+
        def getInterfaces(self, callback = None):
                devicesPattern = re_compile('[a-z]+[0-9]+')
                self.configuredInterfaces = []
@@ -33,7 +51,7 @@ class Network:
                for line in result:
                        try:
                                device = devicesPattern.search(line).group()
-                               if device == 'wifi0':
+                               if device in ('wifi0', 'wmaster0'):
                                        continue
                                self.getDataForInterface(device, callback)
                        except AttributeError:
@@ -63,6 +81,8 @@ class Network:
 
        def getDataForInterface(self, iface,callback):
                #get ip out of ip addr, as avahi sometimes overrides it in ifconfig.
+               if not self.Console:
+                       self.Console = Console()
                cmd = "ip -o addr"
                self.Console.ePopen(cmd, self.IPaddrFinished, [iface,callback])
 
@@ -201,7 +221,7 @@ class Network:
                                        ifaces[currif]["gateway"] = map(int, split[1].split('.'))
                                        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('.'))                                  
+                                                       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
@@ -212,15 +232,18 @@ class Network:
                for ifacename, iface in ifaces.items():
                        if self.ifaces.has_key(ifacename):
                                self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
-               if len(self.Console.appContainers) == 0:
-                       # save configured interfacelist
-                       self.configuredNetworkAdapters = self.configuredInterfaces
-                       # load ns only once     
-                       self.loadNameserverConfig()
-                       print "read configured interfac:", ifaces
-                       print "self.ifaces after loading:", self.ifaces
-                       if callback is not None:
-                               callback(True)
+               if self.Console:
+                       if len(self.Console.appContainers) == 0:
+                               # save configured interfacelist
+                               self.configuredNetworkAdapters = self.configuredInterfaces
+                               # load ns only once     
+                               self.loadNameserverConfig()
+                               print "read configured interface:", ifaces
+                               print "self.ifaces after loading:", self.ifaces
+                               self.config_ready = True
+                               self.msgPlugins()
+                               if callback is not None:
+                                       callback(True)
 
        def loadNameserverConfig(self):
                ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
@@ -245,6 +268,10 @@ class Network:
                print "nameservers:", self.nameservers
 
        def deactivateNetworkConfig(self, callback = None):
+               if self.onRemoteRootFS():
+                       if callback is not None:
+                               callback(True)
+                       return
                self.deactivateConsole = Console()
                self.commands = []
                self.commands.append("/etc/init.d/avahi-daemon stop")
@@ -263,6 +290,10 @@ class Network:
                                callback(True)
 
        def activateNetworkConfig(self, callback = None):
+               if self.onRemoteRootFS():
+                       if callback is not None:
+                               callback(True)
+                       return
                self.activateConsole = Console()
                self.commands = []
                self.commands.append("/etc/init.d/networking start")
@@ -282,13 +313,49 @@ class Network:
                return len(self.ifaces)
 
        def getFriendlyAdapterName(self, x):
-               # maybe this needs to be replaced by an external list.
-               friendlyNames = {
-                       "eth0": _("Integrated Ethernet"),
-                       "wlan0": _("Wireless"),
-                       "ath0": _("Integrated Wireless")
-               }
-               return friendlyNames.get(x, x) # when we have no friendly name, use adapter name
+               if x in self.friendlyNames.keys():
+                       return self.friendlyNames.get(x, x)
+               else:
+                       self.friendlyNames[x] = self.getFriendlyAdapterNaming(x)
+                       return self.friendlyNames.get(x, x) # when we have no friendly name, use adapter name
+
+       def getFriendlyAdapterNaming(self, iface):
+               if iface.startswith('eth'):
+                       if iface not in self.lan_interfaces and len(self.lan_interfaces) == 0:
+                               self.lan_interfaces.append(iface)
+                               return _("LAN connection")
+                       elif iface not in self.lan_interfaces and len(self.lan_interfaces) >= 1:
+                               self.lan_interfaces.append(iface)
+                               return _("LAN connection") + " " + str(len(self.lan_interfaces))
+               else:
+                       if iface not in self.wlan_interfaces and len(self.wlan_interfaces) == 0:
+                               self.wlan_interfaces.append(iface)
+                               return _("WLAN connection")
+                       elif iface not in self.wlan_interfaces and len(self.wlan_interfaces) >= 1:
+                               self.wlan_interfaces.append(iface)
+                               return _("WLAN connection") + " " + str(len(self.wlan_interfaces))
+
+       def getFriendlyAdapterDescription(self, iface):
+               if iface == 'eth0':
+                       return _("Internal LAN adapter.")
+               else:
+                       classdir = "/sys/class/net/" + iface + "/device/"
+                       driverdir = "/sys/class/net/" + iface + "/device/driver/"
+                       if os_path.exists(classdir):
+                               files = listdir(classdir)
+                               if 'driver' in files:
+                                       if os_path.realpath(driverdir).endswith('ath_pci'):
+                                               return _("Atheros")+ " " + str(os_path.basename(os_path.realpath(driverdir))) + " " + _("WLAN adapter.") 
+                                       elif os_path.realpath(driverdir).endswith('zd1211b'):
+                                               return _("Zydas")+ " " + str(os_path.basename(os_path.realpath(driverdir))) + " " + _("WLAN adapter.") 
+                                       elif os_path.realpath(driverdir).endswith('rt73'):
+                                               return _("Ralink")+ " " + str(os_path.basename(os_path.realpath(driverdir))) + " " + _("WLAN adapter.") 
+                                       elif os_path.realpath(driverdir).endswith('rt73usb'):
+                                               return _("Ralink")+ " " + str(os_path.basename(os_path.realpath(driverdir))) + " " + _("WLAN adapter.") 
+                                       else:
+                                               return str(os_path.basename(os_path.realpath(driverdir))) + " " + _("WLAN adapter.") 
+                               else:
+                                       return _("Unknown network adapter.")
 
        def getAdapterName(self, iface):
                return iface
@@ -336,6 +403,10 @@ class Network:
                                        self.nameservers[i] = newnameserver
 
        def resetNetworkConfig(self, mode='lan', callback = None):
+               if self.onRemoteRootFS():
+                       if callback is not None:
+                               callback(True)
+                       return
                self.resetNetworkConsole = Console()
                self.commands = []
                self.commands.append("/etc/init.d/avahi-daemon stop")
@@ -394,6 +465,7 @@ class Network:
 
        def checkNetworkState(self,statecallback):
                # www.dream-multimedia-tv.de, www.heise.de, www.google.de
+               self.NetworkState = 0
                cmd1 = "ping -c 1 82.149.226.170"
                cmd2 = "ping -c 1 193.99.144.85"
                cmd3 = "ping -c 1 209.85.135.103"
@@ -414,7 +486,13 @@ class Network:
                                        statecallback(self.NetworkState)
                
        def restartNetwork(self,callback = None):
+               if self.onRemoteRootFS():
+                       if callback is not None:
+                               callback(True)
+                       return
                self.restartConsole = Console()
+               self.config_ready = False
+               self.msgPlugins()
                self.commands = []
                self.commands.append("/etc/init.d/avahi-daemon stop")
                for iface in self.ifaces.keys():
@@ -439,30 +517,47 @@ class Network:
 
        def getLinkStateFinished(self, result, retval,extra_args):
                (callback) = extra_args
+
                if self.LinkConsole is not None:
                        if len(self.LinkConsole.appContainers) == 0:
                                callback(result)
                        
+       def stopPingConsole(self):
+               if self.PingConsole is not None:
+                       if len(self.PingConsole.appContainers):
+                               for name in self.PingConsole.appContainers.keys():
+                                       self.PingConsole.kill(name)
+
        def stopLinkStateConsole(self):
                if self.LinkConsole is not None:
-                       self.LinkConsole = None
-
+                       if len(self.LinkConsole.appContainers):
+                               for name in self.LinkConsole.appContainers.keys():
+                                       self.LinkConsole.kill(name)
+                                       
        def stopDNSConsole(self):
                if self.DnsConsole is not None:
-                       self.DnsConsole = None
-
+                       if len(self.DnsConsole.appContainers):
+                               for name in self.DnsConsole.appContainers.keys():
+                                       self.DnsConsole.kill(name)
+                                       
        def stopRestartConsole(self):
                if self.restartConsole is not None:
-                       self.restartConsole = None
-                       
+                       if len(self.restartConsole.appContainers):
+                               for name in self.restartConsole.appContainers.keys():
+                                       self.restartConsole.kill(name)
+                                       
        def stopGetInterfacesConsole(self):
                if self.Console is not None:
-                       self.Console = None
-
+                       if len(self.Console.appContainers):
+                               for name in self.Console.appContainers.keys():
+                                       self.Console.kill(name)
+                                       
        def stopDeactivateInterfaceConsole(self):
-               if self.deactivateInterfaceConsole:
-                       self.deactivateInterfaceConsole = None
-                       
+               if self.deactivateInterfaceConsole is not None:
+                       if len(self.deactivateInterfaceConsole.appContainers):
+                               for name in self.deactivateInterfaceConsole.appContainers.keys():
+                                       self.deactivateInterfaceConsole.kill(name)
+                                       
        def checkforInterface(self,iface):
                if self.getAdapterAttribute(iface, 'up') is True:
                        return True
@@ -495,6 +590,10 @@ class Network:
                                        statecallback(self.DnsState)
 
        def deactivateInterface(self,iface,callback = None):
+               if self.onRemoteRootFS():
+                       if callback is not None:
+                               callback(True)
+                       return
                self.deactivateInterfaceConsole = Console()
                self.commands = []
                cmd1 = "ip addr flush " + iface
@@ -510,24 +609,39 @@ class Network:
                                if callback is not None:
                                        callback(True)
 
-       def detectWlanModule(self):
+       def detectWlanModule(self, iface = None):
                self.wlanmodule = None
-               rt73_dir = "/sys/bus/usb/drivers/rt73/"
-               zd1211b_dir = "/sys/bus/usb/drivers/zd1211b/"
-               madwifi_dir = "/sys/bus/pci/drivers/ath_pci/"
-               if os_path.exists(madwifi_dir):
-                       files = listdir(madwifi_dir)
-                       if len(files) >= 1:
-                               self.wlanmodule = 'madwifi'
-               if os_path.exists(rt73_dir):
-                       rtfiles = listdir(rt73_dir)
-                       if len(rtfiles) == 2:
-                               self.wlanmodule = 'ralink'
-               if os_path.exists(zd1211b_dir):
-                       zdfiles = listdir(zd1211b_dir)
-                       if len(zdfiles) == 1:
-                               self.wlanmodule = 'zydas'
-               return self.wlanmodule
+               classdir = "/sys/class/net/" + iface + "/device/"
+               driverdir = "/sys/class/net/" + iface + "/device/driver/"
+               if os_path.exists(classdir):
+                       classfiles = listdir(classdir)
+                       driver_found = False
+                       nl80211_found = False
+                       for x in classfiles:
+                               if x == 'driver':
+                                       driver_found = True
+                               if x.startswith('ieee80211:'):
+                                       nl80211_found = True
+
+                       if driver_found and nl80211_found:
+                               #print about.getKernelVersionString()
+                               self.wlanmodule = "nl80211"
+                       else:
+                               if driver_found and not nl80211_found:
+                                       driverfiles = listdir(driverdir)
+                                       if os_path.realpath(driverdir).endswith('ath_pci'):
+                                               if len(driverfiles) >= 1:
+                                                       self.wlanmodule = 'madwifi'
+                                       if os_path.realpath(driverdir).endswith('rt73'):
+                                               if len(driverfiles) == 2 or len(driverfiles) == 5:
+                                                       self.wlanmodule = 'ralink'                                      
+                                       if os_path.realpath(driverdir).endswith('zd1211b'):
+                                               if len(driverfiles) == 1 or len(driverfiles) == 5:
+                                                       self.wlanmodule = 'zydas'
+                       if self.wlanmodule is None:
+                               self.wlanmodule = "wext"
+                       print 'Using "%s" as wpa-supplicant driver' % (self.wlanmodule)
+                       return self.wlanmodule
        
        def calc_netmask(self,nmask):
                from struct import pack, unpack
@@ -544,6 +658,11 @@ class Network:
                        netmask = str(inet_ntoa(pack('>L', nm)))
                        return netmask
        
+       def msgPlugins(self):
+               if self.config_ready is not None:
+                       for p in plugins.getPlugins(PluginDescriptor.WHERE_NETWORKCONFIG_READ):
+                               p(reason=self.config_ready)
+       
 iNetwork = Network()
 
 def InitNetwork():