* Add License
[enigma2-plugins.git] / wirelesslan / src / Network.py
1 from Components.config import config, ConfigYesNo, ConfigIP, NoSave, ConfigSubsection, ConfigMAC
2
3 import os
4 from socket import *
5 print "[WLAN] imported modified Network.py"
6 class Network:
7         def __init__(self, iface = "eth0"):
8                 self.iface = iface
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 "+self.iface+"\n")
17                 if config.network.dhcp.value:
18                         fp.write("iface "+self.iface+" inet dhcp\n")
19                 else:
20                         fp.write("iface "+self.iface+" 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(self.iface)):
73                                 if (ifaces[self.iface]["dhcp"] == "yes"):
74                                         config.network.dhcp.value = 1
75                                 else:
76                                         config.network.dhcp.value = 0
77                                 if (ifaces[self.iface].has_key("address")): config.network.ip.value = ifaces[self.iface]["address"]
78                                 if (ifaces[self.iface].has_key("netmask")): config.network.netmask.value = ifaces[self.iface]["netmask"]
79                                 if (ifaces[self.iface].has_key("gateway")): config.network.gateway.value = ifaces[self.iface]["gateway"]
80                 except:
81                         print "[Network.py] parsing network failed"
82
83         def deactivateNetworkConfig(self):
84                 import os
85                 os.system("ip addr flush"+self.iface)
86                 os.system("/etc/init.d/networking stop")
87                 os.system("killall -9 udhcpc")
88                 os.system("rm /var/run/udhcpc*")
89
90         def activateNetworkConfig(self):
91                 import os
92                 os.system("/etc/init.d/networking start")
93                 config.network.ip.value = self.getCurrentIP()
94                 config.network.ip.save()
95                 
96         def setDHCP(self, useDHCP):
97                 if (useDHCP):
98                         print "Using DHCP"
99                         config.network.ip.enabled = False
100                         config.network.netmask.enabled = False
101                         config.network.gateway.enabled = False
102                         config.network.dns.enabled = False
103                 else:
104                         print "NOT using DHCP"
105                         config.network.ip.enabled = True
106                         config.network.netmask.enabled = True
107                         config.network.gateway.enabled = True
108                         config.network.dns.enabled = True
109                                         
110         def setIPNameserver(self, ip):
111                 return
112                 resolvconf = file('/etc/resolv.conf', 'w')
113                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
114                 resolvconf.close()
115                 
116         def setMACAddress(self, mac):
117                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
118                 pass
119                 
120         def setIPAddress(self, ip):
121                 pass
122                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
123                 #self.writeNetworkConfig()
124
125         def setGateway(self, ip):
126                 pass
127                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
128                 #self.writeNetworkConfig()
129                 
130         def setNetmask(self, ip):
131                 pass
132                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
133                 #self.writeNetworkConfig()              
134
135         def getCurrentIP(self):
136                 ipstr = [0,0,0,0]
137                 for x in os.popen("ifconfig "+self.iface+" | grep 'inet addr:'", "r").readline().split(' '):
138                         if x.split(':')[0] == "addr":
139                                 ipstr = x.split(':')[1].split('.')
140                 ip = []
141                 for x in ipstr:
142                         ip.append(int(x))
143                 print "[Network.py] got ip " + str(ip)
144                 return ip
145
146 iNetwork = Network()
147
148 def InitNetwork():
149         config.network = ConfigSubsection()
150         config.network.dhcp = NoSave(ConfigYesNo(default=True))
151         config.network.ip = NoSave(ConfigIP(default=iNetwork.getCurrentIP()))
152         config.network.netmask = NoSave(ConfigIP(default=[255,255,255,0]))
153         config.network.gateway = NoSave(ConfigIP(default=[192,168,1,3]))
154         config.network.dns = NoSave(ConfigIP(default=[192,168,1,3]))
155         config.network.mac = NoSave(ConfigMAC(default=[00,11,22,33,44,55]))
156
157         iNetwork.loadNetworkConfig()
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)