Fix network test.
[vuplus_dvbapp] / lib / python / Components / Network.py
1 from os import system, popen, path as os_path, listdir
2 from re import compile as re_compile, search as re_search
3 from socket import *
4 from enigma import eConsoleAppContainer
5 from Components.Console import Console
6 from Components.PluginComponent import plugins
7 from Components.About import about
8 from Plugins.Plugin import PluginDescriptor
9
10 class Network:
11         def __init__(self):
12                 self.ifaces = {}
13                 self.configuredInterfaces = []
14                 self.configuredNetworkAdapters = []
15                 self.NetworkState = 0
16                 self.DnsState = 0
17                 self.nameservers = []
18                 self.ethtool_bin = "ethtool"
19                 self.container = eConsoleAppContainer()
20                 self.Console = Console()
21                 self.LinkConsole = Console()
22                 self.restartConsole = Console()
23                 self.deactivateInterfaceConsole = Console()
24                 self.activateInterfaceConsole = Console()
25                 self.resetNetworkConsole = Console()
26                 self.DnsConsole = Console()
27                 self.PingConsole = Console()
28                 self.config_ready = None
29                 self.friendlyNames = {}
30                 self.lan_interfaces = []
31                 self.wlan_interfaces = []
32                 self.remoteRootFS = None
33                 self.getInterfaces()
34
35         def onRemoteRootFS(self):
36                 if self.remoteRootFS == None:
37                         fp = file('/proc/mounts', 'r')
38                         mounts = fp.readlines()
39                         fp.close()
40                         self.remoteRootFS = False
41                         for line in mounts:
42                                 parts = line.strip().split()
43                                 if parts[1] == '/' and parts[2] == 'nfs':
44                                         self.remoteRootFS = True
45                                         break
46                 return self.remoteRootFS
47
48         def isBlacklisted(self, iface):
49                 return iface in ('lo', 'wifi0', 'wmaster0', 'sys0')
50
51         def getInterfaces(self, callback = None):
52                 self.configuredInterfaces = []
53                 for device in self.getInstalledAdapters():
54                         self.getAddrInet(device, callback)
55
56         # helper function
57         def regExpMatch(self, pattern, string):
58                 if string is None:
59                         return None
60                 try:
61                         return pattern.search(string).group()
62                 except AttributeError:
63                         return None
64
65         # helper function to convert ips from a sring to a list of ints
66         def convertIP(self, ip):
67                 return [ int(n) for n in ip.split('.') ]
68
69         def getAddrInet(self, iface, callback):
70                 if not self.Console:
71                         self.Console = Console()
72                 cmd = "ip -o addr show dev " + iface
73                 self.Console.ePopen(cmd, self.IPaddrFinished, [iface,callback])
74
75         def IPaddrFinished(self, result, retval, extra_args):
76                 (iface, callback ) = extra_args
77                 data = { 'up': False, 'dhcp': False, 'preup' : False, 'predown' : False, 'dns-nameservers' : False }
78                 globalIPpattern = re_compile("scope global")
79                 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
80                 netRegexp = '[0-9]{1,2}'
81                 macRegexp = '[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}'
82                 ipLinePattern = re_compile('inet ' + ipRegexp + '/')
83                 ipPattern = re_compile(ipRegexp)
84                 netmaskLinePattern = re_compile('/' + netRegexp)
85                 netmaskPattern = re_compile(netRegexp)
86                 bcastLinePattern = re_compile(' brd ' + ipRegexp)
87                 upPattern = re_compile('UP')
88                 macPattern = re_compile(macRegexp)
89                 macLinePattern = re_compile('link/ether ' + macRegexp)
90                 
91                 for line in result.splitlines():
92                         split = line.strip().split(' ',2)
93                         if (split[1][:-1] == iface) or (split[1][:-1] == (iface + '@sys0')):
94                                 up = self.regExpMatch(upPattern, split[2])
95                                 mac = self.regExpMatch(macPattern, self.regExpMatch(macLinePattern, split[2]))
96                                 if up is not None:
97                                         data['up'] = True
98                                         if iface is not 'lo':
99                                                 self.configuredInterfaces.append(iface)
100                                 if mac is not None:
101                                         data['mac'] = mac
102                         if (split[1] == iface):
103                                 if re_search(globalIPpattern, split[2]):
104                                         ip = self.regExpMatch(ipPattern, self.regExpMatch(ipLinePattern, split[2]))
105                                         netmask = self.calc_netmask(self.regExpMatch(netmaskPattern, self.regExpMatch(netmaskLinePattern, split[2])))
106                                         bcast = self.regExpMatch(ipPattern, self.regExpMatch(bcastLinePattern, split[2]))
107                                         if ip is not None:
108                                                 data['ip'] = self.convertIP(ip)
109                                         if netmask is not None:
110                                                 data['netmask'] = self.convertIP(netmask)
111                                         if bcast is not None:
112                                                 data['bcast'] = self.convertIP(bcast)
113                                                 
114                 if not data.has_key('ip'):
115                         data['dhcp'] = True
116                         data['ip'] = [0, 0, 0, 0]
117                         data['netmask'] = [0, 0, 0, 0]
118                         data['gateway'] = [0, 0, 0, 0]
119
120                 cmd = "route -n | grep  " + iface
121                 self.Console.ePopen(cmd,self.routeFinished, [iface, data, callback])
122
123         def routeFinished(self, result, retval, extra_args):
124                 (iface, data, callback) = extra_args
125                 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
126                 ipPattern = re_compile(ipRegexp)
127                 ipLinePattern = re_compile(ipRegexp)
128
129                 for line in result.splitlines():
130                         print line[0:7]
131                         if line[0:7] == "0.0.0.0":
132                                 gateway = self.regExpMatch(ipPattern, line[16:31])
133                                 if gateway:
134                                         data['gateway'] = self.convertIP(gateway)
135                                         
136                 self.ifaces[iface] = data
137                 self.loadNetworkConfig(iface,callback)
138
139         def writeNetworkConfig(self):
140                 self.configuredInterfaces = []
141                 fp = file('/etc/network/interfaces', 'w')
142                 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
143                 fp.write("auto lo\n")
144                 fp.write("iface lo inet loopback\n\n")
145                 for ifacename, iface in self.ifaces.items():
146                         if iface['up'] == True:
147                                 fp.write("auto " + ifacename + "\n")
148                                 self.configuredInterfaces.append(ifacename)
149                         if iface['dhcp'] == True:
150                                 fp.write("iface "+ ifacename +" inet dhcp\n")
151                         if iface['dhcp'] == False:
152                                 fp.write("iface "+ ifacename +" inet static\n")
153                                 if iface.has_key('ip'):
154                                         print tuple(iface['ip'])
155                                         fp.write("      address %d.%d.%d.%d\n" % tuple(iface['ip']))
156                                         fp.write("      netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
157                                         if iface.has_key('gateway'):
158                                                 fp.write("      gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
159                         if iface.has_key("configStrings"):
160                                 fp.write(iface["configStrings"])
161                         if iface["preup"] is not False and not iface.has_key("configStrings"):
162                                 fp.write(iface["preup"])
163                         if iface["predown"] is not False and not iface.has_key("configStrings"):
164                                 fp.write(iface["predown"])
165                         if iface["dns-nameservers"] is not False and len(iface["dns-nameservers"])>0:
166                                 fp.write("%s" % (iface["dns-nameservers"]))
167                         fp.write("\n")                          
168                 fp.close()
169                 self.configuredNetworkAdapters = self.configuredInterfaces
170 #               self.writeNameserverConfig()
171
172         def writeNameserverConfig(self):
173                 fp = file('/etc/resolv.conf', 'w')
174                 for nameserver in self.nameservers:
175                         fp.write("nameserver %d.%d.%d.%d\n" % tuple(nameserver))
176                 fp.close()
177
178         def loadNetworkConfig(self,iface,callback = None):
179                 interfaces = []
180                 # parse the interfaces-file
181                 try:
182                         fp = file('/etc/network/interfaces', 'r')
183                         interfaces = fp.readlines()
184                         fp.close()
185                 except:
186                         print "[Network.py] interfaces - opening failed"
187
188                 ifaces = {}
189                 currif = ""
190                 for i in interfaces:
191                         split = i.strip().split(' ')
192                         if (split[0] == "iface"):
193                                 currif = split[1]
194                                 ifaces[currif] = {}
195                                 if (len(split) == 4 and split[3] == "dhcp"):
196                                         ifaces[currif]["dhcp"] = True
197                                 else:
198                                         ifaces[currif]["dhcp"] = False
199                         if (currif == iface): #read information only for available interfaces
200                                 if (split[0] == "address"):
201                                         ifaces[currif]["address"] = map(int, split[1].split('.'))
202                                         if self.ifaces[currif].has_key("ip"):
203                                                 if self.ifaces[currif]["ip"] != ifaces[currif]["address"] and ifaces[currif]["dhcp"] == False:
204                                                         self.ifaces[currif]["ip"] = map(int, split[1].split('.'))
205                                 if (split[0] == "netmask"):
206                                         ifaces[currif]["netmask"] = map(int, split[1].split('.'))
207                                         if self.ifaces[currif].has_key("netmask"):
208                                                 if self.ifaces[currif]["netmask"] != ifaces[currif]["netmask"] and ifaces[currif]["dhcp"] == False:
209                                                         self.ifaces[currif]["netmask"] = map(int, split[1].split('.'))
210                                 if (split[0] == "gateway"):
211                                         ifaces[currif]["gateway"] = map(int, split[1].split('.'))
212                                         if self.ifaces[currif].has_key("gateway"):
213                                                 if self.ifaces[currif]["gateway"] != ifaces[currif]["gateway"] and ifaces[currif]["dhcp"] == False:
214                                                         self.ifaces[currif]["gateway"] = map(int, split[1].split('.'))
215                                 if (split[0] == "pre-up"):
216                                         if self.ifaces[currif].has_key("preup"):
217                                                 self.ifaces[currif]["preup"] = i
218                                 if (split[0] in ("pre-down","post-down")):
219                                         if self.ifaces[currif].has_key("predown"):
220                                                 self.ifaces[currif]["predown"] = i
221                                 if (split[0] == "dns-nameservers"):
222                                         if self.ifaces[currif].has_key("dns-nameservers"):
223                                                 self.ifaces[currif]["dns-nameservers"] = i
224
225                 for ifacename, iface in ifaces.items():
226                         if self.ifaces.has_key(ifacename):
227                                 self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
228                 if self.Console:
229                         if len(self.Console.appContainers) == 0:
230                                 # save configured interfacelist
231                                 self.configuredNetworkAdapters = self.configuredInterfaces
232                                 # load ns only once     
233                                 self.loadNameserverConfig()
234                                 print "read configured interface:", ifaces
235                                 print "self.ifaces after loading:", self.ifaces
236                                 self.config_ready = True
237                                 self.msgPlugins()
238                                 if callback is not None:
239                                         callback(True)
240
241         def loadNameserverConfig(self):
242                 ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
243                 nameserverPattern = re_compile("nameserver +" + ipRegexp)
244                 ipPattern = re_compile(ipRegexp)
245
246                 resolv = []
247                 try:
248                         fp = file('/etc/resolv.conf', 'r')
249                         resolv = fp.readlines()
250                         fp.close()
251                         self.nameservers = []
252                 except:
253                         print "[Network.py] resolv.conf - opening failed"
254
255                 for line in resolv:
256                         if self.regExpMatch(nameserverPattern, line) is not None:
257                                 ip = self.regExpMatch(ipPattern, line)
258                                 if ip:
259                                         self.nameservers.append(self.convertIP(ip))
260
261                 print "nameservers:", self.nameservers
262
263         def getInstalledAdapters(self):
264                 return [x for x in listdir('/sys/class/net') if not self.isBlacklisted(x)]
265
266         def getConfiguredAdapters(self):
267                 return self.configuredNetworkAdapters
268
269         def getNumberOfAdapters(self):
270                 return len(self.ifaces)
271
272         def getFriendlyAdapterName(self, x):
273                 if x in self.friendlyNames.keys():
274                         return self.friendlyNames.get(x, x)
275                 self.friendlyNames[x] = self.getFriendlyAdapterNaming(x)
276                 return self.friendlyNames.get(x, x) # when we have no friendly name, use adapter name
277
278         def getFriendlyAdapterNaming(self, iface):
279                 name = None
280                 if self.isWirelessInterface(iface):
281                         if iface not in self.wlan_interfaces:
282                                 name = _("WLAN connection")
283                                 if len(self.wlan_interfaces):
284                                         name += " " + str(len(self.wlan_interfaces)+1)
285                                 self.wlan_interfaces.append(iface)                                                              
286                 else:
287                         if iface not in self.lan_interfaces:
288                                 name = _("LAN connection")
289                                 if len(self.lan_interfaces):
290                                         name += " " + str(len(self.lan_interfaces)+1)
291                                 self.lan_interfaces.append(iface)
292                 return name
293
294         def useWlCommand(self, iface):
295                 return iface and os_path.exists("/tmp/bcm/%s" % iface)
296         
297         def getFriendlyAdapterDescription(self, iface):
298                 if not self.isWirelessInterface(iface):
299                         return _('Ethernet network interface')
300
301                 moduledir = self.getWlanModuleDir(iface)
302                 if moduledir:
303                         name = os_path.basename(os_path.realpath(moduledir))
304                         if name in ('ath_pci', 'ath5k'):
305                                 name = 'Atheros'
306                         elif name in ('rt73','rt73usb','rt3070sta', 'rt5370sta'):
307                                 name = 'Ralink'
308                         elif name == 'zd1211b':
309                                 name = 'Zydas'
310                         elif name in ('r871x_usb_drv', 'rtw_usb_drv'):
311                                 name = 'Realtek'
312                         elif self.isRalinkModule(iface):
313                                 name = 'Ralink'
314                 elif self.useWlCommand(iface):
315                         name = 'BroadCom'
316                 else:
317                         name = _('Unknown')
318
319                 return name + ' ' + _('wireless network interface')
320
321         def isRalinkModule(self, iface):
322                 import os
323 # check vendor ID for lagacy driver
324                 vendorID = "148f" # ralink vendor ID
325                 idVendorPath = "/sys/class/net/%s/device/idVendor" % iface
326                 if os.access(idVendorPath, os.R_OK):
327                         if open(idVendorPath, "r").read().strip() == vendorID:
328                                 return True
329
330 # check sys driver path for kernel driver
331                 ralinkKmod = "rt2800usb" # ralink kernel driver name
332                 driverPath = "/sys/class/net/%s/device/driver/" % iface
333                 if os.path.exists(driverPath):
334                         driverName = os.path.basename(os_path.realpath(driverPath))
335                         if driverName == ralinkKmod:
336                                 return True
337                 return False
338
339         def getAdapterName(self, iface):
340                 return iface
341
342         def getAdapterList(self):
343                 return self.ifaces.keys()
344
345         def getAdapterAttribute(self, iface, attribute):
346                 if self.ifaces.has_key(iface):
347                         if self.ifaces[iface].has_key(attribute):
348                                 return self.ifaces[iface][attribute]
349                 return None
350
351         def setAdapterAttribute(self, iface, attribute, value):
352                 print "setting for adapter", iface, "attribute", attribute, " to value", value
353                 if self.ifaces.has_key(iface):
354                         self.ifaces[iface][attribute] = value
355
356         def removeAdapterAttribute(self, iface, attribute):
357                 if self.ifaces.has_key(iface):
358                         if self.ifaces[iface].has_key(attribute):
359                                 del self.ifaces[iface][attribute]
360
361         def getNameserverList(self):
362                 if len(self.nameservers) == 0:
363                         return [[0, 0, 0, 0], [0, 0, 0, 0]]
364                 else: 
365                         return self.nameservers
366
367         def clearNameservers(self):
368                 self.nameservers = []
369
370         def addNameserver(self, nameserver):
371                 if nameserver not in self.nameservers:
372                         self.nameservers.append(nameserver)
373
374         def removeNameserver(self, nameserver):
375                 if nameserver in self.nameservers:
376                         self.nameservers.remove(nameserver)
377
378         def changeNameserver(self, oldnameserver, newnameserver):
379                 if oldnameserver in self.nameservers:
380                         for i in range(len(self.nameservers)):
381                                 if self.nameservers[i] == oldnameserver:
382                                         self.nameservers[i] = newnameserver
383
384         def resetNetworkConfig(self, mode='lan', callback = None):
385                 self.resetNetworkConsole = Console()
386                 self.commands = []
387                 self.commands.append("/etc/init.d/avahi-daemon stop")
388                 for iface in self.ifaces.keys():
389                         if iface != 'eth0' or not self.onRemoteRootFS():
390                                 self.commands.append("ip addr flush dev " + iface)      
391                 self.commands.append("/etc/init.d/networking stop")
392                 self.commands.append("killall -9 udhcpc")
393                 self.commands.append("rm /var/run/udhcpc*")
394                 self.resetNetworkConsole.eBatch(self.commands, self.resetNetworkFinishedCB, [mode, callback], debug=True)
395
396         def resetNetworkFinishedCB(self, extra_args):
397                 (mode, callback) = extra_args
398                 if len(self.resetNetworkConsole.appContainers) == 0:
399                         self.writeDefaultNetworkConfig(mode, callback)
400
401         def writeDefaultNetworkConfig(self,mode='lan', callback = None):
402                 fp = file('/etc/network/interfaces', 'w')
403                 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
404                 fp.write("auto lo\n")
405                 fp.write("iface lo inet loopback\n\n")
406                 if mode == 'wlan':
407                         fp.write("auto wlan0\n")
408                         fp.write("iface wlan0 inet dhcp\n")
409                 if mode == 'wlan-mpci':
410                         fp.write("auto ath0\n")
411                         fp.write("iface ath0 inet dhcp\n")
412                 if mode == 'lan':
413                         fp.write("auto eth0\n")
414                         fp.write("iface eth0 inet dhcp\n")
415                 fp.write("\n")
416                 fp.close()
417
418                 self.resetNetworkConsole = Console()
419                 self.commands = []
420                 if mode == 'wlan':
421                         self.commands.append("ifconfig eth0 down")
422                         self.commands.append("ifconfig ath0 down")
423                         self.commands.append("ifconfig wlan0 up")
424                 if mode == 'wlan-mpci':
425                         self.commands.append("ifconfig eth0 down")
426                         self.commands.append("ifconfig wlan0 down")
427                         self.commands.append("ifconfig ath0 up")                
428                 if mode == 'lan':                       
429                         self.commands.append("ifconfig eth0 up")
430                         self.commands.append("ifconfig wlan0 down")
431                         self.commands.append("ifconfig ath0 down")
432                 self.commands.append("/etc/init.d/avahi-daemon start")  
433                 self.resetNetworkConsole.eBatch(self.commands, self.resetNetworkFinished, [mode,callback], debug=True)  
434
435         def resetNetworkFinished(self,extra_args):
436                 (mode, callback) = extra_args
437                 if len(self.resetNetworkConsole.appContainers) == 0:
438                         if callback is not None:
439                                 callback(True,mode)
440
441         def checkNetworkState(self,statecallback):
442                 self.NetworkState = 0
443                 self.PingConsole = Console()
444                 for testserver in ("www.heise.de", "www.google.de", "de.yahoo.com"):
445                         self.PingConsole.ePopen("ping -c 1 %s" % testserver, self.checkNetworkStateFinished,statecallback)
446                 
447         def checkNetworkStateFinished(self, result, retval,extra_args):
448                 (statecallback) = extra_args
449                 if self.PingConsole is not None:
450                         if retval == 0:
451                                 self.PingConsole = None
452                                 statecallback(self.NetworkState)
453                         else:
454                                 self.NetworkState += 1
455                                 if len(self.PingConsole.appContainers) == 0:
456                                         statecallback(self.NetworkState)
457                 
458         def restartNetwork(self,callback = None):
459                 self.restartConsole = Console()
460                 self.config_ready = False
461                 self.msgPlugins()
462                 self.commands = []
463                 self.commands.append("/etc/init.d/avahi-daemon stop")
464                 for iface in self.ifaces.keys():
465                         if iface != 'eth0' or not self.onRemoteRootFS():
466                                 self.commands.append("ifdown " + iface)
467                                 self.commands.append("ip addr flush dev " + iface)
468                 self.commands.append("/etc/init.d/networking stop")
469                 self.commands.append("killall -9 udhcpc")
470                 self.commands.append("rm /var/run/udhcpc*")
471                 self.commands.append("/etc/init.d/networking start")
472                 self.commands.append("/etc/init.d/avahi-daemon start")
473                 self.restartConsole.eBatch(self.commands, self.restartNetworkFinished, callback, debug=True)
474         
475         def restartNetworkFinished(self,extra_args):
476                 ( callback ) = extra_args
477                 if callback is not None:
478                         callback(True)
479
480         def getLinkState(self,iface,callback):
481                 cmd = self.ethtool_bin + " " + iface
482                 self.LinkConsole = Console()
483                 self.LinkConsole.ePopen(cmd, self.getLinkStateFinished,callback)
484
485         def getLinkStateFinished(self, result, retval,extra_args):
486                 (callback) = extra_args
487
488                 if self.LinkConsole is not None:
489                         if len(self.LinkConsole.appContainers) == 0:
490                                 callback(result)
491                         
492         def stopPingConsole(self):
493                 if self.PingConsole is not None:
494                         if len(self.PingConsole.appContainers):
495                                 for name in self.PingConsole.appContainers.keys():
496                                         self.PingConsole.kill(name)
497
498         def stopLinkStateConsole(self):
499                 if self.LinkConsole is not None:
500                         if len(self.LinkConsole.appContainers):
501                                 for name in self.LinkConsole.appContainers.keys():
502                                         self.LinkConsole.kill(name)
503                                         
504         def stopDNSConsole(self):
505                 if self.DnsConsole is not None:
506                         if len(self.DnsConsole.appContainers):
507                                 for name in self.DnsConsole.appContainers.keys():
508                                         self.DnsConsole.kill(name)
509                                         
510         def stopRestartConsole(self):
511                 if self.restartConsole is not None:
512                         if len(self.restartConsole.appContainers):
513                                 for name in self.restartConsole.appContainers.keys():
514                                         self.restartConsole.kill(name)
515                                         
516         def stopGetInterfacesConsole(self):
517                 if self.Console is not None:
518                         if len(self.Console.appContainers):
519                                 for name in self.Console.appContainers.keys():
520                                         self.Console.kill(name)
521                                         
522         def stopDeactivateInterfaceConsole(self):
523                 if self.deactivateInterfaceConsole is not None:
524                         self.deactivateInterfaceConsole.killAll()
525                         self.deactivateInterfaceConsole = None
526
527         def stopActivateInterfaceConsole(self):
528                 if self.activateInterfaceConsole is not None:
529                         self.activateInterfaceConsole.killAll()
530                         self.activateInterfaceConsole = None
531                                         
532         def checkforInterface(self,iface):
533                 if self.getAdapterAttribute(iface, 'up') is True:
534                         return True
535                 else:
536                         ret=system("ifconfig " + iface + " up")
537                         system("ifconfig " + iface + " down")
538                         if ret == 0:
539                                 return True
540                         else:
541                                 return False
542
543         def checkDNSLookup(self,statecallback):
544                 cmd1 = "nslookup www.dream-multimedia-tv.de"
545                 cmd2 = "nslookup www.heise.de"
546                 cmd3 = "nslookup www.google.de"
547                 self.DnsConsole = Console()
548                 self.DnsConsole.ePopen(cmd1, self.checkDNSLookupFinished,statecallback)
549                 self.DnsConsole.ePopen(cmd2, self.checkDNSLookupFinished,statecallback)
550                 self.DnsConsole.ePopen(cmd3, self.checkDNSLookupFinished,statecallback)
551                 
552         def checkDNSLookupFinished(self, result, retval,extra_args):
553                 (statecallback) = extra_args
554                 if self.DnsConsole is not None:
555                         if retval == 0:
556                                 self.DnsConsole = None
557                                 statecallback(self.DnsState)
558                         else:
559                                 self.DnsState += 1
560                                 if len(self.DnsConsole.appContainers) == 0:
561                                         statecallback(self.DnsState)
562
563         def deactivateInterface(self,ifaces,callback = None):
564                 self.config_ready = False
565                 self.msgPlugins()
566                 commands = []
567                 def buildCommands(iface):
568                         commands.append("ifdown " + iface)
569                         commands.append("ip addr flush dev " + iface)
570                         #wpa_supplicant sometimes doesn't quit properly on SIGTERM
571                         if os_path.exists('/var/run/wpa_supplicant/'+ iface):
572                                 commands.append("wpa_cli -i" + iface + " terminate")
573                         
574                 if not self.deactivateInterfaceConsole:
575                         self.deactivateInterfaceConsole = Console()
576
577                 if isinstance(ifaces, (list, tuple)):
578                         for iface in ifaces:
579                                 if iface != 'eth0' or not self.onRemoteRootFS():
580                                         buildCommands(iface)
581                 else:
582                         if ifaces == 'eth0' and self.onRemoteRootFS():
583                                 if callback is not None:
584                                         callback(True)
585                                 return
586                         buildCommands(ifaces)
587                 self.deactivateInterfaceConsole.eBatch(commands, self.deactivateInterfaceFinished, [ifaces,callback], debug=True)
588
589         def deactivateInterfaceFinished(self,extra_args):
590                 (ifaces, callback) = extra_args
591                 def checkCommandResult(iface):
592                         if self.deactivateInterfaceConsole and self.deactivateInterfaceConsole.appResults.has_key("ifdown " + iface):
593                                 result = str(self.deactivateInterfaceConsole.appResults.get("ifdown " + iface)).strip("\n")
594                                 if result == "ifdown: interface " + iface + " not configured":
595                                         return False
596                                 else:
597                                         return True
598                 #ifdown sometimes can't get the interface down.
599                 if isinstance(ifaces, (list, tuple)):
600                         for iface in ifaces:
601                                 if checkCommandResult(iface) is False:
602                                         Console().ePopen(("ifconfig " + iface + " down" ))
603                 else:
604                         if checkCommandResult(ifaces) is False:
605                                 Console().ePopen(("ifconfig " + ifaces + " down" ))
606
607                 if self.deactivateInterfaceConsole:
608                         if len(self.deactivateInterfaceConsole.appContainers) == 0:
609                                 if callback is not None:
610                                         callback(True)
611
612         def activateInterface(self,iface,callback = None):
613                 if self.config_ready:
614                         self.config_ready = False
615                         self.msgPlugins()
616                 if iface == 'eth0' and self.onRemoteRootFS():
617                         if callback is not None:
618                                 callback(True)
619                         return
620                 if not self.activateInterfaceConsole:
621                         self.activateInterfaceConsole = Console()
622                 commands = []
623                 commands.append("ifup " + iface)
624                 self.activateInterfaceConsole.eBatch(commands, self.activateInterfaceFinished, callback, debug=True)
625
626         def activateInterfaceFinished(self,extra_args):
627                 callback = extra_args
628                 if self.activateInterfaceConsole:
629                         if len(self.activateInterfaceConsole.appContainers) == 0:
630                                 if callback is not None:
631                                         callback(True)
632
633         def sysfsPath(self, iface):
634                 return '/sys/class/net/' + iface
635
636         def isWirelessInterface(self, iface):
637                 if iface in self.wlan_interfaces:
638                         return True
639
640                 if os_path.isdir(self.sysfsPath(iface) + '/wireless'):
641                         return True
642
643                 # r871x_usb_drv on kernel 2.6.12 is not identifiable over /sys/class/net/'ifacename'/wireless so look also inside /proc/net/wireless
644                 device = re_compile('[a-z]{2,}[0-9]*:')
645                 ifnames = []
646                 fp = open('/proc/net/wireless', 'r')
647                 for line in fp:
648                         try:
649                                 ifnames.append(device.search(line).group()[:-1])
650                         except AttributeError:
651                                 pass
652                 if iface in ifnames:
653                         return True
654
655                 return False
656
657         def getWlanModuleDir(self, iface = None):
658                 devicedir = self.sysfsPath(iface) + '/device'
659                 if not os_path.exists(devicedir):
660                         return None
661
662                 moduledir = devicedir + '/driver/module'
663                 if os_path.isdir(moduledir):
664                         return moduledir
665
666                 # identification is not possible over default moduledir
667                 for x in listdir(devicedir):
668                         # rt3070 on kernel 2.6.18 registers wireless devices as usb_device (e.g. 1-1.3:1.0) and identification is only possible over /sys/class/net/'ifacename'/device/1-xxx
669                         if x.startswith("1-"):
670                                 moduledir = devicedir + '/' + x + '/driver/module'
671                                 if os_path.isdir(moduledir):
672                                         return moduledir
673                 # rt73, zd1211b, r871x_usb_drv on kernel 2.6.12 can be identified over /sys/class/net/'ifacename'/device/driver, so look also here
674                 moduledir = devicedir + '/driver'
675                 if os_path.isdir(moduledir):
676                         return moduledir
677
678                 return None
679
680         def detectWlanModule(self, iface = None):
681                 if not self.isWirelessInterface(iface):
682                         return None
683
684                 devicedir = self.sysfsPath(iface) + '/device'
685                 if os_path.isdir(devicedir + '/ieee80211'):
686                         return 'nl80211'
687
688                 moduledir = self.getWlanModuleDir(iface)
689                 if moduledir:
690                         module = os_path.basename(os_path.realpath(moduledir))
691                         if module in ('ath_pci','ath5k'):
692                                 return 'madwifi'
693                         if module in ('rt73','rt73'):
694                                 return 'ralink'
695                         if module == 'zd1211b':
696                                 return 'zydas'
697                 return 'wext'
698         
699         def calc_netmask(self,nmask):
700                 from struct import pack, unpack
701                 from socket import inet_ntoa, inet_aton
702                 mask = 1L<<31
703                 xnet = (1L<<32)-1
704                 cidr_range = range(0, 32)
705                 cidr = long(nmask)
706                 if cidr not in cidr_range:
707                         print 'cidr invalid: %d' % cidr
708                         return None
709                 else:
710                         nm = ((1L<<cidr)-1)<<(32-cidr)
711                         netmask = str(inet_ntoa(pack('>L', nm)))
712                         return netmask
713         
714         def msgPlugins(self):
715                 if self.config_ready is not None:
716                         for p in plugins.getPlugins(PluginDescriptor.WHERE_NETWORKCONFIG_READ):
717                                 p(reason=self.config_ready)
718
719         def getInterfacesNameserverList(self, iface):
720                 result = []
721                 nameservers = self.getAdapterAttribute(iface, "dns-nameservers")
722                 if nameservers:
723                         ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
724                         ipPattern = re_compile(ipRegexp)
725                         for x in nameservers.split()[1:]:
726                                 ip = self.regExpMatch(ipPattern, x)
727                                 if ip:
728                                         result.append( [ int(n) for n in ip.split('.') ] )
729                 return result
730
731 iNetwork = Network()
732
733 def InitNetwork():
734         pass