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