1 from os import system, popen, path as os_path, listdir
2 from re import compile as re_compile, search as re_search
4 from enigma import eConsoleAppContainer
5 from Components.Console import Console
10 self.configuredInterfaces = []
11 self.configuredNetworkAdapters = []
15 self.ethtool_bin = "/usr/sbin/ethtool"
16 self.container = eConsoleAppContainer()
17 self.Console = Console()
18 self.LinkConsole = Console()
19 self.restartConsole = Console()
20 self.deactivateConsole = Console()
21 self.deactivateInterfaceConsole = Console()
22 self.activateConsole = Console()
23 self.resetNetworkConsole = Console()
24 self.DnsConsole = Console()
27 def getInterfaces(self, callback = None):
28 devicesPattern = re_compile('[a-z]+[0-9]+')
29 self.configuredInterfaces = []
30 fp = file('/proc/net/dev', 'r')
31 result = fp.readlines()
35 device = devicesPattern.search(line).group()
38 self.getDataForInterface(device, callback)
39 except AttributeError:
41 #print "self.ifaces:", self.ifaces
42 #self.writeNetworkConfig()
48 def regExpMatch(self, pattern, string):
52 return pattern.search(string).group()
53 except AttributeError:
56 # helper function to convert ips from a sring to a list of ints
57 def convertIP(self, ip):
64 def getDataForInterface(self, iface,callback):
65 #get ip out of ip addr, as avahi sometimes overrides it in ifconfig.
67 self.Console = Console()
69 self.Console.ePopen(cmd, self.IPaddrFinished, [iface,callback])
71 def IPaddrFinished(self, result, retval, extra_args):
72 (iface, callback ) = extra_args
73 data = { 'up': False, 'dhcp': False, 'preup' : False, 'postdown' : False }
74 globalIPpattern = re_compile("scope global")
75 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
76 netRegexp = '[0-9]{1,2}'
77 macRegexp = '[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}'
78 ipLinePattern = re_compile('inet ' + ipRegexp + '/')
79 ipPattern = re_compile(ipRegexp)
80 netmaskLinePattern = re_compile('/' + netRegexp)
81 netmaskPattern = re_compile(netRegexp)
82 bcastLinePattern = re_compile(' brd ' + ipRegexp)
83 upPattern = re_compile('UP')
84 macPattern = re_compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}\:[a-z0-9]{2}')
85 macLinePattern = re_compile('link/ether ' + macRegexp)
87 for line in result.splitlines():
88 split = line.strip().split(' ',2)
89 if (split[1][:-1] == iface):
90 up = self.regExpMatch(upPattern, split[2])
91 mac = self.regExpMatch(macPattern, self.regExpMatch(macLinePattern, split[2]))
95 self.configuredInterfaces.append(iface)
98 if (split[1] == iface):
99 if re_search(globalIPpattern, split[2]):
100 ip = self.regExpMatch(ipPattern, self.regExpMatch(ipLinePattern, split[2]))
101 netmask = self.calc_netmask(self.regExpMatch(netmaskPattern, self.regExpMatch(netmaskLinePattern, split[2])))
102 bcast = self.regExpMatch(ipPattern, self.regExpMatch(bcastLinePattern, split[2]))
104 data['ip'] = self.convertIP(ip)
105 if netmask is not None:
106 data['netmask'] = self.convertIP(netmask)
107 if bcast is not None:
108 data['bcast'] = self.convertIP(bcast)
110 if not data.has_key('ip'):
112 data['ip'] = [0, 0, 0, 0]
113 data['netmask'] = [0, 0, 0, 0]
114 data['gateway'] = [0, 0, 0, 0]
116 cmd = "route -n | grep " + iface
117 self.Console.ePopen(cmd,self.routeFinished, [iface, data, callback])
119 def routeFinished(self, result, retval, extra_args):
120 (iface, data, callback) = extra_args
121 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
122 ipPattern = re_compile(ipRegexp)
123 ipLinePattern = re_compile(ipRegexp)
125 for line in result.splitlines():
127 if line[0:7] == "0.0.0.0":
128 gateway = self.regExpMatch(ipPattern, line[16:31])
129 if gateway is not None:
130 data['gateway'] = self.convertIP(gateway)
132 self.ifaces[iface] = data
133 self.loadNetworkConfig(iface,callback)
135 def writeNetworkConfig(self):
136 self.configuredInterfaces = []
137 fp = file('/etc/network/interfaces', 'w')
138 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
139 fp.write("auto lo\n")
140 fp.write("iface lo inet loopback\n\n")
141 for ifacename, iface in self.ifaces.items():
142 if iface['up'] == True:
143 fp.write("auto " + ifacename + "\n")
144 self.configuredInterfaces.append(ifacename)
145 if iface['dhcp'] == True:
146 fp.write("iface "+ ifacename +" inet dhcp\n")
147 if iface['dhcp'] == False:
148 fp.write("iface "+ ifacename +" inet static\n")
149 if iface.has_key('ip'):
150 print tuple(iface['ip'])
151 fp.write(" address %d.%d.%d.%d\n" % tuple(iface['ip']))
152 fp.write(" netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
153 if iface.has_key('gateway'):
154 fp.write(" gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
155 if iface.has_key("configStrings"):
156 fp.write("\n" + iface["configStrings"] + "\n")
157 if iface["preup"] is not False and not iface.has_key("configStrings"):
158 fp.write(iface["preup"])
159 fp.write(iface["postdown"])
162 self.writeNameserverConfig()
164 def writeNameserverConfig(self):
165 fp = file('/etc/resolv.conf', 'w')
166 for nameserver in self.nameservers:
167 fp.write("nameserver %d.%d.%d.%d\n" % tuple(nameserver))
170 def loadNetworkConfig(self,iface,callback = None):
172 # parse the interfaces-file
174 fp = file('/etc/network/interfaces', 'r')
175 interfaces = fp.readlines()
178 print "[Network.py] interfaces - opening failed"
183 split = i.strip().split(' ')
184 if (split[0] == "iface"):
187 if (len(split) == 4 and split[3] == "dhcp"):
188 ifaces[currif]["dhcp"] = True
190 ifaces[currif]["dhcp"] = False
191 if (currif == iface): #read information only for available interfaces
192 if (split[0] == "address"):
193 ifaces[currif]["address"] = map(int, split[1].split('.'))
194 if self.ifaces[currif].has_key("ip"):
195 if self.ifaces[currif]["ip"] != ifaces[currif]["address"] and ifaces[currif]["dhcp"] == False:
196 self.ifaces[currif]["ip"] = map(int, split[1].split('.'))
197 if (split[0] == "netmask"):
198 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
199 if self.ifaces[currif].has_key("netmask"):
200 if self.ifaces[currif]["netmask"] != ifaces[currif]["netmask"] and ifaces[currif]["dhcp"] == False:
201 self.ifaces[currif]["netmask"] = map(int, split[1].split('.'))
202 if (split[0] == "gateway"):
203 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
204 if self.ifaces[currif].has_key("gateway"):
205 if self.ifaces[currif]["gateway"] != ifaces[currif]["gateway"] and ifaces[currif]["dhcp"] == False:
206 self.ifaces[currif]["gateway"] = map(int, split[1].split('.'))
207 if (split[0] == "pre-up"):
208 if self.ifaces[currif].has_key("preup"):
209 self.ifaces[currif]["preup"] = i
210 if (split[0] == "post-down"):
211 if self.ifaces[currif].has_key("postdown"):
212 self.ifaces[currif]["postdown"] = i
214 for ifacename, iface in ifaces.items():
215 if self.ifaces.has_key(ifacename):
216 self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
218 if len(self.Console.appContainers) == 0:
219 # save configured interfacelist
220 self.configuredNetworkAdapters = self.configuredInterfaces
222 self.loadNameserverConfig()
223 print "read configured interfac:", ifaces
224 print "self.ifaces after loading:", self.ifaces
225 if callback is not None:
228 def loadNameserverConfig(self):
229 ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
230 nameserverPattern = re_compile("nameserver +" + ipRegexp)
231 ipPattern = re_compile(ipRegexp)
235 fp = file('/etc/resolv.conf', 'r')
236 resolv = fp.readlines()
238 self.nameservers = []
240 print "[Network.py] resolv.conf - opening failed"
243 if self.regExpMatch(nameserverPattern, line) is not None:
244 ip = self.regExpMatch(ipPattern, line)
246 self.nameservers.append(self.convertIP(ip))
248 print "nameservers:", self.nameservers
250 def deactivateNetworkConfig(self, callback = None):
251 self.deactivateConsole = Console()
253 self.commands.append("/etc/init.d/avahi-daemon stop")
254 for iface in self.ifaces.keys():
255 cmd = "ip addr flush " + iface
256 self.commands.append(cmd)
257 self.commands.append("/etc/init.d/networking stop")
258 self.commands.append("killall -9 udhcpc")
259 self.commands.append("rm /var/run/udhcpc*")
260 self.deactivateConsole.eBatch(self.commands, self.deactivateNetworkFinished, callback, debug=True)
262 def deactivateNetworkFinished(self,extra_args):
263 callback = extra_args
264 if len(self.deactivateConsole.appContainers) == 0:
265 if callback is not None:
268 def activateNetworkConfig(self, callback = None):
269 self.activateConsole = Console()
271 self.commands.append("/etc/init.d/networking start")
272 self.commands.append("/etc/init.d/avahi-daemon start")
273 self.activateConsole.eBatch(self.commands, self.activateNetworkFinished, callback, debug=True)
275 def activateNetworkFinished(self,extra_args):
276 callback = extra_args
277 if len(self.activateConsole.appContainers) == 0:
278 if callback is not None:
281 def getConfiguredAdapters(self):
282 return self.configuredNetworkAdapters
284 def getNumberOfAdapters(self):
285 return len(self.ifaces)
287 def getFriendlyAdapterName(self, x):
288 # maybe this needs to be replaced by an external list.
290 "eth0": _("Integrated Ethernet"),
291 "wlan0": _("Wireless"),
292 "ath0": _("Integrated Wireless")
294 return friendlyNames.get(x, x) # when we have no friendly name, use adapter name
296 def getAdapterName(self, iface):
299 def getAdapterList(self):
300 return self.ifaces.keys()
302 def getAdapterAttribute(self, iface, attribute):
303 if self.ifaces.has_key(iface):
304 if self.ifaces[iface].has_key(attribute):
305 return self.ifaces[iface][attribute]
308 def setAdapterAttribute(self, iface, attribute, value):
309 print "setting for adapter", iface, "attribute", attribute, " to value", value
310 if self.ifaces.has_key(iface):
311 self.ifaces[iface][attribute] = value
313 def removeAdapterAttribute(self, iface, attribute):
314 if self.ifaces.has_key(iface):
315 if self.ifaces[iface].has_key(attribute):
316 del self.ifaces[iface][attribute]
318 def getNameserverList(self):
319 if len(self.nameservers) == 0:
320 return [[0, 0, 0, 0], [0, 0, 0, 0]]
322 return self.nameservers
324 def clearNameservers(self):
325 self.nameservers = []
327 def addNameserver(self, nameserver):
328 if nameserver not in self.nameservers:
329 self.nameservers.append(nameserver)
331 def removeNameserver(self, nameserver):
332 if nameserver in self.nameservers:
333 self.nameservers.remove(nameserver)
335 def changeNameserver(self, oldnameserver, newnameserver):
336 if oldnameserver in self.nameservers:
337 for i in range(len(self.nameservers)):
338 if self.nameservers[i] == oldnameserver:
339 self.nameservers[i] = newnameserver
341 def resetNetworkConfig(self, mode='lan', callback = None):
342 self.resetNetworkConsole = Console()
344 self.commands.append("/etc/init.d/avahi-daemon stop")
345 for iface in self.ifaces.keys():
346 cmd = "ip addr flush " + iface
347 self.commands.append(cmd)
348 self.commands.append("/etc/init.d/networking stop")
349 self.commands.append("killall -9 udhcpc")
350 self.commands.append("rm /var/run/udhcpc*")
351 self.resetNetworkConsole.eBatch(self.commands, self.resetNetworkFinishedCB, [mode, callback], debug=True)
353 def resetNetworkFinishedCB(self, extra_args):
354 (mode, callback) = extra_args
355 if len(self.resetNetworkConsole.appContainers) == 0:
356 self.writeDefaultNetworkConfig(mode, callback)
358 def writeDefaultNetworkConfig(self,mode='lan', callback = None):
359 fp = file('/etc/network/interfaces', 'w')
360 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
361 fp.write("auto lo\n")
362 fp.write("iface lo inet loopback\n\n")
364 fp.write("auto wlan0\n")
365 fp.write("iface wlan0 inet dhcp\n")
366 if mode == 'wlan-mpci':
367 fp.write("auto ath0\n")
368 fp.write("iface ath0 inet dhcp\n")
370 fp.write("auto eth0\n")
371 fp.write("iface eth0 inet dhcp\n")
375 self.resetNetworkConsole = Console()
378 self.commands.append("ifconfig eth0 down")
379 self.commands.append("ifconfig ath0 down")
380 self.commands.append("ifconfig wlan0 up")
381 if mode == 'wlan-mpci':
382 self.commands.append("ifconfig eth0 down")
383 self.commands.append("ifconfig wlan0 down")
384 self.commands.append("ifconfig ath0 up")
386 self.commands.append("ifconfig eth0 up")
387 self.commands.append("ifconfig wlan0 down")
388 self.commands.append("ifconfig ath0 down")
389 self.commands.append("/etc/init.d/avahi-daemon start")
390 self.resetNetworkConsole.eBatch(self.commands, self.resetNetworkFinished, [mode,callback], debug=True)
392 def resetNetworkFinished(self,extra_args):
393 (mode, callback) = extra_args
394 if len(self.resetNetworkConsole.appContainers) == 0:
395 if callback is not None:
398 def checkNetworkState(self,statecallback):
399 # www.dream-multimedia-tv.de, www.heise.de, www.google.de
400 cmd1 = "ping -c 1 82.149.226.170"
401 cmd2 = "ping -c 1 193.99.144.85"
402 cmd3 = "ping -c 1 209.85.135.103"
403 self.PingConsole = Console()
404 self.PingConsole.ePopen(cmd1, self.checkNetworkStateFinished,statecallback)
405 self.PingConsole.ePopen(cmd2, self.checkNetworkStateFinished,statecallback)
406 self.PingConsole.ePopen(cmd3, self.checkNetworkStateFinished,statecallback)
408 def checkNetworkStateFinished(self, result, retval,extra_args):
409 (statecallback) = extra_args
410 if self.PingConsole is not None:
412 self.PingConsole = None
413 statecallback(self.NetworkState)
415 self.NetworkState += 1
416 if len(self.PingConsole.appContainers) == 0:
417 statecallback(self.NetworkState)
419 def restartNetwork(self,callback = None):
420 self.restartConsole = Console()
422 self.commands.append("/etc/init.d/avahi-daemon stop")
423 for iface in self.ifaces.keys():
424 cmd = "ip addr flush " + iface
425 self.commands.append(cmd)
426 self.commands.append("/etc/init.d/networking stop")
427 self.commands.append("killall -9 udhcpc")
428 self.commands.append("rm /var/run/udhcpc*")
429 self.commands.append("/etc/init.d/networking start")
430 self.commands.append("/etc/init.d/avahi-daemon start")
431 self.restartConsole.eBatch(self.commands, self.restartNetworkFinished, callback, debug=True)
433 def restartNetworkFinished(self,extra_args):
434 ( callback ) = extra_args
435 if callback is not None:
438 def getLinkState(self,iface,callback):
439 cmd = self.ethtool_bin + " " + iface
440 self.LinkConsole = Console()
441 self.LinkConsole.ePopen(cmd, self.getLinkStateFinished,callback)
443 def getLinkStateFinished(self, result, retval,extra_args):
444 (callback) = extra_args
445 if self.LinkConsole is not None:
446 if len(self.LinkConsole.appContainers) == 0:
449 def stopLinkStateConsole(self):
450 if self.LinkConsole is not None:
451 self.LinkConsole = None
453 def stopDNSConsole(self):
454 if self.DnsConsole is not None:
455 self.DnsConsole = None
457 def stopRestartConsole(self):
458 if self.restartConsole is not None:
459 self.restartConsole = None
461 def stopGetInterfacesConsole(self):
462 if self.Console is not None:
465 def stopDeactivateInterfaceConsole(self):
466 if self.deactivateInterfaceConsole:
467 self.deactivateInterfaceConsole = None
469 def checkforInterface(self,iface):
470 if self.getAdapterAttribute(iface, 'up') is True:
473 ret=system("ifconfig " + iface + " up")
474 system("ifconfig " + iface + " down")
480 def checkDNSLookup(self,statecallback):
481 cmd1 = "nslookup www.dream-multimedia-tv.de"
482 cmd2 = "nslookup www.heise.de"
483 cmd3 = "nslookup www.google.de"
484 self.DnsConsole = Console()
485 self.DnsConsole.ePopen(cmd1, self.checkDNSLookupFinished,statecallback)
486 self.DnsConsole.ePopen(cmd2, self.checkDNSLookupFinished,statecallback)
487 self.DnsConsole.ePopen(cmd3, self.checkDNSLookupFinished,statecallback)
489 def checkDNSLookupFinished(self, result, retval,extra_args):
490 (statecallback) = extra_args
491 if self.DnsConsole is not None:
493 self.DnsConsole = None
494 statecallback(self.DnsState)
497 if len(self.DnsConsole.appContainers) == 0:
498 statecallback(self.DnsState)
500 def deactivateInterface(self,iface,callback = None):
501 self.deactivateInterfaceConsole = Console()
503 cmd1 = "ip addr flush " + iface
504 cmd2 = "ifconfig " + iface + " down"
505 self.commands.append(cmd1)
506 self.commands.append(cmd2)
507 self.deactivateInterfaceConsole.eBatch(self.commands, self.deactivateInterfaceFinished, callback, debug=True)
509 def deactivateInterfaceFinished(self,extra_args):
510 callback = extra_args
511 if self.deactivateInterfaceConsole:
512 if len(self.deactivateInterfaceConsole.appContainers) == 0:
513 if callback is not None:
516 def detectWlanModule(self):
517 self.wlanmodule = None
518 rt73_dir = "/sys/bus/usb/drivers/rt73/"
519 zd1211b_dir = "/sys/bus/usb/drivers/zd1211b/"
520 madwifi_dir = "/sys/bus/pci/drivers/ath_pci/"
521 if os_path.exists(madwifi_dir):
522 files = listdir(madwifi_dir)
524 self.wlanmodule = 'madwifi'
525 if os_path.exists(rt73_dir):
526 rtfiles = listdir(rt73_dir)
527 if len(rtfiles) == 2:
528 self.wlanmodule = 'ralink'
529 if os_path.exists(zd1211b_dir):
530 zdfiles = listdir(zd1211b_dir)
531 if len(zdfiles) == 1:
532 self.wlanmodule = 'zydas'
533 return self.wlanmodule
535 def calc_netmask(self,nmask):
536 from struct import pack, unpack
537 from socket import inet_ntoa, inet_aton
540 cidr_range = range(0, 32)
542 if cidr not in cidr_range:
543 print 'cidr invalid: %d' % cidr
546 nm = ((1L<<cidr)-1)<<(32-cidr)
547 netmask = str(inet_ntoa(pack('>L', nm)))