Merge branch 'master' of /home/tmbinc/enigma2-git into tmbinc/FixTimingBugs
[enigma2.git] / 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
7 class Network:
8         def __init__(self):
9                 self.ifaces = {}
10                 self.configuredInterfaces = []
11                 self.configuredNetworkAdapters = []
12                 self.NetworkState = 0
13                 self.DnsState = 0
14                 self.nameservers = []
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()
25                 self.getInterfaces()
26
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()
32                 fp.close()
33                 for line in result:
34                         try:
35                                 device = devicesPattern.search(line).group()
36                                 if device == 'wifi0':
37                                         continue
38                                 self.getDataForInterface(device, callback)
39                         except AttributeError:
40                                 pass
41                 #print "self.ifaces:", self.ifaces
42                 #self.writeNetworkConfig()
43                 #print ord(' ')
44                 #for line in result:
45                 #       print ord(line[0])
46
47         # helper function
48         def regExpMatch(self, pattern, string):
49                 if string is None:
50                         return None
51                 try:
52                         return pattern.search(string).group()
53                 except AttributeError:
54                         None
55
56         # helper function to convert ips from a sring to a list of ints
57         def convertIP(self, ip):
58                 strIP = ip.split('.')
59                 ip = []
60                 for x in strIP:
61                         ip.append(int(x))
62                 return ip
63
64         def getDataForInterface(self, iface,callback):
65                 #get ip out of ip addr, as avahi sometimes overrides it in ifconfig.
66                 if not self.Console:
67                         self.Console = Console()
68                 cmd = "ip -o addr"
69                 self.Console.ePopen(cmd, self.IPaddrFinished, [iface,callback])
70
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)
86                 
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]))
92                                 if up is not None:
93                                         data['up'] = True
94                                         if iface is not 'lo':
95                                                 self.configuredInterfaces.append(iface)
96                                 if mac is not None:
97                                         data['mac'] = mac
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]))
103                                         if ip is not None:
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)
109                                                 
110                 if not data.has_key('ip'):
111                         data['dhcp'] = True
112                         data['ip'] = [0, 0, 0, 0]
113                         data['netmask'] = [0, 0, 0, 0]
114                         data['gateway'] = [0, 0, 0, 0]
115
116                 cmd = "route -n | grep  " + iface
117                 self.Console.ePopen(cmd,self.routeFinished, [iface, data, callback])
118
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)
124
125                 for line in result.splitlines():
126                         print line[0:7]
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)
131                                         
132                 self.ifaces[iface] = data
133                 self.loadNetworkConfig(iface,callback)
134
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"])
160                         fp.write("\n")                          
161                 fp.close()
162                 self.writeNameserverConfig()
163
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))
168                 fp.close()
169
170         def loadNetworkConfig(self,iface,callback = None):
171                 interfaces = []
172                 # parse the interfaces-file
173                 try:
174                         fp = file('/etc/network/interfaces', 'r')
175                         interfaces = fp.readlines()
176                         fp.close()
177                 except:
178                         print "[Network.py] interfaces - opening failed"
179
180                 ifaces = {}
181                 currif = ""
182                 for i in interfaces:
183                         split = i.strip().split(' ')
184                         if (split[0] == "iface"):
185                                 currif = split[1]
186                                 ifaces[currif] = {}
187                                 if (len(split) == 4 and split[3] == "dhcp"):
188                                         ifaces[currif]["dhcp"] = True
189                                 else:
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
213
214                 for ifacename, iface in ifaces.items():
215                         if self.ifaces.has_key(ifacename):
216                                 self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
217                 if self.Console:
218                         if len(self.Console.appContainers) == 0:
219                                 # save configured interfacelist
220                                 self.configuredNetworkAdapters = self.configuredInterfaces
221                                 # load ns only once     
222                                 self.loadNameserverConfig()
223                                 print "read configured interfac:", ifaces
224                                 print "self.ifaces after loading:", self.ifaces
225                                 if callback is not None:
226                                         callback(True)
227
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)
232
233                 resolv = []
234                 try:
235                         fp = file('/etc/resolv.conf', 'r')
236                         resolv = fp.readlines()
237                         fp.close()
238                         self.nameservers = []
239                 except:
240                         print "[Network.py] resolv.conf - opening failed"
241
242                 for line in resolv:
243                         if self.regExpMatch(nameserverPattern, line) is not None:
244                                 ip = self.regExpMatch(ipPattern, line)
245                                 if ip is not None:
246                                         self.nameservers.append(self.convertIP(ip))
247
248                 print "nameservers:", self.nameservers
249
250         def deactivateNetworkConfig(self, callback = None):
251                 self.deactivateConsole = Console()
252                 self.commands = []
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)
261                 
262         def deactivateNetworkFinished(self,extra_args):
263                 callback = extra_args
264                 if len(self.deactivateConsole.appContainers) == 0:
265                         if callback is not None:
266                                 callback(True)
267
268         def activateNetworkConfig(self, callback = None):
269                 self.activateConsole = Console()
270                 self.commands = []
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)
274                 
275         def activateNetworkFinished(self,extra_args):
276                 callback = extra_args
277                 if len(self.activateConsole.appContainers) == 0:
278                         if callback is not None:
279                                 callback(True)
280
281         def getConfiguredAdapters(self):
282                 return self.configuredNetworkAdapters
283
284         def getNumberOfAdapters(self):
285                 return len(self.ifaces)
286
287         def getFriendlyAdapterName(self, x):
288                 # maybe this needs to be replaced by an external list.
289                 friendlyNames = {
290                         "eth0": _("Integrated Ethernet"),
291                         "wlan0": _("Wireless"),
292                         "ath0": _("Integrated Wireless")
293                 }
294                 return friendlyNames.get(x, x) # when we have no friendly name, use adapter name
295
296         def getAdapterName(self, iface):
297                 return iface
298
299         def getAdapterList(self):
300                 return self.ifaces.keys()
301
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]
306                 return None
307
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
312
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]
317
318         def getNameserverList(self):
319                 if len(self.nameservers) == 0:
320                         return [[0, 0, 0, 0], [0, 0, 0, 0]]
321                 else: 
322                         return self.nameservers
323
324         def clearNameservers(self):
325                 self.nameservers = []
326
327         def addNameserver(self, nameserver):
328                 if nameserver not in self.nameservers:
329                         self.nameservers.append(nameserver)
330
331         def removeNameserver(self, nameserver):
332                 if nameserver in self.nameservers:
333                         self.nameservers.remove(nameserver)
334
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
340
341         def resetNetworkConfig(self, mode='lan', callback = None):
342                 self.resetNetworkConsole = Console()
343                 self.commands = []
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)
352
353         def resetNetworkFinishedCB(self, extra_args):
354                 (mode, callback) = extra_args
355                 if len(self.resetNetworkConsole.appContainers) == 0:
356                         self.writeDefaultNetworkConfig(mode, callback)
357
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")
363                 if mode == 'wlan':
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")
369                 if mode == 'lan':
370                         fp.write("auto eth0\n")
371                         fp.write("iface eth0 inet dhcp\n")
372                 fp.write("\n")
373                 fp.close()
374
375                 self.resetNetworkConsole = Console()
376                 self.commands = []
377                 if mode == 'wlan':
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")                
385                 if mode == 'lan':                       
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)  
391
392         def resetNetworkFinished(self,extra_args):
393                 (mode, callback) = extra_args
394                 if len(self.resetNetworkConsole.appContainers) == 0:
395                         if callback is not None:
396                                 callback(True,mode)
397
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)
407                 
408         def checkNetworkStateFinished(self, result, retval,extra_args):
409                 (statecallback) = extra_args
410                 if self.PingConsole is not None:
411                         if retval == 0:
412                                 self.PingConsole = None
413                                 statecallback(self.NetworkState)
414                         else:
415                                 self.NetworkState += 1
416                                 if len(self.PingConsole.appContainers) == 0:
417                                         statecallback(self.NetworkState)
418                 
419         def restartNetwork(self,callback = None):
420                 self.restartConsole = Console()
421                 self.commands = []
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)
432         
433         def restartNetworkFinished(self,extra_args):
434                 ( callback ) = extra_args
435                 if callback is not None:
436                         callback(True)
437
438         def getLinkState(self,iface,callback):
439                 cmd = self.ethtool_bin + " " + iface
440                 self.LinkConsole = Console()
441                 self.LinkConsole.ePopen(cmd, self.getLinkStateFinished,callback)
442
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:
447                                 callback(result)
448                         
449         def stopLinkStateConsole(self):
450                 if self.LinkConsole is not None:
451                         self.LinkConsole = None
452
453         def stopDNSConsole(self):
454                 if self.DnsConsole is not None:
455                         self.DnsConsole = None
456
457         def stopRestartConsole(self):
458                 if self.restartConsole is not None:
459                         self.restartConsole = None
460                         
461         def stopGetInterfacesConsole(self):
462                 if self.Console is not None:
463                         self.Console = None
464
465         def stopDeactivateInterfaceConsole(self):
466                 if self.deactivateInterfaceConsole:
467                         self.deactivateInterfaceConsole = None
468                         
469         def checkforInterface(self,iface):
470                 if self.getAdapterAttribute(iface, 'up') is True:
471                         return True
472                 else:
473                         ret=system("ifconfig " + iface + " up")
474                         system("ifconfig " + iface + " down")
475                         if ret == 0:
476                                 return True
477                         else:
478                                 return False
479
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)
488                 
489         def checkDNSLookupFinished(self, result, retval,extra_args):
490                 (statecallback) = extra_args
491                 if self.DnsConsole is not None:
492                         if retval == 0:
493                                 self.DnsConsole = None
494                                 statecallback(self.DnsState)
495                         else:
496                                 self.DnsState += 1
497                                 if len(self.DnsConsole.appContainers) == 0:
498                                         statecallback(self.DnsState)
499
500         def deactivateInterface(self,iface,callback = None):
501                 self.deactivateInterfaceConsole = Console()
502                 self.commands = []
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)
508
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:
514                                         callback(True)
515
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)
523                         if len(files) >= 1:
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
534         
535         def calc_netmask(self,nmask):
536                 from struct import pack, unpack
537                 from socket import inet_ntoa, inet_aton
538                 mask = 1L<<31
539                 xnet = (1L<<32)-1
540                 cidr_range = range(0, 32)
541                 cidr = long(nmask)
542                 if cidr not in cidr_range:
543                         print 'cidr invalid: %d' % cidr
544                         return None
545                 else:
546                         nm = ((1L<<cidr)-1)<<(32-cidr)
547                         netmask = str(inet_ntoa(pack('>L', nm)))
548                         return netmask
549         
550 iNetwork = Network()
551
552 def InitNetwork():
553         pass