summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-connection_setting_ipv4.cpp
blob: 15f897889e6e5bb82d351fd34f4e6d744622f95e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***************************************************************************
 *
 * knetworkmanager-devicestore_dbus.cpp - A NetworkManager frontend for KDE
 *
 * Copyright (C) 2005, 2006 Novell, Inc.
 *
 * Author: Helmut Schaa <hschaa@suse.de>, <helmut.schaa@gmx.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

/* qt headers */
#include <tqhostaddress.h>
#include <tqvariant.h>

/* kde headers */
#include <kdebug.h>
#include <klocale.h>

/* TQT_DBus headers*/
#include <tqdbusdata.h>
#include <tqdbusdatamap.h>

/* knetworkmanager headers */
#include "knetworkmanager.h"
#include "knetworkmanager-connection_setting_ipv4.h"


using namespace ConnectionSettings;

// reverse order the bytes
TQ_UINT32 swap32(TQ_UINT32 x)
{
	TQ_UINT32 ret = 0;

	TQ_UINT8* from = (TQ_UINT8*) &x;
	TQ_UINT8* to   = (TQ_UINT8*) &ret;

	for (int i = 0; i < 4; ++i)
		to[3-i] = from[i];
	return ret;
}

/*
	class IPv4
*/
IPv4::IPv4(Connection* conn)
	: ConnectionSetting(conn, NM_SETTING_IP4_CONFIG_SETTING_NAME)
{
	_method = METHOD_DHCP;
	_ignore_auto_dns = false;
	_ignore_auto_routes = false;
}

TQValueList<IPv4Address> IPv4::getAddresses() const
{
	return _addresses;
}

void IPv4::setAddresses(const TQValueList<IPv4Address> & adr) 
{
	_addresses = adr;
	emitValidityChanged();
}

TQValueList<TQHostAddress> IPv4::getDNS() const
{
	return _dns;
}

void IPv4::setDNS(const TQValueList<TQHostAddress>& dns)
{
	_dns = dns;
	emitValidityChanged();
}

TQStringList IPv4::getDNSSearch() const
{
	return _dns_search;
}

void IPv4::setDNSSearch(const TQStringList & dnsSearch)
{
	_dns_search = dnsSearch;
	emitValidityChanged();
}

void
IPv4::setMethod(IPV4METHOD method)
{
	_method = method;
	emitValidityChanged();
}

IPv4::IPV4METHOD
IPv4::getMethod() const
{
	return _method;
}

void
IPv4::setIgnoreAutoDNS(bool ignore)
{
	_ignore_auto_dns = ignore;
	emitValidityChanged();
}

bool
IPv4::getIgnoreAutoDNS() const
{
	return _ignore_auto_dns;
}

void
IPv4::setIgnoreAutoRoutes(bool ignore)
{
	_ignore_auto_routes = ignore;
	emitValidityChanged();
}

bool
IPv4::getIgnoreAutoRoutes() const
{
	return _ignore_auto_routes;
}

bool
IPv4::isValid() const
{
	if (_method == METHOD_MANUAL)
	{
		// only check the manual settings

		// at least one address has to be specified
		if (_addresses.empty())
			return false;

		// check every address
		for (TQValueList<IPv4Address>::ConstIterator it = _addresses.begin(); it != _addresses.end(); ++it)
		{
			if ((*it).address.isNull())
				return false;
			if ((*it).netmask.isNull())
				return false;
			// no need to check gateway as it is optional
		}

		// check DNS addresses but may be empty
		for (TQValueList<TQHostAddress>::ConstIterator it = _dns.begin(); it != _dns.end(); ++it)
		{
			if ((*it).isNull())
				return false;
		}

		// don't check DNS Search because it is optional ...
	}

	return true;
}

SettingsMap
IPv4::toMap() const
{
	SettingsMap map;

	if (_method == METHOD_DHCP)
		map.insert(NM_SETTING_IP4_CONFIG_METHOD, TQT_DBusData::fromString(NM_SETTING_IP4_CONFIG_METHOD_AUTO));
	else if (_method == METHOD_AUTOIP)
		map.insert(NM_SETTING_IP4_CONFIG_METHOD, TQT_DBusData::fromString(NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL));
	else if (_method == METHOD_SHARED)
		map.insert(NM_SETTING_IP4_CONFIG_METHOD, TQT_DBusData::fromString(NM_SETTING_IP4_CONFIG_METHOD_SHARED));
	else if (_method == METHOD_MANUAL)
	{
		map.insert(NM_SETTING_IP4_CONFIG_METHOD, TQT_DBusData::fromString(NM_SETTING_IP4_CONFIG_METHOD_MANUAL));

		// DNS search
		if (_dns_search.size() > 0)
		{
			TQValueList<TQT_DBusData> dns_search;
			TQStringList::ConstIterator it = _dns_search.begin();
			for(;it != _dns_search.end(); ++it)
				dns_search.append(TQT_DBusData::fromString(*it));
	
			map.insert(NM_SETTING_IP4_CONFIG_DNS_SEARCH, TQT_DBusData::fromTQValueList(dns_search));
		}

		// DNS addresses
		if (_dns.size() > 0)
		{
			TQValueList<TQT_DBusData> dns;
			TQValueList<TQHostAddress>::ConstIterator it_dns = _dns.begin();
			// the strange swap32 is needed as NM reads the address exactly the other way round as TQt
			for(;it_dns != _dns.end(); ++it_dns)
				dns.append(TQT_DBusData::fromUInt32(swap32((*it_dns).toIPv4Address())));
		
			map.insert(NM_SETTING_IP4_CONFIG_DNS, TQT_DBusData::fromTQValueList(dns));
		}

		// IP's
		if (_addresses.size() > 0)
		{
			TQValueList<TQT_DBusData> ips;
			for (TQValueList<IPv4Address>::ConstIterator it = _addresses.begin(); it != _addresses.end(); ++it)
			{
				TQValueList<TQT_DBusData> cur_ip;
				cur_ip.append(TQT_DBusData::fromUInt32(swap32((*it).address.toIPv4Address())));
				cur_ip.append(TQT_DBusData::fromUInt32(toCIDRSuffix((*it).netmask)));
				if (!(*it).gateway.isNull())
					cur_ip.append(TQT_DBusData::fromUInt32(swap32((*it).gateway.toIPv4Address())));
				ips.append(TQT_DBusData::fromTQValueList(cur_ip));
			}
			map.insert(NM_SETTING_IP4_CONFIG_ADDRESSES, TQT_DBusData::fromTQValueList(ips));
		}
	}
	map.insert(NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, TQT_DBusData::fromBool(_ignore_auto_routes));
	map.insert(NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, TQT_DBusData::fromBool(_ignore_auto_dns));
	return map;
}

void
IPv4::fromMap(const SettingsMap& map)
{
	SettingsMap::ConstIterator it;

	if ((it = map.find(NM_SETTING_IP4_CONFIG_METHOD)) != map.end())
	{
		if (it.data().toString() == NM_SETTING_IP4_CONFIG_METHOD_AUTO || it.data().toString() == "dhcp")
			_method = METHOD_DHCP;
		else if (it.data().toString() == NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL || it.data().toString() == "autoip")
			_method = METHOD_AUTOIP;
		else if (it.data().toString() == NM_SETTING_IP4_CONFIG_METHOD_SHARED)
			_method = METHOD_SHARED;
		else if (it.data().toString() == NM_SETTING_IP4_CONFIG_METHOD_MANUAL)
			_method = METHOD_MANUAL;
	}

	// DNS search
	if ((it = map.find(NM_SETTING_IP4_CONFIG_DNS_SEARCH)) != map.end())
	{
		TQValueList<TQT_DBusData> dns_search = it.data().toTQValueList();
		for (TQValueList<TQT_DBusData>::Iterator it = dns_search.begin(); it != dns_search.end(); ++it)
		{
			_dns_search.append( (*it).toString());
		}
	}

	// DNS addresses
	if ((it = map.find(NM_SETTING_IP4_CONFIG_DNS)) != map.end())
	{
		TQValueList<TQT_DBusData> dns = it.data().toTQValueList();
		for (TQValueList<TQT_DBusData>::Iterator it = dns.begin(); it != dns.end(); ++it)
		{
			_dns.append( TQHostAddress(swap32((*it).toUInt32())) );
		}
	}

	// IP's
	if ((it = map.find(NM_SETTING_IP4_CONFIG_ADDRESSES)) != map.end())
	{
		TQValueList<TQT_DBusData> ips = it.data().toTQValueList();
		for (TQValueList<TQT_DBusData>::Iterator it2 = ips.begin(); it2 != ips.end(); ++it2)
		{
			TQValueList<TQT_DBusData> cur_ip = (*it2).toTQValueList();
			IPv4Address address;

			address.address = swap32(cur_ip[0].toUInt32());

			if (cur_ip[1].toUInt32() >= 0 && cur_ip[1].toUInt32() <= 32)
				address.netmask = fromCIDRSuffix(cur_ip[1].toUInt32());
			else
				address.netmask = swap32(cur_ip[1].toUInt32());

			if (cur_ip.size() > 2)
				address.gateway = swap32(cur_ip[2].toUInt32());
		
			_addresses.append(address);
		}
	}

	if ((it = map.find(NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES)) != map.end())
		_ignore_auto_routes = it.data().toBool();

	if ((it = map.find(NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS)) != map.end())
		_ignore_auto_dns = it.data().toBool();
}

TQ_UINT32 IPv4::toCIDRSuffix(const TQHostAddress& adr) const
{
	TQ_UINT32 netmask = adr.toIPv4Address();
	TQ_UINT32 suffix = 0;
	while (netmask > 0)
	{
		suffix++;
		netmask = netmask << 1;
	}
	return suffix;
}

TQHostAddress IPv4::fromCIDRSuffix(TQ_UINT32 suffix)
{
	TQ_UINT32 netmask = 0xFFFFFFFF;
	netmask = netmask << (32 - suffix);
	return TQHostAddress(netmask);
}