fix calculation in calculateDifference for cable and terrestrial which should complet...
[enigma2.git] / lib / python / Components / Network.py
1 from config import *
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 (currentConfigSelectionElement(config.network.dhcp) == "yes"):
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                 
88         def setDHCP(self, useDHCP):
89                 if (useDHCP):
90                         print "Using DHCP"
91                         config.network.ip.enabled = False
92                         config.network.netmask.enabled = False
93                         config.network.gateway.enabled = False
94                         config.network.dns.enabled = False
95                 else:
96                         print "NOT using DHCP"
97                         config.network.ip.enabled = True
98                         config.network.netmask.enabled = True
99                         config.network.gateway.enabled = True
100                         config.network.dns.enabled = True
101                                         
102         def setIPNameserver(self, ip):
103                 return
104                 resolvconf = file('/etc/resolv.conf', 'w')
105                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
106                 resolvconf.close()
107                 
108         def setMACAddress(self, mac):
109                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
110                 pass
111                 
112         def setIPAddress(self, ip):
113                 pass
114                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
115                 #self.writeNetworkConfig()
116
117         def setGateway(self, ip):
118                 pass
119                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
120                 #self.writeNetworkConfig()
121                 
122         def setNetmask(self, ip):
123                 pass
124                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
125                 #self.writeNetworkConfig()              
126
127         def getCurrentIP(self):
128                 ipstr = [0,0,0,0]
129                 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
130                         if x.split(':')[0] == "addr":
131                                 ipstr = x.split(':')[1].split('.')
132                 ip = []
133                 for x in ipstr:
134                         ip.append(int(x))
135                 print "[Network.py] got ip " + str(ip)
136                 return ip
137
138 iNetwork = Network()
139
140 def InitNetwork():
141         ip = iNetwork.getCurrentIP()
142
143                 
144         config.network = ConfigSubsection()
145         config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (("no", _("no")), ("yes", _("yes"))))
146         config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
147         config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
148         config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
149         config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
150         config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
151
152         iNetwork.loadNetworkConfig()
153         
154         #FIXME using this till other concept for this is implemented
155         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
156         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
157
158
159         def writeNetworkConfig(configElement):
160                 iNetwork.writeNetworkConfig()
161                 
162         def setIPAddress(configElement):
163                 iNetwork.setIPAddress(configElement.value)
164
165         def setGateway(configElement):
166                 iNetwork.setGateway(configElement.value)
167
168         def setNetmask(configElement):
169                 iNetwork.setNetmask(configElement.value)                
170
171         def setDHCP(configElement):
172                 iNetwork.setDHCP(configElement.value)
173
174         def setIPNameserver(configElement):
175                 iNetwork.setIPNameserver(configElement.value)
176
177         def setMACAddress(configElement):
178                 iNetwork.setMACAddress(configElement.value)
179
180
181         # this will call the "setup-val" initial
182         config.network.dhcp.addNotifier(setDHCP)
183         config.network.ip.addNotifier(setIPAddress)
184         config.network.netmask.addNotifier(setNetmask)  
185         config.network.gateway.addNotifier(setGateway)
186         config.network.dns.addNotifier(setIPNameserver)
187         config.network.mac.addNotifier(setMACAddress)