summaryrefslogtreecommitdiffstats
path: root/krfb/srvloc/kinetinterface.cpp
blob: 7ba5c15b14b0a9d55ed3d0a40eb10123449c5d4d (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
/*
 *  Represents an Inet interface
 *  Copyright (C) 2002 Tim Jansen <tim@tjansen.de>
 *
 *  $Id$
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include "kinetinterface.h"

#include "getifaddrs.h"

#include <netinet/in.h>
#include <ksockaddr.h>


class KInetInterfacePrivate {
public:
	QString name;
	int flags;
	KInetSocketAddress *address;
	KInetSocketAddress *netmask;
	KInetSocketAddress *broadcast;
	KInetSocketAddress *destination;

	KInetInterfacePrivate() :
		flags(0),
		address(0),
		netmask(0),
		broadcast(0),
		destination(0) {
	}

	KInetInterfacePrivate(const QString _name, 
			      int _flags, 
			      KInetSocketAddress *_address,
			      KInetSocketAddress *_netmask,
			      KInetSocketAddress *_broadcast,
			      KInetSocketAddress *_destination) :
		name(_name),
		flags(_flags),
		address(_address),
		netmask(_netmask),
		broadcast(_broadcast),
		destination(_destination) {
	}

	~KInetInterfacePrivate() {
		if (address)
			delete address;
		if (netmask)
			delete netmask;
		if (broadcast)
			delete broadcast;
		if (destination)
			delete destination;
	}
	
	KInetInterfacePrivate& operator =(const KInetInterfacePrivate& i) {
		name = i.name;
		flags = i.flags;
		if (i.address)
			address = new KInetSocketAddress(*i.address);
		else
			address = 0;
		if (i.netmask)
			netmask = new KInetSocketAddress(*i.netmask);
		else
			netmask = 0;
		if (i.broadcast)
			broadcast = new KInetSocketAddress(*i.broadcast);
		else
			broadcast = 0;
		if (i.destination)
			destination = new KInetSocketAddress(*i.destination);
		else
			destination = 0;
		return *this;
	}

};


KInetInterface::KInetInterface() :
	d(0) {
}

KInetInterface::KInetInterface(const QString &_name, 
			       int _flags, 
			       KInetSocketAddress *_address,
			       KInetSocketAddress *_netmask,
			       KInetSocketAddress *_broadcast,
			       KInetSocketAddress *_destination) {
	d = new KInetInterfacePrivate(_name, _flags,
				      _address, _netmask, 
				      _broadcast, _destination);
}

KInetInterface::KInetInterface(const KInetInterface &i) :
	d(0) {
	if (!i.d)
		return;

	d = new KInetInterfacePrivate();
	*d = *i.d;
}

KInetInterface::~KInetInterface() {
	if (d)
		delete d;
}

KInetInterface& KInetInterface::operator =(const KInetInterface& i) {
	if (this == &i)
		return *this;

	if (d)
		delete d;
	d = 0;
	if (!i.d)
		return *this;

	d = new KInetInterfacePrivate();
	*d = *i.d;
	return *this;
}

bool KInetInterface::isValid() const {
	return d == 0;
}

QString KInetInterface::displayName() const {
	return d->name;
}

QString KInetInterface::name() const {
	return d->name;
}

int KInetInterface::flags() const {
	return d->flags;
}

KInetSocketAddress *KInetInterface::address() const {
	return d->address;
}

KInetSocketAddress *KInetInterface::netmask() const {
	return d->netmask;
}

KInetSocketAddress *KInetInterface::broadcastAddress() const {
	return d->broadcast;
}

KInetSocketAddress *KInetInterface::destinationAddress() const {
	return d->destination;
}

KInetSocketAddress *KInetInterface::getPublicInetAddress() {
	QValueVector<KInetInterface> v = getAllInterfaces(true);		

	// TODO: first step: take the default route interface

	// try to find point-2-point interface, because it may be
	// a dial-up connection. Take it.
	QValueVector<KInetInterface>::const_iterator it = v.begin();
	while (it != v.end()) {
		if (((*it).flags() & (PointToPoint | Up | Running)) &&
		    (!((*it).flags() & Loopback)) &&
		    (*it).address() &&
            ((*it).address()->family() == AF_INET))
			return new KInetSocketAddress(*(*it).address());
		it++;
	}

	// otherwise, just take the first non-loopback interface
	it = v.begin();
	while (it != v.end()) {
		if (((*it).flags() & (Up | Running)) &&
		    (!((*it).flags() & Loopback)) &&
		    (*it).address() &&
            ((*it).address()->family() == AF_INET))
			return new KInetSocketAddress(*(*it).address());
		it++;
	}

	// ok, giving up, try to take loopback
	it = v.begin();
	while (it != v.end()) {
		if (((*it).flags() & (Up | Running)) &&
		    (*it).address())
			return new KInetSocketAddress(*(*it).address());
		it++;
	}

        // not even loopback..
	return 0;
}

namespace {
	KInetSocketAddress *createAddress(struct sockaddr *a) {
		if (!a)
			return 0;
		else if (a->sa_family == AF_INET)
			return new KInetSocketAddress((struct sockaddr_in*) a,
						      sizeof(struct sockaddr_in));
#ifdef AF_INET6
		else if (a->sa_family == AF_INET6)
			return new KInetSocketAddress((struct sockaddr_in6*) a,
						      sizeof(struct sockaddr_in6));
#endif
		else
			return 0;
	}

	int convertFlags(int flags) {
		int r = 0;
		if (flags & IFF_UP)
			r |= KInetInterface::Up;
		if (flags & IFF_BROADCAST)
			r |= KInetInterface::Broadcast;
		if (flags & IFF_LOOPBACK)
			r |= KInetInterface::Loopback;
		if (flags & IFF_POINTOPOINT)
			r |= KInetInterface::PointToPoint;
		if (flags & IFF_RUNNING)
			r |= KInetInterface::Running;
		if (flags & IFF_MULTICAST)
			r |= KInetInterface::Multicast;

		return r;
	}
}

QValueVector<KInetInterface> KInetInterface::getAllInterfaces(bool includeLoopback) {
	struct kde_ifaddrs *ads;
	struct kde_ifaddrs *a;
	QValueVector<KInetInterface> r;
	if (kde_getifaddrs(&ads))
		return r;

	a = ads;
	while (a) {
		if ((a->ifa_flags & IFF_LOOPBACK) && !includeLoopback) {
			a = a->ifa_next;
			continue;
		}
		r.push_back(KInetInterface(QString::fromUtf8(a->ifa_name),
					   convertFlags(a->ifa_flags),
					   createAddress(a->ifa_addr),
					   createAddress(a->ifa_netmask),
					   (a->ifa_flags & IFF_BROADCAST) ?
					   createAddress(a->ifa_broadaddr) : 0,
					   (a->ifa_flags & IFF_POINTOPOINT) ? 
					   createAddress(a->ifa_dstaddr) : 0));
		a = a->ifa_next;
	}
	
	kde_freeifaddrs(ads);
	return r;
}