~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to iris/irisnet/netinterface_unix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006  Justin Karneges
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
// this code assumes the following ioctls work:
 
22
//   SIOCGIFCONF  - get list of devices
 
23
//   SIOCGIFFLAGS - get flags about a device
 
24
 
 
25
// gateway detection currently only works on linux
 
26
 
 
27
#include "irisnetplugin.h"
 
28
 
 
29
#include <errno.h>
 
30
#include <sys/types.h>
 
31
#include <sys/socket.h>
 
32
#include <sys/ioctl.h>
 
33
#include <net/if.h>
 
34
#include <net/route.h>
 
35
#include <netinet/in.h>
 
36
 
 
37
// for solaris
 
38
#ifndef SIOCGIFCONF
 
39
# include<sys/sockio.h>
 
40
#endif
 
41
 
 
42
class UnixIface
 
43
{
 
44
public:
 
45
        QString name;
 
46
        bool loopback;
 
47
        QHostAddress address;
 
48
};
 
49
 
 
50
class UnixGateway
 
51
{
 
52
public:
 
53
        QString ifaceName;
 
54
        QHostAddress address;
 
55
};
 
56
 
 
57
static QList<UnixIface> get_sioc_ifaces()
 
58
{
 
59
        QList<UnixIface> out;
 
60
 
 
61
        int tmpsock = socket(AF_INET, SOCK_DGRAM, 0);
 
62
        if(tmpsock < 0)
 
63
                return out;
 
64
 
 
65
        struct ifconf ifc;
 
66
        int lastlen = 0;
 
67
        QByteArray buf(100 * sizeof(struct ifreq), 0); // guess
 
68
        while(1)
 
69
        {
 
70
                ifc.ifc_len = buf.size();
 
71
                ifc.ifc_buf = buf.data();
 
72
                if(ioctl(tmpsock, SIOCGIFCONF, &ifc) < 0)
 
73
                {
 
74
                        if(errno != EINVAL || lastlen != 0)
 
75
                                return out;
 
76
                }
 
77
                else
 
78
                {
 
79
                        // if it didn't grow since last time, then
 
80
                        //   there's no overflow
 
81
                        if(ifc.ifc_len == lastlen)
 
82
                                break;
 
83
                        lastlen = ifc.ifc_len;
 
84
                }
 
85
                buf.resize(buf.size() + 10 * sizeof(struct ifreq));
 
86
        }
 
87
        buf.resize(lastlen);
 
88
 
 
89
        int itemsize;
 
90
        for(int at = 0; at < buf.size(); at += itemsize)
 
91
        {
 
92
                struct ifreq *ifr = (struct ifreq *)(buf.data() + at);
 
93
 
 
94
                int sockaddr_len;
 
95
                if(((struct sockaddr *)&ifr->ifr_addr)->sa_family == AF_INET)
 
96
                        sockaddr_len = sizeof(struct sockaddr_in);
 
97
                else if(((struct sockaddr *)&ifr->ifr_addr)->sa_family == AF_INET6)
 
98
                        sockaddr_len = sizeof(struct sockaddr_in6);
 
99
                else
 
100
                        sockaddr_len = sizeof(struct sockaddr);
 
101
 
 
102
                // set this asap so the next iteration is possible
 
103
                itemsize = sizeof(ifr->ifr_name) + sockaddr_len;
 
104
 
 
105
                // skip if the family is 0 (sometimes you get empty entries)
 
106
                if(ifr->ifr_addr.sa_family == 0)
 
107
                        continue;
 
108
 
 
109
                // make a copy of this item to do additional ioctls on
 
110
                struct ifreq ifrcopy = *ifr;
 
111
 
 
112
                // grab the flags
 
113
                if(ioctl(tmpsock, SIOCGIFFLAGS, &ifrcopy) < 0)
 
114
                        continue;
 
115
 
 
116
                // device must be up and not loopback
 
117
                if(!(ifrcopy.ifr_flags & IFF_UP))
 
118
                        continue;
 
119
 
 
120
                UnixIface i;
 
121
                i.name = QString::fromLatin1(ifr->ifr_name);
 
122
                i.loopback = (ifrcopy.ifr_flags & IFF_LOOPBACK) ? true : false;
 
123
                i.address.setAddress(&ifr->ifr_addr);
 
124
                out += i;
 
125
        }
 
126
 
 
127
        // don't need this anymore
 
128
        close(tmpsock);
 
129
 
 
130
        return out;
 
131
}
 
132
 
 
133
static QStringList read_proc_as_lines(const char *procfile)
 
134
{
 
135
        QStringList out;
 
136
 
 
137
        FILE *f = fopen(procfile, "r");
 
138
        if(!f)
 
139
                return out;
 
140
 
 
141
        QByteArray buf;
 
142
        while(!feof(f))
 
143
        {
 
144
                // max read on a proc is 4K
 
145
                QByteArray block(4096, 0);
 
146
                int ret = fread(block.data(), 1, block.size(), f);
 
147
                if(ret <= 0)
 
148
                        break;
 
149
                block.resize(ret);
 
150
                buf += block;
 
151
        }
 
152
        fclose(f);
 
153
 
 
154
        QString str = QString::fromLocal8Bit(buf);
 
155
        out = str.split('\n', QString::SkipEmptyParts);
 
156
        return out;
 
157
}
 
158
 
 
159
static QHostAddress linux_ipv6_to_qaddr(const QString &in)
 
160
{
 
161
        QHostAddress out;
 
162
        if(in.length() != 32)
 
163
                return out;
 
164
        quint8 raw[16];
 
165
        for(int n = 0; n < 16; ++n)
 
166
        {
 
167
                bool ok;
 
168
                int x = in.mid(n * 2, 2).toInt(&ok, 16);
 
169
                if(!ok)
 
170
                        return out;
 
171
                raw[n] = (quint8)x;
 
172
        }
 
173
        out.setAddress(raw);
 
174
        return out;
 
175
}
 
176
 
 
177
static QHostAddress linux_ipv4_to_qaddr(const QString &in)
 
178
{
 
179
        QHostAddress out;
 
180
        if(in.length() != 8)
 
181
                return out;
 
182
        quint32 raw;
 
183
        unsigned char *rawp = (unsigned char *)&raw;
 
184
        for(int n = 0; n < 4; ++n)
 
185
        {
 
186
                bool ok;
 
187
                int x = in.mid(n * 2, 2).toInt(&ok, 16);
 
188
                if(!ok)
 
189
                        return out;
 
190
                rawp[n] = (unsigned char )x;
 
191
        }
 
192
        out.setAddress(raw);
 
193
        return out;
 
194
}
 
195
 
 
196
static QList<UnixIface> get_linux_ipv6_ifaces()
 
197
{
 
198
        QList<UnixIface> out;
 
199
 
 
200
        QStringList lines = read_proc_as_lines("/proc/net/if_inet6");
 
201
        for(int n = 0; n < lines.count(); ++n)
 
202
        {
 
203
                const QString &line = lines[n];
 
204
                QStringList parts = line.simplified().split(' ', QString::SkipEmptyParts);
 
205
                if(parts.count() < 6)
 
206
                        continue;
 
207
 
 
208
                QString name = parts[5];
 
209
                if(name.isEmpty())
 
210
                        continue;
 
211
                QHostAddress addr = linux_ipv6_to_qaddr(parts[0]);
 
212
                if(addr.isNull())
 
213
                        continue;
 
214
 
 
215
                QString scopestr = parts[3];
 
216
                bool ok;
 
217
                unsigned int scope = parts[3].toInt(&ok, 16);
 
218
                if(!ok)
 
219
                        continue;
 
220
 
 
221
                // IPV6_ADDR_LOOPBACK    0x0010U
 
222
                // IPV6_ADDR_SCOPE_MASK  0x00f0U
 
223
                bool loopback = false;
 
224
                if((scope & 0x00f0U) == 0x0010U)
 
225
                        loopback = true;
 
226
 
 
227
                UnixIface i;
 
228
                i.name = name;
 
229
                i.loopback = loopback;
 
230
                i.address = addr;
 
231
                out += i;
 
232
        }
 
233
 
 
234
        return out;
 
235
}
 
236
 
 
237
static QList<UnixGateway> get_linux_gateways()
 
238
{
 
239
        QList<UnixGateway> out;
 
240
 
 
241
        QStringList lines = read_proc_as_lines("/proc/net/route");
 
242
        // skip the first line, so we start at 1
 
243
        for(int n = 1; n < lines.count(); ++n)
 
244
        {
 
245
                const QString &line = lines[n];
 
246
                QStringList parts = line.simplified().split(' ', QString::SkipEmptyParts);
 
247
                if(parts.count() < 10) // net-tools does 10, but why not 11?
 
248
                        continue;
 
249
 
 
250
                QHostAddress addr = linux_ipv4_to_qaddr(parts[2]);
 
251
                if(addr.isNull())
 
252
                        continue;
 
253
 
 
254
                int iflags = parts[3].toInt(0, 16);
 
255
                if(!(iflags & RTF_UP))
 
256
                        continue;
 
257
 
 
258
                if(!(iflags & RTF_GATEWAY))
 
259
                        continue;
 
260
 
 
261
                UnixGateway g;
 
262
                g.ifaceName = parts[0];
 
263
                g.address = addr;
 
264
                out += g;
 
265
        }
 
266
 
 
267
        lines = read_proc_as_lines("/proc/net/ipv6_route");
 
268
        for(int n = 0; n < lines.count(); ++n)
 
269
        {
 
270
                const QString &line = lines[n];
 
271
                QStringList parts = line.simplified().split(' ', QString::SkipEmptyParts);
 
272
                if(parts.count() < 10)
 
273
                        continue;
 
274
 
 
275
                QHostAddress addr = linux_ipv6_to_qaddr(parts[4]);
 
276
                if(addr.isNull())
 
277
                        continue;
 
278
 
 
279
                int iflags = parts[8].toInt(0, 16);
 
280
                if(!(iflags & RTF_UP))
 
281
                        continue;
 
282
 
 
283
                if(!(iflags & RTF_GATEWAY))
 
284
                        continue;
 
285
 
 
286
                UnixGateway g;
 
287
                g.ifaceName = parts[9];
 
288
                g.address = addr;
 
289
                out += g;
 
290
        }
 
291
 
 
292
        return out;
 
293
}
 
294
 
 
295
static QList<UnixIface> get_unix_ifaces()
 
296
{
 
297
        QList<UnixIface> out = get_sioc_ifaces();
 
298
#ifdef Q_OS_LINUX
 
299
        out += get_linux_ipv6_ifaces();
 
300
#endif
 
301
        return out;
 
302
}
 
303
 
 
304
static QList<UnixGateway> get_unix_gateways()
 
305
{
 
306
        // support other platforms here
 
307
        QList<UnixGateway> out;
 
308
#ifdef Q_OS_LINUX
 
309
        out = get_linux_gateways();
 
310
#endif
 
311
        return out;
 
312
}
 
313
 
 
314
namespace XMPP {
 
315
 
 
316
class UnixNet : public NetInterfaceProvider
 
317
{
 
318
        Q_OBJECT
 
319
        Q_INTERFACES(XMPP::NetInterfaceProvider);
 
320
public:
 
321
        QList<Info> info;
 
322
        QTimer t;
 
323
 
 
324
        UnixNet() : t(this)
 
325
        {
 
326
                connect(&t, SIGNAL(timeout()), SLOT(check()));
 
327
        }
 
328
 
 
329
        void start()
 
330
        {
 
331
                t.start(5000);
 
332
                poll();
 
333
        }
 
334
 
 
335
        QList<Info> interfaces() const
 
336
        {
 
337
                return info;
 
338
        }
 
339
 
 
340
        void poll()
 
341
        {
 
342
                QList<Info> ifaces;
 
343
 
 
344
                QList<UnixIface> list = get_unix_ifaces();
 
345
                for(int n = 0; n < list.count(); ++n)
 
346
                {
 
347
                        // see if we have it already
 
348
                        int lookup = -1;
 
349
                        for(int k = 0; k < ifaces.count(); ++k)
 
350
                        {
 
351
                                if(ifaces[k].id == list[n].name)
 
352
                                {
 
353
                                        lookup = k;
 
354
                                        break;
 
355
                                }
 
356
                        }
 
357
 
 
358
                        // don't have it?  make it
 
359
                        if(lookup == -1)
 
360
                        {
 
361
                                Info i;
 
362
                                i.id = list[n].name;
 
363
                                i.name = list[n].name;
 
364
                                i.isLoopback = list[n].loopback;
 
365
                                i.addresses += list[n].address;
 
366
                                ifaces += i;
 
367
                        }
 
368
                        // otherwise, tack on the address
 
369
                        else
 
370
                                ifaces[lookup].addresses += list[n].address;
 
371
                }
 
372
 
 
373
                QList<UnixGateway> glist = get_unix_gateways();
 
374
                for(int n = 0; n < glist.count(); ++n)
 
375
                {
 
376
                        // look up the interface
 
377
                        int lookup = -1;
 
378
                        for(int k = 0; k < ifaces.count(); ++k)
 
379
                        {
 
380
                                if(ifaces[k].id == glist[n].ifaceName)
 
381
                                {
 
382
                                        lookup = k;
 
383
                                        break;
 
384
                                }
 
385
                        }
 
386
 
 
387
                        if(lookup == -1)
 
388
                                break;
 
389
 
 
390
                        ifaces[lookup].gateway = glist[n].address;
 
391
                }
 
392
 
 
393
                info = ifaces;
 
394
        }
 
395
 
 
396
signals:
 
397
        void updated();
 
398
 
 
399
public slots:
 
400
        void check()
 
401
        {
 
402
                poll();
 
403
                emit updated();
 
404
        }
 
405
};
 
406
 
 
407
class UnixNetProvider : public IrisNetProvider
 
408
{
 
409
        Q_OBJECT
 
410
        Q_INTERFACES(XMPP::IrisNetProvider);
 
411
public:
 
412
        virtual NetInterfaceProvider *createNetInterfaceProvider()
 
413
        {
 
414
                return new UnixNet;
 
415
        }
 
416
};
 
417
 
 
418
IrisNetProvider *irisnet_createUnixNetProvider()
 
419
{
 
420
        return new UnixNetProvider;
 
421
}
 
422
 
 
423
}
 
424
 
 
425
#include "netinterface_unix.moc"