add debug-msg
[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                 ip = [0, 0, 0, 0]
124                 try:
125                         print gethostbyname(gethostname())
126                         ip = gethostbyname(gethostname()).split('.')
127                 except:
128                         print "[Network.py] Could not get current ip (not necessarily an error)"
129                 print "[Network.py] got ip " + str(ip)
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)