get network config from /etc/network/interfaces
[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                         ifaces = {}
30                         currif = ""
31                         for i in interfaces:
32                                 split = i.strip().split(' ')
33                                 if (split[0] == "iface"):
34                                         currif = split[1]
35                                         ifaces[currif] = {}
36                                         if (len(split) == 4 and split[3] == "dhcp"):
37                                                 ifaces[currif]["dhcp"] = "yes"
38                                         else:
39                                                 ifaces[currif]["dhcp"] = "no"
40                                 if (currif != ""):
41                                         if (split[0] == "address"):
42                                                 ifaces[currif]["address"] = map(int, split[1].split('.'))
43                                         if (split[0] == "netmask"):
44                                                 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
45                                         if (split[0] == "gateway"):
46                                                 ifaces[currif]["gateway"] = map(int, split[1].split('.'))                                                                               
47                         
48                         # set this config
49                         if (ifaces.has_key("eth0")):
50                                 if (ifaces["eth0"]["dhcp"] == "yes"):
51                                         config.network.dhcp.value = 1
52                                 else:
53                                         config.network.dhcp.value = 0
54                                 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
55                                 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
56                                 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
57                         fp.close()
58                 except:
59                         pass
60
61         def activateNetworkConfig(self):
62                 import os
63                 os.system("/etc/init.d/networking restart")
64                 
65         def setDHCP(self, useDHCP):
66                 if (useDHCP):
67                         print "Using DHCP"
68                         config.network.ip.enabled = False
69                         config.network.netmask.enabled = False
70                         config.network.gateway.enabled = False
71                         config.network.dns.enabled = False
72                 else:
73                         print "NOT using DHCP"
74                         config.network.ip.enabled = True
75                         config.network.netmask.enabled = True
76                         config.network.gateway.enabled = True
77                         config.network.dns.enabled = True
78                                         
79         def setIPNameserver(self, ip):
80                 return
81                 resolvconf = file('/etc/resolv.conf', 'w')
82                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
83                 resolvconf.close()
84                 
85         def setMACAddress(self, mac):
86                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
87                 pass
88                 
89         def setIPAddress(self, ip):
90                 pass
91                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
92                 #self.writeNetworkConfig()
93
94         def setGateway(self, ip):
95                 pass
96                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
97                 #self.writeNetworkConfig()
98                 
99         def setNetmask(self, ip):
100                 pass
101                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
102                 #self.writeNetworkConfig()              
103
104
105 iNetwork = Network()
106
107 def InitNetwork():
108         ip = map (int, gethostbyname(gethostname()).split('.'))
109                 
110         config.network = ConfigSubsection()
111         config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, ("no", "yes"))
112         config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, (("."), (1,255)))
113         config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], (("."), (1,255)))
114         config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], (("."), (1,255)))
115         config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], (("."), (1,255)))
116         config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], ((":"), (1,255)))
117
118         iNetwork.loadNetworkConfig()
119         
120         #FIXME using this till other concept for this is implemented
121         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
122         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
123
124
125         def writeNetworkConfig(configElement):
126                 iNetwork.writeNetworkConfig()
127                 
128         def setIPAddress(configElement):
129                 iNetwork.setIPAddress(configElement.value)
130
131         def setGateway(configElement):
132                 iNetwork.setGateway(configElement.value)
133
134         def setNetmask(configElement):
135                 iNetwork.setNetmask(configElement.value)                
136
137         def setDHCP(configElement):
138                 iNetwork.setDHCP(configElement.value)
139
140         def setIPNameserver(configElement):
141                 iNetwork.setIPNameserver(configElement.value)
142
143         def setMACAddress(configElement):
144                 iNetwork.setMACAddress(configElement.value)
145
146
147         # this will call the "setup-val" initial
148         config.network.dhcp.addNotifier(setDHCP)
149         config.network.ip.addNotifier(setIPAddress)
150         config.network.netmask.addNotifier(setNetmask)  
151         config.network.gateway.addNotifier(setGateway)
152         config.network.dns.addNotifier(setIPNameserver)
153         config.network.mac.addNotifier(setMACAddress)