6d28b19d3e6ea0595acba9544a7bfadaad8c4c0e
[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 eth0\n")
15                 if (config.network.dhcp.value == _("yes")):
16                         fp.write("iface eth0 inet dhcp\n")
17                         fp.write("      address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
18                 else:
19                         fp.write("iface eth0 inet static\n")
20                         fp.write("      address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
21                         fp.write("      netmask %d.%d.%d.%d\n" % tuple(config.network.netmask.value))
22                         fp.write("      gateway %d.%d.%d.%d\n" % tuple(config.network.gateway.value))
23                 fp.close()
24
25         def loadNetworkConfig(self):
26                 try:
27                         # parse the interfaces-file
28                         fp = file('/etc/network/interfaces', 'r')
29                         interfaces = fp.readlines()
30                         fp.close()
31                         
32                         ifaces = {}
33                         currif = ""
34                         for i in interfaces:
35                                 split = i.strip().split(' ')
36                                 if (split[0] == "iface"):
37                                         currif = split[1]
38                                         ifaces[currif] = {}
39                                         if (len(split) == 4 and split[3] == "dhcp"):
40                                                 ifaces[currif]["dhcp"] = "yes"
41                                         else:
42                                                 ifaces[currif]["dhcp"] = "no"
43                                 if (currif != ""):
44                                         if (split[0] == "address"):
45                                                 ifaces[currif]["address"] = map(int, split[1].split('.'))
46                                         if (split[0] == "netmask"):
47                                                 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
48                                         if (split[0] == "gateway"):
49                                                 ifaces[currif]["gateway"] = map(int, split[1].split('.'))                                                                       
50                         
51                         # parse the resolv.conf-file
52                         fp = file('/etc/network/interfaces', 'r')
53                         resolv = fp.readlines()
54                         fp.close()
55                 except:
56                         pass
57                         
58                 try:
59                         for i in resolv:
60                                 split = i.strip().split(' ')
61                                 if (split[0] == "nameserver"):
62                                         config.network.nameserver.value = map(int, split[1].split('.'))
63                 except:
64                         pass
65                 
66                 try:
67                         # set this config
68                         if (ifaces.has_key("eth0")):
69                                 if (ifaces["eth0"]["dhcp"] == "yes"):
70                                         config.network.dhcp.value = 1
71                                 else:
72                                         config.network.dhcp.value = 0
73                                 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
74                                 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
75                                 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
76                 except:
77                         pass
78
79         def activateNetworkConfig(self):
80                 import os
81                 os.system("/etc/init.d/networking restart")
82                 config.network.ip.value = self.getCurrentIP()
83                 
84         def setDHCP(self, useDHCP):
85                 if (useDHCP):
86                         print "Using DHCP"
87                         config.network.ip.enabled = False
88                         config.network.netmask.enabled = False
89                         config.network.gateway.enabled = False
90                         config.network.dns.enabled = False
91                 else:
92                         print "NOT using DHCP"
93                         config.network.ip.enabled = True
94                         config.network.netmask.enabled = True
95                         config.network.gateway.enabled = True
96                         config.network.dns.enabled = True
97                                         
98         def setIPNameserver(self, ip):
99                 return
100                 resolvconf = file('/etc/resolv.conf', 'w')
101                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
102                 resolvconf.close()
103                 
104         def setMACAddress(self, mac):
105                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
106                 pass
107                 
108         def setIPAddress(self, ip):
109                 pass
110                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
111                 #self.writeNetworkConfig()
112
113         def setGateway(self, ip):
114                 pass
115                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
116                 #self.writeNetworkConfig()
117                 
118         def setNetmask(self, ip):
119                 pass
120                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
121                 #self.writeNetworkConfig()              
122
123         def getCurrentIP(self):
124                 ip = [0, 0, 0, 0]
125                 try:
126                         print gethostbyname(gethostname())
127                         ip = gethostbyname(gethostname()).split('.')
128                 except:
129                         print "[Network.py] Could not get current ip (not necessarily an error)"
130                 return ip
131
132 iNetwork = Network()
133
134 def InitNetwork():
135         ip = iNetwork.getCurrentIP()
136
137                 
138         config.network = ConfigSubsection()
139         config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (_("no"), _("yes")))
140         config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
141         config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
142         config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
143         config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
144         config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
145
146         iNetwork.loadNetworkConfig()
147         
148         #FIXME using this till other concept for this is implemented
149         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
150         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
151
152
153         def writeNetworkConfig(configElement):
154                 iNetwork.writeNetworkConfig()
155                 
156         def setIPAddress(configElement):
157                 iNetwork.setIPAddress(configElement.value)
158
159         def setGateway(configElement):
160                 iNetwork.setGateway(configElement.value)
161
162         def setNetmask(configElement):
163                 iNetwork.setNetmask(configElement.value)                
164
165         def setDHCP(configElement):
166                 iNetwork.setDHCP(configElement.value)
167
168         def setIPNameserver(configElement):
169                 iNetwork.setIPNameserver(configElement.value)
170
171         def setMACAddress(configElement):
172                 iNetwork.setMACAddress(configElement.value)
173
174
175         # this will call the "setup-val" initial
176         config.network.dhcp.addNotifier(setDHCP)
177         config.network.ip.addNotifier(setIPAddress)
178         config.network.netmask.addNotifier(setNetmask)  
179         config.network.gateway.addNotifier(setGateway)
180         config.network.dns.addNotifier(setIPNameserver)
181         config.network.mac.addNotifier(setMACAddress)