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

« back to all changes in this revision

Viewing changes to iris/src/irisnet/corelib/netinterface.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

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
#ifndef NETINTERFACE_H
 
22
#define NETINTERFACE_H
 
23
 
 
24
#include "irisnetglobal.h"
 
25
 
 
26
namespace XMPP {
 
27
 
 
28
class NetInterfaceManager;
 
29
class NetInterfacePrivate;
 
30
class NetInterfaceManagerPrivate;
 
31
 
 
32
/**
 
33
   \brief Provides information about a network interface
 
34
 
 
35
   NetInterface provides information about a particular network interface.  Construct it by passing the interface id of interest (e.g. "eth0") and a NetInterfaceManager parent object.  Interface ids can be obtained from NetInterfaceManager.
 
36
 
 
37
   To test if a NetInterface is valid, call isValid().  Use name() to return a display-friendly name of the interface.  The addresses() function returns a list of IP addresses for this interface.  There may be a gateway IP address associated with this interface, which can be fetched with gateway().
 
38
 
 
39
   Here's an example of how to print the IP addresses of eth0:
 
40
   \code
 
41
NetInterface iface("eth0");
 
42
if(iface.isValid())
 
43
{
 
44
        QList<QHostAddress> addrs = iface.addresses();
 
45
        for(int n = 0; n < addrs.count(); ++n)
 
46
                printf("%s\n", qPrintable(addrs[n].toString()));
 
47
}
 
48
   \endcode
 
49
 
 
50
   If the interface goes away, the unavailable() signal is emitted and the NetInterface becomes invalid.
 
51
 
 
52
   \sa NetInterfaceManager
 
53
*/
 
54
class IRISNET_EXPORT NetInterface : public QObject
 
55
{
 
56
        Q_OBJECT
 
57
public:
 
58
        /**
 
59
           \brief Constructs a new interface object with the given \a id and \a manager
 
60
 
 
61
           If \a id is not a valid interface id, then the object will not be valid (isValid() will return false).  Normally it is not necessary to check for validity, since interface ids obtained from NetInterfaceManager are guaranteed to be valid until the event loop resumes.
 
62
 
 
63
           \sa isValid
 
64
        */
 
65
        NetInterface(const QString &id, NetInterfaceManager *manager);
 
66
 
 
67
        /**
 
68
           \brief Destroys the interface object
 
69
        */
 
70
        ~NetInterface();
 
71
 
 
72
        /**
 
73
           \brief Returns true if the interface is valid, otherwise returns false
 
74
 
 
75
           \sa unavailable
 
76
        */
 
77
        bool isValid() const;
 
78
 
 
79
        /**
 
80
           \brief Returns the id of this interface
 
81
 
 
82
           This is the id that was passed in the constructor.
 
83
        */
 
84
        QString id() const;
 
85
 
 
86
        /**
 
87
           \brief Returns a display-friendly name of this interface
 
88
 
 
89
           The name may be the same as the id.
 
90
 
 
91
           \sa id
 
92
        */
 
93
        QString name() const;
 
94
 
 
95
        /**
 
96
           \brief Returns the addresses of this interface
 
97
 
 
98
           There will always be at least one address.  In some cases there might be multiple, such as on Unix where it is possible for the same interface to have both an IPv4 and an IPv6 address.
 
99
        */
 
100
        QList<QHostAddress> addresses() const;
 
101
 
 
102
        /**
 
103
           \brief Returns the gateway of this interface
 
104
 
 
105
           If there is no gateway associated with this interface, a null QHostAddress is returned.
 
106
        */
 
107
        QHostAddress gateway() const; // optional
 
108
 
 
109
signals:
 
110
        /**
 
111
           \brief Notifies when the interface becomes unavailable
 
112
 
 
113
           Once this signal is emitted, the NetInterface object becomes invalid and is no longer very useful.  A new NetInterface object must be created if a valid object with current information is desired.
 
114
 
 
115
           \note If the interface information changes, the interface is considered to have become unavailable.
 
116
 
 
117
           \sa isValid
 
118
        */
 
119
        void unavailable();
 
120
 
 
121
private:
 
122
        friend class NetInterfacePrivate;
 
123
        NetInterfacePrivate *d;
 
124
 
 
125
        friend class NetInterfaceManagerPrivate;
 
126
};
 
127
 
 
128
/**
 
129
   \brief Manages network interface information
 
130
 
 
131
   NetInterfaceManager keeps track of all available network interfaces.
 
132
 
 
133
   An interface is considered available if it exists, is "Up", has at least one IP address, and is non-Loopback.
 
134
 
 
135
   The interfaces() function returns a list of available interface ids.  These ids can be used with NetInterface to get information about the interfaces.  For example, here is how you could print the names of the available interfaces:
 
136
 
 
137
   \code
 
138
NetInterfaceManager netman;
 
139
QStringList id_list = netman.interfaces();
 
140
for(int n = 0; n < id_list.count(); ++n)
 
141
{
 
142
        NetInterface iface(id_list[n], &netman);
 
143
        printf("name: [%s]\n", qPrintable(iface.name()));
 
144
}
 
145
   \endcode
 
146
 
 
147
   When a new network interface is available, the interfaceAvailable() signal will be emitted.  Note that interface unavailability is not notified by NetInterfaceManager.  Instead, use NetInterface to monitor a specific network interface for unavailability.
 
148
 
 
149
   Interface ids obtained through NetInterfaceManager are guaranteed to be valid until the event loop resumes, or until the next call to interfaces() or interfaceForAddress().
 
150
 
 
151
   \sa NetInterface
 
152
*/
 
153
class IRISNET_EXPORT NetInterfaceManager : public QObject
 
154
{
 
155
        Q_OBJECT
 
156
public:
 
157
        /**
 
158
           \brief Constructs a new manager object with the given \a parent
 
159
        */
 
160
        NetInterfaceManager(QObject *parent = 0);
 
161
 
 
162
        /**
 
163
           \brief Destroys the manager object
 
164
        */
 
165
        ~NetInterfaceManager();
 
166
 
 
167
        /**
 
168
           \brief Returns the list of available interface ids
 
169
 
 
170
           \sa interfaceAvailable
 
171
           \sa interfaceForAddress
 
172
        */
 
173
        QStringList interfaces() const;
 
174
 
 
175
        /**
 
176
           \brief Looks up an interface id by IP address
 
177
 
 
178
           This function looks for an interface that has the address \a a.  If there is no such interface, a null string is returned.
 
179
 
 
180
           This is useful for determing the network interface associated with an outgoing QTcpSocket:
 
181
 
 
182
           \code
 
183
QString iface = NetInterfaceManager::interfaceForAddress(tcpSocket->localAddress());
 
184
           \endcode
 
185
 
 
186
           \sa interfaces
 
187
        */
 
188
        static QString interfaceForAddress(const QHostAddress &a);
 
189
 
 
190
signals:
 
191
        /**
 
192
           \brief Notifies when an interface becomes available
 
193
 
 
194
           The \a id parameter is the interface id, ready to use with NetInterface.
 
195
        */
 
196
        void interfaceAvailable(const QString &id);
 
197
 
 
198
private:
 
199
        friend class NetInterfaceManagerPrivate;
 
200
        NetInterfaceManagerPrivate *d;
 
201
 
 
202
        friend class NetInterface;
 
203
        friend class NetInterfacePrivate;
 
204
 
 
205
        void *reg(const QString &id, NetInterface *i);
 
206
        void unreg(NetInterface *i);
 
207
};
 
208
 
 
209
}
 
210
 
 
211
#endif