1 #ifndef NUT_COMMON_CONFIG_H
2 #define NUT_COMMON_CONFIG_H
8 #include <QHostAddress>
10 #include <QDBusArgument>
12 #include "macaddress.h"
18 namespace libnutcommon
{
23 class EnvironmentConfig
;
26 QDBusArgument
&operator<< (QDBusArgument
&argument
, const Config
&data
);
27 const QDBusArgument
&operator>> (const QDBusArgument
&argument
, Config
&data
);
29 QDBusArgument
&operator<< (QDBusArgument
&argument
, const DeviceConfig
&data
);
30 const QDBusArgument
&operator>> (const QDBusArgument
&argument
, DeviceConfig
&data
);
32 QDBusArgument
&operator<< (QDBusArgument
&argument
, const SelectRule
&data
);
33 const QDBusArgument
&operator>> (const QDBusArgument
&argument
, SelectRule
&data
);
35 QDBusArgument
&operator<< (QDBusArgument
&argument
, const SelectConfig
&data
);
36 const QDBusArgument
&operator>> (const QDBusArgument
&argument
, SelectConfig
&data
);
38 QDBusArgument
&operator<< (QDBusArgument
&argument
, const EnvironmentConfig
&data
);
39 const QDBusArgument
&operator>> (const QDBusArgument
&argument
, EnvironmentConfig
&data
);
41 QDBusArgument
&operator<< (QDBusArgument
&argument
, const IPv4Config
&data
);
42 const QDBusArgument
&operator>> (const QDBusArgument
&argument
, IPv4Config
&data
);
45 namespace libnutcommon
{
48 * @brief Used as a member in other classes, it determines wheter that class is a original or a copy.
50 * On normal construct (-> default constructor) without params,
51 * the instance evals to False (no copy)
52 * On copy, it changes to true.
53 * So obviously this breaks the idea that a copy is equal to the original.
54 * This helps you to build copy-constructor (or to remove them), and do
55 * some cleanups only in the original destructor.
57 * In a ideal world we would never copy objects which need this behaviour,
58 * but in conjunction with the Qt DBus layer this is helpful.
63 CopyMark(bool isCopy
) : m_isCopy(isCopy
) { }
65 CopyMark() : m_isCopy(false) { }
66 CopyMark(const CopyMark
&) : m_isCopy(true) { }
67 operator bool () { return m_isCopy
; }
69 static CopyMark
Copy() { return CopyMark(true); }
70 static CopyMark
NoCopy() { return CopyMark(false); }
74 /** @brief Container for all \link DeviceConfig device configs\endlink. A deviceName may contain wildcards (not supported yet).
78 internal::CopyMark m_isCopy
;
81 friend class nuts::ConfigParser
;
82 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const Config
&data
);
83 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, Config
&data
);
85 QHash
<QString
, DeviceConfig
*> m_devices
;
91 DeviceConfig
* getDevice(const QString
&deviceName
) {
92 return m_devices
.value(deviceName
, 0);
95 const QHash
<QString
, DeviceConfig
*> &getDevices() {
100 /** @brief Each device has a list of \link EnvironmentConfig Environments\endlink and some additional config values.
102 * Each device can have many Environments, but it can only be in one; each
103 * Environment consists of zero, one or more Interfaces.
105 * Which Environment is taken is controlled by the SelectRules of an Environment.
107 * For WLAN devices supported by wpa_supplicant you can specify a configfile and a driver (wext by default).
111 internal::CopyMark m_isCopy
;
114 friend class nuts::ConfigParser
;
115 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const DeviceConfig
&data
);
116 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, DeviceConfig
&data
);
118 QList
<EnvironmentConfig
*> m_environments
;
120 QString m_wpaConfigFile
;
125 virtual ~DeviceConfig();
127 const QList
<EnvironmentConfig
*>& getEnvironments() {
128 return m_environments
;
131 bool noAutoStart() { return m_noAutoStart
; }
133 QString
wpaConfigFile() { return m_wpaConfigFile
; }
134 QString
wpaDriver() { return m_wpaDriver
; }
137 /** @brief Result type of a select test.
143 False
= 0, //!< Cannot select
144 User
= 1, //!< Do select only if user does want it
145 NotUser
= 2, //!< Should not happen; "only select if user does not want it..." is not good
149 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const SelectResult
&data
);
150 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, SelectResult
&data
);
155 SelectResult(bool_t value
= False
)
158 SelectResult
& operator = (bool_t value
) {
162 SelectResult
& operator = (const SelectResult
&other
) {
163 m_value
= other
.m_value
;
166 SelectResult
& operator = (qint8 value
) {
167 m_value
= (bool_t
) value
;
171 SelectResult
operator || (const SelectResult
&other
) {
172 const bool_t op_or
[16] = {
173 False
, User
, NotUser
, True
,
174 User
, User
, True
, True
,
175 NotUser
, True
, NotUser
, True
,
176 True
, True
, True
, True
178 return op_or
[m_value
*4 + other
.m_value
];
180 SelectResult
operator && (const SelectResult
&other
) {
181 const bool_t op_and
[16] = {
182 False
, False
, False
, False
,
183 False
, User
, False
, User
,
184 False
, False
, NotUser
, NotUser
,
185 False
, User
, NotUser
, True
187 return op_and
[m_value
*4 + other
.m_value
];
190 operator bool_t () const {
194 operator qint8 () const {
195 return (qint8
) m_value
;
198 SelectResult
operator !() const {
199 return (SelectResult::bool_t
) (3 - m_value
);
203 /** @brief A select operation.
208 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const SelectRule
&data
);
209 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, SelectRule
&data
);
213 SEL_USER
, //!< Select if user does want it (=> return SelectResult::User)
214 SEL_ARP
, //!< Select if ipAddr is found on the network from the device; if macAddr != 0 it is matched too.
215 SEL_ESSID
, //!< Select if in wlan essid;
216 SEL_AND_BLOCK
, //!< Select a list of \link SelectRule SelectRules\endlink, results combined with AND
217 SEL_OR_BLOCK
//!< Select a list of \link SelectRule SelectRules\endlink, results combined with OR
220 SelectRule() : invert(false), selType(SEL_USER
) { }
221 SelectRule(const QHostAddress
&ipAddr
, bool invert
= false) : invert(invert
), selType(SEL_ARP
), ipAddr(ipAddr
) { }
222 SelectRule(const QHostAddress
&ipAddr
, const libnutcommon::MacAddress
&macAddr
, bool invert
= false) : invert(invert
), selType(SEL_ARP
), ipAddr(ipAddr
), macAddr(macAddr
) { }
223 SelectRule(const QString
&essid
, bool invert
= false) : invert(invert
), selType(SEL_ESSID
), essid(essid
) { }
224 SelectRule(quint32 block
, SelectType blockType
, bool invert
= false) : invert(invert
), selType(blockType
), block(block
) { }
226 bool invert
; //!< Invert result; unused for now.
228 quint32 block
; //!< Block identifier in SelectConfig for SEL_*_BLOCK
231 libnutcommon::MacAddress macAddr
;
234 /** @brief SelectConfig for an environment.
236 * This structure represents a tree of SelectRules; try qnut for visualization.
239 friend class nuts::ConfigParser
;
240 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const SelectConfig
&data
);
241 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, SelectConfig
&data
);
246 QVector
<SelectRule
> filters
; //!< List of \link SelectRule SelectRules\endlink
247 //! List of blocks; each block is a list of filter ids.
248 //! The type of the block (AND/OR) is specified in the rule for the block
249 QVector
< QVector
<quint32
> > blocks
;
252 /** @brief Each EnvironmentConfig of a \link DeviceConfig device\endlink has a list
253 * of \link IPv4Config interfaces\endlink, which configure the ips.
255 * And it contains a SelectConfig, which determines which Environment is loaded for a device.
257 class EnvironmentConfig
{
259 internal::CopyMark m_isCopy
;
261 friend class nuts::ConfigParser
;
262 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const EnvironmentConfig
&data
);
263 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, EnvironmentConfig
&data
);
266 QList
<IPv4Config
*> m_ipv4Interfaces
;
267 SelectConfig m_select
;
270 EnvironmentConfig(const QString
&name
= "");
271 virtual ~EnvironmentConfig();
273 QString
getName() { return m_name
; } //!< A description for that environment. It does not have to be unique.
274 const QList
<IPv4Config
*>& getIPv4Interfaces() { return m_ipv4Interfaces
; }
275 const SelectConfig
&getSelect() { return m_select
; }
278 /** @brief Each IPv4Config stands for one ip of an interface.
280 * There are several methods how to to this, and some
281 * additional values: netmask, gateway, dns-servers.
283 * If the interface has the method IPv4Config::DO_STATIC,
284 * the configured values for ip/netmask/... can be queried
285 * with the corresponding functions.
289 /** @brief Selects which method is used to determine the ip address.
291 * Not all flags can be set:
292 * assert(USERSTATIC xor (DHCP or (ZEROCONF xor STATIC)))
295 DO_DHCP
= 1, //!< Find ip/gateway/dns via DHCP
296 DO_ZEROCONF
= 2, //!< Probe for ip in the IPv4 Link-Local range (RFC 3927).
297 DO_STATIC
= 4, //!< Use values from config file.
298 DO_USERSTATIC
= 8 //!< Use values specified at runtime by a user
301 /** @brief Unused/Unsupported. Could be used to overwrite some value with static configured ones.
312 friend class nuts::ConfigParser
;
313 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const IPv4Config
&data
);
314 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, IPv4Config
&data
);
316 QHostAddress m_static_ip
, m_static_netmask
, m_static_gateway
;
317 QList
<QHostAddress
> m_static_dnsservers
;
320 int m_overwriteFlags
;
322 bool m_continue_dhcp
;
325 IPv4Config(int flags
= IPv4Config::DO_DHCP
| IPv4Config::DO_ZEROCONF
, int overwriteFlags
= 0);
327 const QHostAddress
& getStaticIP() const { return m_static_ip
; }
328 const QHostAddress
& getStaticNetmask() const { return m_static_netmask
; }
329 const QHostAddress
& getStaticGateway() const { return m_static_gateway
; }
330 const QList
<QHostAddress
>& getStaticDNS() const { return m_static_dnsservers
; }
332 Flags
getFlags() const { return (Flags
) m_flags
; }
333 OverwriteFlags
getOverwriteFlags() const { return (OverwriteFlags
) m_overwriteFlags
; }
334 int getTimeOut() const { return m_timeout
; }
337 /** @brief If an interface has to be configured by the user (IPv4Config::DO_USERSTATIC), he/she has to
338 * set that information with this class.
341 class IPv4UserConfig
{
343 friend QDBusArgument
&operator<< (QDBusArgument
&argument
, const IPv4UserConfig
&data
);
344 friend const QDBusArgument
&operator>> (const QDBusArgument
&argument
, IPv4UserConfig
&data
);
346 QHostAddress m_ip
, m_netmask
, m_gateway
;
347 QList
<QHostAddress
> m_dnsservers
;
350 const QHostAddress
& ip() const { return m_ip
; }
351 bool setIP(const QHostAddress
&ip
) { m_ip
= ip
; return true; }
353 const QHostAddress
& netmask() const { return m_netmask
; }
354 bool setNetmask(const QHostAddress
&netmask
) { m_netmask
= netmask
; return true; }
356 const QHostAddress
& gateway() const { return m_gateway
; }
357 bool setGateway(const QHostAddress
&gateway
) { m_gateway
= gateway
; return true; }
359 const QList
<QHostAddress
>& dnsservers() const { return m_dnsservers
; }
360 bool setDnsservers(const QList
<QHostAddress
>& dnsservers
) { m_dnsservers
= dnsservers
; return true; }
362 /** @brief A very basic check if the configuration is valid.
365 return !m_ip
.isNull();
370 Q_DECLARE_METATYPE(libnutcommon::Config
)
371 Q_DECLARE_METATYPE(libnutcommon::DeviceConfig
)
372 Q_DECLARE_METATYPE(libnutcommon::SelectResult
)
373 Q_DECLARE_METATYPE(QVector
< libnutcommon::SelectResult
>)
374 Q_DECLARE_METATYPE(libnutcommon::SelectRule
)
375 Q_DECLARE_METATYPE(libnutcommon::SelectConfig
)
376 Q_DECLARE_METATYPE(libnutcommon::EnvironmentConfig
)
377 Q_DECLARE_METATYPE(libnutcommon::IPv4Config
)
378 Q_DECLARE_METATYPE(libnutcommon::IPv4UserConfig
)