]> git.ipfire.org Git - people/pmueller/ipfire-3.x.git/blob - pkgs/pyfire/src/net.py
94a262a8dbe99da087d719a1e7b2d998897f842e
[people/pmueller/ipfire-3.x.git] / pkgs / pyfire / src / net.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import os
23 import os.path
24 import socket
25 import string
26
27 import hal
28 from config import ConfigFile
29
30 NETWORK_DEVICES="/etc/sysconfig/network-devices/"
31 NETWORK_SETTINGS="/etc/sysconfig/network"
32 NETWORK_SCRIPTS="/etc/init.d/networking/"
33
34 class Network:
35 def __init__(self):
36 self.bridges = {}
37 self.nics = {}
38
39 self.settings = NetworkSettings(NETWORK_SETTINGS)
40
41 def getNics(self):
42 for device in hal.get_devices_by_type("net"):
43 if device.has_key('net.arp_proto_hw_id'):
44 if device['net.arp_proto_hw_id'] == 1 and \
45 not device['info.category'] == 'net.bridge':
46 nic = device['device']
47 self.nics[nic] = NetworkDevice(nic, device)
48 return self.nics
49
50 def getNic(self, nic):
51 return self.nics[nic]
52
53 def getBridges(self, colour=None):
54 for file in os.listdir(NETWORK_DEVICES):
55 bridge = os.path.basename(file)
56 if colour and not bridge.startswith(colour):
57 continue
58 self.bridges[bridge] = BrideDevice(bridge)
59 return self.bridges
60
61 def getBridge(self, bridge):
62 return self.bridges[bridge]
63
64 def addBridge(self, dev):
65 self.bridges[dev] = BridgeDevice(dev)
66
67 def delBridge(self, dev):
68 self.bridges[dev].remove()
69 del self.bridges[dev]
70
71 def write(self):
72 for bridge in self.bridges.items():
73 self.bridges[bridge].write()
74 self.settings.write()
75
76
77 class NetworkSettings(ConfigFile):
78 def __init__(self, filename):
79 ConfigFile.__init__(self, filename)
80
81 def getHostname(self):
82 return self.get("HOSTNAME")
83
84 def setHostname(self, hostname):
85 self.set(("HOSTNAME", hostname))
86
87
88 class BridgeDevice:
89 def __init__(self, dev):
90 self.filename = "%s/%s/" % (NETWORK_DEVICES, dev,)
91 self.device = dev
92 self.services = []
93
94 for file in os.listdir(self.filename):
95 service = Service(file, bridge=self.device)
96 self.addService(service)
97
98 def addService(self, service):
99 self.services.append(service)
100
101 def getPolicy(self, service):
102 ret = []
103 for service in self.services:
104 if service.service == service:
105 ret.append(service)
106 return ret
107
108 def delService(self, service):
109 pass # how to do this?
110
111 def addNic(self, nic):
112 # requires a NetworkDevice instance
113 filename = os.path.join(self.filename, nic.getDevice())
114 service = Service(filename, bridge=self.name, service="bridge-slave")
115 service.set(("DESCRIPTION", nic.getDescription()),
116 ("MAC", nic.getMac()))
117 self.addService(service)
118
119 def write(self):
120 if not os.path.isdir(fn):
121 os.makedirs(fn)
122
123 # Save all service information
124 for service in self.services:
125 service.write()
126
127
128 class NetworkDevice:
129 def __init__(self, dev, dbus):
130 self.device = dev
131 self.dbus = dbus
132
133 def __str__(self):
134 return "%s (%s) - %s" % (self.device, self.getDescription(), self.getMac())
135
136 def getDevice(self):
137 return self.device
138
139 def getMac(self):
140 return self.dbus["net.address"]
141
142 def getDescription(self):
143 return self.dbus["description"]
144
145
146 class ServiceError(Exception):
147 pass
148
149
150 class Service(ConfigFile):
151 def __init__(self, filename, bridge=None, service=None):
152 self.service = None
153 if service:
154 self.setService(service)
155 ConfigFile.__init__(self, filename)
156 self.bridge = bridge
157 self.name = os.path.basename(self.filename)
158
159 def setService(self, service):
160 if not service in listAllServices():
161 raise ServiceError, "The given service is not available: %s" % service
162 self.service = service
163
164
165 def listAllServices():
166 ret = []
167 for service in os.listdir(NETWORK_SCRIPTS + "services/"):
168 ret.append(service)
169 return ret
170
171 if __name__ == "__main__":
172 network = Network()
173 print "All available nics on this system:"
174 for nic, obj in network.getNics().items():
175 print obj