2
* Copyright (C) 2006 Justin Karneges
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.
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.
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
21
#ifndef NETINTERFACE_H
22
#define NETINTERFACE_H
24
#include "irisnetglobal.h"
28
class NetInterfaceManager;
29
class NetInterfacePrivate;
30
class NetInterfaceManagerPrivate;
33
\brief Provides information about a network interface
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.
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().
39
Here's an example of how to print the IP addresses of eth0:
41
NetInterface iface("eth0");
44
QList<QHostAddress> addrs = iface.addresses();
45
for(int n = 0; n < addrs.count(); ++n)
46
printf("%s\n", qPrintable(addrs[n].toString()));
50
If the interface goes away, the unavailable() signal is emitted and the NetInterface becomes invalid.
52
\sa NetInterfaceManager
54
class IRISNET_EXPORT NetInterface : public QObject
59
\brief Constructs a new interface object with the given \a id and \a manager
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.
65
NetInterface(const QString &id, NetInterfaceManager *manager);
68
\brief Destroys the interface object
73
\brief Returns true if the interface is valid, otherwise returns false
80
\brief Returns the id of this interface
82
This is the id that was passed in the constructor.
87
\brief Returns a display-friendly name of this interface
89
The name may be the same as the id.
96
\brief Returns the addresses of this interface
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.
100
QList<QHostAddress> addresses() const;
103
\brief Returns the gateway of this interface
105
If there is no gateway associated with this interface, a null QHostAddress is returned.
107
QHostAddress gateway() const; // optional
111
\brief Notifies when the interface becomes unavailable
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.
115
\note If the interface information changes, the interface is considered to have become unavailable.
122
friend class NetInterfacePrivate;
123
NetInterfacePrivate *d;
125
friend class NetInterfaceManagerPrivate;
129
\brief Manages network interface information
131
NetInterfaceManager keeps track of all available network interfaces.
133
An interface is considered available if it exists, is "Up", has at least one IP address, and is non-Loopback.
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:
138
NetInterfaceManager netman;
139
QStringList id_list = netman.interfaces();
140
for(int n = 0; n < id_list.count(); ++n)
142
NetInterface iface(id_list[n], &netman);
143
printf("name: [%s]\n", qPrintable(iface.name()));
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.
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().
153
class IRISNET_EXPORT NetInterfaceManager : public QObject
158
\brief Constructs a new manager object with the given \a parent
160
NetInterfaceManager(QObject *parent = 0);
163
\brief Destroys the manager object
165
~NetInterfaceManager();
168
\brief Returns the list of available interface ids
170
\sa interfaceAvailable
171
\sa interfaceForAddress
173
QStringList interfaces() const;
176
\brief Looks up an interface id by IP address
178
This function looks for an interface that has the address \a a. If there is no such interface, a null string is returned.
180
This is useful for determing the network interface associated with an outgoing QTcpSocket:
183
QString iface = NetInterfaceManager::interfaceForAddress(tcpSocket->localAddress());
188
static QString interfaceForAddress(const QHostAddress &a);
192
\brief Notifies when an interface becomes available
194
The \a id parameter is the interface id, ready to use with NetInterface.
196
void interfaceAvailable(const QString &id);
199
friend class NetInterfaceManagerPrivate;
200
NetInterfaceManagerPrivate *d;
202
friend class NetInterface;
203
friend class NetInterfacePrivate;
205
void *reg(const QString &id, NetInterface *i);
206
void unreg(NetInterface *i);