e3077d0039f45723a44ec3df0e363c86e49b8fe0
[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                 
82         def setDHCP(self, useDHCP):
83                 if (useDHCP):
84                         print "Using DHCP"
85                         config.network.ip.enabled = False
86                         config.network.netmask.enabled = False
87                         config.network.gateway.enabled = False
88                         config.network.dns.enabled = False
89                 else:
90                         print "NOT using DHCP"
91                         config.network.ip.enabled = True
92                         config.network.netmask.enabled = True
93                         config.network.gateway.enabled = True
94                         config.network.dns.enabled = True
95                                         
96         def setIPNameserver(self, ip):
97                 return
98                 resolvconf = file('/etc/resolv.conf', 'w')
99                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
100                 resolvconf.close()
101                 
102         def setMACAddress(self, mac):
103                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
104                 pass
105                 
106         def setIPAddress(self, ip):
107                 pass
108                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
109                 #self.writeNetworkConfig()
110
111         def setGateway(self, ip):
112                 pass
113                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
114                 #self.writeNetworkConfig()
115                 
116         def setNetmask(self, ip):
117                 pass
118                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
119                 #self.writeNetworkConfig()              
120
121
122 iNetwork = Network()
123
124 def InitNetwork():
125         try:
126                 ip = [0, 0, 0, 0]
127                 print gethostbyname(gethostname())
128                 ip = gethostbyname(gethostname()).split('.')
129                 print ip
130         except:
131                 print "[Network.py] Could not get current ip (not necessarily an error)"
132                 
133         config.network = ConfigSubsection()
134         config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (_("no"), _("yes")))
135         config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
136         config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
137         config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
138         config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
139         config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
140
141         iNetwork.loadNetworkConfig()
142         
143         #FIXME using this till other concept for this is implemented
144         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
145         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
146
147
148         def writeNetworkConfig(configElement):
149                 iNetwork.writeNetworkConfig()
150                 
151         def setIPAddress(configElement):
152                 iNetwork.setIPAddress(configElement.value)
153
154         def setGateway(configElement):
155                 iNetwork.setGateway(configElement.value)
156
157         def setNetmask(configElement):
158                 iNetwork.setNetmask(configElement.value)                
159
160         def setDHCP(configElement):
161                 iNetwork.setDHCP(configElement.value)
162
163         def setIPNameserver(configElement):
164                 iNetwork.setIPNameserver(configElement.value)
165
166         def setMACAddress(configElement):
167                 iNetwork.setMACAddress(configElement.value)
168
169
170         # this will call the "setup-val" initial
171         config.network.dhcp.addNotifier(setDHCP)
172         config.network.ip.addNotifier(setIPAddress)
173         config.network.netmask.addNotifier(setNetmask)  
174         config.network.gateway.addNotifier(setGateway)
175         config.network.dns.addNotifier(setIPNameserver)
176         config.network.mac.addNotifier(setMACAddress)