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