ask user to reboot dreambox after software update
[enigma2.git] / lib / python / Components / Network.py
1 from config import config, ConfigYesNo, ConfigIP, NoSave, ConfigSubsection, ConfigMAC
2
3 import os
4 from socket import *
5
6 class Network:
7         def __init__(self):
8                 pass
9                 
10         def writeNetworkConfig(self):
11                 # fixme restarting and updating the network too often. possible fix: check current config and execute only if changed :/
12                 # fixme using interfaces.tmp instead of interfaces for now
13                 fp = file('/etc/network/interfaces', 'w')
14                 fp.write("auto lo\n")
15                 fp.write("iface lo inet loopback\n\n")
16                 fp.write("auto eth0\n")
17                 if config.network.dhcp.value:
18                         fp.write("iface eth0 inet dhcp\n")
19                 else:
20                         fp.write("iface eth0 inet static\n")
21                         fp.write("      address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
22                         fp.write("      netmask %d.%d.%d.%d\n" % tuple(config.network.netmask.value))
23                         fp.write("      gateway %d.%d.%d.%d\n" % tuple(config.network.gateway.value))
24                         fp2 = file('/etc/resolv.conf', 'w')
25                         fp2.write("nameserver %d.%d.%d.%d\n" % tuple(config.network.dns.value))
26                         fp2.close()
27                 fp.close()
28
29         def loadNetworkConfig(self):
30                 try:
31                         # parse the interfaces-file
32                         fp = file('/etc/network/interfaces', 'r')
33                         interfaces = fp.readlines()
34                         fp.close()
35                         
36                         ifaces = {}
37                         currif = ""
38                         for i in interfaces:
39                                 split = i.strip().split(' ')
40                                 if (split[0] == "iface"):
41                                         currif = split[1]
42                                         ifaces[currif] = {}
43                                         if (len(split) == 4 and split[3] == "dhcp"):
44                                                 ifaces[currif]["dhcp"] = "yes"
45                                         else:
46                                                 ifaces[currif]["dhcp"] = "no"
47                                 if (currif != ""):
48                                         if (split[0] == "address"):
49                                                 ifaces[currif]["address"] = map(int, split[1].split('.'))
50                                         if (split[0] == "netmask"):
51                                                 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
52                                         if (split[0] == "gateway"):
53                                                 ifaces[currif]["gateway"] = map(int, split[1].split('.'))                                                                       
54                         
55                         # parse the resolv.conf-file
56                         fp = file('/etc/resolv.conf', 'r')
57                         resolv = fp.readlines()
58                         fp.close()
59                 except:
60                         print "[Network.py] loading network files failed"
61                         
62                 try:
63                         for i in resolv:
64                                 split = i.strip().split(' ')
65                                 if (split[0] == "nameserver"):
66                                         config.network.dns.value = map(int, split[1].split('.'))
67                 except:
68                         print "[Network.py] resolv.conf parsing failed"
69                 
70                 try:
71                         # set this config
72                         if (ifaces.has_key("eth0")):
73                                 if (ifaces["eth0"]["dhcp"] == "yes"):
74                                         config.network.dhcp.value = 1
75                                 else:
76                                         config.network.dhcp.value = 0
77                                 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
78                                 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
79                                 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
80                 except:
81                         print "[Network.py] parsing network failed"
82
83         def activateNetworkConfig(self):
84                 import os
85                 os.system("/etc/init.d/networking restart")
86                 config.network.ip.value = self.getCurrentIP()
87                 config.network.ip.save()
88                 
89         def setDHCP(self, useDHCP):
90                 if (useDHCP):
91                         print "Using DHCP"
92                         config.network.ip.enabled = False
93                         config.network.netmask.enabled = False
94                         config.network.gateway.enabled = False
95                         config.network.dns.enabled = False
96                 else:
97                         print "NOT using DHCP"
98                         config.network.ip.enabled = True
99                         config.network.netmask.enabled = True
100                         config.network.gateway.enabled = True
101                         config.network.dns.enabled = True
102                                         
103         def setIPNameserver(self, ip):
104                 return
105                 resolvconf = file('/etc/resolv.conf', 'w')
106                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
107                 resolvconf.close()
108                 
109         def setMACAddress(self, mac):
110                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
111                 pass
112                 
113         def setIPAddress(self, ip):
114                 pass
115                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
116                 #self.writeNetworkConfig()
117
118         def setGateway(self, ip):
119                 pass
120                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
121                 #self.writeNetworkConfig()
122                 
123         def setNetmask(self, ip):
124                 pass
125                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
126                 #self.writeNetworkConfig()              
127
128         def getCurrentIP(self):
129                 ipstr = [0,0,0,0]
130                 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
131                         if x.split(':')[0] == "addr":
132                                 ipstr = x.split(':')[1].split('.')
133                 ip = []
134                 for x in ipstr:
135                         ip.append(int(x))
136                 print "[Network.py] got ip " + str(ip)
137                 return ip
138
139 iNetwork = Network()
140
141 def InitNetwork():
142         config.network = ConfigSubsection()
143         config.network.dhcp = NoSave(ConfigYesNo(default=True))
144         config.network.ip = NoSave(ConfigIP(default=iNetwork.getCurrentIP()))
145         config.network.netmask = NoSave(ConfigIP(default=[255,255,255,0]))
146         config.network.gateway = NoSave(ConfigIP(default=[192,168,1,3]))
147         config.network.dns = NoSave(ConfigIP(default=[192,168,1,3]))
148         config.network.mac = NoSave(ConfigMAC(default=[00,11,22,33,44,55]))
149
150         iNetwork.loadNetworkConfig()
151         
152         def writeNetworkConfig(configElement):
153                 iNetwork.writeNetworkConfig()
154                 
155         def setIPAddress(configElement):
156                 iNetwork.setIPAddress(configElement.value)
157
158         def setGateway(configElement):
159                 iNetwork.setGateway(configElement.value)
160
161         def setNetmask(configElement):
162                 iNetwork.setNetmask(configElement.value)                
163
164         def setDHCP(configElement):
165                 iNetwork.setDHCP(configElement.value)
166
167         def setIPNameserver(configElement):
168                 iNetwork.setIPNameserver(configElement.value)
169
170         def setMACAddress(configElement):
171                 iNetwork.setMACAddress(configElement.value)
172
173
174         # this will call the "setup-val" initial
175         config.network.dhcp.addNotifier(setDHCP)
176         config.network.ip.addNotifier(setIPAddress)
177         config.network.netmask.addNotifier(setNetmask)  
178         config.network.gateway.addNotifier(setGateway)
179         config.network.dns.addNotifier(setIPNameserver)
180         config.network.mac.addNotifier(setMACAddress)