Add source and converter for streaming. Source/StreamService will start the streaming...
[enigma2.git] / lib / python / Components / Network.py
1 from config import config, ConfigYesNo, ConfigIP, NoSave, ConfigSubsection, ConfigMAC
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 lo\n")
15                 fp.write("iface lo inet loopback\n\n")
16                 fp.write("auto eth0\n")
17                 if config.network.dhcp.value:
18                         fp.write("iface eth0 inet dhcp\n")
19                 else:
20                         fp.write("iface eth0 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("eth0")):
73                                 if (ifaces["eth0"]["dhcp"] == "yes"):
74                                         config.network.dhcp.value = 1
75                                 else:
76                                         config.network.dhcp.value = 0
77                                 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
78                                 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
79                                 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
80                 except:
81                         print "[Network.py] parsing network failed"
82
83         def deactivateNetworkConfig(self):
84                 os.system("ip addr flush eth0")
85                 os.system("/etc/init.d/networking stop")
86                 os.system("killall -9 udhcpc")
87                 os.system("rm /var/run/udhcpc*")
88
89         def activateNetworkConfig(self):
90                 os.system("/etc/init.d/networking start")
91                 config.network.ip.value = self.getCurrentIP()
92                 config.network.ip.save()
93                 
94         def setDHCP(self, useDHCP):
95                 if (useDHCP):
96                         print "Using DHCP"
97                         config.network.ip.enabled = False
98                         config.network.netmask.enabled = False
99                         config.network.gateway.enabled = False
100                         config.network.dns.enabled = False
101                 else:
102                         print "NOT using DHCP"
103                         config.network.ip.enabled = True
104                         config.network.netmask.enabled = True
105                         config.network.gateway.enabled = True
106                         config.network.dns.enabled = True
107                                         
108         def setIPNameserver(self, ip):
109                 return
110                 resolvconf = file('/etc/resolv.conf', 'w')
111                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
112                 resolvconf.close()
113                 
114         def setMACAddress(self, mac):
115                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
116                 pass
117                 
118         def setIPAddress(self, ip):
119                 pass
120                 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
121                 #self.writeNetworkConfig()
122
123         def setGateway(self, ip):
124                 pass
125                 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
126                 #self.writeNetworkConfig()
127                 
128         def setNetmask(self, ip):
129                 pass
130                 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))                
131                 #self.writeNetworkConfig()              
132
133         def getCurrentIP(self):
134                 ipstr = [0,0,0,0]
135                 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
136                         if x.split(':')[0] == "addr":
137                                 ipstr = x.split(':')[1].split('.')
138                 ip = []
139                 for x in ipstr:
140                         ip.append(int(x))
141                 print "[Network.py] got ip " + str(ip)
142                 return ip
143
144 iNetwork = Network()
145
146 def InitNetwork():
147         config.network = ConfigSubsection()
148         config.network.dhcp = NoSave(ConfigYesNo(default=True))
149         config.network.ip = NoSave(ConfigIP(default=iNetwork.getCurrentIP()))
150         config.network.netmask = NoSave(ConfigIP(default=[255,255,255,0]))
151         config.network.gateway = NoSave(ConfigIP(default=[192,168,1,3]))
152         config.network.dns = NoSave(ConfigIP(default=[192,168,1,3]))
153         config.network.mac = NoSave(ConfigMAC(default=[00,11,22,33,44,55]))
154
155         iNetwork.loadNetworkConfig()
156         
157         def writeNetworkConfig(configElement):
158                 iNetwork.writeNetworkConfig()
159                 
160         def setIPAddress(configElement):
161                 iNetwork.setIPAddress(configElement.value)
162
163         def setGateway(configElement):
164                 iNetwork.setGateway(configElement.value)
165
166         def setNetmask(configElement):
167                 iNetwork.setNetmask(configElement.value)                
168
169         def setDHCP(configElement):
170                 iNetwork.setDHCP(configElement.value)
171
172         def setIPNameserver(configElement):
173                 iNetwork.setIPNameserver(configElement.value)
174
175         def setMACAddress(configElement):
176                 iNetwork.setMACAddress(configElement.value)
177
178
179         # this will call the "setup-val" initial
180         config.network.dhcp.addNotifier(setDHCP)
181         config.network.ip.addNotifier(setIPAddress)
182         config.network.netmask.addNotifier(setNetmask)  
183         config.network.gateway.addNotifier(setGateway)
184         config.network.dns.addNotifier(setIPNameserver)
185         config.network.mac.addNotifier(setMACAddress)