]>
git.ipfire.org Git - people/pmueller/ipfire-3.x.git/blob - pkgs/pyfire/src/net.py
94a262a8dbe99da087d719a1e7b2d998897f842e
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
28 from config
import ConfigFile
30 NETWORK_DEVICES
="/etc/sysconfig/network-devices/"
31 NETWORK_SETTINGS
="/etc/sysconfig/network"
32 NETWORK_SCRIPTS
="/etc/init.d/networking/"
39 self
.settings
= NetworkSettings(NETWORK_SETTINGS
)
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
)
50 def getNic(self
, nic
):
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
):
58 self
.bridges
[bridge
] = BrideDevice(bridge
)
61 def getBridge(self
, bridge
):
62 return self
.bridges
[bridge
]
64 def addBridge(self
, dev
):
65 self
.bridges
[dev
] = BridgeDevice(dev
)
67 def delBridge(self
, dev
):
68 self
.bridges
[dev
].remove()
72 for bridge
in self
.bridges
.items():
73 self
.bridges
[bridge
].write()
77 class NetworkSettings(ConfigFile
):
78 def __init__(self
, filename
):
79 ConfigFile
.__init
__(self
, filename
)
81 def getHostname(self
):
82 return self
.get("HOSTNAME")
84 def setHostname(self
, hostname
):
85 self
.set(("HOSTNAME", hostname
))
89 def __init__(self
, dev
):
90 self
.filename
= "%s/%s/" % (NETWORK_DEVICES
, dev
,)
94 for file in os
.listdir(self
.filename
):
95 service
= Service(file, bridge
=self
.device
)
96 self
.addService(service
)
98 def addService(self
, service
):
99 self
.services
.append(service
)
101 def getPolicy(self
, service
):
103 for service
in self
.services
:
104 if service
.service
== service
:
108 def delService(self
, service
):
109 pass # how to do this?
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
)
120 if not os
.path
.isdir(fn
):
123 # Save all service information
124 for service
in self
.services
:
129 def __init__(self
, dev
, dbus
):
134 return "%s (%s) - %s" % (self
.device
, self
.getDescription(), self
.getMac())
140 return self
.dbus
["net.address"]
142 def getDescription(self
):
143 return self
.dbus
["description"]
146 class ServiceError(Exception):
150 class Service(ConfigFile
):
151 def __init__(self
, filename
, bridge
=None, service
=None):
154 self
.setService(service
)
155 ConfigFile
.__init
__(self
, filename
)
157 self
.name
= os
.path
.basename(self
.filename
)
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
165 def listAllServices():
167 for service
in os
.listdir(NETWORK_SCRIPTS
+ "services/"):
171 if __name__
== "__main__":
173 print "All available nics on this system:"
174 for nic
, obj
in network
.getNics().items():